aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2016-05-21 01:20:48 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-05-21 01:20:48 -0400
commit2f37dd131c5d3a2eac21cd5baf80658b1b02a8ac (patch)
treee0f191b15865268e694c02f1f02cbc26a168ddf9 /drivers/iio
parent3aa2fc1667acdd9cca816a2bc9529f494bd61b05 (diff)
parentffc83a79b44e02995ab5e93af07e26f6c7243c53 (diff)
Merge tag 'staging-4.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging
Pull staging and IIO driver updates from Greg KH: "Here's the big staging and iio driver update for 4.7-rc1. I think we almost broke even with this release, only adding a few more lines than we removed, which isn't bad overall given that there's a bunch of new iio drivers added. The Lustre developers seem to have woken up from their sleep and have been doing a great job in cleaning up the code and pruning unused or old cruft, the filesystem is almost readable :) Other than that, just a lot of basic coding style cleanups in the churn. All have been in linux-next for a while with no reported issues" * tag 'staging-4.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging: (938 commits) Staging: emxx_udc: emxx_udc: fixed coding style issue staging/gdm724x: fix "alignment should match open parenthesis" issues staging/gdm724x: Fix avoid CamelCase staging: unisys: rename misleading var ii with frag staging: unisys: visorhba: switch success handling to error handling staging: unisys: visorhba: main path needs to flow down the left margin staging: unisys: visorinput: handle_locking_key() simplifications staging: unisys: visorhba: fail gracefully for thread creation failures staging: unisys: visornic: comment restructuring and removing bad diction staging: unisys: fix format string %Lx to %llx for u64 staging: unisys: remove unused struct members staging: unisys: visorchannel: correct variable misspelling staging: unisys: visorhba: replace functionlike macro with function staging: dgnc: Need to check for NULL of ch staging: dgnc: remove redundant condition check staging: dgnc: fix 'line over 80 characters' staging: dgnc: clean up the dgnc_get_modem_info() staging: lustre: lnet: enable configuration per NI interface staging: lustre: o2iblnd: properly set ibr_why staging: lustre: o2iblnd: remove last of kiblnd_tunables_fini ...
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/accel/Kconfig5
-rw-r--r--drivers/iio/accel/bmc150-accel-core.c127
-rw-r--r--drivers/iio/accel/bmc150-accel-i2c.c7
-rw-r--r--drivers/iio/accel/bmc150-accel-spi.c8
-rw-r--r--drivers/iio/accel/bmc150-accel.h1
-rw-r--r--drivers/iio/accel/kxcjk-1013.c25
-rw-r--r--drivers/iio/accel/mma7455_core.c5
-rw-r--r--drivers/iio/accel/mma8452.c188
-rw-r--r--drivers/iio/accel/mma9553.c1
-rw-r--r--drivers/iio/accel/mxc4005.c29
-rw-r--r--drivers/iio/accel/st_accel.h1
-rw-r--r--drivers/iio/accel/st_accel_core.c105
-rw-r--r--drivers/iio/accel/st_accel_i2c.c4
-rw-r--r--drivers/iio/accel/stk8312.c1
-rw-r--r--drivers/iio/accel/stk8ba50.c1
-rw-r--r--drivers/iio/adc/Kconfig16
-rw-r--r--drivers/iio/adc/Makefile1
-rw-r--r--drivers/iio/adc/ad799x.c2
-rw-r--r--drivers/iio/adc/at91-sama5d2_adc.c102
-rw-r--r--drivers/iio/adc/at91_adc.c8
-rw-r--r--drivers/iio/adc/ina2xx-adc.c43
-rw-r--r--drivers/iio/adc/lpc18xx_adc.c231
-rw-r--r--drivers/iio/adc/mcp3422.c6
-rw-r--r--drivers/iio/adc/mxs-lradc.c37
-rw-r--r--drivers/iio/adc/rockchip_saradc.c19
-rw-r--r--drivers/iio/adc/ti-adc081c.c118
-rw-r--r--drivers/iio/adc/vf610_adc.c24
-rw-r--r--drivers/iio/common/hid-sensors/hid-sensor-trigger.c2
-rw-r--r--drivers/iio/common/ms_sensors/ms_sensors_i2c.c2
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_buffer.c97
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_core.c20
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_trigger.c13
-rw-r--r--drivers/iio/dac/Kconfig37
-rw-r--r--drivers/iio/dac/Makefile4
-rw-r--r--drivers/iio/dac/ad5592r-base.c691
-rw-r--r--drivers/iio/dac/ad5592r-base.h76
-rw-r--r--drivers/iio/dac/ad5592r.c164
-rw-r--r--drivers/iio/dac/ad5593r.c131
-rw-r--r--drivers/iio/dac/lpc18xx_dac.c210
-rw-r--r--drivers/iio/frequency/ad9523.c19
-rw-r--r--drivers/iio/gyro/Kconfig2
-rw-r--r--drivers/iio/gyro/bmg160_core.c137
-rw-r--r--drivers/iio/gyro/st_gyro.h1
-rw-r--r--drivers/iio/gyro/st_gyro_core.c4
-rw-r--r--drivers/iio/gyro/st_gyro_i2c.c5
-rw-r--r--drivers/iio/gyro/st_gyro_spi.c1
-rw-r--r--drivers/iio/humidity/Kconfig10
-rw-r--r--drivers/iio/humidity/Makefile1
-rw-r--r--drivers/iio/humidity/am2315.c303
-rw-r--r--drivers/iio/humidity/dht11.c40
-rw-r--r--drivers/iio/imu/Kconfig2
-rw-r--r--drivers/iio/imu/Makefile1
-rw-r--r--drivers/iio/imu/adis.c7
-rw-r--r--drivers/iio/imu/bmi160/Kconfig32
-rw-r--r--drivers/iio/imu/bmi160/Makefile6
-rw-r--r--drivers/iio/imu/bmi160/bmi160.h10
-rw-r--r--drivers/iio/imu/bmi160/bmi160_core.c596
-rw-r--r--drivers/iio/imu/bmi160/bmi160_i2c.c72
-rw-r--r--drivers/iio/imu/bmi160/bmi160_spi.c63
-rw-r--r--drivers/iio/imu/inv_mpu6050/Kconfig10
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_core.c73
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c3
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h16
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c20
-rw-r--r--drivers/iio/imu/kmx61.c1
-rw-r--r--drivers/iio/industrialio-core.c123
-rw-r--r--drivers/iio/inkern.c86
-rw-r--r--drivers/iio/light/Kconfig32
-rw-r--r--drivers/iio/light/Makefile3
-rw-r--r--drivers/iio/light/apds9960.c13
-rw-r--r--drivers/iio/light/bh1780.c297
-rw-r--r--drivers/iio/light/max44000.c639
-rw-r--r--drivers/iio/light/stk3310.c1
-rw-r--r--drivers/iio/light/tsl2563.c3
-rw-r--r--drivers/iio/light/veml6070.c218
-rw-r--r--drivers/iio/magnetometer/Kconfig33
-rw-r--r--drivers/iio/magnetometer/Makefile3
-rw-r--r--drivers/iio/magnetometer/ak8975.c232
-rw-r--r--drivers/iio/magnetometer/bmc150_magn.c156
-rw-r--r--drivers/iio/magnetometer/bmc150_magn.h11
-rw-r--r--drivers/iio/magnetometer/bmc150_magn_i2c.c77
-rw-r--r--drivers/iio/magnetometer/bmc150_magn_spi.c68
-rw-r--r--drivers/iio/magnetometer/st_magn_core.c1
-rw-r--r--drivers/iio/potentiometer/Kconfig28
-rw-r--r--drivers/iio/potentiometer/Makefile2
-rw-r--r--drivers/iio/potentiometer/ds1803.c173
-rw-r--r--drivers/iio/potentiometer/mcp4131.c494
-rw-r--r--drivers/iio/potentiometer/mcp4531.c13
-rw-r--r--drivers/iio/potentiometer/tpl0102.c2
-rw-r--r--drivers/iio/pressure/Kconfig28
-rw-r--r--drivers/iio/pressure/Makefile2
-rw-r--r--drivers/iio/pressure/bmp280.c564
-rw-r--r--drivers/iio/pressure/hp03.c312
-rw-r--r--drivers/iio/pressure/hp206c.c426
-rw-r--r--drivers/iio/pressure/ms5611.h23
-rw-r--r--drivers/iio/pressure/ms5611_core.c148
-rw-r--r--drivers/iio/pressure/ms5611_i2c.c25
-rw-r--r--drivers/iio/pressure/ms5611_spi.c34
-rw-r--r--drivers/iio/pressure/st_pressure_core.c10
99 files changed, 7634 insertions, 643 deletions
diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index b0d3ecf3318b..e4a758cd7d35 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -64,7 +64,7 @@ config IIO_ST_ACCEL_3AXIS
64 help 64 help
65 Say yes here to build support for STMicroelectronics accelerometers: 65 Say yes here to build support for STMicroelectronics accelerometers:
66 LSM303DLH, LSM303DLHC, LIS3DH, LSM330D, LSM330DL, LSM330DLC, 66 LSM303DLH, LSM303DLHC, LIS3DH, LSM330D, LSM330DL, LSM330DLC,
67 LIS331DLH, LSM303DL, LSM303DLM, LSM330, LIS2DH12. 67 LIS331DLH, LSM303DL, LSM303DLM, LSM330, LIS2DH12, H3LIS331DL.
68 68
69 This driver can also be built as a module. If so, these modules 69 This driver can also be built as a module. If so, these modules
70 will be created: 70 will be created:
@@ -143,7 +143,8 @@ config MMA8452
143 select IIO_TRIGGERED_BUFFER 143 select IIO_TRIGGERED_BUFFER
144 help 144 help
145 Say yes here to build support for the following Freescale 3-axis 145 Say yes here to build support for the following Freescale 3-axis
146 accelerometers: MMA8451Q, MMA8452Q, MMA8453Q, MMA8652FC, MMA8653FC. 146 accelerometers: MMA8451Q, MMA8452Q, MMA8453Q, MMA8652FC, MMA8653FC,
147 FXLS8471Q.
147 148
148 To compile this driver as a module, choose M here: the module 149 To compile this driver as a module, choose M here: the module
149 will be called mma8452. 150 will be called mma8452.
diff --git a/drivers/iio/accel/bmc150-accel-core.c b/drivers/iio/accel/bmc150-accel-core.c
index 2072a31e813b..197e693e7e7b 100644
--- a/drivers/iio/accel/bmc150-accel-core.c
+++ b/drivers/iio/accel/bmc150-accel-core.c
@@ -25,7 +25,6 @@
25#include <linux/delay.h> 25#include <linux/delay.h>
26#include <linux/slab.h> 26#include <linux/slab.h>
27#include <linux/acpi.h> 27#include <linux/acpi.h>
28#include <linux/gpio/consumer.h>
29#include <linux/pm.h> 28#include <linux/pm.h>
30#include <linux/pm_runtime.h> 29#include <linux/pm_runtime.h>
31#include <linux/iio/iio.h> 30#include <linux/iio/iio.h>
@@ -138,6 +137,7 @@ enum bmc150_accel_axis {
138 AXIS_X, 137 AXIS_X,
139 AXIS_Y, 138 AXIS_Y,
140 AXIS_Z, 139 AXIS_Z,
140 AXIS_MAX,
141}; 141};
142 142
143enum bmc150_power_modes { 143enum bmc150_power_modes {
@@ -188,7 +188,6 @@ enum bmc150_accel_trigger_id {
188 188
189struct bmc150_accel_data { 189struct bmc150_accel_data {
190 struct regmap *regmap; 190 struct regmap *regmap;
191 struct device *dev;
192 int irq; 191 int irq;
193 struct bmc150_accel_interrupt interrupts[BMC150_ACCEL_INTERRUPTS]; 192 struct bmc150_accel_interrupt interrupts[BMC150_ACCEL_INTERRUPTS];
194 atomic_t active_intr; 193 atomic_t active_intr;
@@ -246,16 +245,18 @@ static const struct {
246 {500000, BMC150_ACCEL_SLEEP_500_MS}, 245 {500000, BMC150_ACCEL_SLEEP_500_MS},
247 {1000000, BMC150_ACCEL_SLEEP_1_SEC} }; 246 {1000000, BMC150_ACCEL_SLEEP_1_SEC} };
248 247
249static const struct regmap_config bmc150_i2c_regmap_conf = { 248const struct regmap_config bmc150_regmap_conf = {
250 .reg_bits = 8, 249 .reg_bits = 8,
251 .val_bits = 8, 250 .val_bits = 8,
252 .max_register = 0x3f, 251 .max_register = 0x3f,
253}; 252};
253EXPORT_SYMBOL_GPL(bmc150_regmap_conf);
254 254
255static int bmc150_accel_set_mode(struct bmc150_accel_data *data, 255static int bmc150_accel_set_mode(struct bmc150_accel_data *data,
256 enum bmc150_power_modes mode, 256 enum bmc150_power_modes mode,
257 int dur_us) 257 int dur_us)
258{ 258{
259 struct device *dev = regmap_get_device(data->regmap);
259 int i; 260 int i;
260 int ret; 261 int ret;
261 u8 lpw_bits; 262 u8 lpw_bits;
@@ -279,11 +280,11 @@ static int bmc150_accel_set_mode(struct bmc150_accel_data *data,
279 lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT; 280 lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT;
280 lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT); 281 lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT);
281 282
282 dev_dbg(data->dev, "Set Mode bits %x\n", lpw_bits); 283 dev_dbg(dev, "Set Mode bits %x\n", lpw_bits);
283 284
284 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits); 285 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits);
285 if (ret < 0) { 286 if (ret < 0) {
286 dev_err(data->dev, "Error writing reg_pmu_lpw\n"); 287 dev_err(dev, "Error writing reg_pmu_lpw\n");
287 return ret; 288 return ret;
288 } 289 }
289 290
@@ -316,23 +317,24 @@ static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val,
316 317
317static int bmc150_accel_update_slope(struct bmc150_accel_data *data) 318static int bmc150_accel_update_slope(struct bmc150_accel_data *data)
318{ 319{
320 struct device *dev = regmap_get_device(data->regmap);
319 int ret; 321 int ret;
320 322
321 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_6, 323 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_6,
322 data->slope_thres); 324 data->slope_thres);
323 if (ret < 0) { 325 if (ret < 0) {
324 dev_err(data->dev, "Error writing reg_int_6\n"); 326 dev_err(dev, "Error writing reg_int_6\n");
325 return ret; 327 return ret;
326 } 328 }
327 329
328 ret = regmap_update_bits(data->regmap, BMC150_ACCEL_REG_INT_5, 330 ret = regmap_update_bits(data->regmap, BMC150_ACCEL_REG_INT_5,
329 BMC150_ACCEL_SLOPE_DUR_MASK, data->slope_dur); 331 BMC150_ACCEL_SLOPE_DUR_MASK, data->slope_dur);
330 if (ret < 0) { 332 if (ret < 0) {
331 dev_err(data->dev, "Error updating reg_int_5\n"); 333 dev_err(dev, "Error updating reg_int_5\n");
332 return ret; 334 return ret;
333 } 335 }
334 336
335 dev_dbg(data->dev, "%s: %x %x\n", __func__, data->slope_thres, 337 dev_dbg(dev, "%s: %x %x\n", __func__, data->slope_thres,
336 data->slope_dur); 338 data->slope_dur);
337 339
338 return ret; 340 return ret;
@@ -378,20 +380,21 @@ static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data)
378 380
379static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on) 381static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
380{ 382{
383 struct device *dev = regmap_get_device(data->regmap);
381 int ret; 384 int ret;
382 385
383 if (on) { 386 if (on) {
384 ret = pm_runtime_get_sync(data->dev); 387 ret = pm_runtime_get_sync(dev);
385 } else { 388 } else {
386 pm_runtime_mark_last_busy(data->dev); 389 pm_runtime_mark_last_busy(dev);
387 ret = pm_runtime_put_autosuspend(data->dev); 390 ret = pm_runtime_put_autosuspend(dev);
388 } 391 }
389 392
390 if (ret < 0) { 393 if (ret < 0) {
391 dev_err(data->dev, 394 dev_err(dev,
392 "Failed: bmc150_accel_set_power_state for %d\n", on); 395 "Failed: bmc150_accel_set_power_state for %d\n", on);
393 if (on) 396 if (on)
394 pm_runtime_put_noidle(data->dev); 397 pm_runtime_put_noidle(dev);
395 398
396 return ret; 399 return ret;
397 } 400 }
@@ -445,6 +448,7 @@ static void bmc150_accel_interrupts_setup(struct iio_dev *indio_dev,
445static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i, 448static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i,
446 bool state) 449 bool state)
447{ 450{
451 struct device *dev = regmap_get_device(data->regmap);
448 struct bmc150_accel_interrupt *intr = &data->interrupts[i]; 452 struct bmc150_accel_interrupt *intr = &data->interrupts[i];
449 const struct bmc150_accel_interrupt_info *info = intr->info; 453 const struct bmc150_accel_interrupt_info *info = intr->info;
450 int ret; 454 int ret;
@@ -474,7 +478,7 @@ static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i,
474 ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask, 478 ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask,
475 (state ? info->map_bitmask : 0)); 479 (state ? info->map_bitmask : 0));
476 if (ret < 0) { 480 if (ret < 0) {
477 dev_err(data->dev, "Error updating reg_int_map\n"); 481 dev_err(dev, "Error updating reg_int_map\n");
478 goto out_fix_power_state; 482 goto out_fix_power_state;
479 } 483 }
480 484
@@ -482,7 +486,7 @@ static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i,
482 ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask, 486 ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask,
483 (state ? info->en_bitmask : 0)); 487 (state ? info->en_bitmask : 0));
484 if (ret < 0) { 488 if (ret < 0) {
485 dev_err(data->dev, "Error updating reg_int_en\n"); 489 dev_err(dev, "Error updating reg_int_en\n");
486 goto out_fix_power_state; 490 goto out_fix_power_state;
487 } 491 }
488 492
@@ -500,6 +504,7 @@ out_fix_power_state:
500 504
501static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val) 505static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val)
502{ 506{
507 struct device *dev = regmap_get_device(data->regmap);
503 int ret, i; 508 int ret, i;
504 509
505 for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) { 510 for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) {
@@ -508,8 +513,7 @@ static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val)
508 BMC150_ACCEL_REG_PMU_RANGE, 513 BMC150_ACCEL_REG_PMU_RANGE,
509 data->chip_info->scale_table[i].reg_range); 514 data->chip_info->scale_table[i].reg_range);
510 if (ret < 0) { 515 if (ret < 0) {
511 dev_err(data->dev, 516 dev_err(dev, "Error writing pmu_range\n");
512 "Error writing pmu_range\n");
513 return ret; 517 return ret;
514 } 518 }
515 519
@@ -523,6 +527,7 @@ static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val)
523 527
524static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val) 528static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val)
525{ 529{
530 struct device *dev = regmap_get_device(data->regmap);
526 int ret; 531 int ret;
527 unsigned int value; 532 unsigned int value;
528 533
@@ -530,7 +535,7 @@ static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val)
530 535
531 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value); 536 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value);
532 if (ret < 0) { 537 if (ret < 0) {
533 dev_err(data->dev, "Error reading reg_temp\n"); 538 dev_err(dev, "Error reading reg_temp\n");
534 mutex_unlock(&data->mutex); 539 mutex_unlock(&data->mutex);
535 return ret; 540 return ret;
536 } 541 }
@@ -545,6 +550,7 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
545 struct iio_chan_spec const *chan, 550 struct iio_chan_spec const *chan,
546 int *val) 551 int *val)
547{ 552{
553 struct device *dev = regmap_get_device(data->regmap);
548 int ret; 554 int ret;
549 int axis = chan->scan_index; 555 int axis = chan->scan_index;
550 __le16 raw_val; 556 __le16 raw_val;
@@ -559,7 +565,7 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
559 ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis), 565 ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
560 &raw_val, sizeof(raw_val)); 566 &raw_val, sizeof(raw_val));
561 if (ret < 0) { 567 if (ret < 0) {
562 dev_err(data->dev, "Error reading axis %d\n", axis); 568 dev_err(dev, "Error reading axis %d\n", axis);
563 bmc150_accel_set_power_state(data, false); 569 bmc150_accel_set_power_state(data, false);
564 mutex_unlock(&data->mutex); 570 mutex_unlock(&data->mutex);
565 return ret; 571 return ret;
@@ -831,6 +837,7 @@ static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val)
831static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data, 837static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data,
832 char *buffer, int samples) 838 char *buffer, int samples)
833{ 839{
840 struct device *dev = regmap_get_device(data->regmap);
834 int sample_length = 3 * 2; 841 int sample_length = 3 * 2;
835 int ret; 842 int ret;
836 int total_length = samples * sample_length; 843 int total_length = samples * sample_length;
@@ -854,7 +861,8 @@ static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data,
854 } 861 }
855 862
856 if (ret) 863 if (ret)
857 dev_err(data->dev, "Error transferring data from fifo in single steps of %zu\n", 864 dev_err(dev,
865 "Error transferring data from fifo in single steps of %zu\n",
858 step); 866 step);
859 867
860 return ret; 868 return ret;
@@ -864,6 +872,7 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
864 unsigned samples, bool irq) 872 unsigned samples, bool irq)
865{ 873{
866 struct bmc150_accel_data *data = iio_priv(indio_dev); 874 struct bmc150_accel_data *data = iio_priv(indio_dev);
875 struct device *dev = regmap_get_device(data->regmap);
867 int ret, i; 876 int ret, i;
868 u8 count; 877 u8 count;
869 u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3]; 878 u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3];
@@ -873,7 +882,7 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
873 882
874 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val); 883 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val);
875 if (ret < 0) { 884 if (ret < 0) {
876 dev_err(data->dev, "Error reading reg_fifo_status\n"); 885 dev_err(dev, "Error reading reg_fifo_status\n");
877 return ret; 886 return ret;
878 } 887 }
879 888
@@ -1105,27 +1114,23 @@ static const struct iio_info bmc150_accel_info_fifo = {
1105 .driver_module = THIS_MODULE, 1114 .driver_module = THIS_MODULE,
1106}; 1115};
1107 1116
1117static const unsigned long bmc150_accel_scan_masks[] = {
1118 BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
1119 0};
1120
1108static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p) 1121static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
1109{ 1122{
1110 struct iio_poll_func *pf = p; 1123 struct iio_poll_func *pf = p;
1111 struct iio_dev *indio_dev = pf->indio_dev; 1124 struct iio_dev *indio_dev = pf->indio_dev;
1112 struct bmc150_accel_data *data = iio_priv(indio_dev); 1125 struct bmc150_accel_data *data = iio_priv(indio_dev);
1113 int bit, ret, i = 0; 1126 int ret;
1114 unsigned int raw_val;
1115 1127
1116 mutex_lock(&data->mutex); 1128 mutex_lock(&data->mutex);
1117 for_each_set_bit(bit, indio_dev->active_scan_mask, 1129 ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_REG_XOUT_L,
1118 indio_dev->masklength) { 1130 data->buffer, AXIS_MAX * 2);
1119 ret = regmap_bulk_read(data->regmap,
1120 BMC150_ACCEL_AXIS_TO_REG(bit), &raw_val,
1121 2);
1122 if (ret < 0) {
1123 mutex_unlock(&data->mutex);
1124 goto err_read;
1125 }
1126 data->buffer[i++] = raw_val;
1127 }
1128 mutex_unlock(&data->mutex); 1131 mutex_unlock(&data->mutex);
1132 if (ret < 0)
1133 goto err_read;
1129 1134
1130 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 1135 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
1131 pf->timestamp); 1136 pf->timestamp);
@@ -1139,6 +1144,7 @@ static int bmc150_accel_trig_try_reen(struct iio_trigger *trig)
1139{ 1144{
1140 struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig); 1145 struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
1141 struct bmc150_accel_data *data = t->data; 1146 struct bmc150_accel_data *data = t->data;
1147 struct device *dev = regmap_get_device(data->regmap);
1142 int ret; 1148 int ret;
1143 1149
1144 /* new data interrupts don't need ack */ 1150 /* new data interrupts don't need ack */
@@ -1152,8 +1158,7 @@ static int bmc150_accel_trig_try_reen(struct iio_trigger *trig)
1152 BMC150_ACCEL_INT_MODE_LATCH_RESET); 1158 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1153 mutex_unlock(&data->mutex); 1159 mutex_unlock(&data->mutex);
1154 if (ret < 0) { 1160 if (ret < 0) {
1155 dev_err(data->dev, 1161 dev_err(dev, "Error writing reg_int_rst_latch\n");
1156 "Error writing reg_int_rst_latch\n");
1157 return ret; 1162 return ret;
1158 } 1163 }
1159 1164
@@ -1204,13 +1209,14 @@ static const struct iio_trigger_ops bmc150_accel_trigger_ops = {
1204static int bmc150_accel_handle_roc_event(struct iio_dev *indio_dev) 1209static int bmc150_accel_handle_roc_event(struct iio_dev *indio_dev)
1205{ 1210{
1206 struct bmc150_accel_data *data = iio_priv(indio_dev); 1211 struct bmc150_accel_data *data = iio_priv(indio_dev);
1212 struct device *dev = regmap_get_device(data->regmap);
1207 int dir; 1213 int dir;
1208 int ret; 1214 int ret;
1209 unsigned int val; 1215 unsigned int val;
1210 1216
1211 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_INT_STATUS_2, &val); 1217 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_INT_STATUS_2, &val);
1212 if (ret < 0) { 1218 if (ret < 0) {
1213 dev_err(data->dev, "Error reading reg_int_status_2\n"); 1219 dev_err(dev, "Error reading reg_int_status_2\n");
1214 return ret; 1220 return ret;
1215 } 1221 }
1216 1222
@@ -1253,6 +1259,7 @@ static irqreturn_t bmc150_accel_irq_thread_handler(int irq, void *private)
1253{ 1259{
1254 struct iio_dev *indio_dev = private; 1260 struct iio_dev *indio_dev = private;
1255 struct bmc150_accel_data *data = iio_priv(indio_dev); 1261 struct bmc150_accel_data *data = iio_priv(indio_dev);
1262 struct device *dev = regmap_get_device(data->regmap);
1256 bool ack = false; 1263 bool ack = false;
1257 int ret; 1264 int ret;
1258 1265
@@ -1276,7 +1283,7 @@ static irqreturn_t bmc150_accel_irq_thread_handler(int irq, void *private)
1276 BMC150_ACCEL_INT_MODE_LATCH_INT | 1283 BMC150_ACCEL_INT_MODE_LATCH_INT |
1277 BMC150_ACCEL_INT_MODE_LATCH_RESET); 1284 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1278 if (ret) 1285 if (ret)
1279 dev_err(data->dev, "Error writing reg_int_rst_latch\n"); 1286 dev_err(dev, "Error writing reg_int_rst_latch\n");
1280 1287
1281 ret = IRQ_HANDLED; 1288 ret = IRQ_HANDLED;
1282 } else { 1289 } else {
@@ -1347,13 +1354,14 @@ static void bmc150_accel_unregister_triggers(struct bmc150_accel_data *data,
1347static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev, 1354static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev,
1348 struct bmc150_accel_data *data) 1355 struct bmc150_accel_data *data)
1349{ 1356{
1357 struct device *dev = regmap_get_device(data->regmap);
1350 int i, ret; 1358 int i, ret;
1351 1359
1352 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) { 1360 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1353 struct bmc150_accel_trigger *t = &data->triggers[i]; 1361 struct bmc150_accel_trigger *t = &data->triggers[i];
1354 1362
1355 t->indio_trig = devm_iio_trigger_alloc(data->dev, 1363 t->indio_trig = devm_iio_trigger_alloc(dev,
1356 bmc150_accel_triggers[i].name, 1364 bmc150_accel_triggers[i].name,
1357 indio_dev->name, 1365 indio_dev->name,
1358 indio_dev->id); 1366 indio_dev->id);
1359 if (!t->indio_trig) { 1367 if (!t->indio_trig) {
@@ -1361,7 +1369,7 @@ static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev,
1361 break; 1369 break;
1362 } 1370 }
1363 1371
1364 t->indio_trig->dev.parent = data->dev; 1372 t->indio_trig->dev.parent = dev;
1365 t->indio_trig->ops = &bmc150_accel_trigger_ops; 1373 t->indio_trig->ops = &bmc150_accel_trigger_ops;
1366 t->intr = bmc150_accel_triggers[i].intr; 1374 t->intr = bmc150_accel_triggers[i].intr;
1367 t->data = data; 1375 t->data = data;
@@ -1385,12 +1393,13 @@ static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev,
1385 1393
1386static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data) 1394static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data)
1387{ 1395{
1396 struct device *dev = regmap_get_device(data->regmap);
1388 u8 reg = BMC150_ACCEL_REG_FIFO_CONFIG1; 1397 u8 reg = BMC150_ACCEL_REG_FIFO_CONFIG1;
1389 int ret; 1398 int ret;
1390 1399
1391 ret = regmap_write(data->regmap, reg, data->fifo_mode); 1400 ret = regmap_write(data->regmap, reg, data->fifo_mode);
1392 if (ret < 0) { 1401 if (ret < 0) {
1393 dev_err(data->dev, "Error writing reg_fifo_config1\n"); 1402 dev_err(dev, "Error writing reg_fifo_config1\n");
1394 return ret; 1403 return ret;
1395 } 1404 }
1396 1405
@@ -1400,7 +1409,7 @@ static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data)
1400 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_FIFO_CONFIG0, 1409 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_FIFO_CONFIG0,
1401 data->watermark); 1410 data->watermark);
1402 if (ret < 0) 1411 if (ret < 0)
1403 dev_err(data->dev, "Error writing reg_fifo_config0\n"); 1412 dev_err(dev, "Error writing reg_fifo_config0\n");
1404 1413
1405 return ret; 1414 return ret;
1406} 1415}
@@ -1484,17 +1493,17 @@ static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops = {
1484 1493
1485static int bmc150_accel_chip_init(struct bmc150_accel_data *data) 1494static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
1486{ 1495{
1496 struct device *dev = regmap_get_device(data->regmap);
1487 int ret, i; 1497 int ret, i;
1488 unsigned int val; 1498 unsigned int val;
1489 1499
1490 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val); 1500 ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val);
1491 if (ret < 0) { 1501 if (ret < 0) {
1492 dev_err(data->dev, 1502 dev_err(dev, "Error: Reading chip id\n");
1493 "Error: Reading chip id\n");
1494 return ret; 1503 return ret;
1495 } 1504 }
1496 1505
1497 dev_dbg(data->dev, "Chip Id %x\n", val); 1506 dev_dbg(dev, "Chip Id %x\n", val);
1498 for (i = 0; i < ARRAY_SIZE(bmc150_accel_chip_info_tbl); i++) { 1507 for (i = 0; i < ARRAY_SIZE(bmc150_accel_chip_info_tbl); i++) {
1499 if (bmc150_accel_chip_info_tbl[i].chip_id == val) { 1508 if (bmc150_accel_chip_info_tbl[i].chip_id == val) {
1500 data->chip_info = &bmc150_accel_chip_info_tbl[i]; 1509 data->chip_info = &bmc150_accel_chip_info_tbl[i];
@@ -1503,7 +1512,7 @@ static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
1503 } 1512 }
1504 1513
1505 if (!data->chip_info) { 1514 if (!data->chip_info) {
1506 dev_err(data->dev, "Invalid chip %x\n", val); 1515 dev_err(dev, "Invalid chip %x\n", val);
1507 return -ENODEV; 1516 return -ENODEV;
1508 } 1517 }
1509 1518
@@ -1520,8 +1529,7 @@ static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
1520 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE, 1529 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE,
1521 BMC150_ACCEL_DEF_RANGE_4G); 1530 BMC150_ACCEL_DEF_RANGE_4G);
1522 if (ret < 0) { 1531 if (ret < 0) {
1523 dev_err(data->dev, 1532 dev_err(dev, "Error writing reg_pmu_range\n");
1524 "Error writing reg_pmu_range\n");
1525 return ret; 1533 return ret;
1526 } 1534 }
1527 1535
@@ -1539,8 +1547,7 @@ static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
1539 BMC150_ACCEL_INT_MODE_LATCH_INT | 1547 BMC150_ACCEL_INT_MODE_LATCH_INT |
1540 BMC150_ACCEL_INT_MODE_LATCH_RESET); 1548 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1541 if (ret < 0) { 1549 if (ret < 0) {
1542 dev_err(data->dev, 1550 dev_err(dev, "Error writing reg_int_rst_latch\n");
1543 "Error writing reg_int_rst_latch\n");
1544 return ret; 1551 return ret;
1545 } 1552 }
1546 1553
@@ -1560,7 +1567,6 @@ int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
1560 1567
1561 data = iio_priv(indio_dev); 1568 data = iio_priv(indio_dev);
1562 dev_set_drvdata(dev, indio_dev); 1569 dev_set_drvdata(dev, indio_dev);
1563 data->dev = dev;
1564 data->irq = irq; 1570 data->irq = irq;
1565 1571
1566 data->regmap = regmap; 1572 data->regmap = regmap;
@@ -1575,6 +1581,7 @@ int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
1575 indio_dev->channels = data->chip_info->channels; 1581 indio_dev->channels = data->chip_info->channels;
1576 indio_dev->num_channels = data->chip_info->num_channels; 1582 indio_dev->num_channels = data->chip_info->num_channels;
1577 indio_dev->name = name ? name : data->chip_info->name; 1583 indio_dev->name = name ? name : data->chip_info->name;
1584 indio_dev->available_scan_masks = bmc150_accel_scan_masks;
1578 indio_dev->modes = INDIO_DIRECT_MODE; 1585 indio_dev->modes = INDIO_DIRECT_MODE;
1579 indio_dev->info = &bmc150_accel_info; 1586 indio_dev->info = &bmc150_accel_info;
1580 1587
@@ -1583,13 +1590,13 @@ int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
1583 bmc150_accel_trigger_handler, 1590 bmc150_accel_trigger_handler,
1584 &bmc150_accel_buffer_ops); 1591 &bmc150_accel_buffer_ops);
1585 if (ret < 0) { 1592 if (ret < 0) {
1586 dev_err(data->dev, "Failed: iio triggered buffer setup\n"); 1593 dev_err(dev, "Failed: iio triggered buffer setup\n");
1587 return ret; 1594 return ret;
1588 } 1595 }
1589 1596
1590 if (data->irq > 0) { 1597 if (data->irq > 0) {
1591 ret = devm_request_threaded_irq( 1598 ret = devm_request_threaded_irq(
1592 data->dev, data->irq, 1599 dev, data->irq,
1593 bmc150_accel_irq_handler, 1600 bmc150_accel_irq_handler,
1594 bmc150_accel_irq_thread_handler, 1601 bmc150_accel_irq_thread_handler,
1595 IRQF_TRIGGER_RISING, 1602 IRQF_TRIGGER_RISING,
@@ -1607,7 +1614,7 @@ int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
1607 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH, 1614 ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1608 BMC150_ACCEL_INT_MODE_LATCH_RESET); 1615 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1609 if (ret < 0) { 1616 if (ret < 0) {
1610 dev_err(data->dev, "Error writing reg_int_rst_latch\n"); 1617 dev_err(dev, "Error writing reg_int_rst_latch\n");
1611 goto err_buffer_cleanup; 1618 goto err_buffer_cleanup;
1612 } 1619 }
1613 1620
@@ -1656,9 +1663,9 @@ int bmc150_accel_core_remove(struct device *dev)
1656 1663
1657 iio_device_unregister(indio_dev); 1664 iio_device_unregister(indio_dev);
1658 1665
1659 pm_runtime_disable(data->dev); 1666 pm_runtime_disable(dev);
1660 pm_runtime_set_suspended(data->dev); 1667 pm_runtime_set_suspended(dev);
1661 pm_runtime_put_noidle(data->dev); 1668 pm_runtime_put_noidle(dev);
1662 1669
1663 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1); 1670 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
1664 1671
@@ -1707,7 +1714,7 @@ static int bmc150_accel_runtime_suspend(struct device *dev)
1707 struct bmc150_accel_data *data = iio_priv(indio_dev); 1714 struct bmc150_accel_data *data = iio_priv(indio_dev);
1708 int ret; 1715 int ret;
1709 1716
1710 dev_dbg(data->dev, __func__); 1717 dev_dbg(dev, __func__);
1711 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0); 1718 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
1712 if (ret < 0) 1719 if (ret < 0)
1713 return -EAGAIN; 1720 return -EAGAIN;
@@ -1722,7 +1729,7 @@ static int bmc150_accel_runtime_resume(struct device *dev)
1722 int ret; 1729 int ret;
1723 int sleep_val; 1730 int sleep_val;
1724 1731
1725 dev_dbg(data->dev, __func__); 1732 dev_dbg(dev, __func__);
1726 1733
1727 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); 1734 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1728 if (ret < 0) 1735 if (ret < 0)
diff --git a/drivers/iio/accel/bmc150-accel-i2c.c b/drivers/iio/accel/bmc150-accel-i2c.c
index b41404ba32fc..8ca8041267ef 100644
--- a/drivers/iio/accel/bmc150-accel-i2c.c
+++ b/drivers/iio/accel/bmc150-accel-i2c.c
@@ -28,11 +28,6 @@
28 28
29#include "bmc150-accel.h" 29#include "bmc150-accel.h"
30 30
31static const struct regmap_config bmc150_i2c_regmap_conf = {
32 .reg_bits = 8,
33 .val_bits = 8,
34};
35
36static int bmc150_accel_probe(struct i2c_client *client, 31static int bmc150_accel_probe(struct i2c_client *client,
37 const struct i2c_device_id *id) 32 const struct i2c_device_id *id)
38{ 33{
@@ -43,7 +38,7 @@ static int bmc150_accel_probe(struct i2c_client *client,
43 i2c_check_functionality(client->adapter, 38 i2c_check_functionality(client->adapter,
44 I2C_FUNC_SMBUS_READ_I2C_BLOCK); 39 I2C_FUNC_SMBUS_READ_I2C_BLOCK);
45 40
46 regmap = devm_regmap_init_i2c(client, &bmc150_i2c_regmap_conf); 41 regmap = devm_regmap_init_i2c(client, &bmc150_regmap_conf);
47 if (IS_ERR(regmap)) { 42 if (IS_ERR(regmap)) {
48 dev_err(&client->dev, "Failed to initialize i2c regmap\n"); 43 dev_err(&client->dev, "Failed to initialize i2c regmap\n");
49 return PTR_ERR(regmap); 44 return PTR_ERR(regmap);
diff --git a/drivers/iio/accel/bmc150-accel-spi.c b/drivers/iio/accel/bmc150-accel-spi.c
index 16b66f2a7204..006794a70a1f 100644
--- a/drivers/iio/accel/bmc150-accel-spi.c
+++ b/drivers/iio/accel/bmc150-accel-spi.c
@@ -25,18 +25,12 @@
25 25
26#include "bmc150-accel.h" 26#include "bmc150-accel.h"
27 27
28static const struct regmap_config bmc150_spi_regmap_conf = {
29 .reg_bits = 8,
30 .val_bits = 8,
31 .max_register = 0x3f,
32};
33
34static int bmc150_accel_probe(struct spi_device *spi) 28static int bmc150_accel_probe(struct spi_device *spi)
35{ 29{
36 struct regmap *regmap; 30 struct regmap *regmap;
37 const struct spi_device_id *id = spi_get_device_id(spi); 31 const struct spi_device_id *id = spi_get_device_id(spi);
38 32
39 regmap = devm_regmap_init_spi(spi, &bmc150_spi_regmap_conf); 33 regmap = devm_regmap_init_spi(spi, &bmc150_regmap_conf);
40 if (IS_ERR(regmap)) { 34 if (IS_ERR(regmap)) {
41 dev_err(&spi->dev, "Failed to initialize spi regmap\n"); 35 dev_err(&spi->dev, "Failed to initialize spi regmap\n");
42 return PTR_ERR(regmap); 36 return PTR_ERR(regmap);
diff --git a/drivers/iio/accel/bmc150-accel.h b/drivers/iio/accel/bmc150-accel.h
index ba0335987f94..38a8b11f8c19 100644
--- a/drivers/iio/accel/bmc150-accel.h
+++ b/drivers/iio/accel/bmc150-accel.h
@@ -16,5 +16,6 @@ int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
16 const char *name, bool block_supported); 16 const char *name, bool block_supported);
17int bmc150_accel_core_remove(struct device *dev); 17int bmc150_accel_core_remove(struct device *dev);
18extern const struct dev_pm_ops bmc150_accel_pm_ops; 18extern const struct dev_pm_ops bmc150_accel_pm_ops;
19extern const struct regmap_config bmc150_regmap_conf;
19 20
20#endif /* _BMC150_ACCEL_H_ */ 21#endif /* _BMC150_ACCEL_H_ */
diff --git a/drivers/iio/accel/kxcjk-1013.c b/drivers/iio/accel/kxcjk-1013.c
index edec1d099e91..bfe219a8bea2 100644
--- a/drivers/iio/accel/kxcjk-1013.c
+++ b/drivers/iio/accel/kxcjk-1013.c
@@ -20,7 +20,6 @@
20#include <linux/slab.h> 20#include <linux/slab.h>
21#include <linux/string.h> 21#include <linux/string.h>
22#include <linux/acpi.h> 22#include <linux/acpi.h>
23#include <linux/gpio/consumer.h>
24#include <linux/pm.h> 23#include <linux/pm.h>
25#include <linux/pm_runtime.h> 24#include <linux/pm_runtime.h>
26#include <linux/iio/iio.h> 25#include <linux/iio/iio.h>
@@ -115,6 +114,7 @@ enum kxcjk1013_axis {
115 AXIS_X, 114 AXIS_X,
116 AXIS_Y, 115 AXIS_Y,
117 AXIS_Z, 116 AXIS_Z,
117 AXIS_MAX,
118}; 118};
119 119
120enum kxcjk1013_mode { 120enum kxcjk1013_mode {
@@ -922,7 +922,7 @@ static const struct iio_event_spec kxcjk1013_event = {
922 .realbits = 12, \ 922 .realbits = 12, \
923 .storagebits = 16, \ 923 .storagebits = 16, \
924 .shift = 4, \ 924 .shift = 4, \
925 .endianness = IIO_CPU, \ 925 .endianness = IIO_LE, \
926 }, \ 926 }, \
927 .event_spec = &kxcjk1013_event, \ 927 .event_spec = &kxcjk1013_event, \
928 .num_event_specs = 1 \ 928 .num_event_specs = 1 \
@@ -953,25 +953,23 @@ static const struct iio_info kxcjk1013_info = {
953 .driver_module = THIS_MODULE, 953 .driver_module = THIS_MODULE,
954}; 954};
955 955
956static const unsigned long kxcjk1013_scan_masks[] = {0x7, 0};
957
956static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p) 958static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p)
957{ 959{
958 struct iio_poll_func *pf = p; 960 struct iio_poll_func *pf = p;
959 struct iio_dev *indio_dev = pf->indio_dev; 961 struct iio_dev *indio_dev = pf->indio_dev;
960 struct kxcjk1013_data *data = iio_priv(indio_dev); 962 struct kxcjk1013_data *data = iio_priv(indio_dev);
961 int bit, ret, i = 0; 963 int ret;
962 964
963 mutex_lock(&data->mutex); 965 mutex_lock(&data->mutex);
964 966 ret = i2c_smbus_read_i2c_block_data_or_emulated(data->client,
965 for_each_set_bit(bit, indio_dev->active_scan_mask, 967 KXCJK1013_REG_XOUT_L,
966 indio_dev->masklength) { 968 AXIS_MAX * 2,
967 ret = kxcjk1013_get_acc_reg(data, bit); 969 (u8 *)data->buffer);
968 if (ret < 0) {
969 mutex_unlock(&data->mutex);
970 goto err;
971 }
972 data->buffer[i++] = ret;
973 }
974 mutex_unlock(&data->mutex); 970 mutex_unlock(&data->mutex);
971 if (ret < 0)
972 goto err;
975 973
976 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 974 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
977 data->timestamp); 975 data->timestamp);
@@ -1204,6 +1202,7 @@ static int kxcjk1013_probe(struct i2c_client *client,
1204 indio_dev->dev.parent = &client->dev; 1202 indio_dev->dev.parent = &client->dev;
1205 indio_dev->channels = kxcjk1013_channels; 1203 indio_dev->channels = kxcjk1013_channels;
1206 indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels); 1204 indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels);
1205 indio_dev->available_scan_masks = kxcjk1013_scan_masks;
1207 indio_dev->name = name; 1206 indio_dev->name = name;
1208 indio_dev->modes = INDIO_DIRECT_MODE; 1207 indio_dev->modes = INDIO_DIRECT_MODE;
1209 indio_dev->info = &kxcjk1013_info; 1208 indio_dev->info = &kxcjk1013_info;
diff --git a/drivers/iio/accel/mma7455_core.c b/drivers/iio/accel/mma7455_core.c
index c633cc2c0789..c902f54c23f5 100644
--- a/drivers/iio/accel/mma7455_core.c
+++ b/drivers/iio/accel/mma7455_core.c
@@ -55,11 +55,11 @@
55 55
56struct mma7455_data { 56struct mma7455_data {
57 struct regmap *regmap; 57 struct regmap *regmap;
58 struct device *dev;
59}; 58};
60 59
61static int mma7455_drdy(struct mma7455_data *mma7455) 60static int mma7455_drdy(struct mma7455_data *mma7455)
62{ 61{
62 struct device *dev = regmap_get_device(mma7455->regmap);
63 unsigned int reg; 63 unsigned int reg;
64 int tries = 3; 64 int tries = 3;
65 int ret; 65 int ret;
@@ -75,7 +75,7 @@ static int mma7455_drdy(struct mma7455_data *mma7455)
75 msleep(20); 75 msleep(20);
76 } 76 }
77 77
78 dev_warn(mma7455->dev, "data not ready\n"); 78 dev_warn(dev, "data not ready\n");
79 79
80 return -EIO; 80 return -EIO;
81} 81}
@@ -260,7 +260,6 @@ int mma7455_core_probe(struct device *dev, struct regmap *regmap,
260 dev_set_drvdata(dev, indio_dev); 260 dev_set_drvdata(dev, indio_dev);
261 mma7455 = iio_priv(indio_dev); 261 mma7455 = iio_priv(indio_dev);
262 mma7455->regmap = regmap; 262 mma7455->regmap = regmap;
263 mma7455->dev = dev;
264 263
265 indio_dev->info = &mma7455_info; 264 indio_dev->info = &mma7455_info;
266 indio_dev->name = name; 265 indio_dev->name = name;
diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
index 7f4994f32a90..e225d3c53bd5 100644
--- a/drivers/iio/accel/mma8452.c
+++ b/drivers/iio/accel/mma8452.c
@@ -6,6 +6,7 @@
6 * MMA8453Q (10 bit) 6 * MMA8453Q (10 bit)
7 * MMA8652FC (12 bit) 7 * MMA8652FC (12 bit)
8 * MMA8653FC (10 bit) 8 * MMA8653FC (10 bit)
9 * FXLS8471Q (14 bit)
9 * 10 *
10 * Copyright 2015 Martin Kepplinger <martin.kepplinger@theobroma-systems.com> 11 * Copyright 2015 Martin Kepplinger <martin.kepplinger@theobroma-systems.com>
11 * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net> 12 * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
@@ -16,7 +17,7 @@
16 * 17 *
17 * 7-bit I2C slave address 0x1c/0x1d (pin selectable) 18 * 7-bit I2C slave address 0x1c/0x1d (pin selectable)
18 * 19 *
19 * TODO: orientation events, autosleep 20 * TODO: orientation events
20 */ 21 */
21 22
22#include <linux/module.h> 23#include <linux/module.h>
@@ -31,6 +32,7 @@
31#include <linux/delay.h> 32#include <linux/delay.h>
32#include <linux/of_device.h> 33#include <linux/of_device.h>
33#include <linux/of_irq.h> 34#include <linux/of_irq.h>
35#include <linux/pm_runtime.h>
34 36
35#define MMA8452_STATUS 0x00 37#define MMA8452_STATUS 0x00
36#define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0)) 38#define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0))
@@ -91,6 +93,9 @@
91#define MMA8453_DEVICE_ID 0x3a 93#define MMA8453_DEVICE_ID 0x3a
92#define MMA8652_DEVICE_ID 0x4a 94#define MMA8652_DEVICE_ID 0x4a
93#define MMA8653_DEVICE_ID 0x5a 95#define MMA8653_DEVICE_ID 0x5a
96#define FXLS8471_DEVICE_ID 0x6a
97
98#define MMA8452_AUTO_SUSPEND_DELAY_MS 2000
94 99
95struct mma8452_data { 100struct mma8452_data {
96 struct i2c_client *client; 101 struct i2c_client *client;
@@ -172,6 +177,31 @@ static int mma8452_drdy(struct mma8452_data *data)
172 return -EIO; 177 return -EIO;
173} 178}
174 179
180static int mma8452_set_runtime_pm_state(struct i2c_client *client, bool on)
181{
182#ifdef CONFIG_PM
183 int ret;
184
185 if (on) {
186 ret = pm_runtime_get_sync(&client->dev);
187 } else {
188 pm_runtime_mark_last_busy(&client->dev);
189 ret = pm_runtime_put_autosuspend(&client->dev);
190 }
191
192 if (ret < 0) {
193 dev_err(&client->dev,
194 "failed to change power state to %d\n", on);
195 if (on)
196 pm_runtime_put_noidle(&client->dev);
197
198 return ret;
199 }
200#endif
201
202 return 0;
203}
204
175static int mma8452_read(struct mma8452_data *data, __be16 buf[3]) 205static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
176{ 206{
177 int ret = mma8452_drdy(data); 207 int ret = mma8452_drdy(data);
@@ -179,8 +209,16 @@ static int mma8452_read(struct mma8452_data *data, __be16 buf[3])
179 if (ret < 0) 209 if (ret < 0)
180 return ret; 210 return ret;
181 211
182 return i2c_smbus_read_i2c_block_data(data->client, MMA8452_OUT_X, 212 ret = mma8452_set_runtime_pm_state(data->client, true);
183 3 * sizeof(__be16), (u8 *)buf); 213 if (ret)
214 return ret;
215
216 ret = i2c_smbus_read_i2c_block_data(data->client, MMA8452_OUT_X,
217 3 * sizeof(__be16), (u8 *)buf);
218
219 ret = mma8452_set_runtime_pm_state(data->client, false);
220
221 return ret;
184} 222}
185 223
186static ssize_t mma8452_show_int_plus_micros(char *buf, const int (*vals)[2], 224static ssize_t mma8452_show_int_plus_micros(char *buf, const int (*vals)[2],
@@ -357,7 +395,8 @@ static int mma8452_read_raw(struct iio_dev *indio_dev,
357 return IIO_VAL_INT_PLUS_MICRO; 395 return IIO_VAL_INT_PLUS_MICRO;
358 case IIO_CHAN_INFO_CALIBBIAS: 396 case IIO_CHAN_INFO_CALIBBIAS:
359 ret = i2c_smbus_read_byte_data(data->client, 397 ret = i2c_smbus_read_byte_data(data->client,
360 MMA8452_OFF_X + chan->scan_index); 398 MMA8452_OFF_X +
399 chan->scan_index);
361 if (ret < 0) 400 if (ret < 0)
362 return ret; 401 return ret;
363 402
@@ -392,24 +431,47 @@ static int mma8452_active(struct mma8452_data *data)
392 data->ctrl_reg1); 431 data->ctrl_reg1);
393} 432}
394 433
434/* returns >0 if active, 0 if in standby and <0 on error */
435static int mma8452_is_active(struct mma8452_data *data)
436{
437 int reg;
438
439 reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG1);
440 if (reg < 0)
441 return reg;
442
443 return reg & MMA8452_CTRL_ACTIVE;
444}
445
395static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val) 446static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val)
396{ 447{
397 int ret; 448 int ret;
449 int is_active;
398 450
399 mutex_lock(&data->lock); 451 mutex_lock(&data->lock);
400 452
401 /* config can only be changed when in standby */ 453 is_active = mma8452_is_active(data);
402 ret = mma8452_standby(data); 454 if (is_active < 0) {
403 if (ret < 0) 455 ret = is_active;
404 goto fail; 456 goto fail;
457 }
458
459 /* config can only be changed when in standby */
460 if (is_active > 0) {
461 ret = mma8452_standby(data);
462 if (ret < 0)
463 goto fail;
464 }
405 465
406 ret = i2c_smbus_write_byte_data(data->client, reg, val); 466 ret = i2c_smbus_write_byte_data(data->client, reg, val);
407 if (ret < 0) 467 if (ret < 0)
408 goto fail; 468 goto fail;
409 469
410 ret = mma8452_active(data); 470 if (is_active > 0) {
411 if (ret < 0) 471 ret = mma8452_active(data);
412 goto fail; 472 if (ret < 0)
473 goto fail;
474 }
413 475
414 ret = 0; 476 ret = 0;
415fail: 477fail:
@@ -418,7 +480,7 @@ fail:
418 return ret; 480 return ret;
419} 481}
420 482
421/* returns >0 if in freefall mode, 0 if not or <0 if an error occured */ 483/* returns >0 if in freefall mode, 0 if not or <0 if an error occurred */
422static int mma8452_freefall_mode_enabled(struct mma8452_data *data) 484static int mma8452_freefall_mode_enabled(struct mma8452_data *data)
423{ 485{
424 int val; 486 int val;
@@ -668,7 +730,8 @@ static int mma8452_read_event_config(struct iio_dev *indio_dev,
668 if (ret < 0) 730 if (ret < 0)
669 return ret; 731 return ret;
670 732
671 return !!(ret & BIT(chan->scan_index + chip->ev_cfg_chan_shift)); 733 return !!(ret & BIT(chan->scan_index +
734 chip->ev_cfg_chan_shift));
672 default: 735 default:
673 return -EINVAL; 736 return -EINVAL;
674 } 737 }
@@ -682,7 +745,11 @@ static int mma8452_write_event_config(struct iio_dev *indio_dev,
682{ 745{
683 struct mma8452_data *data = iio_priv(indio_dev); 746 struct mma8452_data *data = iio_priv(indio_dev);
684 const struct mma_chip_info *chip = data->chip_info; 747 const struct mma_chip_info *chip = data->chip_info;
685 int val; 748 int val, ret;
749
750 ret = mma8452_set_runtime_pm_state(data->client, state);
751 if (ret)
752 return ret;
686 753
687 switch (dir) { 754 switch (dir) {
688 case IIO_EV_DIR_FALLING: 755 case IIO_EV_DIR_FALLING:
@@ -990,6 +1057,7 @@ enum {
990 mma8453, 1057 mma8453,
991 mma8652, 1058 mma8652,
992 mma8653, 1059 mma8653,
1060 fxls8471,
993}; 1061};
994 1062
995static const struct mma_chip_info mma_chip_info_table[] = { 1063static const struct mma_chip_info mma_chip_info_table[] = {
@@ -1003,7 +1071,7 @@ static const struct mma_chip_info mma_chip_info_table[] = {
1003 * bit. 1071 * bit.
1004 * The userspace interface uses m/s^2 and we declare micro units 1072 * The userspace interface uses m/s^2 and we declare micro units
1005 * So scale factor for 12 bit here is given by: 1073 * So scale factor for 12 bit here is given by:
1006 * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665 1074 * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665
1007 */ 1075 */
1008 .mma_scales = { {0, 2394}, {0, 4788}, {0, 9577} }, 1076 .mma_scales = { {0, 2394}, {0, 4788}, {0, 9577} },
1009 .ev_cfg = MMA8452_TRANSIENT_CFG, 1077 .ev_cfg = MMA8452_TRANSIENT_CFG,
@@ -1081,6 +1149,22 @@ static const struct mma_chip_info mma_chip_info_table[] = {
1081 .ev_ths_mask = MMA8452_FF_MT_THS_MASK, 1149 .ev_ths_mask = MMA8452_FF_MT_THS_MASK,
1082 .ev_count = MMA8452_FF_MT_COUNT, 1150 .ev_count = MMA8452_FF_MT_COUNT,
1083 }, 1151 },
1152 [fxls8471] = {
1153 .chip_id = FXLS8471_DEVICE_ID,
1154 .channels = mma8451_channels,
1155 .num_channels = ARRAY_SIZE(mma8451_channels),
1156 .mma_scales = { {0, 2394}, {0, 4788}, {0, 9577} },
1157 .ev_cfg = MMA8452_TRANSIENT_CFG,
1158 .ev_cfg_ele = MMA8452_TRANSIENT_CFG_ELE,
1159 .ev_cfg_chan_shift = 1,
1160 .ev_src = MMA8452_TRANSIENT_SRC,
1161 .ev_src_xe = MMA8452_TRANSIENT_SRC_XTRANSE,
1162 .ev_src_ye = MMA8452_TRANSIENT_SRC_YTRANSE,
1163 .ev_src_ze = MMA8452_TRANSIENT_SRC_ZTRANSE,
1164 .ev_ths = MMA8452_TRANSIENT_THS,
1165 .ev_ths_mask = MMA8452_TRANSIENT_THS_MASK,
1166 .ev_count = MMA8452_TRANSIENT_COUNT,
1167 },
1084}; 1168};
1085 1169
1086static struct attribute *mma8452_attributes[] = { 1170static struct attribute *mma8452_attributes[] = {
@@ -1114,7 +1198,11 @@ static int mma8452_data_rdy_trigger_set_state(struct iio_trigger *trig,
1114{ 1198{
1115 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 1199 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1116 struct mma8452_data *data = iio_priv(indio_dev); 1200 struct mma8452_data *data = iio_priv(indio_dev);
1117 int reg; 1201 int reg, ret;
1202
1203 ret = mma8452_set_runtime_pm_state(data->client, state);
1204 if (ret)
1205 return ret;
1118 1206
1119 reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG4); 1207 reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG4);
1120 if (reg < 0) 1208 if (reg < 0)
@@ -1206,6 +1294,7 @@ static const struct of_device_id mma8452_dt_ids[] = {
1206 { .compatible = "fsl,mma8453", .data = &mma_chip_info_table[mma8453] }, 1294 { .compatible = "fsl,mma8453", .data = &mma_chip_info_table[mma8453] },
1207 { .compatible = "fsl,mma8652", .data = &mma_chip_info_table[mma8652] }, 1295 { .compatible = "fsl,mma8652", .data = &mma_chip_info_table[mma8652] },
1208 { .compatible = "fsl,mma8653", .data = &mma_chip_info_table[mma8653] }, 1296 { .compatible = "fsl,mma8653", .data = &mma_chip_info_table[mma8653] },
1297 { .compatible = "fsl,fxls8471", .data = &mma_chip_info_table[fxls8471] },
1209 { } 1298 { }
1210}; 1299};
1211MODULE_DEVICE_TABLE(of, mma8452_dt_ids); 1300MODULE_DEVICE_TABLE(of, mma8452_dt_ids);
@@ -1243,6 +1332,7 @@ static int mma8452_probe(struct i2c_client *client,
1243 case MMA8453_DEVICE_ID: 1332 case MMA8453_DEVICE_ID:
1244 case MMA8652_DEVICE_ID: 1333 case MMA8652_DEVICE_ID:
1245 case MMA8653_DEVICE_ID: 1334 case MMA8653_DEVICE_ID:
1335 case FXLS8471_DEVICE_ID:
1246 if (ret == data->chip_info->chip_id) 1336 if (ret == data->chip_info->chip_id)
1247 break; 1337 break;
1248 default: 1338 default:
@@ -1340,6 +1430,15 @@ static int mma8452_probe(struct i2c_client *client,
1340 goto buffer_cleanup; 1430 goto buffer_cleanup;
1341 } 1431 }
1342 1432
1433 ret = pm_runtime_set_active(&client->dev);
1434 if (ret < 0)
1435 goto buffer_cleanup;
1436
1437 pm_runtime_enable(&client->dev);
1438 pm_runtime_set_autosuspend_delay(&client->dev,
1439 MMA8452_AUTO_SUSPEND_DELAY_MS);
1440 pm_runtime_use_autosuspend(&client->dev);
1441
1343 ret = iio_device_register(indio_dev); 1442 ret = iio_device_register(indio_dev);
1344 if (ret < 0) 1443 if (ret < 0)
1345 goto buffer_cleanup; 1444 goto buffer_cleanup;
@@ -1364,6 +1463,11 @@ static int mma8452_remove(struct i2c_client *client)
1364 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1463 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1365 1464
1366 iio_device_unregister(indio_dev); 1465 iio_device_unregister(indio_dev);
1466
1467 pm_runtime_disable(&client->dev);
1468 pm_runtime_set_suspended(&client->dev);
1469 pm_runtime_put_noidle(&client->dev);
1470
1367 iio_triggered_buffer_cleanup(indio_dev); 1471 iio_triggered_buffer_cleanup(indio_dev);
1368 mma8452_trigger_cleanup(indio_dev); 1472 mma8452_trigger_cleanup(indio_dev);
1369 mma8452_standby(iio_priv(indio_dev)); 1473 mma8452_standby(iio_priv(indio_dev));
@@ -1371,6 +1475,45 @@ static int mma8452_remove(struct i2c_client *client)
1371 return 0; 1475 return 0;
1372} 1476}
1373 1477
1478#ifdef CONFIG_PM
1479static int mma8452_runtime_suspend(struct device *dev)
1480{
1481 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1482 struct mma8452_data *data = iio_priv(indio_dev);
1483 int ret;
1484
1485 mutex_lock(&data->lock);
1486 ret = mma8452_standby(data);
1487 mutex_unlock(&data->lock);
1488 if (ret < 0) {
1489 dev_err(&data->client->dev, "powering off device failed\n");
1490 return -EAGAIN;
1491 }
1492
1493 return 0;
1494}
1495
1496static int mma8452_runtime_resume(struct device *dev)
1497{
1498 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1499 struct mma8452_data *data = iio_priv(indio_dev);
1500 int ret, sleep_val;
1501
1502 ret = mma8452_active(data);
1503 if (ret < 0)
1504 return ret;
1505
1506 ret = mma8452_get_odr_index(data);
1507 sleep_val = 1000 / mma8452_samp_freq[ret][0];
1508 if (sleep_val < 20)
1509 usleep_range(sleep_val * 1000, 20000);
1510 else
1511 msleep_interruptible(sleep_val);
1512
1513 return 0;
1514}
1515#endif
1516
1374#ifdef CONFIG_PM_SLEEP 1517#ifdef CONFIG_PM_SLEEP
1375static int mma8452_suspend(struct device *dev) 1518static int mma8452_suspend(struct device *dev)
1376{ 1519{
@@ -1383,18 +1526,21 @@ static int mma8452_resume(struct device *dev)
1383 return mma8452_active(iio_priv(i2c_get_clientdata( 1526 return mma8452_active(iio_priv(i2c_get_clientdata(
1384 to_i2c_client(dev)))); 1527 to_i2c_client(dev))));
1385} 1528}
1386
1387static SIMPLE_DEV_PM_OPS(mma8452_pm_ops, mma8452_suspend, mma8452_resume);
1388#define MMA8452_PM_OPS (&mma8452_pm_ops)
1389#else
1390#define MMA8452_PM_OPS NULL
1391#endif 1529#endif
1392 1530
1531static const struct dev_pm_ops mma8452_pm_ops = {
1532 SET_SYSTEM_SLEEP_PM_OPS(mma8452_suspend, mma8452_resume)
1533 SET_RUNTIME_PM_OPS(mma8452_runtime_suspend,
1534 mma8452_runtime_resume, NULL)
1535};
1536
1393static const struct i2c_device_id mma8452_id[] = { 1537static const struct i2c_device_id mma8452_id[] = {
1538 { "mma8451", mma8451 },
1394 { "mma8452", mma8452 }, 1539 { "mma8452", mma8452 },
1395 { "mma8453", mma8453 }, 1540 { "mma8453", mma8453 },
1396 { "mma8652", mma8652 }, 1541 { "mma8652", mma8652 },
1397 { "mma8653", mma8653 }, 1542 { "mma8653", mma8653 },
1543 { "fxls8471", fxls8471 },
1398 { } 1544 { }
1399}; 1545};
1400MODULE_DEVICE_TABLE(i2c, mma8452_id); 1546MODULE_DEVICE_TABLE(i2c, mma8452_id);
@@ -1403,7 +1549,7 @@ static struct i2c_driver mma8452_driver = {
1403 .driver = { 1549 .driver = {
1404 .name = "mma8452", 1550 .name = "mma8452",
1405 .of_match_table = of_match_ptr(mma8452_dt_ids), 1551 .of_match_table = of_match_ptr(mma8452_dt_ids),
1406 .pm = MMA8452_PM_OPS, 1552 .pm = &mma8452_pm_ops,
1407 }, 1553 },
1408 .probe = mma8452_probe, 1554 .probe = mma8452_probe,
1409 .remove = mma8452_remove, 1555 .remove = mma8452_remove,
diff --git a/drivers/iio/accel/mma9553.c b/drivers/iio/accel/mma9553.c
index fa7d36217c4b..bb05f3efddca 100644
--- a/drivers/iio/accel/mma9553.c
+++ b/drivers/iio/accel/mma9553.c
@@ -17,7 +17,6 @@
17#include <linux/interrupt.h> 17#include <linux/interrupt.h>
18#include <linux/slab.h> 18#include <linux/slab.h>
19#include <linux/acpi.h> 19#include <linux/acpi.h>
20#include <linux/gpio/consumer.h>
21#include <linux/iio/iio.h> 20#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h> 21#include <linux/iio/sysfs.h>
23#include <linux/iio/events.h> 22#include <linux/iio/events.h>
diff --git a/drivers/iio/accel/mxc4005.c b/drivers/iio/accel/mxc4005.c
index e72e218c2696..c23f47af7256 100644
--- a/drivers/iio/accel/mxc4005.c
+++ b/drivers/iio/accel/mxc4005.c
@@ -17,7 +17,6 @@
17#include <linux/i2c.h> 17#include <linux/i2c.h>
18#include <linux/iio/iio.h> 18#include <linux/iio/iio.h>
19#include <linux/acpi.h> 19#include <linux/acpi.h>
20#include <linux/gpio/consumer.h>
21#include <linux/regmap.h> 20#include <linux/regmap.h>
22#include <linux/iio/sysfs.h> 21#include <linux/iio/sysfs.h>
23#include <linux/iio/trigger.h> 22#include <linux/iio/trigger.h>
@@ -380,31 +379,6 @@ static const struct iio_trigger_ops mxc4005_trigger_ops = {
380 .owner = THIS_MODULE, 379 .owner = THIS_MODULE,
381}; 380};
382 381
383static int mxc4005_gpio_probe(struct i2c_client *client,
384 struct mxc4005_data *data)
385{
386 struct device *dev;
387 struct gpio_desc *gpio;
388 int ret;
389
390 if (!client)
391 return -EINVAL;
392
393 dev = &client->dev;
394
395 gpio = devm_gpiod_get_index(dev, "mxc4005_int", 0, GPIOD_IN);
396 if (IS_ERR(gpio)) {
397 dev_err(dev, "failed to get acpi gpio index\n");
398 return PTR_ERR(gpio);
399 }
400
401 ret = gpiod_to_irq(gpio);
402
403 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
404
405 return ret;
406}
407
408static int mxc4005_chip_init(struct mxc4005_data *data) 382static int mxc4005_chip_init(struct mxc4005_data *data)
409{ 383{
410 int ret; 384 int ret;
@@ -470,9 +444,6 @@ static int mxc4005_probe(struct i2c_client *client,
470 return ret; 444 return ret;
471 } 445 }
472 446
473 if (client->irq < 0)
474 client->irq = mxc4005_gpio_probe(client, data);
475
476 if (client->irq > 0) { 447 if (client->irq > 0) {
477 data->dready_trig = devm_iio_trigger_alloc(&client->dev, 448 data->dready_trig = devm_iio_trigger_alloc(&client->dev,
478 "%s-dev%d", 449 "%s-dev%d",
diff --git a/drivers/iio/accel/st_accel.h b/drivers/iio/accel/st_accel.h
index 5d4a1897b293..57f83a67948c 100644
--- a/drivers/iio/accel/st_accel.h
+++ b/drivers/iio/accel/st_accel.h
@@ -14,6 +14,7 @@
14#include <linux/types.h> 14#include <linux/types.h>
15#include <linux/iio/common/st_sensors.h> 15#include <linux/iio/common/st_sensors.h>
16 16
17#define H3LIS331DL_DRIVER_NAME "h3lis331dl_accel"
17#define LIS3LV02DL_ACCEL_DEV_NAME "lis3lv02dl_accel" 18#define LIS3LV02DL_ACCEL_DEV_NAME "lis3lv02dl_accel"
18#define LSM303DLHC_ACCEL_DEV_NAME "lsm303dlhc_accel" 19#define LSM303DLHC_ACCEL_DEV_NAME "lsm303dlhc_accel"
19#define LIS3DH_ACCEL_DEV_NAME "lis3dh" 20#define LIS3DH_ACCEL_DEV_NAME "lis3dh"
diff --git a/drivers/iio/accel/st_accel_core.c b/drivers/iio/accel/st_accel_core.c
index a03a1417dd63..dc73f2d85e6d 100644
--- a/drivers/iio/accel/st_accel_core.c
+++ b/drivers/iio/accel/st_accel_core.c
@@ -39,6 +39,9 @@
39#define ST_ACCEL_FS_AVL_6G 6 39#define ST_ACCEL_FS_AVL_6G 6
40#define ST_ACCEL_FS_AVL_8G 8 40#define ST_ACCEL_FS_AVL_8G 8
41#define ST_ACCEL_FS_AVL_16G 16 41#define ST_ACCEL_FS_AVL_16G 16
42#define ST_ACCEL_FS_AVL_100G 100
43#define ST_ACCEL_FS_AVL_200G 200
44#define ST_ACCEL_FS_AVL_400G 400
42 45
43/* CUSTOM VALUES FOR SENSOR 1 */ 46/* CUSTOM VALUES FOR SENSOR 1 */
44#define ST_ACCEL_1_WAI_EXP 0x33 47#define ST_ACCEL_1_WAI_EXP 0x33
@@ -96,6 +99,8 @@
96#define ST_ACCEL_2_DRDY_IRQ_INT2_MASK 0x10 99#define ST_ACCEL_2_DRDY_IRQ_INT2_MASK 0x10
97#define ST_ACCEL_2_IHL_IRQ_ADDR 0x22 100#define ST_ACCEL_2_IHL_IRQ_ADDR 0x22
98#define ST_ACCEL_2_IHL_IRQ_MASK 0x80 101#define ST_ACCEL_2_IHL_IRQ_MASK 0x80
102#define ST_ACCEL_2_OD_IRQ_ADDR 0x22
103#define ST_ACCEL_2_OD_IRQ_MASK 0x40
99#define ST_ACCEL_2_MULTIREAD_BIT true 104#define ST_ACCEL_2_MULTIREAD_BIT true
100 105
101/* CUSTOM VALUES FOR SENSOR 3 */ 106/* CUSTOM VALUES FOR SENSOR 3 */
@@ -177,10 +182,39 @@
177#define ST_ACCEL_5_DRDY_IRQ_INT2_MASK 0x20 182#define ST_ACCEL_5_DRDY_IRQ_INT2_MASK 0x20
178#define ST_ACCEL_5_IHL_IRQ_ADDR 0x22 183#define ST_ACCEL_5_IHL_IRQ_ADDR 0x22
179#define ST_ACCEL_5_IHL_IRQ_MASK 0x80 184#define ST_ACCEL_5_IHL_IRQ_MASK 0x80
185#define ST_ACCEL_5_OD_IRQ_ADDR 0x22
186#define ST_ACCEL_5_OD_IRQ_MASK 0x40
180#define ST_ACCEL_5_IG1_EN_ADDR 0x21 187#define ST_ACCEL_5_IG1_EN_ADDR 0x21
181#define ST_ACCEL_5_IG1_EN_MASK 0x08 188#define ST_ACCEL_5_IG1_EN_MASK 0x08
182#define ST_ACCEL_5_MULTIREAD_BIT false 189#define ST_ACCEL_5_MULTIREAD_BIT false
183 190
191/* CUSTOM VALUES FOR SENSOR 6 */
192#define ST_ACCEL_6_WAI_EXP 0x32
193#define ST_ACCEL_6_ODR_ADDR 0x20
194#define ST_ACCEL_6_ODR_MASK 0x18
195#define ST_ACCEL_6_ODR_AVL_50HZ_VAL 0x00
196#define ST_ACCEL_6_ODR_AVL_100HZ_VAL 0x01
197#define ST_ACCEL_6_ODR_AVL_400HZ_VAL 0x02
198#define ST_ACCEL_6_ODR_AVL_1000HZ_VAL 0x03
199#define ST_ACCEL_6_PW_ADDR 0x20
200#define ST_ACCEL_6_PW_MASK 0x20
201#define ST_ACCEL_6_FS_ADDR 0x23
202#define ST_ACCEL_6_FS_MASK 0x30
203#define ST_ACCEL_6_FS_AVL_100_VAL 0x00
204#define ST_ACCEL_6_FS_AVL_200_VAL 0x01
205#define ST_ACCEL_6_FS_AVL_400_VAL 0x03
206#define ST_ACCEL_6_FS_AVL_100_GAIN IIO_G_TO_M_S_2(49000)
207#define ST_ACCEL_6_FS_AVL_200_GAIN IIO_G_TO_M_S_2(98000)
208#define ST_ACCEL_6_FS_AVL_400_GAIN IIO_G_TO_M_S_2(195000)
209#define ST_ACCEL_6_BDU_ADDR 0x23
210#define ST_ACCEL_6_BDU_MASK 0x80
211#define ST_ACCEL_6_DRDY_IRQ_ADDR 0x22
212#define ST_ACCEL_6_DRDY_IRQ_INT1_MASK 0x02
213#define ST_ACCEL_6_DRDY_IRQ_INT2_MASK 0x10
214#define ST_ACCEL_6_IHL_IRQ_ADDR 0x22
215#define ST_ACCEL_6_IHL_IRQ_MASK 0x80
216#define ST_ACCEL_6_MULTIREAD_BIT true
217
184static const struct iio_chan_spec st_accel_8bit_channels[] = { 218static const struct iio_chan_spec st_accel_8bit_channels[] = {
185 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 219 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
186 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 220 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
@@ -302,6 +336,7 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = {
302 .mask_int2 = ST_ACCEL_1_DRDY_IRQ_INT2_MASK, 336 .mask_int2 = ST_ACCEL_1_DRDY_IRQ_INT2_MASK,
303 .addr_ihl = ST_ACCEL_1_IHL_IRQ_ADDR, 337 .addr_ihl = ST_ACCEL_1_IHL_IRQ_ADDR,
304 .mask_ihl = ST_ACCEL_1_IHL_IRQ_MASK, 338 .mask_ihl = ST_ACCEL_1_IHL_IRQ_MASK,
339 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
305 }, 340 },
306 .multi_read_bit = ST_ACCEL_1_MULTIREAD_BIT, 341 .multi_read_bit = ST_ACCEL_1_MULTIREAD_BIT,
307 .bootime = 2, 342 .bootime = 2,
@@ -367,6 +402,9 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = {
367 .mask_int2 = ST_ACCEL_2_DRDY_IRQ_INT2_MASK, 402 .mask_int2 = ST_ACCEL_2_DRDY_IRQ_INT2_MASK,
368 .addr_ihl = ST_ACCEL_2_IHL_IRQ_ADDR, 403 .addr_ihl = ST_ACCEL_2_IHL_IRQ_ADDR,
369 .mask_ihl = ST_ACCEL_2_IHL_IRQ_MASK, 404 .mask_ihl = ST_ACCEL_2_IHL_IRQ_MASK,
405 .addr_od = ST_ACCEL_2_OD_IRQ_ADDR,
406 .mask_od = ST_ACCEL_2_OD_IRQ_MASK,
407 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
370 }, 408 },
371 .multi_read_bit = ST_ACCEL_2_MULTIREAD_BIT, 409 .multi_read_bit = ST_ACCEL_2_MULTIREAD_BIT,
372 .bootime = 2, 410 .bootime = 2,
@@ -444,6 +482,7 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = {
444 .mask_int2 = ST_ACCEL_3_DRDY_IRQ_INT2_MASK, 482 .mask_int2 = ST_ACCEL_3_DRDY_IRQ_INT2_MASK,
445 .addr_ihl = ST_ACCEL_3_IHL_IRQ_ADDR, 483 .addr_ihl = ST_ACCEL_3_IHL_IRQ_ADDR,
446 .mask_ihl = ST_ACCEL_3_IHL_IRQ_MASK, 484 .mask_ihl = ST_ACCEL_3_IHL_IRQ_MASK,
485 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
447 .ig1 = { 486 .ig1 = {
448 .en_addr = ST_ACCEL_3_IG1_EN_ADDR, 487 .en_addr = ST_ACCEL_3_IG1_EN_ADDR,
449 .en_mask = ST_ACCEL_3_IG1_EN_MASK, 488 .en_mask = ST_ACCEL_3_IG1_EN_MASK,
@@ -502,6 +541,7 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = {
502 .drdy_irq = { 541 .drdy_irq = {
503 .addr = ST_ACCEL_4_DRDY_IRQ_ADDR, 542 .addr = ST_ACCEL_4_DRDY_IRQ_ADDR,
504 .mask_int1 = ST_ACCEL_4_DRDY_IRQ_INT1_MASK, 543 .mask_int1 = ST_ACCEL_4_DRDY_IRQ_INT1_MASK,
544 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
505 }, 545 },
506 .multi_read_bit = ST_ACCEL_4_MULTIREAD_BIT, 546 .multi_read_bit = ST_ACCEL_4_MULTIREAD_BIT,
507 .bootime = 2, /* guess */ 547 .bootime = 2, /* guess */
@@ -553,10 +593,75 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = {
553 .mask_int2 = ST_ACCEL_5_DRDY_IRQ_INT2_MASK, 593 .mask_int2 = ST_ACCEL_5_DRDY_IRQ_INT2_MASK,
554 .addr_ihl = ST_ACCEL_5_IHL_IRQ_ADDR, 594 .addr_ihl = ST_ACCEL_5_IHL_IRQ_ADDR,
555 .mask_ihl = ST_ACCEL_5_IHL_IRQ_MASK, 595 .mask_ihl = ST_ACCEL_5_IHL_IRQ_MASK,
596 .addr_od = ST_ACCEL_5_OD_IRQ_ADDR,
597 .mask_od = ST_ACCEL_5_OD_IRQ_MASK,
598 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
556 }, 599 },
557 .multi_read_bit = ST_ACCEL_5_MULTIREAD_BIT, 600 .multi_read_bit = ST_ACCEL_5_MULTIREAD_BIT,
558 .bootime = 2, /* guess */ 601 .bootime = 2, /* guess */
559 }, 602 },
603 {
604 .wai = ST_ACCEL_6_WAI_EXP,
605 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
606 .sensors_supported = {
607 [0] = H3LIS331DL_DRIVER_NAME,
608 },
609 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
610 .odr = {
611 .addr = ST_ACCEL_6_ODR_ADDR,
612 .mask = ST_ACCEL_6_ODR_MASK,
613 .odr_avl = {
614 { 50, ST_ACCEL_6_ODR_AVL_50HZ_VAL },
615 { 100, ST_ACCEL_6_ODR_AVL_100HZ_VAL, },
616 { 400, ST_ACCEL_6_ODR_AVL_400HZ_VAL, },
617 { 1000, ST_ACCEL_6_ODR_AVL_1000HZ_VAL, },
618 },
619 },
620 .pw = {
621 .addr = ST_ACCEL_6_PW_ADDR,
622 .mask = ST_ACCEL_6_PW_MASK,
623 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
624 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
625 },
626 .enable_axis = {
627 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
628 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
629 },
630 .fs = {
631 .addr = ST_ACCEL_6_FS_ADDR,
632 .mask = ST_ACCEL_6_FS_MASK,
633 .fs_avl = {
634 [0] = {
635 .num = ST_ACCEL_FS_AVL_100G,
636 .value = ST_ACCEL_6_FS_AVL_100_VAL,
637 .gain = ST_ACCEL_6_FS_AVL_100_GAIN,
638 },
639 [1] = {
640 .num = ST_ACCEL_FS_AVL_200G,
641 .value = ST_ACCEL_6_FS_AVL_200_VAL,
642 .gain = ST_ACCEL_6_FS_AVL_200_GAIN,
643 },
644 [2] = {
645 .num = ST_ACCEL_FS_AVL_400G,
646 .value = ST_ACCEL_6_FS_AVL_400_VAL,
647 .gain = ST_ACCEL_6_FS_AVL_400_GAIN,
648 },
649 },
650 },
651 .bdu = {
652 .addr = ST_ACCEL_6_BDU_ADDR,
653 .mask = ST_ACCEL_6_BDU_MASK,
654 },
655 .drdy_irq = {
656 .addr = ST_ACCEL_6_DRDY_IRQ_ADDR,
657 .mask_int1 = ST_ACCEL_6_DRDY_IRQ_INT1_MASK,
658 .mask_int2 = ST_ACCEL_6_DRDY_IRQ_INT2_MASK,
659 .addr_ihl = ST_ACCEL_6_IHL_IRQ_ADDR,
660 .mask_ihl = ST_ACCEL_6_IHL_IRQ_MASK,
661 },
662 .multi_read_bit = ST_ACCEL_6_MULTIREAD_BIT,
663 .bootime = 2,
664 },
560}; 665};
561 666
562static int st_accel_read_raw(struct iio_dev *indio_dev, 667static int st_accel_read_raw(struct iio_dev *indio_dev,
diff --git a/drivers/iio/accel/st_accel_i2c.c b/drivers/iio/accel/st_accel_i2c.c
index 294a32f89367..7333ee9fb11b 100644
--- a/drivers/iio/accel/st_accel_i2c.c
+++ b/drivers/iio/accel/st_accel_i2c.c
@@ -76,6 +76,10 @@ static const struct of_device_id st_accel_of_match[] = {
76 .compatible = "st,lis2dh12-accel", 76 .compatible = "st,lis2dh12-accel",
77 .data = LIS2DH12_ACCEL_DEV_NAME, 77 .data = LIS2DH12_ACCEL_DEV_NAME,
78 }, 78 },
79 {
80 .compatible = "st,h3lis331dl-accel",
81 .data = H3LIS331DL_DRIVER_NAME,
82 },
79 {}, 83 {},
80}; 84};
81MODULE_DEVICE_TABLE(of, st_accel_of_match); 85MODULE_DEVICE_TABLE(of, st_accel_of_match);
diff --git a/drivers/iio/accel/stk8312.c b/drivers/iio/accel/stk8312.c
index 85fe7f7247c1..e31023dc5f1b 100644
--- a/drivers/iio/accel/stk8312.c
+++ b/drivers/iio/accel/stk8312.c
@@ -11,7 +11,6 @@
11 */ 11 */
12 12
13#include <linux/acpi.h> 13#include <linux/acpi.h>
14#include <linux/gpio/consumer.h>
15#include <linux/i2c.h> 14#include <linux/i2c.h>
16#include <linux/interrupt.h> 15#include <linux/interrupt.h>
17#include <linux/kernel.h> 16#include <linux/kernel.h>
diff --git a/drivers/iio/accel/stk8ba50.c b/drivers/iio/accel/stk8ba50.c
index 5709d9eb8f34..300d955bad00 100644
--- a/drivers/iio/accel/stk8ba50.c
+++ b/drivers/iio/accel/stk8ba50.c
@@ -11,7 +11,6 @@
11 */ 11 */
12 12
13#include <linux/acpi.h> 13#include <linux/acpi.h>
14#include <linux/gpio/consumer.h>
15#include <linux/i2c.h> 14#include <linux/i2c.h>
16#include <linux/interrupt.h> 15#include <linux/interrupt.h>
17#include <linux/kernel.h> 16#include <linux/kernel.h>
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 82c718c515a0..25378c5882e2 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -242,6 +242,16 @@ config LP8788_ADC
242 To compile this driver as a module, choose M here: the module will be 242 To compile this driver as a module, choose M here: the module will be
243 called lp8788_adc. 243 called lp8788_adc.
244 244
245config LPC18XX_ADC
246 tristate "NXP LPC18xx ADC driver"
247 depends on ARCH_LPC18XX || COMPILE_TEST
248 depends on OF && HAS_IOMEM
249 help
250 Say yes here to build support for NXP LPC18XX ADC.
251
252 To compile this driver as a module, choose M here: the module will be
253 called lpc18xx_adc.
254
245config MAX1027 255config MAX1027
246 tristate "Maxim max1027 ADC driver" 256 tristate "Maxim max1027 ADC driver"
247 depends on SPI 257 depends on SPI
@@ -375,11 +385,11 @@ config ROCKCHIP_SARADC
375 module will be called rockchip_saradc. 385 module will be called rockchip_saradc.
376 386
377config TI_ADC081C 387config TI_ADC081C
378 tristate "Texas Instruments ADC081C021/027" 388 tristate "Texas Instruments ADC081C/ADC101C/ADC121C family"
379 depends on I2C 389 depends on I2C
380 help 390 help
381 If you say yes here you get support for Texas Instruments ADC081C021 391 If you say yes here you get support for Texas Instruments ADC081C,
382 and ADC081C027 ADC chips. 392 ADC101C and ADC121C ADC chips.
383 393
384 This driver can also be built as a module. If so, the module will be 394 This driver can also be built as a module. If so, the module will be
385 called ti-adc081c. 395 called ti-adc081c.
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 0cb79210a4b0..38638d46f972 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -25,6 +25,7 @@ obj-$(CONFIG_HI8435) += hi8435.o
25obj-$(CONFIG_IMX7D_ADC) += imx7d_adc.o 25obj-$(CONFIG_IMX7D_ADC) += imx7d_adc.o
26obj-$(CONFIG_INA2XX_ADC) += ina2xx-adc.o 26obj-$(CONFIG_INA2XX_ADC) += ina2xx-adc.o
27obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o 27obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o
28obj-$(CONFIG_LPC18XX_ADC) += lpc18xx_adc.o
28obj-$(CONFIG_MAX1027) += max1027.o 29obj-$(CONFIG_MAX1027) += max1027.o
29obj-$(CONFIG_MAX1363) += max1363.o 30obj-$(CONFIG_MAX1363) += max1363.o
30obj-$(CONFIG_MCP320X) += mcp320x.o 31obj-$(CONFIG_MCP320X) += mcp320x.o
diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c
index 01d71588d752..a3f5254f4e51 100644
--- a/drivers/iio/adc/ad799x.c
+++ b/drivers/iio/adc/ad799x.c
@@ -477,7 +477,7 @@ static int ad799x_read_event_value(struct iio_dev *indio_dev,
477 if (ret < 0) 477 if (ret < 0)
478 return ret; 478 return ret;
479 *val = (ret >> chan->scan_type.shift) & 479 *val = (ret >> chan->scan_type.shift) &
480 GENMASK(chan->scan_type.realbits - 1 , 0); 480 GENMASK(chan->scan_type.realbits - 1, 0);
481 481
482 return IIO_VAL_INT; 482 return IIO_VAL_INT;
483} 483}
diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c
index 2e154cb51685..e10dca3ed74b 100644
--- a/drivers/iio/adc/at91-sama5d2_adc.c
+++ b/drivers/iio/adc/at91-sama5d2_adc.c
@@ -66,8 +66,10 @@
66#define AT91_SAMA5D2_MR_PRESCAL(v) ((v) << AT91_SAMA5D2_MR_PRESCAL_OFFSET) 66#define AT91_SAMA5D2_MR_PRESCAL(v) ((v) << AT91_SAMA5D2_MR_PRESCAL_OFFSET)
67#define AT91_SAMA5D2_MR_PRESCAL_OFFSET 8 67#define AT91_SAMA5D2_MR_PRESCAL_OFFSET 8
68#define AT91_SAMA5D2_MR_PRESCAL_MAX 0xff 68#define AT91_SAMA5D2_MR_PRESCAL_MAX 0xff
69#define AT91_SAMA5D2_MR_PRESCAL_MASK GENMASK(15, 8)
69/* Startup Time */ 70/* Startup Time */
70#define AT91_SAMA5D2_MR_STARTUP(v) ((v) << 16) 71#define AT91_SAMA5D2_MR_STARTUP(v) ((v) << 16)
72#define AT91_SAMA5D2_MR_STARTUP_MASK GENMASK(19, 16)
71/* Analog Change */ 73/* Analog Change */
72#define AT91_SAMA5D2_MR_ANACH BIT(23) 74#define AT91_SAMA5D2_MR_ANACH BIT(23)
73/* Tracking Time */ 75/* Tracking Time */
@@ -92,13 +94,13 @@
92/* Last Converted Data Register */ 94/* Last Converted Data Register */
93#define AT91_SAMA5D2_LCDR 0x20 95#define AT91_SAMA5D2_LCDR 0x20
94/* Interrupt Enable Register */ 96/* Interrupt Enable Register */
95#define AT91_SAMA5D2_IER 0x24 97#define AT91_SAMA5D2_IER 0x24
96/* Interrupt Disable Register */ 98/* Interrupt Disable Register */
97#define AT91_SAMA5D2_IDR 0x28 99#define AT91_SAMA5D2_IDR 0x28
98/* Interrupt Mask Register */ 100/* Interrupt Mask Register */
99#define AT91_SAMA5D2_IMR 0x2c 101#define AT91_SAMA5D2_IMR 0x2c
100/* Interrupt Status Register */ 102/* Interrupt Status Register */
101#define AT91_SAMA5D2_ISR 0x30 103#define AT91_SAMA5D2_ISR 0x30
102/* Last Channel Trigger Mode Register */ 104/* Last Channel Trigger Mode Register */
103#define AT91_SAMA5D2_LCTMR 0x34 105#define AT91_SAMA5D2_LCTMR 0x34
104/* Last Channel Compare Window Register */ 106/* Last Channel Compare Window Register */
@@ -106,17 +108,20 @@
106/* Overrun Status Register */ 108/* Overrun Status Register */
107#define AT91_SAMA5D2_OVER 0x3c 109#define AT91_SAMA5D2_OVER 0x3c
108/* Extended Mode Register */ 110/* Extended Mode Register */
109#define AT91_SAMA5D2_EMR 0x40 111#define AT91_SAMA5D2_EMR 0x40
110/* Compare Window Register */ 112/* Compare Window Register */
111#define AT91_SAMA5D2_CWR 0x44 113#define AT91_SAMA5D2_CWR 0x44
112/* Channel Gain Register */ 114/* Channel Gain Register */
113#define AT91_SAMA5D2_CGR 0x48 115#define AT91_SAMA5D2_CGR 0x48
116
114/* Channel Offset Register */ 117/* Channel Offset Register */
115#define AT91_SAMA5D2_COR 0x4c 118#define AT91_SAMA5D2_COR 0x4c
119#define AT91_SAMA5D2_COR_DIFF_OFFSET 16
120
116/* Channel Data Register 0 */ 121/* Channel Data Register 0 */
117#define AT91_SAMA5D2_CDR0 0x50 122#define AT91_SAMA5D2_CDR0 0x50
118/* Analog Control Register */ 123/* Analog Control Register */
119#define AT91_SAMA5D2_ACR 0x94 124#define AT91_SAMA5D2_ACR 0x94
120/* Touchscreen Mode Register */ 125/* Touchscreen Mode Register */
121#define AT91_SAMA5D2_TSMR 0xb0 126#define AT91_SAMA5D2_TSMR 0xb0
122/* Touchscreen X Position Register */ 127/* Touchscreen X Position Register */
@@ -130,7 +135,7 @@
130/* Correction Select Register */ 135/* Correction Select Register */
131#define AT91_SAMA5D2_COSR 0xd0 136#define AT91_SAMA5D2_COSR 0xd0
132/* Correction Value Register */ 137/* Correction Value Register */
133#define AT91_SAMA5D2_CVR 0xd4 138#define AT91_SAMA5D2_CVR 0xd4
134/* Channel Error Correction Register */ 139/* Channel Error Correction Register */
135#define AT91_SAMA5D2_CECR 0xd8 140#define AT91_SAMA5D2_CECR 0xd8
136/* Write Protection Mode Register */ 141/* Write Protection Mode Register */
@@ -140,7 +145,7 @@
140/* Version Register */ 145/* Version Register */
141#define AT91_SAMA5D2_VERSION 0xfc 146#define AT91_SAMA5D2_VERSION 0xfc
142 147
143#define AT91_AT91_SAMA5D2_CHAN(num, addr) \ 148#define AT91_SAMA5D2_CHAN_SINGLE(num, addr) \
144 { \ 149 { \
145 .type = IIO_VOLTAGE, \ 150 .type = IIO_VOLTAGE, \
146 .channel = num, \ 151 .channel = num, \
@@ -156,6 +161,24 @@
156 .indexed = 1, \ 161 .indexed = 1, \
157 } 162 }
158 163
164#define AT91_SAMA5D2_CHAN_DIFF(num, num2, addr) \
165 { \
166 .type = IIO_VOLTAGE, \
167 .differential = 1, \
168 .channel = num, \
169 .channel2 = num2, \
170 .address = addr, \
171 .scan_type = { \
172 .sign = 's', \
173 .realbits = 12, \
174 }, \
175 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
176 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
177 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
178 .datasheet_name = "CH"#num"-CH"#num2, \
179 .indexed = 1, \
180 }
181
159#define at91_adc_readl(st, reg) readl_relaxed(st->base + reg) 182#define at91_adc_readl(st, reg) readl_relaxed(st->base + reg)
160#define at91_adc_writel(st, reg, val) writel_relaxed(val, st->base + reg) 183#define at91_adc_writel(st, reg, val) writel_relaxed(val, st->base + reg)
161 184
@@ -185,18 +208,24 @@ struct at91_adc_state {
185}; 208};
186 209
187static const struct iio_chan_spec at91_adc_channels[] = { 210static const struct iio_chan_spec at91_adc_channels[] = {
188 AT91_AT91_SAMA5D2_CHAN(0, 0x50), 211 AT91_SAMA5D2_CHAN_SINGLE(0, 0x50),
189 AT91_AT91_SAMA5D2_CHAN(1, 0x54), 212 AT91_SAMA5D2_CHAN_SINGLE(1, 0x54),
190 AT91_AT91_SAMA5D2_CHAN(2, 0x58), 213 AT91_SAMA5D2_CHAN_SINGLE(2, 0x58),
191 AT91_AT91_SAMA5D2_CHAN(3, 0x5c), 214 AT91_SAMA5D2_CHAN_SINGLE(3, 0x5c),
192 AT91_AT91_SAMA5D2_CHAN(4, 0x60), 215 AT91_SAMA5D2_CHAN_SINGLE(4, 0x60),
193 AT91_AT91_SAMA5D2_CHAN(5, 0x64), 216 AT91_SAMA5D2_CHAN_SINGLE(5, 0x64),
194 AT91_AT91_SAMA5D2_CHAN(6, 0x68), 217 AT91_SAMA5D2_CHAN_SINGLE(6, 0x68),
195 AT91_AT91_SAMA5D2_CHAN(7, 0x6c), 218 AT91_SAMA5D2_CHAN_SINGLE(7, 0x6c),
196 AT91_AT91_SAMA5D2_CHAN(8, 0x70), 219 AT91_SAMA5D2_CHAN_SINGLE(8, 0x70),
197 AT91_AT91_SAMA5D2_CHAN(9, 0x74), 220 AT91_SAMA5D2_CHAN_SINGLE(9, 0x74),
198 AT91_AT91_SAMA5D2_CHAN(10, 0x78), 221 AT91_SAMA5D2_CHAN_SINGLE(10, 0x78),
199 AT91_AT91_SAMA5D2_CHAN(11, 0x7c), 222 AT91_SAMA5D2_CHAN_SINGLE(11, 0x7c),
223 AT91_SAMA5D2_CHAN_DIFF(0, 1, 0x50),
224 AT91_SAMA5D2_CHAN_DIFF(2, 3, 0x58),
225 AT91_SAMA5D2_CHAN_DIFF(4, 5, 0x60),
226 AT91_SAMA5D2_CHAN_DIFF(6, 7, 0x68),
227 AT91_SAMA5D2_CHAN_DIFF(8, 9, 0x70),
228 AT91_SAMA5D2_CHAN_DIFF(10, 11, 0x78),
200}; 229};
201 230
202static unsigned at91_adc_startup_time(unsigned startup_time_min, 231static unsigned at91_adc_startup_time(unsigned startup_time_min,
@@ -226,7 +255,7 @@ static unsigned at91_adc_startup_time(unsigned startup_time_min,
226static void at91_adc_setup_samp_freq(struct at91_adc_state *st, unsigned freq) 255static void at91_adc_setup_samp_freq(struct at91_adc_state *st, unsigned freq)
227{ 256{
228 struct iio_dev *indio_dev = iio_priv_to_dev(st); 257 struct iio_dev *indio_dev = iio_priv_to_dev(st);
229 unsigned f_per, prescal, startup; 258 unsigned f_per, prescal, startup, mr;
230 259
231 f_per = clk_get_rate(st->per_clk); 260 f_per = clk_get_rate(st->per_clk);
232 prescal = (f_per / (2 * freq)) - 1; 261 prescal = (f_per / (2 * freq)) - 1;
@@ -234,10 +263,11 @@ static void at91_adc_setup_samp_freq(struct at91_adc_state *st, unsigned freq)
234 startup = at91_adc_startup_time(st->soc_info.startup_time, 263 startup = at91_adc_startup_time(st->soc_info.startup_time,
235 freq / 1000); 264 freq / 1000);
236 265
237 at91_adc_writel(st, AT91_SAMA5D2_MR, 266 mr = at91_adc_readl(st, AT91_SAMA5D2_MR);
238 AT91_SAMA5D2_MR_TRANSFER(2) 267 mr &= ~(AT91_SAMA5D2_MR_STARTUP_MASK | AT91_SAMA5D2_MR_PRESCAL_MASK);
239 | AT91_SAMA5D2_MR_STARTUP(startup) 268 mr |= AT91_SAMA5D2_MR_STARTUP(startup);
240 | AT91_SAMA5D2_MR_PRESCAL(prescal)); 269 mr |= AT91_SAMA5D2_MR_PRESCAL(prescal);
270 at91_adc_writel(st, AT91_SAMA5D2_MR, mr);
241 271
242 dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u\n", 272 dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u\n",
243 freq, startup, prescal); 273 freq, startup, prescal);
@@ -278,6 +308,7 @@ static int at91_adc_read_raw(struct iio_dev *indio_dev,
278 int *val, int *val2, long mask) 308 int *val, int *val2, long mask)
279{ 309{
280 struct at91_adc_state *st = iio_priv(indio_dev); 310 struct at91_adc_state *st = iio_priv(indio_dev);
311 u32 cor = 0;
281 int ret; 312 int ret;
282 313
283 switch (mask) { 314 switch (mask) {
@@ -286,6 +317,11 @@ static int at91_adc_read_raw(struct iio_dev *indio_dev,
286 317
287 st->chan = chan; 318 st->chan = chan;
288 319
320 if (chan->differential)
321 cor = (BIT(chan->channel) | BIT(chan->channel2)) <<
322 AT91_SAMA5D2_COR_DIFF_OFFSET;
323
324 at91_adc_writel(st, AT91_SAMA5D2_COR, cor);
289 at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel)); 325 at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel));
290 at91_adc_writel(st, AT91_SAMA5D2_IER, BIT(chan->channel)); 326 at91_adc_writel(st, AT91_SAMA5D2_IER, BIT(chan->channel));
291 at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START); 327 at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START);
@@ -298,6 +334,8 @@ static int at91_adc_read_raw(struct iio_dev *indio_dev,
298 334
299 if (ret > 0) { 335 if (ret > 0) {
300 *val = st->conversion_value; 336 *val = st->conversion_value;
337 if (chan->scan_type.sign == 's')
338 *val = sign_extend32(*val, 11);
301 ret = IIO_VAL_INT; 339 ret = IIO_VAL_INT;
302 st->conversion_done = false; 340 st->conversion_done = false;
303 } 341 }
@@ -310,6 +348,8 @@ static int at91_adc_read_raw(struct iio_dev *indio_dev,
310 348
311 case IIO_CHAN_INFO_SCALE: 349 case IIO_CHAN_INFO_SCALE:
312 *val = st->vref_uv / 1000; 350 *val = st->vref_uv / 1000;
351 if (chan->differential)
352 *val *= 2;
313 *val2 = chan->scan_type.realbits; 353 *val2 = chan->scan_type.realbits;
314 return IIO_VAL_FRACTIONAL_LOG2; 354 return IIO_VAL_FRACTIONAL_LOG2;
315 355
@@ -444,6 +484,12 @@ static int at91_adc_probe(struct platform_device *pdev)
444 484
445 at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST); 485 at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST);
446 at91_adc_writel(st, AT91_SAMA5D2_IDR, 0xffffffff); 486 at91_adc_writel(st, AT91_SAMA5D2_IDR, 0xffffffff);
487 /*
488 * Transfer field must be set to 2 according to the datasheet and
489 * allows different analog settings for each channel.
490 */
491 at91_adc_writel(st, AT91_SAMA5D2_MR,
492 AT91_SAMA5D2_MR_TRANSFER(2) | AT91_SAMA5D2_MR_ANACH);
447 493
448 at91_adc_setup_samp_freq(st, st->soc_info.min_sample_rate); 494 at91_adc_setup_samp_freq(st, st->soc_info.min_sample_rate);
449 495
diff --git a/drivers/iio/adc/at91_adc.c b/drivers/iio/adc/at91_adc.c
index f284cd6a93d6..52430ba171f3 100644
--- a/drivers/iio/adc/at91_adc.c
+++ b/drivers/iio/adc/at91_adc.c
@@ -797,8 +797,8 @@ static u32 calc_startup_ticks_9x5(u32 startup_time, u32 adc_clk_khz)
797 * Startup Time = <lookup_table_value> / ADC Clock 797 * Startup Time = <lookup_table_value> / ADC Clock
798 */ 798 */
799 const int startup_lookup[] = { 799 const int startup_lookup[] = {
800 0 , 8 , 16 , 24 , 800 0, 8, 16, 24,
801 64 , 80 , 96 , 112, 801 64, 80, 96, 112,
802 512, 576, 640, 704, 802 512, 576, 640, 704,
803 768, 832, 896, 960 803 768, 832, 896, 960
804 }; 804 };
@@ -924,14 +924,14 @@ static int at91_adc_probe_dt(struct at91_adc_state *st,
924 ret = -EINVAL; 924 ret = -EINVAL;
925 goto error_ret; 925 goto error_ret;
926 } 926 }
927 trig->name = name; 927 trig->name = name;
928 928
929 if (of_property_read_u32(trig_node, "trigger-value", &prop)) { 929 if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
930 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n"); 930 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
931 ret = -EINVAL; 931 ret = -EINVAL;
932 goto error_ret; 932 goto error_ret;
933 } 933 }
934 trig->value = prop; 934 trig->value = prop;
935 trig->is_external = of_property_read_bool(trig_node, "trigger-external"); 935 trig->is_external = of_property_read_bool(trig_node, "trigger-external");
936 i++; 936 i++;
937 } 937 }
diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c
index 65909d5858b1..502f2fbe8aef 100644
--- a/drivers/iio/adc/ina2xx-adc.c
+++ b/drivers/iio/adc/ina2xx-adc.c
@@ -185,9 +185,9 @@ static int ina2xx_read_raw(struct iio_dev *indio_dev,
185 case IIO_CHAN_INFO_SCALE: 185 case IIO_CHAN_INFO_SCALE:
186 switch (chan->address) { 186 switch (chan->address) {
187 case INA2XX_SHUNT_VOLTAGE: 187 case INA2XX_SHUNT_VOLTAGE:
188 /* processed (mV) = raw*1000/shunt_div */ 188 /* processed (mV) = raw/shunt_div */
189 *val2 = chip->config->shunt_div; 189 *val2 = chip->config->shunt_div;
190 *val = 1000; 190 *val = 1;
191 return IIO_VAL_FRACTIONAL; 191 return IIO_VAL_FRACTIONAL;
192 192
193 case INA2XX_BUS_VOLTAGE: 193 case INA2XX_BUS_VOLTAGE:
@@ -350,6 +350,23 @@ static ssize_t ina2xx_allow_async_readout_store(struct device *dev,
350 return len; 350 return len;
351} 351}
352 352
353/*
354 * Set current LSB to 1mA, shunt is in uOhms
355 * (equation 13 in datasheet). We hardcode a Current_LSB
356 * of 1.0 x10-6. The only remaining parameter is RShunt.
357 * There is no need to expose the CALIBRATION register
358 * to the user for now. But we need to reset this register
359 * if the user updates RShunt after driver init, e.g upon
360 * reading an EEPROM/Probe-type value.
361 */
362static int ina2xx_set_calibration(struct ina2xx_chip_info *chip)
363{
364 u16 regval = DIV_ROUND_CLOSEST(chip->config->calibration_factor,
365 chip->shunt_resistor);
366
367 return regmap_write(chip->regmap, INA2XX_CALIBRATION, regval);
368}
369
353static int set_shunt_resistor(struct ina2xx_chip_info *chip, unsigned int val) 370static int set_shunt_resistor(struct ina2xx_chip_info *chip, unsigned int val)
354{ 371{
355 if (val <= 0 || val > chip->config->calibration_factor) 372 if (val <= 0 || val > chip->config->calibration_factor)
@@ -385,6 +402,11 @@ static ssize_t ina2xx_shunt_resistor_store(struct device *dev,
385 if (ret) 402 if (ret)
386 return ret; 403 return ret;
387 404
405 /* Update the Calibration register */
406 ret = ina2xx_set_calibration(chip);
407 if (ret)
408 return ret;
409
388 return len; 410 return len;
389} 411}
390 412
@@ -602,24 +624,11 @@ static const struct iio_info ina2xx_info = {
602/* Initialize the configuration and calibration registers. */ 624/* Initialize the configuration and calibration registers. */
603static int ina2xx_init(struct ina2xx_chip_info *chip, unsigned int config) 625static int ina2xx_init(struct ina2xx_chip_info *chip, unsigned int config)
604{ 626{
605 u16 regval; 627 int ret = regmap_write(chip->regmap, INA2XX_CONFIG, config);
606 int ret;
607
608 ret = regmap_write(chip->regmap, INA2XX_CONFIG, config);
609 if (ret) 628 if (ret)
610 return ret; 629 return ret;
611 630
612 /* 631 return ina2xx_set_calibration(chip);
613 * Set current LSB to 1mA, shunt is in uOhms
614 * (equation 13 in datasheet). We hardcode a Current_LSB
615 * of 1.0 x10-6. The only remaining parameter is RShunt.
616 * There is no need to expose the CALIBRATION register
617 * to the user for now.
618 */
619 regval = DIV_ROUND_CLOSEST(chip->config->calibration_factor,
620 chip->shunt_resistor);
621
622 return regmap_write(chip->regmap, INA2XX_CALIBRATION, regval);
623} 632}
624 633
625static int ina2xx_probe(struct i2c_client *client, 634static int ina2xx_probe(struct i2c_client *client,
diff --git a/drivers/iio/adc/lpc18xx_adc.c b/drivers/iio/adc/lpc18xx_adc.c
new file mode 100644
index 000000000000..3ef18f4b27f0
--- /dev/null
+++ b/drivers/iio/adc/lpc18xx_adc.c
@@ -0,0 +1,231 @@
1/*
2 * IIO ADC driver for NXP LPC18xx ADC
3 *
4 * Copyright (C) 2016 Joachim Eastwood <manabian@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * UNSUPPORTED hardware features:
11 * - Hardware triggers
12 * - Burst mode
13 * - Interrupts
14 * - DMA
15 */
16
17#include <linux/clk.h>
18#include <linux/err.h>
19#include <linux/iio/iio.h>
20#include <linux/iio/driver.h>
21#include <linux/io.h>
22#include <linux/iopoll.h>
23#include <linux/module.h>
24#include <linux/mutex.h>
25#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/platform_device.h>
28#include <linux/regulator/consumer.h>
29
30/* LPC18XX ADC registers and bits */
31#define LPC18XX_ADC_CR 0x000
32#define LPC18XX_ADC_CR_CLKDIV_SHIFT 8
33#define LPC18XX_ADC_CR_PDN BIT(21)
34#define LPC18XX_ADC_CR_START_NOW (0x1 << 24)
35#define LPC18XX_ADC_GDR 0x004
36
37/* Data register bits */
38#define LPC18XX_ADC_SAMPLE_SHIFT 6
39#define LPC18XX_ADC_SAMPLE_MASK 0x3ff
40#define LPC18XX_ADC_CONV_DONE BIT(31)
41
42/* Clock should be 4.5 MHz or less */
43#define LPC18XX_ADC_CLK_TARGET 4500000
44
45struct lpc18xx_adc {
46 struct regulator *vref;
47 void __iomem *base;
48 struct device *dev;
49 struct mutex lock;
50 struct clk *clk;
51 u32 cr_reg;
52};
53
54#define LPC18XX_ADC_CHAN(_idx) { \
55 .type = IIO_VOLTAGE, \
56 .indexed = 1, \
57 .channel = _idx, \
58 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
59 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
60}
61
62static const struct iio_chan_spec lpc18xx_adc_iio_channels[] = {
63 LPC18XX_ADC_CHAN(0),
64 LPC18XX_ADC_CHAN(1),
65 LPC18XX_ADC_CHAN(2),
66 LPC18XX_ADC_CHAN(3),
67 LPC18XX_ADC_CHAN(4),
68 LPC18XX_ADC_CHAN(5),
69 LPC18XX_ADC_CHAN(6),
70 LPC18XX_ADC_CHAN(7),
71};
72
73static int lpc18xx_adc_read_chan(struct lpc18xx_adc *adc, unsigned int ch)
74{
75 int ret;
76 u32 reg;
77
78 reg = adc->cr_reg | BIT(ch) | LPC18XX_ADC_CR_START_NOW;
79 writel(reg, adc->base + LPC18XX_ADC_CR);
80
81 ret = readl_poll_timeout(adc->base + LPC18XX_ADC_GDR, reg,
82 reg & LPC18XX_ADC_CONV_DONE, 3, 9);
83 if (ret) {
84 dev_warn(adc->dev, "adc read timed out\n");
85 return ret;
86 }
87
88 return (reg >> LPC18XX_ADC_SAMPLE_SHIFT) & LPC18XX_ADC_SAMPLE_MASK;
89}
90
91static int lpc18xx_adc_read_raw(struct iio_dev *indio_dev,
92 struct iio_chan_spec const *chan,
93 int *val, int *val2, long mask)
94{
95 struct lpc18xx_adc *adc = iio_priv(indio_dev);
96
97 switch (mask) {
98 case IIO_CHAN_INFO_RAW:
99 mutex_lock(&adc->lock);
100 *val = lpc18xx_adc_read_chan(adc, chan->channel);
101 mutex_unlock(&adc->lock);
102 if (*val < 0)
103 return *val;
104
105 return IIO_VAL_INT;
106
107 case IIO_CHAN_INFO_SCALE:
108 *val = regulator_get_voltage(adc->vref) / 1000;
109 *val2 = 10;
110
111 return IIO_VAL_FRACTIONAL_LOG2;
112 }
113
114 return -EINVAL;
115}
116
117static const struct iio_info lpc18xx_adc_info = {
118 .read_raw = lpc18xx_adc_read_raw,
119 .driver_module = THIS_MODULE,
120};
121
122static int lpc18xx_adc_probe(struct platform_device *pdev)
123{
124 struct iio_dev *indio_dev;
125 struct lpc18xx_adc *adc;
126 struct resource *res;
127 unsigned int clkdiv;
128 unsigned long rate;
129 int ret;
130
131 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
132 if (!indio_dev)
133 return -ENOMEM;
134
135 platform_set_drvdata(pdev, indio_dev);
136 adc = iio_priv(indio_dev);
137 adc->dev = &pdev->dev;
138 mutex_init(&adc->lock);
139
140 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
141 adc->base = devm_ioremap_resource(&pdev->dev, res);
142 if (IS_ERR(adc->base))
143 return PTR_ERR(adc->base);
144
145 adc->clk = devm_clk_get(&pdev->dev, NULL);
146 if (IS_ERR(adc->clk)) {
147 dev_err(&pdev->dev, "error getting clock\n");
148 return PTR_ERR(adc->clk);
149 }
150
151 rate = clk_get_rate(adc->clk);
152 clkdiv = DIV_ROUND_UP(rate, LPC18XX_ADC_CLK_TARGET);
153
154 adc->vref = devm_regulator_get(&pdev->dev, "vref");
155 if (IS_ERR(adc->vref)) {
156 dev_err(&pdev->dev, "error getting regulator\n");
157 return PTR_ERR(adc->vref);
158 }
159
160 indio_dev->name = dev_name(&pdev->dev);
161 indio_dev->dev.parent = &pdev->dev;
162 indio_dev->info = &lpc18xx_adc_info;
163 indio_dev->modes = INDIO_DIRECT_MODE;
164 indio_dev->channels = lpc18xx_adc_iio_channels;
165 indio_dev->num_channels = ARRAY_SIZE(lpc18xx_adc_iio_channels);
166
167 ret = regulator_enable(adc->vref);
168 if (ret) {
169 dev_err(&pdev->dev, "unable to enable regulator\n");
170 return ret;
171 }
172
173 ret = clk_prepare_enable(adc->clk);
174 if (ret) {
175 dev_err(&pdev->dev, "unable to enable clock\n");
176 goto dis_reg;
177 }
178
179 adc->cr_reg = (clkdiv << LPC18XX_ADC_CR_CLKDIV_SHIFT) |
180 LPC18XX_ADC_CR_PDN;
181 writel(adc->cr_reg, adc->base + LPC18XX_ADC_CR);
182
183 ret = iio_device_register(indio_dev);
184 if (ret) {
185 dev_err(&pdev->dev, "unable to register device\n");
186 goto dis_clk;
187 }
188
189 return 0;
190
191dis_clk:
192 writel(0, adc->base + LPC18XX_ADC_CR);
193 clk_disable_unprepare(adc->clk);
194dis_reg:
195 regulator_disable(adc->vref);
196 return ret;
197}
198
199static int lpc18xx_adc_remove(struct platform_device *pdev)
200{
201 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
202 struct lpc18xx_adc *adc = iio_priv(indio_dev);
203
204 iio_device_unregister(indio_dev);
205
206 writel(0, adc->base + LPC18XX_ADC_CR);
207 clk_disable_unprepare(adc->clk);
208 regulator_disable(adc->vref);
209
210 return 0;
211}
212
213static const struct of_device_id lpc18xx_adc_match[] = {
214 { .compatible = "nxp,lpc1850-adc" },
215 { /* sentinel */ }
216};
217MODULE_DEVICE_TABLE(of, lpc18xx_adc_match);
218
219static struct platform_driver lpc18xx_adc_driver = {
220 .probe = lpc18xx_adc_probe,
221 .remove = lpc18xx_adc_remove,
222 .driver = {
223 .name = "lpc18xx-adc",
224 .of_match_table = lpc18xx_adc_match,
225 },
226};
227module_platform_driver(lpc18xx_adc_driver);
228
229MODULE_DESCRIPTION("LPC18xx ADC driver");
230MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
231MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c
index d7b36efd2f3c..d1172dc1e8e2 100644
--- a/drivers/iio/adc/mcp3422.c
+++ b/drivers/iio/adc/mcp3422.c
@@ -61,9 +61,9 @@
61 61
62static const int mcp3422_scales[4][4] = { 62static const int mcp3422_scales[4][4] = {
63 { 1000000, 500000, 250000, 125000 }, 63 { 1000000, 500000, 250000, 125000 },
64 { 250000 , 125000, 62500 , 31250 }, 64 { 250000, 125000, 62500, 31250 },
65 { 62500 , 31250 , 15625 , 7812 }, 65 { 62500, 31250, 15625, 7812 },
66 { 15625 , 7812 , 3906 , 1953 } }; 66 { 15625, 7812, 3906, 1953 } };
67 67
68/* Constant msleep times for data acquisitions */ 68/* Constant msleep times for data acquisitions */
69static const int mcp3422_read_times[4] = { 69static const int mcp3422_read_times[4] = {
diff --git a/drivers/iio/adc/mxs-lradc.c b/drivers/iio/adc/mxs-lradc.c
index 33051b87aac2..ad26da1edbee 100644
--- a/drivers/iio/adc/mxs-lradc.c
+++ b/drivers/iio/adc/mxs-lradc.c
@@ -686,6 +686,17 @@ static void mxs_lradc_prepare_pressure(struct mxs_lradc *lradc)
686 686
687static void mxs_lradc_enable_touch_detection(struct mxs_lradc *lradc) 687static void mxs_lradc_enable_touch_detection(struct mxs_lradc *lradc)
688{ 688{
689 /* Configure the touchscreen type */
690 if (lradc->soc == IMX28_LRADC) {
691 mxs_lradc_reg_clear(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
692 LRADC_CTRL0);
693
694 if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE)
695 mxs_lradc_reg_set(lradc,
696 LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
697 LRADC_CTRL0);
698 }
699
689 mxs_lradc_setup_touch_detection(lradc); 700 mxs_lradc_setup_touch_detection(lradc);
690 701
691 lradc->cur_plate = LRADC_TOUCH; 702 lradc->cur_plate = LRADC_TOUCH;
@@ -1127,6 +1138,7 @@ static int mxs_lradc_ts_register(struct mxs_lradc *lradc)
1127 __set_bit(EV_ABS, input->evbit); 1138 __set_bit(EV_ABS, input->evbit);
1128 __set_bit(EV_KEY, input->evbit); 1139 __set_bit(EV_KEY, input->evbit);
1129 __set_bit(BTN_TOUCH, input->keybit); 1140 __set_bit(BTN_TOUCH, input->keybit);
1141 __set_bit(INPUT_PROP_DIRECT, input->propbit);
1130 input_set_abs_params(input, ABS_X, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0); 1142 input_set_abs_params(input, ABS_X, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0);
1131 input_set_abs_params(input, ABS_Y, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0); 1143 input_set_abs_params(input, ABS_Y, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0);
1132 input_set_abs_params(input, ABS_PRESSURE, 0, LRADC_SINGLE_SAMPLE_MASK, 1144 input_set_abs_params(input, ABS_PRESSURE, 0, LRADC_SINGLE_SAMPLE_MASK,
@@ -1475,18 +1487,13 @@ static const struct iio_chan_spec mx28_lradc_chan_spec[] = {
1475 MXS_ADC_CHAN(15, IIO_VOLTAGE, "VDD5V"), 1487 MXS_ADC_CHAN(15, IIO_VOLTAGE, "VDD5V"),
1476}; 1488};
1477 1489
1478static int mxs_lradc_hw_init(struct mxs_lradc *lradc) 1490static void mxs_lradc_hw_init(struct mxs_lradc *lradc)
1479{ 1491{
1480 /* The ADC always uses DELAY CHANNEL 0. */ 1492 /* The ADC always uses DELAY CHANNEL 0. */
1481 const u32 adc_cfg = 1493 const u32 adc_cfg =
1482 (1 << (LRADC_DELAY_TRIGGER_DELAYS_OFFSET + 0)) | 1494 (1 << (LRADC_DELAY_TRIGGER_DELAYS_OFFSET + 0)) |
1483 (LRADC_DELAY_TIMER_PER << LRADC_DELAY_DELAY_OFFSET); 1495 (LRADC_DELAY_TIMER_PER << LRADC_DELAY_DELAY_OFFSET);
1484 1496
1485 int ret = stmp_reset_block(lradc->base);
1486
1487 if (ret)
1488 return ret;
1489
1490 /* Configure DELAY CHANNEL 0 for generic ADC sampling. */ 1497 /* Configure DELAY CHANNEL 0 for generic ADC sampling. */
1491 mxs_lradc_reg_wrt(lradc, adc_cfg, LRADC_DELAY(0)); 1498 mxs_lradc_reg_wrt(lradc, adc_cfg, LRADC_DELAY(0));
1492 1499
@@ -1495,20 +1502,8 @@ static int mxs_lradc_hw_init(struct mxs_lradc *lradc)
1495 mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2)); 1502 mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2));
1496 mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3)); 1503 mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3));
1497 1504
1498 /* Configure the touchscreen type */
1499 if (lradc->soc == IMX28_LRADC) {
1500 mxs_lradc_reg_clear(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
1501 LRADC_CTRL0);
1502
1503 if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE)
1504 mxs_lradc_reg_set(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE,
1505 LRADC_CTRL0);
1506 }
1507
1508 /* Start internal temperature sensing. */ 1505 /* Start internal temperature sensing. */
1509 mxs_lradc_reg_wrt(lradc, 0, LRADC_CTRL2); 1506 mxs_lradc_reg_wrt(lradc, 0, LRADC_CTRL2);
1510
1511 return 0;
1512} 1507}
1513 1508
1514static void mxs_lradc_hw_stop(struct mxs_lradc *lradc) 1509static void mxs_lradc_hw_stop(struct mxs_lradc *lradc)
@@ -1708,11 +1703,13 @@ static int mxs_lradc_probe(struct platform_device *pdev)
1708 } 1703 }
1709 } 1704 }
1710 1705
1711 /* Configure the hardware. */ 1706 ret = stmp_reset_block(lradc->base);
1712 ret = mxs_lradc_hw_init(lradc);
1713 if (ret) 1707 if (ret)
1714 goto err_dev; 1708 goto err_dev;
1715 1709
1710 /* Configure the hardware. */
1711 mxs_lradc_hw_init(lradc);
1712
1716 /* Register the touchscreen input device. */ 1713 /* Register the touchscreen input device. */
1717 if (touch_ret == 0) { 1714 if (touch_ret == 0) {
1718 ret = mxs_lradc_ts_register(lradc); 1715 ret = mxs_lradc_ts_register(lradc);
diff --git a/drivers/iio/adc/rockchip_saradc.c b/drivers/iio/adc/rockchip_saradc.c
index 9c311c1e1ac7..f9ad6c2d6821 100644
--- a/drivers/iio/adc/rockchip_saradc.c
+++ b/drivers/iio/adc/rockchip_saradc.c
@@ -159,6 +159,22 @@ static const struct rockchip_saradc_data rk3066_tsadc_data = {
159 .clk_rate = 50000, 159 .clk_rate = 50000,
160}; 160};
161 161
162static const struct iio_chan_spec rockchip_rk3399_saradc_iio_channels[] = {
163 ADC_CHANNEL(0, "adc0"),
164 ADC_CHANNEL(1, "adc1"),
165 ADC_CHANNEL(2, "adc2"),
166 ADC_CHANNEL(3, "adc3"),
167 ADC_CHANNEL(4, "adc4"),
168 ADC_CHANNEL(5, "adc5"),
169};
170
171static const struct rockchip_saradc_data rk3399_saradc_data = {
172 .num_bits = 10,
173 .channels = rockchip_rk3399_saradc_iio_channels,
174 .num_channels = ARRAY_SIZE(rockchip_rk3399_saradc_iio_channels),
175 .clk_rate = 1000000,
176};
177
162static const struct of_device_id rockchip_saradc_match[] = { 178static const struct of_device_id rockchip_saradc_match[] = {
163 { 179 {
164 .compatible = "rockchip,saradc", 180 .compatible = "rockchip,saradc",
@@ -166,6 +182,9 @@ static const struct of_device_id rockchip_saradc_match[] = {
166 }, { 182 }, {
167 .compatible = "rockchip,rk3066-tsadc", 183 .compatible = "rockchip,rk3066-tsadc",
168 .data = &rk3066_tsadc_data, 184 .data = &rk3066_tsadc_data,
185 }, {
186 .compatible = "rockchip,rk3399-saradc",
187 .data = &rk3399_saradc_data,
169 }, 188 },
170 {}, 189 {},
171}; 190};
diff --git a/drivers/iio/adc/ti-adc081c.c b/drivers/iio/adc/ti-adc081c.c
index ecbc12138d58..9fd032d9f402 100644
--- a/drivers/iio/adc/ti-adc081c.c
+++ b/drivers/iio/adc/ti-adc081c.c
@@ -1,9 +1,21 @@
1/* 1/*
2 * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver
3 *
2 * Copyright (C) 2012 Avionic Design GmbH 4 * Copyright (C) 2012 Avionic Design GmbH
5 * Copyright (C) 2016 Intel
3 * 6 *
4 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as 8 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation. 9 * published by the Free Software Foundation.
10 *
11 * Datasheets:
12 * http://www.ti.com/lit/ds/symlink/adc081c021.pdf
13 * http://www.ti.com/lit/ds/symlink/adc101c021.pdf
14 * http://www.ti.com/lit/ds/symlink/adc121c021.pdf
15 *
16 * The devices have a very similar interface and differ mostly in the number of
17 * bits handled. For the 8-bit and 10-bit models the least-significant 4 or 2
18 * bits of value registers are reserved.
7 */ 19 */
8 20
9#include <linux/err.h> 21#include <linux/err.h>
@@ -12,11 +24,17 @@
12#include <linux/of.h> 24#include <linux/of.h>
13 25
14#include <linux/iio/iio.h> 26#include <linux/iio/iio.h>
27#include <linux/iio/buffer.h>
28#include <linux/iio/trigger_consumer.h>
29#include <linux/iio/triggered_buffer.h>
15#include <linux/regulator/consumer.h> 30#include <linux/regulator/consumer.h>
16 31
17struct adc081c { 32struct adc081c {
18 struct i2c_client *i2c; 33 struct i2c_client *i2c;
19 struct regulator *ref; 34 struct regulator *ref;
35
36 /* 8, 10 or 12 */
37 int bits;
20}; 38};
21 39
22#define REG_CONV_RES 0x00 40#define REG_CONV_RES 0x00
@@ -34,7 +52,7 @@ static int adc081c_read_raw(struct iio_dev *iio,
34 if (err < 0) 52 if (err < 0)
35 return err; 53 return err;
36 54
37 *value = (err >> 4) & 0xff; 55 *value = (err & 0xFFF) >> (12 - adc->bits);
38 return IIO_VAL_INT; 56 return IIO_VAL_INT;
39 57
40 case IIO_CHAN_INFO_SCALE: 58 case IIO_CHAN_INFO_SCALE:
@@ -43,7 +61,7 @@ static int adc081c_read_raw(struct iio_dev *iio,
43 return err; 61 return err;
44 62
45 *value = err / 1000; 63 *value = err / 1000;
46 *shift = 8; 64 *shift = adc->bits;
47 65
48 return IIO_VAL_FRACTIONAL_LOG2; 66 return IIO_VAL_FRACTIONAL_LOG2;
49 67
@@ -54,10 +72,53 @@ static int adc081c_read_raw(struct iio_dev *iio,
54 return -EINVAL; 72 return -EINVAL;
55} 73}
56 74
57static const struct iio_chan_spec adc081c_channel = { 75#define ADCxx1C_CHAN(_bits) { \
58 .type = IIO_VOLTAGE, 76 .type = IIO_VOLTAGE, \
59 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 77 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
60 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 78 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
79 .scan_type = { \
80 .sign = 'u', \
81 .realbits = (_bits), \
82 .storagebits = 16, \
83 .shift = 12 - (_bits), \
84 .endianness = IIO_CPU, \
85 }, \
86}
87
88#define DEFINE_ADCxx1C_CHANNELS(_name, _bits) \
89 static const struct iio_chan_spec _name ## _channels[] = { \
90 ADCxx1C_CHAN((_bits)), \
91 IIO_CHAN_SOFT_TIMESTAMP(1), \
92 }; \
93
94#define ADC081C_NUM_CHANNELS 2
95
96struct adcxx1c_model {
97 const struct iio_chan_spec* channels;
98 int bits;
99};
100
101#define ADCxx1C_MODEL(_name, _bits) \
102 { \
103 .channels = _name ## _channels, \
104 .bits = (_bits), \
105 }
106
107DEFINE_ADCxx1C_CHANNELS(adc081c, 8);
108DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
109DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
110
111/* Model ids are indexes in _models array */
112enum adcxx1c_model_id {
113 ADC081C = 0,
114 ADC101C = 1,
115 ADC121C = 2,
116};
117
118static struct adcxx1c_model adcxx1c_models[] = {
119 ADCxx1C_MODEL(adc081c, 8),
120 ADCxx1C_MODEL(adc101c, 10),
121 ADCxx1C_MODEL(adc121c, 12),
61}; 122};
62 123
63static const struct iio_info adc081c_info = { 124static const struct iio_info adc081c_info = {
@@ -65,11 +126,30 @@ static const struct iio_info adc081c_info = {
65 .driver_module = THIS_MODULE, 126 .driver_module = THIS_MODULE,
66}; 127};
67 128
129static irqreturn_t adc081c_trigger_handler(int irq, void *p)
130{
131 struct iio_poll_func *pf = p;
132 struct iio_dev *indio_dev = pf->indio_dev;
133 struct adc081c *data = iio_priv(indio_dev);
134 u16 buf[8]; /* 2 bytes data + 6 bytes padding + 8 bytes timestamp */
135 int ret;
136
137 ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
138 if (ret < 0)
139 goto out;
140 buf[0] = ret;
141 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns());
142out:
143 iio_trigger_notify_done(indio_dev->trig);
144 return IRQ_HANDLED;
145}
146
68static int adc081c_probe(struct i2c_client *client, 147static int adc081c_probe(struct i2c_client *client,
69 const struct i2c_device_id *id) 148 const struct i2c_device_id *id)
70{ 149{
71 struct iio_dev *iio; 150 struct iio_dev *iio;
72 struct adc081c *adc; 151 struct adc081c *adc;
152 struct adcxx1c_model *model = &adcxx1c_models[id->driver_data];
73 int err; 153 int err;
74 154
75 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) 155 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
@@ -81,6 +161,7 @@ static int adc081c_probe(struct i2c_client *client,
81 161
82 adc = iio_priv(iio); 162 adc = iio_priv(iio);
83 adc->i2c = client; 163 adc->i2c = client;
164 adc->bits = model->bits;
84 165
85 adc->ref = devm_regulator_get(&client->dev, "vref"); 166 adc->ref = devm_regulator_get(&client->dev, "vref");
86 if (IS_ERR(adc->ref)) 167 if (IS_ERR(adc->ref))
@@ -95,18 +176,26 @@ static int adc081c_probe(struct i2c_client *client,
95 iio->modes = INDIO_DIRECT_MODE; 176 iio->modes = INDIO_DIRECT_MODE;
96 iio->info = &adc081c_info; 177 iio->info = &adc081c_info;
97 178
98 iio->channels = &adc081c_channel; 179 iio->channels = model->channels;
99 iio->num_channels = 1; 180 iio->num_channels = ADC081C_NUM_CHANNELS;
181
182 err = iio_triggered_buffer_setup(iio, NULL, adc081c_trigger_handler, NULL);
183 if (err < 0) {
184 dev_err(&client->dev, "iio triggered buffer setup failed\n");
185 goto err_regulator_disable;
186 }
100 187
101 err = iio_device_register(iio); 188 err = iio_device_register(iio);
102 if (err < 0) 189 if (err < 0)
103 goto regulator_disable; 190 goto err_buffer_cleanup;
104 191
105 i2c_set_clientdata(client, iio); 192 i2c_set_clientdata(client, iio);
106 193
107 return 0; 194 return 0;
108 195
109regulator_disable: 196err_buffer_cleanup:
197 iio_triggered_buffer_cleanup(iio);
198err_regulator_disable:
110 regulator_disable(adc->ref); 199 regulator_disable(adc->ref);
111 200
112 return err; 201 return err;
@@ -118,13 +207,16 @@ static int adc081c_remove(struct i2c_client *client)
118 struct adc081c *adc = iio_priv(iio); 207 struct adc081c *adc = iio_priv(iio);
119 208
120 iio_device_unregister(iio); 209 iio_device_unregister(iio);
210 iio_triggered_buffer_cleanup(iio);
121 regulator_disable(adc->ref); 211 regulator_disable(adc->ref);
122 212
123 return 0; 213 return 0;
124} 214}
125 215
126static const struct i2c_device_id adc081c_id[] = { 216static const struct i2c_device_id adc081c_id[] = {
127 { "adc081c", 0 }, 217 { "adc081c", ADC081C },
218 { "adc101c", ADC101C },
219 { "adc121c", ADC121C },
128 { } 220 { }
129}; 221};
130MODULE_DEVICE_TABLE(i2c, adc081c_id); 222MODULE_DEVICE_TABLE(i2c, adc081c_id);
@@ -132,6 +224,8 @@ MODULE_DEVICE_TABLE(i2c, adc081c_id);
132#ifdef CONFIG_OF 224#ifdef CONFIG_OF
133static const struct of_device_id adc081c_of_match[] = { 225static const struct of_device_id adc081c_of_match[] = {
134 { .compatible = "ti,adc081c" }, 226 { .compatible = "ti,adc081c" },
227 { .compatible = "ti,adc101c" },
228 { .compatible = "ti,adc121c" },
135 { } 229 { }
136}; 230};
137MODULE_DEVICE_TABLE(of, adc081c_of_match); 231MODULE_DEVICE_TABLE(of, adc081c_of_match);
@@ -149,5 +243,5 @@ static struct i2c_driver adc081c_driver = {
149module_i2c_driver(adc081c_driver); 243module_i2c_driver(adc081c_driver);
150 244
151MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); 245MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
152MODULE_DESCRIPTION("Texas Instruments ADC081C021/027 driver"); 246MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
153MODULE_LICENSE("GPL v2"); 247MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/adc/vf610_adc.c b/drivers/iio/adc/vf610_adc.c
index b10f629cc44b..653bf1379d2e 100644
--- a/drivers/iio/adc/vf610_adc.c
+++ b/drivers/iio/adc/vf610_adc.c
@@ -714,19 +714,19 @@ static int vf610_write_raw(struct iio_dev *indio_dev,
714 int i; 714 int i;
715 715
716 switch (mask) { 716 switch (mask) {
717 case IIO_CHAN_INFO_SAMP_FREQ: 717 case IIO_CHAN_INFO_SAMP_FREQ:
718 for (i = 0; 718 for (i = 0;
719 i < ARRAY_SIZE(info->sample_freq_avail); 719 i < ARRAY_SIZE(info->sample_freq_avail);
720 i++) 720 i++)
721 if (val == info->sample_freq_avail[i]) { 721 if (val == info->sample_freq_avail[i]) {
722 info->adc_feature.sample_rate = i; 722 info->adc_feature.sample_rate = i;
723 vf610_adc_sample_set(info); 723 vf610_adc_sample_set(info);
724 return 0; 724 return 0;
725 } 725 }
726 break; 726 break;
727 727
728 default: 728 default:
729 break; 729 break;
730 } 730 }
731 731
732 return -EINVAL; 732 return -EINVAL;
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
index 595511022795..5b41f9d0d4f3 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
@@ -115,7 +115,7 @@ int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
115 return ret; 115 return ret;
116 } 116 }
117 117
118 return 0; 118 return 0;
119#else 119#else
120 atomic_set(&st->user_requested_state, state); 120 atomic_set(&st->user_requested_state, state);
121 return _hid_sensor_power_state(st, state); 121 return _hid_sensor_power_state(st, state);
diff --git a/drivers/iio/common/ms_sensors/ms_sensors_i2c.c b/drivers/iio/common/ms_sensors/ms_sensors_i2c.c
index 669dc7c270f5..ecf7721ecaf4 100644
--- a/drivers/iio/common/ms_sensors/ms_sensors_i2c.c
+++ b/drivers/iio/common/ms_sensors/ms_sensors_i2c.c
@@ -106,7 +106,7 @@ int ms_sensors_convert_and_read(void *cli, u8 conv, u8 rd,
106 unsigned int delay, u32 *adc) 106 unsigned int delay, u32 *adc)
107{ 107{
108 int ret; 108 int ret;
109 __be32 buf = 0; 109 __be32 buf = 0;
110 struct i2c_client *client = (struct i2c_client *)cli; 110 struct i2c_client *client = (struct i2c_client *)cli;
111 111
112 /* Trigger conversion */ 112 /* Trigger conversion */
diff --git a/drivers/iio/common/st_sensors/st_sensors_buffer.c b/drivers/iio/common/st_sensors/st_sensors_buffer.c
index e18bc6782256..c55898543a47 100644
--- a/drivers/iio/common/st_sensors/st_sensors_buffer.c
+++ b/drivers/iio/common/st_sensors/st_sensors_buffer.c
@@ -24,81 +24,30 @@
24 24
25int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf) 25int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
26{ 26{
27 u8 *addr; 27 int i, len;
28 int i, n = 0, len; 28 int total = 0;
29 struct st_sensor_data *sdata = iio_priv(indio_dev); 29 struct st_sensor_data *sdata = iio_priv(indio_dev);
30 unsigned int num_data_channels = sdata->num_data_channels; 30 unsigned int num_data_channels = sdata->num_data_channels;
31 unsigned int byte_for_channel =
32 indio_dev->channels[0].scan_type.storagebits >> 3;
33
34 addr = kmalloc(num_data_channels, GFP_KERNEL);
35 if (!addr) {
36 len = -ENOMEM;
37 goto st_sensors_get_buffer_element_error;
38 }
39 31
40 for (i = 0; i < num_data_channels; i++) { 32 for (i = 0; i < num_data_channels; i++) {
33 unsigned int bytes_to_read;
34
41 if (test_bit(i, indio_dev->active_scan_mask)) { 35 if (test_bit(i, indio_dev->active_scan_mask)) {
42 addr[n] = indio_dev->channels[i].address; 36 bytes_to_read = indio_dev->channels[i].scan_type.storagebits >> 3;
43 n++;
44 }
45 }
46 switch (n) {
47 case 1:
48 len = sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev,
49 addr[0], byte_for_channel, buf, sdata->multiread_bit);
50 break;
51 case 2:
52 if ((addr[1] - addr[0]) == byte_for_channel) {
53 len = sdata->tf->read_multiple_byte(&sdata->tb, 37 len = sdata->tf->read_multiple_byte(&sdata->tb,
54 sdata->dev, addr[0], byte_for_channel * n, 38 sdata->dev, indio_dev->channels[i].address,
55 buf, sdata->multiread_bit); 39 bytes_to_read,
56 } else { 40 buf + total, sdata->multiread_bit);
57 u8 *rx_array;
58 rx_array = kmalloc(byte_for_channel * num_data_channels,
59 GFP_KERNEL);
60 if (!rx_array) {
61 len = -ENOMEM;
62 goto st_sensors_free_memory;
63 }
64 41
65 len = sdata->tf->read_multiple_byte(&sdata->tb, 42 if (len < bytes_to_read)
66 sdata->dev, addr[0], 43 return -EIO;
67 byte_for_channel * num_data_channels, 44
68 rx_array, sdata->multiread_bit); 45 /* Advance the buffer pointer */
69 if (len < 0) { 46 total += len;
70 kfree(rx_array);
71 goto st_sensors_free_memory;
72 }
73
74 for (i = 0; i < n * byte_for_channel; i++) {
75 if (i < n)
76 buf[i] = rx_array[i];
77 else
78 buf[i] = rx_array[n + i];
79 }
80 kfree(rx_array);
81 len = byte_for_channel * n;
82 } 47 }
83 break;
84 case 3:
85 len = sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev,
86 addr[0], byte_for_channel * num_data_channels,
87 buf, sdata->multiread_bit);
88 break;
89 default:
90 len = -EINVAL;
91 goto st_sensors_free_memory;
92 }
93 if (len != byte_for_channel * n) {
94 len = -EIO;
95 goto st_sensors_free_memory;
96 } 48 }
97 49
98st_sensors_free_memory: 50 return total;
99 kfree(addr);
100st_sensors_get_buffer_element_error:
101 return len;
102} 51}
103EXPORT_SYMBOL(st_sensors_get_buffer_element); 52EXPORT_SYMBOL(st_sensors_get_buffer_element);
104 53
@@ -109,6 +58,24 @@ irqreturn_t st_sensors_trigger_handler(int irq, void *p)
109 struct iio_dev *indio_dev = pf->indio_dev; 58 struct iio_dev *indio_dev = pf->indio_dev;
110 struct st_sensor_data *sdata = iio_priv(indio_dev); 59 struct st_sensor_data *sdata = iio_priv(indio_dev);
111 60
61 /* If we have a status register, check if this IRQ came from us */
62 if (sdata->sensor_settings->drdy_irq.addr_stat_drdy) {
63 u8 status;
64
65 len = sdata->tf->read_byte(&sdata->tb, sdata->dev,
66 sdata->sensor_settings->drdy_irq.addr_stat_drdy,
67 &status);
68 if (len < 0)
69 dev_err(sdata->dev, "could not read channel status\n");
70
71 /*
72 * If this was not caused by any channels on this sensor,
73 * return IRQ_NONE
74 */
75 if (!(status & (u8)indio_dev->active_scan_mask[0]))
76 return IRQ_NONE;
77 }
78
112 len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data); 79 len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data);
113 if (len < 0) 80 if (len < 0)
114 goto st_sensors_get_buffer_element_error; 81 goto st_sensors_get_buffer_element_error;
diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
index f5a2d445d0c0..dffe00692169 100644
--- a/drivers/iio/common/st_sensors/st_sensors_core.c
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -301,6 +301,14 @@ static int st_sensors_set_drdy_int_pin(struct iio_dev *indio_dev,
301 return -EINVAL; 301 return -EINVAL;
302 } 302 }
303 303
304 if (pdata->open_drain) {
305 if (!sdata->sensor_settings->drdy_irq.addr_od)
306 dev_err(&indio_dev->dev,
307 "open drain requested but unsupported.\n");
308 else
309 sdata->int_pin_open_drain = true;
310 }
311
304 return 0; 312 return 0;
305} 313}
306 314
@@ -321,6 +329,8 @@ static struct st_sensors_platform_data *st_sensors_of_probe(struct device *dev,
321 else 329 else
322 pdata->drdy_int_pin = defdata ? defdata->drdy_int_pin : 0; 330 pdata->drdy_int_pin = defdata ? defdata->drdy_int_pin : 0;
323 331
332 pdata->open_drain = of_property_read_bool(np, "drive-open-drain");
333
324 return pdata; 334 return pdata;
325} 335}
326#else 336#else
@@ -374,6 +384,16 @@ int st_sensors_init_sensor(struct iio_dev *indio_dev,
374 return err; 384 return err;
375 } 385 }
376 386
387 if (sdata->int_pin_open_drain) {
388 dev_info(&indio_dev->dev,
389 "set interrupt line to open drain mode\n");
390 err = st_sensors_write_data_with_mask(indio_dev,
391 sdata->sensor_settings->drdy_irq.addr_od,
392 sdata->sensor_settings->drdy_irq.mask_od, 1);
393 if (err < 0)
394 return err;
395 }
396
377 err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS); 397 err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS);
378 398
379 return err; 399 return err;
diff --git a/drivers/iio/common/st_sensors/st_sensors_trigger.c b/drivers/iio/common/st_sensors/st_sensors_trigger.c
index 6a8c98327945..da72279fcf99 100644
--- a/drivers/iio/common/st_sensors/st_sensors_trigger.c
+++ b/drivers/iio/common/st_sensors/st_sensors_trigger.c
@@ -64,6 +64,19 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
64 "rising edge\n", irq_trig); 64 "rising edge\n", irq_trig);
65 irq_trig = IRQF_TRIGGER_RISING; 65 irq_trig = IRQF_TRIGGER_RISING;
66 } 66 }
67
68 /*
69 * If the interrupt pin is Open Drain, by definition this
70 * means that the interrupt line may be shared with other
71 * peripherals. But to do this we also need to have a status
72 * register and mask to figure out if this sensor was firing
73 * the IRQ or not, so we can tell the interrupt handle that
74 * it was "our" interrupt.
75 */
76 if (sdata->int_pin_open_drain &&
77 sdata->sensor_settings->drdy_irq.addr_stat_drdy)
78 irq_trig |= IRQF_SHARED;
79
67 err = request_threaded_irq(irq, 80 err = request_threaded_irq(irq,
68 iio_trigger_generic_data_rdy_poll, 81 iio_trigger_generic_data_rdy_poll,
69 NULL, 82 NULL,
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
index 6abcfb8597d9..e63b957c985f 100644
--- a/drivers/iio/dac/Kconfig
+++ b/drivers/iio/dac/Kconfig
@@ -74,6 +74,33 @@ config AD5449
74 To compile this driver as a module, choose M here: the 74 To compile this driver as a module, choose M here: the
75 module will be called ad5449. 75 module will be called ad5449.
76 76
77config AD5592R_BASE
78 tristate
79
80config AD5592R
81 tristate "Analog Devices AD5592R ADC/DAC driver"
82 depends on SPI_MASTER
83 select GPIOLIB
84 select AD5592R_BASE
85 help
86 Say yes here to build support for Analog Devices AD5592R
87 Digital to Analog / Analog to Digital Converter.
88
89 To compile this driver as a module, choose M here: the
90 module will be called ad5592r.
91
92config AD5593R
93 tristate "Analog Devices AD5593R ADC/DAC driver"
94 depends on I2C
95 select GPIOLIB
96 select AD5592R_BASE
97 help
98 Say yes here to build support for Analog Devices AD5593R
99 Digital to Analog / Analog to Digital Converter.
100
101 To compile this driver as a module, choose M here: the
102 module will be called ad5593r.
103
77config AD5504 104config AD5504
78 tristate "Analog Devices AD5504/AD5501 DAC SPI driver" 105 tristate "Analog Devices AD5504/AD5501 DAC SPI driver"
79 depends on SPI 106 depends on SPI
@@ -154,6 +181,16 @@ config AD7303
154 To compile this driver as module choose M here: the module will be called 181 To compile this driver as module choose M here: the module will be called
155 ad7303. 182 ad7303.
156 183
184config LPC18XX_DAC
185 tristate "NXP LPC18xx DAC driver"
186 depends on ARCH_LPC18XX || COMPILE_TEST
187 depends on OF && HAS_IOMEM
188 help
189 Say yes here to build support for NXP LPC18XX DAC.
190
191 To compile this driver as a module, choose M here: the module will be
192 called lpc18xx_dac.
193
157config M62332 194config M62332
158 tristate "Mitsubishi M62332 DAC driver" 195 tristate "Mitsubishi M62332 DAC driver"
159 depends on I2C 196 depends on I2C
diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
index 67b48429686d..8b78d5ca9b11 100644
--- a/drivers/iio/dac/Makefile
+++ b/drivers/iio/dac/Makefile
@@ -11,12 +11,16 @@ obj-$(CONFIG_AD5064) += ad5064.o
11obj-$(CONFIG_AD5504) += ad5504.o 11obj-$(CONFIG_AD5504) += ad5504.o
12obj-$(CONFIG_AD5446) += ad5446.o 12obj-$(CONFIG_AD5446) += ad5446.o
13obj-$(CONFIG_AD5449) += ad5449.o 13obj-$(CONFIG_AD5449) += ad5449.o
14obj-$(CONFIG_AD5592R_BASE) += ad5592r-base.o
15obj-$(CONFIG_AD5592R) += ad5592r.o
16obj-$(CONFIG_AD5593R) += ad5593r.o
14obj-$(CONFIG_AD5755) += ad5755.o 17obj-$(CONFIG_AD5755) += ad5755.o
15obj-$(CONFIG_AD5761) += ad5761.o 18obj-$(CONFIG_AD5761) += ad5761.o
16obj-$(CONFIG_AD5764) += ad5764.o 19obj-$(CONFIG_AD5764) += ad5764.o
17obj-$(CONFIG_AD5791) += ad5791.o 20obj-$(CONFIG_AD5791) += ad5791.o
18obj-$(CONFIG_AD5686) += ad5686.o 21obj-$(CONFIG_AD5686) += ad5686.o
19obj-$(CONFIG_AD7303) += ad7303.o 22obj-$(CONFIG_AD7303) += ad7303.o
23obj-$(CONFIG_LPC18XX_DAC) += lpc18xx_dac.o
20obj-$(CONFIG_M62332) += m62332.o 24obj-$(CONFIG_M62332) += m62332.o
21obj-$(CONFIG_MAX517) += max517.o 25obj-$(CONFIG_MAX517) += max517.o
22obj-$(CONFIG_MAX5821) += max5821.o 26obj-$(CONFIG_MAX5821) += max5821.o
diff --git a/drivers/iio/dac/ad5592r-base.c b/drivers/iio/dac/ad5592r-base.c
new file mode 100644
index 000000000000..948f600e7059
--- /dev/null
+++ b/drivers/iio/dac/ad5592r-base.c
@@ -0,0 +1,691 @@
1/*
2 * AD5592R Digital <-> Analog converters driver
3 *
4 * Copyright 2014-2016 Analog Devices Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * Licensed under the GPL-2.
8 */
9
10#include <linux/bitops.h>
11#include <linux/delay.h>
12#include <linux/iio/iio.h>
13#include <linux/module.h>
14#include <linux/mutex.h>
15#include <linux/of.h>
16#include <linux/regulator/consumer.h>
17#include <linux/gpio/consumer.h>
18#include <linux/gpio/driver.h>
19#include <linux/gpio.h>
20#include <linux/property.h>
21
22#include <dt-bindings/iio/adi,ad5592r.h>
23
24#include "ad5592r-base.h"
25
26static int ad5592r_gpio_get(struct gpio_chip *chip, unsigned offset)
27{
28 struct ad5592r_state *st = gpiochip_get_data(chip);
29 int ret = 0;
30 u8 val;
31
32 mutex_lock(&st->gpio_lock);
33
34 if (st->gpio_out & BIT(offset))
35 val = st->gpio_val;
36 else
37 ret = st->ops->gpio_read(st, &val);
38
39 mutex_unlock(&st->gpio_lock);
40
41 if (ret < 0)
42 return ret;
43
44 return !!(val & BIT(offset));
45}
46
47static void ad5592r_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
48{
49 struct ad5592r_state *st = gpiochip_get_data(chip);
50
51 mutex_lock(&st->gpio_lock);
52
53 if (value)
54 st->gpio_val |= BIT(offset);
55 else
56 st->gpio_val &= ~BIT(offset);
57
58 st->ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val);
59
60 mutex_unlock(&st->gpio_lock);
61}
62
63static int ad5592r_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
64{
65 struct ad5592r_state *st = gpiochip_get_data(chip);
66 int ret;
67
68 mutex_lock(&st->gpio_lock);
69
70 st->gpio_out &= ~BIT(offset);
71 st->gpio_in |= BIT(offset);
72
73 ret = st->ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out);
74 if (ret < 0)
75 goto err_unlock;
76
77 ret = st->ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in);
78
79err_unlock:
80 mutex_unlock(&st->gpio_lock);
81
82 return ret;
83}
84
85static int ad5592r_gpio_direction_output(struct gpio_chip *chip,
86 unsigned offset, int value)
87{
88 struct ad5592r_state *st = gpiochip_get_data(chip);
89 int ret;
90
91 mutex_lock(&st->gpio_lock);
92
93 if (value)
94 st->gpio_val |= BIT(offset);
95 else
96 st->gpio_val &= ~BIT(offset);
97
98 st->gpio_in &= ~BIT(offset);
99 st->gpio_out |= BIT(offset);
100
101 ret = st->ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val);
102 if (ret < 0)
103 goto err_unlock;
104
105 ret = st->ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out);
106 if (ret < 0)
107 goto err_unlock;
108
109 ret = st->ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in);
110
111err_unlock:
112 mutex_unlock(&st->gpio_lock);
113
114 return ret;
115}
116
117static int ad5592r_gpio_request(struct gpio_chip *chip, unsigned offset)
118{
119 struct ad5592r_state *st = gpiochip_get_data(chip);
120
121 if (!(st->gpio_map & BIT(offset))) {
122 dev_err(st->dev, "GPIO %d is reserved by alternate function\n",
123 offset);
124 return -ENODEV;
125 }
126
127 return 0;
128}
129
130static int ad5592r_gpio_init(struct ad5592r_state *st)
131{
132 if (!st->gpio_map)
133 return 0;
134
135 st->gpiochip.label = dev_name(st->dev);
136 st->gpiochip.base = -1;
137 st->gpiochip.ngpio = 8;
138 st->gpiochip.parent = st->dev;
139 st->gpiochip.can_sleep = true;
140 st->gpiochip.direction_input = ad5592r_gpio_direction_input;
141 st->gpiochip.direction_output = ad5592r_gpio_direction_output;
142 st->gpiochip.get = ad5592r_gpio_get;
143 st->gpiochip.set = ad5592r_gpio_set;
144 st->gpiochip.request = ad5592r_gpio_request;
145 st->gpiochip.owner = THIS_MODULE;
146
147 mutex_init(&st->gpio_lock);
148
149 return gpiochip_add_data(&st->gpiochip, st);
150}
151
152static void ad5592r_gpio_cleanup(struct ad5592r_state *st)
153{
154 if (st->gpio_map)
155 gpiochip_remove(&st->gpiochip);
156}
157
158static int ad5592r_reset(struct ad5592r_state *st)
159{
160 struct gpio_desc *gpio;
161 struct iio_dev *iio_dev = iio_priv_to_dev(st);
162
163 gpio = devm_gpiod_get_optional(st->dev, "reset", GPIOD_OUT_LOW);
164 if (IS_ERR(gpio))
165 return PTR_ERR(gpio);
166
167 if (gpio) {
168 udelay(1);
169 gpiod_set_value(gpio, 1);
170 } else {
171 mutex_lock(&iio_dev->mlock);
172 /* Writing this magic value resets the device */
173 st->ops->reg_write(st, AD5592R_REG_RESET, 0xdac);
174 mutex_unlock(&iio_dev->mlock);
175 }
176
177 udelay(250);
178
179 return 0;
180}
181
182static int ad5592r_get_vref(struct ad5592r_state *st)
183{
184 int ret;
185
186 if (st->reg) {
187 ret = regulator_get_voltage(st->reg);
188 if (ret < 0)
189 return ret;
190
191 return ret / 1000;
192 } else {
193 return 2500;
194 }
195}
196
197static int ad5592r_set_channel_modes(struct ad5592r_state *st)
198{
199 const struct ad5592r_rw_ops *ops = st->ops;
200 int ret;
201 unsigned i;
202 struct iio_dev *iio_dev = iio_priv_to_dev(st);
203 u8 pulldown = 0, tristate = 0, dac = 0, adc = 0;
204 u16 read_back;
205
206 for (i = 0; i < st->num_channels; i++) {
207 switch (st->channel_modes[i]) {
208 case CH_MODE_DAC:
209 dac |= BIT(i);
210 break;
211
212 case CH_MODE_ADC:
213 adc |= BIT(i);
214 break;
215
216 case CH_MODE_DAC_AND_ADC:
217 dac |= BIT(i);
218 adc |= BIT(i);
219 break;
220
221 case CH_MODE_GPIO:
222 st->gpio_map |= BIT(i);
223 st->gpio_in |= BIT(i); /* Default to input */
224 break;
225
226 case CH_MODE_UNUSED:
227 /* fall-through */
228 default:
229 switch (st->channel_offstate[i]) {
230 case CH_OFFSTATE_OUT_TRISTATE:
231 tristate |= BIT(i);
232 break;
233
234 case CH_OFFSTATE_OUT_LOW:
235 st->gpio_out |= BIT(i);
236 break;
237
238 case CH_OFFSTATE_OUT_HIGH:
239 st->gpio_out |= BIT(i);
240 st->gpio_val |= BIT(i);
241 break;
242
243 case CH_OFFSTATE_PULLDOWN:
244 /* fall-through */
245 default:
246 pulldown |= BIT(i);
247 break;
248 }
249 }
250 }
251
252 mutex_lock(&iio_dev->mlock);
253
254 /* Pull down unused pins to GND */
255 ret = ops->reg_write(st, AD5592R_REG_PULLDOWN, pulldown);
256 if (ret)
257 goto err_unlock;
258
259 ret = ops->reg_write(st, AD5592R_REG_TRISTATE, tristate);
260 if (ret)
261 goto err_unlock;
262
263 /* Configure pins that we use */
264 ret = ops->reg_write(st, AD5592R_REG_DAC_EN, dac);
265 if (ret)
266 goto err_unlock;
267
268 ret = ops->reg_write(st, AD5592R_REG_ADC_EN, adc);
269 if (ret)
270 goto err_unlock;
271
272 ret = ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val);
273 if (ret)
274 goto err_unlock;
275
276 ret = ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out);
277 if (ret)
278 goto err_unlock;
279
280 ret = ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in);
281 if (ret)
282 goto err_unlock;
283
284 /* Verify that we can read back at least one register */
285 ret = ops->reg_read(st, AD5592R_REG_ADC_EN, &read_back);
286 if (!ret && (read_back & 0xff) != adc)
287 ret = -EIO;
288
289err_unlock:
290 mutex_unlock(&iio_dev->mlock);
291 return ret;
292}
293
294static int ad5592r_reset_channel_modes(struct ad5592r_state *st)
295{
296 int i;
297
298 for (i = 0; i < ARRAY_SIZE(st->channel_modes); i++)
299 st->channel_modes[i] = CH_MODE_UNUSED;
300
301 return ad5592r_set_channel_modes(st);
302}
303
304static int ad5592r_write_raw(struct iio_dev *iio_dev,
305 struct iio_chan_spec const *chan, int val, int val2, long mask)
306{
307 struct ad5592r_state *st = iio_priv(iio_dev);
308 int ret;
309
310 switch (mask) {
311 case IIO_CHAN_INFO_RAW:
312
313 if (val >= (1 << chan->scan_type.realbits) || val < 0)
314 return -EINVAL;
315
316 if (!chan->output)
317 return -EINVAL;
318
319 mutex_lock(&iio_dev->mlock);
320 ret = st->ops->write_dac(st, chan->channel, val);
321 if (!ret)
322 st->cached_dac[chan->channel] = val;
323 mutex_unlock(&iio_dev->mlock);
324 return ret;
325 case IIO_CHAN_INFO_SCALE:
326 if (chan->type == IIO_VOLTAGE) {
327 bool gain;
328
329 if (val == st->scale_avail[0][0] &&
330 val2 == st->scale_avail[0][1])
331 gain = false;
332 else if (val == st->scale_avail[1][0] &&
333 val2 == st->scale_avail[1][1])
334 gain = true;
335 else
336 return -EINVAL;
337
338 mutex_lock(&iio_dev->mlock);
339
340 ret = st->ops->reg_read(st, AD5592R_REG_CTRL,
341 &st->cached_gp_ctrl);
342 if (ret < 0) {
343 mutex_unlock(&iio_dev->mlock);
344 return ret;
345 }
346
347 if (chan->output) {
348 if (gain)
349 st->cached_gp_ctrl |=
350 AD5592R_REG_CTRL_DAC_RANGE;
351 else
352 st->cached_gp_ctrl &=
353 ~AD5592R_REG_CTRL_DAC_RANGE;
354 } else {
355 if (gain)
356 st->cached_gp_ctrl |=
357 AD5592R_REG_CTRL_ADC_RANGE;
358 else
359 st->cached_gp_ctrl &=
360 ~AD5592R_REG_CTRL_ADC_RANGE;
361 }
362
363 ret = st->ops->reg_write(st, AD5592R_REG_CTRL,
364 st->cached_gp_ctrl);
365 mutex_unlock(&iio_dev->mlock);
366
367 return ret;
368 }
369 break;
370 default:
371 return -EINVAL;
372 }
373
374 return 0;
375}
376
377static int ad5592r_read_raw(struct iio_dev *iio_dev,
378 struct iio_chan_spec const *chan,
379 int *val, int *val2, long m)
380{
381 struct ad5592r_state *st = iio_priv(iio_dev);
382 u16 read_val;
383 int ret;
384
385 switch (m) {
386 case IIO_CHAN_INFO_RAW:
387 mutex_lock(&iio_dev->mlock);
388
389 if (!chan->output) {
390 ret = st->ops->read_adc(st, chan->channel, &read_val);
391 if (ret)
392 goto unlock;
393
394 if ((read_val >> 12 & 0x7) != (chan->channel & 0x7)) {
395 dev_err(st->dev, "Error while reading channel %u\n",
396 chan->channel);
397 ret = -EIO;
398 goto unlock;
399 }
400
401 read_val &= GENMASK(11, 0);
402
403 } else {
404 read_val = st->cached_dac[chan->channel];
405 }
406
407 dev_dbg(st->dev, "Channel %u read: 0x%04hX\n",
408 chan->channel, read_val);
409
410 *val = (int) read_val;
411 ret = IIO_VAL_INT;
412 break;
413 case IIO_CHAN_INFO_SCALE:
414 *val = ad5592r_get_vref(st);
415
416 if (chan->type == IIO_TEMP) {
417 s64 tmp = *val * (3767897513LL / 25LL);
418 *val = div_s64_rem(tmp, 1000000000LL, val2);
419
420 ret = IIO_VAL_INT_PLUS_MICRO;
421 } else {
422 int mult;
423
424 mutex_lock(&iio_dev->mlock);
425
426 if (chan->output)
427 mult = !!(st->cached_gp_ctrl &
428 AD5592R_REG_CTRL_DAC_RANGE);
429 else
430 mult = !!(st->cached_gp_ctrl &
431 AD5592R_REG_CTRL_ADC_RANGE);
432
433 *val *= ++mult;
434
435 *val2 = chan->scan_type.realbits;
436 ret = IIO_VAL_FRACTIONAL_LOG2;
437 }
438 break;
439 case IIO_CHAN_INFO_OFFSET:
440 ret = ad5592r_get_vref(st);
441
442 mutex_lock(&iio_dev->mlock);
443
444 if (st->cached_gp_ctrl & AD5592R_REG_CTRL_ADC_RANGE)
445 *val = (-34365 * 25) / ret;
446 else
447 *val = (-75365 * 25) / ret;
448 ret = IIO_VAL_INT;
449 break;
450 default:
451 ret = -EINVAL;
452 }
453
454unlock:
455 mutex_unlock(&iio_dev->mlock);
456 return ret;
457}
458
459static int ad5592r_write_raw_get_fmt(struct iio_dev *indio_dev,
460 struct iio_chan_spec const *chan, long mask)
461{
462 switch (mask) {
463 case IIO_CHAN_INFO_SCALE:
464 return IIO_VAL_INT_PLUS_NANO;
465
466 default:
467 return IIO_VAL_INT_PLUS_MICRO;
468 }
469
470 return -EINVAL;
471}
472
473static const struct iio_info ad5592r_info = {
474 .read_raw = ad5592r_read_raw,
475 .write_raw = ad5592r_write_raw,
476 .write_raw_get_fmt = ad5592r_write_raw_get_fmt,
477 .driver_module = THIS_MODULE,
478};
479
480static ssize_t ad5592r_show_scale_available(struct iio_dev *iio_dev,
481 uintptr_t private,
482 const struct iio_chan_spec *chan,
483 char *buf)
484{
485 struct ad5592r_state *st = iio_priv(iio_dev);
486
487 return sprintf(buf, "%d.%09u %d.%09u\n",
488 st->scale_avail[0][0], st->scale_avail[0][1],
489 st->scale_avail[1][0], st->scale_avail[1][1]);
490}
491
492static struct iio_chan_spec_ext_info ad5592r_ext_info[] = {
493 {
494 .name = "scale_available",
495 .read = ad5592r_show_scale_available,
496 .shared = true,
497 },
498 {},
499};
500
501static void ad5592r_setup_channel(struct iio_dev *iio_dev,
502 struct iio_chan_spec *chan, bool output, unsigned id)
503{
504 chan->type = IIO_VOLTAGE;
505 chan->indexed = 1;
506 chan->output = output;
507 chan->channel = id;
508 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
509 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
510 chan->scan_type.sign = 'u';
511 chan->scan_type.realbits = 12;
512 chan->scan_type.storagebits = 16;
513 chan->ext_info = ad5592r_ext_info;
514}
515
516static int ad5592r_alloc_channels(struct ad5592r_state *st)
517{
518 unsigned i, curr_channel = 0,
519 num_channels = st->num_channels;
520 struct iio_dev *iio_dev = iio_priv_to_dev(st);
521 struct iio_chan_spec *channels;
522 struct fwnode_handle *child;
523 u32 reg, tmp;
524 int ret;
525
526 device_for_each_child_node(st->dev, child) {
527 ret = fwnode_property_read_u32(child, "reg", &reg);
528 if (ret || reg > ARRAY_SIZE(st->channel_modes))
529 continue;
530
531 ret = fwnode_property_read_u32(child, "adi,mode", &tmp);
532 if (!ret)
533 st->channel_modes[reg] = tmp;
534
535 fwnode_property_read_u32(child, "adi,off-state", &tmp);
536 if (!ret)
537 st->channel_offstate[reg] = tmp;
538 }
539
540 channels = devm_kzalloc(st->dev,
541 (1 + 2 * num_channels) * sizeof(*channels), GFP_KERNEL);
542 if (!channels)
543 return -ENOMEM;
544
545 for (i = 0; i < num_channels; i++) {
546 switch (st->channel_modes[i]) {
547 case CH_MODE_DAC:
548 ad5592r_setup_channel(iio_dev, &channels[curr_channel],
549 true, i);
550 curr_channel++;
551 break;
552
553 case CH_MODE_ADC:
554 ad5592r_setup_channel(iio_dev, &channels[curr_channel],
555 false, i);
556 curr_channel++;
557 break;
558
559 case CH_MODE_DAC_AND_ADC:
560 ad5592r_setup_channel(iio_dev, &channels[curr_channel],
561 true, i);
562 curr_channel++;
563 ad5592r_setup_channel(iio_dev, &channels[curr_channel],
564 false, i);
565 curr_channel++;
566 break;
567
568 default:
569 continue;
570 }
571 }
572
573 channels[curr_channel].type = IIO_TEMP;
574 channels[curr_channel].channel = 8;
575 channels[curr_channel].info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
576 BIT(IIO_CHAN_INFO_SCALE) |
577 BIT(IIO_CHAN_INFO_OFFSET);
578 curr_channel++;
579
580 iio_dev->num_channels = curr_channel;
581 iio_dev->channels = channels;
582
583 return 0;
584}
585
586static void ad5592r_init_scales(struct ad5592r_state *st, int vref_mV)
587{
588 s64 tmp = (s64)vref_mV * 1000000000LL >> 12;
589
590 st->scale_avail[0][0] =
591 div_s64_rem(tmp, 1000000000LL, &st->scale_avail[0][1]);
592 st->scale_avail[1][0] =
593 div_s64_rem(tmp * 2, 1000000000LL, &st->scale_avail[1][1]);
594}
595
596int ad5592r_probe(struct device *dev, const char *name,
597 const struct ad5592r_rw_ops *ops)
598{
599 struct iio_dev *iio_dev;
600 struct ad5592r_state *st;
601 int ret;
602
603 iio_dev = devm_iio_device_alloc(dev, sizeof(*st));
604 if (!iio_dev)
605 return -ENOMEM;
606
607 st = iio_priv(iio_dev);
608 st->dev = dev;
609 st->ops = ops;
610 st->num_channels = 8;
611 dev_set_drvdata(dev, iio_dev);
612
613 st->reg = devm_regulator_get_optional(dev, "vref");
614 if (IS_ERR(st->reg)) {
615 if ((PTR_ERR(st->reg) != -ENODEV) && dev->of_node)
616 return PTR_ERR(st->reg);
617
618 st->reg = NULL;
619 } else {
620 ret = regulator_enable(st->reg);
621 if (ret)
622 return ret;
623 }
624
625 iio_dev->dev.parent = dev;
626 iio_dev->name = name;
627 iio_dev->info = &ad5592r_info;
628 iio_dev->modes = INDIO_DIRECT_MODE;
629
630 ad5592r_init_scales(st, ad5592r_get_vref(st));
631
632 ret = ad5592r_reset(st);
633 if (ret)
634 goto error_disable_reg;
635
636 ret = ops->reg_write(st, AD5592R_REG_PD,
637 (st->reg == NULL) ? AD5592R_REG_PD_EN_REF : 0);
638 if (ret)
639 goto error_disable_reg;
640
641 ret = ad5592r_alloc_channels(st);
642 if (ret)
643 goto error_disable_reg;
644
645 ret = ad5592r_set_channel_modes(st);
646 if (ret)
647 goto error_reset_ch_modes;
648
649 ret = iio_device_register(iio_dev);
650 if (ret)
651 goto error_reset_ch_modes;
652
653 ret = ad5592r_gpio_init(st);
654 if (ret)
655 goto error_dev_unregister;
656
657 return 0;
658
659error_dev_unregister:
660 iio_device_unregister(iio_dev);
661
662error_reset_ch_modes:
663 ad5592r_reset_channel_modes(st);
664
665error_disable_reg:
666 if (st->reg)
667 regulator_disable(st->reg);
668
669 return ret;
670}
671EXPORT_SYMBOL_GPL(ad5592r_probe);
672
673int ad5592r_remove(struct device *dev)
674{
675 struct iio_dev *iio_dev = dev_get_drvdata(dev);
676 struct ad5592r_state *st = iio_priv(iio_dev);
677
678 iio_device_unregister(iio_dev);
679 ad5592r_reset_channel_modes(st);
680 ad5592r_gpio_cleanup(st);
681
682 if (st->reg)
683 regulator_disable(st->reg);
684
685 return 0;
686}
687EXPORT_SYMBOL_GPL(ad5592r_remove);
688
689MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
690MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");
691MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/dac/ad5592r-base.h b/drivers/iio/dac/ad5592r-base.h
new file mode 100644
index 000000000000..841457e93f85
--- /dev/null
+++ b/drivers/iio/dac/ad5592r-base.h
@@ -0,0 +1,76 @@
1/*
2 * AD5592R / AD5593R Digital <-> Analog converters driver
3 *
4 * Copyright 2015-2016 Analog Devices Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * Licensed under the GPL-2.
8 */
9
10#ifndef __DRIVERS_IIO_DAC_AD5592R_BASE_H__
11#define __DRIVERS_IIO_DAC_AD5592R_BASE_H__
12
13#include <linux/types.h>
14#include <linux/cache.h>
15#include <linux/mutex.h>
16#include <linux/gpio/driver.h>
17
18struct device;
19struct ad5592r_state;
20
21enum ad5592r_registers {
22 AD5592R_REG_NOOP = 0x0,
23 AD5592R_REG_DAC_READBACK = 0x1,
24 AD5592R_REG_ADC_SEQ = 0x2,
25 AD5592R_REG_CTRL = 0x3,
26 AD5592R_REG_ADC_EN = 0x4,
27 AD5592R_REG_DAC_EN = 0x5,
28 AD5592R_REG_PULLDOWN = 0x6,
29 AD5592R_REG_LDAC = 0x7,
30 AD5592R_REG_GPIO_OUT_EN = 0x8,
31 AD5592R_REG_GPIO_SET = 0x9,
32 AD5592R_REG_GPIO_IN_EN = 0xA,
33 AD5592R_REG_PD = 0xB,
34 AD5592R_REG_OPEN_DRAIN = 0xC,
35 AD5592R_REG_TRISTATE = 0xD,
36 AD5592R_REG_RESET = 0xF,
37};
38
39#define AD5592R_REG_PD_EN_REF BIT(9)
40#define AD5592R_REG_CTRL_ADC_RANGE BIT(5)
41#define AD5592R_REG_CTRL_DAC_RANGE BIT(4)
42
43struct ad5592r_rw_ops {
44 int (*write_dac)(struct ad5592r_state *st, unsigned chan, u16 value);
45 int (*read_adc)(struct ad5592r_state *st, unsigned chan, u16 *value);
46 int (*reg_write)(struct ad5592r_state *st, u8 reg, u16 value);
47 int (*reg_read)(struct ad5592r_state *st, u8 reg, u16 *value);
48 int (*gpio_read)(struct ad5592r_state *st, u8 *value);
49};
50
51struct ad5592r_state {
52 struct device *dev;
53 struct regulator *reg;
54 struct gpio_chip gpiochip;
55 struct mutex gpio_lock; /* Protect cached gpio_out, gpio_val, etc. */
56 unsigned int num_channels;
57 const struct ad5592r_rw_ops *ops;
58 int scale_avail[2][2];
59 u16 cached_dac[8];
60 u16 cached_gp_ctrl;
61 u8 channel_modes[8];
62 u8 channel_offstate[8];
63 u8 gpio_map;
64 u8 gpio_out;
65 u8 gpio_in;
66 u8 gpio_val;
67
68 __be16 spi_msg ____cacheline_aligned;
69 __be16 spi_msg_nop;
70};
71
72int ad5592r_probe(struct device *dev, const char *name,
73 const struct ad5592r_rw_ops *ops);
74int ad5592r_remove(struct device *dev);
75
76#endif /* __DRIVERS_IIO_DAC_AD5592R_BASE_H__ */
diff --git a/drivers/iio/dac/ad5592r.c b/drivers/iio/dac/ad5592r.c
new file mode 100644
index 000000000000..0b235a2c7359
--- /dev/null
+++ b/drivers/iio/dac/ad5592r.c
@@ -0,0 +1,164 @@
1/*
2 * AD5592R Digital <-> Analog converters driver
3 *
4 * Copyright 2015-2016 Analog Devices Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * Licensed under the GPL-2.
8 */
9
10#include "ad5592r-base.h"
11
12#include <linux/bitops.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/spi/spi.h>
16
17#define AD5592R_GPIO_READBACK_EN BIT(10)
18#define AD5592R_LDAC_READBACK_EN BIT(6)
19
20static int ad5592r_spi_wnop_r16(struct ad5592r_state *st, u16 *buf)
21{
22 struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
23 struct spi_transfer t = {
24 .tx_buf = &st->spi_msg_nop,
25 .rx_buf = buf,
26 .len = 2
27 };
28
29 st->spi_msg_nop = 0; /* NOP */
30
31 return spi_sync_transfer(spi, &t, 1);
32}
33
34static int ad5592r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value)
35{
36 struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
37
38 st->spi_msg = cpu_to_be16(BIT(15) | (chan << 12) | value);
39
40 return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
41}
42
43static int ad5592r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value)
44{
45 struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
46 int ret;
47
48 st->spi_msg = cpu_to_be16((AD5592R_REG_ADC_SEQ << 11) | BIT(chan));
49
50 ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
51 if (ret)
52 return ret;
53
54 /*
55 * Invalid data:
56 * See Figure 40. Single-Channel ADC Conversion Sequence
57 */
58 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
59 if (ret)
60 return ret;
61
62 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
63 if (ret)
64 return ret;
65
66 *value = be16_to_cpu(st->spi_msg);
67
68 return 0;
69}
70
71static int ad5592r_reg_write(struct ad5592r_state *st, u8 reg, u16 value)
72{
73 struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
74
75 st->spi_msg = cpu_to_be16((reg << 11) | value);
76
77 return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
78}
79
80static int ad5592r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value)
81{
82 struct spi_device *spi = container_of(st->dev, struct spi_device, dev);
83 int ret;
84
85 st->spi_msg = cpu_to_be16((AD5592R_REG_LDAC << 11) |
86 AD5592R_LDAC_READBACK_EN | (reg << 2));
87
88 ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg));
89 if (ret)
90 return ret;
91
92 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
93 if (ret)
94 return ret;
95
96 *value = be16_to_cpu(st->spi_msg);
97
98 return 0;
99}
100
101static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value)
102{
103 int ret;
104
105 ret = ad5592r_reg_write(st, AD5592R_REG_GPIO_IN_EN,
106 AD5592R_GPIO_READBACK_EN | st->gpio_in);
107 if (ret)
108 return ret;
109
110 ret = ad5592r_spi_wnop_r16(st, &st->spi_msg);
111 if (ret)
112 return ret;
113
114 *value = (u8) be16_to_cpu(st->spi_msg);
115
116 return 0;
117}
118
119static const struct ad5592r_rw_ops ad5592r_rw_ops = {
120 .write_dac = ad5592r_write_dac,
121 .read_adc = ad5592r_read_adc,
122 .reg_write = ad5592r_reg_write,
123 .reg_read = ad5592r_reg_read,
124 .gpio_read = ad5593r_gpio_read,
125};
126
127static int ad5592r_spi_probe(struct spi_device *spi)
128{
129 const struct spi_device_id *id = spi_get_device_id(spi);
130
131 return ad5592r_probe(&spi->dev, id->name, &ad5592r_rw_ops);
132}
133
134static int ad5592r_spi_remove(struct spi_device *spi)
135{
136 return ad5592r_remove(&spi->dev);
137}
138
139static const struct spi_device_id ad5592r_spi_ids[] = {
140 { .name = "ad5592r", },
141 {}
142};
143MODULE_DEVICE_TABLE(spi, ad5592r_spi_ids);
144
145static const struct of_device_id ad5592r_of_match[] = {
146 { .compatible = "adi,ad5592r", },
147 {},
148};
149MODULE_DEVICE_TABLE(of, ad5592r_of_match);
150
151static struct spi_driver ad5592r_spi_driver = {
152 .driver = {
153 .name = "ad5592r",
154 .of_match_table = of_match_ptr(ad5592r_of_match),
155 },
156 .probe = ad5592r_spi_probe,
157 .remove = ad5592r_spi_remove,
158 .id_table = ad5592r_spi_ids,
159};
160module_spi_driver(ad5592r_spi_driver);
161
162MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
163MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");
164MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/dac/ad5593r.c b/drivers/iio/dac/ad5593r.c
new file mode 100644
index 000000000000..dca158a88f47
--- /dev/null
+++ b/drivers/iio/dac/ad5593r.c
@@ -0,0 +1,131 @@
1/*
2 * AD5593R Digital <-> Analog converters driver
3 *
4 * Copyright 2015-2016 Analog Devices Inc.
5 * Author: Paul Cercueil <paul.cercueil@analog.com>
6 *
7 * Licensed under the GPL-2.
8 */
9
10#include "ad5592r-base.h"
11
12#include <linux/bitops.h>
13#include <linux/i2c.h>
14#include <linux/module.h>
15#include <linux/of.h>
16
17#define AD5593R_MODE_CONF (0 << 4)
18#define AD5593R_MODE_DAC_WRITE (1 << 4)
19#define AD5593R_MODE_ADC_READBACK (4 << 4)
20#define AD5593R_MODE_DAC_READBACK (5 << 4)
21#define AD5593R_MODE_GPIO_READBACK (6 << 4)
22#define AD5593R_MODE_REG_READBACK (7 << 4)
23
24static int ad5593r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value)
25{
26 struct i2c_client *i2c = to_i2c_client(st->dev);
27
28 return i2c_smbus_write_word_swapped(i2c,
29 AD5593R_MODE_DAC_WRITE | chan, value);
30}
31
32static int ad5593r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value)
33{
34 struct i2c_client *i2c = to_i2c_client(st->dev);
35 s32 val;
36
37 val = i2c_smbus_write_word_swapped(i2c,
38 AD5593R_MODE_CONF | AD5592R_REG_ADC_SEQ, BIT(chan));
39 if (val < 0)
40 return (int) val;
41
42 val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_ADC_READBACK);
43 if (val < 0)
44 return (int) val;
45
46 *value = (u16) val;
47
48 return 0;
49}
50
51static int ad5593r_reg_write(struct ad5592r_state *st, u8 reg, u16 value)
52{
53 struct i2c_client *i2c = to_i2c_client(st->dev);
54
55 return i2c_smbus_write_word_swapped(i2c,
56 AD5593R_MODE_CONF | reg, value);
57}
58
59static int ad5593r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value)
60{
61 struct i2c_client *i2c = to_i2c_client(st->dev);
62 s32 val;
63
64 val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_REG_READBACK | reg);
65 if (val < 0)
66 return (int) val;
67
68 *value = (u16) val;
69
70 return 0;
71}
72
73static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value)
74{
75 struct i2c_client *i2c = to_i2c_client(st->dev);
76 s32 val;
77
78 val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_GPIO_READBACK);
79 if (val < 0)
80 return (int) val;
81
82 *value = (u8) val;
83
84 return 0;
85}
86
87static const struct ad5592r_rw_ops ad5593r_rw_ops = {
88 .write_dac = ad5593r_write_dac,
89 .read_adc = ad5593r_read_adc,
90 .reg_write = ad5593r_reg_write,
91 .reg_read = ad5593r_reg_read,
92 .gpio_read = ad5593r_gpio_read,
93};
94
95static int ad5593r_i2c_probe(struct i2c_client *i2c,
96 const struct i2c_device_id *id)
97{
98 return ad5592r_probe(&i2c->dev, id->name, &ad5593r_rw_ops);
99}
100
101static int ad5593r_i2c_remove(struct i2c_client *i2c)
102{
103 return ad5592r_remove(&i2c->dev);
104}
105
106static const struct i2c_device_id ad5593r_i2c_ids[] = {
107 { .name = "ad5593r", },
108 {},
109};
110MODULE_DEVICE_TABLE(i2c, ad5593r_i2c_ids);
111
112static const struct of_device_id ad5593r_of_match[] = {
113 { .compatible = "adi,ad5593r", },
114 {},
115};
116MODULE_DEVICE_TABLE(of, ad5593r_of_match);
117
118static struct i2c_driver ad5593r_driver = {
119 .driver = {
120 .name = "ad5593r",
121 .of_match_table = of_match_ptr(ad5593r_of_match),
122 },
123 .probe = ad5593r_i2c_probe,
124 .remove = ad5593r_i2c_remove,
125 .id_table = ad5593r_i2c_ids,
126};
127module_i2c_driver(ad5593r_driver);
128
129MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
130MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");
131MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/dac/lpc18xx_dac.c b/drivers/iio/dac/lpc18xx_dac.c
new file mode 100644
index 000000000000..55d1456a059d
--- /dev/null
+++ b/drivers/iio/dac/lpc18xx_dac.c
@@ -0,0 +1,210 @@
1/*
2 * IIO DAC driver for NXP LPC18xx DAC
3 *
4 * Copyright (C) 2016 Joachim Eastwood <manabian@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * UNSUPPORTED hardware features:
11 * - Interrupts
12 * - DMA
13 */
14
15#include <linux/clk.h>
16#include <linux/err.h>
17#include <linux/iio/iio.h>
18#include <linux/iio/driver.h>
19#include <linux/io.h>
20#include <linux/iopoll.h>
21#include <linux/module.h>
22#include <linux/mutex.h>
23#include <linux/of.h>
24#include <linux/of_device.h>
25#include <linux/platform_device.h>
26#include <linux/regulator/consumer.h>
27
28/* LPC18XX DAC registers and bits */
29#define LPC18XX_DAC_CR 0x000
30#define LPC18XX_DAC_CR_VALUE_SHIFT 6
31#define LPC18XX_DAC_CR_VALUE_MASK 0x3ff
32#define LPC18XX_DAC_CR_BIAS BIT(16)
33#define LPC18XX_DAC_CTRL 0x004
34#define LPC18XX_DAC_CTRL_DMA_ENA BIT(3)
35
36struct lpc18xx_dac {
37 struct regulator *vref;
38 void __iomem *base;
39 struct mutex lock;
40 struct clk *clk;
41};
42
43static const struct iio_chan_spec lpc18xx_dac_iio_channels[] = {
44 {
45 .type = IIO_VOLTAGE,
46 .output = 1,
47 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
48 BIT(IIO_CHAN_INFO_SCALE),
49 },
50};
51
52static int lpc18xx_dac_read_raw(struct iio_dev *indio_dev,
53 struct iio_chan_spec const *chan,
54 int *val, int *val2, long mask)
55{
56 struct lpc18xx_dac *dac = iio_priv(indio_dev);
57 u32 reg;
58
59 switch (mask) {
60 case IIO_CHAN_INFO_RAW:
61 reg = readl(dac->base + LPC18XX_DAC_CR);
62 *val = reg >> LPC18XX_DAC_CR_VALUE_SHIFT;
63 *val &= LPC18XX_DAC_CR_VALUE_MASK;
64
65 return IIO_VAL_INT;
66
67 case IIO_CHAN_INFO_SCALE:
68 *val = regulator_get_voltage(dac->vref) / 1000;
69 *val2 = 10;
70
71 return IIO_VAL_FRACTIONAL_LOG2;
72 }
73
74 return -EINVAL;
75}
76
77static int lpc18xx_dac_write_raw(struct iio_dev *indio_dev,
78 struct iio_chan_spec const *chan,
79 int val, int val2, long mask)
80{
81 struct lpc18xx_dac *dac = iio_priv(indio_dev);
82 u32 reg;
83
84 switch (mask) {
85 case IIO_CHAN_INFO_RAW:
86 if (val < 0 || val > LPC18XX_DAC_CR_VALUE_MASK)
87 return -EINVAL;
88
89 reg = LPC18XX_DAC_CR_BIAS;
90 reg |= val << LPC18XX_DAC_CR_VALUE_SHIFT;
91
92 mutex_lock(&dac->lock);
93 writel(reg, dac->base + LPC18XX_DAC_CR);
94 writel(LPC18XX_DAC_CTRL_DMA_ENA, dac->base + LPC18XX_DAC_CTRL);
95 mutex_unlock(&dac->lock);
96
97 return 0;
98 }
99
100 return -EINVAL;
101}
102
103static const struct iio_info lpc18xx_dac_info = {
104 .read_raw = lpc18xx_dac_read_raw,
105 .write_raw = lpc18xx_dac_write_raw,
106 .driver_module = THIS_MODULE,
107};
108
109static int lpc18xx_dac_probe(struct platform_device *pdev)
110{
111 struct iio_dev *indio_dev;
112 struct lpc18xx_dac *dac;
113 struct resource *res;
114 int ret;
115
116 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*dac));
117 if (!indio_dev)
118 return -ENOMEM;
119
120 platform_set_drvdata(pdev, indio_dev);
121 dac = iio_priv(indio_dev);
122 mutex_init(&dac->lock);
123
124 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
125 dac->base = devm_ioremap_resource(&pdev->dev, res);
126 if (IS_ERR(dac->base))
127 return PTR_ERR(dac->base);
128
129 dac->clk = devm_clk_get(&pdev->dev, NULL);
130 if (IS_ERR(dac->clk)) {
131 dev_err(&pdev->dev, "error getting clock\n");
132 return PTR_ERR(dac->clk);
133 }
134
135 dac->vref = devm_regulator_get(&pdev->dev, "vref");
136 if (IS_ERR(dac->vref)) {
137 dev_err(&pdev->dev, "error getting regulator\n");
138 return PTR_ERR(dac->vref);
139 }
140
141 indio_dev->name = dev_name(&pdev->dev);
142 indio_dev->dev.parent = &pdev->dev;
143 indio_dev->info = &lpc18xx_dac_info;
144 indio_dev->modes = INDIO_DIRECT_MODE;
145 indio_dev->channels = lpc18xx_dac_iio_channels;
146 indio_dev->num_channels = ARRAY_SIZE(lpc18xx_dac_iio_channels);
147
148 ret = regulator_enable(dac->vref);
149 if (ret) {
150 dev_err(&pdev->dev, "unable to enable regulator\n");
151 return ret;
152 }
153
154 ret = clk_prepare_enable(dac->clk);
155 if (ret) {
156 dev_err(&pdev->dev, "unable to enable clock\n");
157 goto dis_reg;
158 }
159
160 writel(0, dac->base + LPC18XX_DAC_CTRL);
161 writel(0, dac->base + LPC18XX_DAC_CR);
162
163 ret = iio_device_register(indio_dev);
164 if (ret) {
165 dev_err(&pdev->dev, "unable to register device\n");
166 goto dis_clk;
167 }
168
169 return 0;
170
171dis_clk:
172 clk_disable_unprepare(dac->clk);
173dis_reg:
174 regulator_disable(dac->vref);
175 return ret;
176}
177
178static int lpc18xx_dac_remove(struct platform_device *pdev)
179{
180 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
181 struct lpc18xx_dac *dac = iio_priv(indio_dev);
182
183 iio_device_unregister(indio_dev);
184
185 writel(0, dac->base + LPC18XX_DAC_CTRL);
186 clk_disable_unprepare(dac->clk);
187 regulator_disable(dac->vref);
188
189 return 0;
190}
191
192static const struct of_device_id lpc18xx_dac_match[] = {
193 { .compatible = "nxp,lpc1850-dac" },
194 { /* sentinel */ }
195};
196MODULE_DEVICE_TABLE(of, lpc18xx_dac_match);
197
198static struct platform_driver lpc18xx_dac_driver = {
199 .probe = lpc18xx_dac_probe,
200 .remove = lpc18xx_dac_remove,
201 .driver = {
202 .name = "lpc18xx-dac",
203 .of_match_table = lpc18xx_dac_match,
204 },
205};
206module_platform_driver(lpc18xx_dac_driver);
207
208MODULE_DESCRIPTION("LPC18xx DAC driver");
209MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
210MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/frequency/ad9523.c b/drivers/iio/frequency/ad9523.c
index 44a30f286de1..99eba524f6dd 100644
--- a/drivers/iio/frequency/ad9523.c
+++ b/drivers/iio/frequency/ad9523.c
@@ -284,7 +284,7 @@ struct ad9523_state {
284 } data[2] ____cacheline_aligned; 284 } data[2] ____cacheline_aligned;
285}; 285};
286 286
287static int ad9523_read(struct iio_dev *indio_dev, unsigned addr) 287static int ad9523_read(struct iio_dev *indio_dev, unsigned int addr)
288{ 288{
289 struct ad9523_state *st = iio_priv(indio_dev); 289 struct ad9523_state *st = iio_priv(indio_dev);
290 int ret; 290 int ret;
@@ -318,7 +318,8 @@ static int ad9523_read(struct iio_dev *indio_dev, unsigned addr)
318 return ret; 318 return ret;
319}; 319};
320 320
321static int ad9523_write(struct iio_dev *indio_dev, unsigned addr, unsigned val) 321static int ad9523_write(struct iio_dev *indio_dev,
322 unsigned int addr, unsigned int val)
322{ 323{
323 struct ad9523_state *st = iio_priv(indio_dev); 324 struct ad9523_state *st = iio_priv(indio_dev);
324 int ret; 325 int ret;
@@ -351,11 +352,11 @@ static int ad9523_io_update(struct iio_dev *indio_dev)
351} 352}
352 353
353static int ad9523_vco_out_map(struct iio_dev *indio_dev, 354static int ad9523_vco_out_map(struct iio_dev *indio_dev,
354 unsigned ch, unsigned out) 355 unsigned int ch, unsigned int out)
355{ 356{
356 struct ad9523_state *st = iio_priv(indio_dev); 357 struct ad9523_state *st = iio_priv(indio_dev);
357 int ret; 358 int ret;
358 unsigned mask; 359 unsigned int mask;
359 360
360 switch (ch) { 361 switch (ch) {
361 case 0 ... 3: 362 case 0 ... 3:
@@ -405,7 +406,7 @@ static int ad9523_vco_out_map(struct iio_dev *indio_dev,
405} 406}
406 407
407static int ad9523_set_clock_provider(struct iio_dev *indio_dev, 408static int ad9523_set_clock_provider(struct iio_dev *indio_dev,
408 unsigned ch, unsigned long freq) 409 unsigned int ch, unsigned long freq)
409{ 410{
410 struct ad9523_state *st = iio_priv(indio_dev); 411 struct ad9523_state *st = iio_priv(indio_dev);
411 long tmp1, tmp2; 412 long tmp1, tmp2;
@@ -619,7 +620,7 @@ static int ad9523_read_raw(struct iio_dev *indio_dev,
619 long m) 620 long m)
620{ 621{
621 struct ad9523_state *st = iio_priv(indio_dev); 622 struct ad9523_state *st = iio_priv(indio_dev);
622 unsigned code; 623 unsigned int code;
623 int ret; 624 int ret;
624 625
625 mutex_lock(&indio_dev->mlock); 626 mutex_lock(&indio_dev->mlock);
@@ -655,7 +656,7 @@ static int ad9523_write_raw(struct iio_dev *indio_dev,
655 long mask) 656 long mask)
656{ 657{
657 struct ad9523_state *st = iio_priv(indio_dev); 658 struct ad9523_state *st = iio_priv(indio_dev);
658 unsigned reg; 659 unsigned int reg;
659 int ret, tmp, code; 660 int ret, tmp, code;
660 661
661 mutex_lock(&indio_dev->mlock); 662 mutex_lock(&indio_dev->mlock);
@@ -709,8 +710,8 @@ out:
709} 710}
710 711
711static int ad9523_reg_access(struct iio_dev *indio_dev, 712static int ad9523_reg_access(struct iio_dev *indio_dev,
712 unsigned reg, unsigned writeval, 713 unsigned int reg, unsigned int writeval,
713 unsigned *readval) 714 unsigned int *readval)
714{ 715{
715 int ret; 716 int ret;
716 717
diff --git a/drivers/iio/gyro/Kconfig b/drivers/iio/gyro/Kconfig
index e816d29d6a62..205a84420ae9 100644
--- a/drivers/iio/gyro/Kconfig
+++ b/drivers/iio/gyro/Kconfig
@@ -93,7 +93,7 @@ config IIO_ST_GYRO_3AXIS
93 select IIO_TRIGGERED_BUFFER if (IIO_BUFFER) 93 select IIO_TRIGGERED_BUFFER if (IIO_BUFFER)
94 help 94 help
95 Say yes here to build support for STMicroelectronics gyroscopes: 95 Say yes here to build support for STMicroelectronics gyroscopes:
96 L3G4200D, LSM330DL, L3GD20, LSM330DLC, L3G4IS, LSM330. 96 L3G4200D, LSM330DL, L3GD20, LSM330DLC, L3G4IS, LSM330, LSM9DS0.
97 97
98 This driver can also be built as a module. If so, these modules 98 This driver can also be built as a module. If so, these modules
99 will be created: 99 will be created:
diff --git a/drivers/iio/gyro/bmg160_core.c b/drivers/iio/gyro/bmg160_core.c
index 4dac567e75b4..7ccc044063f6 100644
--- a/drivers/iio/gyro/bmg160_core.c
+++ b/drivers/iio/gyro/bmg160_core.c
@@ -17,7 +17,6 @@
17#include <linux/delay.h> 17#include <linux/delay.h>
18#include <linux/slab.h> 18#include <linux/slab.h>
19#include <linux/acpi.h> 19#include <linux/acpi.h>
20#include <linux/gpio/consumer.h>
21#include <linux/pm.h> 20#include <linux/pm.h>
22#include <linux/pm_runtime.h> 21#include <linux/pm_runtime.h>
23#include <linux/iio/iio.h> 22#include <linux/iio/iio.h>
@@ -31,7 +30,6 @@
31#include "bmg160.h" 30#include "bmg160.h"
32 31
33#define BMG160_IRQ_NAME "bmg160_event" 32#define BMG160_IRQ_NAME "bmg160_event"
34#define BMG160_GPIO_NAME "gpio_int"
35 33
36#define BMG160_REG_CHIP_ID 0x00 34#define BMG160_REG_CHIP_ID 0x00
37#define BMG160_CHIP_ID_VAL 0x0F 35#define BMG160_CHIP_ID_VAL 0x0F
@@ -97,7 +95,6 @@
97#define BMG160_AUTO_SUSPEND_DELAY_MS 2000 95#define BMG160_AUTO_SUSPEND_DELAY_MS 2000
98 96
99struct bmg160_data { 97struct bmg160_data {
100 struct device *dev;
101 struct regmap *regmap; 98 struct regmap *regmap;
102 struct iio_trigger *dready_trig; 99 struct iio_trigger *dready_trig;
103 struct iio_trigger *motion_trig; 100 struct iio_trigger *motion_trig;
@@ -116,6 +113,7 @@ enum bmg160_axis {
116 AXIS_X, 113 AXIS_X,
117 AXIS_Y, 114 AXIS_Y,
118 AXIS_Z, 115 AXIS_Z,
116 AXIS_MAX,
119}; 117};
120 118
121static const struct { 119static const struct {
@@ -138,11 +136,12 @@ static const struct {
138 136
139static int bmg160_set_mode(struct bmg160_data *data, u8 mode) 137static int bmg160_set_mode(struct bmg160_data *data, u8 mode)
140{ 138{
139 struct device *dev = regmap_get_device(data->regmap);
141 int ret; 140 int ret;
142 141
143 ret = regmap_write(data->regmap, BMG160_REG_PMU_LPW, mode); 142 ret = regmap_write(data->regmap, BMG160_REG_PMU_LPW, mode);
144 if (ret < 0) { 143 if (ret < 0) {
145 dev_err(data->dev, "Error writing reg_pmu_lpw\n"); 144 dev_err(dev, "Error writing reg_pmu_lpw\n");
146 return ret; 145 return ret;
147 } 146 }
148 147
@@ -163,6 +162,7 @@ static int bmg160_convert_freq_to_bit(int val)
163 162
164static int bmg160_set_bw(struct bmg160_data *data, int val) 163static int bmg160_set_bw(struct bmg160_data *data, int val)
165{ 164{
165 struct device *dev = regmap_get_device(data->regmap);
166 int ret; 166 int ret;
167 int bw_bits; 167 int bw_bits;
168 168
@@ -172,7 +172,7 @@ static int bmg160_set_bw(struct bmg160_data *data, int val)
172 172
173 ret = regmap_write(data->regmap, BMG160_REG_PMU_BW, bw_bits); 173 ret = regmap_write(data->regmap, BMG160_REG_PMU_BW, bw_bits);
174 if (ret < 0) { 174 if (ret < 0) {
175 dev_err(data->dev, "Error writing reg_pmu_bw\n"); 175 dev_err(dev, "Error writing reg_pmu_bw\n");
176 return ret; 176 return ret;
177 } 177 }
178 178
@@ -183,18 +183,19 @@ static int bmg160_set_bw(struct bmg160_data *data, int val)
183 183
184static int bmg160_chip_init(struct bmg160_data *data) 184static int bmg160_chip_init(struct bmg160_data *data)
185{ 185{
186 struct device *dev = regmap_get_device(data->regmap);
186 int ret; 187 int ret;
187 unsigned int val; 188 unsigned int val;
188 189
189 ret = regmap_read(data->regmap, BMG160_REG_CHIP_ID, &val); 190 ret = regmap_read(data->regmap, BMG160_REG_CHIP_ID, &val);
190 if (ret < 0) { 191 if (ret < 0) {
191 dev_err(data->dev, "Error reading reg_chip_id\n"); 192 dev_err(dev, "Error reading reg_chip_id\n");
192 return ret; 193 return ret;
193 } 194 }
194 195
195 dev_dbg(data->dev, "Chip Id %x\n", val); 196 dev_dbg(dev, "Chip Id %x\n", val);
196 if (val != BMG160_CHIP_ID_VAL) { 197 if (val != BMG160_CHIP_ID_VAL) {
197 dev_err(data->dev, "invalid chip %x\n", val); 198 dev_err(dev, "invalid chip %x\n", val);
198 return -ENODEV; 199 return -ENODEV;
199 } 200 }
200 201
@@ -213,14 +214,14 @@ static int bmg160_chip_init(struct bmg160_data *data)
213 /* Set Default Range */ 214 /* Set Default Range */
214 ret = regmap_write(data->regmap, BMG160_REG_RANGE, BMG160_RANGE_500DPS); 215 ret = regmap_write(data->regmap, BMG160_REG_RANGE, BMG160_RANGE_500DPS);
215 if (ret < 0) { 216 if (ret < 0) {
216 dev_err(data->dev, "Error writing reg_range\n"); 217 dev_err(dev, "Error writing reg_range\n");
217 return ret; 218 return ret;
218 } 219 }
219 data->dps_range = BMG160_RANGE_500DPS; 220 data->dps_range = BMG160_RANGE_500DPS;
220 221
221 ret = regmap_read(data->regmap, BMG160_REG_SLOPE_THRES, &val); 222 ret = regmap_read(data->regmap, BMG160_REG_SLOPE_THRES, &val);
222 if (ret < 0) { 223 if (ret < 0) {
223 dev_err(data->dev, "Error reading reg_slope_thres\n"); 224 dev_err(dev, "Error reading reg_slope_thres\n");
224 return ret; 225 return ret;
225 } 226 }
226 data->slope_thres = val; 227 data->slope_thres = val;
@@ -229,7 +230,7 @@ static int bmg160_chip_init(struct bmg160_data *data)
229 ret = regmap_update_bits(data->regmap, BMG160_REG_INT_EN_1, 230 ret = regmap_update_bits(data->regmap, BMG160_REG_INT_EN_1,
230 BMG160_INT1_BIT_OD, 0); 231 BMG160_INT1_BIT_OD, 0);
231 if (ret < 0) { 232 if (ret < 0) {
232 dev_err(data->dev, "Error updating bits in reg_int_en_1\n"); 233 dev_err(dev, "Error updating bits in reg_int_en_1\n");
233 return ret; 234 return ret;
234 } 235 }
235 236
@@ -237,7 +238,7 @@ static int bmg160_chip_init(struct bmg160_data *data)
237 BMG160_INT_MODE_LATCH_INT | 238 BMG160_INT_MODE_LATCH_INT |
238 BMG160_INT_MODE_LATCH_RESET); 239 BMG160_INT_MODE_LATCH_RESET);
239 if (ret < 0) { 240 if (ret < 0) {
240 dev_err(data->dev, 241 dev_err(dev,
241 "Error writing reg_motion_intr\n"); 242 "Error writing reg_motion_intr\n");
242 return ret; 243 return ret;
243 } 244 }
@@ -248,20 +249,21 @@ static int bmg160_chip_init(struct bmg160_data *data)
248static int bmg160_set_power_state(struct bmg160_data *data, bool on) 249static int bmg160_set_power_state(struct bmg160_data *data, bool on)
249{ 250{
250#ifdef CONFIG_PM 251#ifdef CONFIG_PM
252 struct device *dev = regmap_get_device(data->regmap);
251 int ret; 253 int ret;
252 254
253 if (on) 255 if (on)
254 ret = pm_runtime_get_sync(data->dev); 256 ret = pm_runtime_get_sync(dev);
255 else { 257 else {
256 pm_runtime_mark_last_busy(data->dev); 258 pm_runtime_mark_last_busy(dev);
257 ret = pm_runtime_put_autosuspend(data->dev); 259 ret = pm_runtime_put_autosuspend(dev);
258 } 260 }
259 261
260 if (ret < 0) { 262 if (ret < 0) {
261 dev_err(data->dev, 263 dev_err(dev, "Failed: bmg160_set_power_state for %d\n", on);
262 "Failed: bmg160_set_power_state for %d\n", on); 264
263 if (on) 265 if (on)
264 pm_runtime_put_noidle(data->dev); 266 pm_runtime_put_noidle(dev);
265 267
266 return ret; 268 return ret;
267 } 269 }
@@ -273,6 +275,7 @@ static int bmg160_set_power_state(struct bmg160_data *data, bool on)
273static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data, 275static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data,
274 bool status) 276 bool status)
275{ 277{
278 struct device *dev = regmap_get_device(data->regmap);
276 int ret; 279 int ret;
277 280
278 /* Enable/Disable INT_MAP0 mapping */ 281 /* Enable/Disable INT_MAP0 mapping */
@@ -280,7 +283,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data,
280 BMG160_INT_MAP_0_BIT_ANY, 283 BMG160_INT_MAP_0_BIT_ANY,
281 (status ? BMG160_INT_MAP_0_BIT_ANY : 0)); 284 (status ? BMG160_INT_MAP_0_BIT_ANY : 0));
282 if (ret < 0) { 285 if (ret < 0) {
283 dev_err(data->dev, "Error updating bits reg_int_map0\n"); 286 dev_err(dev, "Error updating bits reg_int_map0\n");
284 return ret; 287 return ret;
285 } 288 }
286 289
@@ -290,8 +293,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data,
290 ret = regmap_write(data->regmap, BMG160_REG_SLOPE_THRES, 293 ret = regmap_write(data->regmap, BMG160_REG_SLOPE_THRES,
291 data->slope_thres); 294 data->slope_thres);
292 if (ret < 0) { 295 if (ret < 0) {
293 dev_err(data->dev, 296 dev_err(dev, "Error writing reg_slope_thres\n");
294 "Error writing reg_slope_thres\n");
295 return ret; 297 return ret;
296 } 298 }
297 299
@@ -299,8 +301,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data,
299 BMG160_INT_MOTION_X | BMG160_INT_MOTION_Y | 301 BMG160_INT_MOTION_X | BMG160_INT_MOTION_Y |
300 BMG160_INT_MOTION_Z); 302 BMG160_INT_MOTION_Z);
301 if (ret < 0) { 303 if (ret < 0) {
302 dev_err(data->dev, 304 dev_err(dev, "Error writing reg_motion_intr\n");
303 "Error writing reg_motion_intr\n");
304 return ret; 305 return ret;
305 } 306 }
306 307
@@ -315,8 +316,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data,
315 BMG160_INT_MODE_LATCH_INT | 316 BMG160_INT_MODE_LATCH_INT |
316 BMG160_INT_MODE_LATCH_RESET); 317 BMG160_INT_MODE_LATCH_RESET);
317 if (ret < 0) { 318 if (ret < 0) {
318 dev_err(data->dev, 319 dev_err(dev, "Error writing reg_rst_latch\n");
319 "Error writing reg_rst_latch\n");
320 return ret; 320 return ret;
321 } 321 }
322 } 322 }
@@ -329,7 +329,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data,
329 } 329 }
330 330
331 if (ret < 0) { 331 if (ret < 0) {
332 dev_err(data->dev, "Error writing reg_int_en0\n"); 332 dev_err(dev, "Error writing reg_int_en0\n");
333 return ret; 333 return ret;
334 } 334 }
335 335
@@ -339,6 +339,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data,
339static int bmg160_setup_new_data_interrupt(struct bmg160_data *data, 339static int bmg160_setup_new_data_interrupt(struct bmg160_data *data,
340 bool status) 340 bool status)
341{ 341{
342 struct device *dev = regmap_get_device(data->regmap);
342 int ret; 343 int ret;
343 344
344 /* Enable/Disable INT_MAP1 mapping */ 345 /* Enable/Disable INT_MAP1 mapping */
@@ -346,7 +347,7 @@ static int bmg160_setup_new_data_interrupt(struct bmg160_data *data,
346 BMG160_INT_MAP_1_BIT_NEW_DATA, 347 BMG160_INT_MAP_1_BIT_NEW_DATA,
347 (status ? BMG160_INT_MAP_1_BIT_NEW_DATA : 0)); 348 (status ? BMG160_INT_MAP_1_BIT_NEW_DATA : 0));
348 if (ret < 0) { 349 if (ret < 0) {
349 dev_err(data->dev, "Error updating bits in reg_int_map1\n"); 350 dev_err(dev, "Error updating bits in reg_int_map1\n");
350 return ret; 351 return ret;
351 } 352 }
352 353
@@ -355,9 +356,8 @@ static int bmg160_setup_new_data_interrupt(struct bmg160_data *data,
355 BMG160_INT_MODE_NON_LATCH_INT | 356 BMG160_INT_MODE_NON_LATCH_INT |
356 BMG160_INT_MODE_LATCH_RESET); 357 BMG160_INT_MODE_LATCH_RESET);
357 if (ret < 0) { 358 if (ret < 0) {
358 dev_err(data->dev, 359 dev_err(dev, "Error writing reg_rst_latch\n");
359 "Error writing reg_rst_latch\n"); 360 return ret;
360 return ret;
361 } 361 }
362 362
363 ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0, 363 ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0,
@@ -369,16 +369,15 @@ static int bmg160_setup_new_data_interrupt(struct bmg160_data *data,
369 BMG160_INT_MODE_LATCH_INT | 369 BMG160_INT_MODE_LATCH_INT |
370 BMG160_INT_MODE_LATCH_RESET); 370 BMG160_INT_MODE_LATCH_RESET);
371 if (ret < 0) { 371 if (ret < 0) {
372 dev_err(data->dev, 372 dev_err(dev, "Error writing reg_rst_latch\n");
373 "Error writing reg_rst_latch\n"); 373 return ret;
374 return ret;
375 } 374 }
376 375
377 ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0, 0); 376 ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0, 0);
378 } 377 }
379 378
380 if (ret < 0) { 379 if (ret < 0) {
381 dev_err(data->dev, "Error writing reg_int_en0\n"); 380 dev_err(dev, "Error writing reg_int_en0\n");
382 return ret; 381 return ret;
383 } 382 }
384 383
@@ -401,6 +400,7 @@ static int bmg160_get_bw(struct bmg160_data *data, int *val)
401 400
402static int bmg160_set_scale(struct bmg160_data *data, int val) 401static int bmg160_set_scale(struct bmg160_data *data, int val)
403{ 402{
403 struct device *dev = regmap_get_device(data->regmap);
404 int ret, i; 404 int ret, i;
405 405
406 for (i = 0; i < ARRAY_SIZE(bmg160_scale_table); ++i) { 406 for (i = 0; i < ARRAY_SIZE(bmg160_scale_table); ++i) {
@@ -408,8 +408,7 @@ static int bmg160_set_scale(struct bmg160_data *data, int val)
408 ret = regmap_write(data->regmap, BMG160_REG_RANGE, 408 ret = regmap_write(data->regmap, BMG160_REG_RANGE,
409 bmg160_scale_table[i].dps_range); 409 bmg160_scale_table[i].dps_range);
410 if (ret < 0) { 410 if (ret < 0) {
411 dev_err(data->dev, 411 dev_err(dev, "Error writing reg_range\n");
412 "Error writing reg_range\n");
413 return ret; 412 return ret;
414 } 413 }
415 data->dps_range = bmg160_scale_table[i].dps_range; 414 data->dps_range = bmg160_scale_table[i].dps_range;
@@ -422,6 +421,7 @@ static int bmg160_set_scale(struct bmg160_data *data, int val)
422 421
423static int bmg160_get_temp(struct bmg160_data *data, int *val) 422static int bmg160_get_temp(struct bmg160_data *data, int *val)
424{ 423{
424 struct device *dev = regmap_get_device(data->regmap);
425 int ret; 425 int ret;
426 unsigned int raw_val; 426 unsigned int raw_val;
427 427
@@ -434,7 +434,7 @@ static int bmg160_get_temp(struct bmg160_data *data, int *val)
434 434
435 ret = regmap_read(data->regmap, BMG160_REG_TEMP, &raw_val); 435 ret = regmap_read(data->regmap, BMG160_REG_TEMP, &raw_val);
436 if (ret < 0) { 436 if (ret < 0) {
437 dev_err(data->dev, "Error reading reg_temp\n"); 437 dev_err(dev, "Error reading reg_temp\n");
438 bmg160_set_power_state(data, false); 438 bmg160_set_power_state(data, false);
439 mutex_unlock(&data->mutex); 439 mutex_unlock(&data->mutex);
440 return ret; 440 return ret;
@@ -451,6 +451,7 @@ static int bmg160_get_temp(struct bmg160_data *data, int *val)
451 451
452static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val) 452static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val)
453{ 453{
454 struct device *dev = regmap_get_device(data->regmap);
454 int ret; 455 int ret;
455 __le16 raw_val; 456 __le16 raw_val;
456 457
@@ -464,7 +465,7 @@ static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val)
464 ret = regmap_bulk_read(data->regmap, BMG160_AXIS_TO_REG(axis), &raw_val, 465 ret = regmap_bulk_read(data->regmap, BMG160_AXIS_TO_REG(axis), &raw_val,
465 sizeof(raw_val)); 466 sizeof(raw_val));
466 if (ret < 0) { 467 if (ret < 0) {
467 dev_err(data->dev, "Error reading axis %d\n", axis); 468 dev_err(dev, "Error reading axis %d\n", axis);
468 bmg160_set_power_state(data, false); 469 bmg160_set_power_state(data, false);
469 mutex_unlock(&data->mutex); 470 mutex_unlock(&data->mutex);
470 return ret; 471 return ret;
@@ -764,26 +765,23 @@ static const struct iio_info bmg160_info = {
764 .driver_module = THIS_MODULE, 765 .driver_module = THIS_MODULE,
765}; 766};
766 767
768static const unsigned long bmg160_accel_scan_masks[] = {
769 BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
770 0};
771
767static irqreturn_t bmg160_trigger_handler(int irq, void *p) 772static irqreturn_t bmg160_trigger_handler(int irq, void *p)
768{ 773{
769 struct iio_poll_func *pf = p; 774 struct iio_poll_func *pf = p;
770 struct iio_dev *indio_dev = pf->indio_dev; 775 struct iio_dev *indio_dev = pf->indio_dev;
771 struct bmg160_data *data = iio_priv(indio_dev); 776 struct bmg160_data *data = iio_priv(indio_dev);
772 int bit, ret, i = 0; 777 int ret;
773 unsigned int val;
774 778
775 mutex_lock(&data->mutex); 779 mutex_lock(&data->mutex);
776 for_each_set_bit(bit, indio_dev->active_scan_mask, 780 ret = regmap_bulk_read(data->regmap, BMG160_REG_XOUT_L,
777 indio_dev->masklength) { 781 data->buffer, AXIS_MAX * 2);
778 ret = regmap_bulk_read(data->regmap, BMG160_AXIS_TO_REG(bit),
779 &val, 2);
780 if (ret < 0) {
781 mutex_unlock(&data->mutex);
782 goto err;
783 }
784 data->buffer[i++] = val;
785 }
786 mutex_unlock(&data->mutex); 782 mutex_unlock(&data->mutex);
783 if (ret < 0)
784 goto err;
787 785
788 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 786 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
789 pf->timestamp); 787 pf->timestamp);
@@ -797,6 +795,7 @@ static int bmg160_trig_try_reen(struct iio_trigger *trig)
797{ 795{
798 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 796 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
799 struct bmg160_data *data = iio_priv(indio_dev); 797 struct bmg160_data *data = iio_priv(indio_dev);
798 struct device *dev = regmap_get_device(data->regmap);
800 int ret; 799 int ret;
801 800
802 /* new data interrupts don't need ack */ 801 /* new data interrupts don't need ack */
@@ -808,7 +807,7 @@ static int bmg160_trig_try_reen(struct iio_trigger *trig)
808 BMG160_INT_MODE_LATCH_INT | 807 BMG160_INT_MODE_LATCH_INT |
809 BMG160_INT_MODE_LATCH_RESET); 808 BMG160_INT_MODE_LATCH_RESET);
810 if (ret < 0) { 809 if (ret < 0) {
811 dev_err(data->dev, "Error writing reg_rst_latch\n"); 810 dev_err(dev, "Error writing reg_rst_latch\n");
812 return ret; 811 return ret;
813 } 812 }
814 813
@@ -868,13 +867,14 @@ static irqreturn_t bmg160_event_handler(int irq, void *private)
868{ 867{
869 struct iio_dev *indio_dev = private; 868 struct iio_dev *indio_dev = private;
870 struct bmg160_data *data = iio_priv(indio_dev); 869 struct bmg160_data *data = iio_priv(indio_dev);
870 struct device *dev = regmap_get_device(data->regmap);
871 int ret; 871 int ret;
872 int dir; 872 int dir;
873 unsigned int val; 873 unsigned int val;
874 874
875 ret = regmap_read(data->regmap, BMG160_REG_INT_STATUS_2, &val); 875 ret = regmap_read(data->regmap, BMG160_REG_INT_STATUS_2, &val);
876 if (ret < 0) { 876 if (ret < 0) {
877 dev_err(data->dev, "Error reading reg_int_status2\n"); 877 dev_err(dev, "Error reading reg_int_status2\n");
878 goto ack_intr_status; 878 goto ack_intr_status;
879 } 879 }
880 880
@@ -911,8 +911,7 @@ ack_intr_status:
911 BMG160_INT_MODE_LATCH_INT | 911 BMG160_INT_MODE_LATCH_INT |
912 BMG160_INT_MODE_LATCH_RESET); 912 BMG160_INT_MODE_LATCH_RESET);
913 if (ret < 0) 913 if (ret < 0)
914 dev_err(data->dev, 914 dev_err(dev, "Error writing reg_rst_latch\n");
915 "Error writing reg_rst_latch\n");
916 } 915 }
917 916
918 return IRQ_HANDLED; 917 return IRQ_HANDLED;
@@ -956,29 +955,6 @@ static const struct iio_buffer_setup_ops bmg160_buffer_setup_ops = {
956 .postdisable = bmg160_buffer_postdisable, 955 .postdisable = bmg160_buffer_postdisable,
957}; 956};
958 957
959static int bmg160_gpio_probe(struct bmg160_data *data)
960
961{
962 struct device *dev;
963 struct gpio_desc *gpio;
964
965 dev = data->dev;
966
967 /* data ready gpio interrupt pin */
968 gpio = devm_gpiod_get_index(dev, BMG160_GPIO_NAME, 0, GPIOD_IN);
969 if (IS_ERR(gpio)) {
970 dev_err(dev, "acpi gpio get index failed\n");
971 return PTR_ERR(gpio);
972 }
973
974 data->irq = gpiod_to_irq(gpio);
975
976 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio),
977 data->irq);
978
979 return 0;
980}
981
982static const char *bmg160_match_acpi_device(struct device *dev) 958static const char *bmg160_match_acpi_device(struct device *dev)
983{ 959{
984 const struct acpi_device_id *id; 960 const struct acpi_device_id *id;
@@ -1003,7 +979,6 @@ int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
1003 979
1004 data = iio_priv(indio_dev); 980 data = iio_priv(indio_dev);
1005 dev_set_drvdata(dev, indio_dev); 981 dev_set_drvdata(dev, indio_dev);
1006 data->dev = dev;
1007 data->irq = irq; 982 data->irq = irq;
1008 data->regmap = regmap; 983 data->regmap = regmap;
1009 984
@@ -1020,12 +995,10 @@ int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
1020 indio_dev->channels = bmg160_channels; 995 indio_dev->channels = bmg160_channels;
1021 indio_dev->num_channels = ARRAY_SIZE(bmg160_channels); 996 indio_dev->num_channels = ARRAY_SIZE(bmg160_channels);
1022 indio_dev->name = name; 997 indio_dev->name = name;
998 indio_dev->available_scan_masks = bmg160_accel_scan_masks;
1023 indio_dev->modes = INDIO_DIRECT_MODE; 999 indio_dev->modes = INDIO_DIRECT_MODE;
1024 indio_dev->info = &bmg160_info; 1000 indio_dev->info = &bmg160_info;
1025 1001
1026 if (data->irq <= 0)
1027 bmg160_gpio_probe(data);
1028
1029 if (data->irq > 0) { 1002 if (data->irq > 0) {
1030 ret = devm_request_threaded_irq(dev, 1003 ret = devm_request_threaded_irq(dev,
1031 data->irq, 1004 data->irq,
@@ -1168,7 +1141,7 @@ static int bmg160_runtime_suspend(struct device *dev)
1168 1141
1169 ret = bmg160_set_mode(data, BMG160_MODE_SUSPEND); 1142 ret = bmg160_set_mode(data, BMG160_MODE_SUSPEND);
1170 if (ret < 0) { 1143 if (ret < 0) {
1171 dev_err(data->dev, "set mode failed\n"); 1144 dev_err(dev, "set mode failed\n");
1172 return -EAGAIN; 1145 return -EAGAIN;
1173 } 1146 }
1174 1147
diff --git a/drivers/iio/gyro/st_gyro.h b/drivers/iio/gyro/st_gyro.h
index 5353d6328c54..a5c5c4e29add 100644
--- a/drivers/iio/gyro/st_gyro.h
+++ b/drivers/iio/gyro/st_gyro.h
@@ -21,6 +21,7 @@
21#define L3GD20_GYRO_DEV_NAME "l3gd20" 21#define L3GD20_GYRO_DEV_NAME "l3gd20"
22#define L3G4IS_GYRO_DEV_NAME "l3g4is_ui" 22#define L3G4IS_GYRO_DEV_NAME "l3g4is_ui"
23#define LSM330_GYRO_DEV_NAME "lsm330_gyro" 23#define LSM330_GYRO_DEV_NAME "lsm330_gyro"
24#define LSM9DS0_GYRO_DEV_NAME "lsm9ds0_gyro"
24 25
25/** 26/**
26 * struct st_sensors_platform_data - gyro platform data 27 * struct st_sensors_platform_data - gyro platform data
diff --git a/drivers/iio/gyro/st_gyro_core.c b/drivers/iio/gyro/st_gyro_core.c
index 110f95b6e52f..52a3c87c375c 100644
--- a/drivers/iio/gyro/st_gyro_core.c
+++ b/drivers/iio/gyro/st_gyro_core.c
@@ -190,6 +190,7 @@ static const struct st_sensor_settings st_gyro_sensors_settings[] = {
190 * drain settings, but only for INT1 and not 190 * drain settings, but only for INT1 and not
191 * for the DRDY line on INT2. 191 * for the DRDY line on INT2.
192 */ 192 */
193 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
193 }, 194 },
194 .multi_read_bit = ST_GYRO_1_MULTIREAD_BIT, 195 .multi_read_bit = ST_GYRO_1_MULTIREAD_BIT,
195 .bootime = 2, 196 .bootime = 2,
@@ -203,6 +204,7 @@ static const struct st_sensor_settings st_gyro_sensors_settings[] = {
203 [2] = LSM330DLC_GYRO_DEV_NAME, 204 [2] = LSM330DLC_GYRO_DEV_NAME,
204 [3] = L3G4IS_GYRO_DEV_NAME, 205 [3] = L3G4IS_GYRO_DEV_NAME,
205 [4] = LSM330_GYRO_DEV_NAME, 206 [4] = LSM330_GYRO_DEV_NAME,
207 [5] = LSM9DS0_GYRO_DEV_NAME,
206 }, 208 },
207 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels, 209 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
208 .odr = { 210 .odr = {
@@ -258,6 +260,7 @@ static const struct st_sensor_settings st_gyro_sensors_settings[] = {
258 * drain settings, but only for INT1 and not 260 * drain settings, but only for INT1 and not
259 * for the DRDY line on INT2. 261 * for the DRDY line on INT2.
260 */ 262 */
263 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
261 }, 264 },
262 .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT, 265 .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT,
263 .bootime = 2, 266 .bootime = 2,
@@ -322,6 +325,7 @@ static const struct st_sensor_settings st_gyro_sensors_settings[] = {
322 * drain settings, but only for INT1 and not 325 * drain settings, but only for INT1 and not
323 * for the DRDY line on INT2. 326 * for the DRDY line on INT2.
324 */ 327 */
328 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
325 }, 329 },
326 .multi_read_bit = ST_GYRO_3_MULTIREAD_BIT, 330 .multi_read_bit = ST_GYRO_3_MULTIREAD_BIT,
327 .bootime = 2, 331 .bootime = 2,
diff --git a/drivers/iio/gyro/st_gyro_i2c.c b/drivers/iio/gyro/st_gyro_i2c.c
index 6848451f817a..40056b821036 100644
--- a/drivers/iio/gyro/st_gyro_i2c.c
+++ b/drivers/iio/gyro/st_gyro_i2c.c
@@ -48,6 +48,10 @@ static const struct of_device_id st_gyro_of_match[] = {
48 .compatible = "st,lsm330-gyro", 48 .compatible = "st,lsm330-gyro",
49 .data = LSM330_GYRO_DEV_NAME, 49 .data = LSM330_GYRO_DEV_NAME,
50 }, 50 },
51 {
52 .compatible = "st,lsm9ds0-gyro",
53 .data = LSM9DS0_GYRO_DEV_NAME,
54 },
51 {}, 55 {},
52}; 56};
53MODULE_DEVICE_TABLE(of, st_gyro_of_match); 57MODULE_DEVICE_TABLE(of, st_gyro_of_match);
@@ -93,6 +97,7 @@ static const struct i2c_device_id st_gyro_id_table[] = {
93 { L3GD20_GYRO_DEV_NAME }, 97 { L3GD20_GYRO_DEV_NAME },
94 { L3G4IS_GYRO_DEV_NAME }, 98 { L3G4IS_GYRO_DEV_NAME },
95 { LSM330_GYRO_DEV_NAME }, 99 { LSM330_GYRO_DEV_NAME },
100 { LSM9DS0_GYRO_DEV_NAME },
96 {}, 101 {},
97}; 102};
98MODULE_DEVICE_TABLE(i2c, st_gyro_id_table); 103MODULE_DEVICE_TABLE(i2c, st_gyro_id_table);
diff --git a/drivers/iio/gyro/st_gyro_spi.c b/drivers/iio/gyro/st_gyro_spi.c
index d2b7a5fa344c..fbf2faed501c 100644
--- a/drivers/iio/gyro/st_gyro_spi.c
+++ b/drivers/iio/gyro/st_gyro_spi.c
@@ -54,6 +54,7 @@ static const struct spi_device_id st_gyro_id_table[] = {
54 { L3GD20_GYRO_DEV_NAME }, 54 { L3GD20_GYRO_DEV_NAME },
55 { L3G4IS_GYRO_DEV_NAME }, 55 { L3G4IS_GYRO_DEV_NAME },
56 { LSM330_GYRO_DEV_NAME }, 56 { LSM330_GYRO_DEV_NAME },
57 { LSM9DS0_GYRO_DEV_NAME },
57 {}, 58 {},
58}; 59};
59MODULE_DEVICE_TABLE(spi, st_gyro_id_table); 60MODULE_DEVICE_TABLE(spi, st_gyro_id_table);
diff --git a/drivers/iio/humidity/Kconfig b/drivers/iio/humidity/Kconfig
index 866dda133336..738a86d9e4a9 100644
--- a/drivers/iio/humidity/Kconfig
+++ b/drivers/iio/humidity/Kconfig
@@ -3,6 +3,16 @@
3# 3#
4menu "Humidity sensors" 4menu "Humidity sensors"
5 5
6config AM2315
7 tristate "Aosong AM2315 relative humidity and temperature sensor"
8 depends on I2C
9 help
10 If you say yes here you get support for the Aosong AM2315
11 relative humidity and ambient temperature sensor.
12
13 This driver can also be built as a module. If so, the module will
14 be called am2315.
15
6config DHT11 16config DHT11
7 tristate "DHT11 (and compatible sensors) driver" 17 tristate "DHT11 (and compatible sensors) driver"
8 depends on GPIOLIB || COMPILE_TEST 18 depends on GPIOLIB || COMPILE_TEST
diff --git a/drivers/iio/humidity/Makefile b/drivers/iio/humidity/Makefile
index c9f089a9a6b8..4a73442fcd9c 100644
--- a/drivers/iio/humidity/Makefile
+++ b/drivers/iio/humidity/Makefile
@@ -2,6 +2,7 @@
2# Makefile for IIO humidity sensor drivers 2# Makefile for IIO humidity sensor drivers
3# 3#
4 4
5obj-$(CONFIG_AM2315) += am2315.o
5obj-$(CONFIG_DHT11) += dht11.o 6obj-$(CONFIG_DHT11) += dht11.o
6obj-$(CONFIG_HDC100X) += hdc100x.o 7obj-$(CONFIG_HDC100X) += hdc100x.o
7obj-$(CONFIG_HTU21) += htu21.o 8obj-$(CONFIG_HTU21) += htu21.o
diff --git a/drivers/iio/humidity/am2315.c b/drivers/iio/humidity/am2315.c
new file mode 100644
index 000000000000..3be6d209a159
--- /dev/null
+++ b/drivers/iio/humidity/am2315.c
@@ -0,0 +1,303 @@
1/**
2 * Aosong AM2315 relative humidity and temperature
3 *
4 * Copyright (c) 2016, Intel Corporation.
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * 7-bit I2C address: 0x5C.
11 */
12
13#include <linux/acpi.h>
14#include <linux/delay.h>
15#include <linux/i2c.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/iio/buffer.h>
19#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
21#include <linux/iio/trigger_consumer.h>
22#include <linux/iio/triggered_buffer.h>
23
24#define AM2315_REG_HUM_MSB 0x00
25#define AM2315_REG_HUM_LSB 0x01
26#define AM2315_REG_TEMP_MSB 0x02
27#define AM2315_REG_TEMP_LSB 0x03
28
29#define AM2315_FUNCTION_READ 0x03
30#define AM2315_HUM_OFFSET 2
31#define AM2315_TEMP_OFFSET 4
32#define AM2315_ALL_CHANNEL_MASK GENMASK(1, 0)
33
34#define AM2315_DRIVER_NAME "am2315"
35
36struct am2315_data {
37 struct i2c_client *client;
38 struct mutex lock;
39 s16 buffer[8]; /* 2x16-bit channels + 2x16 padding + 4x16 timestamp */
40};
41
42struct am2315_sensor_data {
43 s16 hum_data;
44 s16 temp_data;
45};
46
47static const struct iio_chan_spec am2315_channels[] = {
48 {
49 .type = IIO_HUMIDITYRELATIVE,
50 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
51 BIT(IIO_CHAN_INFO_SCALE),
52 .scan_index = 0,
53 .scan_type = {
54 .sign = 's',
55 .realbits = 16,
56 .storagebits = 16,
57 .endianness = IIO_CPU,
58 },
59 },
60 {
61 .type = IIO_TEMP,
62 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
63 BIT(IIO_CHAN_INFO_SCALE),
64 .scan_index = 1,
65 .scan_type = {
66 .sign = 's',
67 .realbits = 16,
68 .storagebits = 16,
69 .endianness = IIO_CPU,
70 },
71 },
72 IIO_CHAN_SOFT_TIMESTAMP(2),
73};
74
75/* CRC calculation algorithm, as specified in the datasheet (page 13). */
76static u16 am2315_crc(u8 *data, u8 nr_bytes)
77{
78 int i;
79 u16 crc = 0xffff;
80
81 while (nr_bytes--) {
82 crc ^= *data++;
83 for (i = 0; i < 8; i++) {
84 if (crc & 0x01) {
85 crc >>= 1;
86 crc ^= 0xA001;
87 } else {
88 crc >>= 1;
89 }
90 }
91 }
92
93 return crc;
94}
95
96/* Simple function that sends a few bytes to the device to wake it up. */
97static void am2315_ping(struct i2c_client *client)
98{
99 i2c_smbus_read_byte_data(client, AM2315_REG_HUM_MSB);
100}
101
102static int am2315_read_data(struct am2315_data *data,
103 struct am2315_sensor_data *sensor_data)
104{
105 int ret;
106 /* tx_buf format: <function code> <start addr> <nr of regs to read> */
107 u8 tx_buf[3] = { AM2315_FUNCTION_READ, AM2315_REG_HUM_MSB, 4 };
108 /*
109 * rx_buf format:
110 * <function code> <number of registers read>
111 * <humidity MSB> <humidity LSB> <temp MSB> <temp LSB>
112 * <CRC LSB> <CRC MSB>
113 */
114 u8 rx_buf[8];
115 u16 crc;
116
117 /* First wake up the device. */
118 am2315_ping(data->client);
119
120 mutex_lock(&data->lock);
121 ret = i2c_master_send(data->client, tx_buf, sizeof(tx_buf));
122 if (ret < 0) {
123 dev_err(&data->client->dev, "failed to send read request\n");
124 goto exit_unlock;
125 }
126 /* Wait 2-3 ms, then read back the data sent by the device. */
127 usleep_range(2000, 3000);
128 /* Do a bulk data read, then pick out what we need. */
129 ret = i2c_master_recv(data->client, rx_buf, sizeof(rx_buf));
130 if (ret < 0) {
131 dev_err(&data->client->dev, "failed to read sensor data\n");
132 goto exit_unlock;
133 }
134 mutex_unlock(&data->lock);
135 /*
136 * Do a CRC check on the data and compare it to the value
137 * calculated by the device.
138 */
139 crc = am2315_crc(rx_buf, sizeof(rx_buf) - 2);
140 if ((crc & 0xff) != rx_buf[6] || (crc >> 8) != rx_buf[7]) {
141 dev_err(&data->client->dev, "failed to verify sensor data\n");
142 return -EIO;
143 }
144
145 sensor_data->hum_data = (rx_buf[AM2315_HUM_OFFSET] << 8) |
146 rx_buf[AM2315_HUM_OFFSET + 1];
147 sensor_data->temp_data = (rx_buf[AM2315_TEMP_OFFSET] << 8) |
148 rx_buf[AM2315_TEMP_OFFSET + 1];
149
150 return ret;
151
152exit_unlock:
153 mutex_unlock(&data->lock);
154 return ret;
155}
156
157static irqreturn_t am2315_trigger_handler(int irq, void *p)
158{
159 int i;
160 int ret;
161 int bit;
162 struct iio_poll_func *pf = p;
163 struct iio_dev *indio_dev = pf->indio_dev;
164 struct am2315_data *data = iio_priv(indio_dev);
165 struct am2315_sensor_data sensor_data;
166
167 ret = am2315_read_data(data, &sensor_data);
168 if (ret < 0) {
169 mutex_unlock(&data->lock);
170 goto err;
171 }
172
173 mutex_lock(&data->lock);
174 if (*(indio_dev->active_scan_mask) == AM2315_ALL_CHANNEL_MASK) {
175 data->buffer[0] = sensor_data.hum_data;
176 data->buffer[1] = sensor_data.temp_data;
177 } else {
178 i = 0;
179 for_each_set_bit(bit, indio_dev->active_scan_mask,
180 indio_dev->masklength) {
181 data->buffer[i] = (bit ? sensor_data.temp_data :
182 sensor_data.hum_data);
183 i++;
184 }
185 }
186 mutex_unlock(&data->lock);
187
188 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
189 pf->timestamp);
190err:
191 iio_trigger_notify_done(indio_dev->trig);
192 return IRQ_HANDLED;
193}
194
195static int am2315_read_raw(struct iio_dev *indio_dev,
196 struct iio_chan_spec const *chan,
197 int *val, int *val2, long mask)
198{
199 int ret;
200 struct am2315_sensor_data sensor_data;
201 struct am2315_data *data = iio_priv(indio_dev);
202
203 switch (mask) {
204 case IIO_CHAN_INFO_RAW:
205 ret = am2315_read_data(data, &sensor_data);
206 if (ret < 0)
207 return ret;
208 *val = (chan->type == IIO_HUMIDITYRELATIVE) ?
209 sensor_data.hum_data : sensor_data.temp_data;
210 return IIO_VAL_INT;
211 case IIO_CHAN_INFO_SCALE:
212 *val = 100;
213 return IIO_VAL_INT;
214 }
215
216 return -EINVAL;
217}
218
219static const struct iio_info am2315_info = {
220 .driver_module = THIS_MODULE,
221 .read_raw = am2315_read_raw,
222};
223
224static int am2315_probe(struct i2c_client *client,
225 const struct i2c_device_id *id)
226{
227 int ret;
228 struct iio_dev *indio_dev;
229 struct am2315_data *data;
230
231 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
232 if (!indio_dev) {
233 dev_err(&client->dev, "iio allocation failed!\n");
234 return -ENOMEM;
235 }
236
237 data = iio_priv(indio_dev);
238 data->client = client;
239 i2c_set_clientdata(client, indio_dev);
240 mutex_init(&data->lock);
241
242 indio_dev->dev.parent = &client->dev;
243 indio_dev->info = &am2315_info;
244 indio_dev->name = AM2315_DRIVER_NAME;
245 indio_dev->modes = INDIO_DIRECT_MODE;
246 indio_dev->channels = am2315_channels;
247 indio_dev->num_channels = ARRAY_SIZE(am2315_channels);
248
249 ret = iio_triggered_buffer_setup(indio_dev, NULL,
250 am2315_trigger_handler, NULL);
251 if (ret < 0) {
252 dev_err(&client->dev, "iio triggered buffer setup failed\n");
253 return ret;
254 }
255
256 ret = iio_device_register(indio_dev);
257 if (ret < 0)
258 goto err_buffer_cleanup;
259
260 return 0;
261
262err_buffer_cleanup:
263 iio_triggered_buffer_cleanup(indio_dev);
264 return ret;
265}
266
267static int am2315_remove(struct i2c_client *client)
268{
269 struct iio_dev *indio_dev = i2c_get_clientdata(client);
270
271 iio_device_unregister(indio_dev);
272 iio_triggered_buffer_cleanup(indio_dev);
273
274 return 0;
275}
276
277static const struct i2c_device_id am2315_i2c_id[] = {
278 {"am2315", 0},
279 {}
280};
281
282static const struct acpi_device_id am2315_acpi_id[] = {
283 {"AOS2315", 0},
284 {}
285};
286
287MODULE_DEVICE_TABLE(acpi, am2315_acpi_id);
288
289static struct i2c_driver am2315_driver = {
290 .driver = {
291 .name = "am2315",
292 .acpi_match_table = ACPI_PTR(am2315_acpi_id),
293 },
294 .probe = am2315_probe,
295 .remove = am2315_remove,
296 .id_table = am2315_i2c_id,
297};
298
299module_i2c_driver(am2315_driver);
300
301MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
302MODULE_DESCRIPTION("Aosong AM2315 relative humidity and temperature");
303MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/humidity/dht11.c b/drivers/iio/humidity/dht11.c
index 20b500da94db..9c47bc98f3ac 100644
--- a/drivers/iio/humidity/dht11.c
+++ b/drivers/iio/humidity/dht11.c
@@ -96,6 +96,24 @@ struct dht11 {
96 struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ]; 96 struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ];
97}; 97};
98 98
99#ifdef CONFIG_DYNAMIC_DEBUG
100/*
101 * dht11_edges_print: show the data as actually received by the
102 * driver.
103 */
104static void dht11_edges_print(struct dht11 *dht11)
105{
106 int i;
107
108 dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
109 for (i = 1; i < dht11->num_edges; ++i) {
110 dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
111 dht11->edges[i].ts - dht11->edges[i - 1].ts,
112 dht11->edges[i - 1].value ? "high" : "low");
113 }
114}
115#endif /* CONFIG_DYNAMIC_DEBUG */
116
99static unsigned char dht11_decode_byte(char *bits) 117static unsigned char dht11_decode_byte(char *bits)
100{ 118{
101 unsigned char ret = 0; 119 unsigned char ret = 0;
@@ -119,8 +137,12 @@ static int dht11_decode(struct dht11 *dht11, int offset)
119 for (i = 0; i < DHT11_BITS_PER_READ; ++i) { 137 for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
120 t = dht11->edges[offset + 2 * i + 2].ts - 138 t = dht11->edges[offset + 2 * i + 2].ts -
121 dht11->edges[offset + 2 * i + 1].ts; 139 dht11->edges[offset + 2 * i + 1].ts;
122 if (!dht11->edges[offset + 2 * i + 1].value) 140 if (!dht11->edges[offset + 2 * i + 1].value) {
123 return -EIO; /* lost synchronisation */ 141 dev_dbg(dht11->dev,
142 "lost synchronisation at edge %d\n",
143 offset + 2 * i + 1);
144 return -EIO;
145 }
124 bits[i] = t > DHT11_THRESHOLD; 146 bits[i] = t > DHT11_THRESHOLD;
125 } 147 }
126 148
@@ -130,8 +152,10 @@ static int dht11_decode(struct dht11 *dht11, int offset)
130 temp_dec = dht11_decode_byte(&bits[24]); 152 temp_dec = dht11_decode_byte(&bits[24]);
131 checksum = dht11_decode_byte(&bits[32]); 153 checksum = dht11_decode_byte(&bits[32]);
132 154
133 if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) 155 if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
156 dev_dbg(dht11->dev, "invalid checksum\n");
134 return -EIO; 157 return -EIO;
158 }
135 159
136 dht11->timestamp = ktime_get_boot_ns(); 160 dht11->timestamp = ktime_get_boot_ns();
137 if (hum_int < 20) { /* DHT22 */ 161 if (hum_int < 20) { /* DHT22 */
@@ -182,6 +206,7 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
182 mutex_lock(&dht11->lock); 206 mutex_lock(&dht11->lock);
183 if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) { 207 if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
184 timeres = ktime_get_resolution_ns(); 208 timeres = ktime_get_resolution_ns();
209 dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
185 if (timeres > DHT11_MIN_TIMERES) { 210 if (timeres > DHT11_MIN_TIMERES) {
186 dev_err(dht11->dev, "timeresolution %dns too low\n", 211 dev_err(dht11->dev, "timeresolution %dns too low\n",
187 timeres); 212 timeres);
@@ -219,10 +244,13 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
219 244
220 free_irq(dht11->irq, iio_dev); 245 free_irq(dht11->irq, iio_dev);
221 246
247#ifdef CONFIG_DYNAMIC_DEBUG
248 dht11_edges_print(dht11);
249#endif
250
222 if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) { 251 if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
223 dev_err(&iio_dev->dev, 252 dev_err(dht11->dev, "Only %d signal edges detected\n",
224 "Only %d signal edges detected\n", 253 dht11->num_edges);
225 dht11->num_edges);
226 ret = -ETIMEDOUT; 254 ret = -ETIMEDOUT;
227 } 255 }
228 if (ret < 0) 256 if (ret < 0)
diff --git a/drivers/iio/imu/Kconfig b/drivers/iio/imu/Kconfig
index 5e610f7de5aa..1f1ad41ef881 100644
--- a/drivers/iio/imu/Kconfig
+++ b/drivers/iio/imu/Kconfig
@@ -25,6 +25,8 @@ config ADIS16480
25 Say yes here to build support for Analog Devices ADIS16375, ADIS16480, 25 Say yes here to build support for Analog Devices ADIS16375, ADIS16480,
26 ADIS16485, ADIS16488 inertial sensors. 26 ADIS16485, ADIS16488 inertial sensors.
27 27
28source "drivers/iio/imu/bmi160/Kconfig"
29
28config KMX61 30config KMX61
29 tristate "Kionix KMX61 6-axis accelerometer and magnetometer" 31 tristate "Kionix KMX61 6-axis accelerometer and magnetometer"
30 depends on I2C 32 depends on I2C
diff --git a/drivers/iio/imu/Makefile b/drivers/iio/imu/Makefile
index e1e6e3d70e26..c71bcd30dc38 100644
--- a/drivers/iio/imu/Makefile
+++ b/drivers/iio/imu/Makefile
@@ -13,6 +13,7 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_trigger.o
13adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o 13adis_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 += bmi160/
16obj-y += inv_mpu6050/ 17obj-y += inv_mpu6050/
17 18
18obj-$(CONFIG_KMX61) += kmx61.o 19obj-$(CONFIG_KMX61) += kmx61.o
diff --git a/drivers/iio/imu/adis.c b/drivers/iio/imu/adis.c
index 911255d41c1a..ad6f91d06185 100644
--- a/drivers/iio/imu/adis.c
+++ b/drivers/iio/imu/adis.c
@@ -324,7 +324,12 @@ static int adis_self_test(struct adis *adis)
324 324
325 msleep(adis->data->startup_delay); 325 msleep(adis->data->startup_delay);
326 326
327 return adis_check_status(adis); 327 ret = adis_check_status(adis);
328
329 if (adis->data->self_test_no_autoclear)
330 adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00);
331
332 return ret;
328} 333}
329 334
330/** 335/**
diff --git a/drivers/iio/imu/bmi160/Kconfig b/drivers/iio/imu/bmi160/Kconfig
new file mode 100644
index 000000000000..005c17ccc2b0
--- /dev/null
+++ b/drivers/iio/imu/bmi160/Kconfig
@@ -0,0 +1,32 @@
1#
2# BMI160 IMU driver
3#
4
5config BMI160
6 tristate
7 select IIO_BUFFER
8 select IIO_TRIGGERED_BUFFER
9
10config BMI160_I2C
11 tristate "Bosch BMI160 I2C driver"
12 depends on I2C
13 select BMI160
14 select REGMAP_I2C
15 help
16 If you say yes here you get support for BMI160 IMU on I2C with
17 accelerometer, gyroscope and external BMG160 magnetometer.
18
19 This driver can also be built as a module. If so, the module will be
20 called bmi160_i2c.
21
22config BMI160_SPI
23 tristate "Bosch BMI160 SPI driver"
24 depends on SPI
25 select BMI160
26 select REGMAP_SPI
27 help
28 If you say yes here you get support for BMI160 IMU on SPI with
29 accelerometer, gyroscope and external BMG160 magnetometer.
30
31 This driver can also be built as a module. If so, the module will be
32 called bmi160_spi.
diff --git a/drivers/iio/imu/bmi160/Makefile b/drivers/iio/imu/bmi160/Makefile
new file mode 100644
index 000000000000..10365e493ae2
--- /dev/null
+++ b/drivers/iio/imu/bmi160/Makefile
@@ -0,0 +1,6 @@
1#
2# Makefile for Bosch BMI160 IMU
3#
4obj-$(CONFIG_BMI160) += bmi160_core.o
5obj-$(CONFIG_BMI160_I2C) += bmi160_i2c.o
6obj-$(CONFIG_BMI160_SPI) += bmi160_spi.o
diff --git a/drivers/iio/imu/bmi160/bmi160.h b/drivers/iio/imu/bmi160/bmi160.h
new file mode 100644
index 000000000000..d2ae6ed70271
--- /dev/null
+++ b/drivers/iio/imu/bmi160/bmi160.h
@@ -0,0 +1,10 @@
1#ifndef BMI160_H_
2#define BMI160_H_
3
4extern const struct regmap_config bmi160_regmap_config;
5
6int bmi160_core_probe(struct device *dev, struct regmap *regmap,
7 const char *name, bool use_spi);
8void bmi160_core_remove(struct device *dev);
9
10#endif /* BMI160_H_ */
diff --git a/drivers/iio/imu/bmi160/bmi160_core.c b/drivers/iio/imu/bmi160/bmi160_core.c
new file mode 100644
index 000000000000..0bf92b06d7d8
--- /dev/null
+++ b/drivers/iio/imu/bmi160/bmi160_core.c
@@ -0,0 +1,596 @@
1/*
2 * BMI160 - Bosch IMU (accel, gyro plus external magnetometer)
3 *
4 * Copyright (c) 2016, Intel Corporation.
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * IIO core driver for BMI160, with support for I2C/SPI busses
11 *
12 * TODO: magnetometer, interrupts, hardware FIFO
13 */
14#include <linux/module.h>
15#include <linux/regmap.h>
16#include <linux/acpi.h>
17#include <linux/delay.h>
18
19#include <linux/iio/iio.h>
20#include <linux/iio/triggered_buffer.h>
21#include <linux/iio/trigger_consumer.h>
22#include <linux/iio/buffer.h>
23
24#include "bmi160.h"
25
26#define BMI160_REG_CHIP_ID 0x00
27#define BMI160_CHIP_ID_VAL 0xD1
28
29#define BMI160_REG_PMU_STATUS 0x03
30
31/* X axis data low byte address, the rest can be obtained using axis offset */
32#define BMI160_REG_DATA_MAGN_XOUT_L 0x04
33#define BMI160_REG_DATA_GYRO_XOUT_L 0x0C
34#define BMI160_REG_DATA_ACCEL_XOUT_L 0x12
35
36#define BMI160_REG_ACCEL_CONFIG 0x40
37#define BMI160_ACCEL_CONFIG_ODR_MASK GENMASK(3, 0)
38#define BMI160_ACCEL_CONFIG_BWP_MASK GENMASK(6, 4)
39
40#define BMI160_REG_ACCEL_RANGE 0x41
41#define BMI160_ACCEL_RANGE_2G 0x03
42#define BMI160_ACCEL_RANGE_4G 0x05
43#define BMI160_ACCEL_RANGE_8G 0x08
44#define BMI160_ACCEL_RANGE_16G 0x0C
45
46#define BMI160_REG_GYRO_CONFIG 0x42
47#define BMI160_GYRO_CONFIG_ODR_MASK GENMASK(3, 0)
48#define BMI160_GYRO_CONFIG_BWP_MASK GENMASK(5, 4)
49
50#define BMI160_REG_GYRO_RANGE 0x43
51#define BMI160_GYRO_RANGE_2000DPS 0x00
52#define BMI160_GYRO_RANGE_1000DPS 0x01
53#define BMI160_GYRO_RANGE_500DPS 0x02
54#define BMI160_GYRO_RANGE_250DPS 0x03
55#define BMI160_GYRO_RANGE_125DPS 0x04
56
57#define BMI160_REG_CMD 0x7E
58#define BMI160_CMD_ACCEL_PM_SUSPEND 0x10
59#define BMI160_CMD_ACCEL_PM_NORMAL 0x11
60#define BMI160_CMD_ACCEL_PM_LOW_POWER 0x12
61#define BMI160_CMD_GYRO_PM_SUSPEND 0x14
62#define BMI160_CMD_GYRO_PM_NORMAL 0x15
63#define BMI160_CMD_GYRO_PM_FAST_STARTUP 0x17
64#define BMI160_CMD_SOFTRESET 0xB6
65
66#define BMI160_REG_DUMMY 0x7F
67
68#define BMI160_ACCEL_PMU_MIN_USLEEP 3200
69#define BMI160_ACCEL_PMU_MAX_USLEEP 3800
70#define BMI160_GYRO_PMU_MIN_USLEEP 55000
71#define BMI160_GYRO_PMU_MAX_USLEEP 80000
72#define BMI160_SOFTRESET_USLEEP 1000
73
74#define BMI160_CHANNEL(_type, _axis, _index) { \
75 .type = _type, \
76 .modified = 1, \
77 .channel2 = IIO_MOD_##_axis, \
78 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
79 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
80 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
81 .scan_index = _index, \
82 .scan_type = { \
83 .sign = 's', \
84 .realbits = 16, \
85 .storagebits = 16, \
86 .endianness = IIO_LE, \
87 }, \
88}
89
90/* scan indexes follow DATA register order */
91enum bmi160_scan_axis {
92 BMI160_SCAN_EXT_MAGN_X = 0,
93 BMI160_SCAN_EXT_MAGN_Y,
94 BMI160_SCAN_EXT_MAGN_Z,
95 BMI160_SCAN_RHALL,
96 BMI160_SCAN_GYRO_X,
97 BMI160_SCAN_GYRO_Y,
98 BMI160_SCAN_GYRO_Z,
99 BMI160_SCAN_ACCEL_X,
100 BMI160_SCAN_ACCEL_Y,
101 BMI160_SCAN_ACCEL_Z,
102 BMI160_SCAN_TIMESTAMP,
103};
104
105enum bmi160_sensor_type {
106 BMI160_ACCEL = 0,
107 BMI160_GYRO,
108 BMI160_EXT_MAGN,
109 BMI160_NUM_SENSORS /* must be last */
110};
111
112struct bmi160_data {
113 struct regmap *regmap;
114};
115
116const struct regmap_config bmi160_regmap_config = {
117 .reg_bits = 8,
118 .val_bits = 8,
119};
120EXPORT_SYMBOL(bmi160_regmap_config);
121
122struct bmi160_regs {
123 u8 data; /* LSB byte register for X-axis */
124 u8 config;
125 u8 config_odr_mask;
126 u8 config_bwp_mask;
127 u8 range;
128 u8 pmu_cmd_normal;
129 u8 pmu_cmd_suspend;
130};
131
132static struct bmi160_regs bmi160_regs[] = {
133 [BMI160_ACCEL] = {
134 .data = BMI160_REG_DATA_ACCEL_XOUT_L,
135 .config = BMI160_REG_ACCEL_CONFIG,
136 .config_odr_mask = BMI160_ACCEL_CONFIG_ODR_MASK,
137 .config_bwp_mask = BMI160_ACCEL_CONFIG_BWP_MASK,
138 .range = BMI160_REG_ACCEL_RANGE,
139 .pmu_cmd_normal = BMI160_CMD_ACCEL_PM_NORMAL,
140 .pmu_cmd_suspend = BMI160_CMD_ACCEL_PM_SUSPEND,
141 },
142 [BMI160_GYRO] = {
143 .data = BMI160_REG_DATA_GYRO_XOUT_L,
144 .config = BMI160_REG_GYRO_CONFIG,
145 .config_odr_mask = BMI160_GYRO_CONFIG_ODR_MASK,
146 .config_bwp_mask = BMI160_GYRO_CONFIG_BWP_MASK,
147 .range = BMI160_REG_GYRO_RANGE,
148 .pmu_cmd_normal = BMI160_CMD_GYRO_PM_NORMAL,
149 .pmu_cmd_suspend = BMI160_CMD_GYRO_PM_SUSPEND,
150 },
151};
152
153struct bmi160_pmu_time {
154 unsigned long min;
155 unsigned long max;
156};
157
158static struct bmi160_pmu_time bmi160_pmu_time[] = {
159 [BMI160_ACCEL] = {
160 .min = BMI160_ACCEL_PMU_MIN_USLEEP,
161 .max = BMI160_ACCEL_PMU_MAX_USLEEP
162 },
163 [BMI160_GYRO] = {
164 .min = BMI160_GYRO_PMU_MIN_USLEEP,
165 .max = BMI160_GYRO_PMU_MIN_USLEEP,
166 },
167};
168
169struct bmi160_scale {
170 u8 bits;
171 int uscale;
172};
173
174struct bmi160_odr {
175 u8 bits;
176 int odr;
177 int uodr;
178};
179
180static const struct bmi160_scale bmi160_accel_scale[] = {
181 { BMI160_ACCEL_RANGE_2G, 598},
182 { BMI160_ACCEL_RANGE_4G, 1197},
183 { BMI160_ACCEL_RANGE_8G, 2394},
184 { BMI160_ACCEL_RANGE_16G, 4788},
185};
186
187static const struct bmi160_scale bmi160_gyro_scale[] = {
188 { BMI160_GYRO_RANGE_2000DPS, 1065},
189 { BMI160_GYRO_RANGE_1000DPS, 532},
190 { BMI160_GYRO_RANGE_500DPS, 266},
191 { BMI160_GYRO_RANGE_250DPS, 133},
192 { BMI160_GYRO_RANGE_125DPS, 66},
193};
194
195struct bmi160_scale_item {
196 const struct bmi160_scale *tbl;
197 int num;
198};
199
200static const struct bmi160_scale_item bmi160_scale_table[] = {
201 [BMI160_ACCEL] = {
202 .tbl = bmi160_accel_scale,
203 .num = ARRAY_SIZE(bmi160_accel_scale),
204 },
205 [BMI160_GYRO] = {
206 .tbl = bmi160_gyro_scale,
207 .num = ARRAY_SIZE(bmi160_gyro_scale),
208 },
209};
210
211static const struct bmi160_odr bmi160_accel_odr[] = {
212 {0x01, 0, 78125},
213 {0x02, 1, 5625},
214 {0x03, 3, 125},
215 {0x04, 6, 25},
216 {0x05, 12, 5},
217 {0x06, 25, 0},
218 {0x07, 50, 0},
219 {0x08, 100, 0},
220 {0x09, 200, 0},
221 {0x0A, 400, 0},
222 {0x0B, 800, 0},
223 {0x0C, 1600, 0},
224};
225
226static const struct bmi160_odr bmi160_gyro_odr[] = {
227 {0x06, 25, 0},
228 {0x07, 50, 0},
229 {0x08, 100, 0},
230 {0x09, 200, 0},
231 {0x0A, 400, 0},
232 {0x0B, 8000, 0},
233 {0x0C, 1600, 0},
234 {0x0D, 3200, 0},
235};
236
237struct bmi160_odr_item {
238 const struct bmi160_odr *tbl;
239 int num;
240};
241
242static const struct bmi160_odr_item bmi160_odr_table[] = {
243 [BMI160_ACCEL] = {
244 .tbl = bmi160_accel_odr,
245 .num = ARRAY_SIZE(bmi160_accel_odr),
246 },
247 [BMI160_GYRO] = {
248 .tbl = bmi160_gyro_odr,
249 .num = ARRAY_SIZE(bmi160_gyro_odr),
250 },
251};
252
253static const struct iio_chan_spec bmi160_channels[] = {
254 BMI160_CHANNEL(IIO_ACCEL, X, BMI160_SCAN_ACCEL_X),
255 BMI160_CHANNEL(IIO_ACCEL, Y, BMI160_SCAN_ACCEL_Y),
256 BMI160_CHANNEL(IIO_ACCEL, Z, BMI160_SCAN_ACCEL_Z),
257 BMI160_CHANNEL(IIO_ANGL_VEL, X, BMI160_SCAN_GYRO_X),
258 BMI160_CHANNEL(IIO_ANGL_VEL, Y, BMI160_SCAN_GYRO_Y),
259 BMI160_CHANNEL(IIO_ANGL_VEL, Z, BMI160_SCAN_GYRO_Z),
260 IIO_CHAN_SOFT_TIMESTAMP(BMI160_SCAN_TIMESTAMP),
261};
262
263static enum bmi160_sensor_type bmi160_to_sensor(enum iio_chan_type iio_type)
264{
265 switch (iio_type) {
266 case IIO_ACCEL:
267 return BMI160_ACCEL;
268 case IIO_ANGL_VEL:
269 return BMI160_GYRO;
270 default:
271 return -EINVAL;
272 }
273}
274
275static
276int bmi160_set_mode(struct bmi160_data *data, enum bmi160_sensor_type t,
277 bool mode)
278{
279 int ret;
280 u8 cmd;
281
282 if (mode)
283 cmd = bmi160_regs[t].pmu_cmd_normal;
284 else
285 cmd = bmi160_regs[t].pmu_cmd_suspend;
286
287 ret = regmap_write(data->regmap, BMI160_REG_CMD, cmd);
288 if (ret < 0)
289 return ret;
290
291 usleep_range(bmi160_pmu_time[t].min, bmi160_pmu_time[t].max);
292
293 return 0;
294}
295
296static
297int bmi160_set_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
298 int uscale)
299{
300 int i;
301
302 for (i = 0; i < bmi160_scale_table[t].num; i++)
303 if (bmi160_scale_table[t].tbl[i].uscale == uscale)
304 break;
305
306 if (i == bmi160_scale_table[t].num)
307 return -EINVAL;
308
309 return regmap_write(data->regmap, bmi160_regs[t].range,
310 bmi160_scale_table[t].tbl[i].bits);
311}
312
313static
314int bmi160_get_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
315 int *uscale)
316{
317 int i, ret, val;
318
319 ret = regmap_read(data->regmap, bmi160_regs[t].range, &val);
320 if (ret < 0)
321 return ret;
322
323 for (i = 0; i < bmi160_scale_table[t].num; i++)
324 if (bmi160_scale_table[t].tbl[i].bits == val) {
325 *uscale = bmi160_scale_table[t].tbl[i].uscale;
326 return 0;
327 }
328
329 return -EINVAL;
330}
331
332static int bmi160_get_data(struct bmi160_data *data, int chan_type,
333 int axis, int *val)
334{
335 u8 reg;
336 int ret;
337 __le16 sample;
338 enum bmi160_sensor_type t = bmi160_to_sensor(chan_type);
339
340 reg = bmi160_regs[t].data + (axis - IIO_MOD_X) * sizeof(__le16);
341
342 ret = regmap_bulk_read(data->regmap, reg, &sample, sizeof(__le16));
343 if (ret < 0)
344 return ret;
345
346 *val = sign_extend32(le16_to_cpu(sample), 15);
347
348 return 0;
349}
350
351static
352int bmi160_set_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
353 int odr, int uodr)
354{
355 int i;
356
357 for (i = 0; i < bmi160_odr_table[t].num; i++)
358 if (bmi160_odr_table[t].tbl[i].odr == odr &&
359 bmi160_odr_table[t].tbl[i].uodr == uodr)
360 break;
361
362 if (i >= bmi160_odr_table[t].num)
363 return -EINVAL;
364
365 return regmap_update_bits(data->regmap,
366 bmi160_regs[t].config,
367 bmi160_odr_table[t].tbl[i].bits,
368 bmi160_regs[t].config_odr_mask);
369}
370
371static int bmi160_get_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
372 int *odr, int *uodr)
373{
374 int i, val, ret;
375
376 ret = regmap_read(data->regmap, bmi160_regs[t].config, &val);
377 if (ret < 0)
378 return ret;
379
380 val &= bmi160_regs[t].config_odr_mask;
381
382 for (i = 0; i < bmi160_odr_table[t].num; i++)
383 if (val == bmi160_odr_table[t].tbl[i].bits)
384 break;
385
386 if (i >= bmi160_odr_table[t].num)
387 return -EINVAL;
388
389 *odr = bmi160_odr_table[t].tbl[i].odr;
390 *uodr = bmi160_odr_table[t].tbl[i].uodr;
391
392 return 0;
393}
394
395static irqreturn_t bmi160_trigger_handler(int irq, void *p)
396{
397 struct iio_poll_func *pf = p;
398 struct iio_dev *indio_dev = pf->indio_dev;
399 struct bmi160_data *data = iio_priv(indio_dev);
400 s16 buf[16]; /* 3 sens x 3 axis x s16 + 3 x s16 pad + 4 x s16 tstamp */
401 int i, ret, j = 0, base = BMI160_REG_DATA_MAGN_XOUT_L;
402 __le16 sample;
403
404 for_each_set_bit(i, indio_dev->active_scan_mask,
405 indio_dev->masklength) {
406 ret = regmap_bulk_read(data->regmap, base + i * sizeof(__le16),
407 &sample, sizeof(__le16));
408 if (ret < 0)
409 goto done;
410 buf[j++] = sample;
411 }
412
413 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns());
414done:
415 iio_trigger_notify_done(indio_dev->trig);
416 return IRQ_HANDLED;
417}
418
419static int bmi160_read_raw(struct iio_dev *indio_dev,
420 struct iio_chan_spec const *chan,
421 int *val, int *val2, long mask)
422{
423 int ret;
424 struct bmi160_data *data = iio_priv(indio_dev);
425
426 switch (mask) {
427 case IIO_CHAN_INFO_RAW:
428 ret = bmi160_get_data(data, chan->type, chan->channel2, val);
429 if (ret < 0)
430 return ret;
431 return IIO_VAL_INT;
432 case IIO_CHAN_INFO_SCALE:
433 *val = 0;
434 ret = bmi160_get_scale(data,
435 bmi160_to_sensor(chan->type), val2);
436 return ret < 0 ? ret : IIO_VAL_INT_PLUS_MICRO;
437 case IIO_CHAN_INFO_SAMP_FREQ:
438 ret = bmi160_get_odr(data, bmi160_to_sensor(chan->type),
439 val, val2);
440 return ret < 0 ? ret : IIO_VAL_INT_PLUS_MICRO;
441 default:
442 return -EINVAL;
443 }
444
445 return 0;
446}
447
448static int bmi160_write_raw(struct iio_dev *indio_dev,
449 struct iio_chan_spec const *chan,
450 int val, int val2, long mask)
451{
452 struct bmi160_data *data = iio_priv(indio_dev);
453
454 switch (mask) {
455 case IIO_CHAN_INFO_SCALE:
456 return bmi160_set_scale(data,
457 bmi160_to_sensor(chan->type), val2);
458 break;
459 case IIO_CHAN_INFO_SAMP_FREQ:
460 return bmi160_set_odr(data, bmi160_to_sensor(chan->type),
461 val, val2);
462 default:
463 return -EINVAL;
464 }
465
466 return 0;
467}
468
469static const struct iio_info bmi160_info = {
470 .driver_module = THIS_MODULE,
471 .read_raw = bmi160_read_raw,
472 .write_raw = bmi160_write_raw,
473};
474
475static const char *bmi160_match_acpi_device(struct device *dev)
476{
477 const struct acpi_device_id *id;
478
479 id = acpi_match_device(dev->driver->acpi_match_table, dev);
480 if (!id)
481 return NULL;
482
483 return dev_name(dev);
484}
485
486static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
487{
488 int ret;
489 unsigned int val;
490 struct device *dev = regmap_get_device(data->regmap);
491
492 ret = regmap_write(data->regmap, BMI160_REG_CMD, BMI160_CMD_SOFTRESET);
493 if (ret < 0)
494 return ret;
495
496 usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1);
497
498 /*
499 * CS rising edge is needed before starting SPI, so do a dummy read
500 * See Section 3.2.1, page 86 of the datasheet
501 */
502 if (use_spi) {
503 ret = regmap_read(data->regmap, BMI160_REG_DUMMY, &val);
504 if (ret < 0)
505 return ret;
506 }
507
508 ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val);
509 if (ret < 0) {
510 dev_err(dev, "Error reading chip id\n");
511 return ret;
512 }
513 if (val != BMI160_CHIP_ID_VAL) {
514 dev_err(dev, "Wrong chip id, got %x expected %x\n",
515 val, BMI160_CHIP_ID_VAL);
516 return -ENODEV;
517 }
518
519 ret = bmi160_set_mode(data, BMI160_ACCEL, true);
520 if (ret < 0)
521 return ret;
522
523 ret = bmi160_set_mode(data, BMI160_GYRO, true);
524 if (ret < 0)
525 return ret;
526
527 return 0;
528}
529
530static void bmi160_chip_uninit(struct bmi160_data *data)
531{
532 bmi160_set_mode(data, BMI160_GYRO, false);
533 bmi160_set_mode(data, BMI160_ACCEL, false);
534}
535
536int bmi160_core_probe(struct device *dev, struct regmap *regmap,
537 const char *name, bool use_spi)
538{
539 struct iio_dev *indio_dev;
540 struct bmi160_data *data;
541 int ret;
542
543 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
544 if (!indio_dev)
545 return -ENOMEM;
546
547 data = iio_priv(indio_dev);
548 dev_set_drvdata(dev, indio_dev);
549 data->regmap = regmap;
550
551 ret = bmi160_chip_init(data, use_spi);
552 if (ret < 0)
553 return ret;
554
555 if (!name && ACPI_HANDLE(dev))
556 name = bmi160_match_acpi_device(dev);
557
558 indio_dev->dev.parent = dev;
559 indio_dev->channels = bmi160_channels;
560 indio_dev->num_channels = ARRAY_SIZE(bmi160_channels);
561 indio_dev->name = name;
562 indio_dev->modes = INDIO_DIRECT_MODE;
563 indio_dev->info = &bmi160_info;
564
565 ret = iio_triggered_buffer_setup(indio_dev, NULL,
566 bmi160_trigger_handler, NULL);
567 if (ret < 0)
568 goto uninit;
569
570 ret = iio_device_register(indio_dev);
571 if (ret < 0)
572 goto buffer_cleanup;
573
574 return 0;
575buffer_cleanup:
576 iio_triggered_buffer_cleanup(indio_dev);
577uninit:
578 bmi160_chip_uninit(data);
579 return ret;
580}
581EXPORT_SYMBOL_GPL(bmi160_core_probe);
582
583void bmi160_core_remove(struct device *dev)
584{
585 struct iio_dev *indio_dev = dev_get_drvdata(dev);
586 struct bmi160_data *data = iio_priv(indio_dev);
587
588 iio_device_unregister(indio_dev);
589 iio_triggered_buffer_cleanup(indio_dev);
590 bmi160_chip_uninit(data);
591}
592EXPORT_SYMBOL_GPL(bmi160_core_remove);
593
594MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com");
595MODULE_DESCRIPTION("Bosch BMI160 driver");
596MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/imu/bmi160/bmi160_i2c.c b/drivers/iio/imu/bmi160/bmi160_i2c.c
new file mode 100644
index 000000000000..07a179d8fb48
--- /dev/null
+++ b/drivers/iio/imu/bmi160/bmi160_i2c.c
@@ -0,0 +1,72 @@
1/*
2 * BMI160 - Bosch IMU, I2C bits
3 *
4 * Copyright (c) 2016, Intel Corporation.
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * 7-bit I2C slave address is:
11 * - 0x68 if SDO is pulled to GND
12 * - 0x69 if SDO is pulled to VDDIO
13 */
14#include <linux/module.h>
15#include <linux/i2c.h>
16#include <linux/regmap.h>
17#include <linux/acpi.h>
18
19#include "bmi160.h"
20
21static int bmi160_i2c_probe(struct i2c_client *client,
22 const struct i2c_device_id *id)
23{
24 struct regmap *regmap;
25 const char *name = NULL;
26
27 regmap = devm_regmap_init_i2c(client, &bmi160_regmap_config);
28 if (IS_ERR(regmap)) {
29 dev_err(&client->dev, "Failed to register i2c regmap %d\n",
30 (int)PTR_ERR(regmap));
31 return PTR_ERR(regmap);
32 }
33
34 if (id)
35 name = id->name;
36
37 return bmi160_core_probe(&client->dev, regmap, name, false);
38}
39
40static int bmi160_i2c_remove(struct i2c_client *client)
41{
42 bmi160_core_remove(&client->dev);
43
44 return 0;
45}
46
47static const struct i2c_device_id bmi160_i2c_id[] = {
48 {"bmi160", 0},
49 {}
50};
51MODULE_DEVICE_TABLE(i2c, bmi160_i2c_id);
52
53static const struct acpi_device_id bmi160_acpi_match[] = {
54 {"BMI0160", 0},
55 { },
56};
57MODULE_DEVICE_TABLE(acpi, bmi160_acpi_match);
58
59static struct i2c_driver bmi160_i2c_driver = {
60 .driver = {
61 .name = "bmi160_i2c",
62 .acpi_match_table = ACPI_PTR(bmi160_acpi_match),
63 },
64 .probe = bmi160_i2c_probe,
65 .remove = bmi160_i2c_remove,
66 .id_table = bmi160_i2c_id,
67};
68module_i2c_driver(bmi160_i2c_driver);
69
70MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
71MODULE_DESCRIPTION("BMI160 I2C driver");
72MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/imu/bmi160/bmi160_spi.c b/drivers/iio/imu/bmi160/bmi160_spi.c
new file mode 100644
index 000000000000..1ec8b12bd984
--- /dev/null
+++ b/drivers/iio/imu/bmi160/bmi160_spi.c
@@ -0,0 +1,63 @@
1/*
2 * BMI160 - Bosch IMU, SPI bits
3 *
4 * Copyright (c) 2016, Intel Corporation.
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 */
10#include <linux/module.h>
11#include <linux/spi/spi.h>
12#include <linux/regmap.h>
13#include <linux/acpi.h>
14
15#include "bmi160.h"
16
17static int bmi160_spi_probe(struct spi_device *spi)
18{
19 struct regmap *regmap;
20 const struct spi_device_id *id = spi_get_device_id(spi);
21
22 regmap = devm_regmap_init_spi(spi, &bmi160_regmap_config);
23 if (IS_ERR(regmap)) {
24 dev_err(&spi->dev, "Failed to register spi regmap %d\n",
25 (int)PTR_ERR(regmap));
26 return PTR_ERR(regmap);
27 }
28 return bmi160_core_probe(&spi->dev, regmap, id->name, true);
29}
30
31static int bmi160_spi_remove(struct spi_device *spi)
32{
33 bmi160_core_remove(&spi->dev);
34
35 return 0;
36}
37
38static const struct spi_device_id bmi160_spi_id[] = {
39 {"bmi160", 0},
40 {}
41};
42MODULE_DEVICE_TABLE(spi, bmi160_spi_id);
43
44static const struct acpi_device_id bmi160_acpi_match[] = {
45 {"BMI0160", 0},
46 { },
47};
48MODULE_DEVICE_TABLE(acpi, bmi160_acpi_match);
49
50static struct spi_driver bmi160_spi_driver = {
51 .probe = bmi160_spi_probe,
52 .remove = bmi160_spi_remove,
53 .id_table = bmi160_spi_id,
54 .driver = {
55 .acpi_match_table = ACPI_PTR(bmi160_acpi_match),
56 .name = "bmi160_spi",
57 },
58};
59module_spi_driver(bmi160_spi_driver);
60
61MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com");
62MODULE_DESCRIPTION("Bosch BMI160 SPI driver");
63MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/imu/inv_mpu6050/Kconfig b/drivers/iio/imu/inv_mpu6050/Kconfig
index 847455a2d6bb..f756feecfa4c 100644
--- a/drivers/iio/imu/inv_mpu6050/Kconfig
+++ b/drivers/iio/imu/inv_mpu6050/Kconfig
@@ -13,10 +13,8 @@ config INV_MPU6050_I2C
13 select INV_MPU6050_IIO 13 select INV_MPU6050_IIO
14 select REGMAP_I2C 14 select REGMAP_I2C
15 help 15 help
16 This driver supports the Invensense MPU6050 devices. 16 This driver supports the Invensense MPU6050/6500/9150 motion tracking
17 This driver can also support MPU6500 in MPU6050 compatibility mode 17 devices over I2C.
18 and also in MPU6500 mode with some limitations.
19 It is a gyroscope/accelerometer combo device.
20 This driver can be built as a module. The module will be called 18 This driver can be built as a module. The module will be called
21 inv-mpu6050-i2c. 19 inv-mpu6050-i2c.
22 20
@@ -26,7 +24,7 @@ config INV_MPU6050_SPI
26 select INV_MPU6050_IIO 24 select INV_MPU6050_IIO
27 select REGMAP_SPI 25 select REGMAP_SPI
28 help 26 help
29 This driver supports the Invensense MPU6050 devices. 27 This driver supports the Invensense MPU6000/6500/9150 motion tracking
30 It is a gyroscope/accelerometer combo device. 28 devices over SPI.
31 This driver can be built as a module. The module will be called 29 This driver can be built as a module. The module will be called
32 inv-mpu6050-spi. 30 inv-mpu6050-spi.
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
index 0c2bded2b5b7..ee40dae5ab58 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
@@ -87,16 +87,29 @@ static const struct inv_mpu6050_chip_config chip_config_6050 = {
87 .accl_fs = INV_MPU6050_FS_02G, 87 .accl_fs = INV_MPU6050_FS_02G,
88}; 88};
89 89
90/* Indexed by enum inv_devices */
90static const struct inv_mpu6050_hw hw_info[] = { 91static const struct inv_mpu6050_hw hw_info[] = {
91 { 92 {
92 .num_reg = 117, 93 .whoami = INV_MPU6050_WHOAMI_VALUE,
94 .name = "MPU6050",
95 .reg = &reg_set_6050,
96 .config = &chip_config_6050,
97 },
98 {
99 .whoami = INV_MPU6500_WHOAMI_VALUE,
93 .name = "MPU6500", 100 .name = "MPU6500",
94 .reg = &reg_set_6500, 101 .reg = &reg_set_6500,
95 .config = &chip_config_6050, 102 .config = &chip_config_6050,
96 }, 103 },
97 { 104 {
98 .num_reg = 117, 105 .whoami = INV_MPU6000_WHOAMI_VALUE,
99 .name = "MPU6050", 106 .name = "MPU6000",
107 .reg = &reg_set_6050,
108 .config = &chip_config_6050,
109 },
110 {
111 .whoami = INV_MPU9150_WHOAMI_VALUE,
112 .name = "MPU9150",
100 .reg = &reg_set_6050, 113 .reg = &reg_set_6050,
101 .config = &chip_config_6050, 114 .config = &chip_config_6050,
102 }, 115 },
@@ -599,6 +612,10 @@ inv_fifo_rate_show(struct device *dev, struct device_attribute *attr,
599/** 612/**
600 * inv_attr_show() - calling this function will show current 613 * inv_attr_show() - calling this function will show current
601 * parameters. 614 * parameters.
615 *
616 * Deprecated in favor of IIO mounting matrix API.
617 *
618 * See inv_get_mount_matrix()
602 */ 619 */
603static ssize_t inv_attr_show(struct device *dev, struct device_attribute *attr, 620static ssize_t inv_attr_show(struct device *dev, struct device_attribute *attr,
604 char *buf) 621 char *buf)
@@ -643,6 +660,18 @@ static int inv_mpu6050_validate_trigger(struct iio_dev *indio_dev,
643 return 0; 660 return 0;
644} 661}
645 662
663static const struct iio_mount_matrix *
664inv_get_mount_matrix(const struct iio_dev *indio_dev,
665 const struct iio_chan_spec *chan)
666{
667 return &((struct inv_mpu6050_state *)iio_priv(indio_dev))->orientation;
668}
669
670static const struct iio_chan_spec_ext_info inv_ext_info[] = {
671 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, inv_get_mount_matrix),
672 { },
673};
674
646#define INV_MPU6050_CHAN(_type, _channel2, _index) \ 675#define INV_MPU6050_CHAN(_type, _channel2, _index) \
647 { \ 676 { \
648 .type = _type, \ 677 .type = _type, \
@@ -659,6 +688,7 @@ static int inv_mpu6050_validate_trigger(struct iio_dev *indio_dev,
659 .shift = 0, \ 688 .shift = 0, \
660 .endianness = IIO_BE, \ 689 .endianness = IIO_BE, \
661 }, \ 690 }, \
691 .ext_info = inv_ext_info, \
662 } 692 }
663 693
664static const struct iio_chan_spec inv_mpu_channels[] = { 694static const struct iio_chan_spec inv_mpu_channels[] = {
@@ -691,14 +721,16 @@ static IIO_CONST_ATTR(in_accel_scale_available,
691 "0.000598 0.001196 0.002392 0.004785"); 721 "0.000598 0.001196 0.002392 0.004785");
692static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, inv_fifo_rate_show, 722static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, inv_fifo_rate_show,
693 inv_mpu6050_fifo_rate_store); 723 inv_mpu6050_fifo_rate_store);
724
725/* Deprecated: kept for userspace backward compatibility. */
694static IIO_DEVICE_ATTR(in_gyro_matrix, S_IRUGO, inv_attr_show, NULL, 726static IIO_DEVICE_ATTR(in_gyro_matrix, S_IRUGO, inv_attr_show, NULL,
695 ATTR_GYRO_MATRIX); 727 ATTR_GYRO_MATRIX);
696static IIO_DEVICE_ATTR(in_accel_matrix, S_IRUGO, inv_attr_show, NULL, 728static IIO_DEVICE_ATTR(in_accel_matrix, S_IRUGO, inv_attr_show, NULL,
697 ATTR_ACCL_MATRIX); 729 ATTR_ACCL_MATRIX);
698 730
699static struct attribute *inv_attributes[] = { 731static struct attribute *inv_attributes[] = {
700 &iio_dev_attr_in_gyro_matrix.dev_attr.attr, 732 &iio_dev_attr_in_gyro_matrix.dev_attr.attr, /* deprecated */
701 &iio_dev_attr_in_accel_matrix.dev_attr.attr, 733 &iio_dev_attr_in_accel_matrix.dev_attr.attr, /* deprecated */
702 &iio_dev_attr_sampling_frequency.dev_attr.attr, 734 &iio_dev_attr_sampling_frequency.dev_attr.attr,
703 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 735 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
704 &iio_const_attr_in_accel_scale_available.dev_attr.attr, 736 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
@@ -725,6 +757,7 @@ static const struct iio_info mpu_info = {
725static int inv_check_and_setup_chip(struct inv_mpu6050_state *st) 757static int inv_check_and_setup_chip(struct inv_mpu6050_state *st)
726{ 758{
727 int result; 759 int result;
760 unsigned int regval;
728 761
729 st->hw = &hw_info[st->chip_type]; 762 st->hw = &hw_info[st->chip_type];
730 st->reg = hw_info[st->chip_type].reg; 763 st->reg = hw_info[st->chip_type].reg;
@@ -735,6 +768,17 @@ static int inv_check_and_setup_chip(struct inv_mpu6050_state *st)
735 if (result) 768 if (result)
736 return result; 769 return result;
737 msleep(INV_MPU6050_POWER_UP_TIME); 770 msleep(INV_MPU6050_POWER_UP_TIME);
771
772 /* check chip self-identification */
773 result = regmap_read(st->map, INV_MPU6050_REG_WHOAMI, &regval);
774 if (result)
775 return result;
776 if (regval != st->hw->whoami) {
777 dev_warn(regmap_get_device(st->map),
778 "whoami mismatch got %#02x expected %#02hhx for %s\n",
779 regval, st->hw->whoami, st->hw->name);
780 }
781
738 /* 782 /*
739 * toggle power state. After reset, the sleep bit could be on 783 * toggle power state. After reset, the sleep bit could be on
740 * or off depending on the OTP settings. Toggling power would 784 * or off depending on the OTP settings. Toggling power would
@@ -773,14 +817,31 @@ int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
773 if (!indio_dev) 817 if (!indio_dev)
774 return -ENOMEM; 818 return -ENOMEM;
775 819
820 BUILD_BUG_ON(ARRAY_SIZE(hw_info) != INV_NUM_PARTS);
821 if (chip_type < 0 || chip_type >= INV_NUM_PARTS) {
822 dev_err(dev, "Bad invensense chip_type=%d name=%s\n",
823 chip_type, name);
824 return -ENODEV;
825 }
776 st = iio_priv(indio_dev); 826 st = iio_priv(indio_dev);
777 st->chip_type = chip_type; 827 st->chip_type = chip_type;
778 st->powerup_count = 0; 828 st->powerup_count = 0;
779 st->irq = irq; 829 st->irq = irq;
780 st->map = regmap; 830 st->map = regmap;
831
781 pdata = dev_get_platdata(dev); 832 pdata = dev_get_platdata(dev);
782 if (pdata) 833 if (!pdata) {
834 result = of_iio_read_mount_matrix(dev, "mount-matrix",
835 &st->orientation);
836 if (result) {
837 dev_err(dev, "Failed to retrieve mounting matrix %d\n",
838 result);
839 return result;
840 }
841 } else {
783 st->plat_data = *pdata; 842 st->plat_data = *pdata;
843 }
844
784 /* power is turned on inside check chip type*/ 845 /* power is turned on inside check chip type*/
785 result = inv_check_and_setup_chip(st); 846 result = inv_check_and_setup_chip(st);
786 if (result) 847 if (result)
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
index 9ba1179105bd..e1fd7fa53e3b 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
@@ -169,13 +169,14 @@ static int inv_mpu_remove(struct i2c_client *client)
169static const struct i2c_device_id inv_mpu_id[] = { 169static const struct i2c_device_id inv_mpu_id[] = {
170 {"mpu6050", INV_MPU6050}, 170 {"mpu6050", INV_MPU6050},
171 {"mpu6500", INV_MPU6500}, 171 {"mpu6500", INV_MPU6500},
172 {"mpu9150", INV_MPU9150},
172 {} 173 {}
173}; 174};
174 175
175MODULE_DEVICE_TABLE(i2c, inv_mpu_id); 176MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
176 177
177static const struct acpi_device_id inv_acpi_match[] = { 178static const struct acpi_device_id inv_acpi_match[] = {
178 {"INVN6500", 0}, 179 {"INVN6500", INV_MPU6500},
179 { }, 180 { },
180}; 181};
181 182
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
index bb3cef6d7059..3bf8544ccc9f 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
@@ -69,6 +69,7 @@ enum inv_devices {
69 INV_MPU6050, 69 INV_MPU6050,
70 INV_MPU6500, 70 INV_MPU6500,
71 INV_MPU6000, 71 INV_MPU6000,
72 INV_MPU9150,
72 INV_NUM_PARTS 73 INV_NUM_PARTS
73}; 74};
74 75
@@ -94,13 +95,13 @@ struct inv_mpu6050_chip_config {
94 95
95/** 96/**
96 * struct inv_mpu6050_hw - Other important hardware information. 97 * struct inv_mpu6050_hw - Other important hardware information.
97 * @num_reg: Number of registers on device. 98 * @whoami: Self identification byte from WHO_AM_I register
98 * @name: name of the chip. 99 * @name: name of the chip.
99 * @reg: register map of the chip. 100 * @reg: register map of the chip.
100 * @config: configuration of the chip. 101 * @config: configuration of the chip.
101 */ 102 */
102struct inv_mpu6050_hw { 103struct inv_mpu6050_hw {
103 u8 num_reg; 104 u8 whoami;
104 u8 *name; 105 u8 *name;
105 const struct inv_mpu6050_reg_map *reg; 106 const struct inv_mpu6050_reg_map *reg;
106 const struct inv_mpu6050_chip_config *config; 107 const struct inv_mpu6050_chip_config *config;
@@ -115,7 +116,8 @@ struct inv_mpu6050_hw {
115 * @hw: Other hardware-specific information. 116 * @hw: Other hardware-specific information.
116 * @chip_type: chip type. 117 * @chip_type: chip type.
117 * @time_stamp_lock: spin lock to time stamp. 118 * @time_stamp_lock: spin lock to time stamp.
118 * @plat_data: platform data. 119 * @plat_data: platform data (deprecated in favor of @orientation).
120 * @orientation: sensor chip orientation relative to main hardware.
119 * @timestamps: kfifo queue to store time stamp. 121 * @timestamps: kfifo queue to store time stamp.
120 * @map regmap pointer. 122 * @map regmap pointer.
121 * @irq interrupt number. 123 * @irq interrupt number.
@@ -132,6 +134,7 @@ struct inv_mpu6050_state {
132 struct i2c_client *mux_client; 134 struct i2c_client *mux_client;
133 unsigned int powerup_count; 135 unsigned int powerup_count;
134 struct inv_mpu6050_platform_data plat_data; 136 struct inv_mpu6050_platform_data plat_data;
137 struct iio_mount_matrix orientation;
135 DECLARE_KFIFO(timestamps, long long, TIMESTAMP_FIFO_SIZE); 138 DECLARE_KFIFO(timestamps, long long, TIMESTAMP_FIFO_SIZE);
136 struct regmap *map; 139 struct regmap *map;
137 int irq; 140 int irq;
@@ -216,6 +219,13 @@ struct inv_mpu6050_state {
216#define INV_MPU6050_MIN_FIFO_RATE 4 219#define INV_MPU6050_MIN_FIFO_RATE 4
217#define INV_MPU6050_ONE_K_HZ 1000 220#define INV_MPU6050_ONE_K_HZ 1000
218 221
222#define INV_MPU6050_REG_WHOAMI 117
223
224#define INV_MPU6000_WHOAMI_VALUE 0x68
225#define INV_MPU6050_WHOAMI_VALUE 0x68
226#define INV_MPU6500_WHOAMI_VALUE 0x70
227#define INV_MPU9150_WHOAMI_VALUE 0x68
228
219/* scan element definition */ 229/* scan element definition */
220enum inv_mpu6050_scan { 230enum inv_mpu6050_scan {
221 INV_MPU6050_SCAN_ACCL_X, 231 INV_MPU6050_SCAN_ACCL_X,
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c
index 7bcb8d839f05..190a4a51c830 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c
@@ -44,9 +44,19 @@ static int inv_mpu_i2c_disable(struct iio_dev *indio_dev)
44static int inv_mpu_probe(struct spi_device *spi) 44static int inv_mpu_probe(struct spi_device *spi)
45{ 45{
46 struct regmap *regmap; 46 struct regmap *regmap;
47 const struct spi_device_id *id = spi_get_device_id(spi); 47 const struct spi_device_id *spi_id;
48 const char *name = id ? id->name : NULL; 48 const struct acpi_device_id *acpi_id;
49 const int chip_type = id ? id->driver_data : 0; 49 const char *name = NULL;
50 enum inv_devices chip_type;
51
52 if ((spi_id = spi_get_device_id(spi))) {
53 chip_type = (enum inv_devices)spi_id->driver_data;
54 name = spi_id->name;
55 } else if ((acpi_id = acpi_match_device(spi->dev.driver->acpi_match_table, &spi->dev))) {
56 chip_type = (enum inv_devices)acpi_id->driver_data;
57 } else {
58 return -ENODEV;
59 }
50 60
51 regmap = devm_regmap_init_spi(spi, &inv_mpu_regmap_config); 61 regmap = devm_regmap_init_spi(spi, &inv_mpu_regmap_config);
52 if (IS_ERR(regmap)) { 62 if (IS_ERR(regmap)) {
@@ -70,13 +80,15 @@ static int inv_mpu_remove(struct spi_device *spi)
70 */ 80 */
71static const struct spi_device_id inv_mpu_id[] = { 81static const struct spi_device_id inv_mpu_id[] = {
72 {"mpu6000", INV_MPU6000}, 82 {"mpu6000", INV_MPU6000},
83 {"mpu6500", INV_MPU6500},
84 {"mpu9150", INV_MPU9150},
73 {} 85 {}
74}; 86};
75 87
76MODULE_DEVICE_TABLE(spi, inv_mpu_id); 88MODULE_DEVICE_TABLE(spi, inv_mpu_id);
77 89
78static const struct acpi_device_id inv_acpi_match[] = { 90static const struct acpi_device_id inv_acpi_match[] = {
79 {"INVN6000", 0}, 91 {"INVN6000", INV_MPU6000},
80 { }, 92 { },
81}; 93};
82MODULE_DEVICE_TABLE(acpi, inv_acpi_match); 94MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
diff --git a/drivers/iio/imu/kmx61.c b/drivers/iio/imu/kmx61.c
index e5306b4e020e..2e7dd5754a56 100644
--- a/drivers/iio/imu/kmx61.c
+++ b/drivers/iio/imu/kmx61.c
@@ -14,7 +14,6 @@
14#include <linux/module.h> 14#include <linux/module.h>
15#include <linux/i2c.h> 15#include <linux/i2c.h>
16#include <linux/acpi.h> 16#include <linux/acpi.h>
17#include <linux/gpio/consumer.h>
18#include <linux/interrupt.h> 17#include <linux/interrupt.h>
19#include <linux/pm.h> 18#include <linux/pm.h>
20#include <linux/pm_runtime.h> 19#include <linux/pm_runtime.h>
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 70cb7eb0a75c..e6319a9346b2 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -25,6 +25,7 @@
25#include <linux/slab.h> 25#include <linux/slab.h>
26#include <linux/anon_inodes.h> 26#include <linux/anon_inodes.h>
27#include <linux/debugfs.h> 27#include <linux/debugfs.h>
28#include <linux/mutex.h>
28#include <linux/iio/iio.h> 29#include <linux/iio/iio.h>
29#include "iio_core.h" 30#include "iio_core.h"
30#include "iio_core_trigger.h" 31#include "iio_core_trigger.h"
@@ -78,6 +79,7 @@ static const char * const iio_chan_type_name_spec[] = {
78 [IIO_CONCENTRATION] = "concentration", 79 [IIO_CONCENTRATION] = "concentration",
79 [IIO_RESISTANCE] = "resistance", 80 [IIO_RESISTANCE] = "resistance",
80 [IIO_PH] = "ph", 81 [IIO_PH] = "ph",
82 [IIO_UVINDEX] = "uvindex",
81}; 83};
82 84
83static const char * const iio_modifier_names[] = { 85static const char * const iio_modifier_names[] = {
@@ -100,6 +102,7 @@ static const char * const iio_modifier_names[] = {
100 [IIO_MOD_LIGHT_RED] = "red", 102 [IIO_MOD_LIGHT_RED] = "red",
101 [IIO_MOD_LIGHT_GREEN] = "green", 103 [IIO_MOD_LIGHT_GREEN] = "green",
102 [IIO_MOD_LIGHT_BLUE] = "blue", 104 [IIO_MOD_LIGHT_BLUE] = "blue",
105 [IIO_MOD_LIGHT_UV] = "uv",
103 [IIO_MOD_QUATERNION] = "quaternion", 106 [IIO_MOD_QUATERNION] = "quaternion",
104 [IIO_MOD_TEMP_AMBIENT] = "ambient", 107 [IIO_MOD_TEMP_AMBIENT] = "ambient",
105 [IIO_MOD_TEMP_OBJECT] = "object", 108 [IIO_MOD_TEMP_OBJECT] = "object",
@@ -409,6 +412,88 @@ ssize_t iio_enum_write(struct iio_dev *indio_dev,
409} 412}
410EXPORT_SYMBOL_GPL(iio_enum_write); 413EXPORT_SYMBOL_GPL(iio_enum_write);
411 414
415static const struct iio_mount_matrix iio_mount_idmatrix = {
416 .rotation = {
417 "1", "0", "0",
418 "0", "1", "0",
419 "0", "0", "1"
420 }
421};
422
423static int iio_setup_mount_idmatrix(const struct device *dev,
424 struct iio_mount_matrix *matrix)
425{
426 *matrix = iio_mount_idmatrix;
427 dev_info(dev, "mounting matrix not found: using identity...\n");
428 return 0;
429}
430
431ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
432 const struct iio_chan_spec *chan, char *buf)
433{
434 const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
435 priv)(indio_dev, chan);
436
437 if (IS_ERR(mtx))
438 return PTR_ERR(mtx);
439
440 if (!mtx)
441 mtx = &iio_mount_idmatrix;
442
443 return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
444 mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
445 mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
446 mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
447}
448EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
449
450/**
451 * of_iio_read_mount_matrix() - retrieve iio device mounting matrix from
452 * device-tree "mount-matrix" property
453 * @dev: device the mounting matrix property is assigned to
454 * @propname: device specific mounting matrix property name
455 * @matrix: where to store retrieved matrix
456 *
457 * If device is assigned no mounting matrix property, a default 3x3 identity
458 * matrix will be filled in.
459 *
460 * Return: 0 if success, or a negative error code on failure.
461 */
462#ifdef CONFIG_OF
463int of_iio_read_mount_matrix(const struct device *dev,
464 const char *propname,
465 struct iio_mount_matrix *matrix)
466{
467 if (dev->of_node) {
468 int err = of_property_read_string_array(dev->of_node,
469 propname, matrix->rotation,
470 ARRAY_SIZE(iio_mount_idmatrix.rotation));
471
472 if (err == ARRAY_SIZE(iio_mount_idmatrix.rotation))
473 return 0;
474
475 if (err >= 0)
476 /* Invalid number of matrix entries. */
477 return -EINVAL;
478
479 if (err != -EINVAL)
480 /* Invalid matrix declaration format. */
481 return err;
482 }
483
484 /* Matrix was not declared at all: fallback to identity. */
485 return iio_setup_mount_idmatrix(dev, matrix);
486}
487#else
488int of_iio_read_mount_matrix(const struct device *dev,
489 const char *propname,
490 struct iio_mount_matrix *matrix)
491{
492 return iio_setup_mount_idmatrix(dev, matrix);
493}
494#endif
495EXPORT_SYMBOL(of_iio_read_mount_matrix);
496
412/** 497/**
413 * iio_format_value() - Formats a IIO value into its string representation 498 * iio_format_value() - Formats a IIO value into its string representation
414 * @buf: The buffer to which the formatted value gets written 499 * @buf: The buffer to which the formatted value gets written
@@ -1375,6 +1460,44 @@ void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1375} 1460}
1376EXPORT_SYMBOL_GPL(devm_iio_device_unregister); 1461EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1377 1462
1463/**
1464 * iio_device_claim_direct_mode - Keep device in direct mode
1465 * @indio_dev: the iio_dev associated with the device
1466 *
1467 * If the device is in direct mode it is guaranteed to stay
1468 * that way until iio_device_release_direct_mode() is called.
1469 *
1470 * Use with iio_device_release_direct_mode()
1471 *
1472 * Returns: 0 on success, -EBUSY on failure
1473 */
1474int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
1475{
1476 mutex_lock(&indio_dev->mlock);
1477
1478 if (iio_buffer_enabled(indio_dev)) {
1479 mutex_unlock(&indio_dev->mlock);
1480 return -EBUSY;
1481 }
1482 return 0;
1483}
1484EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
1485
1486/**
1487 * iio_device_release_direct_mode - releases claim on direct mode
1488 * @indio_dev: the iio_dev associated with the device
1489 *
1490 * Release the claim. Device is no longer guaranteed to stay
1491 * in direct mode.
1492 *
1493 * Use with iio_device_claim_direct_mode()
1494 */
1495void iio_device_release_direct_mode(struct iio_dev *indio_dev)
1496{
1497 mutex_unlock(&indio_dev->mlock);
1498}
1499EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
1500
1378subsys_initcall(iio_init); 1501subsys_initcall(iio_init);
1379module_exit(iio_exit); 1502module_exit(iio_exit);
1380 1503
diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index 734a0042de0c..c4757e6367e7 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -356,6 +356,54 @@ void iio_channel_release(struct iio_channel *channel)
356} 356}
357EXPORT_SYMBOL_GPL(iio_channel_release); 357EXPORT_SYMBOL_GPL(iio_channel_release);
358 358
359static void devm_iio_channel_free(struct device *dev, void *res)
360{
361 struct iio_channel *channel = *(struct iio_channel **)res;
362
363 iio_channel_release(channel);
364}
365
366static int devm_iio_channel_match(struct device *dev, void *res, void *data)
367{
368 struct iio_channel **r = res;
369
370 if (!r || !*r) {
371 WARN_ON(!r || !*r);
372 return 0;
373 }
374
375 return *r == data;
376}
377
378struct iio_channel *devm_iio_channel_get(struct device *dev,
379 const char *channel_name)
380{
381 struct iio_channel **ptr, *channel;
382
383 ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
384 if (!ptr)
385 return ERR_PTR(-ENOMEM);
386
387 channel = iio_channel_get(dev, channel_name);
388 if (IS_ERR(channel)) {
389 devres_free(ptr);
390 return channel;
391 }
392
393 *ptr = channel;
394 devres_add(dev, ptr);
395
396 return channel;
397}
398EXPORT_SYMBOL_GPL(devm_iio_channel_get);
399
400void devm_iio_channel_release(struct device *dev, struct iio_channel *channel)
401{
402 WARN_ON(devres_release(dev, devm_iio_channel_free,
403 devm_iio_channel_match, channel));
404}
405EXPORT_SYMBOL_GPL(devm_iio_channel_release);
406
359struct iio_channel *iio_channel_get_all(struct device *dev) 407struct iio_channel *iio_channel_get_all(struct device *dev)
360{ 408{
361 const char *name; 409 const char *name;
@@ -441,6 +489,42 @@ void iio_channel_release_all(struct iio_channel *channels)
441} 489}
442EXPORT_SYMBOL_GPL(iio_channel_release_all); 490EXPORT_SYMBOL_GPL(iio_channel_release_all);
443 491
492static void devm_iio_channel_free_all(struct device *dev, void *res)
493{
494 struct iio_channel *channels = *(struct iio_channel **)res;
495
496 iio_channel_release_all(channels);
497}
498
499struct iio_channel *devm_iio_channel_get_all(struct device *dev)
500{
501 struct iio_channel **ptr, *channels;
502
503 ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL);
504 if (!ptr)
505 return ERR_PTR(-ENOMEM);
506
507 channels = iio_channel_get_all(dev);
508 if (IS_ERR(channels)) {
509 devres_free(ptr);
510 return channels;
511 }
512
513 *ptr = channels;
514 devres_add(dev, ptr);
515
516 return channels;
517}
518EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
519
520void devm_iio_channel_release_all(struct device *dev,
521 struct iio_channel *channels)
522{
523 WARN_ON(devres_release(dev, devm_iio_channel_free_all,
524 devm_iio_channel_match, channels));
525}
526EXPORT_SYMBOL_GPL(devm_iio_channel_release_all);
527
444static int iio_channel_read(struct iio_channel *chan, int *val, int *val2, 528static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
445 enum iio_chan_info_enum info) 529 enum iio_chan_info_enum info)
446{ 530{
@@ -452,7 +536,7 @@ static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
452 if (val2 == NULL) 536 if (val2 == NULL)
453 val2 = &unused; 537 val2 = &unused;
454 538
455 if(!iio_channel_has_info(chan->channel, info)) 539 if (!iio_channel_has_info(chan->channel, info))
456 return -EINVAL; 540 return -EINVAL;
457 541
458 if (chan->indio_dev->info->read_raw_multi) { 542 if (chan->indio_dev->info->read_raw_multi) {
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index cfd3df8416bb..7c566f516572 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -73,6 +73,17 @@ config BH1750
73 To compile this driver as a module, choose M here: the module will 73 To compile this driver as a module, choose M here: the module will
74 be called bh1750. 74 be called bh1750.
75 75
76config BH1780
77 tristate "ROHM BH1780 ambient light sensor"
78 depends on I2C
79 depends on !SENSORS_BH1780
80 help
81 Say Y here to build support for the ROHM BH1780GLI ambient
82 light sensor.
83
84 To compile this driver as a module, choose M here: the module will
85 be called bh1780.
86
76config CM32181 87config CM32181
77 depends on I2C 88 depends on I2C
78 tristate "CM32181 driver" 89 tristate "CM32181 driver"
@@ -223,6 +234,17 @@ config LTR501
223 This driver can also be built as a module. If so, the module 234 This driver can also be built as a module. If so, the module
224 will be called ltr501. 235 will be called ltr501.
225 236
237config MAX44000
238 tristate "MAX44000 Ambient and Infrared Proximity Sensor"
239 depends on I2C
240 select REGMAP_I2C
241 help
242 Say Y here if you want to build support for Maxim Integrated's
243 MAX44000 ambient and infrared proximity sensor device.
244
245 To compile this driver as a module, choose M here:
246 the module will be called max44000.
247
226config OPT3001 248config OPT3001
227 tristate "Texas Instruments OPT3001 Light Sensor" 249 tristate "Texas Instruments OPT3001 Light Sensor"
228 depends on I2C 250 depends on I2C
@@ -320,4 +342,14 @@ config VCNL4000
320 To compile this driver as a module, choose M here: the 342 To compile this driver as a module, choose M here: the
321 module will be called vcnl4000. 343 module will be called vcnl4000.
322 344
345config VEML6070
346 tristate "VEML6070 UV A light sensor"
347 depends on I2C
348 help
349 Say Y here if you want to build a driver for the Vishay VEML6070 UV A
350 light sensor.
351
352 To compile this driver as a module, choose M here: the
353 module will be called veml6070.
354
323endmenu 355endmenu
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index b2c31053db0c..6f2a3c62de27 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -9,6 +9,7 @@ obj-$(CONFIG_AL3320A) += al3320a.o
9obj-$(CONFIG_APDS9300) += apds9300.o 9obj-$(CONFIG_APDS9300) += apds9300.o
10obj-$(CONFIG_APDS9960) += apds9960.o 10obj-$(CONFIG_APDS9960) += apds9960.o
11obj-$(CONFIG_BH1750) += bh1750.o 11obj-$(CONFIG_BH1750) += bh1750.o
12obj-$(CONFIG_BH1780) += bh1780.o
12obj-$(CONFIG_CM32181) += cm32181.o 13obj-$(CONFIG_CM32181) += cm32181.o
13obj-$(CONFIG_CM3232) += cm3232.o 14obj-$(CONFIG_CM3232) += cm3232.o
14obj-$(CONFIG_CM3323) += cm3323.o 15obj-$(CONFIG_CM3323) += cm3323.o
@@ -20,6 +21,7 @@ obj-$(CONFIG_ISL29125) += isl29125.o
20obj-$(CONFIG_JSA1212) += jsa1212.o 21obj-$(CONFIG_JSA1212) += jsa1212.o
21obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o 22obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o
22obj-$(CONFIG_LTR501) += ltr501.o 23obj-$(CONFIG_LTR501) += ltr501.o
24obj-$(CONFIG_MAX44000) += max44000.o
23obj-$(CONFIG_OPT3001) += opt3001.o 25obj-$(CONFIG_OPT3001) += opt3001.o
24obj-$(CONFIG_PA12203001) += pa12203001.o 26obj-$(CONFIG_PA12203001) += pa12203001.o
25obj-$(CONFIG_RPR0521) += rpr0521.o 27obj-$(CONFIG_RPR0521) += rpr0521.o
@@ -30,3 +32,4 @@ obj-$(CONFIG_TCS3472) += tcs3472.o
30obj-$(CONFIG_TSL4531) += tsl4531.o 32obj-$(CONFIG_TSL4531) += tsl4531.o
31obj-$(CONFIG_US5182D) += us5182d.o 33obj-$(CONFIG_US5182D) += us5182d.o
32obj-$(CONFIG_VCNL4000) += vcnl4000.o 34obj-$(CONFIG_VCNL4000) += vcnl4000.o
35obj-$(CONFIG_VEML6070) += veml6070.o
diff --git a/drivers/iio/light/apds9960.c b/drivers/iio/light/apds9960.c
index a6af56ad10e1..b4dbb3912977 100644
--- a/drivers/iio/light/apds9960.c
+++ b/drivers/iio/light/apds9960.c
@@ -321,8 +321,12 @@ static const struct iio_chan_spec apds9960_channels[] = {
321}; 321};
322 322
323/* integration time in us */ 323/* integration time in us */
324static const int apds9960_int_time[][2] = 324static const int apds9960_int_time[][2] = {
325 { {28000, 246}, {100000, 219}, {200000, 182}, {700000, 0} }; 325 { 28000, 246},
326 {100000, 219},
327 {200000, 182},
328 {700000, 0}
329};
326 330
327/* gain mapping */ 331/* gain mapping */
328static const int apds9960_pxs_gain_map[] = {1, 2, 4, 8}; 332static const int apds9960_pxs_gain_map[] = {1, 2, 4, 8};
@@ -491,9 +495,10 @@ static int apds9960_read_raw(struct iio_dev *indio_dev,
491 case IIO_INTENSITY: 495 case IIO_INTENSITY:
492 ret = regmap_bulk_read(data->regmap, chan->address, 496 ret = regmap_bulk_read(data->regmap, chan->address,
493 &buf, 2); 497 &buf, 2);
494 if (!ret) 498 if (!ret) {
495 ret = IIO_VAL_INT; 499 ret = IIO_VAL_INT;
496 *val = le16_to_cpu(buf); 500 *val = le16_to_cpu(buf);
501 }
497 break; 502 break;
498 default: 503 default:
499 ret = -EINVAL; 504 ret = -EINVAL;
diff --git a/drivers/iio/light/bh1780.c b/drivers/iio/light/bh1780.c
new file mode 100644
index 000000000000..72b364e4aa72
--- /dev/null
+++ b/drivers/iio/light/bh1780.c
@@ -0,0 +1,297 @@
1/*
2 * ROHM 1780GLI Ambient Light Sensor Driver
3 *
4 * Copyright (C) 2016 Linaro Ltd.
5 * Author: Linus Walleij <linus.walleij@linaro.org>
6 * Loosely based on the previous BH1780 ALS misc driver
7 * Copyright (C) 2010 Texas Instruments
8 * Author: Hemanth V <hemanthv@ti.com>
9 */
10#include <linux/i2c.h>
11#include <linux/slab.h>
12#include <linux/platform_device.h>
13#include <linux/delay.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/pm_runtime.h>
17#include <linux/iio/iio.h>
18#include <linux/iio/sysfs.h>
19#include <linux/bitops.h>
20
21#define BH1780_CMD_BIT BIT(7)
22#define BH1780_REG_CONTROL 0x00
23#define BH1780_REG_PARTID 0x0A
24#define BH1780_REG_MANFID 0x0B
25#define BH1780_REG_DLOW 0x0C
26#define BH1780_REG_DHIGH 0x0D
27
28#define BH1780_REVMASK GENMASK(3,0)
29#define BH1780_POWMASK GENMASK(1,0)
30#define BH1780_POFF (0x0)
31#define BH1780_PON (0x3)
32
33/* power on settling time in ms */
34#define BH1780_PON_DELAY 2
35/* max time before value available in ms */
36#define BH1780_INTERVAL 250
37
38struct bh1780_data {
39 struct i2c_client *client;
40};
41
42static int bh1780_write(struct bh1780_data *bh1780, u8 reg, u8 val)
43{
44 int ret = i2c_smbus_write_byte_data(bh1780->client,
45 BH1780_CMD_BIT | reg,
46 val);
47 if (ret < 0)
48 dev_err(&bh1780->client->dev,
49 "i2c_smbus_write_byte_data failed error "
50 "%d, register %01x\n",
51 ret, reg);
52 return ret;
53}
54
55static int bh1780_read(struct bh1780_data *bh1780, u8 reg)
56{
57 int ret = i2c_smbus_read_byte_data(bh1780->client,
58 BH1780_CMD_BIT | reg);
59 if (ret < 0)
60 dev_err(&bh1780->client->dev,
61 "i2c_smbus_read_byte_data failed error "
62 "%d, register %01x\n",
63 ret, reg);
64 return ret;
65}
66
67static int bh1780_read_word(struct bh1780_data *bh1780, u8 reg)
68{
69 int ret = i2c_smbus_read_word_data(bh1780->client,
70 BH1780_CMD_BIT | reg);
71 if (ret < 0)
72 dev_err(&bh1780->client->dev,
73 "i2c_smbus_read_word_data failed error "
74 "%d, register %01x\n",
75 ret, reg);
76 return ret;
77}
78
79static int bh1780_debugfs_reg_access(struct iio_dev *indio_dev,
80 unsigned int reg, unsigned int writeval,
81 unsigned int *readval)
82{
83 struct bh1780_data *bh1780 = iio_priv(indio_dev);
84 int ret;
85
86 if (!readval)
87 bh1780_write(bh1780, (u8)reg, (u8)writeval);
88
89 ret = bh1780_read(bh1780, (u8)reg);
90 if (ret < 0)
91 return ret;
92
93 *readval = ret;
94
95 return 0;
96}
97
98static int bh1780_read_raw(struct iio_dev *indio_dev,
99 struct iio_chan_spec const *chan,
100 int *val, int *val2, long mask)
101{
102 struct bh1780_data *bh1780 = iio_priv(indio_dev);
103 int value;
104
105 switch (mask) {
106 case IIO_CHAN_INFO_RAW:
107 switch (chan->type) {
108 case IIO_LIGHT:
109 pm_runtime_get_sync(&bh1780->client->dev);
110 value = bh1780_read_word(bh1780, BH1780_REG_DLOW);
111 if (value < 0)
112 return value;
113 pm_runtime_mark_last_busy(&bh1780->client->dev);
114 pm_runtime_put_autosuspend(&bh1780->client->dev);
115 *val = value;
116
117 return IIO_VAL_INT;
118 default:
119 return -EINVAL;
120 }
121 case IIO_CHAN_INFO_INT_TIME:
122 *val = 0;
123 *val2 = BH1780_INTERVAL * 1000;
124 return IIO_VAL_INT_PLUS_MICRO;
125 default:
126 return -EINVAL;
127 }
128}
129
130static const struct iio_info bh1780_info = {
131 .driver_module = THIS_MODULE,
132 .read_raw = bh1780_read_raw,
133 .debugfs_reg_access = bh1780_debugfs_reg_access,
134};
135
136static const struct iio_chan_spec bh1780_channels[] = {
137 {
138 .type = IIO_LIGHT,
139 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
140 BIT(IIO_CHAN_INFO_INT_TIME)
141 }
142};
143
144static int bh1780_probe(struct i2c_client *client,
145 const struct i2c_device_id *id)
146{
147 int ret;
148 struct bh1780_data *bh1780;
149 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
150 struct iio_dev *indio_dev;
151
152 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
153 return -EIO;
154
155 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*bh1780));
156 if (!indio_dev)
157 return -ENOMEM;
158
159 bh1780 = iio_priv(indio_dev);
160 bh1780->client = client;
161 i2c_set_clientdata(client, indio_dev);
162
163 /* Power up the device */
164 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
165 if (ret < 0)
166 return ret;
167 msleep(BH1780_PON_DELAY);
168 pm_runtime_get_noresume(&client->dev);
169 pm_runtime_set_active(&client->dev);
170 pm_runtime_enable(&client->dev);
171
172 ret = bh1780_read(bh1780, BH1780_REG_PARTID);
173 if (ret < 0)
174 goto out_disable_pm;
175 dev_info(&client->dev,
176 "Ambient Light Sensor, Rev : %lu\n",
177 (ret & BH1780_REVMASK));
178
179 /*
180 * As the device takes 250 ms to even come up with a fresh
181 * measurement after power-on, do not shut it down unnecessarily.
182 * Set autosuspend to a five seconds.
183 */
184 pm_runtime_set_autosuspend_delay(&client->dev, 5000);
185 pm_runtime_use_autosuspend(&client->dev);
186 pm_runtime_put(&client->dev);
187
188 indio_dev->dev.parent = &client->dev;
189 indio_dev->info = &bh1780_info;
190 indio_dev->name = id->name;
191 indio_dev->channels = bh1780_channels;
192 indio_dev->num_channels = ARRAY_SIZE(bh1780_channels);
193 indio_dev->modes = INDIO_DIRECT_MODE;
194
195 ret = iio_device_register(indio_dev);
196 if (ret)
197 goto out_disable_pm;
198 return 0;
199
200out_disable_pm:
201 pm_runtime_put_noidle(&client->dev);
202 pm_runtime_disable(&client->dev);
203 return ret;
204}
205
206static int bh1780_remove(struct i2c_client *client)
207{
208 struct iio_dev *indio_dev = i2c_get_clientdata(client);
209 struct bh1780_data *bh1780 = iio_priv(indio_dev);
210 int ret;
211
212 iio_device_unregister(indio_dev);
213 pm_runtime_get_sync(&client->dev);
214 pm_runtime_put_noidle(&client->dev);
215 pm_runtime_disable(&client->dev);
216 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
217 if (ret < 0) {
218 dev_err(&client->dev, "failed to power off\n");
219 return ret;
220 }
221
222 return 0;
223}
224
225#ifdef CONFIG_PM
226static int bh1780_runtime_suspend(struct device *dev)
227{
228 struct i2c_client *client = to_i2c_client(dev);
229 struct bh1780_data *bh1780 = i2c_get_clientdata(client);
230 int ret;
231
232 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF);
233 if (ret < 0) {
234 dev_err(dev, "failed to runtime suspend\n");
235 return ret;
236 }
237
238 return 0;
239}
240
241static int bh1780_runtime_resume(struct device *dev)
242{
243 struct i2c_client *client = to_i2c_client(dev);
244 struct bh1780_data *bh1780 = i2c_get_clientdata(client);
245 int ret;
246
247 ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON);
248 if (ret < 0) {
249 dev_err(dev, "failed to runtime resume\n");
250 return ret;
251 }
252
253 /* Wait for power on, then for a value to be available */
254 msleep(BH1780_PON_DELAY + BH1780_INTERVAL);
255
256 return 0;
257}
258#endif /* CONFIG_PM */
259
260static const struct dev_pm_ops bh1780_dev_pm_ops = {
261 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
262 pm_runtime_force_resume)
263 SET_RUNTIME_PM_OPS(bh1780_runtime_suspend,
264 bh1780_runtime_resume, NULL)
265};
266
267static const struct i2c_device_id bh1780_id[] = {
268 { "bh1780", 0 },
269 { },
270};
271
272MODULE_DEVICE_TABLE(i2c, bh1780_id);
273
274#ifdef CONFIG_OF
275static const struct of_device_id of_bh1780_match[] = {
276 { .compatible = "rohm,bh1780gli", },
277 {},
278};
279MODULE_DEVICE_TABLE(of, of_bh1780_match);
280#endif
281
282static struct i2c_driver bh1780_driver = {
283 .probe = bh1780_probe,
284 .remove = bh1780_remove,
285 .id_table = bh1780_id,
286 .driver = {
287 .name = "bh1780",
288 .pm = &bh1780_dev_pm_ops,
289 .of_match_table = of_match_ptr(of_bh1780_match),
290 },
291};
292
293module_i2c_driver(bh1780_driver);
294
295MODULE_DESCRIPTION("ROHM BH1780GLI Ambient Light Sensor Driver");
296MODULE_LICENSE("GPL");
297MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
diff --git a/drivers/iio/light/max44000.c b/drivers/iio/light/max44000.c
new file mode 100644
index 000000000000..e01e58a9bd14
--- /dev/null
+++ b/drivers/iio/light/max44000.c
@@ -0,0 +1,639 @@
1/*
2 * MAX44000 Ambient and Infrared Proximity Sensor
3 *
4 * Copyright (c) 2016, Intel Corporation.
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * Data sheet: https://datasheets.maximintegrated.com/en/ds/MAX44000.pdf
11 *
12 * 7-bit I2C slave address 0x4a
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/i2c.h>
18#include <linux/regmap.h>
19#include <linux/util_macros.h>
20#include <linux/iio/iio.h>
21#include <linux/iio/sysfs.h>
22#include <linux/iio/buffer.h>
23#include <linux/iio/trigger_consumer.h>
24#include <linux/iio/triggered_buffer.h>
25#include <linux/acpi.h>
26
27#define MAX44000_DRV_NAME "max44000"
28
29/* Registers in datasheet order */
30#define MAX44000_REG_STATUS 0x00
31#define MAX44000_REG_CFG_MAIN 0x01
32#define MAX44000_REG_CFG_RX 0x02
33#define MAX44000_REG_CFG_TX 0x03
34#define MAX44000_REG_ALS_DATA_HI 0x04
35#define MAX44000_REG_ALS_DATA_LO 0x05
36#define MAX44000_REG_PRX_DATA 0x16
37#define MAX44000_REG_ALS_UPTHR_HI 0x06
38#define MAX44000_REG_ALS_UPTHR_LO 0x07
39#define MAX44000_REG_ALS_LOTHR_HI 0x08
40#define MAX44000_REG_ALS_LOTHR_LO 0x09
41#define MAX44000_REG_PST 0x0a
42#define MAX44000_REG_PRX_IND 0x0b
43#define MAX44000_REG_PRX_THR 0x0c
44#define MAX44000_REG_TRIM_GAIN_GREEN 0x0f
45#define MAX44000_REG_TRIM_GAIN_IR 0x10
46
47/* REG_CFG bits */
48#define MAX44000_CFG_ALSINTE 0x01
49#define MAX44000_CFG_PRXINTE 0x02
50#define MAX44000_CFG_MASK 0x1c
51#define MAX44000_CFG_MODE_SHUTDOWN 0x00
52#define MAX44000_CFG_MODE_ALS_GIR 0x04
53#define MAX44000_CFG_MODE_ALS_G 0x08
54#define MAX44000_CFG_MODE_ALS_IR 0x0c
55#define MAX44000_CFG_MODE_ALS_PRX 0x10
56#define MAX44000_CFG_MODE_PRX 0x14
57#define MAX44000_CFG_TRIM 0x20
58
59/*
60 * Upper 4 bits are not documented but start as 1 on powerup
61 * Setting them to 0 causes proximity to misbehave so set them to 1
62 */
63#define MAX44000_REG_CFG_RX_DEFAULT 0xf0
64
65/* REG_RX bits */
66#define MAX44000_CFG_RX_ALSTIM_MASK 0x0c
67#define MAX44000_CFG_RX_ALSTIM_SHIFT 2
68#define MAX44000_CFG_RX_ALSPGA_MASK 0x03
69#define MAX44000_CFG_RX_ALSPGA_SHIFT 0
70
71/* REG_TX bits */
72#define MAX44000_LED_CURRENT_MASK 0xf
73#define MAX44000_LED_CURRENT_MAX 11
74#define MAX44000_LED_CURRENT_DEFAULT 6
75
76#define MAX44000_ALSDATA_OVERFLOW 0x4000
77
78struct max44000_data {
79 struct mutex lock;
80 struct regmap *regmap;
81};
82
83/* Default scale is set to the minimum of 0.03125 or 1 / (1 << 5) lux */
84#define MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2 5
85
86/* Scale can be multiplied by up to 128x via ALSPGA for measurement gain */
87static const int max44000_alspga_shift[] = {0, 2, 4, 7};
88#define MAX44000_ALSPGA_MAX_SHIFT 7
89
90/*
91 * Scale can be multiplied by up to 64x via ALSTIM because of lost resolution
92 *
93 * This scaling factor is hidden from userspace and instead accounted for when
94 * reading raw values from the device.
95 *
96 * This makes it possible to cleanly expose ALSPGA as IIO_CHAN_INFO_SCALE and
97 * ALSTIM as IIO_CHAN_INFO_INT_TIME without the values affecting each other.
98 *
99 * Handling this internally is also required for buffer support because the
100 * channel's scan_type can't be modified dynamically.
101 */
102static const int max44000_alstim_shift[] = {0, 2, 4, 6};
103#define MAX44000_ALSTIM_SHIFT(alstim) (2 * (alstim))
104
105/* Available integration times with pretty manual alignment: */
106static const int max44000_int_time_avail_ns_array[] = {
107 100000000,
108 25000000,
109 6250000,
110 1562500,
111};
112static const char max44000_int_time_avail_str[] =
113 "0.100 "
114 "0.025 "
115 "0.00625 "
116 "0.001625";
117
118/* Available scales (internal to ulux) with pretty manual alignment: */
119static const int max44000_scale_avail_ulux_array[] = {
120 31250,
121 125000,
122 500000,
123 4000000,
124};
125static const char max44000_scale_avail_str[] =
126 "0.03125 "
127 "0.125 "
128 "0.5 "
129 "4";
130
131#define MAX44000_SCAN_INDEX_ALS 0
132#define MAX44000_SCAN_INDEX_PRX 1
133
134static const struct iio_chan_spec max44000_channels[] = {
135 {
136 .type = IIO_LIGHT,
137 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
138 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
139 BIT(IIO_CHAN_INFO_INT_TIME),
140 .scan_index = MAX44000_SCAN_INDEX_ALS,
141 .scan_type = {
142 .sign = 'u',
143 .realbits = 14,
144 .storagebits = 16,
145 }
146 },
147 {
148 .type = IIO_PROXIMITY,
149 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
150 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
151 .scan_index = MAX44000_SCAN_INDEX_PRX,
152 .scan_type = {
153 .sign = 'u',
154 .realbits = 8,
155 .storagebits = 16,
156 }
157 },
158 IIO_CHAN_SOFT_TIMESTAMP(2),
159 {
160 .type = IIO_CURRENT,
161 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
162 BIT(IIO_CHAN_INFO_SCALE),
163 .extend_name = "led",
164 .output = 1,
165 .scan_index = -1,
166 },
167};
168
169static int max44000_read_alstim(struct max44000_data *data)
170{
171 unsigned int val;
172 int ret;
173
174 ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val);
175 if (ret < 0)
176 return ret;
177 return (val & MAX44000_CFG_RX_ALSTIM_MASK) >> MAX44000_CFG_RX_ALSTIM_SHIFT;
178}
179
180static int max44000_write_alstim(struct max44000_data *data, int val)
181{
182 return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX,
183 MAX44000_CFG_RX_ALSTIM_MASK,
184 val << MAX44000_CFG_RX_ALSTIM_SHIFT);
185}
186
187static int max44000_read_alspga(struct max44000_data *data)
188{
189 unsigned int val;
190 int ret;
191
192 ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val);
193 if (ret < 0)
194 return ret;
195 return (val & MAX44000_CFG_RX_ALSPGA_MASK) >> MAX44000_CFG_RX_ALSPGA_SHIFT;
196}
197
198static int max44000_write_alspga(struct max44000_data *data, int val)
199{
200 return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX,
201 MAX44000_CFG_RX_ALSPGA_MASK,
202 val << MAX44000_CFG_RX_ALSPGA_SHIFT);
203}
204
205static int max44000_read_alsval(struct max44000_data *data)
206{
207 u16 regval;
208 int alstim, ret;
209
210 ret = regmap_bulk_read(data->regmap, MAX44000_REG_ALS_DATA_HI,
211 &regval, sizeof(regval));
212 if (ret < 0)
213 return ret;
214 alstim = ret = max44000_read_alstim(data);
215 if (ret < 0)
216 return ret;
217
218 regval = be16_to_cpu(regval);
219
220 /*
221 * Overflow is explained on datasheet page 17.
222 *
223 * It's a warning that either the G or IR channel has become saturated
224 * and that the value in the register is likely incorrect.
225 *
226 * The recommendation is to change the scale (ALSPGA).
227 * The driver just returns the max representable value.
228 */
229 if (regval & MAX44000_ALSDATA_OVERFLOW)
230 return 0x3FFF;
231
232 return regval << MAX44000_ALSTIM_SHIFT(alstim);
233}
234
235static int max44000_write_led_current_raw(struct max44000_data *data, int val)
236{
237 /* Maybe we should clamp the value instead? */
238 if (val < 0 || val > MAX44000_LED_CURRENT_MAX)
239 return -ERANGE;
240 if (val >= 8)
241 val += 4;
242 return regmap_write_bits(data->regmap, MAX44000_REG_CFG_TX,
243 MAX44000_LED_CURRENT_MASK, val);
244}
245
246static int max44000_read_led_current_raw(struct max44000_data *data)
247{
248 unsigned int regval;
249 int ret;
250
251 ret = regmap_read(data->regmap, MAX44000_REG_CFG_TX, &regval);
252 if (ret < 0)
253 return ret;
254 regval &= MAX44000_LED_CURRENT_MASK;
255 if (regval >= 8)
256 regval -= 4;
257 return regval;
258}
259
260static int max44000_read_raw(struct iio_dev *indio_dev,
261 struct iio_chan_spec const *chan,
262 int *val, int *val2, long mask)
263{
264 struct max44000_data *data = iio_priv(indio_dev);
265 int alstim, alspga;
266 unsigned int regval;
267 int ret;
268
269 switch (mask) {
270 case IIO_CHAN_INFO_RAW:
271 switch (chan->type) {
272 case IIO_LIGHT:
273 mutex_lock(&data->lock);
274 ret = max44000_read_alsval(data);
275 mutex_unlock(&data->lock);
276 if (ret < 0)
277 return ret;
278 *val = ret;
279 return IIO_VAL_INT;
280
281 case IIO_PROXIMITY:
282 mutex_lock(&data->lock);
283 ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, &regval);
284 mutex_unlock(&data->lock);
285 if (ret < 0)
286 return ret;
287 *val = regval;
288 return IIO_VAL_INT;
289
290 case IIO_CURRENT:
291 mutex_lock(&data->lock);
292 ret = max44000_read_led_current_raw(data);
293 mutex_unlock(&data->lock);
294 if (ret < 0)
295 return ret;
296 *val = ret;
297 return IIO_VAL_INT;
298
299 default:
300 return -EINVAL;
301 }
302
303 case IIO_CHAN_INFO_SCALE:
304 switch (chan->type) {
305 case IIO_CURRENT:
306 /* Output register is in 10s of miliamps */
307 *val = 10;
308 return IIO_VAL_INT;
309
310 case IIO_LIGHT:
311 mutex_lock(&data->lock);
312 alspga = ret = max44000_read_alspga(data);
313 mutex_unlock(&data->lock);
314 if (ret < 0)
315 return ret;
316
317 /* Avoid negative shifts */
318 *val = (1 << MAX44000_ALSPGA_MAX_SHIFT);
319 *val2 = MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2
320 + MAX44000_ALSPGA_MAX_SHIFT
321 - max44000_alspga_shift[alspga];
322 return IIO_VAL_FRACTIONAL_LOG2;
323
324 default:
325 return -EINVAL;
326 }
327
328 case IIO_CHAN_INFO_INT_TIME:
329 mutex_lock(&data->lock);
330 alstim = ret = max44000_read_alstim(data);
331 mutex_unlock(&data->lock);
332
333 if (ret < 0)
334 return ret;
335 *val = 0;
336 *val2 = max44000_int_time_avail_ns_array[alstim];
337 return IIO_VAL_INT_PLUS_NANO;
338
339 default:
340 return -EINVAL;
341 }
342}
343
344static int max44000_write_raw(struct iio_dev *indio_dev,
345 struct iio_chan_spec const *chan,
346 int val, int val2, long mask)
347{
348 struct max44000_data *data = iio_priv(indio_dev);
349 int ret;
350
351 if (mask == IIO_CHAN_INFO_RAW && chan->type == IIO_CURRENT) {
352 mutex_lock(&data->lock);
353 ret = max44000_write_led_current_raw(data, val);
354 mutex_unlock(&data->lock);
355 return ret;
356 } else if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) {
357 s64 valns = val * NSEC_PER_SEC + val2;
358 int alstim = find_closest_descending(valns,
359 max44000_int_time_avail_ns_array,
360 ARRAY_SIZE(max44000_int_time_avail_ns_array));
361 mutex_lock(&data->lock);
362 ret = max44000_write_alstim(data, alstim);
363 mutex_unlock(&data->lock);
364 return ret;
365 } else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT) {
366 s64 valus = val * USEC_PER_SEC + val2;
367 int alspga = find_closest(valus,
368 max44000_scale_avail_ulux_array,
369 ARRAY_SIZE(max44000_scale_avail_ulux_array));
370 mutex_lock(&data->lock);
371 ret = max44000_write_alspga(data, alspga);
372 mutex_unlock(&data->lock);
373 return ret;
374 }
375
376 return -EINVAL;
377}
378
379static int max44000_write_raw_get_fmt(struct iio_dev *indio_dev,
380 struct iio_chan_spec const *chan,
381 long mask)
382{
383 if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT)
384 return IIO_VAL_INT_PLUS_NANO;
385 else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT)
386 return IIO_VAL_INT_PLUS_MICRO;
387 else
388 return IIO_VAL_INT;
389}
390
391static IIO_CONST_ATTR(illuminance_integration_time_available, max44000_int_time_avail_str);
392static IIO_CONST_ATTR(illuminance_scale_available, max44000_scale_avail_str);
393
394static struct attribute *max44000_attributes[] = {
395 &iio_const_attr_illuminance_integration_time_available.dev_attr.attr,
396 &iio_const_attr_illuminance_scale_available.dev_attr.attr,
397 NULL
398};
399
400static const struct attribute_group max44000_attribute_group = {
401 .attrs = max44000_attributes,
402};
403
404static const struct iio_info max44000_info = {
405 .driver_module = THIS_MODULE,
406 .read_raw = max44000_read_raw,
407 .write_raw = max44000_write_raw,
408 .write_raw_get_fmt = max44000_write_raw_get_fmt,
409 .attrs = &max44000_attribute_group,
410};
411
412static bool max44000_readable_reg(struct device *dev, unsigned int reg)
413{
414 switch (reg) {
415 case MAX44000_REG_STATUS:
416 case MAX44000_REG_CFG_MAIN:
417 case MAX44000_REG_CFG_RX:
418 case MAX44000_REG_CFG_TX:
419 case MAX44000_REG_ALS_DATA_HI:
420 case MAX44000_REG_ALS_DATA_LO:
421 case MAX44000_REG_PRX_DATA:
422 case MAX44000_REG_ALS_UPTHR_HI:
423 case MAX44000_REG_ALS_UPTHR_LO:
424 case MAX44000_REG_ALS_LOTHR_HI:
425 case MAX44000_REG_ALS_LOTHR_LO:
426 case MAX44000_REG_PST:
427 case MAX44000_REG_PRX_IND:
428 case MAX44000_REG_PRX_THR:
429 case MAX44000_REG_TRIM_GAIN_GREEN:
430 case MAX44000_REG_TRIM_GAIN_IR:
431 return true;
432 default:
433 return false;
434 }
435}
436
437static bool max44000_writeable_reg(struct device *dev, unsigned int reg)
438{
439 switch (reg) {
440 case MAX44000_REG_CFG_MAIN:
441 case MAX44000_REG_CFG_RX:
442 case MAX44000_REG_CFG_TX:
443 case MAX44000_REG_ALS_UPTHR_HI:
444 case MAX44000_REG_ALS_UPTHR_LO:
445 case MAX44000_REG_ALS_LOTHR_HI:
446 case MAX44000_REG_ALS_LOTHR_LO:
447 case MAX44000_REG_PST:
448 case MAX44000_REG_PRX_IND:
449 case MAX44000_REG_PRX_THR:
450 case MAX44000_REG_TRIM_GAIN_GREEN:
451 case MAX44000_REG_TRIM_GAIN_IR:
452 return true;
453 default:
454 return false;
455 }
456}
457
458static bool max44000_volatile_reg(struct device *dev, unsigned int reg)
459{
460 switch (reg) {
461 case MAX44000_REG_STATUS:
462 case MAX44000_REG_ALS_DATA_HI:
463 case MAX44000_REG_ALS_DATA_LO:
464 case MAX44000_REG_PRX_DATA:
465 return true;
466 default:
467 return false;
468 }
469}
470
471static bool max44000_precious_reg(struct device *dev, unsigned int reg)
472{
473 return reg == MAX44000_REG_STATUS;
474}
475
476static const struct regmap_config max44000_regmap_config = {
477 .reg_bits = 8,
478 .val_bits = 8,
479
480 .max_register = MAX44000_REG_PRX_DATA,
481 .readable_reg = max44000_readable_reg,
482 .writeable_reg = max44000_writeable_reg,
483 .volatile_reg = max44000_volatile_reg,
484 .precious_reg = max44000_precious_reg,
485
486 .use_single_rw = 1,
487 .cache_type = REGCACHE_RBTREE,
488};
489
490static irqreturn_t max44000_trigger_handler(int irq, void *p)
491{
492 struct iio_poll_func *pf = p;
493 struct iio_dev *indio_dev = pf->indio_dev;
494 struct max44000_data *data = iio_priv(indio_dev);
495 u16 buf[8]; /* 2x u16 + padding + 8 bytes timestamp */
496 int index = 0;
497 unsigned int regval;
498 int ret;
499
500 mutex_lock(&data->lock);
501 if (test_bit(MAX44000_SCAN_INDEX_ALS, indio_dev->active_scan_mask)) {
502 ret = max44000_read_alsval(data);
503 if (ret < 0)
504 goto out_unlock;
505 buf[index++] = ret;
506 }
507 if (test_bit(MAX44000_SCAN_INDEX_PRX, indio_dev->active_scan_mask)) {
508 ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, &regval);
509 if (ret < 0)
510 goto out_unlock;
511 buf[index] = regval;
512 }
513 mutex_unlock(&data->lock);
514
515 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns());
516 iio_trigger_notify_done(indio_dev->trig);
517 return IRQ_HANDLED;
518
519out_unlock:
520 mutex_unlock(&data->lock);
521 iio_trigger_notify_done(indio_dev->trig);
522 return IRQ_HANDLED;
523}
524
525static int max44000_probe(struct i2c_client *client,
526 const struct i2c_device_id *id)
527{
528 struct max44000_data *data;
529 struct iio_dev *indio_dev;
530 int ret, reg;
531
532 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
533 if (!indio_dev)
534 return -ENOMEM;
535 data = iio_priv(indio_dev);
536 data->regmap = devm_regmap_init_i2c(client, &max44000_regmap_config);
537 if (IS_ERR(data->regmap)) {
538 dev_err(&client->dev, "regmap_init failed!\n");
539 return PTR_ERR(data->regmap);
540 }
541
542 i2c_set_clientdata(client, indio_dev);
543 mutex_init(&data->lock);
544 indio_dev->dev.parent = &client->dev;
545 indio_dev->info = &max44000_info;
546 indio_dev->name = MAX44000_DRV_NAME;
547 indio_dev->channels = max44000_channels;
548 indio_dev->num_channels = ARRAY_SIZE(max44000_channels);
549
550 /*
551 * The device doesn't have a reset function so we just clear some
552 * important bits at probe time to ensure sane operation.
553 *
554 * Since we don't support interrupts/events the threshold values are
555 * not important. We also don't touch trim values.
556 */
557
558 /* Reset ALS scaling bits */
559 ret = regmap_write(data->regmap, MAX44000_REG_CFG_RX,
560 MAX44000_REG_CFG_RX_DEFAULT);
561 if (ret < 0) {
562 dev_err(&client->dev, "failed to write default CFG_RX: %d\n",
563 ret);
564 return ret;
565 }
566
567 /*
568 * By default the LED pulse used for the proximity sensor is disabled.
569 * Set a middle value so that we get some sort of valid data by default.
570 */
571 ret = max44000_write_led_current_raw(data, MAX44000_LED_CURRENT_DEFAULT);
572 if (ret < 0) {
573 dev_err(&client->dev, "failed to write init config: %d\n", ret);
574 return ret;
575 }
576
577 /* Reset CFG bits to ALS_PRX mode which allows easy reading of both values. */
578 reg = MAX44000_CFG_TRIM | MAX44000_CFG_MODE_ALS_PRX;
579 ret = regmap_write(data->regmap, MAX44000_REG_CFG_MAIN, reg);
580 if (ret < 0) {
581 dev_err(&client->dev, "failed to write init config: %d\n", ret);
582 return ret;
583 }
584
585 /* Read status at least once to clear any stale interrupt bits. */
586 ret = regmap_read(data->regmap, MAX44000_REG_STATUS, &reg);
587 if (ret < 0) {
588 dev_err(&client->dev, "failed to read init status: %d\n", ret);
589 return ret;
590 }
591
592 ret = iio_triggered_buffer_setup(indio_dev, NULL, max44000_trigger_handler, NULL);
593 if (ret < 0) {
594 dev_err(&client->dev, "iio triggered buffer setup failed\n");
595 return ret;
596 }
597
598 return iio_device_register(indio_dev);
599}
600
601static int max44000_remove(struct i2c_client *client)
602{
603 struct iio_dev *indio_dev = i2c_get_clientdata(client);
604
605 iio_device_unregister(indio_dev);
606 iio_triggered_buffer_cleanup(indio_dev);
607
608 return 0;
609}
610
611static const struct i2c_device_id max44000_id[] = {
612 {"max44000", 0},
613 { }
614};
615MODULE_DEVICE_TABLE(i2c, max44000_id);
616
617#ifdef CONFIG_ACPI
618static const struct acpi_device_id max44000_acpi_match[] = {
619 {"MAX44000", 0},
620 { }
621};
622MODULE_DEVICE_TABLE(acpi, max44000_acpi_match);
623#endif
624
625static struct i2c_driver max44000_driver = {
626 .driver = {
627 .name = MAX44000_DRV_NAME,
628 .acpi_match_table = ACPI_PTR(max44000_acpi_match),
629 },
630 .probe = max44000_probe,
631 .remove = max44000_remove,
632 .id_table = max44000_id,
633};
634
635module_i2c_driver(max44000_driver);
636
637MODULE_AUTHOR("Crestez Dan Leonard <leonard.crestez@intel.com>");
638MODULE_DESCRIPTION("MAX44000 Ambient and Infrared Proximity Sensor");
639MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/light/stk3310.c b/drivers/iio/light/stk3310.c
index 42d334ba612e..9e847f8f4f0c 100644
--- a/drivers/iio/light/stk3310.c
+++ b/drivers/iio/light/stk3310.c
@@ -16,7 +16,6 @@
16#include <linux/kernel.h> 16#include <linux/kernel.h>
17#include <linux/module.h> 17#include <linux/module.h>
18#include <linux/regmap.h> 18#include <linux/regmap.h>
19#include <linux/gpio/consumer.h>
20#include <linux/iio/events.h> 19#include <linux/iio/events.h>
21#include <linux/iio/iio.h> 20#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h> 21#include <linux/iio/sysfs.h>
diff --git a/drivers/iio/light/tsl2563.c b/drivers/iio/light/tsl2563.c
index 12731d6b89ec..57b108c30e98 100644
--- a/drivers/iio/light/tsl2563.c
+++ b/drivers/iio/light/tsl2563.c
@@ -806,8 +806,7 @@ static int tsl2563_probe(struct i2c_client *client,
806 return 0; 806 return 0;
807 807
808fail: 808fail:
809 cancel_delayed_work(&chip->poweroff_work); 809 cancel_delayed_work_sync(&chip->poweroff_work);
810 flush_scheduled_work();
811 return err; 810 return err;
812} 811}
813 812
diff --git a/drivers/iio/light/veml6070.c b/drivers/iio/light/veml6070.c
new file mode 100644
index 000000000000..bc1c4cb782cd
--- /dev/null
+++ b/drivers/iio/light/veml6070.c
@@ -0,0 +1,218 @@
1/*
2 * veml6070.c - Support for Vishay VEML6070 UV A light sensor
3 *
4 * Copyright 2016 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * IIO driver for VEML6070 (7-bit I2C slave addresses 0x38 and 0x39)
11 *
12 * TODO: integration time, ACK signal
13 */
14
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/mutex.h>
18#include <linux/err.h>
19#include <linux/delay.h>
20
21#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
23
24#define VEML6070_DRV_NAME "veml6070"
25
26#define VEML6070_ADDR_CONFIG_DATA_MSB 0x38 /* read: MSB data, write: config */
27#define VEML6070_ADDR_DATA_LSB 0x39 /* LSB data */
28
29#define VEML6070_COMMAND_ACK BIT(5) /* raise interrupt when over threshold */
30#define VEML6070_COMMAND_IT GENMASK(3, 2) /* bit mask integration time */
31#define VEML6070_COMMAND_RSRVD BIT(1) /* reserved, set to 1 */
32#define VEML6070_COMMAND_SD BIT(0) /* shutdown mode when set */
33
34#define VEML6070_IT_10 0x04 /* integration time 1x */
35
36struct veml6070_data {
37 struct i2c_client *client1;
38 struct i2c_client *client2;
39 u8 config;
40 struct mutex lock;
41};
42
43static int veml6070_read(struct veml6070_data *data)
44{
45 int ret;
46 u8 msb, lsb;
47
48 mutex_lock(&data->lock);
49
50 /* disable shutdown */
51 ret = i2c_smbus_write_byte(data->client1,
52 data->config & ~VEML6070_COMMAND_SD);
53 if (ret < 0)
54 goto out;
55
56 msleep(125 + 10); /* measurement takes up to 125 ms for IT 1x */
57
58 ret = i2c_smbus_read_byte(data->client2); /* read MSB, address 0x39 */
59 if (ret < 0)
60 goto out;
61 msb = ret;
62
63 ret = i2c_smbus_read_byte(data->client1); /* read LSB, address 0x38 */
64 if (ret < 0)
65 goto out;
66 lsb = ret;
67
68 /* shutdown again */
69 ret = i2c_smbus_write_byte(data->client1, data->config);
70 if (ret < 0)
71 goto out;
72
73 ret = (msb << 8) | lsb;
74
75out:
76 mutex_unlock(&data->lock);
77 return ret;
78}
79
80static const struct iio_chan_spec veml6070_channels[] = {
81 {
82 .type = IIO_INTENSITY,
83 .modified = 1,
84 .channel2 = IIO_MOD_LIGHT_UV,
85 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
86 },
87 {
88 .type = IIO_UVINDEX,
89 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
90 }
91};
92
93static int veml6070_to_uv_index(unsigned val)
94{
95 /*
96 * conversion of raw UV intensity values to UV index depends on
97 * integration time (IT) and value of the resistor connected to
98 * the RSET pin (default: 270 KOhm)
99 */
100 unsigned uvi[11] = {
101 187, 373, 560, /* low */
102 746, 933, 1120, /* moderate */
103 1308, 1494, /* high */
104 1681, 1868, 2054}; /* very high */
105 int i;
106
107 for (i = 0; i < ARRAY_SIZE(uvi); i++)
108 if (val <= uvi[i])
109 return i;
110
111 return 11; /* extreme */
112}
113
114static int veml6070_read_raw(struct iio_dev *indio_dev,
115 struct iio_chan_spec const *chan,
116 int *val, int *val2, long mask)
117{
118 struct veml6070_data *data = iio_priv(indio_dev);
119 int ret;
120
121 switch (mask) {
122 case IIO_CHAN_INFO_RAW:
123 case IIO_CHAN_INFO_PROCESSED:
124 ret = veml6070_read(data);
125 if (ret < 0)
126 return ret;
127 if (mask == IIO_CHAN_INFO_PROCESSED)
128 *val = veml6070_to_uv_index(ret);
129 else
130 *val = ret;
131 return IIO_VAL_INT;
132 default:
133 return -EINVAL;
134 }
135}
136
137static const struct iio_info veml6070_info = {
138 .read_raw = veml6070_read_raw,
139 .driver_module = THIS_MODULE,
140};
141
142static int veml6070_probe(struct i2c_client *client,
143 const struct i2c_device_id *id)
144{
145 struct veml6070_data *data;
146 struct iio_dev *indio_dev;
147 int ret;
148
149 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
150 if (!indio_dev)
151 return -ENOMEM;
152
153 data = iio_priv(indio_dev);
154 i2c_set_clientdata(client, indio_dev);
155 data->client1 = client;
156 mutex_init(&data->lock);
157
158 indio_dev->dev.parent = &client->dev;
159 indio_dev->info = &veml6070_info;
160 indio_dev->channels = veml6070_channels;
161 indio_dev->num_channels = ARRAY_SIZE(veml6070_channels);
162 indio_dev->name = VEML6070_DRV_NAME;
163 indio_dev->modes = INDIO_DIRECT_MODE;
164
165 data->client2 = i2c_new_dummy(client->adapter, VEML6070_ADDR_DATA_LSB);
166 if (!data->client2) {
167 dev_err(&client->dev, "i2c device for second chip address failed\n");
168 return -ENODEV;
169 }
170
171 data->config = VEML6070_IT_10 | VEML6070_COMMAND_RSRVD |
172 VEML6070_COMMAND_SD;
173 ret = i2c_smbus_write_byte(data->client1, data->config);
174 if (ret < 0)
175 goto fail;
176
177 ret = iio_device_register(indio_dev);
178 if (ret < 0)
179 goto fail;
180
181 return ret;
182
183fail:
184 i2c_unregister_device(data->client2);
185 return ret;
186}
187
188static int veml6070_remove(struct i2c_client *client)
189{
190 struct iio_dev *indio_dev = i2c_get_clientdata(client);
191 struct veml6070_data *data = iio_priv(indio_dev);
192
193 iio_device_unregister(indio_dev);
194 i2c_unregister_device(data->client2);
195
196 return 0;
197}
198
199static const struct i2c_device_id veml6070_id[] = {
200 { "veml6070", 0 },
201 { }
202};
203MODULE_DEVICE_TABLE(i2c, veml6070_id);
204
205static struct i2c_driver veml6070_driver = {
206 .driver = {
207 .name = VEML6070_DRV_NAME,
208 },
209 .probe = veml6070_probe,
210 .remove = veml6070_remove,
211 .id_table = veml6070_id,
212};
213
214module_i2c_driver(veml6070_driver);
215
216MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
217MODULE_DESCRIPTION("Vishay VEML6070 UV A light sensor driver");
218MODULE_LICENSE("GPL");
diff --git a/drivers/iio/magnetometer/Kconfig b/drivers/iio/magnetometer/Kconfig
index 021dc5361f53..84e6559ccc65 100644
--- a/drivers/iio/magnetometer/Kconfig
+++ b/drivers/iio/magnetometer/Kconfig
@@ -9,6 +9,8 @@ config AK8975
9 tristate "Asahi Kasei AK 3-Axis Magnetometer" 9 tristate "Asahi Kasei AK 3-Axis Magnetometer"
10 depends on I2C 10 depends on I2C
11 depends on GPIOLIB || COMPILE_TEST 11 depends on GPIOLIB || COMPILE_TEST
12 select IIO_BUFFER
13 select IIO_TRIGGERED_BUFFER
12 help 14 help
13 Say yes here to build support for Asahi Kasei AK8975, AK8963, 15 Say yes here to build support for Asahi Kasei AK8975, AK8963,
14 AK09911 or AK09912 3-Axis Magnetometer. 16 AK09911 or AK09912 3-Axis Magnetometer.
@@ -25,22 +27,41 @@ config AK09911
25 Deprecated: AK09911 is now supported by AK8975 driver. 27 Deprecated: AK09911 is now supported by AK8975 driver.
26 28
27config BMC150_MAGN 29config BMC150_MAGN
28 tristate "Bosch BMC150 Magnetometer Driver" 30 tristate
29 depends on I2C
30 select REGMAP_I2C
31 select IIO_BUFFER 31 select IIO_BUFFER
32 select IIO_TRIGGERED_BUFFER 32 select IIO_TRIGGERED_BUFFER
33
34config BMC150_MAGN_I2C
35 tristate "Bosch BMC150 I2C Magnetometer Driver"
36 depends on I2C
37 select BMC150_MAGN
38 select REGMAP_I2C
33 help 39 help
34 Say yes here to build support for the BMC150 magnetometer. 40 Say yes here to build support for the BMC150 magnetometer with
41 I2C interface.
35 42
36 Currently this only supports the device via an i2c interface. 43 This is a combo module with both accelerometer and magnetometer.
44 This driver is only implementing magnetometer part, which has
45 its own address and register map.
46
47 To compile this driver as a module, choose M here: the module will be
48 called bmc150_magn_i2c.
49
50config BMC150_MAGN_SPI
51 tristate "Bosch BMC150 SPI Magnetometer Driver"
52 depends on SPI
53 select BMC150_MAGN
54 select REGMAP_SPI
55 help
56 Say yes here to build support for the BMC150 magnetometer with
57 SPI interface.
37 58
38 This is a combo module with both accelerometer and magnetometer. 59 This is a combo module with both accelerometer and magnetometer.
39 This driver is only implementing magnetometer part, which has 60 This driver is only implementing magnetometer part, which has
40 its own address and register map. 61 its own address and register map.
41 62
42 To compile this driver as a module, choose M here: the module will be 63 To compile this driver as a module, choose M here: the module will be
43 called bmc150_magn. 64 called bmc150_magn_spi.
44 65
45config MAG3110 66config MAG3110
46 tristate "Freescale MAG3110 3-Axis Magnetometer" 67 tristate "Freescale MAG3110 3-Axis Magnetometer"
diff --git a/drivers/iio/magnetometer/Makefile b/drivers/iio/magnetometer/Makefile
index dd03fe524481..92a745c9a6e8 100644
--- a/drivers/iio/magnetometer/Makefile
+++ b/drivers/iio/magnetometer/Makefile
@@ -5,6 +5,9 @@
5# When adding new entries keep the list in alphabetical order 5# When adding new entries keep the list in alphabetical order
6obj-$(CONFIG_AK8975) += ak8975.o 6obj-$(CONFIG_AK8975) += ak8975.o
7obj-$(CONFIG_BMC150_MAGN) += bmc150_magn.o 7obj-$(CONFIG_BMC150_MAGN) += bmc150_magn.o
8obj-$(CONFIG_BMC150_MAGN_I2C) += bmc150_magn_i2c.o
9obj-$(CONFIG_BMC150_MAGN_SPI) += bmc150_magn_spi.o
10
8obj-$(CONFIG_MAG3110) += mag3110.o 11obj-$(CONFIG_MAG3110) += mag3110.o
9obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o 12obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o
10obj-$(CONFIG_MMC35240) += mmc35240.o 13obj-$(CONFIG_MMC35240) += mmc35240.o
diff --git a/drivers/iio/magnetometer/ak8975.c b/drivers/iio/magnetometer/ak8975.c
index 0e931a9a1669..609a2c401b5d 100644
--- a/drivers/iio/magnetometer/ak8975.c
+++ b/drivers/iio/magnetometer/ak8975.c
@@ -32,9 +32,17 @@
32#include <linux/gpio.h> 32#include <linux/gpio.h>
33#include <linux/of_gpio.h> 33#include <linux/of_gpio.h>
34#include <linux/acpi.h> 34#include <linux/acpi.h>
35#include <linux/regulator/consumer.h>
35 36
36#include <linux/iio/iio.h> 37#include <linux/iio/iio.h>
37#include <linux/iio/sysfs.h> 38#include <linux/iio/sysfs.h>
39#include <linux/iio/buffer.h>
40#include <linux/iio/trigger.h>
41#include <linux/iio/trigger_consumer.h>
42#include <linux/iio/triggered_buffer.h>
43
44#include <linux/iio/magnetometer/ak8975.h>
45
38/* 46/*
39 * Register definitions, as well as various shifts and masks to get at the 47 * Register definitions, as well as various shifts and masks to get at the
40 * individual fields of the registers. 48 * individual fields of the registers.
@@ -361,7 +369,6 @@ static const struct ak_def ak_def_array[AK_MAX_TYPE] = {
361struct ak8975_data { 369struct ak8975_data {
362 struct i2c_client *client; 370 struct i2c_client *client;
363 const struct ak_def *def; 371 const struct ak_def *def;
364 struct attribute_group attrs;
365 struct mutex lock; 372 struct mutex lock;
366 u8 asa[3]; 373 u8 asa[3];
367 long raw_to_gauss[3]; 374 long raw_to_gauss[3];
@@ -370,8 +377,41 @@ struct ak8975_data {
370 wait_queue_head_t data_ready_queue; 377 wait_queue_head_t data_ready_queue;
371 unsigned long flags; 378 unsigned long flags;
372 u8 cntl_cache; 379 u8 cntl_cache;
380 struct iio_mount_matrix orientation;
381 struct regulator *vdd;
373}; 382};
374 383
384/* Enable attached power regulator if any. */
385static int ak8975_power_on(struct i2c_client *client)
386{
387 const struct iio_dev *indio_dev = i2c_get_clientdata(client);
388 struct ak8975_data *data = iio_priv(indio_dev);
389 int ret;
390
391 data->vdd = devm_regulator_get(&client->dev, "vdd");
392 if (IS_ERR_OR_NULL(data->vdd)) {
393 ret = PTR_ERR(data->vdd);
394 if (ret == -ENODEV)
395 ret = 0;
396 } else {
397 ret = regulator_enable(data->vdd);
398 }
399
400 if (ret)
401 dev_err(&client->dev, "failed to enable Vdd supply: %d\n", ret);
402 return ret;
403}
404
405/* Disable attached power regulator if any. */
406static void ak8975_power_off(const struct i2c_client *client)
407{
408 const struct iio_dev *indio_dev = i2c_get_clientdata(client);
409 const struct ak8975_data *data = iio_priv(indio_dev);
410
411 if (!IS_ERR_OR_NULL(data->vdd))
412 regulator_disable(data->vdd);
413}
414
375/* 415/*
376 * Return 0 if the i2c device is the one we expect. 416 * Return 0 if the i2c device is the one we expect.
377 * return a negative error number otherwise 417 * return a negative error number otherwise
@@ -601,22 +641,15 @@ static int wait_conversion_complete_interrupt(struct ak8975_data *data)
601 return ret > 0 ? 0 : -ETIME; 641 return ret > 0 ? 0 : -ETIME;
602} 642}
603 643
604/* 644static int ak8975_start_read_axis(struct ak8975_data *data,
605 * Emits the raw flux value for the x, y, or z axis. 645 const struct i2c_client *client)
606 */
607static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
608{ 646{
609 struct ak8975_data *data = iio_priv(indio_dev);
610 struct i2c_client *client = data->client;
611 int ret;
612
613 mutex_lock(&data->lock);
614
615 /* Set up the device for taking a sample. */ 647 /* Set up the device for taking a sample. */
616 ret = ak8975_set_mode(data, MODE_ONCE); 648 int ret = ak8975_set_mode(data, MODE_ONCE);
649
617 if (ret < 0) { 650 if (ret < 0) {
618 dev_err(&client->dev, "Error in setting operating mode\n"); 651 dev_err(&client->dev, "Error in setting operating mode\n");
619 goto exit; 652 return ret;
620 } 653 }
621 654
622 /* Wait for the conversion to complete. */ 655 /* Wait for the conversion to complete. */
@@ -627,7 +660,7 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
627 else 660 else
628 ret = wait_conversion_complete_polled(data); 661 ret = wait_conversion_complete_polled(data);
629 if (ret < 0) 662 if (ret < 0)
630 goto exit; 663 return ret;
631 664
632 /* This will be executed only for non-interrupt based waiting case */ 665 /* This will be executed only for non-interrupt based waiting case */
633 if (ret & data->def->ctrl_masks[ST1_DRDY]) { 666 if (ret & data->def->ctrl_masks[ST1_DRDY]) {
@@ -635,32 +668,45 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
635 data->def->ctrl_regs[ST2]); 668 data->def->ctrl_regs[ST2]);
636 if (ret < 0) { 669 if (ret < 0) {
637 dev_err(&client->dev, "Error in reading ST2\n"); 670 dev_err(&client->dev, "Error in reading ST2\n");
638 goto exit; 671 return ret;
639 } 672 }
640 if (ret & (data->def->ctrl_masks[ST2_DERR] | 673 if (ret & (data->def->ctrl_masks[ST2_DERR] |
641 data->def->ctrl_masks[ST2_HOFL])) { 674 data->def->ctrl_masks[ST2_HOFL])) {
642 dev_err(&client->dev, "ST2 status error 0x%x\n", ret); 675 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
643 ret = -EINVAL; 676 return -EINVAL;
644 goto exit;
645 } 677 }
646 } 678 }
647 679
648 /* Read the flux value from the appropriate register 680 return 0;
649 (the register is specified in the iio device attributes). */ 681}
650 ret = i2c_smbus_read_word_data(client, data->def->data_regs[index]); 682
651 if (ret < 0) { 683/* Retrieve raw flux value for one of the x, y, or z axis. */
652 dev_err(&client->dev, "Read axis data fails\n"); 684static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
685{
686 struct ak8975_data *data = iio_priv(indio_dev);
687 const struct i2c_client *client = data->client;
688 const struct ak_def *def = data->def;
689 int ret;
690
691 mutex_lock(&data->lock);
692
693 ret = ak8975_start_read_axis(data, client);
694 if (ret)
695 goto exit;
696
697 ret = i2c_smbus_read_word_data(client, def->data_regs[index]);
698 if (ret < 0)
653 goto exit; 699 goto exit;
654 }
655 700
656 mutex_unlock(&data->lock); 701 mutex_unlock(&data->lock);
657 702
658 /* Clamp to valid range. */ 703 /* Clamp to valid range. */
659 *val = clamp_t(s16, ret, -data->def->range, data->def->range); 704 *val = clamp_t(s16, ret, -def->range, def->range);
660 return IIO_VAL_INT; 705 return IIO_VAL_INT;
661 706
662exit: 707exit:
663 mutex_unlock(&data->lock); 708 mutex_unlock(&data->lock);
709 dev_err(&client->dev, "Error in reading axis\n");
664 return ret; 710 return ret;
665} 711}
666 712
@@ -682,6 +728,18 @@ static int ak8975_read_raw(struct iio_dev *indio_dev,
682 return -EINVAL; 728 return -EINVAL;
683} 729}
684 730
731static const struct iio_mount_matrix *
732ak8975_get_mount_matrix(const struct iio_dev *indio_dev,
733 const struct iio_chan_spec *chan)
734{
735 return &((struct ak8975_data *)iio_priv(indio_dev))->orientation;
736}
737
738static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
739 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix),
740 { },
741};
742
685#define AK8975_CHANNEL(axis, index) \ 743#define AK8975_CHANNEL(axis, index) \
686 { \ 744 { \
687 .type = IIO_MAGN, \ 745 .type = IIO_MAGN, \
@@ -690,12 +748,23 @@ static int ak8975_read_raw(struct iio_dev *indio_dev,
690 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 748 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
691 BIT(IIO_CHAN_INFO_SCALE), \ 749 BIT(IIO_CHAN_INFO_SCALE), \
692 .address = index, \ 750 .address = index, \
751 .scan_index = index, \
752 .scan_type = { \
753 .sign = 's', \
754 .realbits = 16, \
755 .storagebits = 16, \
756 .endianness = IIO_CPU \
757 }, \
758 .ext_info = ak8975_ext_info, \
693 } 759 }
694 760
695static const struct iio_chan_spec ak8975_channels[] = { 761static const struct iio_chan_spec ak8975_channels[] = {
696 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2), 762 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
763 IIO_CHAN_SOFT_TIMESTAMP(3),
697}; 764};
698 765
766static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
767
699static const struct iio_info ak8975_info = { 768static const struct iio_info ak8975_info = {
700 .read_raw = &ak8975_read_raw, 769 .read_raw = &ak8975_read_raw,
701 .driver_module = THIS_MODULE, 770 .driver_module = THIS_MODULE,
@@ -724,6 +793,56 @@ static const char *ak8975_match_acpi_device(struct device *dev,
724 return dev_name(dev); 793 return dev_name(dev);
725} 794}
726 795
796static void ak8975_fill_buffer(struct iio_dev *indio_dev)
797{
798 struct ak8975_data *data = iio_priv(indio_dev);
799 const struct i2c_client *client = data->client;
800 const struct ak_def *def = data->def;
801 int ret;
802 s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
803
804 mutex_lock(&data->lock);
805
806 ret = ak8975_start_read_axis(data, client);
807 if (ret)
808 goto unlock;
809
810 /*
811 * For each axis, read the flux value from the appropriate register
812 * (the register is specified in the iio device attributes).
813 */
814 ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
815 def->data_regs[0],
816 3 * sizeof(buff[0]),
817 (u8 *)buff);
818 if (ret < 0)
819 goto unlock;
820
821 mutex_unlock(&data->lock);
822
823 /* Clamp to valid range. */
824 buff[0] = clamp_t(s16, le16_to_cpu(buff[0]), -def->range, def->range);
825 buff[1] = clamp_t(s16, le16_to_cpu(buff[1]), -def->range, def->range);
826 buff[2] = clamp_t(s16, le16_to_cpu(buff[2]), -def->range, def->range);
827
828 iio_push_to_buffers_with_timestamp(indio_dev, buff, iio_get_time_ns());
829 return;
830
831unlock:
832 mutex_unlock(&data->lock);
833 dev_err(&client->dev, "Error in reading axes block\n");
834}
835
836static irqreturn_t ak8975_handle_trigger(int irq, void *p)
837{
838 const struct iio_poll_func *pf = p;
839 struct iio_dev *indio_dev = pf->indio_dev;
840
841 ak8975_fill_buffer(indio_dev);
842 iio_trigger_notify_done(indio_dev->trig);
843 return IRQ_HANDLED;
844}
845
727static int ak8975_probe(struct i2c_client *client, 846static int ak8975_probe(struct i2c_client *client,
728 const struct i2c_device_id *id) 847 const struct i2c_device_id *id)
729{ 848{
@@ -733,10 +852,12 @@ static int ak8975_probe(struct i2c_client *client,
733 int err; 852 int err;
734 const char *name = NULL; 853 const char *name = NULL;
735 enum asahi_compass_chipset chipset = AK_MAX_TYPE; 854 enum asahi_compass_chipset chipset = AK_MAX_TYPE;
855 const struct ak8975_platform_data *pdata =
856 dev_get_platdata(&client->dev);
736 857
737 /* Grab and set up the supplied GPIO. */ 858 /* Grab and set up the supplied GPIO. */
738 if (client->dev.platform_data) 859 if (pdata)
739 eoc_gpio = *(int *)(client->dev.platform_data); 860 eoc_gpio = pdata->eoc_gpio;
740 else if (client->dev.of_node) 861 else if (client->dev.of_node)
741 eoc_gpio = of_get_gpio(client->dev.of_node, 0); 862 eoc_gpio = of_get_gpio(client->dev.of_node, 0);
742 else 863 else
@@ -770,13 +891,24 @@ static int ak8975_probe(struct i2c_client *client,
770 data->eoc_gpio = eoc_gpio; 891 data->eoc_gpio = eoc_gpio;
771 data->eoc_irq = 0; 892 data->eoc_irq = 0;
772 893
894 if (!pdata) {
895 err = of_iio_read_mount_matrix(&client->dev,
896 "mount-matrix",
897 &data->orientation);
898 if (err)
899 return err;
900 } else
901 data->orientation = pdata->orientation;
902
773 /* id will be NULL when enumerated via ACPI */ 903 /* id will be NULL when enumerated via ACPI */
774 if (id) { 904 if (id) {
775 chipset = (enum asahi_compass_chipset)(id->driver_data); 905 chipset = (enum asahi_compass_chipset)(id->driver_data);
776 name = id->name; 906 name = id->name;
777 } else if (ACPI_HANDLE(&client->dev)) 907 } else if (ACPI_HANDLE(&client->dev)) {
778 name = ak8975_match_acpi_device(&client->dev, &chipset); 908 name = ak8975_match_acpi_device(&client->dev, &chipset);
779 else 909 if (!name)
910 return -ENODEV;
911 } else
780 return -ENOSYS; 912 return -ENOSYS;
781 913
782 if (chipset >= AK_MAX_TYPE) { 914 if (chipset >= AK_MAX_TYPE) {
@@ -786,10 +918,15 @@ static int ak8975_probe(struct i2c_client *client,
786 } 918 }
787 919
788 data->def = &ak_def_array[chipset]; 920 data->def = &ak_def_array[chipset];
921
922 err = ak8975_power_on(client);
923 if (err)
924 return err;
925
789 err = ak8975_who_i_am(client, data->def->type); 926 err = ak8975_who_i_am(client, data->def->type);
790 if (err < 0) { 927 if (err < 0) {
791 dev_err(&client->dev, "Unexpected device\n"); 928 dev_err(&client->dev, "Unexpected device\n");
792 return err; 929 goto power_off;
793 } 930 }
794 dev_dbg(&client->dev, "Asahi compass chip %s\n", name); 931 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
795 932
@@ -797,7 +934,7 @@ static int ak8975_probe(struct i2c_client *client,
797 err = ak8975_setup(client); 934 err = ak8975_setup(client);
798 if (err < 0) { 935 if (err < 0) {
799 dev_err(&client->dev, "%s initialization fails\n", name); 936 dev_err(&client->dev, "%s initialization fails\n", name);
800 return err; 937 goto power_off;
801 } 938 }
802 939
803 mutex_init(&data->lock); 940 mutex_init(&data->lock);
@@ -805,9 +942,41 @@ static int ak8975_probe(struct i2c_client *client,
805 indio_dev->channels = ak8975_channels; 942 indio_dev->channels = ak8975_channels;
806 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); 943 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
807 indio_dev->info = &ak8975_info; 944 indio_dev->info = &ak8975_info;
945 indio_dev->available_scan_masks = ak8975_scan_masks;
808 indio_dev->modes = INDIO_DIRECT_MODE; 946 indio_dev->modes = INDIO_DIRECT_MODE;
809 indio_dev->name = name; 947 indio_dev->name = name;
810 return devm_iio_device_register(&client->dev, indio_dev); 948
949 err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
950 NULL);
951 if (err) {
952 dev_err(&client->dev, "triggered buffer setup failed\n");
953 goto power_off;
954 }
955
956 err = iio_device_register(indio_dev);
957 if (err) {
958 dev_err(&client->dev, "device register failed\n");
959 goto cleanup_buffer;
960 }
961
962 return 0;
963
964cleanup_buffer:
965 iio_triggered_buffer_cleanup(indio_dev);
966power_off:
967 ak8975_power_off(client);
968 return err;
969}
970
971static int ak8975_remove(struct i2c_client *client)
972{
973 struct iio_dev *indio_dev = i2c_get_clientdata(client);
974
975 iio_device_unregister(indio_dev);
976 iio_triggered_buffer_cleanup(indio_dev);
977 ak8975_power_off(client);
978
979 return 0;
811} 980}
812 981
813static const struct i2c_device_id ak8975_id[] = { 982static const struct i2c_device_id ak8975_id[] = {
@@ -841,6 +1010,7 @@ static struct i2c_driver ak8975_driver = {
841 .acpi_match_table = ACPI_PTR(ak_acpi_match), 1010 .acpi_match_table = ACPI_PTR(ak_acpi_match),
842 }, 1011 },
843 .probe = ak8975_probe, 1012 .probe = ak8975_probe,
1013 .remove = ak8975_remove,
844 .id_table = ak8975_id, 1014 .id_table = ak8975_id,
845}; 1015};
846module_i2c_driver(ak8975_driver); 1016module_i2c_driver(ak8975_driver);
diff --git a/drivers/iio/magnetometer/bmc150_magn.c b/drivers/iio/magnetometer/bmc150_magn.c
index ffcb75ea64fb..d104fb8d9379 100644
--- a/drivers/iio/magnetometer/bmc150_magn.c
+++ b/drivers/iio/magnetometer/bmc150_magn.c
@@ -23,7 +23,6 @@
23#include <linux/delay.h> 23#include <linux/delay.h>
24#include <linux/slab.h> 24#include <linux/slab.h>
25#include <linux/acpi.h> 25#include <linux/acpi.h>
26#include <linux/gpio/consumer.h>
27#include <linux/pm.h> 26#include <linux/pm.h>
28#include <linux/pm_runtime.h> 27#include <linux/pm_runtime.h>
29#include <linux/iio/iio.h> 28#include <linux/iio/iio.h>
@@ -35,6 +34,8 @@
35#include <linux/iio/triggered_buffer.h> 34#include <linux/iio/triggered_buffer.h>
36#include <linux/regmap.h> 35#include <linux/regmap.h>
37 36
37#include "bmc150_magn.h"
38
38#define BMC150_MAGN_DRV_NAME "bmc150_magn" 39#define BMC150_MAGN_DRV_NAME "bmc150_magn"
39#define BMC150_MAGN_IRQ_NAME "bmc150_magn_event" 40#define BMC150_MAGN_IRQ_NAME "bmc150_magn_event"
40 41
@@ -135,7 +136,7 @@ struct bmc150_magn_trim_regs {
135} __packed; 136} __packed;
136 137
137struct bmc150_magn_data { 138struct bmc150_magn_data {
138 struct i2c_client *client; 139 struct device *dev;
139 /* 140 /*
140 * 1. Protect this structure. 141 * 1. Protect this structure.
141 * 2. Serialize sequences that power on/off the device and access HW. 142 * 2. Serialize sequences that power on/off the device and access HW.
@@ -147,6 +148,7 @@ struct bmc150_magn_data {
147 struct iio_trigger *dready_trig; 148 struct iio_trigger *dready_trig;
148 bool dready_trigger_on; 149 bool dready_trigger_on;
149 int max_odr; 150 int max_odr;
151 int irq;
150}; 152};
151 153
152static const struct { 154static const struct {
@@ -216,7 +218,7 @@ static bool bmc150_magn_is_volatile_reg(struct device *dev, unsigned int reg)
216 } 218 }
217} 219}
218 220
219static const struct regmap_config bmc150_magn_regmap_config = { 221const struct regmap_config bmc150_magn_regmap_config = {
220 .reg_bits = 8, 222 .reg_bits = 8,
221 .val_bits = 8, 223 .val_bits = 8,
222 224
@@ -226,6 +228,7 @@ static const struct regmap_config bmc150_magn_regmap_config = {
226 .writeable_reg = bmc150_magn_is_writeable_reg, 228 .writeable_reg = bmc150_magn_is_writeable_reg,
227 .volatile_reg = bmc150_magn_is_volatile_reg, 229 .volatile_reg = bmc150_magn_is_volatile_reg,
228}; 230};
231EXPORT_SYMBOL(bmc150_magn_regmap_config);
229 232
230static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data, 233static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data,
231 enum bmc150_magn_power_modes mode, 234 enum bmc150_magn_power_modes mode,
@@ -264,17 +267,17 @@ static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on)
264 int ret; 267 int ret;
265 268
266 if (on) { 269 if (on) {
267 ret = pm_runtime_get_sync(&data->client->dev); 270 ret = pm_runtime_get_sync(data->dev);
268 } else { 271 } else {
269 pm_runtime_mark_last_busy(&data->client->dev); 272 pm_runtime_mark_last_busy(data->dev);
270 ret = pm_runtime_put_autosuspend(&data->client->dev); 273 ret = pm_runtime_put_autosuspend(data->dev);
271 } 274 }
272 275
273 if (ret < 0) { 276 if (ret < 0) {
274 dev_err(&data->client->dev, 277 dev_err(data->dev,
275 "failed to change power state to %d\n", on); 278 "failed to change power state to %d\n", on);
276 if (on) 279 if (on)
277 pm_runtime_put_noidle(&data->client->dev); 280 pm_runtime_put_noidle(data->dev);
278 281
279 return ret; 282 return ret;
280 } 283 }
@@ -351,7 +354,7 @@ static int bmc150_magn_set_max_odr(struct bmc150_magn_data *data, int rep_xy,
351 /* the maximum selectable read-out frequency from datasheet */ 354 /* the maximum selectable read-out frequency from datasheet */
352 max_odr = 1000000 / (145 * rep_xy + 500 * rep_z + 980); 355 max_odr = 1000000 / (145 * rep_xy + 500 * rep_z + 980);
353 if (odr > max_odr) { 356 if (odr > max_odr) {
354 dev_err(&data->client->dev, 357 dev_err(data->dev,
355 "Can't set oversampling with sampling freq %d\n", 358 "Can't set oversampling with sampling freq %d\n",
356 odr); 359 odr);
357 return -EINVAL; 360 return -EINVAL;
@@ -685,27 +688,27 @@ static int bmc150_magn_init(struct bmc150_magn_data *data)
685 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, 688 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND,
686 false); 689 false);
687 if (ret < 0) { 690 if (ret < 0) {
688 dev_err(&data->client->dev, 691 dev_err(data->dev,
689 "Failed to bring up device from suspend mode\n"); 692 "Failed to bring up device from suspend mode\n");
690 return ret; 693 return ret;
691 } 694 }
692 695
693 ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id); 696 ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id);
694 if (ret < 0) { 697 if (ret < 0) {
695 dev_err(&data->client->dev, "Failed reading chip id\n"); 698 dev_err(data->dev, "Failed reading chip id\n");
696 goto err_poweroff; 699 goto err_poweroff;
697 } 700 }
698 if (chip_id != BMC150_MAGN_CHIP_ID_VAL) { 701 if (chip_id != BMC150_MAGN_CHIP_ID_VAL) {
699 dev_err(&data->client->dev, "Invalid chip id 0x%x\n", chip_id); 702 dev_err(data->dev, "Invalid chip id 0x%x\n", chip_id);
700 ret = -ENODEV; 703 ret = -ENODEV;
701 goto err_poweroff; 704 goto err_poweroff;
702 } 705 }
703 dev_dbg(&data->client->dev, "Chip id %x\n", chip_id); 706 dev_dbg(data->dev, "Chip id %x\n", chip_id);
704 707
705 preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET]; 708 preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET];
706 ret = bmc150_magn_set_odr(data, preset.odr); 709 ret = bmc150_magn_set_odr(data, preset.odr);
707 if (ret < 0) { 710 if (ret < 0) {
708 dev_err(&data->client->dev, "Failed to set ODR to %d\n", 711 dev_err(data->dev, "Failed to set ODR to %d\n",
709 preset.odr); 712 preset.odr);
710 goto err_poweroff; 713 goto err_poweroff;
711 } 714 }
@@ -713,7 +716,7 @@ static int bmc150_magn_init(struct bmc150_magn_data *data)
713 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY, 716 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY,
714 BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy)); 717 BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy));
715 if (ret < 0) { 718 if (ret < 0) {
716 dev_err(&data->client->dev, "Failed to set REP XY to %d\n", 719 dev_err(data->dev, "Failed to set REP XY to %d\n",
717 preset.rep_xy); 720 preset.rep_xy);
718 goto err_poweroff; 721 goto err_poweroff;
719 } 722 }
@@ -721,7 +724,7 @@ static int bmc150_magn_init(struct bmc150_magn_data *data)
721 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z, 724 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z,
722 BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z)); 725 BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z));
723 if (ret < 0) { 726 if (ret < 0) {
724 dev_err(&data->client->dev, "Failed to set REP Z to %d\n", 727 dev_err(data->dev, "Failed to set REP Z to %d\n",
725 preset.rep_z); 728 preset.rep_z);
726 goto err_poweroff; 729 goto err_poweroff;
727 } 730 }
@@ -734,7 +737,7 @@ static int bmc150_magn_init(struct bmc150_magn_data *data)
734 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, 737 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
735 true); 738 true);
736 if (ret < 0) { 739 if (ret < 0) {
737 dev_err(&data->client->dev, "Failed to power on device\n"); 740 dev_err(data->dev, "Failed to power on device\n");
738 goto err_poweroff; 741 goto err_poweroff;
739 } 742 }
740 743
@@ -843,41 +846,33 @@ static const char *bmc150_magn_match_acpi_device(struct device *dev)
843 return dev_name(dev); 846 return dev_name(dev);
844} 847}
845 848
846static int bmc150_magn_probe(struct i2c_client *client, 849int bmc150_magn_probe(struct device *dev, struct regmap *regmap,
847 const struct i2c_device_id *id) 850 int irq, const char *name)
848{ 851{
849 struct bmc150_magn_data *data; 852 struct bmc150_magn_data *data;
850 struct iio_dev *indio_dev; 853 struct iio_dev *indio_dev;
851 const char *name = NULL;
852 int ret; 854 int ret;
853 855
854 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 856 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
855 if (!indio_dev) 857 if (!indio_dev)
856 return -ENOMEM; 858 return -ENOMEM;
857 859
858 data = iio_priv(indio_dev); 860 data = iio_priv(indio_dev);
859 i2c_set_clientdata(client, indio_dev); 861 dev_set_drvdata(dev, indio_dev);
860 data->client = client; 862 data->regmap = regmap;
863 data->irq = irq;
864 data->dev = dev;
861 865
862 if (id) 866 if (!name && ACPI_HANDLE(dev))
863 name = id->name; 867 name = bmc150_magn_match_acpi_device(dev);
864 else if (ACPI_HANDLE(&client->dev))
865 name = bmc150_magn_match_acpi_device(&client->dev);
866 else
867 return -ENOSYS;
868 868
869 mutex_init(&data->mutex); 869 mutex_init(&data->mutex);
870 data->regmap = devm_regmap_init_i2c(client, &bmc150_magn_regmap_config);
871 if (IS_ERR(data->regmap)) {
872 dev_err(&client->dev, "Failed to allocate register map\n");
873 return PTR_ERR(data->regmap);
874 }
875 870
876 ret = bmc150_magn_init(data); 871 ret = bmc150_magn_init(data);
877 if (ret < 0) 872 if (ret < 0)
878 return ret; 873 return ret;
879 874
880 indio_dev->dev.parent = &client->dev; 875 indio_dev->dev.parent = dev;
881 indio_dev->channels = bmc150_magn_channels; 876 indio_dev->channels = bmc150_magn_channels;
882 indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels); 877 indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels);
883 indio_dev->available_scan_masks = bmc150_magn_scan_masks; 878 indio_dev->available_scan_masks = bmc150_magn_scan_masks;
@@ -885,35 +880,34 @@ static int bmc150_magn_probe(struct i2c_client *client,
885 indio_dev->modes = INDIO_DIRECT_MODE; 880 indio_dev->modes = INDIO_DIRECT_MODE;
886 indio_dev->info = &bmc150_magn_info; 881 indio_dev->info = &bmc150_magn_info;
887 882
888 if (client->irq > 0) { 883 if (irq > 0) {
889 data->dready_trig = devm_iio_trigger_alloc(&client->dev, 884 data->dready_trig = devm_iio_trigger_alloc(dev,
890 "%s-dev%d", 885 "%s-dev%d",
891 indio_dev->name, 886 indio_dev->name,
892 indio_dev->id); 887 indio_dev->id);
893 if (!data->dready_trig) { 888 if (!data->dready_trig) {
894 ret = -ENOMEM; 889 ret = -ENOMEM;
895 dev_err(&client->dev, "iio trigger alloc failed\n"); 890 dev_err(dev, "iio trigger alloc failed\n");
896 goto err_poweroff; 891 goto err_poweroff;
897 } 892 }
898 893
899 data->dready_trig->dev.parent = &client->dev; 894 data->dready_trig->dev.parent = dev;
900 data->dready_trig->ops = &bmc150_magn_trigger_ops; 895 data->dready_trig->ops = &bmc150_magn_trigger_ops;
901 iio_trigger_set_drvdata(data->dready_trig, indio_dev); 896 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
902 ret = iio_trigger_register(data->dready_trig); 897 ret = iio_trigger_register(data->dready_trig);
903 if (ret) { 898 if (ret) {
904 dev_err(&client->dev, "iio trigger register failed\n"); 899 dev_err(dev, "iio trigger register failed\n");
905 goto err_poweroff; 900 goto err_poweroff;
906 } 901 }
907 902
908 ret = request_threaded_irq(client->irq, 903 ret = request_threaded_irq(irq,
909 iio_trigger_generic_data_rdy_poll, 904 iio_trigger_generic_data_rdy_poll,
910 NULL, 905 NULL,
911 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 906 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
912 BMC150_MAGN_IRQ_NAME, 907 BMC150_MAGN_IRQ_NAME,
913 data->dready_trig); 908 data->dready_trig);
914 if (ret < 0) { 909 if (ret < 0) {
915 dev_err(&client->dev, "request irq %d failed\n", 910 dev_err(dev, "request irq %d failed\n", irq);
916 client->irq);
917 goto err_trigger_unregister; 911 goto err_trigger_unregister;
918 } 912 }
919 } 913 }
@@ -923,34 +917,33 @@ static int bmc150_magn_probe(struct i2c_client *client,
923 bmc150_magn_trigger_handler, 917 bmc150_magn_trigger_handler,
924 &bmc150_magn_buffer_setup_ops); 918 &bmc150_magn_buffer_setup_ops);
925 if (ret < 0) { 919 if (ret < 0) {
926 dev_err(&client->dev, 920 dev_err(dev, "iio triggered buffer setup failed\n");
927 "iio triggered buffer setup failed\n");
928 goto err_free_irq; 921 goto err_free_irq;
929 } 922 }
930 923
931 ret = pm_runtime_set_active(&client->dev); 924 ret = pm_runtime_set_active(dev);
932 if (ret) 925 if (ret)
933 goto err_buffer_cleanup; 926 goto err_buffer_cleanup;
934 927
935 pm_runtime_enable(&client->dev); 928 pm_runtime_enable(dev);
936 pm_runtime_set_autosuspend_delay(&client->dev, 929 pm_runtime_set_autosuspend_delay(dev,
937 BMC150_MAGN_AUTO_SUSPEND_DELAY_MS); 930 BMC150_MAGN_AUTO_SUSPEND_DELAY_MS);
938 pm_runtime_use_autosuspend(&client->dev); 931 pm_runtime_use_autosuspend(dev);
939 932
940 ret = iio_device_register(indio_dev); 933 ret = iio_device_register(indio_dev);
941 if (ret < 0) { 934 if (ret < 0) {
942 dev_err(&client->dev, "unable to register iio device\n"); 935 dev_err(dev, "unable to register iio device\n");
943 goto err_buffer_cleanup; 936 goto err_buffer_cleanup;
944 } 937 }
945 938
946 dev_dbg(&indio_dev->dev, "Registered device %s\n", name); 939 dev_dbg(dev, "Registered device %s\n", name);
947 return 0; 940 return 0;
948 941
949err_buffer_cleanup: 942err_buffer_cleanup:
950 iio_triggered_buffer_cleanup(indio_dev); 943 iio_triggered_buffer_cleanup(indio_dev);
951err_free_irq: 944err_free_irq:
952 if (client->irq > 0) 945 if (irq > 0)
953 free_irq(client->irq, data->dready_trig); 946 free_irq(irq, data->dready_trig);
954err_trigger_unregister: 947err_trigger_unregister:
955 if (data->dready_trig) 948 if (data->dready_trig)
956 iio_trigger_unregister(data->dready_trig); 949 iio_trigger_unregister(data->dready_trig);
@@ -958,22 +951,23 @@ err_poweroff:
958 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); 951 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true);
959 return ret; 952 return ret;
960} 953}
954EXPORT_SYMBOL(bmc150_magn_probe);
961 955
962static int bmc150_magn_remove(struct i2c_client *client) 956int bmc150_magn_remove(struct device *dev)
963{ 957{
964 struct iio_dev *indio_dev = i2c_get_clientdata(client); 958 struct iio_dev *indio_dev = dev_get_drvdata(dev);
965 struct bmc150_magn_data *data = iio_priv(indio_dev); 959 struct bmc150_magn_data *data = iio_priv(indio_dev);
966 960
967 iio_device_unregister(indio_dev); 961 iio_device_unregister(indio_dev);
968 962
969 pm_runtime_disable(&client->dev); 963 pm_runtime_disable(dev);
970 pm_runtime_set_suspended(&client->dev); 964 pm_runtime_set_suspended(dev);
971 pm_runtime_put_noidle(&client->dev); 965 pm_runtime_put_noidle(dev);
972 966
973 iio_triggered_buffer_cleanup(indio_dev); 967 iio_triggered_buffer_cleanup(indio_dev);
974 968
975 if (client->irq > 0) 969 if (data->irq > 0)
976 free_irq(data->client->irq, data->dready_trig); 970 free_irq(data->irq, data->dready_trig);
977 971
978 if (data->dready_trig) 972 if (data->dready_trig)
979 iio_trigger_unregister(data->dready_trig); 973 iio_trigger_unregister(data->dready_trig);
@@ -984,11 +978,12 @@ static int bmc150_magn_remove(struct i2c_client *client)
984 978
985 return 0; 979 return 0;
986} 980}
981EXPORT_SYMBOL(bmc150_magn_remove);
987 982
988#ifdef CONFIG_PM 983#ifdef CONFIG_PM
989static int bmc150_magn_runtime_suspend(struct device *dev) 984static int bmc150_magn_runtime_suspend(struct device *dev)
990{ 985{
991 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 986 struct iio_dev *indio_dev = dev_get_drvdata(dev);
992 struct bmc150_magn_data *data = iio_priv(indio_dev); 987 struct bmc150_magn_data *data = iio_priv(indio_dev);
993 int ret; 988 int ret;
994 989
@@ -997,7 +992,7 @@ static int bmc150_magn_runtime_suspend(struct device *dev)
997 true); 992 true);
998 mutex_unlock(&data->mutex); 993 mutex_unlock(&data->mutex);
999 if (ret < 0) { 994 if (ret < 0) {
1000 dev_err(&data->client->dev, "powering off device failed\n"); 995 dev_err(dev, "powering off device failed\n");
1001 return ret; 996 return ret;
1002 } 997 }
1003 return 0; 998 return 0;
@@ -1008,7 +1003,7 @@ static int bmc150_magn_runtime_suspend(struct device *dev)
1008 */ 1003 */
1009static int bmc150_magn_runtime_resume(struct device *dev) 1004static int bmc150_magn_runtime_resume(struct device *dev)
1010{ 1005{
1011 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1006 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1012 struct bmc150_magn_data *data = iio_priv(indio_dev); 1007 struct bmc150_magn_data *data = iio_priv(indio_dev);
1013 1008
1014 return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, 1009 return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL,
@@ -1019,7 +1014,7 @@ static int bmc150_magn_runtime_resume(struct device *dev)
1019#ifdef CONFIG_PM_SLEEP 1014#ifdef CONFIG_PM_SLEEP
1020static int bmc150_magn_suspend(struct device *dev) 1015static int bmc150_magn_suspend(struct device *dev)
1021{ 1016{
1022 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1017 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1023 struct bmc150_magn_data *data = iio_priv(indio_dev); 1018 struct bmc150_magn_data *data = iio_priv(indio_dev);
1024 int ret; 1019 int ret;
1025 1020
@@ -1033,7 +1028,7 @@ static int bmc150_magn_suspend(struct device *dev)
1033 1028
1034static int bmc150_magn_resume(struct device *dev) 1029static int bmc150_magn_resume(struct device *dev)
1035{ 1030{
1036 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1031 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1037 struct bmc150_magn_data *data = iio_priv(indio_dev); 1032 struct bmc150_magn_data *data = iio_priv(indio_dev);
1038 int ret; 1033 int ret;
1039 1034
@@ -1046,38 +1041,13 @@ static int bmc150_magn_resume(struct device *dev)
1046} 1041}
1047#endif 1042#endif
1048 1043
1049static const struct dev_pm_ops bmc150_magn_pm_ops = { 1044const struct dev_pm_ops bmc150_magn_pm_ops = {
1050 SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume) 1045 SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume)
1051 SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend, 1046 SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend,
1052 bmc150_magn_runtime_resume, NULL) 1047 bmc150_magn_runtime_resume, NULL)
1053}; 1048};
1054 1049EXPORT_SYMBOL(bmc150_magn_pm_ops);
1055static const struct acpi_device_id bmc150_magn_acpi_match[] = {
1056 {"BMC150B", 0},
1057 {"BMC156B", 0},
1058 {},
1059};
1060MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match);
1061
1062static const struct i2c_device_id bmc150_magn_id[] = {
1063 {"bmc150_magn", 0},
1064 {"bmc156_magn", 0},
1065 {},
1066};
1067MODULE_DEVICE_TABLE(i2c, bmc150_magn_id);
1068
1069static struct i2c_driver bmc150_magn_driver = {
1070 .driver = {
1071 .name = BMC150_MAGN_DRV_NAME,
1072 .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match),
1073 .pm = &bmc150_magn_pm_ops,
1074 },
1075 .probe = bmc150_magn_probe,
1076 .remove = bmc150_magn_remove,
1077 .id_table = bmc150_magn_id,
1078};
1079module_i2c_driver(bmc150_magn_driver);
1080 1050
1081MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>"); 1051MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1082MODULE_LICENSE("GPL v2"); 1052MODULE_LICENSE("GPL v2");
1083MODULE_DESCRIPTION("BMC150 magnetometer driver"); 1053MODULE_DESCRIPTION("BMC150 magnetometer core driver");
diff --git a/drivers/iio/magnetometer/bmc150_magn.h b/drivers/iio/magnetometer/bmc150_magn.h
new file mode 100644
index 000000000000..9a8e26812ca8
--- /dev/null
+++ b/drivers/iio/magnetometer/bmc150_magn.h
@@ -0,0 +1,11 @@
1#ifndef _BMC150_MAGN_H_
2#define _BMC150_MAGN_H_
3
4extern const struct regmap_config bmc150_magn_regmap_config;
5extern const struct dev_pm_ops bmc150_magn_pm_ops;
6
7int bmc150_magn_probe(struct device *dev, struct regmap *regmap, int irq,
8 const char *name);
9int bmc150_magn_remove(struct device *dev);
10
11#endif /* _BMC150_MAGN_H_ */
diff --git a/drivers/iio/magnetometer/bmc150_magn_i2c.c b/drivers/iio/magnetometer/bmc150_magn_i2c.c
new file mode 100644
index 000000000000..eddc7f0d0096
--- /dev/null
+++ b/drivers/iio/magnetometer/bmc150_magn_i2c.c
@@ -0,0 +1,77 @@
1/*
2 * 3-axis magnetometer driver supporting following I2C Bosch-Sensortec chips:
3 * - BMC150
4 * - BMC156
5 *
6 * Copyright (c) 2016, Intel Corporation.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 */
17#include <linux/device.h>
18#include <linux/mod_devicetable.h>
19#include <linux/i2c.h>
20#include <linux/module.h>
21#include <linux/acpi.h>
22#include <linux/regmap.h>
23
24#include "bmc150_magn.h"
25
26static int bmc150_magn_i2c_probe(struct i2c_client *client,
27 const struct i2c_device_id *id)
28{
29 struct regmap *regmap;
30 const char *name = NULL;
31
32 regmap = devm_regmap_init_i2c(client, &bmc150_magn_regmap_config);
33 if (IS_ERR(regmap)) {
34 dev_err(&client->dev, "Failed to initialize i2c regmap\n");
35 return PTR_ERR(regmap);
36 }
37
38 if (id)
39 name = id->name;
40
41 return bmc150_magn_probe(&client->dev, regmap, client->irq, name);
42}
43
44static int bmc150_magn_i2c_remove(struct i2c_client *client)
45{
46 return bmc150_magn_remove(&client->dev);
47}
48
49static const struct acpi_device_id bmc150_magn_acpi_match[] = {
50 {"BMC150B", 0},
51 {"BMC156B", 0},
52 {},
53};
54MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match);
55
56static const struct i2c_device_id bmc150_magn_i2c_id[] = {
57 {"bmc150_magn", 0},
58 {"bmc156_magn", 0},
59 {}
60};
61MODULE_DEVICE_TABLE(i2c, bmc150_magn_i2c_id);
62
63static struct i2c_driver bmc150_magn_driver = {
64 .driver = {
65 .name = "bmc150_magn_i2c",
66 .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match),
67 .pm = &bmc150_magn_pm_ops,
68 },
69 .probe = bmc150_magn_i2c_probe,
70 .remove = bmc150_magn_i2c_remove,
71 .id_table = bmc150_magn_i2c_id,
72};
73module_i2c_driver(bmc150_magn_driver);
74
75MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com");
76MODULE_LICENSE("GPL v2");
77MODULE_DESCRIPTION("BMC150 I2C magnetometer driver");
diff --git a/drivers/iio/magnetometer/bmc150_magn_spi.c b/drivers/iio/magnetometer/bmc150_magn_spi.c
new file mode 100644
index 000000000000..c4c738a07695
--- /dev/null
+++ b/drivers/iio/magnetometer/bmc150_magn_spi.c
@@ -0,0 +1,68 @@
1/*
2 * 3-axis magnetometer driver support following SPI Bosch-Sensortec chips:
3 * - BMC150
4 * - BMC156
5 *
6 * Copyright (c) 2016, Intel Corporation.
7 *
8 * This file is subject to the terms and conditions of version 2 of
9 * the GNU General Public License. See the file COPYING in the main
10 * directory of this archive for more details.
11 */
12#include <linux/module.h>
13#include <linux/mod_devicetable.h>
14#include <linux/spi/spi.h>
15#include <linux/acpi.h>
16#include <linux/regmap.h>
17
18#include "bmc150_magn.h"
19
20static int bmc150_magn_spi_probe(struct spi_device *spi)
21{
22 struct regmap *regmap;
23 const struct spi_device_id *id = spi_get_device_id(spi);
24
25 regmap = devm_regmap_init_spi(spi, &bmc150_magn_regmap_config);
26 if (IS_ERR(regmap)) {
27 dev_err(&spi->dev, "Failed to register spi regmap %d\n",
28 (int)PTR_ERR(regmap));
29 return PTR_ERR(regmap);
30 }
31 return bmc150_magn_probe(&spi->dev, regmap, spi->irq, id->name);
32}
33
34static int bmc150_magn_spi_remove(struct spi_device *spi)
35{
36 bmc150_magn_remove(&spi->dev);
37
38 return 0;
39}
40
41static const struct spi_device_id bmc150_magn_spi_id[] = {
42 {"bmc150_magn", 0},
43 {"bmc156_magn", 0},
44 {}
45};
46MODULE_DEVICE_TABLE(spi, bmc150_magn_spi_id);
47
48static const struct acpi_device_id bmc150_magn_acpi_match[] = {
49 {"BMC150B", 0},
50 {"BMC156B", 0},
51 {},
52};
53MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match);
54
55static struct spi_driver bmc150_magn_spi_driver = {
56 .probe = bmc150_magn_spi_probe,
57 .remove = bmc150_magn_spi_remove,
58 .id_table = bmc150_magn_spi_id,
59 .driver = {
60 .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match),
61 .name = "bmc150_magn_spi",
62 },
63};
64module_spi_driver(bmc150_magn_spi_driver);
65
66MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com");
67MODULE_DESCRIPTION("BMC150 magnetometer SPI driver");
68MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/magnetometer/st_magn_core.c b/drivers/iio/magnetometer/st_magn_core.c
index 501f858df413..62036d2a9956 100644
--- a/drivers/iio/magnetometer/st_magn_core.c
+++ b/drivers/iio/magnetometer/st_magn_core.c
@@ -484,6 +484,7 @@ static const struct st_sensor_settings st_magn_sensors_settings[] = {
484 .mask_int1 = ST_MAGN_3_DRDY_INT_MASK, 484 .mask_int1 = ST_MAGN_3_DRDY_INT_MASK,
485 .addr_ihl = ST_MAGN_3_IHL_IRQ_ADDR, 485 .addr_ihl = ST_MAGN_3_IHL_IRQ_ADDR,
486 .mask_ihl = ST_MAGN_3_IHL_IRQ_MASK, 486 .mask_ihl = ST_MAGN_3_IHL_IRQ_MASK,
487 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
487 }, 488 },
488 .multi_read_bit = ST_MAGN_3_MULTIREAD_BIT, 489 .multi_read_bit = ST_MAGN_3_MULTIREAD_BIT,
489 .bootime = 2, 490 .bootime = 2,
diff --git a/drivers/iio/potentiometer/Kconfig b/drivers/iio/potentiometer/Kconfig
index ffc735c168fb..6acb23810bb4 100644
--- a/drivers/iio/potentiometer/Kconfig
+++ b/drivers/iio/potentiometer/Kconfig
@@ -5,6 +5,34 @@
5 5
6menu "Digital potentiometers" 6menu "Digital potentiometers"
7 7
8config DS1803
9 tristate "Maxim Integrated DS1803 Digital Potentiometer driver"
10 depends on I2C
11 help
12 Say yes here to build support for the Maxim Integrated DS1803
13 digital potentiomenter chip.
14
15 To compile this driver as a module, choose M here: the
16 module will be called ds1803.
17
18config MCP4131
19 tristate "Microchip MCP413X/414X/415X/416X/423X/424X/425X/426X Digital Potentiometer driver"
20 depends on SPI
21 help
22 Say yes here to build support for the Microchip
23 MCP4131, MCP4132,
24 MCP4141, MCP4142,
25 MCP4151, MCP4152,
26 MCP4161, MCP4162,
27 MCP4231, MCP4232,
28 MCP4241, MCP4242,
29 MCP4251, MCP4252,
30 MCP4261, MCP4262,
31 digital potentiomenter chips.
32
33 To compile this driver as a module, choose M here: the
34 module will be called mcp4131.
35
8config MCP4531 36config MCP4531
9 tristate "Microchip MCP45xx/MCP46xx Digital Potentiometer driver" 37 tristate "Microchip MCP45xx/MCP46xx Digital Potentiometer driver"
10 depends on I2C 38 depends on I2C
diff --git a/drivers/iio/potentiometer/Makefile b/drivers/iio/potentiometer/Makefile
index b563b492b486..6007faa2fb02 100644
--- a/drivers/iio/potentiometer/Makefile
+++ b/drivers/iio/potentiometer/Makefile
@@ -3,5 +3,7 @@
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order 5# When adding new entries keep the list in alphabetical order
6obj-$(CONFIG_DS1803) += ds1803.o
7obj-$(CONFIG_MCP4131) += mcp4131.o
6obj-$(CONFIG_MCP4531) += mcp4531.o 8obj-$(CONFIG_MCP4531) += mcp4531.o
7obj-$(CONFIG_TPL0102) += tpl0102.o 9obj-$(CONFIG_TPL0102) += tpl0102.o
diff --git a/drivers/iio/potentiometer/ds1803.c b/drivers/iio/potentiometer/ds1803.c
new file mode 100644
index 000000000000..fb9e2a337dc2
--- /dev/null
+++ b/drivers/iio/potentiometer/ds1803.c
@@ -0,0 +1,173 @@
1/*
2 * Maxim Integrated DS1803 digital potentiometer driver
3 * Copyright (c) 2016 Slawomir Stepien
4 *
5 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS1803.pdf
6 *
7 * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address
8 * ds1803 2 256 10, 50, 100 0101xxx
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
13 */
14
15#include <linux/err.h>
16#include <linux/export.h>
17#include <linux/i2c.h>
18#include <linux/iio/iio.h>
19#include <linux/module.h>
20#include <linux/of.h>
21
22#define DS1803_MAX_POS 255
23#define DS1803_WRITE(chan) (0xa8 | ((chan) + 1))
24
25enum ds1803_type {
26 DS1803_010,
27 DS1803_050,
28 DS1803_100,
29};
30
31struct ds1803_cfg {
32 int kohms;
33};
34
35static const struct ds1803_cfg ds1803_cfg[] = {
36 [DS1803_010] = { .kohms = 10, },
37 [DS1803_050] = { .kohms = 50, },
38 [DS1803_100] = { .kohms = 100, },
39};
40
41struct ds1803_data {
42 struct i2c_client *client;
43 const struct ds1803_cfg *cfg;
44};
45
46#define DS1803_CHANNEL(ch) { \
47 .type = IIO_RESISTANCE, \
48 .indexed = 1, \
49 .output = 1, \
50 .channel = (ch), \
51 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
52 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
53}
54
55static const struct iio_chan_spec ds1803_channels[] = {
56 DS1803_CHANNEL(0),
57 DS1803_CHANNEL(1),
58};
59
60static int ds1803_read_raw(struct iio_dev *indio_dev,
61 struct iio_chan_spec const *chan,
62 int *val, int *val2, long mask)
63{
64 struct ds1803_data *data = iio_priv(indio_dev);
65 int pot = chan->channel;
66 int ret;
67 u8 result[indio_dev->num_channels];
68
69 switch (mask) {
70 case IIO_CHAN_INFO_RAW:
71 ret = i2c_master_recv(data->client, result,
72 indio_dev->num_channels);
73 if (ret < 0)
74 return ret;
75
76 *val = result[pot];
77 return IIO_VAL_INT;
78
79 case IIO_CHAN_INFO_SCALE:
80 *val = 1000 * data->cfg->kohms;
81 *val2 = DS1803_MAX_POS;
82 return IIO_VAL_FRACTIONAL;
83 }
84
85 return -EINVAL;
86}
87
88static int ds1803_write_raw(struct iio_dev *indio_dev,
89 struct iio_chan_spec const *chan,
90 int val, int val2, long mask)
91{
92 struct ds1803_data *data = iio_priv(indio_dev);
93 int pot = chan->channel;
94
95 if (val2 != 0)
96 return -EINVAL;
97
98 switch (mask) {
99 case IIO_CHAN_INFO_RAW:
100 if (val > DS1803_MAX_POS || val < 0)
101 return -EINVAL;
102 break;
103 default:
104 return -EINVAL;
105 }
106
107 return i2c_smbus_write_byte_data(data->client, DS1803_WRITE(pot), val);
108}
109
110static const struct iio_info ds1803_info = {
111 .read_raw = ds1803_read_raw,
112 .write_raw = ds1803_write_raw,
113 .driver_module = THIS_MODULE,
114};
115
116static int ds1803_probe(struct i2c_client *client,
117 const struct i2c_device_id *id)
118{
119 struct device *dev = &client->dev;
120 struct ds1803_data *data;
121 struct iio_dev *indio_dev;
122
123 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
124 if (!indio_dev)
125 return -ENOMEM;
126
127 i2c_set_clientdata(client, indio_dev);
128
129 data = iio_priv(indio_dev);
130 data->client = client;
131 data->cfg = &ds1803_cfg[id->driver_data];
132
133 indio_dev->dev.parent = dev;
134 indio_dev->info = &ds1803_info;
135 indio_dev->channels = ds1803_channels;
136 indio_dev->num_channels = ARRAY_SIZE(ds1803_channels);
137 indio_dev->name = client->name;
138
139 return devm_iio_device_register(dev, indio_dev);
140}
141
142#if defined(CONFIG_OF)
143static const struct of_device_id ds1803_dt_ids[] = {
144 { .compatible = "maxim,ds1803-010", .data = &ds1803_cfg[DS1803_010] },
145 { .compatible = "maxim,ds1803-050", .data = &ds1803_cfg[DS1803_050] },
146 { .compatible = "maxim,ds1803-100", .data = &ds1803_cfg[DS1803_100] },
147 {}
148};
149MODULE_DEVICE_TABLE(of, ds1803_dt_ids);
150#endif /* CONFIG_OF */
151
152static const struct i2c_device_id ds1803_id[] = {
153 { "ds1803-010", DS1803_010 },
154 { "ds1803-050", DS1803_050 },
155 { "ds1803-100", DS1803_100 },
156 {}
157};
158MODULE_DEVICE_TABLE(i2c, ds1803_id);
159
160static struct i2c_driver ds1803_driver = {
161 .driver = {
162 .name = "ds1803",
163 .of_match_table = of_match_ptr(ds1803_dt_ids),
164 },
165 .probe = ds1803_probe,
166 .id_table = ds1803_id,
167};
168
169module_i2c_driver(ds1803_driver);
170
171MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>");
172MODULE_DESCRIPTION("DS1803 digital potentiometer");
173MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/potentiometer/mcp4131.c b/drivers/iio/potentiometer/mcp4131.c
new file mode 100644
index 000000000000..4e7e2c6c522c
--- /dev/null
+++ b/drivers/iio/potentiometer/mcp4131.c
@@ -0,0 +1,494 @@
1/*
2 * Industrial I/O driver for Microchip digital potentiometers
3 *
4 * Copyright (c) 2016 Slawomir Stepien
5 * Based on: Peter Rosin's code from mcp4531.c
6 *
7 * Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/22060b.pdf
8 *
9 * DEVID #Wipers #Positions Resistor Opts (kOhm)
10 * mcp4131 1 129 5, 10, 50, 100
11 * mcp4132 1 129 5, 10, 50, 100
12 * mcp4141 1 129 5, 10, 50, 100
13 * mcp4142 1 129 5, 10, 50, 100
14 * mcp4151 1 257 5, 10, 50, 100
15 * mcp4152 1 257 5, 10, 50, 100
16 * mcp4161 1 257 5, 10, 50, 100
17 * mcp4162 1 257 5, 10, 50, 100
18 * mcp4231 2 129 5, 10, 50, 100
19 * mcp4232 2 129 5, 10, 50, 100
20 * mcp4241 2 129 5, 10, 50, 100
21 * mcp4242 2 129 5, 10, 50, 100
22 * mcp4251 2 257 5, 10, 50, 100
23 * mcp4252 2 257 5, 10, 50, 100
24 * mcp4261 2 257 5, 10, 50, 100
25 * mcp4262 2 257 5, 10, 50, 100
26 *
27 * This program is free software; you can redistribute it and/or modify it
28 * under the terms of the GNU General Public License version 2 as published by
29 * the Free Software Foundation.
30 */
31
32/*
33 * TODO:
34 * 1. Write wiper setting to EEPROM for EEPROM capable models.
35 */
36
37#include <linux/cache.h>
38#include <linux/err.h>
39#include <linux/export.h>
40#include <linux/iio/iio.h>
41#include <linux/iio/types.h>
42#include <linux/module.h>
43#include <linux/mutex.h>
44#include <linux/of.h>
45#include <linux/spi/spi.h>
46
47#define MCP4131_WRITE (0x00 << 2)
48#define MCP4131_READ (0x03 << 2)
49
50#define MCP4131_WIPER_SHIFT 4
51#define MCP4131_CMDERR(r) ((r[0]) & 0x02)
52#define MCP4131_RAW(r) ((r[0]) == 0xff ? 0x100 : (r[1]))
53
54struct mcp4131_cfg {
55 int wipers;
56 int max_pos;
57 int kohms;
58};
59
60enum mcp4131_type {
61 MCP413x_502 = 0,
62 MCP413x_103,
63 MCP413x_503,
64 MCP413x_104,
65 MCP414x_502,
66 MCP414x_103,
67 MCP414x_503,
68 MCP414x_104,
69 MCP415x_502,
70 MCP415x_103,
71 MCP415x_503,
72 MCP415x_104,
73 MCP416x_502,
74 MCP416x_103,
75 MCP416x_503,
76 MCP416x_104,
77 MCP423x_502,
78 MCP423x_103,
79 MCP423x_503,
80 MCP423x_104,
81 MCP424x_502,
82 MCP424x_103,
83 MCP424x_503,
84 MCP424x_104,
85 MCP425x_502,
86 MCP425x_103,
87 MCP425x_503,
88 MCP425x_104,
89 MCP426x_502,
90 MCP426x_103,
91 MCP426x_503,
92 MCP426x_104,
93};
94
95static const struct mcp4131_cfg mcp4131_cfg[] = {
96 [MCP413x_502] = { .wipers = 1, .max_pos = 128, .kohms = 5, },
97 [MCP413x_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, },
98 [MCP413x_503] = { .wipers = 1, .max_pos = 128, .kohms = 50, },
99 [MCP413x_104] = { .wipers = 1, .max_pos = 128, .kohms = 100, },
100 [MCP414x_502] = { .wipers = 1, .max_pos = 128, .kohms = 5, },
101 [MCP414x_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, },
102 [MCP414x_503] = { .wipers = 1, .max_pos = 128, .kohms = 50, },
103 [MCP414x_104] = { .wipers = 1, .max_pos = 128, .kohms = 100, },
104 [MCP415x_502] = { .wipers = 1, .max_pos = 256, .kohms = 5, },
105 [MCP415x_103] = { .wipers = 1, .max_pos = 256, .kohms = 10, },
106 [MCP415x_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, },
107 [MCP415x_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, },
108 [MCP416x_502] = { .wipers = 1, .max_pos = 256, .kohms = 5, },
109 [MCP416x_103] = { .wipers = 1, .max_pos = 256, .kohms = 10, },
110 [MCP416x_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, },
111 [MCP416x_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, },
112 [MCP423x_502] = { .wipers = 2, .max_pos = 128, .kohms = 5, },
113 [MCP423x_103] = { .wipers = 2, .max_pos = 128, .kohms = 10, },
114 [MCP423x_503] = { .wipers = 2, .max_pos = 128, .kohms = 50, },
115 [MCP423x_104] = { .wipers = 2, .max_pos = 128, .kohms = 100, },
116 [MCP424x_502] = { .wipers = 2, .max_pos = 128, .kohms = 5, },
117 [MCP424x_103] = { .wipers = 2, .max_pos = 128, .kohms = 10, },
118 [MCP424x_503] = { .wipers = 2, .max_pos = 128, .kohms = 50, },
119 [MCP424x_104] = { .wipers = 2, .max_pos = 128, .kohms = 100, },
120 [MCP425x_502] = { .wipers = 2, .max_pos = 256, .kohms = 5, },
121 [MCP425x_103] = { .wipers = 2, .max_pos = 256, .kohms = 10, },
122 [MCP425x_503] = { .wipers = 2, .max_pos = 256, .kohms = 50, },
123 [MCP425x_104] = { .wipers = 2, .max_pos = 256, .kohms = 100, },
124 [MCP426x_502] = { .wipers = 2, .max_pos = 256, .kohms = 5, },
125 [MCP426x_103] = { .wipers = 2, .max_pos = 256, .kohms = 10, },
126 [MCP426x_503] = { .wipers = 2, .max_pos = 256, .kohms = 50, },
127 [MCP426x_104] = { .wipers = 2, .max_pos = 256, .kohms = 100, },
128};
129
130struct mcp4131_data {
131 struct spi_device *spi;
132 const struct mcp4131_cfg *cfg;
133 struct mutex lock;
134 u8 buf[2] ____cacheline_aligned;
135};
136
137#define MCP4131_CHANNEL(ch) { \
138 .type = IIO_RESISTANCE, \
139 .indexed = 1, \
140 .output = 1, \
141 .channel = (ch), \
142 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
143 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
144}
145
146static const struct iio_chan_spec mcp4131_channels[] = {
147 MCP4131_CHANNEL(0),
148 MCP4131_CHANNEL(1),
149};
150
151static int mcp4131_read(struct spi_device *spi, void *buf, size_t len)
152{
153 struct spi_transfer t = {
154 .tx_buf = buf, /* We need to send addr, cmd and 12 bits */
155 .rx_buf = buf,
156 .len = len,
157 };
158 struct spi_message m;
159
160 spi_message_init(&m);
161 spi_message_add_tail(&t, &m);
162
163 return spi_sync(spi, &m);
164}
165
166static int mcp4131_read_raw(struct iio_dev *indio_dev,
167 struct iio_chan_spec const *chan,
168 int *val, int *val2, long mask)
169{
170 int err;
171 struct mcp4131_data *data = iio_priv(indio_dev);
172 int address = chan->channel;
173
174 switch (mask) {
175 case IIO_CHAN_INFO_RAW:
176 mutex_lock(&data->lock);
177
178 data->buf[0] = (address << MCP4131_WIPER_SHIFT) | MCP4131_READ;
179 data->buf[1] = 0;
180
181 err = mcp4131_read(data->spi, data->buf, 2);
182 if (err) {
183 mutex_unlock(&data->lock);
184 return err;
185 }
186
187 /* Error, bad address/command combination */
188 if (!MCP4131_CMDERR(data->buf)) {
189 mutex_unlock(&data->lock);
190 return -EIO;
191 }
192
193 *val = MCP4131_RAW(data->buf);
194 mutex_unlock(&data->lock);
195
196 return IIO_VAL_INT;
197
198 case IIO_CHAN_INFO_SCALE:
199 *val = 1000 * data->cfg->kohms;
200 *val2 = data->cfg->max_pos;
201 return IIO_VAL_FRACTIONAL;
202 }
203
204 return -EINVAL;
205}
206
207static int mcp4131_write_raw(struct iio_dev *indio_dev,
208 struct iio_chan_spec const *chan,
209 int val, int val2, long mask)
210{
211 int err;
212 struct mcp4131_data *data = iio_priv(indio_dev);
213 int address = chan->channel << MCP4131_WIPER_SHIFT;
214
215 switch (mask) {
216 case IIO_CHAN_INFO_RAW:
217 if (val > data->cfg->max_pos || val < 0)
218 return -EINVAL;
219 break;
220
221 default:
222 return -EINVAL;
223 }
224
225 mutex_lock(&data->lock);
226
227 data->buf[0] = address << MCP4131_WIPER_SHIFT;
228 data->buf[0] |= MCP4131_WRITE | (val >> 8);
229 data->buf[1] = val & 0xFF; /* 8 bits here */
230
231 err = spi_write(data->spi, data->buf, 2);
232 mutex_unlock(&data->lock);
233
234 return err;
235}
236
237static const struct iio_info mcp4131_info = {
238 .read_raw = mcp4131_read_raw,
239 .write_raw = mcp4131_write_raw,
240 .driver_module = THIS_MODULE,
241};
242
243static int mcp4131_probe(struct spi_device *spi)
244{
245 int err;
246 struct device *dev = &spi->dev;
247 unsigned long devid = spi_get_device_id(spi)->driver_data;
248 struct mcp4131_data *data;
249 struct iio_dev *indio_dev;
250
251 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
252 if (!indio_dev)
253 return -ENOMEM;
254
255 data = iio_priv(indio_dev);
256 spi_set_drvdata(spi, indio_dev);
257 data->spi = spi;
258 data->cfg = &mcp4131_cfg[devid];
259
260 mutex_init(&data->lock);
261
262 indio_dev->dev.parent = dev;
263 indio_dev->info = &mcp4131_info;
264 indio_dev->channels = mcp4131_channels;
265 indio_dev->num_channels = data->cfg->wipers;
266 indio_dev->name = spi_get_device_id(spi)->name;
267
268 err = devm_iio_device_register(dev, indio_dev);
269 if (err) {
270 dev_info(&spi->dev, "Unable to register %s\n", indio_dev->name);
271 return err;
272 }
273
274 return 0;
275}
276
277#if defined(CONFIG_OF)
278static const struct of_device_id mcp4131_dt_ids[] = {
279 { .compatible = "microchip,mcp4131-502",
280 .data = &mcp4131_cfg[MCP413x_502] },
281 { .compatible = "microchip,mcp4131-103",
282 .data = &mcp4131_cfg[MCP413x_103] },
283 { .compatible = "microchip,mcp4131-503",
284 .data = &mcp4131_cfg[MCP413x_503] },
285 { .compatible = "microchip,mcp4131-104",
286 .data = &mcp4131_cfg[MCP413x_104] },
287 { .compatible = "microchip,mcp4132-502",
288 .data = &mcp4131_cfg[MCP413x_502] },
289 { .compatible = "microchip,mcp4132-103",
290 .data = &mcp4131_cfg[MCP413x_103] },
291 { .compatible = "microchip,mcp4132-503",
292 .data = &mcp4131_cfg[MCP413x_503] },
293 { .compatible = "microchip,mcp4132-104",
294 .data = &mcp4131_cfg[MCP413x_104] },
295 { .compatible = "microchip,mcp4141-502",
296 .data = &mcp4131_cfg[MCP414x_502] },
297 { .compatible = "microchip,mcp4141-103",
298 .data = &mcp4131_cfg[MCP414x_103] },
299 { .compatible = "microchip,mcp4141-503",
300 .data = &mcp4131_cfg[MCP414x_503] },
301 { .compatible = "microchip,mcp4141-104",
302 .data = &mcp4131_cfg[MCP414x_104] },
303 { .compatible = "microchip,mcp4142-502",
304 .data = &mcp4131_cfg[MCP414x_502] },
305 { .compatible = "microchip,mcp4142-103",
306 .data = &mcp4131_cfg[MCP414x_103] },
307 { .compatible = "microchip,mcp4142-503",
308 .data = &mcp4131_cfg[MCP414x_503] },
309 { .compatible = "microchip,mcp4142-104",
310 .data = &mcp4131_cfg[MCP414x_104] },
311 { .compatible = "microchip,mcp4151-502",
312 .data = &mcp4131_cfg[MCP415x_502] },
313 { .compatible = "microchip,mcp4151-103",
314 .data = &mcp4131_cfg[MCP415x_103] },
315 { .compatible = "microchip,mcp4151-503",
316 .data = &mcp4131_cfg[MCP415x_503] },
317 { .compatible = "microchip,mcp4151-104",
318 .data = &mcp4131_cfg[MCP415x_104] },
319 { .compatible = "microchip,mcp4152-502",
320 .data = &mcp4131_cfg[MCP415x_502] },
321 { .compatible = "microchip,mcp4152-103",
322 .data = &mcp4131_cfg[MCP415x_103] },
323 { .compatible = "microchip,mcp4152-503",
324 .data = &mcp4131_cfg[MCP415x_503] },
325 { .compatible = "microchip,mcp4152-104",
326 .data = &mcp4131_cfg[MCP415x_104] },
327 { .compatible = "microchip,mcp4161-502",
328 .data = &mcp4131_cfg[MCP416x_502] },
329 { .compatible = "microchip,mcp4161-103",
330 .data = &mcp4131_cfg[MCP416x_103] },
331 { .compatible = "microchip,mcp4161-503",
332 .data = &mcp4131_cfg[MCP416x_503] },
333 { .compatible = "microchip,mcp4161-104",
334 .data = &mcp4131_cfg[MCP416x_104] },
335 { .compatible = "microchip,mcp4162-502",
336 .data = &mcp4131_cfg[MCP416x_502] },
337 { .compatible = "microchip,mcp4162-103",
338 .data = &mcp4131_cfg[MCP416x_103] },
339 { .compatible = "microchip,mcp4162-503",
340 .data = &mcp4131_cfg[MCP416x_503] },
341 { .compatible = "microchip,mcp4162-104",
342 .data = &mcp4131_cfg[MCP416x_104] },
343 { .compatible = "microchip,mcp4231-502",
344 .data = &mcp4131_cfg[MCP423x_502] },
345 { .compatible = "microchip,mcp4231-103",
346 .data = &mcp4131_cfg[MCP423x_103] },
347 { .compatible = "microchip,mcp4231-503",
348 .data = &mcp4131_cfg[MCP423x_503] },
349 { .compatible = "microchip,mcp4231-104",
350 .data = &mcp4131_cfg[MCP423x_104] },
351 { .compatible = "microchip,mcp4232-502",
352 .data = &mcp4131_cfg[MCP423x_502] },
353 { .compatible = "microchip,mcp4232-103",
354 .data = &mcp4131_cfg[MCP423x_103] },
355 { .compatible = "microchip,mcp4232-503",
356 .data = &mcp4131_cfg[MCP423x_503] },
357 { .compatible = "microchip,mcp4232-104",
358 .data = &mcp4131_cfg[MCP423x_104] },
359 { .compatible = "microchip,mcp4241-502",
360 .data = &mcp4131_cfg[MCP424x_502] },
361 { .compatible = "microchip,mcp4241-103",
362 .data = &mcp4131_cfg[MCP424x_103] },
363 { .compatible = "microchip,mcp4241-503",
364 .data = &mcp4131_cfg[MCP424x_503] },
365 { .compatible = "microchip,mcp4241-104",
366 .data = &mcp4131_cfg[MCP424x_104] },
367 { .compatible = "microchip,mcp4242-502",
368 .data = &mcp4131_cfg[MCP424x_502] },
369 { .compatible = "microchip,mcp4242-103",
370 .data = &mcp4131_cfg[MCP424x_103] },
371 { .compatible = "microchip,mcp4242-503",
372 .data = &mcp4131_cfg[MCP424x_503] },
373 { .compatible = "microchip,mcp4242-104",
374 .data = &mcp4131_cfg[MCP424x_104] },
375 { .compatible = "microchip,mcp4251-502",
376 .data = &mcp4131_cfg[MCP425x_502] },
377 { .compatible = "microchip,mcp4251-103",
378 .data = &mcp4131_cfg[MCP425x_103] },
379 { .compatible = "microchip,mcp4251-503",
380 .data = &mcp4131_cfg[MCP425x_503] },
381 { .compatible = "microchip,mcp4251-104",
382 .data = &mcp4131_cfg[MCP425x_104] },
383 { .compatible = "microchip,mcp4252-502",
384 .data = &mcp4131_cfg[MCP425x_502] },
385 { .compatible = "microchip,mcp4252-103",
386 .data = &mcp4131_cfg[MCP425x_103] },
387 { .compatible = "microchip,mcp4252-503",
388 .data = &mcp4131_cfg[MCP425x_503] },
389 { .compatible = "microchip,mcp4252-104",
390 .data = &mcp4131_cfg[MCP425x_104] },
391 { .compatible = "microchip,mcp4261-502",
392 .data = &mcp4131_cfg[MCP426x_502] },
393 { .compatible = "microchip,mcp4261-103",
394 .data = &mcp4131_cfg[MCP426x_103] },
395 { .compatible = "microchip,mcp4261-503",
396 .data = &mcp4131_cfg[MCP426x_503] },
397 { .compatible = "microchip,mcp4261-104",
398 .data = &mcp4131_cfg[MCP426x_104] },
399 { .compatible = "microchip,mcp4262-502",
400 .data = &mcp4131_cfg[MCP426x_502] },
401 { .compatible = "microchip,mcp4262-103",
402 .data = &mcp4131_cfg[MCP426x_103] },
403 { .compatible = "microchip,mcp4262-503",
404 .data = &mcp4131_cfg[MCP426x_503] },
405 { .compatible = "microchip,mcp4262-104",
406 .data = &mcp4131_cfg[MCP426x_104] },
407 {}
408};
409MODULE_DEVICE_TABLE(of, mcp4131_dt_ids);
410#endif /* CONFIG_OF */
411
412static const struct spi_device_id mcp4131_id[] = {
413 { "mcp4131-502", MCP413x_502 },
414 { "mcp4131-103", MCP413x_103 },
415 { "mcp4131-503", MCP413x_503 },
416 { "mcp4131-104", MCP413x_104 },
417 { "mcp4132-502", MCP413x_502 },
418 { "mcp4132-103", MCP413x_103 },
419 { "mcp4132-503", MCP413x_503 },
420 { "mcp4132-104", MCP413x_104 },
421 { "mcp4141-502", MCP414x_502 },
422 { "mcp4141-103", MCP414x_103 },
423 { "mcp4141-503", MCP414x_503 },
424 { "mcp4141-104", MCP414x_104 },
425 { "mcp4142-502", MCP414x_502 },
426 { "mcp4142-103", MCP414x_103 },
427 { "mcp4142-503", MCP414x_503 },
428 { "mcp4142-104", MCP414x_104 },
429 { "mcp4151-502", MCP415x_502 },
430 { "mcp4151-103", MCP415x_103 },
431 { "mcp4151-503", MCP415x_503 },
432 { "mcp4151-104", MCP415x_104 },
433 { "mcp4152-502", MCP415x_502 },
434 { "mcp4152-103", MCP415x_103 },
435 { "mcp4152-503", MCP415x_503 },
436 { "mcp4152-104", MCP415x_104 },
437 { "mcp4161-502", MCP416x_502 },
438 { "mcp4161-103", MCP416x_103 },
439 { "mcp4161-503", MCP416x_503 },
440 { "mcp4161-104", MCP416x_104 },
441 { "mcp4162-502", MCP416x_502 },
442 { "mcp4162-103", MCP416x_103 },
443 { "mcp4162-503", MCP416x_503 },
444 { "mcp4162-104", MCP416x_104 },
445 { "mcp4231-502", MCP423x_502 },
446 { "mcp4231-103", MCP423x_103 },
447 { "mcp4231-503", MCP423x_503 },
448 { "mcp4231-104", MCP423x_104 },
449 { "mcp4232-502", MCP423x_502 },
450 { "mcp4232-103", MCP423x_103 },
451 { "mcp4232-503", MCP423x_503 },
452 { "mcp4232-104", MCP423x_104 },
453 { "mcp4241-502", MCP424x_502 },
454 { "mcp4241-103", MCP424x_103 },
455 { "mcp4241-503", MCP424x_503 },
456 { "mcp4241-104", MCP424x_104 },
457 { "mcp4242-502", MCP424x_502 },
458 { "mcp4242-103", MCP424x_103 },
459 { "mcp4242-503", MCP424x_503 },
460 { "mcp4242-104", MCP424x_104 },
461 { "mcp4251-502", MCP425x_502 },
462 { "mcp4251-103", MCP425x_103 },
463 { "mcp4251-503", MCP425x_503 },
464 { "mcp4251-104", MCP425x_104 },
465 { "mcp4252-502", MCP425x_502 },
466 { "mcp4252-103", MCP425x_103 },
467 { "mcp4252-503", MCP425x_503 },
468 { "mcp4252-104", MCP425x_104 },
469 { "mcp4261-502", MCP426x_502 },
470 { "mcp4261-103", MCP426x_103 },
471 { "mcp4261-503", MCP426x_503 },
472 { "mcp4261-104", MCP426x_104 },
473 { "mcp4262-502", MCP426x_502 },
474 { "mcp4262-103", MCP426x_103 },
475 { "mcp4262-503", MCP426x_503 },
476 { "mcp4262-104", MCP426x_104 },
477 {}
478};
479MODULE_DEVICE_TABLE(spi, mcp4131_id);
480
481static struct spi_driver mcp4131_driver = {
482 .driver = {
483 .name = "mcp4131",
484 .of_match_table = of_match_ptr(mcp4131_dt_ids),
485 },
486 .probe = mcp4131_probe,
487 .id_table = mcp4131_id,
488};
489
490module_spi_driver(mcp4131_driver);
491
492MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>");
493MODULE_DESCRIPTION("MCP4131 digital potentiometer");
494MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/potentiometer/mcp4531.c b/drivers/iio/potentiometer/mcp4531.c
index 0db67fe14766..3b72e1a595db 100644
--- a/drivers/iio/potentiometer/mcp4531.c
+++ b/drivers/iio/potentiometer/mcp4531.c
@@ -79,7 +79,7 @@ static const struct mcp4531_cfg mcp4531_cfg[] = {
79 79
80struct mcp4531_data { 80struct mcp4531_data {
81 struct i2c_client *client; 81 struct i2c_client *client;
82 unsigned long devid; 82 const struct mcp4531_cfg *cfg;
83}; 83};
84 84
85#define MCP4531_CHANNEL(ch) { \ 85#define MCP4531_CHANNEL(ch) { \
@@ -113,8 +113,8 @@ static int mcp4531_read_raw(struct iio_dev *indio_dev,
113 *val = ret; 113 *val = ret;
114 return IIO_VAL_INT; 114 return IIO_VAL_INT;
115 case IIO_CHAN_INFO_SCALE: 115 case IIO_CHAN_INFO_SCALE:
116 *val = 1000 * mcp4531_cfg[data->devid].kohms; 116 *val = 1000 * data->cfg->kohms;
117 *val2 = mcp4531_cfg[data->devid].max_pos; 117 *val2 = data->cfg->max_pos;
118 return IIO_VAL_FRACTIONAL; 118 return IIO_VAL_FRACTIONAL;
119 } 119 }
120 120
@@ -130,7 +130,7 @@ static int mcp4531_write_raw(struct iio_dev *indio_dev,
130 130
131 switch (mask) { 131 switch (mask) {
132 case IIO_CHAN_INFO_RAW: 132 case IIO_CHAN_INFO_RAW:
133 if (val > mcp4531_cfg[data->devid].max_pos || val < 0) 133 if (val > data->cfg->max_pos || val < 0)
134 return -EINVAL; 134 return -EINVAL;
135 break; 135 break;
136 default: 136 default:
@@ -152,7 +152,6 @@ static int mcp4531_probe(struct i2c_client *client,
152 const struct i2c_device_id *id) 152 const struct i2c_device_id *id)
153{ 153{
154 struct device *dev = &client->dev; 154 struct device *dev = &client->dev;
155 unsigned long devid = id->driver_data;
156 struct mcp4531_data *data; 155 struct mcp4531_data *data;
157 struct iio_dev *indio_dev; 156 struct iio_dev *indio_dev;
158 157
@@ -168,12 +167,12 @@ static int mcp4531_probe(struct i2c_client *client,
168 data = iio_priv(indio_dev); 167 data = iio_priv(indio_dev);
169 i2c_set_clientdata(client, indio_dev); 168 i2c_set_clientdata(client, indio_dev);
170 data->client = client; 169 data->client = client;
171 data->devid = devid; 170 data->cfg = &mcp4531_cfg[id->driver_data];
172 171
173 indio_dev->dev.parent = dev; 172 indio_dev->dev.parent = dev;
174 indio_dev->info = &mcp4531_info; 173 indio_dev->info = &mcp4531_info;
175 indio_dev->channels = mcp4531_channels; 174 indio_dev->channels = mcp4531_channels;
176 indio_dev->num_channels = mcp4531_cfg[devid].wipers; 175 indio_dev->num_channels = data->cfg->wipers;
177 indio_dev->name = client->name; 176 indio_dev->name = client->name;
178 177
179 return devm_iio_device_register(dev, indio_dev); 178 return devm_iio_device_register(dev, indio_dev);
diff --git a/drivers/iio/potentiometer/tpl0102.c b/drivers/iio/potentiometer/tpl0102.c
index 313124b6fd59..5c304d42d713 100644
--- a/drivers/iio/potentiometer/tpl0102.c
+++ b/drivers/iio/potentiometer/tpl0102.c
@@ -118,7 +118,7 @@ static int tpl0102_probe(struct i2c_client *client,
118 118
119 if (!i2c_check_functionality(client->adapter, 119 if (!i2c_check_functionality(client->adapter,
120 I2C_FUNC_SMBUS_WORD_DATA)) 120 I2C_FUNC_SMBUS_WORD_DATA))
121 return -ENOTSUPP; 121 return -EOPNOTSUPP;
122 122
123 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 123 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
124 if (!indio_dev) 124 if (!indio_dev)
diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig
index 31c0e1fd2202..cda9f128f3a4 100644
--- a/drivers/iio/pressure/Kconfig
+++ b/drivers/iio/pressure/Kconfig
@@ -6,12 +6,13 @@
6menu "Pressure sensors" 6menu "Pressure sensors"
7 7
8config BMP280 8config BMP280
9 tristate "Bosch Sensortec BMP280 pressure sensor driver" 9 tristate "Bosch Sensortec BMP180 and BMP280 pressure sensor driver"
10 depends on I2C 10 depends on I2C
11 depends on !(BMP085_I2C=y || BMP085_I2C=m)
11 select REGMAP_I2C 12 select REGMAP_I2C
12 help 13 help
13 Say yes here to build support for Bosch Sensortec BMP280 14 Say yes here to build support for Bosch Sensortec BMP180 and BMP280
14 pressure and temperature sensor. 15 pressure and temperature sensors.
15 16
16 To compile this driver as a module, choose M here: the module 17 To compile this driver as a module, choose M here: the module
17 will be called bmp280. 18 will be called bmp280.
@@ -30,6 +31,17 @@ config HID_SENSOR_PRESS
30 To compile this driver as a module, choose M here: the module 31 To compile this driver as a module, choose M here: the module
31 will be called hid-sensor-press. 32 will be called hid-sensor-press.
32 33
34config HP03
35 tristate "Hope RF HP03 temperature and pressure sensor driver"
36 depends on I2C
37 select REGMAP_I2C
38 help
39 Say yes here to build support for Hope RF HP03 pressure and
40 temperature sensor.
41
42 To compile this driver as a module, choose M here: the module
43 will be called hp03.
44
33config MPL115 45config MPL115
34 tristate 46 tristate
35 47
@@ -148,4 +160,14 @@ config T5403
148 To compile this driver as a module, choose M here: the module 160 To compile this driver as a module, choose M here: the module
149 will be called t5403. 161 will be called t5403.
150 162
163config HP206C
164 tristate "HOPERF HP206C precision barometer and altimeter sensor"
165 depends on I2C
166 help
167 Say yes here to build support for the HOPREF HP206C precision
168 barometer and altimeter sensor.
169
170 This driver can also be built as a module. If so, the module will
171 be called hp206c.
172
151endmenu 173endmenu
diff --git a/drivers/iio/pressure/Makefile b/drivers/iio/pressure/Makefile
index d336af14f3fe..17d6e7afa1ff 100644
--- a/drivers/iio/pressure/Makefile
+++ b/drivers/iio/pressure/Makefile
@@ -5,6 +5,7 @@
5# When adding new entries keep the list in alphabetical order 5# When adding new entries keep the list in alphabetical order
6obj-$(CONFIG_BMP280) += bmp280.o 6obj-$(CONFIG_BMP280) += bmp280.o
7obj-$(CONFIG_HID_SENSOR_PRESS) += hid-sensor-press.o 7obj-$(CONFIG_HID_SENSOR_PRESS) += hid-sensor-press.o
8obj-$(CONFIG_HP03) += hp03.o
8obj-$(CONFIG_MPL115) += mpl115.o 9obj-$(CONFIG_MPL115) += mpl115.o
9obj-$(CONFIG_MPL115_I2C) += mpl115_i2c.o 10obj-$(CONFIG_MPL115_I2C) += mpl115_i2c.o
10obj-$(CONFIG_MPL115_SPI) += mpl115_spi.o 11obj-$(CONFIG_MPL115_SPI) += mpl115_spi.o
@@ -17,6 +18,7 @@ obj-$(CONFIG_IIO_ST_PRESS) += st_pressure.o
17st_pressure-y := st_pressure_core.o 18st_pressure-y := st_pressure_core.o
18st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o 19st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o
19obj-$(CONFIG_T5403) += t5403.o 20obj-$(CONFIG_T5403) += t5403.o
21obj-$(CONFIG_HP206C) += hp206c.o
20 22
21obj-$(CONFIG_IIO_ST_PRESS_I2C) += st_pressure_i2c.o 23obj-$(CONFIG_IIO_ST_PRESS_I2C) += st_pressure_i2c.o
22obj-$(CONFIG_IIO_ST_PRESS_SPI) += st_pressure_spi.o 24obj-$(CONFIG_IIO_ST_PRESS_SPI) += st_pressure_spi.o
diff --git a/drivers/iio/pressure/bmp280.c b/drivers/iio/pressure/bmp280.c
index a2602d8dd6d5..2f1498e12bb2 100644
--- a/drivers/iio/pressure/bmp280.c
+++ b/drivers/iio/pressure/bmp280.c
@@ -1,12 +1,15 @@
1/* 1/*
2 * Copyright (c) 2014 Intel Corporation 2 * Copyright (c) 2014 Intel Corporation
3 * 3 *
4 * Driver for Bosch Sensortec BMP280 digital pressure sensor. 4 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as 7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation. 8 * published by the Free Software Foundation.
9 * 9 *
10 * Datasheet:
11 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf
12 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001-12.pdf
10 */ 13 */
11 14
12#define pr_fmt(fmt) "bmp280: " fmt 15#define pr_fmt(fmt) "bmp280: " fmt
@@ -15,9 +18,11 @@
15#include <linux/i2c.h> 18#include <linux/i2c.h>
16#include <linux/acpi.h> 19#include <linux/acpi.h>
17#include <linux/regmap.h> 20#include <linux/regmap.h>
21#include <linux/delay.h>
18#include <linux/iio/iio.h> 22#include <linux/iio/iio.h>
19#include <linux/iio/sysfs.h> 23#include <linux/iio/sysfs.h>
20 24
25/* BMP280 specific registers */
21#define BMP280_REG_TEMP_XLSB 0xFC 26#define BMP280_REG_TEMP_XLSB 0xFC
22#define BMP280_REG_TEMP_LSB 0xFB 27#define BMP280_REG_TEMP_LSB 0xFB
23#define BMP280_REG_TEMP_MSB 0xFA 28#define BMP280_REG_TEMP_MSB 0xFA
@@ -26,10 +31,7 @@
26#define BMP280_REG_PRESS_MSB 0xF7 31#define BMP280_REG_PRESS_MSB 0xF7
27 32
28#define BMP280_REG_CONFIG 0xF5 33#define BMP280_REG_CONFIG 0xF5
29#define BMP280_REG_CTRL_MEAS 0xF4
30#define BMP280_REG_STATUS 0xF3 34#define BMP280_REG_STATUS 0xF3
31#define BMP280_REG_RESET 0xE0
32#define BMP280_REG_ID 0xD0
33 35
34#define BMP280_REG_COMP_TEMP_START 0x88 36#define BMP280_REG_COMP_TEMP_START 0x88
35#define BMP280_COMP_TEMP_REG_COUNT 6 37#define BMP280_COMP_TEMP_REG_COUNT 6
@@ -46,25 +48,49 @@
46 48
47#define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5)) 49#define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
48#define BMP280_OSRS_TEMP_SKIP 0 50#define BMP280_OSRS_TEMP_SKIP 0
49#define BMP280_OSRS_TEMP_1X BIT(5) 51#define BMP280_OSRS_TEMP_X(osrs_t) ((osrs_t) << 5)
50#define BMP280_OSRS_TEMP_2X BIT(6) 52#define BMP280_OSRS_TEMP_1X BMP280_OSRS_TEMP_X(1)
51#define BMP280_OSRS_TEMP_4X (BIT(6) | BIT(5)) 53#define BMP280_OSRS_TEMP_2X BMP280_OSRS_TEMP_X(2)
52#define BMP280_OSRS_TEMP_8X BIT(7) 54#define BMP280_OSRS_TEMP_4X BMP280_OSRS_TEMP_X(3)
53#define BMP280_OSRS_TEMP_16X (BIT(7) | BIT(5)) 55#define BMP280_OSRS_TEMP_8X BMP280_OSRS_TEMP_X(4)
56#define BMP280_OSRS_TEMP_16X BMP280_OSRS_TEMP_X(5)
54 57
55#define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2)) 58#define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
56#define BMP280_OSRS_PRESS_SKIP 0 59#define BMP280_OSRS_PRESS_SKIP 0
57#define BMP280_OSRS_PRESS_1X BIT(2) 60#define BMP280_OSRS_PRESS_X(osrs_p) ((osrs_p) << 2)
58#define BMP280_OSRS_PRESS_2X BIT(3) 61#define BMP280_OSRS_PRESS_1X BMP280_OSRS_PRESS_X(1)
59#define BMP280_OSRS_PRESS_4X (BIT(3) | BIT(2)) 62#define BMP280_OSRS_PRESS_2X BMP280_OSRS_PRESS_X(2)
60#define BMP280_OSRS_PRESS_8X BIT(4) 63#define BMP280_OSRS_PRESS_4X BMP280_OSRS_PRESS_X(3)
61#define BMP280_OSRS_PRESS_16X (BIT(4) | BIT(2)) 64#define BMP280_OSRS_PRESS_8X BMP280_OSRS_PRESS_X(4)
65#define BMP280_OSRS_PRESS_16X BMP280_OSRS_PRESS_X(5)
62 66
63#define BMP280_MODE_MASK (BIT(1) | BIT(0)) 67#define BMP280_MODE_MASK (BIT(1) | BIT(0))
64#define BMP280_MODE_SLEEP 0 68#define BMP280_MODE_SLEEP 0
65#define BMP280_MODE_FORCED BIT(0) 69#define BMP280_MODE_FORCED BIT(0)
66#define BMP280_MODE_NORMAL (BIT(1) | BIT(0)) 70#define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
67 71
72/* BMP180 specific registers */
73#define BMP180_REG_OUT_XLSB 0xF8
74#define BMP180_REG_OUT_LSB 0xF7
75#define BMP180_REG_OUT_MSB 0xF6
76
77#define BMP180_REG_CALIB_START 0xAA
78#define BMP180_REG_CALIB_COUNT 22
79
80#define BMP180_MEAS_SCO BIT(5)
81#define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO)
82#define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO)
83#define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0)
84#define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1)
85#define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2)
86#define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3)
87
88/* BMP180 and BMP280 common registers */
89#define BMP280_REG_CTRL_MEAS 0xF4
90#define BMP280_REG_RESET 0xE0
91#define BMP280_REG_ID 0xD0
92
93#define BMP180_CHIP_ID 0x55
68#define BMP280_CHIP_ID 0x58 94#define BMP280_CHIP_ID 0x58
69#define BMP280_SOFT_RESET_VAL 0xB6 95#define BMP280_SOFT_RESET_VAL 0xB6
70 96
@@ -72,6 +98,11 @@ struct bmp280_data {
72 struct i2c_client *client; 98 struct i2c_client *client;
73 struct mutex lock; 99 struct mutex lock;
74 struct regmap *regmap; 100 struct regmap *regmap;
101 const struct bmp280_chip_info *chip_info;
102
103 /* log of base 2 of oversampling rate */
104 u8 oversampling_press;
105 u8 oversampling_temp;
75 106
76 /* 107 /*
77 * Carryover value from temperature conversion, used in pressure 108 * Carryover value from temperature conversion, used in pressure
@@ -80,9 +111,23 @@ struct bmp280_data {
80 s32 t_fine; 111 s32 t_fine;
81}; 112};
82 113
114struct bmp280_chip_info {
115 const struct regmap_config *regmap_config;
116
117 const int *oversampling_temp_avail;
118 int num_oversampling_temp_avail;
119
120 const int *oversampling_press_avail;
121 int num_oversampling_press_avail;
122
123 int (*chip_config)(struct bmp280_data *);
124 int (*read_temp)(struct bmp280_data *, int *);
125 int (*read_press)(struct bmp280_data *, int *, int *);
126};
127
83/* 128/*
84 * These enums are used for indexing into the array of compensation 129 * These enums are used for indexing into the array of compensation
85 * parameters. 130 * parameters for BMP280.
86 */ 131 */
87enum { T1, T2, T3 }; 132enum { T1, T2, T3 };
88enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 }; 133enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
@@ -90,11 +135,13 @@ enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
90static const struct iio_chan_spec bmp280_channels[] = { 135static const struct iio_chan_spec bmp280_channels[] = {
91 { 136 {
92 .type = IIO_PRESSURE, 137 .type = IIO_PRESSURE,
93 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 138 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
139 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
94 }, 140 },
95 { 141 {
96 .type = IIO_TEMP, 142 .type = IIO_TEMP,
97 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 143 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
144 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
98 }, 145 },
99}; 146};
100 147
@@ -290,10 +337,25 @@ static int bmp280_read_raw(struct iio_dev *indio_dev,
290 case IIO_CHAN_INFO_PROCESSED: 337 case IIO_CHAN_INFO_PROCESSED:
291 switch (chan->type) { 338 switch (chan->type) {
292 case IIO_PRESSURE: 339 case IIO_PRESSURE:
293 ret = bmp280_read_press(data, val, val2); 340 ret = data->chip_info->read_press(data, val, val2);
294 break; 341 break;
295 case IIO_TEMP: 342 case IIO_TEMP:
296 ret = bmp280_read_temp(data, val); 343 ret = data->chip_info->read_temp(data, val);
344 break;
345 default:
346 ret = -EINVAL;
347 break;
348 }
349 break;
350 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
351 switch (chan->type) {
352 case IIO_PRESSURE:
353 *val = 1 << data->oversampling_press;
354 ret = IIO_VAL_INT;
355 break;
356 case IIO_TEMP:
357 *val = 1 << data->oversampling_temp;
358 ret = IIO_VAL_INT;
297 break; 359 break;
298 default: 360 default:
299 ret = -EINVAL; 361 ret = -EINVAL;
@@ -310,22 +372,135 @@ static int bmp280_read_raw(struct iio_dev *indio_dev,
310 return ret; 372 return ret;
311} 373}
312 374
375static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
376 int val)
377{
378 int i;
379 const int *avail = data->chip_info->oversampling_temp_avail;
380 const int n = data->chip_info->num_oversampling_temp_avail;
381
382 for (i = 0; i < n; i++) {
383 if (avail[i] == val) {
384 data->oversampling_temp = ilog2(val);
385
386 return data->chip_info->chip_config(data);
387 }
388 }
389 return -EINVAL;
390}
391
392static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
393 int val)
394{
395 int i;
396 const int *avail = data->chip_info->oversampling_press_avail;
397 const int n = data->chip_info->num_oversampling_press_avail;
398
399 for (i = 0; i < n; i++) {
400 if (avail[i] == val) {
401 data->oversampling_press = ilog2(val);
402
403 return data->chip_info->chip_config(data);
404 }
405 }
406 return -EINVAL;
407}
408
409static int bmp280_write_raw(struct iio_dev *indio_dev,
410 struct iio_chan_spec const *chan,
411 int val, int val2, long mask)
412{
413 int ret = 0;
414 struct bmp280_data *data = iio_priv(indio_dev);
415
416 switch (mask) {
417 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
418 mutex_lock(&data->lock);
419 switch (chan->type) {
420 case IIO_PRESSURE:
421 ret = bmp280_write_oversampling_ratio_press(data, val);
422 break;
423 case IIO_TEMP:
424 ret = bmp280_write_oversampling_ratio_temp(data, val);
425 break;
426 default:
427 ret = -EINVAL;
428 break;
429 }
430 mutex_unlock(&data->lock);
431 break;
432 default:
433 return -EINVAL;
434 }
435
436 return ret;
437}
438
439static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
440{
441 size_t len = 0;
442 int i;
443
444 for (i = 0; i < n; i++)
445 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
446
447 buf[len - 1] = '\n';
448
449 return len;
450}
451
452static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
453 struct device_attribute *attr, char *buf)
454{
455 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
456
457 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
458 data->chip_info->num_oversampling_temp_avail);
459}
460
461static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
464 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
465
466 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
467 data->chip_info->num_oversampling_press_avail);
468}
469
470static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
471 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
472
473static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
474 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
475
476static struct attribute *bmp280_attributes[] = {
477 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
478 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
479 NULL,
480};
481
482static const struct attribute_group bmp280_attrs_group = {
483 .attrs = bmp280_attributes,
484};
485
313static const struct iio_info bmp280_info = { 486static const struct iio_info bmp280_info = {
314 .driver_module = THIS_MODULE, 487 .driver_module = THIS_MODULE,
315 .read_raw = &bmp280_read_raw, 488 .read_raw = &bmp280_read_raw,
489 .write_raw = &bmp280_write_raw,
490 .attrs = &bmp280_attrs_group,
316}; 491};
317 492
318static int bmp280_chip_init(struct bmp280_data *data) 493static int bmp280_chip_config(struct bmp280_data *data)
319{ 494{
320 int ret; 495 int ret;
496 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
497 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
321 498
322 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS, 499 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
323 BMP280_OSRS_TEMP_MASK | 500 BMP280_OSRS_TEMP_MASK |
324 BMP280_OSRS_PRESS_MASK | 501 BMP280_OSRS_PRESS_MASK |
325 BMP280_MODE_MASK, 502 BMP280_MODE_MASK,
326 BMP280_OSRS_TEMP_2X | 503 osrs | BMP280_MODE_NORMAL);
327 BMP280_OSRS_PRESS_16X |
328 BMP280_MODE_NORMAL);
329 if (ret < 0) { 504 if (ret < 0) {
330 dev_err(&data->client->dev, 505 dev_err(&data->client->dev,
331 "failed to write ctrl_meas register\n"); 506 "failed to write ctrl_meas register\n");
@@ -344,6 +519,317 @@ static int bmp280_chip_init(struct bmp280_data *data)
344 return ret; 519 return ret;
345} 520}
346 521
522static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
523
524static const struct bmp280_chip_info bmp280_chip_info = {
525 .regmap_config = &bmp280_regmap_config,
526
527 .oversampling_temp_avail = bmp280_oversampling_avail,
528 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
529
530 .oversampling_press_avail = bmp280_oversampling_avail,
531 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
532
533 .chip_config = bmp280_chip_config,
534 .read_temp = bmp280_read_temp,
535 .read_press = bmp280_read_press,
536};
537
538static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg)
539{
540 switch (reg) {
541 case BMP280_REG_CTRL_MEAS:
542 case BMP280_REG_RESET:
543 return true;
544 default:
545 return false;
546 };
547}
548
549static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg)
550{
551 switch (reg) {
552 case BMP180_REG_OUT_XLSB:
553 case BMP180_REG_OUT_LSB:
554 case BMP180_REG_OUT_MSB:
555 case BMP280_REG_CTRL_MEAS:
556 return true;
557 default:
558 return false;
559 }
560}
561
562static const struct regmap_config bmp180_regmap_config = {
563 .reg_bits = 8,
564 .val_bits = 8,
565
566 .max_register = BMP180_REG_OUT_XLSB,
567 .cache_type = REGCACHE_RBTREE,
568
569 .writeable_reg = bmp180_is_writeable_reg,
570 .volatile_reg = bmp180_is_volatile_reg,
571};
572
573static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
574{
575 int ret;
576 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
577 unsigned int delay_us;
578 unsigned int ctrl;
579
580 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
581 if (ret)
582 return ret;
583
584 if (ctrl_meas == BMP180_MEAS_TEMP)
585 delay_us = 4500;
586 else
587 delay_us = conversion_time_max[data->oversampling_press];
588
589 usleep_range(delay_us, delay_us + 1000);
590
591 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
592 if (ret)
593 return ret;
594
595 /* The value of this bit reset to "0" after conversion is complete */
596 if (ctrl & BMP180_MEAS_SCO)
597 return -EIO;
598
599 return 0;
600}
601
602static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
603{
604 int ret;
605 __be16 tmp = 0;
606
607 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
608 if (ret)
609 return ret;
610
611 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
612 if (ret)
613 return ret;
614
615 *val = be16_to_cpu(tmp);
616
617 return 0;
618}
619
620/*
621 * These enums are used for indexing into the array of calibration
622 * coefficients for BMP180.
623 */
624enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
625
626struct bmp180_calib {
627 s16 AC1;
628 s16 AC2;
629 s16 AC3;
630 u16 AC4;
631 u16 AC5;
632 u16 AC6;
633 s16 B1;
634 s16 B2;
635 s16 MB;
636 s16 MC;
637 s16 MD;
638};
639
640static int bmp180_read_calib(struct bmp280_data *data,
641 struct bmp180_calib *calib)
642{
643 int ret;
644 int i;
645 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
646
647 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
648 sizeof(buf));
649
650 if (ret < 0)
651 return ret;
652
653 /* None of the words has the value 0 or 0xFFFF */
654 for (i = 0; i < ARRAY_SIZE(buf); i++) {
655 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
656 return -EIO;
657 }
658
659 calib->AC1 = be16_to_cpu(buf[AC1]);
660 calib->AC2 = be16_to_cpu(buf[AC2]);
661 calib->AC3 = be16_to_cpu(buf[AC3]);
662 calib->AC4 = be16_to_cpu(buf[AC4]);
663 calib->AC5 = be16_to_cpu(buf[AC5]);
664 calib->AC6 = be16_to_cpu(buf[AC6]);
665 calib->B1 = be16_to_cpu(buf[B1]);
666 calib->B2 = be16_to_cpu(buf[B2]);
667 calib->MB = be16_to_cpu(buf[MB]);
668 calib->MC = be16_to_cpu(buf[MC]);
669 calib->MD = be16_to_cpu(buf[MD]);
670
671 return 0;
672}
673
674/*
675 * Returns temperature in DegC, resolution is 0.1 DegC.
676 * t_fine carries fine temperature as global value.
677 *
678 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
679 */
680static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
681{
682 int ret;
683 s32 x1, x2;
684 struct bmp180_calib calib;
685
686 ret = bmp180_read_calib(data, &calib);
687 if (ret < 0) {
688 dev_err(&data->client->dev,
689 "failed to read calibration coefficients\n");
690 return ret;
691 }
692
693 x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15;
694 x2 = (calib.MC << 11) / (x1 + calib.MD);
695 data->t_fine = x1 + x2;
696
697 return (data->t_fine + 8) >> 4;
698}
699
700static int bmp180_read_temp(struct bmp280_data *data, int *val)
701{
702 int ret;
703 s32 adc_temp, comp_temp;
704
705 ret = bmp180_read_adc_temp(data, &adc_temp);
706 if (ret)
707 return ret;
708
709 comp_temp = bmp180_compensate_temp(data, adc_temp);
710
711 /*
712 * val might be NULL if we're called by the read_press routine,
713 * who only cares about the carry over t_fine value.
714 */
715 if (val) {
716 *val = comp_temp * 100;
717 return IIO_VAL_INT;
718 }
719
720 return 0;
721}
722
723static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
724{
725 int ret;
726 __be32 tmp = 0;
727 u8 oss = data->oversampling_press;
728
729 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
730 if (ret)
731 return ret;
732
733 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
734 if (ret)
735 return ret;
736
737 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
738
739 return 0;
740}
741
742/*
743 * Returns pressure in Pa, resolution is 1 Pa.
744 *
745 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
746 */
747static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
748{
749 int ret;
750 s32 x1, x2, x3, p;
751 s32 b3, b6;
752 u32 b4, b7;
753 s32 oss = data->oversampling_press;
754 struct bmp180_calib calib;
755
756 ret = bmp180_read_calib(data, &calib);
757 if (ret < 0) {
758 dev_err(&data->client->dev,
759 "failed to read calibration coefficients\n");
760 return ret;
761 }
762
763 b6 = data->t_fine - 4000;
764 x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11;
765 x2 = calib.AC2 * b6 >> 11;
766 x3 = x1 + x2;
767 b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4;
768 x1 = calib.AC3 * b6 >> 13;
769 x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16;
770 x3 = (x1 + x2 + 2) >> 2;
771 b4 = calib.AC4 * (u32)(x3 + 32768) >> 15;
772 b7 = ((u32)adc_press - b3) * (50000 >> oss);
773 if (b7 < 0x80000000)
774 p = (b7 * 2) / b4;
775 else
776 p = (b7 / b4) * 2;
777
778 x1 = (p >> 8) * (p >> 8);
779 x1 = (x1 * 3038) >> 16;
780 x2 = (-7357 * p) >> 16;
781
782 return p + ((x1 + x2 + 3791) >> 4);
783}
784
785static int bmp180_read_press(struct bmp280_data *data,
786 int *val, int *val2)
787{
788 int ret;
789 s32 adc_press;
790 u32 comp_press;
791
792 /* Read and compensate temperature so we get a reading of t_fine. */
793 ret = bmp180_read_temp(data, NULL);
794 if (ret)
795 return ret;
796
797 ret = bmp180_read_adc_press(data, &adc_press);
798 if (ret)
799 return ret;
800
801 comp_press = bmp180_compensate_press(data, adc_press);
802
803 *val = comp_press;
804 *val2 = 1000;
805
806 return IIO_VAL_FRACTIONAL;
807}
808
809static int bmp180_chip_config(struct bmp280_data *data)
810{
811 return 0;
812}
813
814static const int bmp180_oversampling_temp_avail[] = { 1 };
815static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
816
817static const struct bmp280_chip_info bmp180_chip_info = {
818 .regmap_config = &bmp180_regmap_config,
819
820 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
821 .num_oversampling_temp_avail =
822 ARRAY_SIZE(bmp180_oversampling_temp_avail),
823
824 .oversampling_press_avail = bmp180_oversampling_press_avail,
825 .num_oversampling_press_avail =
826 ARRAY_SIZE(bmp180_oversampling_press_avail),
827
828 .chip_config = bmp180_chip_config,
829 .read_temp = bmp180_read_temp,
830 .read_press = bmp180_read_press,
831};
832
347static int bmp280_probe(struct i2c_client *client, 833static int bmp280_probe(struct i2c_client *client,
348 const struct i2c_device_id *id) 834 const struct i2c_device_id *id)
349{ 835{
@@ -367,7 +853,23 @@ static int bmp280_probe(struct i2c_client *client,
367 indio_dev->info = &bmp280_info; 853 indio_dev->info = &bmp280_info;
368 indio_dev->modes = INDIO_DIRECT_MODE; 854 indio_dev->modes = INDIO_DIRECT_MODE;
369 855
370 data->regmap = devm_regmap_init_i2c(client, &bmp280_regmap_config); 856 switch (id->driver_data) {
857 case BMP180_CHIP_ID:
858 data->chip_info = &bmp180_chip_info;
859 data->oversampling_press = ilog2(8);
860 data->oversampling_temp = ilog2(1);
861 break;
862 case BMP280_CHIP_ID:
863 data->chip_info = &bmp280_chip_info;
864 data->oversampling_press = ilog2(16);
865 data->oversampling_temp = ilog2(2);
866 break;
867 default:
868 return -EINVAL;
869 }
870
871 data->regmap = devm_regmap_init_i2c(client,
872 data->chip_info->regmap_config);
371 if (IS_ERR(data->regmap)) { 873 if (IS_ERR(data->regmap)) {
372 dev_err(&client->dev, "failed to allocate register map\n"); 874 dev_err(&client->dev, "failed to allocate register map\n");
373 return PTR_ERR(data->regmap); 875 return PTR_ERR(data->regmap);
@@ -376,13 +878,13 @@ static int bmp280_probe(struct i2c_client *client,
376 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id); 878 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id);
377 if (ret < 0) 879 if (ret < 0)
378 return ret; 880 return ret;
379 if (chip_id != BMP280_CHIP_ID) { 881 if (chip_id != id->driver_data) {
380 dev_err(&client->dev, "bad chip id. expected %x got %x\n", 882 dev_err(&client->dev, "bad chip id. expected %x got %x\n",
381 BMP280_CHIP_ID, chip_id); 883 BMP280_CHIP_ID, chip_id);
382 return -EINVAL; 884 return -EINVAL;
383 } 885 }
384 886
385 ret = bmp280_chip_init(data); 887 ret = data->chip_info->chip_config(data);
386 if (ret < 0) 888 if (ret < 0)
387 return ret; 889 return ret;
388 890
@@ -390,13 +892,17 @@ static int bmp280_probe(struct i2c_client *client,
390} 892}
391 893
392static const struct acpi_device_id bmp280_acpi_match[] = { 894static const struct acpi_device_id bmp280_acpi_match[] = {
393 {"BMP0280", 0}, 895 {"BMP0280", BMP280_CHIP_ID },
896 {"BMP0180", BMP180_CHIP_ID },
897 {"BMP0085", BMP180_CHIP_ID },
394 { }, 898 { },
395}; 899};
396MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match); 900MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
397 901
398static const struct i2c_device_id bmp280_id[] = { 902static const struct i2c_device_id bmp280_id[] = {
399 {"bmp280", 0}, 903 {"bmp280", BMP280_CHIP_ID },
904 {"bmp180", BMP180_CHIP_ID },
905 {"bmp085", BMP180_CHIP_ID },
400 { }, 906 { },
401}; 907};
402MODULE_DEVICE_TABLE(i2c, bmp280_id); 908MODULE_DEVICE_TABLE(i2c, bmp280_id);
@@ -412,5 +918,5 @@ static struct i2c_driver bmp280_driver = {
412module_i2c_driver(bmp280_driver); 918module_i2c_driver(bmp280_driver);
413 919
414MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); 920MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
415MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP280 pressure and temperature sensor"); 921MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
416MODULE_LICENSE("GPL v2"); 922MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/pressure/hp03.c b/drivers/iio/pressure/hp03.c
new file mode 100644
index 000000000000..ac76515d5d49
--- /dev/null
+++ b/drivers/iio/pressure/hp03.c
@@ -0,0 +1,312 @@
1/*
2 * Copyright (c) 2016 Marek Vasut <marex@denx.de>
3 *
4 * Driver for Hope RF HP03 digital temperature and pressure sensor.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#define pr_fmt(fmt) "hp03: " fmt
12
13#include <linux/module.h>
14#include <linux/delay.h>
15#include <linux/gpio/consumer.h>
16#include <linux/i2c.h>
17#include <linux/regmap.h>
18#include <linux/iio/iio.h>
19#include <linux/iio/sysfs.h>
20
21/*
22 * The HP03 sensor occupies two fixed I2C addresses:
23 * 0x50 ... read-only EEPROM with calibration data
24 * 0x77 ... read-write ADC for pressure and temperature
25 */
26#define HP03_EEPROM_ADDR 0x50
27#define HP03_ADC_ADDR 0x77
28
29#define HP03_EEPROM_CX_OFFSET 0x10
30#define HP03_EEPROM_AB_OFFSET 0x1e
31#define HP03_EEPROM_CD_OFFSET 0x20
32
33#define HP03_ADC_WRITE_REG 0xff
34#define HP03_ADC_READ_REG 0xfd
35#define HP03_ADC_READ_PRESSURE 0xf0 /* D1 in datasheet */
36#define HP03_ADC_READ_TEMP 0xe8 /* D2 in datasheet */
37
38struct hp03_priv {
39 struct i2c_client *client;
40 struct mutex lock;
41 struct gpio_desc *xclr_gpio;
42
43 struct i2c_client *eeprom_client;
44 struct regmap *eeprom_regmap;
45
46 s32 pressure; /* kPa */
47 s32 temp; /* Deg. C */
48};
49
50static const struct iio_chan_spec hp03_channels[] = {
51 {
52 .type = IIO_PRESSURE,
53 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
54 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
55 },
56 {
57 .type = IIO_TEMP,
58 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
59 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
60 },
61};
62
63static bool hp03_is_writeable_reg(struct device *dev, unsigned int reg)
64{
65 return false;
66}
67
68static bool hp03_is_volatile_reg(struct device *dev, unsigned int reg)
69{
70 return false;
71}
72
73static const struct regmap_config hp03_regmap_config = {
74 .reg_bits = 8,
75 .val_bits = 8,
76
77 .max_register = HP03_EEPROM_CD_OFFSET + 1,
78 .cache_type = REGCACHE_RBTREE,
79
80 .writeable_reg = hp03_is_writeable_reg,
81 .volatile_reg = hp03_is_volatile_reg,
82};
83
84static int hp03_get_temp_pressure(struct hp03_priv *priv, const u8 reg)
85{
86 int ret;
87
88 ret = i2c_smbus_write_byte_data(priv->client, HP03_ADC_WRITE_REG, reg);
89 if (ret < 0)
90 return ret;
91
92 msleep(50); /* Wait for conversion to finish */
93
94 return i2c_smbus_read_word_data(priv->client, HP03_ADC_READ_REG);
95}
96
97static int hp03_update_temp_pressure(struct hp03_priv *priv)
98{
99 struct device *dev = &priv->client->dev;
100 u8 coefs[18];
101 u16 cx_val[7];
102 int ab_val, d1_val, d2_val, diff_val, dut, off, sens, x;
103 int i, ret;
104
105 /* Sample coefficients from EEPROM */
106 ret = regmap_bulk_read(priv->eeprom_regmap, HP03_EEPROM_CX_OFFSET,
107 coefs, sizeof(coefs));
108 if (ret < 0) {
109 dev_err(dev, "Failed to read EEPROM (reg=%02x)\n",
110 HP03_EEPROM_CX_OFFSET);
111 return ret;
112 }
113
114 /* Sample Temperature and Pressure */
115 gpiod_set_value_cansleep(priv->xclr_gpio, 1);
116
117 ret = hp03_get_temp_pressure(priv, HP03_ADC_READ_PRESSURE);
118 if (ret < 0) {
119 dev_err(dev, "Failed to read pressure\n");
120 goto err_adc;
121 }
122 d1_val = ret;
123
124 ret = hp03_get_temp_pressure(priv, HP03_ADC_READ_TEMP);
125 if (ret < 0) {
126 dev_err(dev, "Failed to read temperature\n");
127 goto err_adc;
128 }
129 d2_val = ret;
130
131 gpiod_set_value_cansleep(priv->xclr_gpio, 0);
132
133 /* The Cx coefficients and Temp/Pressure values are MSB first. */
134 for (i = 0; i < 7; i++)
135 cx_val[i] = (coefs[2 * i] << 8) | (coefs[(2 * i) + 1] << 0);
136 d1_val = ((d1_val >> 8) & 0xff) | ((d1_val & 0xff) << 8);
137 d2_val = ((d2_val >> 8) & 0xff) | ((d2_val & 0xff) << 8);
138
139 /* Coefficient voodoo from the HP03 datasheet. */
140 if (d2_val >= cx_val[4])
141 ab_val = coefs[14]; /* A-value */
142 else
143 ab_val = coefs[15]; /* B-value */
144
145 diff_val = d2_val - cx_val[4];
146 dut = (ab_val * (diff_val >> 7) * (diff_val >> 7)) >> coefs[16];
147 dut = diff_val - dut;
148
149 off = (cx_val[1] + (((cx_val[3] - 1024) * dut) >> 14)) * 4;
150 sens = cx_val[0] + ((cx_val[2] * dut) >> 10);
151 x = ((sens * (d1_val - 7168)) >> 14) - off;
152
153 priv->pressure = ((x * 100) >> 5) + (cx_val[6] * 10);
154 priv->temp = 250 + ((dut * cx_val[5]) >> 16) - (dut >> coefs[17]);
155
156 return 0;
157
158err_adc:
159 gpiod_set_value_cansleep(priv->xclr_gpio, 0);
160 return ret;
161}
162
163static int hp03_read_raw(struct iio_dev *indio_dev,
164 struct iio_chan_spec const *chan,
165 int *val, int *val2, long mask)
166{
167 struct hp03_priv *priv = iio_priv(indio_dev);
168 int ret;
169
170 mutex_lock(&priv->lock);
171 ret = hp03_update_temp_pressure(priv);
172 mutex_unlock(&priv->lock);
173
174 if (ret)
175 return ret;
176
177 switch (mask) {
178 case IIO_CHAN_INFO_RAW:
179 switch (chan->type) {
180 case IIO_PRESSURE:
181 *val = priv->pressure;
182 return IIO_VAL_INT;
183 case IIO_TEMP:
184 *val = priv->temp;
185 return IIO_VAL_INT;
186 default:
187 return -EINVAL;
188 }
189 break;
190 case IIO_CHAN_INFO_SCALE:
191 switch (chan->type) {
192 case IIO_PRESSURE:
193 *val = 0;
194 *val2 = 1000;
195 return IIO_VAL_INT_PLUS_MICRO;
196 case IIO_TEMP:
197 *val = 10;
198 return IIO_VAL_INT;
199 default:
200 return -EINVAL;
201 }
202 break;
203 default:
204 return -EINVAL;
205 }
206
207 return -EINVAL;
208}
209
210static const struct iio_info hp03_info = {
211 .driver_module = THIS_MODULE,
212 .read_raw = &hp03_read_raw,
213};
214
215static int hp03_probe(struct i2c_client *client,
216 const struct i2c_device_id *id)
217{
218 struct device *dev = &client->dev;
219 struct iio_dev *indio_dev;
220 struct hp03_priv *priv;
221 int ret;
222
223 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
224 if (!indio_dev)
225 return -ENOMEM;
226
227 priv = iio_priv(indio_dev);
228 priv->client = client;
229 mutex_init(&priv->lock);
230
231 indio_dev->dev.parent = dev;
232 indio_dev->name = id->name;
233 indio_dev->channels = hp03_channels;
234 indio_dev->num_channels = ARRAY_SIZE(hp03_channels);
235 indio_dev->info = &hp03_info;
236 indio_dev->modes = INDIO_DIRECT_MODE;
237
238 priv->xclr_gpio = devm_gpiod_get_index(dev, "xclr", 0, GPIOD_OUT_HIGH);
239 if (IS_ERR(priv->xclr_gpio)) {
240 dev_err(dev, "Failed to claim XCLR GPIO\n");
241 ret = PTR_ERR(priv->xclr_gpio);
242 return ret;
243 }
244
245 /*
246 * Allocate another device for the on-sensor EEPROM,
247 * which has it's dedicated I2C address and contains
248 * the calibration constants for the sensor.
249 */
250 priv->eeprom_client = i2c_new_dummy(client->adapter, HP03_EEPROM_ADDR);
251 if (!priv->eeprom_client) {
252 dev_err(dev, "New EEPROM I2C device failed\n");
253 return -ENODEV;
254 }
255
256 priv->eeprom_regmap = regmap_init_i2c(priv->eeprom_client,
257 &hp03_regmap_config);
258 if (IS_ERR(priv->eeprom_regmap)) {
259 dev_err(dev, "Failed to allocate EEPROM regmap\n");
260 ret = PTR_ERR(priv->eeprom_regmap);
261 goto err_cleanup_eeprom_client;
262 }
263
264 ret = iio_device_register(indio_dev);
265 if (ret) {
266 dev_err(dev, "Failed to register IIO device\n");
267 goto err_cleanup_eeprom_regmap;
268 }
269
270 i2c_set_clientdata(client, indio_dev);
271
272 return 0;
273
274err_cleanup_eeprom_regmap:
275 regmap_exit(priv->eeprom_regmap);
276
277err_cleanup_eeprom_client:
278 i2c_unregister_device(priv->eeprom_client);
279 return ret;
280}
281
282static int hp03_remove(struct i2c_client *client)
283{
284 struct iio_dev *indio_dev = i2c_get_clientdata(client);
285 struct hp03_priv *priv = iio_priv(indio_dev);
286
287 iio_device_unregister(indio_dev);
288 regmap_exit(priv->eeprom_regmap);
289 i2c_unregister_device(priv->eeprom_client);
290
291 return 0;
292}
293
294static const struct i2c_device_id hp03_id[] = {
295 { "hp03", 0 },
296 { },
297};
298MODULE_DEVICE_TABLE(i2c, hp03_id);
299
300static struct i2c_driver hp03_driver = {
301 .driver = {
302 .name = "hp03",
303 },
304 .probe = hp03_probe,
305 .remove = hp03_remove,
306 .id_table = hp03_id,
307};
308module_i2c_driver(hp03_driver);
309
310MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
311MODULE_DESCRIPTION("Driver for Hope RF HP03 pressure and temperature sensor");
312MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/pressure/hp206c.c b/drivers/iio/pressure/hp206c.c
new file mode 100644
index 000000000000..90f2b6e4a920
--- /dev/null
+++ b/drivers/iio/pressure/hp206c.c
@@ -0,0 +1,426 @@
1/*
2 * hp206c.c - HOPERF HP206C precision barometer and altimeter sensor
3 *
4 * Copyright (c) 2016, Intel Corporation.
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * (7-bit I2C slave address 0x76)
11 *
12 * Datasheet:
13 * http://www.hoperf.com/upload/sensor/HP206C_DataSheet_EN_V2.0.pdf
14 */
15
16#include <linux/module.h>
17#include <linux/i2c.h>
18#include <linux/iio/iio.h>
19#include <linux/iio/sysfs.h>
20#include <linux/delay.h>
21#include <linux/util_macros.h>
22#include <linux/acpi.h>
23
24/* I2C commands: */
25#define HP206C_CMD_SOFT_RST 0x06
26
27#define HP206C_CMD_ADC_CVT 0x40
28
29#define HP206C_CMD_ADC_CVT_OSR_4096 0x00
30#define HP206C_CMD_ADC_CVT_OSR_2048 0x04
31#define HP206C_CMD_ADC_CVT_OSR_1024 0x08
32#define HP206C_CMD_ADC_CVT_OSR_512 0x0c
33#define HP206C_CMD_ADC_CVT_OSR_256 0x10
34#define HP206C_CMD_ADC_CVT_OSR_128 0x14
35
36#define HP206C_CMD_ADC_CVT_CHNL_PT 0x00
37#define HP206C_CMD_ADC_CVT_CHNL_T 0x02
38
39#define HP206C_CMD_READ_P 0x30
40#define HP206C_CMD_READ_T 0x32
41
42#define HP206C_CMD_READ_REG 0x80
43#define HP206C_CMD_WRITE_REG 0xc0
44
45#define HP206C_REG_INT_EN 0x0b
46#define HP206C_REG_INT_CFG 0x0c
47
48#define HP206C_REG_INT_SRC 0x0d
49#define HP206C_FLAG_DEV_RDY 0x40
50
51#define HP206C_REG_PARA 0x0f
52#define HP206C_FLAG_CMPS_EN 0x80
53
54/* Maximum spin for DEV_RDY */
55#define HP206C_MAX_DEV_RDY_WAIT_COUNT 20
56#define HP206C_DEV_RDY_WAIT_US 20000
57
58struct hp206c_data {
59 struct mutex mutex;
60 struct i2c_client *client;
61 int temp_osr_index;
62 int pres_osr_index;
63};
64
65struct hp206c_osr_setting {
66 u8 osr_mask;
67 unsigned int temp_conv_time_us;
68 unsigned int pres_conv_time_us;
69};
70
71/* Data from Table 5 in datasheet. */
72static const struct hp206c_osr_setting hp206c_osr_settings[] = {
73 { HP206C_CMD_ADC_CVT_OSR_4096, 65600, 131100 },
74 { HP206C_CMD_ADC_CVT_OSR_2048, 32800, 65600 },
75 { HP206C_CMD_ADC_CVT_OSR_1024, 16400, 32800 },
76 { HP206C_CMD_ADC_CVT_OSR_512, 8200, 16400 },
77 { HP206C_CMD_ADC_CVT_OSR_256, 4100, 8200 },
78 { HP206C_CMD_ADC_CVT_OSR_128, 2100, 4100 },
79};
80static const int hp206c_osr_rates[] = { 4096, 2048, 1024, 512, 256, 128 };
81static const char hp206c_osr_rates_str[] = "4096 2048 1024 512 256 128";
82
83static inline int hp206c_read_reg(struct i2c_client *client, u8 reg)
84{
85 return i2c_smbus_read_byte_data(client, HP206C_CMD_READ_REG | reg);
86}
87
88static inline int hp206c_write_reg(struct i2c_client *client, u8 reg, u8 val)
89{
90 return i2c_smbus_write_byte_data(client,
91 HP206C_CMD_WRITE_REG | reg, val);
92}
93
94static int hp206c_read_20bit(struct i2c_client *client, u8 cmd)
95{
96 int ret;
97 u8 values[3];
98
99 ret = i2c_smbus_read_i2c_block_data(client, cmd, 3, values);
100 if (ret < 0)
101 return ret;
102 if (ret != 3)
103 return -EIO;
104 return ((values[0] & 0xF) << 16) | (values[1] << 8) | (values[2]);
105}
106
107/* Spin for max 160ms until DEV_RDY is 1, or return error. */
108static int hp206c_wait_dev_rdy(struct iio_dev *indio_dev)
109{
110 int ret;
111 int count = 0;
112 struct hp206c_data *data = iio_priv(indio_dev);
113 struct i2c_client *client = data->client;
114
115 while (++count <= HP206C_MAX_DEV_RDY_WAIT_COUNT) {
116 ret = hp206c_read_reg(client, HP206C_REG_INT_SRC);
117 if (ret < 0) {
118 dev_err(&indio_dev->dev, "Failed READ_REG INT_SRC: %d\n", ret);
119 return ret;
120 }
121 if (ret & HP206C_FLAG_DEV_RDY)
122 return 0;
123 usleep_range(HP206C_DEV_RDY_WAIT_US, HP206C_DEV_RDY_WAIT_US * 3 / 2);
124 }
125 return -ETIMEDOUT;
126}
127
128static int hp206c_set_compensation(struct i2c_client *client, bool enabled)
129{
130 int val;
131
132 val = hp206c_read_reg(client, HP206C_REG_PARA);
133 if (val < 0)
134 return val;
135 if (enabled)
136 val |= HP206C_FLAG_CMPS_EN;
137 else
138 val &= ~HP206C_FLAG_CMPS_EN;
139
140 return hp206c_write_reg(client, HP206C_REG_PARA, val);
141}
142
143/* Do a soft reset */
144static int hp206c_soft_reset(struct iio_dev *indio_dev)
145{
146 int ret;
147 struct hp206c_data *data = iio_priv(indio_dev);
148 struct i2c_client *client = data->client;
149
150 ret = i2c_smbus_write_byte(client, HP206C_CMD_SOFT_RST);
151 if (ret) {
152 dev_err(&client->dev, "Failed to reset device: %d\n", ret);
153 return ret;
154 }
155
156 usleep_range(400, 600);
157
158 ret = hp206c_wait_dev_rdy(indio_dev);
159 if (ret) {
160 dev_err(&client->dev, "Device not ready after soft reset: %d\n", ret);
161 return ret;
162 }
163
164 ret = hp206c_set_compensation(client, true);
165 if (ret)
166 dev_err(&client->dev, "Failed to enable compensation: %d\n", ret);
167 return ret;
168}
169
170static int hp206c_conv_and_read(struct iio_dev *indio_dev,
171 u8 conv_cmd, u8 read_cmd,
172 unsigned int sleep_us)
173{
174 int ret;
175 struct hp206c_data *data = iio_priv(indio_dev);
176 struct i2c_client *client = data->client;
177
178 ret = hp206c_wait_dev_rdy(indio_dev);
179 if (ret < 0) {
180 dev_err(&indio_dev->dev, "Device not ready: %d\n", ret);
181 return ret;
182 }
183
184 ret = i2c_smbus_write_byte(client, conv_cmd);
185 if (ret < 0) {
186 dev_err(&indio_dev->dev, "Failed convert: %d\n", ret);
187 return ret;
188 }
189
190 usleep_range(sleep_us, sleep_us * 3 / 2);
191
192 ret = hp206c_wait_dev_rdy(indio_dev);
193 if (ret < 0) {
194 dev_err(&indio_dev->dev, "Device not ready: %d\n", ret);
195 return ret;
196 }
197
198 ret = hp206c_read_20bit(client, read_cmd);
199 if (ret < 0)
200 dev_err(&indio_dev->dev, "Failed read: %d\n", ret);
201
202 return ret;
203}
204
205static int hp206c_read_raw(struct iio_dev *indio_dev,
206 struct iio_chan_spec const *chan, int *val,
207 int *val2, long mask)
208{
209 int ret;
210 struct hp206c_data *data = iio_priv(indio_dev);
211 const struct hp206c_osr_setting *osr_setting;
212 u8 conv_cmd;
213
214 mutex_lock(&data->mutex);
215
216 switch (mask) {
217 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
218 switch (chan->type) {
219 case IIO_TEMP:
220 *val = hp206c_osr_rates[data->temp_osr_index];
221 ret = IIO_VAL_INT;
222 break;
223
224 case IIO_PRESSURE:
225 *val = hp206c_osr_rates[data->pres_osr_index];
226 ret = IIO_VAL_INT;
227 break;
228 default:
229 ret = -EINVAL;
230 }
231 break;
232
233 case IIO_CHAN_INFO_RAW:
234 switch (chan->type) {
235 case IIO_TEMP:
236 osr_setting = &hp206c_osr_settings[data->temp_osr_index];
237 conv_cmd = HP206C_CMD_ADC_CVT |
238 osr_setting->osr_mask |
239 HP206C_CMD_ADC_CVT_CHNL_T;
240 ret = hp206c_conv_and_read(indio_dev,
241 conv_cmd,
242 HP206C_CMD_READ_T,
243 osr_setting->temp_conv_time_us);
244 if (ret >= 0) {
245 /* 20 significant bits are provided.
246 * Extend sign over the rest.
247 */
248 *val = sign_extend32(ret, 19);
249 ret = IIO_VAL_INT;
250 }
251 break;
252
253 case IIO_PRESSURE:
254 osr_setting = &hp206c_osr_settings[data->pres_osr_index];
255 conv_cmd = HP206C_CMD_ADC_CVT |
256 osr_setting->osr_mask |
257 HP206C_CMD_ADC_CVT_CHNL_PT;
258 ret = hp206c_conv_and_read(indio_dev,
259 conv_cmd,
260 HP206C_CMD_READ_P,
261 osr_setting->pres_conv_time_us);
262 if (ret >= 0) {
263 *val = ret;
264 ret = IIO_VAL_INT;
265 }
266 break;
267 default:
268 ret = -EINVAL;
269 }
270 break;
271
272 case IIO_CHAN_INFO_SCALE:
273 switch (chan->type) {
274 case IIO_TEMP:
275 *val = 0;
276 *val2 = 10000;
277 ret = IIO_VAL_INT_PLUS_MICRO;
278 break;
279
280 case IIO_PRESSURE:
281 *val = 0;
282 *val2 = 1000;
283 ret = IIO_VAL_INT_PLUS_MICRO;
284 break;
285 default:
286 ret = -EINVAL;
287 }
288 break;
289
290 default:
291 ret = -EINVAL;
292 }
293
294 mutex_unlock(&data->mutex);
295 return ret;
296}
297
298static int hp206c_write_raw(struct iio_dev *indio_dev,
299 struct iio_chan_spec const *chan,
300 int val, int val2, long mask)
301{
302 int ret = 0;
303 struct hp206c_data *data = iio_priv(indio_dev);
304
305 if (mask != IIO_CHAN_INFO_OVERSAMPLING_RATIO)
306 return -EINVAL;
307 mutex_lock(&data->mutex);
308 switch (chan->type) {
309 case IIO_TEMP:
310 data->temp_osr_index = find_closest_descending(val,
311 hp206c_osr_rates, ARRAY_SIZE(hp206c_osr_rates));
312 break;
313 case IIO_PRESSURE:
314 data->pres_osr_index = find_closest_descending(val,
315 hp206c_osr_rates, ARRAY_SIZE(hp206c_osr_rates));
316 break;
317 default:
318 ret = -EINVAL;
319 }
320 mutex_unlock(&data->mutex);
321 return ret;
322}
323
324static const struct iio_chan_spec hp206c_channels[] = {
325 {
326 .type = IIO_TEMP,
327 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
328 BIT(IIO_CHAN_INFO_SCALE) |
329 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
330 },
331 {
332 .type = IIO_PRESSURE,
333 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
334 BIT(IIO_CHAN_INFO_SCALE) |
335 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
336 }
337};
338
339static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(hp206c_osr_rates_str);
340
341static struct attribute *hp206c_attributes[] = {
342 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
343 NULL,
344};
345
346static const struct attribute_group hp206c_attribute_group = {
347 .attrs = hp206c_attributes,
348};
349
350static const struct iio_info hp206c_info = {
351 .attrs = &hp206c_attribute_group,
352 .read_raw = hp206c_read_raw,
353 .write_raw = hp206c_write_raw,
354 .driver_module = THIS_MODULE,
355};
356
357static int hp206c_probe(struct i2c_client *client,
358 const struct i2c_device_id *id)
359{
360 struct iio_dev *indio_dev;
361 struct hp206c_data *data;
362 int ret;
363
364 if (!i2c_check_functionality(client->adapter,
365 I2C_FUNC_SMBUS_BYTE |
366 I2C_FUNC_SMBUS_BYTE_DATA |
367 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
368 dev_err(&client->dev, "Adapter does not support "
369 "all required i2c functionality\n");
370 return -ENODEV;
371 }
372
373 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
374 if (!indio_dev)
375 return -ENOMEM;
376
377 data = iio_priv(indio_dev);
378 data->client = client;
379 mutex_init(&data->mutex);
380
381 indio_dev->info = &hp206c_info;
382 indio_dev->name = id->name;
383 indio_dev->dev.parent = &client->dev;
384 indio_dev->modes = INDIO_DIRECT_MODE;
385 indio_dev->channels = hp206c_channels;
386 indio_dev->num_channels = ARRAY_SIZE(hp206c_channels);
387
388 i2c_set_clientdata(client, indio_dev);
389
390 /* Do a soft reset on probe */
391 ret = hp206c_soft_reset(indio_dev);
392 if (ret) {
393 dev_err(&client->dev, "Failed to reset on startup: %d\n", ret);
394 return -ENODEV;
395 }
396
397 return devm_iio_device_register(&client->dev, indio_dev);
398}
399
400static const struct i2c_device_id hp206c_id[] = {
401 {"hp206c"},
402 {}
403};
404
405#ifdef CONFIG_ACPI
406static const struct acpi_device_id hp206c_acpi_match[] = {
407 {"HOP206C", 0},
408 { },
409};
410MODULE_DEVICE_TABLE(acpi, hp206c_acpi_match);
411#endif
412
413static struct i2c_driver hp206c_driver = {
414 .probe = hp206c_probe,
415 .id_table = hp206c_id,
416 .driver = {
417 .name = "hp206c",
418 .acpi_match_table = ACPI_PTR(hp206c_acpi_match),
419 },
420};
421
422module_i2c_driver(hp206c_driver);
423
424MODULE_DESCRIPTION("HOPERF HP206C precision barometer and altimeter sensor");
425MODULE_AUTHOR("Leonard Crestez <leonard.crestez@intel.com>");
426MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/pressure/ms5611.h b/drivers/iio/pressure/ms5611.h
index 8b08e4b7e3a9..ccda63c5b3c3 100644
--- a/drivers/iio/pressure/ms5611.h
+++ b/drivers/iio/pressure/ms5611.h
@@ -16,15 +16,11 @@
16#include <linux/iio/iio.h> 16#include <linux/iio/iio.h>
17#include <linux/mutex.h> 17#include <linux/mutex.h>
18 18
19struct regulator;
20
19#define MS5611_RESET 0x1e 21#define MS5611_RESET 0x1e
20#define MS5611_READ_ADC 0x00 22#define MS5611_READ_ADC 0x00
21#define MS5611_READ_PROM_WORD 0xA0 23#define MS5611_READ_PROM_WORD 0xA0
22#define MS5611_START_TEMP_CONV 0x58
23#define MS5611_START_PRESSURE_CONV 0x48
24
25#define MS5611_CONV_TIME_MIN 9040
26#define MS5611_CONV_TIME_MAX 10000
27
28#define MS5611_PROM_WORDS_NB 8 24#define MS5611_PROM_WORDS_NB 8
29 25
30enum { 26enum {
@@ -39,16 +35,31 @@ struct ms5611_chip_info {
39 s32 *temp, s32 *pressure); 35 s32 *temp, s32 *pressure);
40}; 36};
41 37
38/*
39 * OverSampling Rate descriptor.
40 * Warning: cmd MUST be kept aligned on a word boundary (see
41 * m5611_spi_read_adc_temp_and_pressure in ms5611_spi.c).
42 */
43struct ms5611_osr {
44 unsigned long conv_usec;
45 u8 cmd;
46 unsigned short rate;
47};
48
42struct ms5611_state { 49struct ms5611_state {
43 void *client; 50 void *client;
44 struct mutex lock; 51 struct mutex lock;
45 52
53 const struct ms5611_osr *pressure_osr;
54 const struct ms5611_osr *temp_osr;
55
46 int (*reset)(struct device *dev); 56 int (*reset)(struct device *dev);
47 int (*read_prom_word)(struct device *dev, int index, u16 *word); 57 int (*read_prom_word)(struct device *dev, int index, u16 *word);
48 int (*read_adc_temp_and_pressure)(struct device *dev, 58 int (*read_adc_temp_and_pressure)(struct device *dev,
49 s32 *temp, s32 *pressure); 59 s32 *temp, s32 *pressure);
50 60
51 struct ms5611_chip_info *chip_info; 61 struct ms5611_chip_info *chip_info;
62 struct regulator *vdd;
52}; 63};
53 64
54int ms5611_probe(struct iio_dev *indio_dev, struct device *dev, 65int ms5611_probe(struct iio_dev *indio_dev, struct device *dev,
diff --git a/drivers/iio/pressure/ms5611_core.c b/drivers/iio/pressure/ms5611_core.c
index 992ad8d3b67a..76578b07bb6e 100644
--- a/drivers/iio/pressure/ms5611_core.c
+++ b/drivers/iio/pressure/ms5611_core.c
@@ -18,11 +18,44 @@
18#include <linux/delay.h> 18#include <linux/delay.h>
19#include <linux/regulator/consumer.h> 19#include <linux/regulator/consumer.h>
20 20
21#include <linux/iio/sysfs.h>
21#include <linux/iio/buffer.h> 22#include <linux/iio/buffer.h>
22#include <linux/iio/triggered_buffer.h> 23#include <linux/iio/triggered_buffer.h>
23#include <linux/iio/trigger_consumer.h> 24#include <linux/iio/trigger_consumer.h>
24#include "ms5611.h" 25#include "ms5611.h"
25 26
27#define MS5611_INIT_OSR(_cmd, _conv_usec, _rate) \
28 { .cmd = _cmd, .conv_usec = _conv_usec, .rate = _rate }
29
30static const struct ms5611_osr ms5611_avail_pressure_osr[] = {
31 MS5611_INIT_OSR(0x40, 600, 256),
32 MS5611_INIT_OSR(0x42, 1170, 512),
33 MS5611_INIT_OSR(0x44, 2280, 1024),
34 MS5611_INIT_OSR(0x46, 4540, 2048),
35 MS5611_INIT_OSR(0x48, 9040, 4096)
36};
37
38static const struct ms5611_osr ms5611_avail_temp_osr[] = {
39 MS5611_INIT_OSR(0x50, 600, 256),
40 MS5611_INIT_OSR(0x52, 1170, 512),
41 MS5611_INIT_OSR(0x54, 2280, 1024),
42 MS5611_INIT_OSR(0x56, 4540, 2048),
43 MS5611_INIT_OSR(0x58, 9040, 4096)
44};
45
46static const char ms5611_show_osr[] = "256 512 1024 2048 4096";
47
48static IIO_CONST_ATTR(oversampling_ratio_available, ms5611_show_osr);
49
50static struct attribute *ms5611_attributes[] = {
51 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
52 NULL,
53};
54
55static const struct attribute_group ms5611_attribute_group = {
56 .attrs = ms5611_attributes,
57};
58
26static bool ms5611_prom_is_valid(u16 *prom, size_t len) 59static bool ms5611_prom_is_valid(u16 *prom, size_t len)
27{ 60{
28 int i, j; 61 int i, j;
@@ -239,11 +272,70 @@ static int ms5611_read_raw(struct iio_dev *indio_dev,
239 default: 272 default:
240 return -EINVAL; 273 return -EINVAL;
241 } 274 }
275 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
276 if (chan->type != IIO_TEMP && chan->type != IIO_PRESSURE)
277 break;
278 mutex_lock(&st->lock);
279 if (chan->type == IIO_TEMP)
280 *val = (int)st->temp_osr->rate;
281 else
282 *val = (int)st->pressure_osr->rate;
283 mutex_unlock(&st->lock);
284 return IIO_VAL_INT;
242 } 285 }
243 286
244 return -EINVAL; 287 return -EINVAL;
245} 288}
246 289
290static const struct ms5611_osr *ms5611_find_osr(int rate,
291 const struct ms5611_osr *osr,
292 size_t count)
293{
294 unsigned int r;
295
296 for (r = 0; r < count; r++)
297 if ((unsigned short)rate == osr[r].rate)
298 break;
299 if (r >= count)
300 return NULL;
301 return &osr[r];
302}
303
304static int ms5611_write_raw(struct iio_dev *indio_dev,
305 struct iio_chan_spec const *chan,
306 int val, int val2, long mask)
307{
308 struct ms5611_state *st = iio_priv(indio_dev);
309 const struct ms5611_osr *osr = NULL;
310
311 if (mask != IIO_CHAN_INFO_OVERSAMPLING_RATIO)
312 return -EINVAL;
313
314 if (chan->type == IIO_TEMP)
315 osr = ms5611_find_osr(val, ms5611_avail_temp_osr,
316 ARRAY_SIZE(ms5611_avail_temp_osr));
317 else if (chan->type == IIO_PRESSURE)
318 osr = ms5611_find_osr(val, ms5611_avail_pressure_osr,
319 ARRAY_SIZE(ms5611_avail_pressure_osr));
320 if (!osr)
321 return -EINVAL;
322
323 mutex_lock(&st->lock);
324
325 if (iio_buffer_enabled(indio_dev)) {
326 mutex_unlock(&st->lock);
327 return -EBUSY;
328 }
329
330 if (chan->type == IIO_TEMP)
331 st->temp_osr = osr;
332 else
333 st->pressure_osr = osr;
334
335 mutex_unlock(&st->lock);
336 return 0;
337}
338
247static const unsigned long ms5611_scan_masks[] = {0x3, 0}; 339static const unsigned long ms5611_scan_masks[] = {0x3, 0};
248 340
249static struct ms5611_chip_info chip_info_tbl[] = { 341static struct ms5611_chip_info chip_info_tbl[] = {
@@ -259,7 +351,8 @@ static const struct iio_chan_spec ms5611_channels[] = {
259 { 351 {
260 .type = IIO_PRESSURE, 352 .type = IIO_PRESSURE,
261 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 353 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
262 BIT(IIO_CHAN_INFO_SCALE), 354 BIT(IIO_CHAN_INFO_SCALE) |
355 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
263 .scan_index = 0, 356 .scan_index = 0,
264 .scan_type = { 357 .scan_type = {
265 .sign = 's', 358 .sign = 's',
@@ -271,7 +364,8 @@ static const struct iio_chan_spec ms5611_channels[] = {
271 { 364 {
272 .type = IIO_TEMP, 365 .type = IIO_TEMP,
273 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 366 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
274 BIT(IIO_CHAN_INFO_SCALE), 367 BIT(IIO_CHAN_INFO_SCALE) |
368 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
275 .scan_index = 1, 369 .scan_index = 1,
276 .scan_type = { 370 .scan_type = {
277 .sign = 's', 371 .sign = 's',
@@ -285,40 +379,68 @@ static const struct iio_chan_spec ms5611_channels[] = {
285 379
286static const struct iio_info ms5611_info = { 380static const struct iio_info ms5611_info = {
287 .read_raw = &ms5611_read_raw, 381 .read_raw = &ms5611_read_raw,
382 .write_raw = &ms5611_write_raw,
383 .attrs = &ms5611_attribute_group,
288 .driver_module = THIS_MODULE, 384 .driver_module = THIS_MODULE,
289}; 385};
290 386
291static int ms5611_init(struct iio_dev *indio_dev) 387static int ms5611_init(struct iio_dev *indio_dev)
292{ 388{
293 int ret; 389 int ret;
294 struct regulator *vdd = devm_regulator_get(indio_dev->dev.parent, 390 struct ms5611_state *st = iio_priv(indio_dev);
295 "vdd");
296 391
297 /* Enable attached regulator if any. */ 392 /* Enable attached regulator if any. */
298 if (!IS_ERR(vdd)) { 393 st->vdd = devm_regulator_get(indio_dev->dev.parent, "vdd");
299 ret = regulator_enable(vdd); 394 if (!IS_ERR(st->vdd)) {
395 ret = regulator_enable(st->vdd);
300 if (ret) { 396 if (ret) {
301 dev_err(indio_dev->dev.parent, 397 dev_err(indio_dev->dev.parent,
302 "failed to enable Vdd supply: %d\n", ret); 398 "failed to enable Vdd supply: %d\n", ret);
303 return ret; 399 return ret;
304 } 400 }
401 } else {
402 ret = PTR_ERR(st->vdd);
403 if (ret != -ENODEV)
404 return ret;
305 } 405 }
306 406
307 ret = ms5611_reset(indio_dev); 407 ret = ms5611_reset(indio_dev);
308 if (ret < 0) 408 if (ret < 0)
309 return ret; 409 goto err_regulator_disable;
310 410
311 return ms5611_read_prom(indio_dev); 411 ret = ms5611_read_prom(indio_dev);
412 if (ret < 0)
413 goto err_regulator_disable;
414
415 return 0;
416
417err_regulator_disable:
418 if (!IS_ERR_OR_NULL(st->vdd))
419 regulator_disable(st->vdd);
420 return ret;
421}
422
423static void ms5611_fini(const struct iio_dev *indio_dev)
424{
425 const struct ms5611_state *st = iio_priv(indio_dev);
426
427 if (!IS_ERR_OR_NULL(st->vdd))
428 regulator_disable(st->vdd);
312} 429}
313 430
314int ms5611_probe(struct iio_dev *indio_dev, struct device *dev, 431int ms5611_probe(struct iio_dev *indio_dev, struct device *dev,
315 const char *name, int type) 432 const char *name, int type)
316{ 433{
317 int ret; 434 int ret;
318 struct ms5611_state *st = iio_priv(indio_dev); 435 struct ms5611_state *st = iio_priv(indio_dev);
319 436
320 mutex_init(&st->lock); 437 mutex_init(&st->lock);
321 st->chip_info = &chip_info_tbl[type]; 438 st->chip_info = &chip_info_tbl[type];
439 st->temp_osr =
440 &ms5611_avail_temp_osr[ARRAY_SIZE(ms5611_avail_temp_osr) - 1];
441 st->pressure_osr =
442 &ms5611_avail_pressure_osr[ARRAY_SIZE(ms5611_avail_pressure_osr)
443 - 1];
322 indio_dev->dev.parent = dev; 444 indio_dev->dev.parent = dev;
323 indio_dev->name = name; 445 indio_dev->name = name;
324 indio_dev->info = &ms5611_info; 446 indio_dev->info = &ms5611_info;
@@ -335,7 +457,7 @@ int ms5611_probe(struct iio_dev *indio_dev, struct device *dev,
335 ms5611_trigger_handler, NULL); 457 ms5611_trigger_handler, NULL);
336 if (ret < 0) { 458 if (ret < 0) {
337 dev_err(dev, "iio triggered buffer setup failed\n"); 459 dev_err(dev, "iio triggered buffer setup failed\n");
338 return ret; 460 goto err_fini;
339 } 461 }
340 462
341 ret = iio_device_register(indio_dev); 463 ret = iio_device_register(indio_dev);
@@ -348,7 +470,8 @@ int ms5611_probe(struct iio_dev *indio_dev, struct device *dev,
348 470
349err_buffer_cleanup: 471err_buffer_cleanup:
350 iio_triggered_buffer_cleanup(indio_dev); 472 iio_triggered_buffer_cleanup(indio_dev);
351 473err_fini:
474 ms5611_fini(indio_dev);
352 return ret; 475 return ret;
353} 476}
354EXPORT_SYMBOL(ms5611_probe); 477EXPORT_SYMBOL(ms5611_probe);
@@ -357,6 +480,7 @@ int ms5611_remove(struct iio_dev *indio_dev)
357{ 480{
358 iio_device_unregister(indio_dev); 481 iio_device_unregister(indio_dev);
359 iio_triggered_buffer_cleanup(indio_dev); 482 iio_triggered_buffer_cleanup(indio_dev);
483 ms5611_fini(indio_dev);
360 484
361 return 0; 485 return 0;
362} 486}
diff --git a/drivers/iio/pressure/ms5611_i2c.c b/drivers/iio/pressure/ms5611_i2c.c
index 7f6fc8eee922..55fb5fc0b6ea 100644
--- a/drivers/iio/pressure/ms5611_i2c.c
+++ b/drivers/iio/pressure/ms5611_i2c.c
@@ -17,6 +17,7 @@
17#include <linux/delay.h> 17#include <linux/delay.h>
18#include <linux/i2c.h> 18#include <linux/i2c.h>
19#include <linux/module.h> 19#include <linux/module.h>
20#include <linux/of_device.h>
20 21
21#include "ms5611.h" 22#include "ms5611.h"
22 23
@@ -62,23 +63,23 @@ static int ms5611_i2c_read_adc_temp_and_pressure(struct device *dev,
62{ 63{
63 int ret; 64 int ret;
64 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev)); 65 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
66 const struct ms5611_osr *osr = st->temp_osr;
65 67
66 ret = i2c_smbus_write_byte(st->client, MS5611_START_TEMP_CONV); 68 ret = i2c_smbus_write_byte(st->client, osr->cmd);
67 if (ret < 0) 69 if (ret < 0)
68 return ret; 70 return ret;
69 71
70 usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX); 72 usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
71
72 ret = ms5611_i2c_read_adc(st, temp); 73 ret = ms5611_i2c_read_adc(st, temp);
73 if (ret < 0) 74 if (ret < 0)
74 return ret; 75 return ret;
75 76
76 ret = i2c_smbus_write_byte(st->client, MS5611_START_PRESSURE_CONV); 77 osr = st->pressure_osr;
78 ret = i2c_smbus_write_byte(st->client, osr->cmd);
77 if (ret < 0) 79 if (ret < 0)
78 return ret; 80 return ret;
79 81
80 usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX); 82 usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
81
82 return ms5611_i2c_read_adc(st, pressure); 83 return ms5611_i2c_read_adc(st, pressure);
83} 84}
84 85
@@ -113,6 +114,17 @@ static int ms5611_i2c_remove(struct i2c_client *client)
113 return ms5611_remove(i2c_get_clientdata(client)); 114 return ms5611_remove(i2c_get_clientdata(client));
114} 115}
115 116
117#if defined(CONFIG_OF)
118static const struct of_device_id ms5611_i2c_matches[] = {
119 { .compatible = "meas,ms5611" },
120 { .compatible = "ms5611" },
121 { .compatible = "meas,ms5607" },
122 { .compatible = "ms5607" },
123 { }
124};
125MODULE_DEVICE_TABLE(of, ms5611_i2c_matches);
126#endif
127
116static const struct i2c_device_id ms5611_id[] = { 128static const struct i2c_device_id ms5611_id[] = {
117 { "ms5611", MS5611 }, 129 { "ms5611", MS5611 },
118 { "ms5607", MS5607 }, 130 { "ms5607", MS5607 },
@@ -123,6 +135,7 @@ MODULE_DEVICE_TABLE(i2c, ms5611_id);
123static struct i2c_driver ms5611_driver = { 135static struct i2c_driver ms5611_driver = {
124 .driver = { 136 .driver = {
125 .name = "ms5611", 137 .name = "ms5611",
138 .of_match_table = of_match_ptr(ms5611_i2c_matches)
126 }, 139 },
127 .id_table = ms5611_id, 140 .id_table = ms5611_id,
128 .probe = ms5611_i2c_probe, 141 .probe = ms5611_i2c_probe,
diff --git a/drivers/iio/pressure/ms5611_spi.c b/drivers/iio/pressure/ms5611_spi.c
index 5cc009e85f0e..932e05001e1a 100644
--- a/drivers/iio/pressure/ms5611_spi.c
+++ b/drivers/iio/pressure/ms5611_spi.c
@@ -12,6 +12,7 @@
12#include <linux/delay.h> 12#include <linux/delay.h>
13#include <linux/module.h> 13#include <linux/module.h>
14#include <linux/spi/spi.h> 14#include <linux/spi/spi.h>
15#include <linux/of_device.h>
15 16
16#include "ms5611.h" 17#include "ms5611.h"
17 18
@@ -55,28 +56,29 @@ static int ms5611_spi_read_adc(struct device *dev, s32 *val)
55static int ms5611_spi_read_adc_temp_and_pressure(struct device *dev, 56static int ms5611_spi_read_adc_temp_and_pressure(struct device *dev,
56 s32 *temp, s32 *pressure) 57 s32 *temp, s32 *pressure)
57{ 58{
58 u8 cmd;
59 int ret; 59 int ret;
60 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev)); 60 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
61 const struct ms5611_osr *osr = st->temp_osr;
61 62
62 cmd = MS5611_START_TEMP_CONV; 63 /*
63 ret = spi_write_then_read(st->client, &cmd, 1, NULL, 0); 64 * Warning: &osr->cmd MUST be aligned on a word boundary since used as
65 * 2nd argument (void*) of spi_write_then_read.
66 */
67 ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
64 if (ret < 0) 68 if (ret < 0)
65 return ret; 69 return ret;
66 70
67 usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX); 71 usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
68
69 ret = ms5611_spi_read_adc(dev, temp); 72 ret = ms5611_spi_read_adc(dev, temp);
70 if (ret < 0) 73 if (ret < 0)
71 return ret; 74 return ret;
72 75
73 cmd = MS5611_START_PRESSURE_CONV; 76 osr = st->pressure_osr;
74 ret = spi_write_then_read(st->client, &cmd, 1, NULL, 0); 77 ret = spi_write_then_read(st->client, &osr->cmd, 1, NULL, 0);
75 if (ret < 0) 78 if (ret < 0)
76 return ret; 79 return ret;
77 80
78 usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX); 81 usleep_range(osr->conv_usec, osr->conv_usec + (osr->conv_usec / 10UL));
79
80 return ms5611_spi_read_adc(dev, pressure); 82 return ms5611_spi_read_adc(dev, pressure);
81} 83}
82 84
@@ -106,7 +108,7 @@ static int ms5611_spi_probe(struct spi_device *spi)
106 st->client = spi; 108 st->client = spi;
107 109
108 return ms5611_probe(indio_dev, &spi->dev, spi_get_device_id(spi)->name, 110 return ms5611_probe(indio_dev, &spi->dev, spi_get_device_id(spi)->name,
109 spi_get_device_id(spi)->driver_data); 111 spi_get_device_id(spi)->driver_data);
110} 112}
111 113
112static int ms5611_spi_remove(struct spi_device *spi) 114static int ms5611_spi_remove(struct spi_device *spi)
@@ -114,6 +116,17 @@ static int ms5611_spi_remove(struct spi_device *spi)
114 return ms5611_remove(spi_get_drvdata(spi)); 116 return ms5611_remove(spi_get_drvdata(spi));
115} 117}
116 118
119#if defined(CONFIG_OF)
120static const struct of_device_id ms5611_spi_matches[] = {
121 { .compatible = "meas,ms5611" },
122 { .compatible = "ms5611" },
123 { .compatible = "meas,ms5607" },
124 { .compatible = "ms5607" },
125 { }
126};
127MODULE_DEVICE_TABLE(of, ms5611_spi_matches);
128#endif
129
117static const struct spi_device_id ms5611_id[] = { 130static const struct spi_device_id ms5611_id[] = {
118 { "ms5611", MS5611 }, 131 { "ms5611", MS5611 },
119 { "ms5607", MS5607 }, 132 { "ms5607", MS5607 },
@@ -124,6 +137,7 @@ MODULE_DEVICE_TABLE(spi, ms5611_id);
124static struct spi_driver ms5611_driver = { 137static struct spi_driver ms5611_driver = {
125 .driver = { 138 .driver = {
126 .name = "ms5611", 139 .name = "ms5611",
140 .of_match_table = of_match_ptr(ms5611_spi_matches)
127 }, 141 },
128 .id_table = ms5611_id, 142 .id_table = ms5611_id,
129 .probe = ms5611_spi_probe, 143 .probe = ms5611_spi_probe,
diff --git a/drivers/iio/pressure/st_pressure_core.c b/drivers/iio/pressure/st_pressure_core.c
index 172393ad34af..9e9b72a8f18f 100644
--- a/drivers/iio/pressure/st_pressure_core.c
+++ b/drivers/iio/pressure/st_pressure_core.c
@@ -64,6 +64,8 @@
64#define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20 64#define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20
65#define ST_PRESS_LPS331AP_IHL_IRQ_ADDR 0x22 65#define ST_PRESS_LPS331AP_IHL_IRQ_ADDR 0x22
66#define ST_PRESS_LPS331AP_IHL_IRQ_MASK 0x80 66#define ST_PRESS_LPS331AP_IHL_IRQ_MASK 0x80
67#define ST_PRESS_LPS331AP_OD_IRQ_ADDR 0x22
68#define ST_PRESS_LPS331AP_OD_IRQ_MASK 0x40
67#define ST_PRESS_LPS331AP_MULTIREAD_BIT true 69#define ST_PRESS_LPS331AP_MULTIREAD_BIT true
68#define ST_PRESS_LPS331AP_TEMP_OFFSET 42500 70#define ST_PRESS_LPS331AP_TEMP_OFFSET 42500
69 71
@@ -104,6 +106,8 @@
104#define ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK 0x10 106#define ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK 0x10
105#define ST_PRESS_LPS25H_IHL_IRQ_ADDR 0x22 107#define ST_PRESS_LPS25H_IHL_IRQ_ADDR 0x22
106#define ST_PRESS_LPS25H_IHL_IRQ_MASK 0x80 108#define ST_PRESS_LPS25H_IHL_IRQ_MASK 0x80
109#define ST_PRESS_LPS25H_OD_IRQ_ADDR 0x22
110#define ST_PRESS_LPS25H_OD_IRQ_MASK 0x40
107#define ST_PRESS_LPS25H_MULTIREAD_BIT true 111#define ST_PRESS_LPS25H_MULTIREAD_BIT true
108#define ST_PRESS_LPS25H_TEMP_OFFSET 42500 112#define ST_PRESS_LPS25H_TEMP_OFFSET 42500
109#define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28 113#define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28
@@ -226,6 +230,9 @@ static const struct st_sensor_settings st_press_sensors_settings[] = {
226 .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK, 230 .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK,
227 .addr_ihl = ST_PRESS_LPS331AP_IHL_IRQ_ADDR, 231 .addr_ihl = ST_PRESS_LPS331AP_IHL_IRQ_ADDR,
228 .mask_ihl = ST_PRESS_LPS331AP_IHL_IRQ_MASK, 232 .mask_ihl = ST_PRESS_LPS331AP_IHL_IRQ_MASK,
233 .addr_od = ST_PRESS_LPS331AP_OD_IRQ_ADDR,
234 .mask_od = ST_PRESS_LPS331AP_OD_IRQ_MASK,
235 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
229 }, 236 },
230 .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT, 237 .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT,
231 .bootime = 2, 238 .bootime = 2,
@@ -312,6 +319,9 @@ static const struct st_sensor_settings st_press_sensors_settings[] = {
312 .mask_int2 = ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK, 319 .mask_int2 = ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK,
313 .addr_ihl = ST_PRESS_LPS25H_IHL_IRQ_ADDR, 320 .addr_ihl = ST_PRESS_LPS25H_IHL_IRQ_ADDR,
314 .mask_ihl = ST_PRESS_LPS25H_IHL_IRQ_MASK, 321 .mask_ihl = ST_PRESS_LPS25H_IHL_IRQ_MASK,
322 .addr_od = ST_PRESS_LPS25H_OD_IRQ_ADDR,
323 .mask_od = ST_PRESS_LPS25H_OD_IRQ_MASK,
324 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
315 }, 325 },
316 .multi_read_bit = ST_PRESS_LPS25H_MULTIREAD_BIT, 326 .multi_read_bit = ST_PRESS_LPS25H_MULTIREAD_BIT,
317 .bootime = 2, 327 .bootime = 2,