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

Merge tag 'iio-for-3.18a' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into work-next

Jonathan writes:

1st round of new IIO drivers, functionality and cleanups for the 3.18 cycle.

Maintainer Updates

* Add 3 designated reviewers for IIO. Lars, Peter and Hartmut have been
actively reviewing a lot of patches for a while now so this reflects
the status quo. These three are probably the only reason I keep
my head above the water!

New drivers and device support

* max5821 DAC
* Rockchip SARADC
* TI ADC128S052 ADC
* BMC150 Accelerometer
* exynos ADC driver gains support for s3c24xx and s3c64xx parts.
* kxcjk-1013 gainst range control and runtime PM support to drive
down it's power usage.

Driver removals

* Drop ad5930, ad99850, ad9852, ad9910 and ad9951 drivers on the simple
basis that they drivers just provided a register write function with
no compliant user space ABI whatsoever. Much better to drop them and
start again for these in the fullness of time.

Core Enhancements

* Join together neighbouring elements in the demux units that feeds
the binary interfaces. This cuts down on the number of individual
copies needed when splitting out individual channels from the incoming
channel scans.
* Other demux related cleanups such as using roundup instead of a local
implementation.

Cleanups

* Drop an unnecessary double setting of the owner field in xilinx adc.
* Some more patches to use managed (devm) interfaces to cut down on
complexity of removal code.
* adis16060 coding style fixlets.
* Fix some incorrect error returns in the Xilinx ADC driver.
* Coding style fixlets for various accelerometer drivers.
* Some sparse warning fixes to do with endianness and sign of variables.
* Fix an incorrect and entirely pointless use of sizeof on a dynamic pointer
in hid-sensor-magn-3d by dropping the relevant code.

+2757 -1271
+7
Documentation/ABI/testing/sysfs-bus-iio-accel-bmc150
··· 1 + What: /sys/bus/iio/devices/triggerX/name = "bmc150_accel-any-motion-devX" 2 + KernelVersion: 3.17 3 + Contact: linux-iio@vger.kernel.org 4 + Description: 5 + The BMC150 accelerometer kernel module provides an additional trigger, 6 + which sets driver in a mode, where data is pushed to the buffer 7 + only when there is any motion.
+15 -3
Documentation/devicetree/bindings/arm/samsung/exynos-adc.txt
··· 11 11 12 12 Required properties: 13 13 - compatible: Must be "samsung,exynos-adc-v1" 14 - for exynos4412/5250 controllers. 14 + for exynos4412/5250 and s5pv210 controllers. 15 15 Must be "samsung,exynos-adc-v2" for 16 16 future controllers. 17 17 Must be "samsung,exynos3250-adc" for 18 18 controllers compatible with ADC of Exynos3250. 19 - - reg: Contains ADC register address range (base address and 20 - length) and the address of the phy enable register. 19 + Must be "samsung,s3c2410-adc" for 20 + the ADC in s3c2410 and compatibles 21 + Must be "samsung,s3c2416-adc" for 22 + the ADC in s3c2416 and compatibles 23 + Must be "samsung,s3c2440-adc" for 24 + the ADC in s3c2440 and compatibles 25 + Must be "samsung,s3c2443-adc" for 26 + the ADC in s3c2443 and compatibles 27 + Must be "samsung,s3c6410-adc" for 28 + the ADC in s3c6410 and compatibles 29 + - reg: List of ADC register address range 30 + - The base address and range of ADC register 31 + - The base address and range of ADC_PHY register (every 32 + SoC except for s3c24xx/s3c64xx ADC) 21 33 - interrupts: Contains the interrupt information for the timer. The 22 34 format is being dependent on which interrupt controller 23 35 the Samsung device uses.
+14
Documentation/devicetree/bindings/iio/dac/max5821.txt
··· 1 + Maxim max5821 DAC device driver 2 + 3 + Required properties: 4 + - compatible: Must be "maxim,max5821" 5 + - reg: Should contain the DAC I2C address 6 + - vref-supply: Phandle to the vref power supply 7 + 8 + Example: 9 + 10 + max5821@38 { 11 + compatible = "maxim,max5821"; 12 + reg = <0x38>; 13 + vref-supply = <&reg_max5821>; 14 + };
+14
MAINTAINERS
··· 685 685 W: http://blackfin.uclinux.org/ 686 686 S: Supported 687 687 F: sound/soc/blackfin/* 688 + 689 + ANALOG DEVICES INC IIO DRIVERS 690 + M: Lars-Peter Clausen <lars@metafoo.de> 691 + M: Michael Hennerich <Michael.Hennerich@analog.com> 692 + W: http://wiki.analog.com/ 693 + W: http://ez.analog.com/community/linux-device-drivers 694 + S: Supported 695 + F: drivers/iio/*/ad* 696 + X: drivers/iio/*/adjd* 697 + F: drivers/staging/iio/*/ad* 698 + F: staging/iio/trigger/iio-trig-bfin-timer.c 688 699 689 700 AOA (Apple Onboard Audio) ALSA DRIVER 690 701 M: Johannes Berg <johannes@sipsolutions.net> ··· 4591 4580 4592 4581 IIO SUBSYSTEM AND DRIVERS 4593 4582 M: Jonathan Cameron <jic23@kernel.org> 4583 + R: Hartmut Knaack <knaack.h@gmx.de> 4584 + R: Lars-Peter Clausen <lars@metafoo.de> 4585 + R: Peter Meerwald <pmeerw@pmeerw.net> 4594 4586 L: linux-iio@vger.kernel.org 4595 4587 S: Maintained 4596 4588 F: drivers/iio/
+11 -22
drivers/hid/hid-sensor-hub.c
··· 604 604 ret = -EINVAL; 605 605 goto err_stop_hw; 606 606 } 607 - sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt * 608 - sizeof(struct mfd_cell), 609 - GFP_KERNEL); 607 + sd->hid_sensor_hub_client_devs = devm_kzalloc(&hdev->dev, dev_cnt * 608 + sizeof(struct mfd_cell), 609 + GFP_KERNEL); 610 610 if (sd->hid_sensor_hub_client_devs == NULL) { 611 611 hid_err(hdev, "Failed to allocate memory for mfd cells\n"); 612 612 ret = -ENOMEM; ··· 618 618 619 619 if (collection->type == HID_COLLECTION_PHYSICAL) { 620 620 621 - hsdev = kzalloc(sizeof(*hsdev), GFP_KERNEL); 621 + hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev), 622 + GFP_KERNEL); 622 623 if (!hsdev) { 623 624 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n"); 624 625 ret = -ENOMEM; 625 - goto err_no_mem; 626 + goto err_stop_hw; 626 627 } 627 628 hsdev->hdev = hdev; 628 629 hsdev->vendor_id = hdev->vendor; ··· 632 631 if (last_hsdev) 633 632 last_hsdev->end_collection_index = i; 634 633 last_hsdev = hsdev; 635 - name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x", 636 - collection->usage); 634 + name = devm_kasprintf(&hdev->dev, GFP_KERNEL, 635 + "HID-SENSOR-%x", 636 + collection->usage); 637 637 if (name == NULL) { 638 638 hid_err(hdev, "Failed MFD device name\n"); 639 639 ret = -ENOMEM; 640 - kfree(hsdev); 641 - goto err_no_mem; 640 + goto err_stop_hw; 642 641 } 643 642 sd->hid_sensor_hub_client_devs[ 644 643 sd->hid_sensor_client_cnt].id = ··· 662 661 ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs, 663 662 sd->hid_sensor_client_cnt, NULL, 0, NULL); 664 663 if (ret < 0) 665 - goto err_no_mem; 664 + goto err_stop_hw; 666 665 667 666 return ret; 668 667 669 - err_no_mem: 670 - for (i = 0; i < sd->hid_sensor_client_cnt; ++i) { 671 - kfree(sd->hid_sensor_hub_client_devs[i].name); 672 - kfree(sd->hid_sensor_hub_client_devs[i].platform_data); 673 - } 674 - kfree(sd->hid_sensor_hub_client_devs); 675 668 err_stop_hw: 676 669 hid_hw_stop(hdev); 677 670 ··· 676 681 { 677 682 struct sensor_hub_data *data = hid_get_drvdata(hdev); 678 683 unsigned long flags; 679 - int i; 680 684 681 685 hid_dbg(hdev, " hardware removed\n"); 682 686 hid_hw_close(hdev); ··· 685 691 complete(&data->pending.ready); 686 692 spin_unlock_irqrestore(&data->lock, flags); 687 693 mfd_remove_devices(&hdev->dev); 688 - for (i = 0; i < data->hid_sensor_client_cnt; ++i) { 689 - kfree(data->hid_sensor_hub_client_devs[i].name); 690 - kfree(data->hid_sensor_hub_client_devs[i].platform_data); 691 - } 692 - kfree(data->hid_sensor_hub_client_devs); 693 694 hid_set_drvdata(hdev, NULL); 694 695 mutex_destroy(&data->mutex); 695 696 }
+13
drivers/iio/accel/Kconfig
··· 17 17 To compile this driver as a module, choose M here: the 18 18 module will be called bma180. 19 19 20 + config BMC150_ACCEL 21 + tristate "Bosch BMC150 Accelerometer Driver" 22 + depends on I2C 23 + select IIO_BUFFER 24 + select IIO_TRIGGERED_BUFFER 25 + help 26 + Say yes here to build support for the Bosch BMC150 accelerometer. 27 + Currently this only supports the device via an i2c interface. 28 + 29 + This is a combo module with both accelerometer and magnetometer. 30 + This driver is only implementing accelerometer part, which has 31 + its own address and register map. 32 + 20 33 config HID_SENSOR_ACCEL_3D 21 34 depends on HID_SENSOR_HUB 22 35 select IIO_BUFFER
+1
drivers/iio/accel/Makefile
··· 4 4 5 5 # When adding new entries keep the list in alphabetical order 6 6 obj-$(CONFIG_BMA180) += bma180.o 7 + obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel.o 7 8 obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o 8 9 obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o 9 10 obj-$(CONFIG_KXSD9) += kxsd9.o
+1307
drivers/iio/accel/bmc150-accel.c
··· 1 + /* 2 + * BMC150 3-axis accelerometer driver 3 + * Copyright (c) 2014, Intel Corporation. 4 + * 5 + * This program is free software; you can redistribute it and/or modify it 6 + * under the terms and conditions of the GNU General Public License, 7 + * version 2, as published by the Free Software Foundation. 8 + * 9 + * This program is distributed in the hope it will be useful, but WITHOUT 10 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 + * more details. 13 + */ 14 + 15 + #include <linux/module.h> 16 + #include <linux/i2c.h> 17 + #include <linux/interrupt.h> 18 + #include <linux/delay.h> 19 + #include <linux/slab.h> 20 + #include <linux/acpi.h> 21 + #include <linux/gpio/consumer.h> 22 + #include <linux/pm.h> 23 + #include <linux/pm_runtime.h> 24 + #include <linux/iio/iio.h> 25 + #include <linux/iio/sysfs.h> 26 + #include <linux/iio/buffer.h> 27 + #include <linux/iio/events.h> 28 + #include <linux/iio/trigger.h> 29 + #include <linux/iio/trigger_consumer.h> 30 + #include <linux/iio/triggered_buffer.h> 31 + 32 + #define BMC150_ACCEL_DRV_NAME "bmc150_accel" 33 + #define BMC150_ACCEL_IRQ_NAME "bmc150_accel_event" 34 + #define BMC150_ACCEL_GPIO_NAME "bmc150_accel_int" 35 + 36 + #define BMC150_ACCEL_REG_CHIP_ID 0x00 37 + #define BMC150_ACCEL_CHIP_ID_VAL 0xFA 38 + 39 + #define BMC150_ACCEL_REG_INT_STATUS_2 0x0B 40 + #define BMC150_ACCEL_ANY_MOTION_MASK 0x07 41 + #define BMC150_ACCEL_ANY_MOTION_BIT_SIGN BIT(3) 42 + 43 + #define BMC150_ACCEL_REG_PMU_LPW 0x11 44 + #define BMC150_ACCEL_PMU_MODE_MASK 0xE0 45 + #define BMC150_ACCEL_PMU_MODE_SHIFT 5 46 + #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_MASK 0x17 47 + #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT 1 48 + 49 + #define BMC150_ACCEL_REG_PMU_RANGE 0x0F 50 + 51 + #define BMC150_ACCEL_DEF_RANGE_2G 0x03 52 + #define BMC150_ACCEL_DEF_RANGE_4G 0x05 53 + #define BMC150_ACCEL_DEF_RANGE_8G 0x08 54 + #define BMC150_ACCEL_DEF_RANGE_16G 0x0C 55 + 56 + /* Default BW: 125Hz */ 57 + #define BMC150_ACCEL_REG_PMU_BW 0x10 58 + #define BMC150_ACCEL_DEF_BW 125 59 + 60 + #define BMC150_ACCEL_REG_INT_MAP_0 0x19 61 + #define BMC150_ACCEL_INT_MAP_0_BIT_SLOPE BIT(2) 62 + 63 + #define BMC150_ACCEL_REG_INT_MAP_1 0x1A 64 + #define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0) 65 + 66 + #define BMC150_ACCEL_REG_INT_RST_LATCH 0x21 67 + #define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80 68 + #define BMC150_ACCEL_INT_MODE_LATCH_INT 0x0F 69 + #define BMC150_ACCEL_INT_MODE_NON_LATCH_INT 0x00 70 + 71 + #define BMC150_ACCEL_REG_INT_EN_0 0x16 72 + #define BMC150_ACCEL_INT_EN_BIT_SLP_X BIT(0) 73 + #define BMC150_ACCEL_INT_EN_BIT_SLP_Y BIT(1) 74 + #define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2) 75 + 76 + #define BMC150_ACCEL_REG_INT_EN_1 0x17 77 + #define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4) 78 + 79 + #define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20 80 + #define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0) 81 + 82 + #define BMC150_ACCEL_REG_INT_5 0x27 83 + #define BMC150_ACCEL_SLOPE_DUR_MASK 0x03 84 + 85 + #define BMC150_ACCEL_REG_INT_6 0x28 86 + #define BMC150_ACCEL_SLOPE_THRES_MASK 0xFF 87 + 88 + /* Slope duration in terms of number of samples */ 89 + #define BMC150_ACCEL_DEF_SLOPE_DURATION 2 90 + /* in terms of multiples of g's/LSB, based on range */ 91 + #define BMC150_ACCEL_DEF_SLOPE_THRESHOLD 5 92 + 93 + #define BMC150_ACCEL_REG_XOUT_L 0x02 94 + 95 + #define BMC150_ACCEL_MAX_STARTUP_TIME_MS 100 96 + 97 + /* Sleep Duration values */ 98 + #define BMC150_ACCEL_SLEEP_500_MICRO 0x05 99 + #define BMC150_ACCEL_SLEEP_1_MS 0x06 100 + #define BMC150_ACCEL_SLEEP_2_MS 0x07 101 + #define BMC150_ACCEL_SLEEP_4_MS 0x08 102 + #define BMC150_ACCEL_SLEEP_6_MS 0x09 103 + #define BMC150_ACCEL_SLEEP_10_MS 0x0A 104 + #define BMC150_ACCEL_SLEEP_25_MS 0x0B 105 + #define BMC150_ACCEL_SLEEP_50_MS 0x0C 106 + #define BMC150_ACCEL_SLEEP_100_MS 0x0D 107 + #define BMC150_ACCEL_SLEEP_500_MS 0x0E 108 + #define BMC150_ACCEL_SLEEP_1_SEC 0x0F 109 + 110 + #define BMC150_ACCEL_REG_TEMP 0x08 111 + #define BMC150_ACCEL_TEMP_CENTER_VAL 24 112 + 113 + #define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2)) 114 + #define BMC150_AUTO_SUSPEND_DELAY_MS 2000 115 + 116 + enum bmc150_accel_axis { 117 + AXIS_X, 118 + AXIS_Y, 119 + AXIS_Z, 120 + }; 121 + 122 + enum bmc150_power_modes { 123 + BMC150_ACCEL_SLEEP_MODE_NORMAL, 124 + BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 125 + BMC150_ACCEL_SLEEP_MODE_LPM, 126 + BMC150_ACCEL_SLEEP_MODE_SUSPEND = 0x04, 127 + }; 128 + 129 + struct bmc150_accel_data { 130 + struct i2c_client *client; 131 + struct iio_trigger *dready_trig; 132 + struct iio_trigger *motion_trig; 133 + struct mutex mutex; 134 + s16 buffer[8]; 135 + u8 bw_bits; 136 + u32 slope_dur; 137 + u32 slope_thres; 138 + u32 range; 139 + int ev_enable_state; 140 + bool dready_trigger_on; 141 + bool motion_trigger_on; 142 + int64_t timestamp; 143 + }; 144 + 145 + static const struct { 146 + int val; 147 + int val2; 148 + u8 bw_bits; 149 + } bmc150_accel_samp_freq_table[] = { {7, 810000, 0x08}, 150 + {15, 630000, 0x09}, 151 + {31, 250000, 0x0A}, 152 + {62, 500000, 0x0B}, 153 + {125, 0, 0x0C}, 154 + {250, 0, 0x0D}, 155 + {500, 0, 0x0E}, 156 + {1000, 0, 0x0F} }; 157 + 158 + static const struct { 159 + int bw_bits; 160 + int msec; 161 + } bmc150_accel_sample_upd_time[] = { {0x08, 64}, 162 + {0x09, 32}, 163 + {0x0A, 16}, 164 + {0x0B, 8}, 165 + {0x0C, 4}, 166 + {0x0D, 2}, 167 + {0x0E, 1}, 168 + {0x0F, 1} }; 169 + 170 + static const struct { 171 + int scale; 172 + int range; 173 + } bmc150_accel_scale_table[] = { {9610, BMC150_ACCEL_DEF_RANGE_2G}, 174 + {19122, BMC150_ACCEL_DEF_RANGE_4G}, 175 + {38344, BMC150_ACCEL_DEF_RANGE_8G}, 176 + {77057, BMC150_ACCEL_DEF_RANGE_16G} }; 177 + 178 + static const struct { 179 + int sleep_dur; 180 + int reg_value; 181 + } bmc150_accel_sleep_value_table[] = { {0, 0}, 182 + {500, BMC150_ACCEL_SLEEP_500_MICRO}, 183 + {1000, BMC150_ACCEL_SLEEP_1_MS}, 184 + {2000, BMC150_ACCEL_SLEEP_2_MS}, 185 + {4000, BMC150_ACCEL_SLEEP_4_MS}, 186 + {6000, BMC150_ACCEL_SLEEP_6_MS}, 187 + {10000, BMC150_ACCEL_SLEEP_10_MS}, 188 + {25000, BMC150_ACCEL_SLEEP_25_MS}, 189 + {50000, BMC150_ACCEL_SLEEP_50_MS}, 190 + {100000, BMC150_ACCEL_SLEEP_100_MS}, 191 + {500000, BMC150_ACCEL_SLEEP_500_MS}, 192 + {1000000, BMC150_ACCEL_SLEEP_1_SEC} }; 193 + 194 + 195 + static int bmc150_accel_set_mode(struct bmc150_accel_data *data, 196 + enum bmc150_power_modes mode, 197 + int dur_us) 198 + { 199 + int i; 200 + int ret; 201 + u8 lpw_bits; 202 + int dur_val = -1; 203 + 204 + if (dur_us > 0) { 205 + for (i = 0; i < ARRAY_SIZE(bmc150_accel_sleep_value_table); 206 + ++i) { 207 + if (bmc150_accel_sleep_value_table[i].sleep_dur == 208 + dur_us) 209 + dur_val = 210 + bmc150_accel_sleep_value_table[i].reg_value; 211 + } 212 + } else 213 + dur_val = 0; 214 + 215 + if (dur_val < 0) 216 + return -EINVAL; 217 + 218 + lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT; 219 + lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT); 220 + 221 + dev_dbg(&data->client->dev, "Set Mode bits %x\n", lpw_bits); 222 + 223 + ret = i2c_smbus_write_byte_data(data->client, 224 + BMC150_ACCEL_REG_PMU_LPW, lpw_bits); 225 + if (ret < 0) { 226 + dev_err(&data->client->dev, "Error writing reg_pmu_lpw\n"); 227 + return ret; 228 + } 229 + 230 + return 0; 231 + } 232 + 233 + static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val, 234 + int val2) 235 + { 236 + int i; 237 + int ret; 238 + 239 + for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) { 240 + if (bmc150_accel_samp_freq_table[i].val == val && 241 + bmc150_accel_samp_freq_table[i].val2 == val2) { 242 + ret = i2c_smbus_write_byte_data( 243 + data->client, 244 + BMC150_ACCEL_REG_PMU_BW, 245 + bmc150_accel_samp_freq_table[i].bw_bits); 246 + if (ret < 0) 247 + return ret; 248 + 249 + data->bw_bits = 250 + bmc150_accel_samp_freq_table[i].bw_bits; 251 + return 0; 252 + } 253 + } 254 + 255 + return -EINVAL; 256 + } 257 + 258 + static int bmc150_accel_chip_init(struct bmc150_accel_data *data) 259 + { 260 + int ret; 261 + 262 + ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_CHIP_ID); 263 + if (ret < 0) { 264 + dev_err(&data->client->dev, 265 + "Error: Reading chip id\n"); 266 + return ret; 267 + } 268 + 269 + dev_dbg(&data->client->dev, "Chip Id %x\n", ret); 270 + if (ret != BMC150_ACCEL_CHIP_ID_VAL) { 271 + dev_err(&data->client->dev, "Invalid chip %x\n", ret); 272 + return -ENODEV; 273 + } 274 + 275 + ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); 276 + if (ret < 0) 277 + return ret; 278 + 279 + /* Set Bandwidth */ 280 + ret = bmc150_accel_set_bw(data, BMC150_ACCEL_DEF_BW, 0); 281 + if (ret < 0) 282 + return ret; 283 + 284 + /* Set Default Range */ 285 + ret = i2c_smbus_write_byte_data(data->client, 286 + BMC150_ACCEL_REG_PMU_RANGE, 287 + BMC150_ACCEL_DEF_RANGE_4G); 288 + if (ret < 0) { 289 + dev_err(&data->client->dev, 290 + "Error writing reg_pmu_range\n"); 291 + return ret; 292 + } 293 + 294 + data->range = BMC150_ACCEL_DEF_RANGE_4G; 295 + 296 + /* Set default slope duration */ 297 + ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_INT_5); 298 + if (ret < 0) { 299 + dev_err(&data->client->dev, "Error reading reg_int_5\n"); 300 + return ret; 301 + } 302 + data->slope_dur |= BMC150_ACCEL_DEF_SLOPE_DURATION; 303 + ret = i2c_smbus_write_byte_data(data->client, 304 + BMC150_ACCEL_REG_INT_5, 305 + data->slope_dur); 306 + if (ret < 0) { 307 + dev_err(&data->client->dev, "Error writing reg_int_5\n"); 308 + return ret; 309 + } 310 + dev_dbg(&data->client->dev, "slope_dur %x\n", data->slope_dur); 311 + 312 + /* Set default slope thresholds */ 313 + ret = i2c_smbus_write_byte_data(data->client, 314 + BMC150_ACCEL_REG_INT_6, 315 + BMC150_ACCEL_DEF_SLOPE_THRESHOLD); 316 + if (ret < 0) { 317 + dev_err(&data->client->dev, "Error writing reg_int_6\n"); 318 + return ret; 319 + } 320 + data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD; 321 + dev_dbg(&data->client->dev, "slope_thres %x\n", data->slope_thres); 322 + 323 + /* Set default as latched interrupts */ 324 + ret = i2c_smbus_write_byte_data(data->client, 325 + BMC150_ACCEL_REG_INT_RST_LATCH, 326 + BMC150_ACCEL_INT_MODE_LATCH_INT | 327 + BMC150_ACCEL_INT_MODE_LATCH_RESET); 328 + if (ret < 0) { 329 + dev_err(&data->client->dev, 330 + "Error writing reg_int_rst_latch\n"); 331 + return ret; 332 + } 333 + 334 + return 0; 335 + } 336 + 337 + static int bmc150_accel_setup_any_motion_interrupt( 338 + struct bmc150_accel_data *data, 339 + bool status) 340 + { 341 + int ret; 342 + 343 + /* Enable/Disable INT1 mapping */ 344 + ret = i2c_smbus_read_byte_data(data->client, 345 + BMC150_ACCEL_REG_INT_MAP_0); 346 + if (ret < 0) { 347 + dev_err(&data->client->dev, "Error reading reg_int_map_0\n"); 348 + return ret; 349 + } 350 + if (status) 351 + ret |= BMC150_ACCEL_INT_MAP_0_BIT_SLOPE; 352 + else 353 + ret &= ~BMC150_ACCEL_INT_MAP_0_BIT_SLOPE; 354 + 355 + ret = i2c_smbus_write_byte_data(data->client, 356 + BMC150_ACCEL_REG_INT_MAP_0, 357 + ret); 358 + if (ret < 0) { 359 + dev_err(&data->client->dev, "Error writing reg_int_map_0\n"); 360 + return ret; 361 + } 362 + 363 + if (status) { 364 + /* Set slope duration (no of samples) */ 365 + ret = i2c_smbus_write_byte_data(data->client, 366 + BMC150_ACCEL_REG_INT_5, 367 + data->slope_dur); 368 + if (ret < 0) { 369 + dev_err(&data->client->dev, "Error write reg_int_5\n"); 370 + return ret; 371 + } 372 + 373 + /* Set slope thresholds */ 374 + ret = i2c_smbus_write_byte_data(data->client, 375 + BMC150_ACCEL_REG_INT_6, 376 + data->slope_thres); 377 + if (ret < 0) { 378 + dev_err(&data->client->dev, "Error write reg_int_6\n"); 379 + return ret; 380 + } 381 + 382 + /* 383 + * New data interrupt is always non-latched, 384 + * which will have higher priority, so no need 385 + * to set latched mode, we will be flooded anyway with INTR 386 + */ 387 + if (!data->dready_trigger_on) { 388 + ret = i2c_smbus_write_byte_data(data->client, 389 + BMC150_ACCEL_REG_INT_RST_LATCH, 390 + BMC150_ACCEL_INT_MODE_LATCH_INT | 391 + BMC150_ACCEL_INT_MODE_LATCH_RESET); 392 + if (ret < 0) { 393 + dev_err(&data->client->dev, 394 + "Error writing reg_int_rst_latch\n"); 395 + return ret; 396 + } 397 + } 398 + 399 + ret = i2c_smbus_write_byte_data(data->client, 400 + BMC150_ACCEL_REG_INT_EN_0, 401 + BMC150_ACCEL_INT_EN_BIT_SLP_X | 402 + BMC150_ACCEL_INT_EN_BIT_SLP_Y | 403 + BMC150_ACCEL_INT_EN_BIT_SLP_Z); 404 + } else 405 + ret = i2c_smbus_write_byte_data(data->client, 406 + BMC150_ACCEL_REG_INT_EN_0, 407 + 0); 408 + 409 + if (ret < 0) { 410 + dev_err(&data->client->dev, "Error writing reg_int_en_0\n"); 411 + return ret; 412 + } 413 + 414 + return 0; 415 + } 416 + 417 + static int bmc150_accel_setup_new_data_interrupt(struct bmc150_accel_data *data, 418 + bool status) 419 + { 420 + int ret; 421 + 422 + /* Enable/Disable INT1 mapping */ 423 + ret = i2c_smbus_read_byte_data(data->client, 424 + BMC150_ACCEL_REG_INT_MAP_1); 425 + if (ret < 0) { 426 + dev_err(&data->client->dev, "Error reading reg_int_map_1\n"); 427 + return ret; 428 + } 429 + if (status) 430 + ret |= BMC150_ACCEL_INT_MAP_1_BIT_DATA; 431 + else 432 + ret &= ~BMC150_ACCEL_INT_MAP_1_BIT_DATA; 433 + 434 + ret = i2c_smbus_write_byte_data(data->client, 435 + BMC150_ACCEL_REG_INT_MAP_1, 436 + ret); 437 + if (ret < 0) { 438 + dev_err(&data->client->dev, "Error writing reg_int_map_1\n"); 439 + return ret; 440 + } 441 + 442 + if (status) { 443 + /* 444 + * Set non latched mode interrupt and clear any latched 445 + * interrupt 446 + */ 447 + ret = i2c_smbus_write_byte_data(data->client, 448 + BMC150_ACCEL_REG_INT_RST_LATCH, 449 + BMC150_ACCEL_INT_MODE_NON_LATCH_INT | 450 + BMC150_ACCEL_INT_MODE_LATCH_RESET); 451 + if (ret < 0) { 452 + dev_err(&data->client->dev, 453 + "Error writing reg_int_rst_latch\n"); 454 + return ret; 455 + } 456 + 457 + ret = i2c_smbus_write_byte_data(data->client, 458 + BMC150_ACCEL_REG_INT_EN_1, 459 + BMC150_ACCEL_INT_EN_BIT_DATA_EN); 460 + 461 + } else { 462 + /* Restore default interrupt mode */ 463 + ret = i2c_smbus_write_byte_data(data->client, 464 + BMC150_ACCEL_REG_INT_RST_LATCH, 465 + BMC150_ACCEL_INT_MODE_LATCH_INT | 466 + BMC150_ACCEL_INT_MODE_LATCH_RESET); 467 + if (ret < 0) { 468 + dev_err(&data->client->dev, 469 + "Error writing reg_int_rst_latch\n"); 470 + return ret; 471 + } 472 + 473 + ret = i2c_smbus_write_byte_data(data->client, 474 + BMC150_ACCEL_REG_INT_EN_1, 475 + 0); 476 + } 477 + 478 + if (ret < 0) { 479 + dev_err(&data->client->dev, "Error writing reg_int_en_1\n"); 480 + return ret; 481 + } 482 + 483 + return 0; 484 + } 485 + 486 + static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val, 487 + int *val2) 488 + { 489 + int i; 490 + 491 + for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) { 492 + if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) { 493 + *val = bmc150_accel_samp_freq_table[i].val; 494 + *val2 = bmc150_accel_samp_freq_table[i].val2; 495 + return IIO_VAL_INT_PLUS_MICRO; 496 + } 497 + } 498 + 499 + return -EINVAL; 500 + } 501 + 502 + static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data) 503 + { 504 + int i; 505 + 506 + for (i = 0; i < ARRAY_SIZE(bmc150_accel_sample_upd_time); ++i) { 507 + if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits) 508 + return bmc150_accel_sample_upd_time[i].msec; 509 + } 510 + 511 + return BMC150_ACCEL_MAX_STARTUP_TIME_MS; 512 + } 513 + 514 + static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on) 515 + { 516 + int ret; 517 + 518 + if (on) 519 + ret = pm_runtime_get_sync(&data->client->dev); 520 + else { 521 + pm_runtime_mark_last_busy(&data->client->dev); 522 + ret = pm_runtime_put_autosuspend(&data->client->dev); 523 + } 524 + if (ret < 0) { 525 + dev_err(&data->client->dev, 526 + "Failed: bmc150_accel_set_power_state for %d\n", on); 527 + return ret; 528 + } 529 + 530 + return 0; 531 + } 532 + 533 + static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val) 534 + { 535 + int ret, i; 536 + 537 + for (i = 0; i < ARRAY_SIZE(bmc150_accel_scale_table); ++i) { 538 + if (bmc150_accel_scale_table[i].scale == val) { 539 + ret = i2c_smbus_write_byte_data( 540 + data->client, 541 + BMC150_ACCEL_REG_PMU_RANGE, 542 + bmc150_accel_scale_table[i].range); 543 + if (ret < 0) { 544 + dev_err(&data->client->dev, 545 + "Error writing pmu_range\n"); 546 + return ret; 547 + } 548 + 549 + data->range = bmc150_accel_scale_table[i].range; 550 + return 0; 551 + } 552 + } 553 + 554 + return -EINVAL; 555 + } 556 + 557 + static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val) 558 + { 559 + int ret; 560 + 561 + mutex_lock(&data->mutex); 562 + 563 + ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_TEMP); 564 + if (ret < 0) { 565 + dev_err(&data->client->dev, "Error reading reg_temp\n"); 566 + mutex_unlock(&data->mutex); 567 + return ret; 568 + } 569 + *val = sign_extend32(ret, 7); 570 + 571 + mutex_unlock(&data->mutex); 572 + 573 + return IIO_VAL_INT; 574 + } 575 + 576 + static int bmc150_accel_get_axis(struct bmc150_accel_data *data, int axis, 577 + int *val) 578 + { 579 + int ret; 580 + 581 + mutex_lock(&data->mutex); 582 + ret = bmc150_accel_set_power_state(data, true); 583 + if (ret < 0) { 584 + mutex_unlock(&data->mutex); 585 + return ret; 586 + } 587 + 588 + ret = i2c_smbus_read_word_data(data->client, 589 + BMC150_ACCEL_AXIS_TO_REG(axis)); 590 + if (ret < 0) { 591 + dev_err(&data->client->dev, "Error reading axis %d\n", axis); 592 + bmc150_accel_set_power_state(data, false); 593 + mutex_unlock(&data->mutex); 594 + return ret; 595 + } 596 + *val = sign_extend32(ret >> 4, 11); 597 + ret = bmc150_accel_set_power_state(data, false); 598 + mutex_unlock(&data->mutex); 599 + if (ret < 0) 600 + return ret; 601 + 602 + return IIO_VAL_INT; 603 + } 604 + 605 + static int bmc150_accel_read_raw(struct iio_dev *indio_dev, 606 + struct iio_chan_spec const *chan, 607 + int *val, int *val2, long mask) 608 + { 609 + struct bmc150_accel_data *data = iio_priv(indio_dev); 610 + int ret; 611 + 612 + switch (mask) { 613 + case IIO_CHAN_INFO_RAW: 614 + switch (chan->type) { 615 + case IIO_TEMP: 616 + return bmc150_accel_get_temp(data, val); 617 + case IIO_ACCEL: 618 + if (iio_buffer_enabled(indio_dev)) 619 + return -EBUSY; 620 + else 621 + return bmc150_accel_get_axis(data, 622 + chan->scan_index, 623 + val); 624 + default: 625 + return -EINVAL; 626 + } 627 + case IIO_CHAN_INFO_OFFSET: 628 + if (chan->type == IIO_TEMP) { 629 + *val = BMC150_ACCEL_TEMP_CENTER_VAL; 630 + return IIO_VAL_INT; 631 + } else 632 + return -EINVAL; 633 + case IIO_CHAN_INFO_SCALE: 634 + *val = 0; 635 + switch (chan->type) { 636 + case IIO_TEMP: 637 + *val2 = 500000; 638 + return IIO_VAL_INT_PLUS_MICRO; 639 + case IIO_ACCEL: 640 + { 641 + int i; 642 + 643 + for (i = 0; i < ARRAY_SIZE(bmc150_accel_scale_table); 644 + ++i) { 645 + if (bmc150_accel_scale_table[i].range == 646 + data->range) { 647 + *val2 = 648 + bmc150_accel_scale_table[i].scale; 649 + return IIO_VAL_INT_PLUS_MICRO; 650 + } 651 + } 652 + return -EINVAL; 653 + } 654 + default: 655 + return -EINVAL; 656 + } 657 + case IIO_CHAN_INFO_SAMP_FREQ: 658 + mutex_lock(&data->mutex); 659 + ret = bmc150_accel_get_bw(data, val, val2); 660 + mutex_unlock(&data->mutex); 661 + return ret; 662 + default: 663 + return -EINVAL; 664 + } 665 + } 666 + 667 + static int bmc150_accel_write_raw(struct iio_dev *indio_dev, 668 + struct iio_chan_spec const *chan, 669 + int val, int val2, long mask) 670 + { 671 + struct bmc150_accel_data *data = iio_priv(indio_dev); 672 + int ret; 673 + 674 + switch (mask) { 675 + case IIO_CHAN_INFO_SAMP_FREQ: 676 + mutex_lock(&data->mutex); 677 + ret = bmc150_accel_set_bw(data, val, val2); 678 + mutex_unlock(&data->mutex); 679 + break; 680 + case IIO_CHAN_INFO_SCALE: 681 + if (val) 682 + return -EINVAL; 683 + 684 + mutex_lock(&data->mutex); 685 + ret = bmc150_accel_set_scale(data, val2); 686 + mutex_unlock(&data->mutex); 687 + return ret; 688 + default: 689 + ret = -EINVAL; 690 + } 691 + 692 + return ret; 693 + } 694 + 695 + static int bmc150_accel_read_event(struct iio_dev *indio_dev, 696 + const struct iio_chan_spec *chan, 697 + enum iio_event_type type, 698 + enum iio_event_direction dir, 699 + enum iio_event_info info, 700 + int *val, int *val2) 701 + { 702 + struct bmc150_accel_data *data = iio_priv(indio_dev); 703 + 704 + *val2 = 0; 705 + switch (info) { 706 + case IIO_EV_INFO_VALUE: 707 + *val = data->slope_thres; 708 + break; 709 + case IIO_EV_INFO_PERIOD: 710 + *val = data->slope_dur & BMC150_ACCEL_SLOPE_DUR_MASK; 711 + break; 712 + default: 713 + return -EINVAL; 714 + } 715 + 716 + return IIO_VAL_INT; 717 + } 718 + 719 + static int bmc150_accel_write_event(struct iio_dev *indio_dev, 720 + const struct iio_chan_spec *chan, 721 + enum iio_event_type type, 722 + enum iio_event_direction dir, 723 + enum iio_event_info info, 724 + int val, int val2) 725 + { 726 + struct bmc150_accel_data *data = iio_priv(indio_dev); 727 + 728 + if (data->ev_enable_state) 729 + return -EBUSY; 730 + 731 + switch (info) { 732 + case IIO_EV_INFO_VALUE: 733 + data->slope_thres = val; 734 + break; 735 + case IIO_EV_INFO_PERIOD: 736 + data->slope_dur &= ~BMC150_ACCEL_SLOPE_DUR_MASK; 737 + data->slope_dur |= val & BMC150_ACCEL_SLOPE_DUR_MASK; 738 + break; 739 + default: 740 + return -EINVAL; 741 + } 742 + 743 + return 0; 744 + } 745 + 746 + static int bmc150_accel_read_event_config(struct iio_dev *indio_dev, 747 + const struct iio_chan_spec *chan, 748 + enum iio_event_type type, 749 + enum iio_event_direction dir) 750 + { 751 + 752 + struct bmc150_accel_data *data = iio_priv(indio_dev); 753 + 754 + return data->ev_enable_state; 755 + } 756 + 757 + static int bmc150_accel_write_event_config(struct iio_dev *indio_dev, 758 + const struct iio_chan_spec *chan, 759 + enum iio_event_type type, 760 + enum iio_event_direction dir, 761 + int state) 762 + { 763 + struct bmc150_accel_data *data = iio_priv(indio_dev); 764 + int ret; 765 + 766 + if (state && data->ev_enable_state) 767 + return 0; 768 + 769 + mutex_lock(&data->mutex); 770 + 771 + if (!state && data->motion_trigger_on) { 772 + data->ev_enable_state = 0; 773 + mutex_unlock(&data->mutex); 774 + return 0; 775 + } 776 + 777 + /* 778 + * We will expect the enable and disable to do operation in 779 + * in reverse order. This will happen here anyway as our 780 + * resume operation uses sync mode runtime pm calls, the 781 + * suspend operation will be delayed by autosuspend delay 782 + * So the disable operation will still happen in reverse of 783 + * enable operation. When runtime pm is disabled the mode 784 + * is always on so sequence doesn't matter 785 + */ 786 + 787 + ret = bmc150_accel_set_power_state(data, state); 788 + if (ret < 0) { 789 + mutex_unlock(&data->mutex); 790 + return ret; 791 + } 792 + 793 + ret = bmc150_accel_setup_any_motion_interrupt(data, state); 794 + if (ret < 0) { 795 + mutex_unlock(&data->mutex); 796 + return ret; 797 + } 798 + 799 + data->ev_enable_state = state; 800 + mutex_unlock(&data->mutex); 801 + 802 + return 0; 803 + } 804 + 805 + static int bmc150_accel_validate_trigger(struct iio_dev *indio_dev, 806 + struct iio_trigger *trig) 807 + { 808 + struct bmc150_accel_data *data = iio_priv(indio_dev); 809 + 810 + if (data->dready_trig != trig && data->motion_trig != trig) 811 + return -EINVAL; 812 + 813 + return 0; 814 + } 815 + 816 + static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( 817 + "7.810000 15.630000 31.250000 62.500000 125 250 500 1000"); 818 + 819 + static struct attribute *bmc150_accel_attributes[] = { 820 + &iio_const_attr_sampling_frequency_available.dev_attr.attr, 821 + NULL, 822 + }; 823 + 824 + static const struct attribute_group bmc150_accel_attrs_group = { 825 + .attrs = bmc150_accel_attributes, 826 + }; 827 + 828 + static const struct iio_event_spec bmc150_accel_event = { 829 + .type = IIO_EV_TYPE_ROC, 830 + .dir = IIO_EV_DIR_RISING | IIO_EV_DIR_FALLING, 831 + .mask_separate = BIT(IIO_EV_INFO_VALUE) | 832 + BIT(IIO_EV_INFO_ENABLE) | 833 + BIT(IIO_EV_INFO_PERIOD) 834 + }; 835 + 836 + #define BMC150_ACCEL_CHANNEL(_axis) { \ 837 + .type = IIO_ACCEL, \ 838 + .modified = 1, \ 839 + .channel2 = IIO_MOD_##_axis, \ 840 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 841 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 842 + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 843 + .scan_index = AXIS_##_axis, \ 844 + .scan_type = { \ 845 + .sign = 's', \ 846 + .realbits = 12, \ 847 + .storagebits = 16, \ 848 + .shift = 4, \ 849 + }, \ 850 + .event_spec = &bmc150_accel_event, \ 851 + .num_event_specs = 1 \ 852 + } 853 + 854 + static const struct iio_chan_spec bmc150_accel_channels[] = { 855 + { 856 + .type = IIO_TEMP, 857 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 858 + BIT(IIO_CHAN_INFO_SCALE) | 859 + BIT(IIO_CHAN_INFO_OFFSET), 860 + .scan_index = -1, 861 + }, 862 + BMC150_ACCEL_CHANNEL(X), 863 + BMC150_ACCEL_CHANNEL(Y), 864 + BMC150_ACCEL_CHANNEL(Z), 865 + IIO_CHAN_SOFT_TIMESTAMP(3), 866 + }; 867 + 868 + static const struct iio_info bmc150_accel_info = { 869 + .attrs = &bmc150_accel_attrs_group, 870 + .read_raw = bmc150_accel_read_raw, 871 + .write_raw = bmc150_accel_write_raw, 872 + .read_event_value = bmc150_accel_read_event, 873 + .write_event_value = bmc150_accel_write_event, 874 + .write_event_config = bmc150_accel_write_event_config, 875 + .read_event_config = bmc150_accel_read_event_config, 876 + .validate_trigger = bmc150_accel_validate_trigger, 877 + .driver_module = THIS_MODULE, 878 + }; 879 + 880 + static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p) 881 + { 882 + struct iio_poll_func *pf = p; 883 + struct iio_dev *indio_dev = pf->indio_dev; 884 + struct bmc150_accel_data *data = iio_priv(indio_dev); 885 + int bit, ret, i = 0; 886 + 887 + mutex_lock(&data->mutex); 888 + for_each_set_bit(bit, indio_dev->buffer->scan_mask, 889 + indio_dev->masklength) { 890 + ret = i2c_smbus_read_word_data(data->client, 891 + BMC150_ACCEL_AXIS_TO_REG(bit)); 892 + if (ret < 0) { 893 + mutex_unlock(&data->mutex); 894 + goto err_read; 895 + } 896 + data->buffer[i++] = ret; 897 + } 898 + mutex_unlock(&data->mutex); 899 + 900 + iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 901 + data->timestamp); 902 + err_read: 903 + iio_trigger_notify_done(indio_dev->trig); 904 + 905 + return IRQ_HANDLED; 906 + } 907 + 908 + static int bmc150_accel_trig_try_reen(struct iio_trigger *trig) 909 + { 910 + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 911 + struct bmc150_accel_data *data = iio_priv(indio_dev); 912 + int ret; 913 + 914 + /* new data interrupts don't need ack */ 915 + if (data->dready_trigger_on) 916 + return 0; 917 + 918 + mutex_lock(&data->mutex); 919 + /* clear any latched interrupt */ 920 + ret = i2c_smbus_write_byte_data(data->client, 921 + BMC150_ACCEL_REG_INT_RST_LATCH, 922 + BMC150_ACCEL_INT_MODE_LATCH_INT | 923 + BMC150_ACCEL_INT_MODE_LATCH_RESET); 924 + mutex_unlock(&data->mutex); 925 + if (ret < 0) { 926 + dev_err(&data->client->dev, 927 + "Error writing reg_int_rst_latch\n"); 928 + return ret; 929 + } 930 + 931 + return 0; 932 + } 933 + 934 + static int bmc150_accel_data_rdy_trigger_set_state(struct iio_trigger *trig, 935 + bool state) 936 + { 937 + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 938 + struct bmc150_accel_data *data = iio_priv(indio_dev); 939 + int ret; 940 + 941 + mutex_lock(&data->mutex); 942 + 943 + if (!state && data->ev_enable_state && data->motion_trigger_on) { 944 + data->motion_trigger_on = false; 945 + mutex_unlock(&data->mutex); 946 + return 0; 947 + } 948 + 949 + /* 950 + * Refer to comment in bmc150_accel_write_event_config for 951 + * enable/disable operation order 952 + */ 953 + ret = bmc150_accel_set_power_state(data, state); 954 + if (ret < 0) { 955 + mutex_unlock(&data->mutex); 956 + return ret; 957 + } 958 + if (data->motion_trig == trig) 959 + ret = bmc150_accel_setup_any_motion_interrupt(data, state); 960 + else 961 + ret = bmc150_accel_setup_new_data_interrupt(data, state); 962 + if (ret < 0) { 963 + mutex_unlock(&data->mutex); 964 + return ret; 965 + } 966 + if (data->motion_trig == trig) 967 + data->motion_trigger_on = state; 968 + else 969 + data->dready_trigger_on = state; 970 + 971 + mutex_unlock(&data->mutex); 972 + 973 + return ret; 974 + } 975 + 976 + static const struct iio_trigger_ops bmc150_accel_trigger_ops = { 977 + .set_trigger_state = bmc150_accel_data_rdy_trigger_set_state, 978 + .try_reenable = bmc150_accel_trig_try_reen, 979 + .owner = THIS_MODULE, 980 + }; 981 + 982 + static irqreturn_t bmc150_accel_event_handler(int irq, void *private) 983 + { 984 + struct iio_dev *indio_dev = private; 985 + struct bmc150_accel_data *data = iio_priv(indio_dev); 986 + int ret; 987 + int dir; 988 + 989 + ret = i2c_smbus_read_byte_data(data->client, 990 + BMC150_ACCEL_REG_INT_STATUS_2); 991 + if (ret < 0) { 992 + dev_err(&data->client->dev, "Error reading reg_int_status_2\n"); 993 + goto ack_intr_status; 994 + } 995 + 996 + if (ret & BMC150_ACCEL_ANY_MOTION_BIT_SIGN) 997 + dir = IIO_EV_DIR_FALLING; 998 + else 999 + dir = IIO_EV_DIR_RISING; 1000 + 1001 + if (ret & BMC150_ACCEL_ANY_MOTION_MASK) 1002 + iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ACCEL, 1003 + 0, 1004 + IIO_MOD_X_OR_Y_OR_Z, 1005 + IIO_EV_TYPE_ROC, 1006 + IIO_EV_DIR_EITHER), 1007 + data->timestamp); 1008 + ack_intr_status: 1009 + if (!data->dready_trigger_on) 1010 + ret = i2c_smbus_write_byte_data(data->client, 1011 + BMC150_ACCEL_REG_INT_RST_LATCH, 1012 + BMC150_ACCEL_INT_MODE_LATCH_INT | 1013 + BMC150_ACCEL_INT_MODE_LATCH_RESET); 1014 + 1015 + return IRQ_HANDLED; 1016 + } 1017 + 1018 + static irqreturn_t bmc150_accel_data_rdy_trig_poll(int irq, void *private) 1019 + { 1020 + struct iio_dev *indio_dev = private; 1021 + struct bmc150_accel_data *data = iio_priv(indio_dev); 1022 + 1023 + data->timestamp = iio_get_time_ns(); 1024 + 1025 + if (data->dready_trigger_on) 1026 + iio_trigger_poll(data->dready_trig); 1027 + else if (data->motion_trigger_on) 1028 + iio_trigger_poll(data->motion_trig); 1029 + 1030 + if (data->ev_enable_state) 1031 + return IRQ_WAKE_THREAD; 1032 + else 1033 + return IRQ_HANDLED; 1034 + } 1035 + 1036 + static int bmc150_accel_acpi_gpio_probe(struct i2c_client *client, 1037 + struct bmc150_accel_data *data) 1038 + { 1039 + const struct acpi_device_id *id; 1040 + struct device *dev; 1041 + struct gpio_desc *gpio; 1042 + int ret; 1043 + 1044 + if (!client) 1045 + return -EINVAL; 1046 + 1047 + dev = &client->dev; 1048 + if (!ACPI_HANDLE(dev)) 1049 + return -ENODEV; 1050 + 1051 + id = acpi_match_device(dev->driver->acpi_match_table, dev); 1052 + if (!id) 1053 + return -ENODEV; 1054 + 1055 + /* data ready gpio interrupt pin */ 1056 + gpio = devm_gpiod_get_index(dev, BMC150_ACCEL_GPIO_NAME, 0); 1057 + if (IS_ERR(gpio)) { 1058 + dev_err(dev, "Failed: acpi gpio get index\n"); 1059 + return PTR_ERR(gpio); 1060 + } 1061 + 1062 + ret = gpiod_direction_input(gpio); 1063 + if (ret) 1064 + return ret; 1065 + 1066 + ret = gpiod_to_irq(gpio); 1067 + 1068 + dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); 1069 + 1070 + return ret; 1071 + } 1072 + 1073 + static int bmc150_accel_probe(struct i2c_client *client, 1074 + const struct i2c_device_id *id) 1075 + { 1076 + struct bmc150_accel_data *data; 1077 + struct iio_dev *indio_dev; 1078 + int ret; 1079 + 1080 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 1081 + if (!indio_dev) 1082 + return -ENOMEM; 1083 + 1084 + data = iio_priv(indio_dev); 1085 + i2c_set_clientdata(client, indio_dev); 1086 + data->client = client; 1087 + 1088 + ret = bmc150_accel_chip_init(data); 1089 + if (ret < 0) 1090 + return ret; 1091 + 1092 + mutex_init(&data->mutex); 1093 + 1094 + indio_dev->dev.parent = &client->dev; 1095 + indio_dev->channels = bmc150_accel_channels; 1096 + indio_dev->num_channels = ARRAY_SIZE(bmc150_accel_channels); 1097 + indio_dev->name = BMC150_ACCEL_DRV_NAME; 1098 + indio_dev->modes = INDIO_DIRECT_MODE; 1099 + indio_dev->info = &bmc150_accel_info; 1100 + 1101 + if (client->irq < 0) 1102 + client->irq = bmc150_accel_acpi_gpio_probe(client, data); 1103 + 1104 + if (client->irq >= 0) { 1105 + ret = devm_request_threaded_irq( 1106 + &client->dev, client->irq, 1107 + bmc150_accel_data_rdy_trig_poll, 1108 + bmc150_accel_event_handler, 1109 + IRQF_TRIGGER_RISING, 1110 + BMC150_ACCEL_IRQ_NAME, 1111 + indio_dev); 1112 + if (ret) 1113 + return ret; 1114 + 1115 + data->dready_trig = devm_iio_trigger_alloc(&client->dev, 1116 + "%s-dev%d", 1117 + indio_dev->name, 1118 + indio_dev->id); 1119 + if (!data->dready_trig) 1120 + return -ENOMEM; 1121 + 1122 + data->motion_trig = devm_iio_trigger_alloc(&client->dev, 1123 + "%s-any-motion-dev%d", 1124 + indio_dev->name, 1125 + indio_dev->id); 1126 + if (!data->motion_trig) 1127 + return -ENOMEM; 1128 + 1129 + data->dready_trig->dev.parent = &client->dev; 1130 + data->dready_trig->ops = &bmc150_accel_trigger_ops; 1131 + iio_trigger_set_drvdata(data->dready_trig, indio_dev); 1132 + ret = iio_trigger_register(data->dready_trig); 1133 + if (ret) 1134 + return ret; 1135 + 1136 + data->motion_trig->dev.parent = &client->dev; 1137 + data->motion_trig->ops = &bmc150_accel_trigger_ops; 1138 + iio_trigger_set_drvdata(data->motion_trig, indio_dev); 1139 + ret = iio_trigger_register(data->motion_trig); 1140 + if (ret) { 1141 + data->motion_trig = NULL; 1142 + goto err_trigger_unregister; 1143 + } 1144 + 1145 + ret = iio_triggered_buffer_setup(indio_dev, 1146 + &iio_pollfunc_store_time, 1147 + bmc150_accel_trigger_handler, 1148 + NULL); 1149 + if (ret < 0) { 1150 + dev_err(&client->dev, 1151 + "Failed: iio triggered buffer setup\n"); 1152 + goto err_trigger_unregister; 1153 + } 1154 + } 1155 + 1156 + ret = iio_device_register(indio_dev); 1157 + if (ret < 0) { 1158 + dev_err(&client->dev, "Unable to register iio device\n"); 1159 + goto err_buffer_cleanup; 1160 + } 1161 + 1162 + ret = pm_runtime_set_active(&client->dev); 1163 + if (ret) 1164 + goto err_iio_unregister; 1165 + 1166 + pm_runtime_enable(&client->dev); 1167 + pm_runtime_set_autosuspend_delay(&client->dev, 1168 + BMC150_AUTO_SUSPEND_DELAY_MS); 1169 + pm_runtime_use_autosuspend(&client->dev); 1170 + 1171 + return 0; 1172 + 1173 + err_iio_unregister: 1174 + iio_device_unregister(indio_dev); 1175 + err_buffer_cleanup: 1176 + if (data->dready_trig) 1177 + iio_triggered_buffer_cleanup(indio_dev); 1178 + err_trigger_unregister: 1179 + if (data->dready_trig) 1180 + iio_trigger_unregister(data->dready_trig); 1181 + if (data->motion_trig) 1182 + iio_trigger_unregister(data->motion_trig); 1183 + 1184 + return ret; 1185 + } 1186 + 1187 + static int bmc150_accel_remove(struct i2c_client *client) 1188 + { 1189 + struct iio_dev *indio_dev = i2c_get_clientdata(client); 1190 + struct bmc150_accel_data *data = iio_priv(indio_dev); 1191 + 1192 + pm_runtime_disable(&client->dev); 1193 + pm_runtime_set_suspended(&client->dev); 1194 + pm_runtime_put_noidle(&client->dev); 1195 + 1196 + iio_device_unregister(indio_dev); 1197 + 1198 + if (data->dready_trig) { 1199 + iio_triggered_buffer_cleanup(indio_dev); 1200 + iio_trigger_unregister(data->dready_trig); 1201 + iio_trigger_unregister(data->motion_trig); 1202 + } 1203 + 1204 + mutex_lock(&data->mutex); 1205 + bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0); 1206 + mutex_unlock(&data->mutex); 1207 + 1208 + return 0; 1209 + } 1210 + 1211 + #ifdef CONFIG_PM_SLEEP 1212 + static int bmc150_accel_suspend(struct device *dev) 1213 + { 1214 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1215 + struct bmc150_accel_data *data = iio_priv(indio_dev); 1216 + 1217 + mutex_lock(&data->mutex); 1218 + bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0); 1219 + mutex_unlock(&data->mutex); 1220 + 1221 + return 0; 1222 + } 1223 + 1224 + static int bmc150_accel_resume(struct device *dev) 1225 + { 1226 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1227 + struct bmc150_accel_data *data = iio_priv(indio_dev); 1228 + 1229 + mutex_lock(&data->mutex); 1230 + if (data->dready_trigger_on || data->motion_trigger_on || 1231 + data->ev_enable_state) 1232 + bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); 1233 + mutex_unlock(&data->mutex); 1234 + 1235 + return 0; 1236 + } 1237 + #endif 1238 + 1239 + #ifdef CONFIG_PM_RUNTIME 1240 + static int bmc150_accel_runtime_suspend(struct device *dev) 1241 + { 1242 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1243 + struct bmc150_accel_data *data = iio_priv(indio_dev); 1244 + 1245 + dev_dbg(&data->client->dev, __func__); 1246 + 1247 + return bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0); 1248 + } 1249 + 1250 + static int bmc150_accel_runtime_resume(struct device *dev) 1251 + { 1252 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1253 + struct bmc150_accel_data *data = iio_priv(indio_dev); 1254 + int ret; 1255 + int sleep_val; 1256 + 1257 + dev_dbg(&data->client->dev, __func__); 1258 + 1259 + ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); 1260 + if (ret < 0) 1261 + return ret; 1262 + 1263 + sleep_val = bmc150_accel_get_startup_times(data); 1264 + if (sleep_val < 20) 1265 + usleep_range(sleep_val * 1000, 20000); 1266 + else 1267 + msleep_interruptible(sleep_val); 1268 + 1269 + return 0; 1270 + } 1271 + #endif 1272 + 1273 + static const struct dev_pm_ops bmc150_accel_pm_ops = { 1274 + SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend, bmc150_accel_resume) 1275 + SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend, 1276 + bmc150_accel_runtime_resume, NULL) 1277 + }; 1278 + 1279 + static const struct acpi_device_id bmc150_accel_acpi_match[] = { 1280 + {"BSBA0150", 0}, 1281 + {"BMC150A", 0}, 1282 + { }, 1283 + }; 1284 + MODULE_DEVICE_TABLE(acpi, bmc150_accel_acpi_match); 1285 + 1286 + static const struct i2c_device_id bmc150_accel_id[] = { 1287 + {"bmc150_accel", 0}, 1288 + {} 1289 + }; 1290 + 1291 + MODULE_DEVICE_TABLE(i2c, bmc150_accel_id); 1292 + 1293 + static struct i2c_driver bmc150_accel_driver = { 1294 + .driver = { 1295 + .name = BMC150_ACCEL_DRV_NAME, 1296 + .acpi_match_table = ACPI_PTR(bmc150_accel_acpi_match), 1297 + .pm = &bmc150_accel_pm_ops, 1298 + }, 1299 + .probe = bmc150_accel_probe, 1300 + .remove = bmc150_accel_remove, 1301 + .id_table = bmc150_accel_id, 1302 + }; 1303 + module_i2c_driver(bmc150_accel_driver); 1304 + 1305 + MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 1306 + MODULE_LICENSE("GPL v2"); 1307 + MODULE_DESCRIPTION("BMC150 accelerometer driver");
+246 -50
drivers/iio/accel/kxcjk-1013.c
··· 21 21 #include <linux/string.h> 22 22 #include <linux/acpi.h> 23 23 #include <linux/gpio/consumer.h> 24 + #include <linux/pm.h> 25 + #include <linux/pm_runtime.h> 24 26 #include <linux/iio/iio.h> 25 27 #include <linux/iio/sysfs.h> 26 28 #include <linux/iio/buffer.h> ··· 73 71 #define KXCJK1013_DATA_MASK_12_BIT 0x0FFF 74 72 #define KXCJK1013_MAX_STARTUP_TIME_US 100000 75 73 74 + #define KXCJK1013_SLEEP_DELAY_MS 2000 75 + 76 76 struct kxcjk1013_data { 77 77 struct i2c_client *client; 78 78 struct iio_trigger *trig; 79 79 bool trig_mode; 80 80 struct mutex mutex; 81 81 s16 buffer[8]; 82 - int power_state; 83 82 u8 odr_bits; 83 + u8 range; 84 84 bool active_high_intr; 85 + bool trigger_on; 85 86 }; 86 87 87 88 enum kxcjk1013_axis { ··· 96 91 enum kxcjk1013_mode { 97 92 STANDBY, 98 93 OPERATION, 94 + }; 95 + 96 + enum kxcjk1013_range { 97 + KXCJK1013_RANGE_2G, 98 + KXCJK1013_RANGE_4G, 99 + KXCJK1013_RANGE_8G, 99 100 }; 100 101 101 102 static const struct { ··· 122 111 {0x0B, 100000}, { 0, 80000}, {0x01, 41000}, 123 112 {0x02, 21000}, {0x03, 11000}, {0x04, 6400}, 124 113 {0x05, 3900}, {0x06, 2700}, {0x07, 2100} }; 114 + 115 + static const struct { 116 + u16 scale; 117 + u8 gsel_0; 118 + u8 gsel_1; 119 + } KXCJK1013_scale_table[] = { {9582, 0, 0}, 120 + {19163, 1, 0}, 121 + {38326, 0, 1} }; 125 122 126 123 static int kxcjk1013_set_mode(struct kxcjk1013_data *data, 127 124 enum kxcjk1013_mode mode) ··· 157 138 return 0; 158 139 } 159 140 141 + static int kxcjk1013_get_mode(struct kxcjk1013_data *data, 142 + enum kxcjk1013_mode *mode) 143 + { 144 + int ret; 145 + 146 + ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); 147 + if (ret < 0) { 148 + dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 149 + return ret; 150 + } 151 + 152 + if (ret & KXCJK1013_REG_CTRL1_BIT_PC1) 153 + *mode = OPERATION; 154 + else 155 + *mode = STANDBY; 156 + 157 + return 0; 158 + } 159 + 160 + static int kxcjk1013_set_range(struct kxcjk1013_data *data, int range_index) 161 + { 162 + int ret; 163 + 164 + ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); 165 + if (ret < 0) { 166 + dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 167 + return ret; 168 + } 169 + 170 + ret |= (KXCJK1013_scale_table[range_index].gsel_0 << 3); 171 + ret |= (KXCJK1013_scale_table[range_index].gsel_1 << 4); 172 + 173 + ret = i2c_smbus_write_byte_data(data->client, 174 + KXCJK1013_REG_CTRL1, 175 + ret); 176 + if (ret < 0) { 177 + dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); 178 + return ret; 179 + } 180 + 181 + data->range = range_index; 182 + 183 + return 0; 184 + } 185 + 160 186 static int kxcjk1013_chip_init(struct kxcjk1013_data *data) 161 187 { 162 188 int ret; ··· 224 160 return ret; 225 161 } 226 162 227 - /* Setting range to 4G */ 228 - ret |= KXCJK1013_REG_CTRL1_BIT_GSEL0; 229 - ret &= ~KXCJK1013_REG_CTRL1_BIT_GSEL1; 230 - 231 163 /* Set 12 bit mode */ 232 164 ret |= KXCJK1013_REG_CTRL1_BIT_RES; 233 165 ··· 233 173 dev_err(&data->client->dev, "Error reading reg_ctrl\n"); 234 174 return ret; 235 175 } 176 + 177 + /* Setting range to 4G */ 178 + ret = kxcjk1013_set_range(data, KXCJK1013_RANGE_4G); 179 + if (ret < 0) 180 + return ret; 181 + 182 + data->range = KXCJK1013_RANGE_4G; 183 + 236 184 237 185 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_DATA_CTRL); 238 186 if (ret < 0) { ··· 269 201 return ret; 270 202 } 271 203 204 + ret = kxcjk1013_set_mode(data, OPERATION); 205 + if (ret < 0) 206 + return ret; 207 + 208 + return 0; 209 + } 210 + 211 + static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data) 212 + { 213 + int i; 214 + 215 + for (i = 0; i < ARRAY_SIZE(odr_start_up_times); ++i) { 216 + if (odr_start_up_times[i].odr_bits == data->odr_bits) 217 + return odr_start_up_times[i].usec; 218 + } 219 + 220 + return KXCJK1013_MAX_STARTUP_TIME_US; 221 + } 222 + 223 + static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on) 224 + { 225 + int ret; 226 + 227 + if (on) 228 + ret = pm_runtime_get_sync(&data->client->dev); 229 + else { 230 + pm_runtime_mark_last_busy(&data->client->dev); 231 + ret = pm_runtime_put_autosuspend(&data->client->dev); 232 + } 233 + if (ret < 0) { 234 + dev_err(&data->client->dev, 235 + "Failed: kxcjk1013_set_power_state for %d\n", on); 236 + return ret; 237 + } 238 + 272 239 return 0; 273 240 } 274 241 ··· 311 208 bool status) 312 209 { 313 210 int ret; 211 + enum kxcjk1013_mode store_mode; 212 + 213 + ret = kxcjk1013_get_mode(data, &store_mode); 214 + if (ret < 0) 215 + return ret; 314 216 315 217 /* This is requirement by spec to change state to STANDBY */ 316 218 ret = kxcjk1013_set_mode(data, STANDBY); ··· 358 250 return ret; 359 251 } 360 252 361 - return ret; 253 + if (store_mode == OPERATION) { 254 + ret = kxcjk1013_set_mode(data, OPERATION); 255 + if (ret < 0) 256 + return ret; 257 + } 258 + 259 + return 0; 362 260 } 363 261 364 262 static int kxcjk1013_convert_freq_to_bit(int val, int val2) ··· 385 271 { 386 272 int ret; 387 273 int odr_bits; 274 + enum kxcjk1013_mode store_mode; 275 + 276 + ret = kxcjk1013_get_mode(data, &store_mode); 277 + if (ret < 0) 278 + return ret; 388 279 389 280 odr_bits = kxcjk1013_convert_freq_to_bit(val, val2); 390 281 if (odr_bits < 0) ··· 409 290 410 291 data->odr_bits = odr_bits; 411 292 412 - /* Check, if the ODR is changed after data enable */ 413 - if (data->power_state) { 414 - /* Set the state back to operation */ 293 + if (store_mode == OPERATION) { 415 294 ret = kxcjk1013_set_mode(data, OPERATION); 416 295 if (ret < 0) 417 296 return ret; ··· 448 331 return ret; 449 332 } 450 333 451 - static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data) 334 + static int kxcjk1013_set_scale(struct kxcjk1013_data *data, int val) 452 335 { 453 - int i; 336 + int ret, i; 337 + enum kxcjk1013_mode store_mode; 454 338 455 - for (i = 0; i < ARRAY_SIZE(odr_start_up_times); ++i) { 456 - if (odr_start_up_times[i].odr_bits == data->odr_bits) 457 - return odr_start_up_times[i].usec; 339 + 340 + for (i = 0; i < ARRAY_SIZE(KXCJK1013_scale_table); ++i) { 341 + if (KXCJK1013_scale_table[i].scale == val) { 342 + 343 + ret = kxcjk1013_get_mode(data, &store_mode); 344 + if (ret < 0) 345 + return ret; 346 + 347 + ret = kxcjk1013_set_mode(data, STANDBY); 348 + if (ret < 0) 349 + return ret; 350 + 351 + ret = kxcjk1013_set_range(data, i); 352 + if (ret < 0) 353 + return ret; 354 + 355 + if (store_mode == OPERATION) { 356 + ret = kxcjk1013_set_mode(data, OPERATION); 357 + if (ret) 358 + return ret; 359 + } 360 + 361 + return 0; 362 + } 458 363 } 459 364 460 - return KXCJK1013_MAX_STARTUP_TIME_US; 365 + return -EINVAL; 461 366 } 462 367 463 368 static int kxcjk1013_read_raw(struct iio_dev *indio_dev, ··· 495 356 if (iio_buffer_enabled(indio_dev)) 496 357 ret = -EBUSY; 497 358 else { 498 - int sleep_val; 499 - 500 - ret = kxcjk1013_set_mode(data, OPERATION); 359 + ret = kxcjk1013_set_power_state(data, true); 501 360 if (ret < 0) { 502 361 mutex_unlock(&data->mutex); 503 362 return ret; 504 363 } 505 - ++data->power_state; 506 - sleep_val = kxcjk1013_get_startup_times(data); 507 - if (sleep_val < 20000) 508 - usleep_range(sleep_val, 20000); 509 - else 510 - msleep_interruptible(sleep_val/1000); 511 364 ret = kxcjk1013_get_acc_reg(data, chan->scan_index); 512 - if (--data->power_state == 0) 513 - kxcjk1013_set_mode(data, STANDBY); 365 + if (ret < 0) { 366 + kxcjk1013_set_power_state(data, false); 367 + mutex_unlock(&data->mutex); 368 + return ret; 369 + } 370 + *val = sign_extend32(ret >> 4, 11); 371 + ret = kxcjk1013_set_power_state(data, false); 514 372 } 515 373 mutex_unlock(&data->mutex); 516 374 517 375 if (ret < 0) 518 376 return ret; 519 377 520 - *val = sign_extend32(ret >> 4, 11); 521 378 return IIO_VAL_INT; 522 379 523 380 case IIO_CHAN_INFO_SCALE: 524 381 *val = 0; 525 - *val2 = 19163; /* range +-4g (4/2047*9.806650) */ 382 + *val2 = KXCJK1013_scale_table[data->range].scale; 526 383 return IIO_VAL_INT_PLUS_MICRO; 527 384 528 385 case IIO_CHAN_INFO_SAMP_FREQ: ··· 545 410 ret = kxcjk1013_set_odr(data, val, val2); 546 411 mutex_unlock(&data->mutex); 547 412 break; 413 + case IIO_CHAN_INFO_SCALE: 414 + if (val) 415 + return -EINVAL; 416 + 417 + mutex_lock(&data->mutex); 418 + ret = kxcjk1013_set_scale(data, val2); 419 + mutex_unlock(&data->mutex); 420 + break; 548 421 default: 549 422 ret = -EINVAL; 550 423 } ··· 574 431 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( 575 432 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600"); 576 433 434 + static IIO_CONST_ATTR(in_accel_scale_available, "0.009582 0.019163 0.038326"); 435 + 577 436 static struct attribute *kxcjk1013_attributes[] = { 578 437 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 438 + &iio_const_attr_in_accel_scale_available.dev_attr.attr, 579 439 NULL, 580 440 }; 581 441 ··· 666 520 { 667 521 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 668 522 struct kxcjk1013_data *data = iio_priv(indio_dev); 523 + int ret; 524 + 525 + if (state && data->trigger_on) 526 + return 0; 669 527 670 528 mutex_lock(&data->mutex); 671 - if (state) { 672 - kxcjk1013_chip_setup_interrupt(data, true); 673 - kxcjk1013_set_mode(data, OPERATION); 674 - ++data->power_state; 675 - } else { 676 - if (--data->power_state) { 529 + ret = kxcjk1013_chip_setup_interrupt(data, state); 530 + if (!ret) { 531 + ret = kxcjk1013_set_power_state(data, state); 532 + if (ret < 0) { 677 533 mutex_unlock(&data->mutex); 678 - return 0; 534 + return ret; 679 535 } 680 - kxcjk1013_chip_setup_interrupt(data, false); 681 - kxcjk1013_set_mode(data, STANDBY); 682 536 } 537 + data->trigger_on = state; 683 538 mutex_unlock(&data->mutex); 684 539 685 540 return 0; ··· 808 661 } 809 662 } 810 663 811 - ret = devm_iio_device_register(&client->dev, indio_dev); 664 + ret = iio_device_register(indio_dev); 812 665 if (ret < 0) { 813 666 dev_err(&client->dev, "unable to register iio device\n"); 814 667 goto err_buffer_cleanup; 815 668 } 816 669 670 + ret = pm_runtime_set_active(&client->dev); 671 + if (ret) 672 + goto err_iio_unregister; 673 + 674 + pm_runtime_enable(&client->dev); 675 + pm_runtime_set_autosuspend_delay(&client->dev, 676 + KXCJK1013_SLEEP_DELAY_MS); 677 + pm_runtime_use_autosuspend(&client->dev); 678 + 817 679 return 0; 818 680 681 + err_iio_unregister: 682 + iio_device_unregister(indio_dev); 819 683 err_buffer_cleanup: 820 684 if (data->trig_mode) 821 685 iio_triggered_buffer_cleanup(indio_dev); ··· 844 686 { 845 687 struct iio_dev *indio_dev = i2c_get_clientdata(client); 846 688 struct kxcjk1013_data *data = iio_priv(indio_dev); 689 + 690 + pm_runtime_disable(&client->dev); 691 + pm_runtime_set_suspended(&client->dev); 692 + pm_runtime_put_noidle(&client->dev); 693 + 694 + iio_device_unregister(indio_dev); 847 695 848 696 if (data->trig_mode) { 849 697 iio_triggered_buffer_cleanup(indio_dev); ··· 869 705 { 870 706 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 871 707 struct kxcjk1013_data *data = iio_priv(indio_dev); 708 + int ret; 872 709 873 710 mutex_lock(&data->mutex); 874 - kxcjk1013_set_mode(data, STANDBY); 711 + ret = kxcjk1013_set_mode(data, STANDBY); 875 712 mutex_unlock(&data->mutex); 876 713 877 - return 0; 714 + return ret; 878 715 } 879 716 880 717 static int kxcjk1013_resume(struct device *dev) 881 718 { 882 719 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 883 720 struct kxcjk1013_data *data = iio_priv(indio_dev); 721 + int ret = 0; 884 722 885 723 mutex_lock(&data->mutex); 886 - 887 - if (data->power_state) 888 - kxcjk1013_set_mode(data, OPERATION); 889 - 724 + /* Check, if the suspend occured while active */ 725 + if (data->trigger_on) 726 + ret = kxcjk1013_set_mode(data, OPERATION); 890 727 mutex_unlock(&data->mutex); 728 + 729 + return ret; 730 + } 731 + #endif 732 + 733 + #ifdef CONFIG_PM_RUNTIME 734 + static int kxcjk1013_runtime_suspend(struct device *dev) 735 + { 736 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 737 + struct kxcjk1013_data *data = iio_priv(indio_dev); 738 + 739 + return kxcjk1013_set_mode(data, STANDBY); 740 + } 741 + 742 + static int kxcjk1013_runtime_resume(struct device *dev) 743 + { 744 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 745 + struct kxcjk1013_data *data = iio_priv(indio_dev); 746 + int ret; 747 + int sleep_val; 748 + 749 + ret = kxcjk1013_set_mode(data, OPERATION); 750 + if (ret < 0) 751 + return ret; 752 + 753 + sleep_val = kxcjk1013_get_startup_times(data); 754 + if (sleep_val < 20000) 755 + usleep_range(sleep_val, 20000); 756 + else 757 + msleep_interruptible(sleep_val/1000); 891 758 892 759 return 0; 893 760 } 894 - 895 - static SIMPLE_DEV_PM_OPS(kxcjk1013_pm_ops, kxcjk1013_suspend, kxcjk1013_resume); 896 - #define KXCJK1013_PM_OPS (&kxcjk1013_pm_ops) 897 - #else 898 - #define KXCJK1013_PM_OPS NULL 899 761 #endif 762 + 763 + static const struct dev_pm_ops kxcjk1013_pm_ops = { 764 + SET_SYSTEM_SLEEP_PM_OPS(kxcjk1013_suspend, kxcjk1013_resume) 765 + SET_RUNTIME_PM_OPS(kxcjk1013_runtime_suspend, 766 + kxcjk1013_runtime_resume, NULL) 767 + }; 900 768 901 769 static const struct acpi_device_id kx_acpi_match[] = { 902 770 {"KXCJ1013", 0}, ··· 947 751 .driver = { 948 752 .name = KXCJK1013_DRV_NAME, 949 753 .acpi_match_table = ACPI_PTR(kx_acpi_match), 950 - .pm = KXCJK1013_PM_OPS, 754 + .pm = &kxcjk1013_pm_ops, 951 755 }, 952 756 .probe = kxcjk1013_probe, 953 757 .remove = kxcjk1013_remove,
+21 -1
drivers/iio/adc/Kconfig
··· 129 129 130 130 config EXYNOS_ADC 131 131 tristate "Exynos ADC driver support" 132 - depends on ARCH_EXYNOS || (OF && COMPILE_TEST) 132 + depends on ARCH_EXYNOS || ARCH_S3C24XX || ARCH_S3C64XX || (OF && COMPILE_TEST) 133 133 help 134 134 Core support for the ADC block found in the Samsung EXYNOS series 135 135 of SoCs for drivers such as the touchscreen and hwmon to use to share ··· 206 206 To compile this driver as a module, choose M here: the 207 207 module will be called nau7802. 208 208 209 + config ROCKCHIP_SARADC 210 + tristate "Rockchip SARADC driver" 211 + depends on ARCH_ROCKCHIP || (ARM && COMPILE_TEST) 212 + help 213 + Say yes here to build support for the SARADC found in SoCs from 214 + Rockchip. 215 + 216 + To compile this driver as a module, choose M here: the 217 + module will be called rockchip_saradc. 218 + 209 219 config TI_ADC081C 210 220 tristate "Texas Instruments ADC081C021/027" 211 221 depends on I2C ··· 225 215 226 216 This driver can also be built as a module. If so, the module will be 227 217 called ti-adc081c. 218 + 219 + config TI_ADC128S052 220 + tristate "Texas Instruments ADC128S052" 221 + depends on SPI 222 + help 223 + If you say yes here you get support for Texas Instruments ADC128S052 224 + chip. 225 + 226 + This driver can also be built as a module. If so, the module will be 227 + called ti-adc128s052. 228 228 229 229 config TI_AM335X_ADC 230 230 tristate "TI's AM335X ADC driver"
+2
drivers/iio/adc/Makefile
··· 22 22 obj-$(CONFIG_MCP3422) += mcp3422.o 23 23 obj-$(CONFIG_MEN_Z188_ADC) += men_z188_adc.o 24 24 obj-$(CONFIG_NAU7802) += nau7802.o 25 + obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o 25 26 obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o 27 + obj-$(CONFIG_TI_ADC128S052) += ti-adc128s052.o 26 28 obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o 27 29 obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o 28 30 obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
+127 -10
drivers/iio/adc/exynos_adc.c
··· 40 40 #include <linux/iio/machine.h> 41 41 #include <linux/iio/driver.h> 42 42 43 - /* EXYNOS4412/5250 ADC_V1 registers definitions */ 43 + /* S3C/EXYNOS4412/5250 ADC_V1 registers definitions */ 44 44 #define ADC_V1_CON(x) ((x) + 0x00) 45 45 #define ADC_V1_DLY(x) ((x) + 0x08) 46 46 #define ADC_V1_DATX(x) ((x) + 0x0C) 47 47 #define ADC_V1_INTCLR(x) ((x) + 0x18) 48 48 #define ADC_V1_MUX(x) ((x) + 0x1c) 49 + 50 + /* S3C2410 ADC registers definitions */ 51 + #define ADC_S3C2410_MUX(x) ((x) + 0x18) 49 52 50 53 /* Future ADC_V2 registers definitions */ 51 54 #define ADC_V2_CON1(x) ((x) + 0x00) ··· 63 60 #define ADC_V1_CON_PRSCEN (1u << 14) 64 61 #define ADC_V1_CON_PRSCLV(x) (((x) & 0xFF) << 6) 65 62 #define ADC_V1_CON_STANDBY (1u << 2) 63 + 64 + /* Bit definitions for S3C2410 ADC */ 65 + #define ADC_S3C2410_CON_SELMUX(x) (((x) & 7) << 3) 66 + #define ADC_S3C2410_DATX_MASK 0x3FF 67 + #define ADC_S3C2416_CON_RES_SEL (1u << 3) 66 68 67 69 /* Bit definitions for ADC_V2 */ 68 70 #define ADC_V2_CON1_SOFT_RESET (1u << 2) ··· 85 77 86 78 /* Bit definitions common for ADC_V1 and ADC_V2 */ 87 79 #define ADC_CON_EN_START (1u << 0) 80 + #define ADC_CON_EN_START_MASK (0x3 << 0) 88 81 #define ADC_DATX_MASK 0xFFF 89 82 90 83 #define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(100)) ··· 109 100 struct exynos_adc_data { 110 101 int num_channels; 111 102 bool needs_sclk; 103 + bool needs_adc_phy; 104 + u32 mask; 112 105 113 106 void (*init_hw)(struct exynos_adc *info); 114 107 void (*exit_hw)(struct exynos_adc *info); ··· 182 171 { 183 172 u32 con1; 184 173 185 - writel(1, info->enable_reg); 174 + if (info->data->needs_adc_phy) 175 + writel(1, info->enable_reg); 186 176 187 177 /* set default prescaler values and Enable prescaler */ 188 178 con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN; ··· 197 185 { 198 186 u32 con; 199 187 200 - writel(0, info->enable_reg); 188 + if (info->data->needs_adc_phy) 189 + writel(0, info->enable_reg); 201 190 202 191 con = readl(ADC_V1_CON(info->regs)); 203 192 con |= ADC_V1_CON_STANDBY; ··· 223 210 224 211 static const struct exynos_adc_data exynos_adc_v1_data = { 225 212 .num_channels = MAX_ADC_V1_CHANNELS, 213 + .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */ 214 + .needs_adc_phy = true, 226 215 227 216 .init_hw = exynos_adc_v1_init_hw, 228 217 .exit_hw = exynos_adc_v1_exit_hw, ··· 232 217 .start_conv = exynos_adc_v1_start_conv, 233 218 }; 234 219 220 + static void exynos_adc_s3c2416_start_conv(struct exynos_adc *info, 221 + unsigned long addr) 222 + { 223 + u32 con1; 224 + 225 + /* Enable 12 bit ADC resolution */ 226 + con1 = readl(ADC_V1_CON(info->regs)); 227 + con1 |= ADC_S3C2416_CON_RES_SEL; 228 + writel(con1, ADC_V1_CON(info->regs)); 229 + 230 + /* Select channel for S3C2416 */ 231 + writel(addr, ADC_S3C2410_MUX(info->regs)); 232 + 233 + con1 = readl(ADC_V1_CON(info->regs)); 234 + writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs)); 235 + } 236 + 237 + static struct exynos_adc_data const exynos_adc_s3c2416_data = { 238 + .num_channels = MAX_ADC_V1_CHANNELS, 239 + .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */ 240 + 241 + .init_hw = exynos_adc_v1_init_hw, 242 + .exit_hw = exynos_adc_v1_exit_hw, 243 + .start_conv = exynos_adc_s3c2416_start_conv, 244 + }; 245 + 246 + static void exynos_adc_s3c2443_start_conv(struct exynos_adc *info, 247 + unsigned long addr) 248 + { 249 + u32 con1; 250 + 251 + /* Select channel for S3C2433 */ 252 + writel(addr, ADC_S3C2410_MUX(info->regs)); 253 + 254 + con1 = readl(ADC_V1_CON(info->regs)); 255 + writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs)); 256 + } 257 + 258 + static struct exynos_adc_data const exynos_adc_s3c2443_data = { 259 + .num_channels = MAX_ADC_V1_CHANNELS, 260 + .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */ 261 + 262 + .init_hw = exynos_adc_v1_init_hw, 263 + .exit_hw = exynos_adc_v1_exit_hw, 264 + .start_conv = exynos_adc_s3c2443_start_conv, 265 + }; 266 + 267 + static void exynos_adc_s3c64xx_start_conv(struct exynos_adc *info, 268 + unsigned long addr) 269 + { 270 + u32 con1; 271 + 272 + con1 = readl(ADC_V1_CON(info->regs)); 273 + con1 &= ~ADC_S3C2410_CON_SELMUX(0x7); 274 + con1 |= ADC_S3C2410_CON_SELMUX(addr); 275 + writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs)); 276 + } 277 + 278 + static struct exynos_adc_data const exynos_adc_s3c24xx_data = { 279 + .num_channels = MAX_ADC_V1_CHANNELS, 280 + .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */ 281 + 282 + .init_hw = exynos_adc_v1_init_hw, 283 + .exit_hw = exynos_adc_v1_exit_hw, 284 + .start_conv = exynos_adc_s3c64xx_start_conv, 285 + }; 286 + 287 + static struct exynos_adc_data const exynos_adc_s3c64xx_data = { 288 + .num_channels = MAX_ADC_V1_CHANNELS, 289 + .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */ 290 + 291 + .init_hw = exynos_adc_v1_init_hw, 292 + .exit_hw = exynos_adc_v1_exit_hw, 293 + .clear_irq = exynos_adc_v1_clear_irq, 294 + .start_conv = exynos_adc_s3c64xx_start_conv, 295 + }; 296 + 235 297 static void exynos_adc_v2_init_hw(struct exynos_adc *info) 236 298 { 237 299 u32 con1, con2; 238 300 239 - writel(1, info->enable_reg); 301 + if (info->data->needs_adc_phy) 302 + writel(1, info->enable_reg); 240 303 241 304 con1 = ADC_V2_CON1_SOFT_RESET; 242 305 writel(con1, ADC_V2_CON1(info->regs)); ··· 331 238 { 332 239 u32 con; 333 240 334 - writel(0, info->enable_reg); 241 + if (info->data->needs_adc_phy) 242 + writel(0, info->enable_reg); 335 243 336 244 con = readl(ADC_V2_CON1(info->regs)); 337 245 con &= ~ADC_CON_EN_START; ··· 360 266 361 267 static const struct exynos_adc_data exynos_adc_v2_data = { 362 268 .num_channels = MAX_ADC_V2_CHANNELS, 269 + .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */ 270 + .needs_adc_phy = true, 363 271 364 272 .init_hw = exynos_adc_v2_init_hw, 365 273 .exit_hw = exynos_adc_v2_exit_hw, ··· 371 275 372 276 static const struct exynos_adc_data exynos3250_adc_data = { 373 277 .num_channels = MAX_EXYNOS3250_ADC_CHANNELS, 278 + .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */ 374 279 .needs_sclk = true, 280 + .needs_adc_phy = true, 375 281 376 282 .init_hw = exynos_adc_v2_init_hw, 377 283 .exit_hw = exynos_adc_v2_exit_hw, ··· 383 285 384 286 static const struct of_device_id exynos_adc_match[] = { 385 287 { 288 + .compatible = "samsung,s3c2410-adc", 289 + .data = &exynos_adc_s3c24xx_data, 290 + }, { 291 + .compatible = "samsung,s3c2416-adc", 292 + .data = &exynos_adc_s3c2416_data, 293 + }, { 294 + .compatible = "samsung,s3c2440-adc", 295 + .data = &exynos_adc_s3c24xx_data, 296 + }, { 297 + .compatible = "samsung,s3c2443-adc", 298 + .data = &exynos_adc_s3c2443_data, 299 + }, { 300 + .compatible = "samsung,s3c6410-adc", 301 + .data = &exynos_adc_s3c64xx_data, 302 + }, { 386 303 .compatible = "samsung,exynos-adc-v1", 387 304 .data = &exynos_adc_v1_data, 388 305 }, { ··· 460 347 static irqreturn_t exynos_adc_isr(int irq, void *dev_id) 461 348 { 462 349 struct exynos_adc *info = (struct exynos_adc *)dev_id; 350 + u32 mask = info->data->mask; 463 351 464 352 /* Read value */ 465 - info->value = readl(ADC_V1_DATX(info->regs)) & ADC_DATX_MASK; 353 + info->value = readl(ADC_V1_DATX(info->regs)) & mask; 466 354 467 355 /* clear irq */ 468 356 if (info->data->clear_irq) ··· 556 442 if (IS_ERR(info->regs)) 557 443 return PTR_ERR(info->regs); 558 444 559 - mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); 560 - info->enable_reg = devm_ioremap_resource(&pdev->dev, mem); 561 - if (IS_ERR(info->enable_reg)) 562 - return PTR_ERR(info->enable_reg); 445 + 446 + if (info->data->needs_adc_phy) { 447 + mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); 448 + info->enable_reg = devm_ioremap_resource(&pdev->dev, mem); 449 + if (IS_ERR(info->enable_reg)) 450 + return PTR_ERR(info->enable_reg); 451 + } 563 452 564 453 irq = platform_get_irq(pdev, 0); 565 454 if (irq < 0) {
+317
drivers/iio/adc/rockchip_saradc.c
··· 1 + /* 2 + * Rockchip Successive Approximation Register (SAR) A/D Converter 3 + * Copyright (C) 2014 ROCKCHIP, Inc. 4 + * 5 + * This program is free software; you can redistribute it and/or modify 6 + * it under the terms of the GNU General Public License as published by 7 + * the Free Software Foundation; either version 2 of the License, or 8 + * (at your option) any later version. 9 + * 10 + * This program is distributed in the hope that it will be useful, 11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 + * GNU General Public License for more details. 14 + */ 15 + 16 + #include <linux/module.h> 17 + #include <linux/platform_device.h> 18 + #include <linux/interrupt.h> 19 + #include <linux/io.h> 20 + #include <linux/of.h> 21 + #include <linux/clk.h> 22 + #include <linux/completion.h> 23 + #include <linux/regulator/consumer.h> 24 + #include <linux/iio/iio.h> 25 + 26 + #define SARADC_DATA 0x00 27 + #define SARADC_DATA_MASK 0x3ff 28 + 29 + #define SARADC_STAS 0x04 30 + #define SARADC_STAS_BUSY BIT(0) 31 + 32 + #define SARADC_CTRL 0x08 33 + #define SARADC_CTRL_IRQ_STATUS BIT(6) 34 + #define SARADC_CTRL_IRQ_ENABLE BIT(5) 35 + #define SARADC_CTRL_POWER_CTRL BIT(3) 36 + #define SARADC_CTRL_CHN_MASK 0x7 37 + 38 + #define SARADC_DLY_PU_SOC 0x0c 39 + #define SARADC_DLY_PU_SOC_MASK 0x3f 40 + 41 + #define SARADC_BITS 10 42 + #define SARADC_TIMEOUT msecs_to_jiffies(100) 43 + 44 + struct rockchip_saradc { 45 + void __iomem *regs; 46 + struct clk *pclk; 47 + struct clk *clk; 48 + struct completion completion; 49 + struct regulator *vref; 50 + u16 last_val; 51 + }; 52 + 53 + static int rockchip_saradc_read_raw(struct iio_dev *indio_dev, 54 + struct iio_chan_spec const *chan, 55 + int *val, int *val2, long mask) 56 + { 57 + struct rockchip_saradc *info = iio_priv(indio_dev); 58 + int ret; 59 + 60 + switch (mask) { 61 + case IIO_CHAN_INFO_RAW: 62 + mutex_lock(&indio_dev->mlock); 63 + 64 + reinit_completion(&info->completion); 65 + 66 + /* 8 clock periods as delay between power up and start cmd */ 67 + writel_relaxed(8, info->regs + SARADC_DLY_PU_SOC); 68 + 69 + /* Select the channel to be used and trigger conversion */ 70 + writel(SARADC_CTRL_POWER_CTRL 71 + | (chan->channel & SARADC_CTRL_CHN_MASK) 72 + | SARADC_CTRL_IRQ_ENABLE, 73 + info->regs + SARADC_CTRL); 74 + 75 + if (!wait_for_completion_timeout(&info->completion, 76 + SARADC_TIMEOUT)) { 77 + writel_relaxed(0, info->regs + SARADC_CTRL); 78 + mutex_unlock(&indio_dev->mlock); 79 + return -ETIMEDOUT; 80 + } 81 + 82 + *val = info->last_val; 83 + mutex_unlock(&indio_dev->mlock); 84 + return IIO_VAL_INT; 85 + case IIO_CHAN_INFO_SCALE: 86 + ret = regulator_get_voltage(info->vref); 87 + if (ret < 0) { 88 + dev_err(&indio_dev->dev, "failed to get voltage\n"); 89 + return ret; 90 + } 91 + 92 + *val = ret / 1000; 93 + *val2 = SARADC_BITS; 94 + return IIO_VAL_FRACTIONAL_LOG2; 95 + default: 96 + return -EINVAL; 97 + } 98 + } 99 + 100 + static irqreturn_t rockchip_saradc_isr(int irq, void *dev_id) 101 + { 102 + struct rockchip_saradc *info = (struct rockchip_saradc *)dev_id; 103 + 104 + /* Read value */ 105 + info->last_val = readl_relaxed(info->regs + SARADC_DATA); 106 + info->last_val &= SARADC_DATA_MASK; 107 + 108 + /* Clear irq & power down adc */ 109 + writel_relaxed(0, info->regs + SARADC_CTRL); 110 + 111 + complete(&info->completion); 112 + 113 + return IRQ_HANDLED; 114 + } 115 + 116 + static const struct iio_info rockchip_saradc_iio_info = { 117 + .read_raw = rockchip_saradc_read_raw, 118 + .driver_module = THIS_MODULE, 119 + }; 120 + 121 + #define ADC_CHANNEL(_index, _id) { \ 122 + .type = IIO_VOLTAGE, \ 123 + .indexed = 1, \ 124 + .channel = _index, \ 125 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 126 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 127 + .datasheet_name = _id, \ 128 + } 129 + 130 + static const struct iio_chan_spec rockchip_saradc_iio_channels[] = { 131 + ADC_CHANNEL(0, "adc0"), 132 + ADC_CHANNEL(1, "adc1"), 133 + ADC_CHANNEL(2, "adc2"), 134 + }; 135 + 136 + static int rockchip_saradc_probe(struct platform_device *pdev) 137 + { 138 + struct rockchip_saradc *info = NULL; 139 + struct device_node *np = pdev->dev.of_node; 140 + struct iio_dev *indio_dev = NULL; 141 + struct resource *mem; 142 + int ret; 143 + int irq; 144 + u32 rate; 145 + 146 + if (!np) 147 + return -ENODEV; 148 + 149 + indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info)); 150 + if (!indio_dev) { 151 + dev_err(&pdev->dev, "failed allocating iio device\n"); 152 + return -ENOMEM; 153 + } 154 + info = iio_priv(indio_dev); 155 + 156 + mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 157 + info->regs = devm_ioremap_resource(&pdev->dev, mem); 158 + if (IS_ERR(info->regs)) 159 + return PTR_ERR(info->regs); 160 + 161 + init_completion(&info->completion); 162 + 163 + irq = platform_get_irq(pdev, 0); 164 + if (irq < 0) { 165 + dev_err(&pdev->dev, "no irq resource?\n"); 166 + return irq; 167 + } 168 + 169 + ret = devm_request_irq(&pdev->dev, irq, rockchip_saradc_isr, 170 + 0, dev_name(&pdev->dev), info); 171 + if (ret < 0) { 172 + dev_err(&pdev->dev, "failed requesting irq %d\n", irq); 173 + return ret; 174 + } 175 + 176 + info->pclk = devm_clk_get(&pdev->dev, "apb_pclk"); 177 + if (IS_ERR(info->pclk)) { 178 + dev_err(&pdev->dev, "failed to get pclk\n"); 179 + return PTR_ERR(info->pclk); 180 + } 181 + 182 + info->clk = devm_clk_get(&pdev->dev, "saradc"); 183 + if (IS_ERR(info->clk)) { 184 + dev_err(&pdev->dev, "failed to get adc clock\n"); 185 + return PTR_ERR(info->clk); 186 + } 187 + 188 + info->vref = devm_regulator_get(&pdev->dev, "vref"); 189 + if (IS_ERR(info->vref)) { 190 + dev_err(&pdev->dev, "failed to get regulator, %ld\n", 191 + PTR_ERR(info->vref)); 192 + return PTR_ERR(info->vref); 193 + } 194 + 195 + /* 196 + * Use a default of 1MHz for the converter clock. 197 + * This may become user-configurable in the future. 198 + */ 199 + ret = clk_set_rate(info->clk, 1000000); 200 + if (ret < 0) { 201 + dev_err(&pdev->dev, "failed to set adc clk rate, %d\n", ret); 202 + return ret; 203 + } 204 + 205 + ret = regulator_enable(info->vref); 206 + if (ret < 0) { 207 + dev_err(&pdev->dev, "failed to enable vref regulator\n"); 208 + return ret; 209 + } 210 + 211 + ret = clk_prepare_enable(info->pclk); 212 + if (ret < 0) { 213 + dev_err(&pdev->dev, "failed to enable pclk\n"); 214 + goto err_reg_voltage; 215 + } 216 + 217 + ret = clk_prepare_enable(info->clk); 218 + if (ret < 0) { 219 + dev_err(&pdev->dev, "failed to enable converter clock\n"); 220 + goto err_pclk; 221 + } 222 + 223 + platform_set_drvdata(pdev, indio_dev); 224 + 225 + indio_dev->name = dev_name(&pdev->dev); 226 + indio_dev->dev.parent = &pdev->dev; 227 + indio_dev->dev.of_node = pdev->dev.of_node; 228 + indio_dev->info = &rockchip_saradc_iio_info; 229 + indio_dev->modes = INDIO_DIRECT_MODE; 230 + 231 + indio_dev->channels = rockchip_saradc_iio_channels; 232 + indio_dev->num_channels = ARRAY_SIZE(rockchip_saradc_iio_channels); 233 + 234 + ret = iio_device_register(indio_dev); 235 + if (ret) 236 + goto err_clk; 237 + 238 + return 0; 239 + 240 + err_clk: 241 + clk_disable_unprepare(info->clk); 242 + err_pclk: 243 + clk_disable_unprepare(info->pclk); 244 + err_reg_voltage: 245 + regulator_disable(info->vref); 246 + return ret; 247 + } 248 + 249 + static int rockchip_saradc_remove(struct platform_device *pdev) 250 + { 251 + struct iio_dev *indio_dev = platform_get_drvdata(pdev); 252 + struct rockchip_saradc *info = iio_priv(indio_dev); 253 + 254 + iio_device_unregister(indio_dev); 255 + clk_disable_unprepare(info->clk); 256 + clk_disable_unprepare(info->pclk); 257 + regulator_disable(info->vref); 258 + 259 + return 0; 260 + } 261 + 262 + #ifdef CONFIG_PM_SLEEP 263 + static int rockchip_saradc_suspend(struct device *dev) 264 + { 265 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 266 + struct rockchip_saradc *info = iio_priv(indio_dev); 267 + 268 + clk_disable_unprepare(info->clk); 269 + clk_disable_unprepare(info->pclk); 270 + regulator_disable(info->vref); 271 + 272 + return 0; 273 + } 274 + 275 + static int rockchip_saradc_resume(struct device *dev) 276 + { 277 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 278 + struct rockchip_saradc *info = iio_priv(indio_dev); 279 + int ret; 280 + 281 + ret = regulator_enable(info->vref); 282 + if (ret) 283 + return ret; 284 + 285 + ret = clk_prepare_enable(info->pclk); 286 + if (ret) 287 + return ret; 288 + 289 + ret = clk_prepare_enable(info->clk); 290 + if (ret) 291 + return ret; 292 + 293 + return ret; 294 + } 295 + #endif 296 + 297 + static SIMPLE_DEV_PM_OPS(rockchip_saradc_pm_ops, 298 + rockchip_saradc_suspend, rockchip_saradc_resume); 299 + 300 + static const struct of_device_id rockchip_saradc_match[] = { 301 + { .compatible = "rockchip,saradc" }, 302 + {}, 303 + }; 304 + MODULE_DEVICE_TABLE(of, rockchip_saradc_match); 305 + 306 + static struct platform_driver rockchip_saradc_driver = { 307 + .probe = rockchip_saradc_probe, 308 + .remove = rockchip_saradc_remove, 309 + .driver = { 310 + .name = "rockchip-saradc", 311 + .owner = THIS_MODULE, 312 + .of_match_table = rockchip_saradc_match, 313 + .pm = &rockchip_saradc_pm_ops, 314 + }, 315 + }; 316 + 317 + module_platform_driver(rockchip_saradc_driver);
+179
drivers/iio/adc/ti-adc128s052.c
··· 1 + /* 2 + * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com> 3 + * 4 + * Driver for Texas Instruments' ADC128S052 ADC chip. 5 + * Datasheet can be found here: 6 + * http://www.ti.com/lit/ds/symlink/adc128s052.pdf 7 + * 8 + * This program is free software; you can redistribute it and/or modify 9 + * it under the terms of the GNU General Public License version 2 as 10 + * published by the Free Software Foundation. 11 + */ 12 + 13 + #include <linux/err.h> 14 + #include <linux/spi/spi.h> 15 + #include <linux/module.h> 16 + #include <linux/iio/iio.h> 17 + #include <linux/regulator/consumer.h> 18 + 19 + struct adc128 { 20 + struct spi_device *spi; 21 + 22 + struct regulator *reg; 23 + struct mutex lock; 24 + 25 + u8 buffer[2] ____cacheline_aligned; 26 + }; 27 + 28 + static int adc128_adc_conversion(struct adc128 *adc, u8 channel) 29 + { 30 + int ret; 31 + 32 + mutex_lock(&adc->lock); 33 + 34 + adc->buffer[0] = channel << 3; 35 + adc->buffer[1] = 0; 36 + 37 + ret = spi_write(adc->spi, &adc->buffer, 2); 38 + if (ret < 0) { 39 + mutex_unlock(&adc->lock); 40 + return ret; 41 + } 42 + 43 + ret = spi_read(adc->spi, &adc->buffer, 2); 44 + 45 + mutex_unlock(&adc->lock); 46 + 47 + if (ret < 0) 48 + return ret; 49 + 50 + return ((adc->buffer[0] << 8 | adc->buffer[1]) & 0xFFF); 51 + } 52 + 53 + static int adc128_read_raw(struct iio_dev *indio_dev, 54 + struct iio_chan_spec const *channel, int *val, 55 + int *val2, long mask) 56 + { 57 + struct adc128 *adc = iio_priv(indio_dev); 58 + int ret; 59 + 60 + switch (mask) { 61 + case IIO_CHAN_INFO_RAW: 62 + 63 + ret = adc128_adc_conversion(adc, channel->channel); 64 + if (ret < 0) 65 + return ret; 66 + 67 + *val = ret; 68 + return IIO_VAL_INT; 69 + 70 + case IIO_CHAN_INFO_SCALE: 71 + 72 + ret = regulator_get_voltage(adc->reg); 73 + if (ret < 0) 74 + return ret; 75 + 76 + *val = ret / 1000; 77 + *val2 = 12; 78 + return IIO_VAL_FRACTIONAL_LOG2; 79 + 80 + default: 81 + return -EINVAL; 82 + } 83 + 84 + } 85 + 86 + #define ADC128_VOLTAGE_CHANNEL(num) \ 87 + { \ 88 + .type = IIO_VOLTAGE, \ 89 + .indexed = 1, \ 90 + .channel = (num), \ 91 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 92 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \ 93 + } 94 + 95 + static const struct iio_chan_spec adc128_channels[] = { 96 + ADC128_VOLTAGE_CHANNEL(0), 97 + ADC128_VOLTAGE_CHANNEL(1), 98 + ADC128_VOLTAGE_CHANNEL(2), 99 + ADC128_VOLTAGE_CHANNEL(3), 100 + ADC128_VOLTAGE_CHANNEL(4), 101 + ADC128_VOLTAGE_CHANNEL(5), 102 + ADC128_VOLTAGE_CHANNEL(6), 103 + ADC128_VOLTAGE_CHANNEL(7), 104 + }; 105 + 106 + static const struct iio_info adc128_info = { 107 + .read_raw = adc128_read_raw, 108 + .driver_module = THIS_MODULE, 109 + }; 110 + 111 + static int adc128_probe(struct spi_device *spi) 112 + { 113 + struct iio_dev *indio_dev; 114 + struct adc128 *adc; 115 + int ret; 116 + 117 + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc)); 118 + if (!indio_dev) 119 + return -ENOMEM; 120 + 121 + adc = iio_priv(indio_dev); 122 + adc->spi = spi; 123 + 124 + spi_set_drvdata(spi, indio_dev); 125 + 126 + indio_dev->dev.parent = &spi->dev; 127 + indio_dev->name = spi_get_device_id(spi)->name; 128 + indio_dev->modes = INDIO_DIRECT_MODE; 129 + indio_dev->info = &adc128_info; 130 + 131 + indio_dev->channels = adc128_channels; 132 + indio_dev->num_channels = ARRAY_SIZE(adc128_channels); 133 + 134 + adc->reg = devm_regulator_get(&spi->dev, "vref"); 135 + if (IS_ERR(adc->reg)) 136 + return PTR_ERR(adc->reg); 137 + 138 + ret = regulator_enable(adc->reg); 139 + if (ret < 0) 140 + return ret; 141 + 142 + mutex_init(&adc->lock); 143 + 144 + ret = iio_device_register(indio_dev); 145 + 146 + return ret; 147 + } 148 + 149 + static int adc128_remove(struct spi_device *spi) 150 + { 151 + struct iio_dev *indio_dev = spi_get_drvdata(spi); 152 + struct adc128 *adc = iio_priv(indio_dev); 153 + 154 + iio_device_unregister(indio_dev); 155 + regulator_disable(adc->reg); 156 + 157 + return 0; 158 + } 159 + 160 + static const struct spi_device_id adc128_id[] = { 161 + { "adc128s052", 0}, 162 + { } 163 + }; 164 + MODULE_DEVICE_TABLE(spi, adc128_id); 165 + 166 + static struct spi_driver adc128_driver = { 167 + .driver = { 168 + .name = "adc128s052", 169 + .owner = THIS_MODULE, 170 + }, 171 + .probe = adc128_probe, 172 + .remove = adc128_remove, 173 + .id_table = adc128_id, 174 + }; 175 + module_spi_driver(adc128_driver); 176 + 177 + MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>"); 178 + MODULE_DESCRIPTION("Texas Instruments ADC128S052"); 179 + MODULE_LICENSE("GPL v2");
+6 -3
drivers/iio/adc/xilinx-xadc-core.c
··· 1201 1201 goto err_device_free; 1202 1202 1203 1203 xadc->convst_trigger = xadc_alloc_trigger(indio_dev, "convst"); 1204 - if (IS_ERR(xadc->convst_trigger)) 1204 + if (IS_ERR(xadc->convst_trigger)) { 1205 + ret = PTR_ERR(xadc->convst_trigger); 1205 1206 goto err_triggered_buffer_cleanup; 1207 + } 1206 1208 xadc->samplerate_trigger = xadc_alloc_trigger(indio_dev, 1207 1209 "samplerate"); 1208 - if (IS_ERR(xadc->samplerate_trigger)) 1210 + if (IS_ERR(xadc->samplerate_trigger)) { 1211 + ret = PTR_ERR(xadc->samplerate_trigger); 1209 1212 goto err_free_convst_trigger; 1213 + } 1210 1214 } 1211 1215 1212 1216 xadc->clk = devm_clk_get(&pdev->dev, NULL); ··· 1326 1322 .remove = xadc_remove, 1327 1323 .driver = { 1328 1324 .name = "xadc", 1329 - .owner = THIS_MODULE, 1330 1325 .of_match_table = xadc_of_match_table, 1331 1326 }, 1332 1327 };
+8
drivers/iio/dac/Kconfig
··· 152 152 This driver can also be built as a module. If so, the module 153 153 will be called max517. 154 154 155 + config MAX5821 156 + tristate "Maxim MAX5821 DAC driver" 157 + depends on I2C 158 + depends on OF 159 + help 160 + Say yes here to build support for Maxim MAX5821 161 + 10 bits DAC. 162 + 155 163 config MCP4725 156 164 tristate "MCP4725 DAC driver" 157 165 depends on I2C
+1
drivers/iio/dac/Makefile
··· 17 17 obj-$(CONFIG_AD5686) += ad5686.o 18 18 obj-$(CONFIG_AD7303) += ad7303.o 19 19 obj-$(CONFIG_MAX517) += max517.o 20 + obj-$(CONFIG_MAX5821) += max5821.o 20 21 obj-$(CONFIG_MCP4725) += mcp4725.o 21 22 obj-$(CONFIG_MCP4922) += mcp4922.o
+405
drivers/iio/dac/max5821.c
··· 1 + /* 2 + * iio/dac/max5821.c 3 + * Copyright (C) 2014 Philippe Reynes 4 + * 5 + * This program is free software; you can redistribute it and/or modify 6 + * it under the terms of the GNU General Public License version 2 as 7 + * published by the Free Software Foundation. 8 + */ 9 + 10 + #include <linux/kernel.h> 11 + #include <linux/module.h> 12 + #include <linux/i2c.h> 13 + #include <linux/iio/iio.h> 14 + #include <linux/regulator/consumer.h> 15 + 16 + #define MAX5821_MAX_DAC_CHANNELS 2 17 + 18 + /* command bytes */ 19 + #define MAX5821_LOAD_DAC_A_IN_REG_B 0x00 20 + #define MAX5821_LOAD_DAC_B_IN_REG_A 0x10 21 + #define MAX5821_EXTENDED_COMMAND_MODE 0xf0 22 + #define MAX5821_READ_DAC_A_COMMAND 0xf1 23 + #define MAX5821_READ_DAC_B_COMMAND 0xf2 24 + 25 + #define MAX5821_EXTENDED_POWER_UP 0x00 26 + #define MAX5821_EXTENDED_POWER_DOWN_MODE0 0x01 27 + #define MAX5821_EXTENDED_POWER_DOWN_MODE1 0x02 28 + #define MAX5821_EXTENDED_POWER_DOWN_MODE2 0x03 29 + #define MAX5821_EXTENDED_DAC_A 0x04 30 + #define MAX5821_EXTENDED_DAC_B 0x08 31 + 32 + enum max5821_device_ids { 33 + ID_MAX5821, 34 + }; 35 + 36 + struct max5821_data { 37 + struct i2c_client *client; 38 + struct regulator *vref_reg; 39 + unsigned short vref_mv; 40 + bool powerdown[MAX5821_MAX_DAC_CHANNELS]; 41 + u8 powerdown_mode[MAX5821_MAX_DAC_CHANNELS]; 42 + struct mutex lock; 43 + }; 44 + 45 + static const char * const max5821_powerdown_modes[] = { 46 + "three_state", 47 + "1kohm_to_gnd", 48 + "100kohm_to_gnd", 49 + }; 50 + 51 + enum { 52 + MAX5821_THREE_STATE, 53 + MAX5821_1KOHM_TO_GND, 54 + MAX5821_100KOHM_TO_GND 55 + }; 56 + 57 + static int max5821_get_powerdown_mode(struct iio_dev *indio_dev, 58 + const struct iio_chan_spec *chan) 59 + { 60 + struct max5821_data *st = iio_priv(indio_dev); 61 + 62 + return st->powerdown_mode[chan->channel]; 63 + } 64 + 65 + static int max5821_set_powerdown_mode(struct iio_dev *indio_dev, 66 + const struct iio_chan_spec *chan, 67 + unsigned int mode) 68 + { 69 + struct max5821_data *st = iio_priv(indio_dev); 70 + 71 + st->powerdown_mode[chan->channel] = mode; 72 + 73 + return 0; 74 + } 75 + 76 + static const struct iio_enum max5821_powerdown_mode_enum = { 77 + .items = max5821_powerdown_modes, 78 + .num_items = ARRAY_SIZE(max5821_powerdown_modes), 79 + .get = max5821_get_powerdown_mode, 80 + .set = max5821_set_powerdown_mode, 81 + }; 82 + 83 + static ssize_t max5821_read_dac_powerdown(struct iio_dev *indio_dev, 84 + uintptr_t private, 85 + const struct iio_chan_spec *chan, 86 + char *buf) 87 + { 88 + struct max5821_data *st = iio_priv(indio_dev); 89 + 90 + return sprintf(buf, "%d\n", st->powerdown[chan->channel]); 91 + } 92 + 93 + static int max5821_sync_powerdown_mode(struct max5821_data *data, 94 + const struct iio_chan_spec *chan) 95 + { 96 + u8 outbuf[2]; 97 + 98 + outbuf[0] = MAX5821_EXTENDED_COMMAND_MODE; 99 + 100 + if (chan->channel == 0) 101 + outbuf[1] = MAX5821_EXTENDED_DAC_A; 102 + else 103 + outbuf[1] = MAX5821_EXTENDED_DAC_B; 104 + 105 + if (data->powerdown[chan->channel]) 106 + outbuf[1] |= data->powerdown_mode[chan->channel] + 1; 107 + else 108 + outbuf[1] |= MAX5821_EXTENDED_POWER_UP; 109 + 110 + return i2c_master_send(data->client, outbuf, 2); 111 + } 112 + 113 + static ssize_t max5821_write_dac_powerdown(struct iio_dev *indio_dev, 114 + uintptr_t private, 115 + const struct iio_chan_spec *chan, 116 + const char *buf, size_t len) 117 + { 118 + struct max5821_data *data = iio_priv(indio_dev); 119 + bool powerdown; 120 + int ret; 121 + 122 + ret = strtobool(buf, &powerdown); 123 + if (ret) 124 + return ret; 125 + 126 + data->powerdown[chan->channel] = powerdown; 127 + 128 + ret = max5821_sync_powerdown_mode(data, chan); 129 + if (ret < 0) 130 + return ret; 131 + 132 + return len; 133 + } 134 + 135 + static const struct iio_chan_spec_ext_info max5821_ext_info[] = { 136 + { 137 + .name = "powerdown", 138 + .read = max5821_read_dac_powerdown, 139 + .write = max5821_write_dac_powerdown, 140 + .shared = IIO_SEPARATE, 141 + }, 142 + IIO_ENUM("powerdown_mode", IIO_SEPARATE, &max5821_powerdown_mode_enum), 143 + IIO_ENUM_AVAILABLE("powerdown_mode", &max5821_powerdown_mode_enum), 144 + { }, 145 + }; 146 + 147 + #define MAX5821_CHANNEL(chan) { \ 148 + .type = IIO_VOLTAGE, \ 149 + .indexed = 1, \ 150 + .output = 1, \ 151 + .channel = (chan), \ 152 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 153 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE), \ 154 + .ext_info = max5821_ext_info, \ 155 + } 156 + 157 + static const struct iio_chan_spec max5821_channels[] = { 158 + MAX5821_CHANNEL(0), 159 + MAX5821_CHANNEL(1) 160 + }; 161 + 162 + static const u8 max5821_read_dac_command[] = { 163 + MAX5821_READ_DAC_A_COMMAND, 164 + MAX5821_READ_DAC_B_COMMAND 165 + }; 166 + 167 + static const u8 max5821_load_dac_command[] = { 168 + MAX5821_LOAD_DAC_A_IN_REG_B, 169 + MAX5821_LOAD_DAC_B_IN_REG_A 170 + }; 171 + 172 + static int max5821_get_value(struct iio_dev *indio_dev, 173 + int *val, int channel) 174 + { 175 + struct max5821_data *data = iio_priv(indio_dev); 176 + struct i2c_client *client = data->client; 177 + u8 outbuf[1]; 178 + u8 inbuf[2]; 179 + int ret; 180 + 181 + if ((channel != 0) && (channel != 1)) 182 + return -EINVAL; 183 + 184 + outbuf[0] = max5821_read_dac_command[channel]; 185 + 186 + mutex_lock(&data->lock); 187 + 188 + ret = i2c_master_send(client, outbuf, 1); 189 + if (ret < 0) { 190 + mutex_unlock(&data->lock); 191 + return ret; 192 + } else if (ret != 1) { 193 + mutex_unlock(&data->lock); 194 + return -EIO; 195 + } 196 + 197 + ret = i2c_master_recv(client, inbuf, 2); 198 + if (ret < 0) { 199 + mutex_unlock(&data->lock); 200 + return ret; 201 + } else if (ret != 2) { 202 + mutex_unlock(&data->lock); 203 + return -EIO; 204 + } 205 + 206 + mutex_unlock(&data->lock); 207 + 208 + *val = ((inbuf[0] & 0x0f) << 6) | (inbuf[1] >> 2); 209 + 210 + return IIO_VAL_INT; 211 + } 212 + 213 + static int max5821_set_value(struct iio_dev *indio_dev, 214 + int val, int channel) 215 + { 216 + struct max5821_data *data = iio_priv(indio_dev); 217 + struct i2c_client *client = data->client; 218 + u8 outbuf[2]; 219 + int ret; 220 + 221 + if ((val < 0) || (val > 1023)) 222 + return -EINVAL; 223 + 224 + if ((channel != 0) && (channel != 1)) 225 + return -EINVAL; 226 + 227 + outbuf[0] = max5821_load_dac_command[channel]; 228 + outbuf[0] |= val >> 6; 229 + outbuf[1] = (val & 0x3f) << 2; 230 + 231 + ret = i2c_master_send(client, outbuf, 2); 232 + if (ret < 0) 233 + return ret; 234 + else if (ret != 2) 235 + return -EIO; 236 + else 237 + return 0; 238 + } 239 + 240 + static int max5821_read_raw(struct iio_dev *indio_dev, 241 + struct iio_chan_spec const *chan, 242 + int *val, int *val2, long mask) 243 + { 244 + struct max5821_data *data = iio_priv(indio_dev); 245 + 246 + switch (mask) { 247 + case IIO_CHAN_INFO_RAW: 248 + return max5821_get_value(indio_dev, val, chan->channel); 249 + case IIO_CHAN_INFO_SCALE: 250 + *val = data->vref_mv; 251 + *val2 = 10; 252 + return IIO_VAL_FRACTIONAL_LOG2; 253 + default: 254 + return -EINVAL; 255 + } 256 + } 257 + 258 + static int max5821_write_raw(struct iio_dev *indio_dev, 259 + struct iio_chan_spec const *chan, 260 + int val, int val2, long mask) 261 + { 262 + if (val2 != 0) 263 + return -EINVAL; 264 + 265 + switch (mask) { 266 + case IIO_CHAN_INFO_RAW: 267 + return max5821_set_value(indio_dev, val, chan->channel); 268 + default: 269 + return -EINVAL; 270 + } 271 + } 272 + 273 + #ifdef CONFIG_PM_SLEEP 274 + static int max5821_suspend(struct device *dev) 275 + { 276 + u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE, 277 + MAX5821_EXTENDED_DAC_A | 278 + MAX5821_EXTENDED_DAC_B | 279 + MAX5821_EXTENDED_POWER_DOWN_MODE2 }; 280 + 281 + return i2c_master_send(to_i2c_client(dev), outbuf, 2); 282 + } 283 + 284 + static int max5821_resume(struct device *dev) 285 + { 286 + u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE, 287 + MAX5821_EXTENDED_DAC_A | 288 + MAX5821_EXTENDED_DAC_B | 289 + MAX5821_EXTENDED_POWER_UP }; 290 + 291 + return i2c_master_send(to_i2c_client(dev), outbuf, 2); 292 + } 293 + 294 + static SIMPLE_DEV_PM_OPS(max5821_pm_ops, max5821_suspend, max5821_resume); 295 + #define MAX5821_PM_OPS (&max5821_pm_ops) 296 + #else 297 + #define MAX5821_PM_OPS NULL 298 + #endif /* CONFIG_PM_SLEEP */ 299 + 300 + static const struct iio_info max5821_info = { 301 + .read_raw = max5821_read_raw, 302 + .write_raw = max5821_write_raw, 303 + .driver_module = THIS_MODULE, 304 + }; 305 + 306 + static int max5821_probe(struct i2c_client *client, 307 + const struct i2c_device_id *id) 308 + { 309 + struct max5821_data *data; 310 + struct iio_dev *indio_dev; 311 + u32 tmp; 312 + int ret; 313 + 314 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 315 + if (!indio_dev) 316 + return -ENOMEM; 317 + data = iio_priv(indio_dev); 318 + i2c_set_clientdata(client, indio_dev); 319 + data->client = client; 320 + mutex_init(&data->lock); 321 + 322 + /* max5821 start in powerdown mode 100Kohm to ground */ 323 + for (tmp = 0; tmp < MAX5821_MAX_DAC_CHANNELS; tmp++) { 324 + data->powerdown[tmp] = true; 325 + data->powerdown_mode[tmp] = MAX5821_100KOHM_TO_GND; 326 + } 327 + 328 + data->vref_reg = devm_regulator_get(&client->dev, "vref"); 329 + if (IS_ERR(data->vref_reg)) { 330 + ret = PTR_ERR(data->vref_reg); 331 + dev_err(&client->dev, 332 + "Failed to get vref regulator: %d\n", ret); 333 + goto error_free_reg; 334 + } 335 + 336 + ret = regulator_enable(data->vref_reg); 337 + if (ret) { 338 + dev_err(&client->dev, 339 + "Failed to enable vref regulator: %d\n", ret); 340 + goto error_free_reg; 341 + } 342 + 343 + ret = regulator_get_voltage(data->vref_reg); 344 + if (ret < 0) { 345 + dev_err(&client->dev, 346 + "Failed to get voltage on regulator: %d\n", ret); 347 + goto error_disable_reg; 348 + } 349 + 350 + data->vref_mv = ret / 1000; 351 + 352 + indio_dev->name = id->name; 353 + indio_dev->dev.parent = &client->dev; 354 + indio_dev->num_channels = ARRAY_SIZE(max5821_channels); 355 + indio_dev->channels = max5821_channels; 356 + indio_dev->modes = INDIO_DIRECT_MODE; 357 + indio_dev->info = &max5821_info; 358 + 359 + return iio_device_register(indio_dev); 360 + 361 + error_disable_reg: 362 + regulator_disable(data->vref_reg); 363 + 364 + error_free_reg: 365 + 366 + return ret; 367 + } 368 + 369 + static int max5821_remove(struct i2c_client *client) 370 + { 371 + struct iio_dev *indio_dev = i2c_get_clientdata(client); 372 + struct max5821_data *data = iio_priv(indio_dev); 373 + 374 + iio_device_unregister(indio_dev); 375 + regulator_disable(data->vref_reg); 376 + 377 + return 0; 378 + } 379 + 380 + static const struct i2c_device_id max5821_id[] = { 381 + { "max5821", ID_MAX5821 }, 382 + { } 383 + }; 384 + MODULE_DEVICE_TABLE(i2c, max5821_id); 385 + 386 + static const struct of_device_id max5821_of_match[] = { 387 + { .compatible = "maxim,max5821" }, 388 + { } 389 + }; 390 + 391 + static struct i2c_driver max5821_driver = { 392 + .driver = { 393 + .name = "max5821", 394 + .pm = MAX5821_PM_OPS, 395 + .owner = THIS_MODULE, 396 + }, 397 + .probe = max5821_probe, 398 + .remove = max5821_remove, 399 + .id_table = max5821_id, 400 + }; 401 + module_i2c_driver(max5821_driver); 402 + 403 + MODULE_AUTHOR("Philippe Reynes <tremyfr@yahoo.fr>"); 404 + MODULE_DESCRIPTION("MAX5821 DAC"); 405 + MODULE_LICENSE("GPL v2");
+33 -30
drivers/iio/industrialio-buffer.c
··· 942 942 } 943 943 EXPORT_SYMBOL_GPL(iio_push_to_buffers); 944 944 945 + static int iio_buffer_add_demux(struct iio_buffer *buffer, 946 + struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc, 947 + unsigned int length) 948 + { 949 + 950 + if (*p && (*p)->from + (*p)->length == in_loc && 951 + (*p)->to + (*p)->length == out_loc) { 952 + (*p)->length += length; 953 + } else { 954 + *p = kmalloc(sizeof(**p), GFP_KERNEL); 955 + if (*p == NULL) 956 + return -ENOMEM; 957 + (*p)->from = in_loc; 958 + (*p)->to = out_loc; 959 + (*p)->length = length; 960 + list_add_tail(&(*p)->l, &buffer->demux_list); 961 + } 962 + 963 + return 0; 964 + } 965 + 945 966 static int iio_buffer_update_demux(struct iio_dev *indio_dev, 946 967 struct iio_buffer *buffer) 947 968 { 948 969 const struct iio_chan_spec *ch; 949 970 int ret, in_ind = -1, out_ind, length; 950 971 unsigned in_loc = 0, out_loc = 0; 951 - struct iio_demux_table *p; 972 + struct iio_demux_table *p = NULL; 952 973 953 974 /* Clear out any old demux */ 954 975 iio_buffer_demux_free(buffer); ··· 1000 979 else 1001 980 length = ch->scan_type.storagebits / 8; 1002 981 /* Make sure we are aligned */ 1003 - in_loc += length; 1004 - if (in_loc % length) 1005 - in_loc += length - in_loc % length; 1006 - } 1007 - p = kmalloc(sizeof(*p), GFP_KERNEL); 1008 - if (p == NULL) { 1009 - ret = -ENOMEM; 1010 - goto error_clear_mux_table; 982 + in_loc = roundup(in_loc, length) + length; 1011 983 } 1012 984 ch = iio_find_channel_from_si(indio_dev, in_ind); 1013 985 if (ch->scan_type.repeat > 1) ··· 1008 994 ch->scan_type.repeat; 1009 995 else 1010 996 length = ch->scan_type.storagebits / 8; 1011 - if (out_loc % length) 1012 - out_loc += length - out_loc % length; 1013 - if (in_loc % length) 1014 - in_loc += length - in_loc % length; 1015 - p->from = in_loc; 1016 - p->to = out_loc; 1017 - p->length = length; 1018 - list_add_tail(&p->l, &buffer->demux_list); 997 + out_loc = roundup(out_loc, length); 998 + in_loc = roundup(in_loc, length); 999 + ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); 1000 + if (ret) 1001 + goto error_clear_mux_table; 1019 1002 out_loc += length; 1020 1003 in_loc += length; 1021 1004 } 1022 1005 /* Relies on scan_timestamp being last */ 1023 1006 if (buffer->scan_timestamp) { 1024 - p = kmalloc(sizeof(*p), GFP_KERNEL); 1025 - if (p == NULL) { 1026 - ret = -ENOMEM; 1027 - goto error_clear_mux_table; 1028 - } 1029 1007 ch = iio_find_channel_from_si(indio_dev, 1030 1008 indio_dev->scan_index_timestamp); 1031 1009 if (ch->scan_type.repeat > 1) ··· 1025 1019 ch->scan_type.repeat; 1026 1020 else 1027 1021 length = ch->scan_type.storagebits / 8; 1028 - if (out_loc % length) 1029 - out_loc += length - out_loc % length; 1030 - if (in_loc % length) 1031 - in_loc += length - in_loc % length; 1032 - p->from = in_loc; 1033 - p->to = out_loc; 1034 - p->length = length; 1035 - list_add_tail(&p->l, &buffer->demux_list); 1022 + out_loc = roundup(out_loc, length); 1023 + in_loc = roundup(in_loc, length); 1024 + ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); 1025 + if (ret) 1026 + goto error_clear_mux_table; 1036 1027 out_loc += length; 1037 1028 in_loc += length; 1038 1029 }
+2 -5
drivers/iio/magnetometer/hid-sensor-magn-3d.c
··· 246 246 }; 247 247 248 248 /* Function to push data to buffer */ 249 - static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data, 250 - int len) 249 + static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data) 251 250 { 252 251 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n"); 253 252 iio_push_to_buffers(indio_dev, data); ··· 262 263 263 264 dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n"); 264 265 if (atomic_read(&magn_state->common_attributes.data_ready)) 265 - hid_sensor_push_data(indio_dev, 266 - magn_state->iio_vals, 267 - sizeof(magn_state->iio_vals)); 266 + hid_sensor_push_data(indio_dev, magn_state->iio_vals); 268 267 269 268 return 0; 270 269 }
+3 -2
drivers/staging/iio/accel/adis16201_core.c
··· 111 111 int bits; 112 112 s16 val16; 113 113 u8 addr; 114 + 114 115 switch (mask) { 115 116 case IIO_CHAN_INFO_CALIBBIAS: 116 117 switch (chan->type) { ··· 132 131 } 133 132 134 133 static const struct iio_chan_spec adis16201_channels[] = { 135 - ADIS_SUPPLY_CHAN(ADIS16201_SUPPLY_OUT, ADIS16201_SCAN_SUPPLY, 0, 12), 136 - ADIS_TEMP_CHAN(ADIS16201_TEMP_OUT, ADIS16201_SCAN_TEMP, 0, 12), 134 + ADIS_SUPPLY_CHAN(ADIS16201_SUPPLY_OUT, ADIS16201_SCAN_SUPPLY, 0, 12), 135 + ADIS_TEMP_CHAN(ADIS16201_TEMP_OUT, ADIS16201_SCAN_TEMP, 0, 12), 137 136 ADIS_ACCEL_CHAN(X, ADIS16201_XACCL_OUT, ADIS16201_SCAN_ACC_X, 138 137 BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14), 139 138 ADIS_ACCEL_CHAN(Y, ADIS16201_YACCL_OUT, ADIS16201_SCAN_ACC_Y,
+2
drivers/staging/iio/accel/adis16203_core.c
··· 37 37 struct adis *st = iio_priv(indio_dev); 38 38 /* currently only one writable parameter which keeps this simple */ 39 39 u8 addr = adis16203_addresses[chan->scan_index]; 40 + 40 41 return adis_write_reg_16(st, addr, val & 0x3FFF); 41 42 } 42 43 ··· 51 50 int bits; 52 51 u8 addr; 53 52 s16 val16; 53 + 54 54 switch (mask) { 55 55 case IIO_CHAN_INFO_RAW: 56 56 return adis_single_conversion(indio_dev, chan,
+1
drivers/staging/iio/accel/adis16204_core.c
··· 119 119 int bits; 120 120 s16 val16; 121 121 u8 addr; 122 + 122 123 switch (mask) { 123 124 case IIO_CHAN_INFO_CALIBBIAS: 124 125 switch (chan->type) {
+1
drivers/staging/iio/accel/adis16209_core.c
··· 44 44 int bits; 45 45 s16 val16; 46 46 u8 addr; 47 + 47 48 switch (mask) { 48 49 case IIO_CHAN_INFO_CALIBBIAS: 49 50 switch (chan->type) {
+1
drivers/staging/iio/accel/adis16240_core.c
··· 163 163 int bits = 10; 164 164 s16 val16; 165 165 u8 addr; 166 + 166 167 switch (mask) { 167 168 case IIO_CHAN_INFO_CALIBBIAS: 168 169 val16 = val & ((1 << bits) - 1);
+4
drivers/staging/iio/accel/lis3l02dq_core.c
··· 212 212 int val, int val2) 213 213 { 214 214 u16 value = val; 215 + 215 216 return lis3l02dq_spi_write_reg_s16(indio_dev, 216 217 LIS3L02DQ_REG_THS_L_ADDR, 217 218 value); ··· 227 226 int ret = -EINVAL, reg; 228 227 u8 uval; 229 228 s8 sval; 229 + 230 230 switch (mask) { 231 231 case IIO_CHAN_INFO_CALIBBIAS: 232 232 if (val > 255 || val < -256) ··· 304 302 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 305 303 int ret, len = 0; 306 304 s8 t; 305 + 307 306 ret = lis3l02dq_spi_read_reg_8(indio_dev, 308 307 LIS3L02DQ_REG_CTRL_1_ADDR, 309 308 (u8 *)&t); ··· 568 565 u8 val; 569 566 int ret; 570 567 u8 mask = (1 << (chan->channel2*2 + (dir == IIO_EV_DIR_RISING))); 568 + 571 569 ret = lis3l02dq_spi_read_reg_8(indio_dev, 572 570 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR, 573 571 &val);
+1
drivers/staging/iio/accel/lis3l02dq_ring.c
··· 19 19 { 20 20 u16 _lower = lower; 21 21 u16 _upper = upper; 22 + 22 23 return _lower | (_upper << 8); 23 24 } 24 25
+3 -1
drivers/staging/iio/accel/sca3000_core.c
··· 506 506 mutex_unlock(&st->lock); 507 507 return ret; 508 508 } 509 - *val = ((st->rx[0] & 0x3F) << 3) | ((st->rx[1] & 0xE0) >> 5); 509 + *val = ((st->rx[0] & 0x3F) << 3) | 510 + ((st->rx[1] & 0xE0) >> 5); 510 511 } 511 512 mutex_unlock(&st->lock); 512 513 return IIO_VAL_INT; ··· 714 713 int ret, i; 715 714 struct sca3000_state *st = iio_priv(indio_dev); 716 715 int num = chan->channel2; 716 + 717 717 mutex_lock(&st->lock); 718 718 ret = sca3000_read_ctrl_reg(st, sca3000_addresses[num][1]); 719 719 mutex_unlock(&st->lock);
-35
drivers/staging/iio/frequency/Kconfig
··· 3 3 # 4 4 menu "Direct Digital Synthesis" 5 5 6 - config AD5930 7 - tristate "Analog Devices ad5930/5932 driver" 8 - depends on SPI 9 - help 10 - Say yes here to build support for Analog Devices DDS chip 11 - ad5930/ad5932, provides direct access via sysfs. 12 - 13 6 config AD9832 14 7 tristate "Analog Devices ad9832/5 driver" 15 8 depends on SPI ··· 22 29 23 30 To compile this driver as a module, choose M here: the 24 31 module will be called ad9834. 25 - 26 - config AD9850 27 - tristate "Analog Devices ad9850/1 driver" 28 - depends on SPI 29 - help 30 - Say yes here to build support for Analog Devices DDS chip 31 - ad9850/1, provides direct access via sysfs. 32 - 33 - config AD9852 34 - tristate "Analog Devices ad9852/4 driver" 35 - depends on SPI 36 - help 37 - Say yes here to build support for Analog Devices DDS chip 38 - ad9852/4, provides direct access via sysfs. 39 - 40 - config AD9910 41 - tristate "Analog Devices ad9910 driver" 42 - depends on SPI 43 - help 44 - Say yes here to build support for Analog Devices DDS chip 45 - ad9910, provides direct access via sysfs. 46 - 47 - config AD9951 48 - tristate "Analog Devices ad9951 driver" 49 - depends on SPI 50 - help 51 - Say yes here to build support for Analog Devices DDS chip 52 - ad9951, provides direct access via sysfs. 53 32 54 33 endmenu
-5
drivers/staging/iio/frequency/Makefile
··· 2 2 # Makefile for Direct Digital Synthesis drivers 3 3 # 4 4 5 - obj-$(CONFIG_AD5930) += ad5930.o 6 5 obj-$(CONFIG_AD9832) += ad9832.o 7 6 obj-$(CONFIG_AD9834) += ad9834.o 8 - obj-$(CONFIG_AD9850) += ad9850.o 9 - obj-$(CONFIG_AD9852) += ad9852.o 10 - obj-$(CONFIG_AD9910) += ad9910.o 11 - obj-$(CONFIG_AD9951) += ad9951.o
-140
drivers/staging/iio/frequency/ad5930.c
··· 1 - /* 2 - * Driver for ADI Direct Digital Synthesis ad5930 3 - * 4 - * Copyright (c) 2010-2010 Analog Devices Inc. 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 - #include <linux/types.h> 12 - #include <linux/mutex.h> 13 - #include <linux/device.h> 14 - #include <linux/spi/spi.h> 15 - #include <linux/slab.h> 16 - #include <linux/sysfs.h> 17 - #include <linux/module.h> 18 - 19 - #include <linux/iio/iio.h> 20 - #include <linux/iio/sysfs.h> 21 - 22 - #define DRV_NAME "ad5930" 23 - 24 - #define value_mask (u16)0xf000 25 - #define addr_shift 12 26 - 27 - /* Register format: 4 bits addr + 12 bits value */ 28 - struct ad5903_config { 29 - u16 control; 30 - u16 incnum; 31 - u16 frqdelt[2]; 32 - u16 incitvl; 33 - u16 buritvl; 34 - u16 strtfrq[2]; 35 - }; 36 - 37 - struct ad5930_state { 38 - struct mutex lock; 39 - struct spi_device *sdev; 40 - }; 41 - 42 - static ssize_t ad5930_set_parameter(struct device *dev, 43 - struct device_attribute *attr, 44 - const char *buf, 45 - size_t len) 46 - { 47 - struct spi_transfer xfer; 48 - int ret; 49 - struct ad5903_config *config = (struct ad5903_config *)buf; 50 - struct iio_dev *idev = dev_to_iio_dev(dev); 51 - struct ad5930_state *st = iio_priv(idev); 52 - 53 - config->control = (config->control & ~value_mask); 54 - config->incnum = (config->control & ~value_mask) | (1 << addr_shift); 55 - config->frqdelt[0] = (config->control & ~value_mask) | (2 << addr_shift); 56 - config->frqdelt[1] = (config->control & ~value_mask) | 3 << addr_shift; 57 - config->incitvl = (config->control & ~value_mask) | 4 << addr_shift; 58 - config->buritvl = (config->control & ~value_mask) | 8 << addr_shift; 59 - config->strtfrq[0] = (config->control & ~value_mask) | 0xc << addr_shift; 60 - config->strtfrq[1] = (config->control & ~value_mask) | 0xd << addr_shift; 61 - 62 - xfer.len = len; 63 - xfer.tx_buf = config; 64 - mutex_lock(&st->lock); 65 - 66 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 67 - if (ret) 68 - goto error_ret; 69 - error_ret: 70 - mutex_unlock(&st->lock); 71 - 72 - return ret ? ret : len; 73 - } 74 - 75 - static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad5930_set_parameter, 0); 76 - 77 - static struct attribute *ad5930_attributes[] = { 78 - &iio_dev_attr_dds.dev_attr.attr, 79 - NULL, 80 - }; 81 - 82 - static const struct attribute_group ad5930_attribute_group = { 83 - .attrs = ad5930_attributes, 84 - }; 85 - 86 - static const struct iio_info ad5930_info = { 87 - .attrs = &ad5930_attribute_group, 88 - .driver_module = THIS_MODULE, 89 - }; 90 - 91 - static int ad5930_probe(struct spi_device *spi) 92 - { 93 - struct ad5930_state *st; 94 - struct iio_dev *idev; 95 - int ret = 0; 96 - 97 - idev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 98 - if (!idev) 99 - return -ENOMEM; 100 - spi_set_drvdata(spi, idev); 101 - st = iio_priv(idev); 102 - 103 - mutex_init(&st->lock); 104 - st->sdev = spi; 105 - idev->dev.parent = &spi->dev; 106 - idev->info = &ad5930_info; 107 - idev->modes = INDIO_DIRECT_MODE; 108 - 109 - ret = iio_device_register(idev); 110 - if (ret) 111 - return ret; 112 - spi->max_speed_hz = 2000000; 113 - spi->mode = SPI_MODE_3; 114 - spi->bits_per_word = 16; 115 - spi_setup(spi); 116 - 117 - return 0; 118 - } 119 - 120 - static int ad5930_remove(struct spi_device *spi) 121 - { 122 - iio_device_unregister(spi_get_drvdata(spi)); 123 - 124 - return 0; 125 - } 126 - 127 - static struct spi_driver ad5930_driver = { 128 - .driver = { 129 - .name = DRV_NAME, 130 - .owner = THIS_MODULE, 131 - }, 132 - .probe = ad5930_probe, 133 - .remove = ad5930_remove, 134 - }; 135 - module_spi_driver(ad5930_driver); 136 - 137 - MODULE_AUTHOR("Cliff Cai"); 138 - MODULE_DESCRIPTION("Analog Devices ad5930 driver"); 139 - MODULE_LICENSE("GPL v2"); 140 - MODULE_ALIAS("spi:" DRV_NAME);
-120
drivers/staging/iio/frequency/ad9850.c
··· 1 - /* 2 - * Driver for ADI Direct Digital Synthesis ad9850 3 - * 4 - * Copyright (c) 2010-2010 Analog Devices Inc. 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 - #include <linux/types.h> 12 - #include <linux/mutex.h> 13 - #include <linux/device.h> 14 - #include <linux/spi/spi.h> 15 - #include <linux/slab.h> 16 - #include <linux/sysfs.h> 17 - #include <linux/module.h> 18 - 19 - #include <linux/iio/iio.h> 20 - #include <linux/iio/sysfs.h> 21 - 22 - #define DRV_NAME "ad9850" 23 - 24 - /* Register format: 4 bits addr + 12 bits value */ 25 - struct ad9850_config { 26 - u8 control[5]; 27 - }; 28 - 29 - struct ad9850_state { 30 - struct mutex lock; 31 - struct spi_device *sdev; 32 - }; 33 - 34 - static ssize_t ad9850_set_parameter(struct device *dev, 35 - struct device_attribute *attr, 36 - const char *buf, 37 - size_t len) 38 - { 39 - struct spi_transfer xfer; 40 - int ret; 41 - struct ad9850_config *config = (struct ad9850_config *)buf; 42 - struct iio_dev *idev = dev_to_iio_dev(dev); 43 - struct ad9850_state *st = iio_priv(idev); 44 - 45 - xfer.len = len; 46 - xfer.tx_buf = config; 47 - mutex_lock(&st->lock); 48 - 49 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 50 - mutex_unlock(&st->lock); 51 - 52 - return ret ? ret : len; 53 - } 54 - 55 - static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad9850_set_parameter, 0); 56 - 57 - static struct attribute *ad9850_attributes[] = { 58 - &iio_dev_attr_dds.dev_attr.attr, 59 - NULL, 60 - }; 61 - 62 - static const struct attribute_group ad9850_attribute_group = { 63 - .attrs = ad9850_attributes, 64 - }; 65 - 66 - static const struct iio_info ad9850_info = { 67 - .attrs = &ad9850_attribute_group, 68 - .driver_module = THIS_MODULE, 69 - }; 70 - 71 - static int ad9850_probe(struct spi_device *spi) 72 - { 73 - struct ad9850_state *st; 74 - struct iio_dev *idev; 75 - int ret = 0; 76 - 77 - idev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 78 - if (!idev) 79 - return -ENOMEM; 80 - spi_set_drvdata(spi, idev); 81 - st = iio_priv(idev); 82 - mutex_init(&st->lock); 83 - st->sdev = spi; 84 - 85 - idev->dev.parent = &spi->dev; 86 - idev->info = &ad9850_info; 87 - idev->modes = INDIO_DIRECT_MODE; 88 - 89 - ret = iio_device_register(idev); 90 - if (ret) 91 - return ret; 92 - spi->max_speed_hz = 2000000; 93 - spi->mode = SPI_MODE_3; 94 - spi->bits_per_word = 16; 95 - spi_setup(spi); 96 - 97 - return 0; 98 - } 99 - 100 - static int ad9850_remove(struct spi_device *spi) 101 - { 102 - iio_device_unregister(spi_get_drvdata(spi)); 103 - 104 - return 0; 105 - } 106 - 107 - static struct spi_driver ad9850_driver = { 108 - .driver = { 109 - .name = DRV_NAME, 110 - .owner = THIS_MODULE, 111 - }, 112 - .probe = ad9850_probe, 113 - .remove = ad9850_remove, 114 - }; 115 - module_spi_driver(ad9850_driver); 116 - 117 - MODULE_AUTHOR("Cliff Cai"); 118 - MODULE_DESCRIPTION("Analog Devices ad9850 driver"); 119 - MODULE_LICENSE("GPL v2"); 120 - MODULE_ALIAS("spi:" DRV_NAME);
-253
drivers/staging/iio/frequency/ad9852.c
··· 1 - /* 2 - * Driver for ADI Direct Digital Synthesis ad9852 3 - * 4 - * Copyright (c) 2010 Analog Devices Inc. 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 - #include <linux/types.h> 12 - #include <linux/mutex.h> 13 - #include <linux/device.h> 14 - #include <linux/spi/spi.h> 15 - #include <linux/slab.h> 16 - #include <linux/sysfs.h> 17 - #include <linux/module.h> 18 - 19 - #include <linux/iio/iio.h> 20 - #include <linux/iio/sysfs.h> 21 - 22 - #define DRV_NAME "ad9852" 23 - 24 - #define addr_phaad1 0x0 25 - #define addr_phaad2 0x1 26 - #define addr_fretu1 0x2 27 - #define addr_fretu2 0x3 28 - #define addr_delfre 0x4 29 - #define addr_updclk 0x5 30 - #define addr_ramclk 0x6 31 - #define addr_contrl 0x7 32 - #define addr_optskm 0x8 33 - #define addr_optskr 0xa 34 - #define addr_dacctl 0xb 35 - 36 - #define COMPPD (1 << 4) 37 - #define REFMULT2 (1 << 2) 38 - #define BYPPLL (1 << 5) 39 - #define PLLRANG (1 << 6) 40 - #define IEUPCLK (1) 41 - #define OSKEN (1 << 5) 42 - 43 - #define read_bit (1 << 7) 44 - 45 - /* Register format: 1 byte addr + value */ 46 - struct ad9852_config { 47 - u8 phajst0[3]; 48 - u8 phajst1[3]; 49 - u8 fretun1[6]; 50 - u8 fretun2[6]; 51 - u8 dltafre[6]; 52 - u8 updtclk[5]; 53 - u8 ramprat[4]; 54 - u8 control[5]; 55 - u8 outpskm[3]; 56 - u8 outpskr[2]; 57 - u8 daccntl[3]; 58 - }; 59 - 60 - struct ad9852_state { 61 - struct mutex lock; 62 - struct spi_device *sdev; 63 - }; 64 - 65 - static ssize_t ad9852_set_parameter(struct device *dev, 66 - struct device_attribute *attr, 67 - const char *buf, 68 - size_t len) 69 - { 70 - struct spi_transfer xfer; 71 - int ret; 72 - struct ad9852_config *config = (struct ad9852_config *)buf; 73 - struct iio_dev *idev = dev_to_iio_dev(dev); 74 - struct ad9852_state *st = iio_priv(idev); 75 - 76 - xfer.len = 3; 77 - xfer.tx_buf = &config->phajst0[0]; 78 - mutex_lock(&st->lock); 79 - 80 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 81 - if (ret) 82 - goto error_ret; 83 - 84 - xfer.len = 3; 85 - xfer.tx_buf = &config->phajst1[0]; 86 - 87 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 88 - if (ret) 89 - goto error_ret; 90 - 91 - xfer.len = 6; 92 - xfer.tx_buf = &config->fretun1[0]; 93 - 94 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 95 - if (ret) 96 - goto error_ret; 97 - 98 - xfer.len = 6; 99 - xfer.tx_buf = &config->fretun2[0]; 100 - 101 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 102 - if (ret) 103 - goto error_ret; 104 - 105 - xfer.len = 6; 106 - xfer.tx_buf = &config->dltafre[0]; 107 - 108 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 109 - if (ret) 110 - goto error_ret; 111 - 112 - xfer.len = 5; 113 - xfer.tx_buf = &config->updtclk[0]; 114 - 115 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 116 - if (ret) 117 - goto error_ret; 118 - 119 - xfer.len = 4; 120 - xfer.tx_buf = &config->ramprat[0]; 121 - 122 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 123 - if (ret) 124 - goto error_ret; 125 - 126 - xfer.len = 5; 127 - xfer.tx_buf = &config->control[0]; 128 - 129 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 130 - if (ret) 131 - goto error_ret; 132 - 133 - xfer.len = 3; 134 - xfer.tx_buf = &config->outpskm[0]; 135 - 136 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 137 - if (ret) 138 - goto error_ret; 139 - 140 - xfer.len = 2; 141 - xfer.tx_buf = &config->outpskr[0]; 142 - 143 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 144 - if (ret) 145 - goto error_ret; 146 - 147 - xfer.len = 3; 148 - xfer.tx_buf = &config->daccntl[0]; 149 - 150 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 151 - if (ret) 152 - goto error_ret; 153 - error_ret: 154 - mutex_unlock(&st->lock); 155 - 156 - return ret ? ret : len; 157 - } 158 - 159 - static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad9852_set_parameter, 0); 160 - 161 - static void ad9852_init(struct ad9852_state *st) 162 - { 163 - struct spi_transfer xfer; 164 - int ret; 165 - u8 config[5]; 166 - 167 - config[0] = addr_contrl; 168 - config[1] = COMPPD; 169 - config[2] = REFMULT2 | BYPPLL | PLLRANG; 170 - config[3] = IEUPCLK; 171 - config[4] = OSKEN; 172 - 173 - mutex_lock(&st->lock); 174 - 175 - xfer.len = 5; 176 - xfer.tx_buf = &config; 177 - 178 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 179 - if (ret) 180 - goto error_ret; 181 - 182 - error_ret: 183 - mutex_unlock(&st->lock); 184 - 185 - 186 - 187 - } 188 - 189 - static struct attribute *ad9852_attributes[] = { 190 - &iio_dev_attr_dds.dev_attr.attr, 191 - NULL, 192 - }; 193 - 194 - static const struct attribute_group ad9852_attribute_group = { 195 - .attrs = ad9852_attributes, 196 - }; 197 - 198 - static const struct iio_info ad9852_info = { 199 - .attrs = &ad9852_attribute_group, 200 - .driver_module = THIS_MODULE, 201 - }; 202 - 203 - static int ad9852_probe(struct spi_device *spi) 204 - { 205 - struct ad9852_state *st; 206 - struct iio_dev *idev; 207 - int ret = 0; 208 - 209 - idev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 210 - if (!idev) 211 - return -ENOMEM; 212 - st = iio_priv(idev); 213 - spi_set_drvdata(spi, idev); 214 - mutex_init(&st->lock); 215 - st->sdev = spi; 216 - 217 - idev->dev.parent = &spi->dev; 218 - idev->info = &ad9852_info; 219 - idev->modes = INDIO_DIRECT_MODE; 220 - 221 - ret = iio_device_register(idev); 222 - if (ret) 223 - return ret; 224 - spi->max_speed_hz = 2000000; 225 - spi->mode = SPI_MODE_3; 226 - spi->bits_per_word = 8; 227 - spi_setup(spi); 228 - ad9852_init(st); 229 - 230 - return 0; 231 - } 232 - 233 - static int ad9852_remove(struct spi_device *spi) 234 - { 235 - iio_device_unregister(spi_get_drvdata(spi)); 236 - 237 - return 0; 238 - } 239 - 240 - static struct spi_driver ad9852_driver = { 241 - .driver = { 242 - .name = DRV_NAME, 243 - .owner = THIS_MODULE, 244 - }, 245 - .probe = ad9852_probe, 246 - .remove = ad9852_remove, 247 - }; 248 - module_spi_driver(ad9852_driver); 249 - 250 - MODULE_AUTHOR("Cliff Cai"); 251 - MODULE_DESCRIPTION("Analog Devices ad9852 driver"); 252 - MODULE_LICENSE("GPL v2"); 253 - MODULE_ALIAS("spi:" DRV_NAME);
-371
drivers/staging/iio/frequency/ad9910.c
··· 1 - /* 2 - * Driver for ADI Direct Digital Synthesis ad9910 3 - * 4 - * Copyright (c) 2010 Analog Devices Inc. 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 - #include <linux/types.h> 12 - #include <linux/mutex.h> 13 - #include <linux/device.h> 14 - #include <linux/spi/spi.h> 15 - #include <linux/slab.h> 16 - #include <linux/sysfs.h> 17 - #include <linux/module.h> 18 - 19 - #include <linux/iio/iio.h> 20 - #include <linux/iio/sysfs.h> 21 - 22 - #define DRV_NAME "ad9910" 23 - 24 - #define CFR1 0x0 25 - #define CFR2 0x1 26 - #define CFR3 0x2 27 - 28 - #define AUXDAC 0x3 29 - #define IOUPD 0x4 30 - #define FTW 0x7 31 - #define POW 0x8 32 - #define ASF 0x9 33 - #define MULTC 0x0A 34 - #define DIG_RAMPL 0x0B 35 - #define DIG_RAMPS 0x0C 36 - #define DIG_RAMPR 0x0D 37 - #define SIN_TONEP0 0x0E 38 - #define SIN_TONEP1 0x0F 39 - #define SIN_TONEP2 0x10 40 - #define SIN_TONEP3 0x11 41 - #define SIN_TONEP4 0x12 42 - #define SIN_TONEP5 0x13 43 - #define SIN_TONEP6 0x14 44 - #define SIN_TONEP7 0x15 45 - 46 - #define RAM_ENABLE (1 << 7) 47 - 48 - #define MANUAL_OSK (1 << 7) 49 - #define INVSIC (1 << 6) 50 - #define DDS_SINEOP (1) 51 - 52 - #define AUTO_OSK (1) 53 - #define OSKEN (1 << 1) 54 - #define LOAD_ARR (1 << 2) 55 - #define CLR_PHA (1 << 3) 56 - #define CLR_DIG (1 << 4) 57 - #define ACLR_PHA (1 << 5) 58 - #define ACLR_DIG (1 << 6) 59 - #define LOAD_LRR (1 << 7) 60 - 61 - #define LSB_FST (1) 62 - #define SDIO_IPT (1 << 1) 63 - #define EXT_PWD (1 << 3) 64 - #define ADAC_PWD (1 << 4) 65 - #define REFCLK_PWD (1 << 5) 66 - #define DAC_PWD (1 << 6) 67 - #define DIG_PWD (1 << 7) 68 - 69 - #define ENA_AMP (1) 70 - #define READ_FTW (1) 71 - #define DIGR_LOW (1 << 1) 72 - #define DIGR_HIGH (1 << 2) 73 - #define DIGR_ENA (1 << 3) 74 - #define SYNCCLK_ENA (1 << 6) 75 - #define ITER_IOUPD (1 << 7) 76 - 77 - #define TX_ENA (1 << 1) 78 - #define PDCLK_INV (1 << 2) 79 - #define PDCLK_ENB (1 << 3) 80 - 81 - #define PARA_ENA (1 << 4) 82 - #define SYNC_DIS (1 << 5) 83 - #define DATA_ASS (1 << 6) 84 - #define MATCH_ENA (1 << 7) 85 - 86 - #define PLL_ENA (1) 87 - #define PFD_RST (1 << 2) 88 - #define REFCLK_RST (1 << 6) 89 - #define REFCLK_BYP (1 << 7) 90 - 91 - /* Register format: 1 byte addr + value */ 92 - struct ad9910_config { 93 - u8 auxdac[5]; 94 - u8 ioupd[5]; 95 - u8 ftw[5]; 96 - u8 pow[3]; 97 - u8 asf[5]; 98 - u8 multc[5]; 99 - u8 dig_rampl[9]; 100 - u8 dig_ramps[9]; 101 - u8 dig_rampr[5]; 102 - u8 sin_tonep0[9]; 103 - u8 sin_tonep1[9]; 104 - u8 sin_tonep2[9]; 105 - u8 sin_tonep3[9]; 106 - u8 sin_tonep4[9]; 107 - u8 sin_tonep5[9]; 108 - u8 sin_tonep6[9]; 109 - u8 sin_tonep7[9]; 110 - }; 111 - 112 - struct ad9910_state { 113 - struct mutex lock; 114 - struct spi_device *sdev; 115 - }; 116 - 117 - static ssize_t ad9910_set_parameter(struct device *dev, 118 - struct device_attribute *attr, 119 - const char *buf, 120 - size_t len) 121 - { 122 - struct spi_transfer xfer; 123 - int ret; 124 - struct ad9910_config *config = (struct ad9910_config *)buf; 125 - struct iio_dev *idev = dev_to_iio_dev(dev); 126 - struct ad9910_state *st = iio_priv(idev); 127 - 128 - xfer.len = 5; 129 - xfer.tx_buf = &config->auxdac[0]; 130 - mutex_lock(&st->lock); 131 - 132 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 133 - if (ret) 134 - goto error_ret; 135 - 136 - xfer.len = 5; 137 - xfer.tx_buf = &config->ioupd[0]; 138 - 139 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 140 - if (ret) 141 - goto error_ret; 142 - 143 - xfer.len = 5; 144 - xfer.tx_buf = &config->ftw[0]; 145 - 146 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 147 - if (ret) 148 - goto error_ret; 149 - 150 - xfer.len = 3; 151 - xfer.tx_buf = &config->pow[0]; 152 - 153 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 154 - if (ret) 155 - goto error_ret; 156 - 157 - xfer.len = 5; 158 - xfer.tx_buf = &config->asf[0]; 159 - 160 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 161 - if (ret) 162 - goto error_ret; 163 - 164 - xfer.len = 5; 165 - xfer.tx_buf = &config->multc[0]; 166 - 167 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 168 - if (ret) 169 - goto error_ret; 170 - 171 - xfer.len = 9; 172 - xfer.tx_buf = &config->dig_rampl[0]; 173 - 174 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 175 - if (ret) 176 - goto error_ret; 177 - 178 - xfer.len = 9; 179 - xfer.tx_buf = &config->dig_ramps[0]; 180 - 181 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 182 - if (ret) 183 - goto error_ret; 184 - 185 - xfer.len = 5; 186 - xfer.tx_buf = &config->dig_rampr[0]; 187 - 188 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 189 - if (ret) 190 - goto error_ret; 191 - 192 - xfer.len = 9; 193 - xfer.tx_buf = &config->sin_tonep0[0]; 194 - 195 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 196 - if (ret) 197 - goto error_ret; 198 - 199 - xfer.len = 9; 200 - xfer.tx_buf = &config->sin_tonep1[0]; 201 - 202 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 203 - if (ret) 204 - goto error_ret; 205 - 206 - xfer.len = 9; 207 - xfer.tx_buf = &config->sin_tonep2[0]; 208 - 209 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 210 - if (ret) 211 - goto error_ret; 212 - xfer.len = 9; 213 - xfer.tx_buf = &config->sin_tonep3[0]; 214 - 215 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 216 - if (ret) 217 - goto error_ret; 218 - 219 - xfer.len = 9; 220 - xfer.tx_buf = &config->sin_tonep4[0]; 221 - 222 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 223 - if (ret) 224 - goto error_ret; 225 - 226 - xfer.len = 9; 227 - xfer.tx_buf = &config->sin_tonep5[0]; 228 - 229 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 230 - if (ret) 231 - goto error_ret; 232 - 233 - xfer.len = 9; 234 - xfer.tx_buf = &config->sin_tonep6[0]; 235 - 236 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 237 - if (ret) 238 - goto error_ret; 239 - 240 - xfer.len = 9; 241 - xfer.tx_buf = &config->sin_tonep7[0]; 242 - 243 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 244 - if (ret) 245 - goto error_ret; 246 - error_ret: 247 - mutex_unlock(&st->lock); 248 - 249 - return ret ? ret : len; 250 - } 251 - 252 - static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad9910_set_parameter, 0); 253 - 254 - static void ad9910_init(struct ad9910_state *st) 255 - { 256 - struct spi_transfer xfer; 257 - int ret; 258 - u8 cfr[5]; 259 - 260 - cfr[0] = CFR1; 261 - cfr[1] = 0; 262 - cfr[2] = MANUAL_OSK | INVSIC | DDS_SINEOP; 263 - cfr[3] = AUTO_OSK | OSKEN | ACLR_PHA | ACLR_DIG | LOAD_LRR; 264 - cfr[4] = 0; 265 - 266 - mutex_lock(&st->lock); 267 - 268 - xfer.len = 5; 269 - xfer.tx_buf = &cfr; 270 - 271 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 272 - if (ret) 273 - goto error_ret; 274 - 275 - cfr[0] = CFR2; 276 - cfr[1] = ENA_AMP; 277 - cfr[2] = READ_FTW | DIGR_ENA | ITER_IOUPD; 278 - cfr[3] = TX_ENA | PDCLK_INV | PDCLK_ENB; 279 - cfr[4] = PARA_ENA; 280 - 281 - xfer.len = 5; 282 - xfer.tx_buf = &cfr; 283 - 284 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 285 - if (ret) 286 - goto error_ret; 287 - 288 - cfr[0] = CFR3; 289 - cfr[1] = PLL_ENA; 290 - cfr[2] = 0; 291 - cfr[3] = REFCLK_RST | REFCLK_BYP; 292 - cfr[4] = 0; 293 - 294 - xfer.len = 5; 295 - xfer.tx_buf = &cfr; 296 - 297 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 298 - if (ret) 299 - goto error_ret; 300 - 301 - error_ret: 302 - mutex_unlock(&st->lock); 303 - 304 - 305 - 306 - } 307 - 308 - static struct attribute *ad9910_attributes[] = { 309 - &iio_dev_attr_dds.dev_attr.attr, 310 - NULL, 311 - }; 312 - 313 - static const struct attribute_group ad9910_attribute_group = { 314 - .attrs = ad9910_attributes, 315 - }; 316 - 317 - static const struct iio_info ad9910_info = { 318 - .attrs = &ad9910_attribute_group, 319 - .driver_module = THIS_MODULE, 320 - }; 321 - 322 - static int ad9910_probe(struct spi_device *spi) 323 - { 324 - struct ad9910_state *st; 325 - struct iio_dev *idev; 326 - int ret = 0; 327 - 328 - idev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 329 - if (!idev) 330 - return -ENOMEM; 331 - spi_set_drvdata(spi, idev); 332 - st = iio_priv(idev); 333 - mutex_init(&st->lock); 334 - st->sdev = spi; 335 - 336 - idev->dev.parent = &spi->dev; 337 - idev->info = &ad9910_info; 338 - idev->modes = INDIO_DIRECT_MODE; 339 - 340 - ret = iio_device_register(idev); 341 - if (ret) 342 - return ret; 343 - spi->max_speed_hz = 2000000; 344 - spi->mode = SPI_MODE_3; 345 - spi->bits_per_word = 8; 346 - spi_setup(spi); 347 - ad9910_init(st); 348 - return 0; 349 - } 350 - 351 - static int ad9910_remove(struct spi_device *spi) 352 - { 353 - iio_device_unregister(spi_get_drvdata(spi)); 354 - 355 - return 0; 356 - } 357 - 358 - static struct spi_driver ad9910_driver = { 359 - .driver = { 360 - .name = DRV_NAME, 361 - .owner = THIS_MODULE, 362 - }, 363 - .probe = ad9910_probe, 364 - .remove = ad9910_remove, 365 - }; 366 - module_spi_driver(ad9910_driver); 367 - 368 - MODULE_AUTHOR("Cliff Cai"); 369 - MODULE_DESCRIPTION("Analog Devices ad9910 driver"); 370 - MODULE_LICENSE("GPL v2"); 371 - MODULE_ALIAS("spi:" DRV_NAME);
-209
drivers/staging/iio/frequency/ad9951.c
··· 1 - /* 2 - * Driver for ADI Direct Digital Synthesis ad9951 3 - * 4 - * Copyright (c) 2010 Analog Devices Inc. 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 - #include <linux/types.h> 12 - #include <linux/mutex.h> 13 - #include <linux/device.h> 14 - #include <linux/spi/spi.h> 15 - #include <linux/slab.h> 16 - #include <linux/sysfs.h> 17 - #include <linux/module.h> 18 - 19 - #include <linux/iio/iio.h> 20 - #include <linux/iio/sysfs.h> 21 - 22 - #define DRV_NAME "ad9951" 23 - 24 - #define CFR1 0x0 25 - #define CFR2 0x1 26 - 27 - #define AUTO_OSK (1) 28 - #define OSKEN (1 << 1) 29 - #define LOAD_ARR (1 << 2) 30 - 31 - #define AUTO_SYNC (1 << 7) 32 - 33 - #define LSB_FST (1) 34 - #define SDIO_IPT (1 << 1) 35 - #define CLR_PHA (1 << 2) 36 - #define SINE_OPT (1 << 4) 37 - #define ACLR_PHA (1 << 5) 38 - 39 - #define VCO_RANGE (1 << 2) 40 - 41 - #define CRS_OPT (1 << 1) 42 - #define HMANU_SYNC (1 << 2) 43 - #define HSPD_SYNC (1 << 3) 44 - 45 - /* Register format: 1 byte addr + value */ 46 - struct ad9951_config { 47 - u8 asf[3]; 48 - u8 arr[2]; 49 - u8 ftw0[5]; 50 - u8 ftw1[3]; 51 - }; 52 - 53 - struct ad9951_state { 54 - struct mutex lock; 55 - struct spi_device *sdev; 56 - }; 57 - 58 - static ssize_t ad9951_set_parameter(struct device *dev, 59 - struct device_attribute *attr, 60 - const char *buf, 61 - size_t len) 62 - { 63 - struct spi_transfer xfer; 64 - int ret; 65 - struct ad9951_config *config = (struct ad9951_config *)buf; 66 - struct iio_dev *idev = dev_to_iio_dev(dev); 67 - struct ad9951_state *st = iio_priv(idev); 68 - 69 - xfer.len = 3; 70 - xfer.tx_buf = &config->asf[0]; 71 - mutex_lock(&st->lock); 72 - 73 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 74 - if (ret) 75 - goto error_ret; 76 - 77 - xfer.len = 2; 78 - xfer.tx_buf = &config->arr[0]; 79 - 80 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 81 - if (ret) 82 - goto error_ret; 83 - 84 - xfer.len = 5; 85 - xfer.tx_buf = &config->ftw0[0]; 86 - 87 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 88 - if (ret) 89 - goto error_ret; 90 - 91 - xfer.len = 3; 92 - xfer.tx_buf = &config->ftw1[0]; 93 - 94 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 95 - if (ret) 96 - goto error_ret; 97 - error_ret: 98 - mutex_unlock(&st->lock); 99 - 100 - return ret ? ret : len; 101 - } 102 - 103 - static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad9951_set_parameter, 0); 104 - 105 - static void ad9951_init(struct ad9951_state *st) 106 - { 107 - struct spi_transfer xfer; 108 - int ret; 109 - u8 cfr[5]; 110 - 111 - cfr[0] = CFR1; 112 - cfr[1] = 0; 113 - cfr[2] = LSB_FST | CLR_PHA | SINE_OPT | ACLR_PHA; 114 - cfr[3] = AUTO_OSK | OSKEN | LOAD_ARR; 115 - cfr[4] = 0; 116 - 117 - mutex_lock(&st->lock); 118 - 119 - xfer.len = 5; 120 - xfer.tx_buf = &cfr; 121 - 122 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 123 - if (ret) 124 - goto error_ret; 125 - 126 - cfr[0] = CFR2; 127 - cfr[1] = VCO_RANGE; 128 - cfr[2] = HSPD_SYNC; 129 - cfr[3] = 0; 130 - 131 - xfer.len = 4; 132 - xfer.tx_buf = &cfr; 133 - 134 - ret = spi_sync_transfer(st->sdev, &xfer, 1); 135 - if (ret) 136 - goto error_ret; 137 - 138 - error_ret: 139 - mutex_unlock(&st->lock); 140 - 141 - 142 - 143 - } 144 - 145 - static struct attribute *ad9951_attributes[] = { 146 - &iio_dev_attr_dds.dev_attr.attr, 147 - NULL, 148 - }; 149 - 150 - static const struct attribute_group ad9951_attribute_group = { 151 - .attrs = ad9951_attributes, 152 - }; 153 - 154 - static const struct iio_info ad9951_info = { 155 - .attrs = &ad9951_attribute_group, 156 - .driver_module = THIS_MODULE, 157 - }; 158 - 159 - static int ad9951_probe(struct spi_device *spi) 160 - { 161 - struct ad9951_state *st; 162 - struct iio_dev *idev; 163 - int ret = 0; 164 - 165 - idev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 166 - if (!idev) 167 - return -ENOMEM; 168 - spi_set_drvdata(spi, idev); 169 - st = iio_priv(idev); 170 - mutex_init(&st->lock); 171 - st->sdev = spi; 172 - 173 - idev->dev.parent = &spi->dev; 174 - 175 - idev->info = &ad9951_info; 176 - idev->modes = INDIO_DIRECT_MODE; 177 - 178 - ret = iio_device_register(idev); 179 - if (ret) 180 - return ret; 181 - spi->max_speed_hz = 2000000; 182 - spi->mode = SPI_MODE_3; 183 - spi->bits_per_word = 8; 184 - spi_setup(spi); 185 - ad9951_init(st); 186 - return 0; 187 - } 188 - 189 - static int ad9951_remove(struct spi_device *spi) 190 - { 191 - iio_device_unregister(spi_get_drvdata(spi)); 192 - 193 - return 0; 194 - } 195 - 196 - static struct spi_driver ad9951_driver = { 197 - .driver = { 198 - .name = DRV_NAME, 199 - .owner = THIS_MODULE, 200 - }, 201 - .probe = ad9951_probe, 202 - .remove = ad9951_remove, 203 - }; 204 - module_spi_driver(ad9951_driver); 205 - 206 - MODULE_AUTHOR("Cliff Cai"); 207 - MODULE_DESCRIPTION("Analog Devices ad9951 driver"); 208 - MODULE_LICENSE("GPL v2"); 209 - MODULE_ALIAS("spi:" DRV_NAME);
+1
drivers/staging/iio/gyro/adis16060_core.c
··· 180 180 int ret; 181 181 struct iio_dev *indio_dev = adis16060_iio_dev; 182 182 struct adis16060_state *st; 183 + 183 184 if (!indio_dev) { 184 185 ret = -ENODEV; 185 186 goto error_ret;
+5 -5
drivers/staging/iio/impedance-analyzer/ad5933.c
··· 220 220 { 221 221 unsigned long long freqreg; 222 222 union { 223 - u32 d32; 223 + __be32 d32; 224 224 u8 d8[4]; 225 225 } dat; 226 226 ··· 244 244 245 245 static int ad5933_setup(struct ad5933_state *st) 246 246 { 247 - unsigned short dat; 247 + __be16 dat; 248 248 int ret; 249 249 250 250 ret = ad5933_reset(st); ··· 297 297 int ret; 298 298 unsigned long long freqreg; 299 299 union { 300 - u32 d32; 300 + __be32 d32; 301 301 u8 d8[4]; 302 302 } dat; 303 303 ··· 402 402 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 403 403 u16 val; 404 404 int i, ret = 0; 405 - unsigned short dat; 405 + __be16 dat; 406 406 407 407 if (this_attr->address != AD5933_IN_PGA_GAIN) { 408 408 ret = kstrtou16(buf, 10, &val); ··· 521 521 long m) 522 522 { 523 523 struct ad5933_state *st = iio_priv(indio_dev); 524 - unsigned short dat; 524 + __be16 dat; 525 525 int ret = -EINVAL; 526 526 527 527 mutex_lock(&indio_dev->mlock);
+1 -1
drivers/staging/iio/light/isl29018.c
··· 454 454 static int isl29018_chip_init(struct isl29018_chip *chip) 455 455 { 456 456 int status; 457 - int new_adc_bit; 457 + unsigned int new_adc_bit; 458 458 unsigned int new_range; 459 459 460 460 /* Code added per Intersil Application Note 1534:
+3 -3
drivers/staging/iio/magnetometer/hmc5843_core.c
··· 131 131 static int hmc5843_wait_measurement(struct hmc5843_data *data) 132 132 { 133 133 int tries = 150; 134 - int val; 134 + unsigned int val; 135 135 int ret; 136 136 137 137 while (tries-- > 0) { ··· 209 209 char *buf) 210 210 { 211 211 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev)); 212 - int val; 212 + unsigned int val; 213 213 int ret; 214 214 215 215 ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_A, &val); ··· 344 344 int *val, int *val2, long mask) 345 345 { 346 346 struct hmc5843_data *data = iio_priv(indio_dev); 347 - int rval; 347 + unsigned int rval; 348 348 int ret; 349 349 350 350 switch (mask) {
+2 -2
drivers/staging/iio/resolver/ad2s1210.c
··· 491 491 492 492 switch (chan->type) { 493 493 case IIO_ANGL: 494 - pos = be16_to_cpup((u16 *)st->rx); 494 + pos = be16_to_cpup((__be16 *) st->rx); 495 495 if (st->hysteresis) 496 496 pos >>= 16 - st->resolution; 497 497 *val = pos; ··· 499 499 break; 500 500 case IIO_ANGL_VEL: 501 501 negative = st->rx[0] & 0x80; 502 - vel = be16_to_cpup((s16 *)st->rx); 502 + vel = be16_to_cpup((__be16 *) st->rx); 503 503 vel >>= 16 - st->resolution; 504 504 if (vel & 0x8000) { 505 505 negative = (0xffff >> st->resolution) << st->resolution;