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

Merge tag 'iio-for-v4.2b' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into staging-next

Jonathan writes:

Second set of new driver, functionality and cleanups for IIO in the 4.2 cycle.

Core functionality
* i and q modifiers from quadrature channels.
* IIO_CHAN_INFO_OVERSAMPLING_RATIO added.
* High pass filter attributes added to mirror the existing low pass filter
ones.

Core cleanups
* Make IIO tools building more cross compiler friendly.
* Substantial rework of the function __iio_update_buffers to greatly simplify
a hideously evolved function.

New drivers and support
* ACPI0008 ambient light sensor driver. This one has been around a long time to
will be good to finally get it into mainline.
* Berlin SOC ADC support.
* BMC150 magnetometer. The accelerometer in the same package has been supported
for quite some time, so good to have this half as well.
* m62332 DAC driver
* MEMSIC MMC35420 magnetometer.
* ROHM BH1710 and similar ambient light sensors.
* Sensortek STK3310 light sensor.
* Sensortek STK8312 accelerometer.
* Sensortek STK8BA50 accelerometer.
* ti-adc128s052 gains support form the adc122s021 2 channel ADC.

Driver cleanups and functionality.
* Allow various drivers to compile with !GPIOLIB if COMPILE_TEST enabled.
* bmc150 - decouple trigger from buffer to allow other triggers to be used.
* bmg160 - decouple trigger from buffer to allow other triggers to be used.
Fix a trivial unused field.
* Constify a load of platform_device_id structures.
* inv_mpu6050 - device tree bindings.
* hid-sensors - fix a memory leak during probe if certain errors occur.
* ltr501 - illuminance channel derived (in an non obvious fashion) from the
intensity channels.
* ltr501 - fix a boundary check on the proximity threshold.
* mlx90614 - drop a pointless return.
* mma8452 - Debugfs register access and fix a bug that had no effect (by
coincidence)
* ti_am335x_adc - add device tree bindings for sample-delay, open-delay and
averaging. The ideal settings for these tend to be board design specific.

+5061 -259
+88
Documentation/ABI/testing/sysfs-bus-iio
··· 71 71 72 72 What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_raw 73 73 What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_supply_raw 74 + What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_i_raw 75 + What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_q_raw 74 76 KernelVersion: 2.6.35 75 77 Contact: linux-iio@vger.kernel.org 76 78 Description: ··· 82 80 versions may be used. The number must always be specified and 83 81 unique to allow association with event codes. Units after 84 82 application of scale and offset are millivolts. 83 + 84 + Channels with 'i' and 'q' modifiers always exist in pairs and both 85 + channels refer to the same signal. The 'i' channel contains the in-phase 86 + component of the signal while the 'q' channel contains the quadrature 87 + component. 85 88 86 89 What: /sys/bus/iio/devices/iio:deviceX/in_voltageY-voltageZ_raw 87 90 KernelVersion: 2.6.35 ··· 253 246 What: /sys/bus/iio/devices/iio:deviceX/in_accel_z_offset 254 247 What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_offset 255 248 What: /sys/bus/iio/devices/iio:deviceX/in_voltage_offset 249 + What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_i_offset 250 + What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_q_offset 251 + What: /sys/bus/iio/devices/iio:deviceX/in_voltage_q_offset 252 + What: /sys/bus/iio/devices/iio:deviceX/in_voltage_i_offset 256 253 What: /sys/bus/iio/devices/iio:deviceX/in_currentY_offset 257 254 What: /sys/bus/iio/devices/iio:deviceX/in_current_offset 255 + What: /sys/bus/iio/devices/iio:deviceX/in_currentY_i_offset 256 + What: /sys/bus/iio/devices/iio:deviceX/in_currentY_q_offset 257 + What: /sys/bus/iio/devices/iio:deviceX/in_current_q_offset 258 + What: /sys/bus/iio/devices/iio:deviceX/in_current_i_offset 258 259 What: /sys/bus/iio/devices/iio:deviceX/in_tempY_offset 259 260 What: /sys/bus/iio/devices/iio:deviceX/in_temp_offset 260 261 What: /sys/bus/iio/devices/iio:deviceX/in_pressureY_offset ··· 288 273 to the _raw output. 289 274 290 275 What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_scale 276 + What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_i_scale 277 + What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_q_scale 291 278 What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_supply_scale 292 279 What: /sys/bus/iio/devices/iio:deviceX/in_voltage_scale 280 + What: /sys/bus/iio/devices/iio:deviceX/in_voltage_i_scale 281 + What: /sys/bus/iio/devices/iio:deviceX/in_voltage_q_scale 293 282 What: /sys/bus/iio/devices/iio:deviceX/in_voltage-voltage_scale 294 283 What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_scale 295 284 What: /sys/bus/iio/devices/iio:deviceX/out_altvoltageY_scale 296 285 What: /sys/bus/iio/devices/iio:deviceX/in_currentY_scale 297 286 What: /sys/bus/iio/devices/iio:deviceX/in_currentY_supply_scale 298 287 What: /sys/bus/iio/devices/iio:deviceX/in_current_scale 288 + What: /sys/bus/iio/devices/iio:deviceX/in_currentY_i_scale 289 + What: /sys/bus/iio/devices/iio:deviceX/in_currentY_q_scale 290 + What: /sys/bus/iio/devices/iio:deviceX/in_current_i_scale 291 + What: /sys/bus/iio/devices/iio:deviceX/in_current_q_scale 299 292 What: /sys/bus/iio/devices/iio:deviceX/in_accel_scale 300 293 What: /sys/bus/iio/devices/iio:deviceX/in_accel_peak_scale 301 294 What: /sys/bus/iio/devices/iio:deviceX/in_anglvel_scale ··· 351 328 352 329 What /sys/bus/iio/devices/iio:deviceX/in_voltageY_calibscale 353 330 What /sys/bus/iio/devices/iio:deviceX/in_voltageY_supply_calibscale 331 + What /sys/bus/iio/devices/iio:deviceX/in_voltageY_i_calibscale 332 + What /sys/bus/iio/devices/iio:deviceX/in_voltageY_q_calibscale 333 + What /sys/bus/iio/devices/iio:deviceX/in_voltage_i_calibscale 334 + What /sys/bus/iio/devices/iio:deviceX/in_voltage_q_calibscale 354 335 What /sys/bus/iio/devices/iio:deviceX/in_voltage_calibscale 355 336 What /sys/bus/iio/devices/iio:deviceX/in_accel_x_calibscale 356 337 What /sys/bus/iio/devices/iio:deviceX/in_accel_y_calibscale ··· 444 417 Contact: linux-iio@vger.kernel.org 445 418 Description: 446 419 If a known or controllable low pass filter is applied 420 + to the underlying data channel, then this parameter 421 + gives the 3dB frequency of the filter in Hz. 422 + 423 + What: /sys/.../in_accel_filter_high_pass_3db_frequency 424 + What: /sys/.../in_anglvel_filter_high_pass_3db_frequency 425 + What: /sys/.../in_magn_filter_high_pass_3db_frequency 426 + KernelVersion: 4.2 427 + Contact: linux-iio@vger.kernel.org 428 + Description: 429 + If a known or controllable high pass filter is applied 447 430 to the underlying data channel, then this parameter 448 431 gives the 3dB frequency of the filter in Hz. 449 432 ··· 917 880 met before an event is generated. If direction is not 918 881 specified then this period applies to both directions. 919 882 883 + What: /sys/.../events/in_accel_thresh_rising_low_pass_filter_3db 884 + What: /sys/.../events/in_anglvel_thresh_rising_low_pass_filter_3db 885 + What: /sys/.../events/in_magn_thresh_rising_low_pass_filter_3db 886 + KernelVersion: 4.2 887 + Contact: linux-iio@vger.kernel.org 888 + Description: 889 + If a low pass filter can be applied to the event generation 890 + this property gives its 3db frequency in Hz. 891 + A value of zero disables the filter. 892 + 893 + What: /sys/.../events/in_accel_thresh_rising_high_pass_filter_3db 894 + What: /sys/.../events/in_anglvel_thresh_rising_high_pass_filter_3db 895 + What: /sys/.../events/in_magn_thresh_rising_high_pass_filter_3db 896 + KernelVersion: 4.2 897 + Contact: linux-iio@vger.kernel.org 898 + Description: 899 + If a high pass filter can be applied to the event generation 900 + this property gives its 3db frequency in Hz. 901 + A value of zero disables the filter. 902 + 920 903 What: /sys/.../events/in_activity_still_thresh_rising_en 921 904 What: /sys/.../events/in_activity_still_thresh_falling_en 922 905 What: /sys/.../events/in_activity_walking_thresh_rising_en ··· 1073 1016 What: /sys/.../iio:deviceX/scan_elements/in_voltageY_supply_en 1074 1017 What: /sys/.../iio:deviceX/scan_elements/in_voltageY_en 1075 1018 What: /sys/.../iio:deviceX/scan_elements/in_voltageY-voltageZ_en 1019 + What: /sys/.../iio:deviceX/scan_elements/in_voltageY_i_en 1020 + What: /sys/.../iio:deviceX/scan_elements/in_voltageY_q_en 1021 + What: /sys/.../iio:deviceX/scan_elements/in_voltage_i_en 1022 + What: /sys/.../iio:deviceX/scan_elements/in_voltage_q_en 1076 1023 What: /sys/.../iio:deviceX/scan_elements/in_incli_x_en 1077 1024 What: /sys/.../iio:deviceX/scan_elements/in_incli_y_en 1078 1025 What: /sys/.../iio:deviceX/scan_elements/in_pressureY_en ··· 1095 1034 What: /sys/.../iio:deviceX/scan_elements/in_voltageY_type 1096 1035 What: /sys/.../iio:deviceX/scan_elements/in_voltage_type 1097 1036 What: /sys/.../iio:deviceX/scan_elements/in_voltageY_supply_type 1037 + What: /sys/.../iio:deviceX/scan_elements/in_voltageY_i_type 1038 + What: /sys/.../iio:deviceX/scan_elements/in_voltageY_q_type 1039 + What: /sys/.../iio:deviceX/scan_elements/in_voltage_i_type 1040 + What: /sys/.../iio:deviceX/scan_elements/in_voltage_q_type 1098 1041 What: /sys/.../iio:deviceX/scan_elements/in_timestamp_type 1099 1042 What: /sys/.../iio:deviceX/scan_elements/in_pressureY_type 1100 1043 What: /sys/.../iio:deviceX/scan_elements/in_pressure_type ··· 1136 1071 1137 1072 What: /sys/.../iio:deviceX/scan_elements/in_voltageY_index 1138 1073 What: /sys/.../iio:deviceX/scan_elements/in_voltageY_supply_index 1074 + What: /sys/.../iio:deviceX/scan_elements/in_voltageY_i_index 1075 + What: /sys/.../iio:deviceX/scan_elements/in_voltageY_q_index 1076 + What: /sys/.../iio:deviceX/scan_elements/in_voltage_i_index 1077 + What: /sys/.../iio:deviceX/scan_elements/in_voltage_q_index 1139 1078 What: /sys/.../iio:deviceX/scan_elements/in_accel_x_index 1140 1079 What: /sys/.../iio:deviceX/scan_elements/in_accel_y_index 1141 1080 What: /sys/.../iio:deviceX/scan_elements/in_accel_z_index ··· 1299 1230 or without compensation from tilt sensors. 1300 1231 1301 1232 What: /sys/bus/iio/devices/iio:deviceX/in_currentX_raw 1233 + What: /sys/bus/iio/devices/iio:deviceX/in_currentX_i_raw 1234 + What: /sys/bus/iio/devices/iio:deviceX/in_currentX_q_raw 1302 1235 KernelVersion: 3.18 1303 1236 Contact: linux-iio@vger.kernel.org 1304 1237 Description: ··· 1308 1237 after application of scale and offset. If no offset or scale is 1309 1238 present, output should be considered as processed with the 1310 1239 unit in milliamps. 1240 + 1241 + Channels with 'i' and 'q' modifiers always exist in pairs and both 1242 + channels refer to the same signal. The 'i' channel contains the in-phase 1243 + component of the signal while the 'q' channel contains the quadrature 1244 + component. 1311 1245 1312 1246 What: /sys/.../iio:deviceX/in_energy_en 1313 1247 What: /sys/.../iio:deviceX/in_distance_en ··· 1451 1375 The emissivity ratio of the surface in the field of view of the 1452 1376 contactless temperature sensor. Emissivity varies from 0 to 1, 1453 1377 with 1 being the emissivity of a black body. 1378 + 1379 + What: /sys/bus/iio/devices/iio:deviceX/in_magn_x_oversampling_ratio 1380 + What: /sys/bus/iio/devices/iio:deviceX/in_magn_y_oversampling_ratio 1381 + What: /sys/bus/iio/devices/iio:deviceX/in_magn_z_oversampling_ratio 1382 + KernelVersion: 4.2 1383 + Contact: linux-iio@vger.kernel.org 1384 + Description: 1385 + Hardware applied number of measurements for acquiring one 1386 + data point. The HW will do <type>[_name]_oversampling_ratio 1387 + measurements and return the average value as output data. Each 1388 + value resulted from <type>[_name]_oversampling_ratio measurements 1389 + is considered as one sample for <type>[_name]_sampling_frequency.
+19
Documentation/devicetree/bindings/iio/adc/berlin2_adc.txt
··· 1 + * Berlin Analog to Digital Converter (ADC) 2 + 3 + The Berlin ADC has 8 channels, with one connected to a temperature sensor. 4 + It is part of the system controller register set. The ADC node should be a 5 + sub-node of the system controller node. 6 + 7 + Required properties: 8 + - compatible: must be "marvell,berlin2-adc" 9 + - interrupts: the interrupts for the ADC and the temperature sensor 10 + - interrupt-names: should be "adc" and "tsen" 11 + 12 + Example: 13 + 14 + adc: adc { 15 + compatible = "marvell,berlin2-adc"; 16 + interrupt-parent = <&sic>; 17 + interrupts = <12>, <14>; 18 + interrupt-names = "adc", "tsen"; 19 + };
+2 -2
Documentation/devicetree/bindings/iio/adc/ti-adc128s052.txt
··· 1 - * Texas Instruments' ADC128S052 ADC chip 1 + * Texas Instruments' ADC128S052 and ADC122S021 ADC chip 2 2 3 3 Required properties: 4 - - compatible: Should be "ti,adc128s052" 4 + - compatible: Should be "ti,adc128s052" or "ti,adc122s021" 5 5 - reg: spi chip select number for the device 6 6 - vref-supply: The regulator supply for ADC reference voltage 7 7
+17
Documentation/devicetree/bindings/iio/imu/inv_mpu6050.txt
··· 1 + InvenSense MPU-6050 Six-Axis (Gyro + Accelerometer) MEMS MotionTracking Device 2 + 3 + http://www.invensense.com/mems/gyro/mpu6050.html 4 + 5 + Required properties: 6 + - compatible : should be "invensense,mpu6050" 7 + - reg : the I2C address of the sensor 8 + - interrupt-parent : should be the phandle for the interrupt controller 9 + - interrupts : interrupt mapping for GPIO IRQ 10 + 11 + Example: 12 + mpu6050@68 { 13 + compatible = "invensense,mpu6050"; 14 + reg = <0x68>; 15 + interrupt-parent = <&gpio1>; 16 + interrupts = <18 1>; 17 + };
+22
Documentation/devicetree/bindings/iio/magnetometer/bmc150_magn.txt
··· 1 + * Bosch BMC150 magnetometer sensor 2 + 3 + http://ae-bst.resource.bosch.com/media/products/dokumente/bmc150/BST-BMC150-DS000-04.pdf 4 + 5 + Required properties: 6 + 7 + - compatible : should be "bosch,bmc150_magn" 8 + - reg : the I2C address of the magnetometer 9 + 10 + Optional properties: 11 + 12 + - interrupt-parent : phandle to the parent interrupt controller 13 + - interrupts : interrupt mapping for GPIO IRQ 14 + 15 + Example: 16 + 17 + bmc150_magn@12 { 18 + compatible = "bosch,bmc150_magn"; 19 + reg = <0x12>; 20 + interrupt-parent = <&gpio1>; 21 + interrupts = <0 1>; 22 + };
+24
Documentation/devicetree/bindings/input/touchscreen/ti-tsc-adc.txt
··· 42 42 hardware knob for adjusting the amount of "settling 43 43 time". 44 44 45 + - child "adc" 46 + ti,chan-step-opendelay: List of open delays for each channel of 47 + ADC in the order of ti,adc-channels. The 48 + value corresponds to the number of ADC 49 + clock cycles to wait after applying the 50 + step configuration registers and before 51 + sending the start of ADC conversion. 52 + Maximum value is 0x3FFFF. 53 + ti,chan-step-sampledelay: List of sample delays for each channel 54 + of ADC in the order of ti,adc-channels. 55 + The value corresponds to the number of 56 + ADC clock cycles to sample (to hold 57 + start of conversion high). 58 + Maximum value is 0xFF. 59 + ti,chan-step-avg: Number of averages to be performed for each 60 + channel of ADC. If average is 16 then input 61 + is sampled 16 times and averaged to get more 62 + accurate value. This increases the time taken 63 + by ADC to generate a sample. Valid range is 0 64 + average to 16 averages. Maximum value is 16. 65 + 45 66 Example: 46 67 tscadc: tscadc@44e0d000 { 47 68 compatible = "ti,am3359-tscadc"; ··· 76 55 77 56 adc { 78 57 ti,adc-channels = <4 5 6 7>; 58 + ti,chan-step-opendelay = <0x098 0x3ffff 0x098 0x0>; 59 + ti,chan-step-sampledelay = <0xff 0x0 0xf 0x0>; 60 + ti,chan-step-avg = <16 2 4 8>; 79 61 }; 80 62 }
+21
drivers/iio/accel/Kconfig
··· 136 136 137 137 To compile this driver as a module, choose M here: the module 138 138 will be called mma9553. 139 + 140 + config STK8312 141 + tristate "Sensortek STK8312 3-Axis Accelerometer Driver" 142 + depends on I2C 143 + help 144 + Say yes here to get support for the Sensortek STK8312 3-axis 145 + accelerometer. 146 + 147 + Choosing M will build the driver as a module. If so, the module 148 + will be called stk8312. 149 + 150 + config STK8BA50 151 + tristate "Sensortek STK8BA50 3-Axis Accelerometer Driver" 152 + depends on I2C 153 + help 154 + Say yes here to get support for the Sensortek STK8BA50 3-axis 155 + accelerometer. 156 + 157 + Choosing M will build the driver as a module. If so, the module 158 + will be called stk8ba50. 159 + 139 160 endmenu
+3
drivers/iio/accel/Makefile
··· 14 14 obj-$(CONFIG_MMA9551) += mma9551.o 15 15 obj-$(CONFIG_MMA9553) += mma9553.o 16 16 17 + obj-$(CONFIG_STK8312) += stk8312.o 18 + obj-$(CONFIG_STK8BA50) += stk8ba50.o 19 + 17 20 obj-$(CONFIG_IIO_SSP_SENSORS_COMMONS) += ssp_accel_sensor.o 18 21 19 22 obj-$(CONFIG_IIO_ST_ACCEL_3AXIS) += st_accel.o
+35 -20
drivers/iio/accel/bmc150-accel.c
··· 196 196 u32 slope_thres; 197 197 u32 range; 198 198 int ev_enable_state; 199 - int64_t timestamp, old_timestamp; 199 + int64_t timestamp, old_timestamp; /* Only used in hw fifo mode. */ 200 200 const struct bmc150_accel_chip_info *chip_info; 201 201 }; 202 202 ··· 1183 1183 .write_event_value = bmc150_accel_write_event, 1184 1184 .write_event_config = bmc150_accel_write_event_config, 1185 1185 .read_event_config = bmc150_accel_read_event_config, 1186 - .validate_trigger = bmc150_accel_validate_trigger, 1187 1186 .driver_module = THIS_MODULE, 1188 1187 }; 1189 1188 ··· 1221 1222 mutex_unlock(&data->mutex); 1222 1223 1223 1224 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 1224 - data->timestamp); 1225 + pf->timestamp); 1225 1226 err_read: 1226 1227 iio_trigger_notify_done(indio_dev->trig); 1227 1228 ··· 1534 1535 return ret; 1535 1536 } 1536 1537 1538 + static int bmc150_accel_buffer_preenable(struct iio_dev *indio_dev) 1539 + { 1540 + struct bmc150_accel_data *data = iio_priv(indio_dev); 1541 + 1542 + return bmc150_accel_set_power_state(data, true); 1543 + } 1544 + 1537 1545 static int bmc150_accel_buffer_postenable(struct iio_dev *indio_dev) 1538 1546 { 1539 1547 struct bmc150_accel_data *data = iio_priv(indio_dev); ··· 1597 1591 return 0; 1598 1592 } 1599 1593 1594 + static int bmc150_accel_buffer_postdisable(struct iio_dev *indio_dev) 1595 + { 1596 + struct bmc150_accel_data *data = iio_priv(indio_dev); 1597 + 1598 + return bmc150_accel_set_power_state(data, false); 1599 + } 1600 + 1600 1601 static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops = { 1602 + .preenable = bmc150_accel_buffer_preenable, 1601 1603 .postenable = bmc150_accel_buffer_postenable, 1602 1604 .predisable = bmc150_accel_buffer_predisable, 1605 + .postdisable = bmc150_accel_buffer_postdisable, 1603 1606 }; 1604 1607 1605 1608 static int bmc150_accel_probe(struct i2c_client *client, ··· 1651 1636 indio_dev->modes = INDIO_DIRECT_MODE; 1652 1637 indio_dev->info = &bmc150_accel_info; 1653 1638 1639 + ret = iio_triggered_buffer_setup(indio_dev, 1640 + &iio_pollfunc_store_time, 1641 + bmc150_accel_trigger_handler, 1642 + &bmc150_accel_buffer_ops); 1643 + if (ret < 0) { 1644 + dev_err(&client->dev, "Failed: iio triggered buffer setup\n"); 1645 + return ret; 1646 + } 1647 + 1654 1648 if (client->irq < 0) 1655 1649 client->irq = bmc150_accel_gpio_probe(client, data); 1656 1650 ··· 1672 1648 BMC150_ACCEL_IRQ_NAME, 1673 1649 indio_dev); 1674 1650 if (ret) 1675 - return ret; 1651 + goto err_buffer_cleanup; 1676 1652 1677 1653 /* 1678 1654 * Set latched mode interrupt. While certain interrupts are ··· 1685 1661 BMC150_ACCEL_INT_MODE_LATCH_RESET); 1686 1662 if (ret < 0) { 1687 1663 dev_err(&data->client->dev, "Error writing reg_int_rst_latch\n"); 1688 - return ret; 1664 + goto err_buffer_cleanup; 1689 1665 } 1690 1666 1691 1667 bmc150_accel_interrupts_setup(indio_dev, data); 1692 1668 1693 1669 ret = bmc150_accel_triggers_setup(indio_dev, data); 1694 1670 if (ret) 1695 - return ret; 1696 - 1697 - ret = iio_triggered_buffer_setup(indio_dev, 1698 - &iio_pollfunc_store_time, 1699 - bmc150_accel_trigger_handler, 1700 - &bmc150_accel_buffer_ops); 1701 - if (ret < 0) { 1702 - dev_err(&client->dev, 1703 - "Failed: iio triggered buffer setup\n"); 1704 - goto err_trigger_unregister; 1705 - } 1671 + goto err_buffer_cleanup; 1706 1672 1707 1673 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) || 1708 1674 i2c_check_functionality(client->adapter, ··· 1706 1692 ret = iio_device_register(indio_dev); 1707 1693 if (ret < 0) { 1708 1694 dev_err(&client->dev, "Unable to register iio device\n"); 1709 - goto err_buffer_cleanup; 1695 + goto err_trigger_unregister; 1710 1696 } 1711 1697 1712 1698 ret = pm_runtime_set_active(&client->dev); ··· 1722 1708 1723 1709 err_iio_unregister: 1724 1710 iio_device_unregister(indio_dev); 1725 - err_buffer_cleanup: 1726 - if (indio_dev->pollfunc) 1727 - iio_triggered_buffer_cleanup(indio_dev); 1728 1711 err_trigger_unregister: 1729 1712 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1); 1713 + err_buffer_cleanup: 1714 + iio_triggered_buffer_cleanup(indio_dev); 1730 1715 1731 1716 return ret; 1732 1717 } ··· 1742 1729 iio_device_unregister(indio_dev); 1743 1730 1744 1731 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1); 1732 + 1733 + iio_triggered_buffer_cleanup(indio_dev); 1745 1734 1746 1735 mutex_lock(&data->mutex); 1747 1736 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0);
+7 -8
drivers/iio/accel/hid-sensor-accel-3d.c
··· 299 299 struct iio_dev *indio_dev; 300 300 struct accel_3d_state *accel_state; 301 301 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 302 - struct iio_chan_spec *channels; 303 302 304 303 indio_dev = devm_iio_device_alloc(&pdev->dev, 305 304 sizeof(struct accel_3d_state)); ··· 319 320 return ret; 320 321 } 321 322 322 - channels = kmemdup(accel_3d_channels, sizeof(accel_3d_channels), 323 - GFP_KERNEL); 324 - if (!channels) { 323 + indio_dev->channels = kmemdup(accel_3d_channels, 324 + sizeof(accel_3d_channels), GFP_KERNEL); 325 + if (!indio_dev->channels) { 325 326 dev_err(&pdev->dev, "failed to duplicate channels\n"); 326 327 return -ENOMEM; 327 328 } 328 329 329 - ret = accel_3d_parse_report(pdev, hsdev, channels, 330 - HID_USAGE_SENSOR_ACCEL_3D, accel_state); 330 + ret = accel_3d_parse_report(pdev, hsdev, 331 + (struct iio_chan_spec *)indio_dev->channels, 332 + HID_USAGE_SENSOR_ACCEL_3D, accel_state); 331 333 if (ret) { 332 334 dev_err(&pdev->dev, "failed to setup attributes\n"); 333 335 goto error_free_dev_mem; 334 336 } 335 337 336 - indio_dev->channels = channels; 337 338 indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels); 338 339 indio_dev->dev.parent = &pdev->dev; 339 340 indio_dev->info = &accel_3d_info; ··· 399 400 return 0; 400 401 } 401 402 402 - static struct platform_device_id hid_accel_3d_ids[] = { 403 + static const struct platform_device_id hid_accel_3d_ids[] = { 403 404 { 404 405 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 405 406 .name = "HID-SENSOR-200073",
+58 -4
drivers/iio/accel/mma8452.c
··· 32 32 #define MMA8452_OFF_Z 0x31 33 33 #define MMA8452_CTRL_REG1 0x2a 34 34 #define MMA8452_CTRL_REG2 0x2b 35 + #define MMA8452_CTRL_REG2_RST BIT(6) 36 + 37 + #define MMA8452_MAX_REG 0x31 35 38 36 39 #define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0)) 37 40 ··· 294 291 return IRQ_HANDLED; 295 292 } 296 293 294 + static int mma8452_reg_access_dbg(struct iio_dev *indio_dev, 295 + unsigned reg, unsigned writeval, 296 + unsigned *readval) 297 + { 298 + int ret; 299 + struct mma8452_data *data = iio_priv(indio_dev); 300 + 301 + if (reg > MMA8452_MAX_REG) 302 + return -EINVAL; 303 + 304 + if (!readval) 305 + return mma8452_change_config(data, reg, writeval); 306 + 307 + ret = i2c_smbus_read_byte_data(data->client, reg); 308 + if (ret < 0) 309 + return ret; 310 + 311 + *readval = ret; 312 + 313 + return 0; 314 + } 315 + 297 316 #define MMA8452_CHANNEL(axis, idx) { \ 298 317 .type = IIO_ACCEL, \ 299 318 .modified = 1, \ ··· 355 330 .attrs = &mma8452_group, 356 331 .read_raw = &mma8452_read_raw, 357 332 .write_raw = &mma8452_write_raw, 333 + .debugfs_reg_access = &mma8452_reg_access_dbg, 358 334 .driver_module = THIS_MODULE, 359 335 }; 360 336 361 337 static const unsigned long mma8452_scan_masks[] = {0x7, 0}; 338 + 339 + static int mma8452_reset(struct i2c_client *client) 340 + { 341 + int i; 342 + int ret; 343 + 344 + ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2, 345 + MMA8452_CTRL_REG2_RST); 346 + if (ret < 0) 347 + return ret; 348 + 349 + for (i = 0; i < 10; i++) { 350 + usleep_range(100, 200); 351 + ret = i2c_smbus_read_byte_data(client, MMA8452_CTRL_REG2); 352 + if (ret == -EIO) 353 + continue; /* I2C comm reset */ 354 + if (ret < 0) 355 + return ret; 356 + if (!(ret & MMA8452_CTRL_REG2_RST)) 357 + return 0; 358 + } 359 + 360 + return -ETIMEDOUT; 361 + } 362 362 363 363 static int mma8452_probe(struct i2c_client *client, 364 364 const struct i2c_device_id *id) ··· 415 365 indio_dev->num_channels = ARRAY_SIZE(mma8452_channels); 416 366 indio_dev->available_scan_masks = mma8452_scan_masks; 417 367 418 - data->ctrl_reg1 = MMA8452_CTRL_ACTIVE | 419 - (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT); 420 - ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1, 421 - data->ctrl_reg1); 368 + ret = mma8452_reset(client); 422 369 if (ret < 0) 423 370 return ret; 424 371 425 372 data->data_cfg = MMA8452_DATA_CFG_FS_2G; 426 373 ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG, 427 374 data->data_cfg); 375 + if (ret < 0) 376 + return ret; 377 + 378 + data->ctrl_reg1 = MMA8452_CTRL_ACTIVE | 379 + (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT); 380 + ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1, 381 + data->ctrl_reg1); 428 382 if (ret < 0) 429 383 return ret; 430 384
+390
drivers/iio/accel/stk8312.c
··· 1 + /** 2 + * Sensortek STK8312 3-Axis Accelerometer 3 + * 4 + * Copyright (c) 2015, Intel Corporation. 5 + * 6 + * This file is subject to the terms and conditions of version 2 of 7 + * the GNU General Public License. See the file COPYING in the main 8 + * directory of this archive for more details. 9 + * 10 + * IIO driver for STK8312; 7-bit I2C address: 0x3D. 11 + */ 12 + 13 + #include <linux/acpi.h> 14 + #include <linux/i2c.h> 15 + #include <linux/kernel.h> 16 + #include <linux/module.h> 17 + #include <linux/delay.h> 18 + #include <linux/iio/iio.h> 19 + #include <linux/iio/sysfs.h> 20 + 21 + #define STK8312_REG_XOUT 0x00 22 + #define STK8312_REG_YOUT 0x01 23 + #define STK8312_REG_ZOUT 0x02 24 + #define STK8312_REG_MODE 0x07 25 + #define STK8312_REG_STH 0x13 26 + #define STK8312_REG_RESET 0x20 27 + #define STK8312_REG_AFECTRL 0x24 28 + #define STK8312_REG_OTPADDR 0x3D 29 + #define STK8312_REG_OTPDATA 0x3E 30 + #define STK8312_REG_OTPCTRL 0x3F 31 + 32 + #define STK8312_MODE_ACTIVE 1 33 + #define STK8312_MODE_STANDBY 0 34 + #define STK8312_MODE_MASK 0x01 35 + #define STK8312_RNG_MASK 0xC0 36 + #define STK8312_RNG_SHIFT 6 37 + #define STK8312_READ_RETRIES 16 38 + 39 + #define STK8312_DRIVER_NAME "stk8312" 40 + 41 + /* 42 + * The accelerometer has two measurement ranges: 43 + * 44 + * -6g - +6g (8-bit, signed) 45 + * -16g - +16g (8-bit, signed) 46 + * 47 + * scale1 = (6 + 6) * 9.81 / (2^8 - 1) = 0.4616 48 + * scale2 = (16 + 16) * 9.81 / (2^8 - 1) = 1.2311 49 + */ 50 + #define STK8312_SCALE_AVAIL "0.4616 1.2311" 51 + 52 + static const int stk8312_scale_table[][2] = { 53 + {0, 461600}, {1, 231100} 54 + }; 55 + 56 + #define STK8312_ACCEL_CHANNEL(reg, axis) { \ 57 + .type = IIO_ACCEL, \ 58 + .address = reg, \ 59 + .modified = 1, \ 60 + .channel2 = IIO_MOD_##axis, \ 61 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 62 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 63 + } 64 + 65 + static const struct iio_chan_spec stk8312_channels[] = { 66 + STK8312_ACCEL_CHANNEL(STK8312_REG_XOUT, X), 67 + STK8312_ACCEL_CHANNEL(STK8312_REG_YOUT, Y), 68 + STK8312_ACCEL_CHANNEL(STK8312_REG_ZOUT, Z), 69 + }; 70 + 71 + struct stk8312_data { 72 + struct i2c_client *client; 73 + struct mutex lock; 74 + int range; 75 + u8 mode; 76 + }; 77 + 78 + static IIO_CONST_ATTR(in_accel_scale_available, STK8312_SCALE_AVAIL); 79 + 80 + static struct attribute *stk8312_attributes[] = { 81 + &iio_const_attr_in_accel_scale_available.dev_attr.attr, 82 + NULL, 83 + }; 84 + 85 + static const struct attribute_group stk8312_attribute_group = { 86 + .attrs = stk8312_attributes 87 + }; 88 + 89 + static int stk8312_otp_init(struct stk8312_data *data) 90 + { 91 + int ret; 92 + int count = 10; 93 + struct i2c_client *client = data->client; 94 + 95 + ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPADDR, 0x70); 96 + if (ret < 0) 97 + goto exit_err; 98 + ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPCTRL, 0x02); 99 + if (ret < 0) 100 + goto exit_err; 101 + 102 + do { 103 + usleep_range(1000, 5000); 104 + ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPCTRL); 105 + if (ret < 0) 106 + goto exit_err; 107 + count--; 108 + } while (!(ret & 0x80) && count > 0); 109 + 110 + if (count == 0) 111 + goto exit_err; 112 + 113 + ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPDATA); 114 + if (ret < 0) 115 + goto exit_err; 116 + 117 + ret = i2c_smbus_write_byte_data(data->client, 118 + STK8312_REG_AFECTRL, ret); 119 + if (ret < 0) 120 + goto exit_err; 121 + msleep(150); 122 + 123 + return ret; 124 + 125 + exit_err: 126 + dev_err(&client->dev, "failed to initialize sensor\n"); 127 + return ret; 128 + } 129 + 130 + static int stk8312_set_mode(struct stk8312_data *data, u8 mode) 131 + { 132 + int ret; 133 + u8 masked_reg; 134 + struct i2c_client *client = data->client; 135 + 136 + if (mode > 1) 137 + return -EINVAL; 138 + else if (mode == data->mode) 139 + return 0; 140 + 141 + ret = i2c_smbus_read_byte_data(client, STK8312_REG_MODE); 142 + if (ret < 0) { 143 + dev_err(&client->dev, "failed to change sensor mode\n"); 144 + return ret; 145 + } 146 + masked_reg = ret & (~STK8312_MODE_MASK); 147 + masked_reg |= mode; 148 + 149 + ret = i2c_smbus_write_byte_data(client, 150 + STK8312_REG_MODE, masked_reg); 151 + if (ret < 0) { 152 + dev_err(&client->dev, "failed to change sensor mode\n"); 153 + return ret; 154 + } 155 + 156 + data->mode = mode; 157 + if (mode == STK8312_MODE_ACTIVE) { 158 + /* Need to run OTP sequence before entering active mode */ 159 + usleep_range(1000, 5000); 160 + ret = stk8312_otp_init(data); 161 + } 162 + 163 + return ret; 164 + } 165 + 166 + static int stk8312_set_range(struct stk8312_data *data, u8 range) 167 + { 168 + int ret; 169 + u8 masked_reg; 170 + u8 mode; 171 + struct i2c_client *client = data->client; 172 + 173 + if (range != 1 && range != 2) 174 + return -EINVAL; 175 + else if (range == data->range) 176 + return 0; 177 + 178 + mode = data->mode; 179 + /* We need to go in standby mode to modify registers */ 180 + ret = stk8312_set_mode(data, STK8312_MODE_STANDBY); 181 + if (ret < 0) 182 + return ret; 183 + 184 + ret = i2c_smbus_read_byte_data(client, STK8312_REG_STH); 185 + if (ret < 0) { 186 + dev_err(&client->dev, "failed to change sensor range\n"); 187 + return ret; 188 + } 189 + 190 + masked_reg = ret & (~STK8312_RNG_MASK); 191 + masked_reg |= range << STK8312_RNG_SHIFT; 192 + 193 + ret = i2c_smbus_write_byte_data(client, STK8312_REG_STH, masked_reg); 194 + if (ret < 0) 195 + dev_err(&client->dev, "failed to change sensor range\n"); 196 + else 197 + data->range = range; 198 + 199 + return stk8312_set_mode(data, mode); 200 + } 201 + 202 + static int stk8312_read_accel(struct stk8312_data *data, u8 address) 203 + { 204 + int ret; 205 + struct i2c_client *client = data->client; 206 + 207 + if (address > 2) 208 + return -EINVAL; 209 + 210 + ret = i2c_smbus_read_byte_data(client, address); 211 + if (ret < 0) { 212 + dev_err(&client->dev, "register read failed\n"); 213 + return ret; 214 + } 215 + 216 + return sign_extend32(ret, 7); 217 + } 218 + 219 + static int stk8312_read_raw(struct iio_dev *indio_dev, 220 + struct iio_chan_spec const *chan, 221 + int *val, int *val2, long mask) 222 + { 223 + struct stk8312_data *data = iio_priv(indio_dev); 224 + 225 + if (chan->type != IIO_ACCEL) 226 + return -EINVAL; 227 + 228 + switch (mask) { 229 + case IIO_CHAN_INFO_RAW: 230 + mutex_lock(&data->lock); 231 + *val = stk8312_read_accel(data, chan->address); 232 + mutex_unlock(&data->lock); 233 + return IIO_VAL_INT; 234 + case IIO_CHAN_INFO_SCALE: 235 + *val = stk8312_scale_table[data->range - 1][0]; 236 + *val2 = stk8312_scale_table[data->range - 1][1]; 237 + return IIO_VAL_INT_PLUS_MICRO; 238 + } 239 + 240 + return -EINVAL; 241 + } 242 + 243 + static int stk8312_write_raw(struct iio_dev *indio_dev, 244 + struct iio_chan_spec const *chan, 245 + int val, int val2, long mask) 246 + { 247 + int i; 248 + int index = -1; 249 + int ret; 250 + struct stk8312_data *data = iio_priv(indio_dev); 251 + 252 + switch (mask) { 253 + case IIO_CHAN_INFO_SCALE: 254 + for (i = 0; i < ARRAY_SIZE(stk8312_scale_table); i++) 255 + if (val == stk8312_scale_table[i][0] && 256 + val2 == stk8312_scale_table[i][1]) { 257 + index = i + 1; 258 + break; 259 + } 260 + if (index < 0) 261 + return -EINVAL; 262 + 263 + mutex_lock(&data->lock); 264 + ret = stk8312_set_range(data, index); 265 + mutex_unlock(&data->lock); 266 + 267 + return ret; 268 + } 269 + 270 + return -EINVAL; 271 + } 272 + 273 + static const struct iio_info stk8312_info = { 274 + .driver_module = THIS_MODULE, 275 + .read_raw = stk8312_read_raw, 276 + .write_raw = stk8312_write_raw, 277 + .attrs = &stk8312_attribute_group, 278 + }; 279 + 280 + static int stk8312_probe(struct i2c_client *client, 281 + const struct i2c_device_id *id) 282 + { 283 + int ret; 284 + struct iio_dev *indio_dev; 285 + struct stk8312_data *data; 286 + 287 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 288 + if (!indio_dev) { 289 + dev_err(&client->dev, "iio allocation failed!\n"); 290 + return -ENOMEM; 291 + } 292 + 293 + data = iio_priv(indio_dev); 294 + data->client = client; 295 + i2c_set_clientdata(client, indio_dev); 296 + mutex_init(&data->lock); 297 + 298 + indio_dev->dev.parent = &client->dev; 299 + indio_dev->info = &stk8312_info; 300 + indio_dev->name = STK8312_DRIVER_NAME; 301 + indio_dev->modes = INDIO_DIRECT_MODE; 302 + indio_dev->channels = stk8312_channels; 303 + indio_dev->num_channels = ARRAY_SIZE(stk8312_channels); 304 + 305 + /* A software reset is recommended at power-on */ 306 + ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_RESET, 0x00); 307 + if (ret < 0) { 308 + dev_err(&client->dev, "failed to reset sensor\n"); 309 + return ret; 310 + } 311 + ret = stk8312_set_range(data, 1); 312 + if (ret < 0) 313 + return ret; 314 + 315 + ret = stk8312_set_mode(data, STK8312_MODE_ACTIVE); 316 + if (ret < 0) 317 + return ret; 318 + 319 + ret = iio_device_register(indio_dev); 320 + if (ret < 0) { 321 + dev_err(&client->dev, "device_register failed\n"); 322 + stk8312_set_mode(data, STK8312_MODE_STANDBY); 323 + } 324 + 325 + return ret; 326 + } 327 + 328 + static int stk8312_remove(struct i2c_client *client) 329 + { 330 + struct iio_dev *indio_dev = i2c_get_clientdata(client); 331 + 332 + iio_device_unregister(indio_dev); 333 + 334 + return stk8312_set_mode(iio_priv(indio_dev), STK8312_MODE_STANDBY); 335 + } 336 + 337 + #ifdef CONFIG_PM_SLEEP 338 + static int stk8312_suspend(struct device *dev) 339 + { 340 + struct stk8312_data *data; 341 + 342 + data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 343 + 344 + return stk8312_set_mode(data, STK8312_MODE_STANDBY); 345 + } 346 + 347 + static int stk8312_resume(struct device *dev) 348 + { 349 + struct stk8312_data *data; 350 + 351 + data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 352 + 353 + return stk8312_set_mode(data, STK8312_MODE_ACTIVE); 354 + } 355 + 356 + static SIMPLE_DEV_PM_OPS(stk8312_pm_ops, stk8312_suspend, stk8312_resume); 357 + 358 + #define STK8312_PM_OPS (&stk8312_pm_ops) 359 + #else 360 + #define STK8312_PM_OPS NULL 361 + #endif 362 + 363 + static const struct i2c_device_id stk8312_i2c_id[] = { 364 + {"STK8312", 0}, 365 + {} 366 + }; 367 + 368 + static const struct acpi_device_id stk8312_acpi_id[] = { 369 + {"STK8312", 0}, 370 + {} 371 + }; 372 + 373 + MODULE_DEVICE_TABLE(acpi, stk8312_acpi_id); 374 + 375 + static struct i2c_driver stk8312_driver = { 376 + .driver = { 377 + .name = "stk8312", 378 + .pm = STK8312_PM_OPS, 379 + .acpi_match_table = ACPI_PTR(stk8312_acpi_id), 380 + }, 381 + .probe = stk8312_probe, 382 + .remove = stk8312_remove, 383 + .id_table = stk8312_i2c_id, 384 + }; 385 + 386 + module_i2c_driver(stk8312_driver); 387 + 388 + MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); 389 + MODULE_DESCRIPTION("STK8312 3-Axis Accelerometer driver"); 390 + MODULE_LICENSE("GPL v2");
+302
drivers/iio/accel/stk8ba50.c
··· 1 + /** 2 + * Sensortek STK8BA50 3-Axis Accelerometer 3 + * 4 + * Copyright (c) 2015, Intel Corporation. 5 + * 6 + * This file is subject to the terms and conditions of version 2 of 7 + * the GNU General Public License. See the file COPYING in the main 8 + * directory of this archive for more details. 9 + * 10 + * STK8BA50 7-bit I2C address: 0x18. 11 + */ 12 + 13 + #include <linux/acpi.h> 14 + #include <linux/i2c.h> 15 + #include <linux/kernel.h> 16 + #include <linux/module.h> 17 + #include <linux/iio/iio.h> 18 + #include <linux/iio/sysfs.h> 19 + 20 + #define STK8BA50_REG_XOUT 0x02 21 + #define STK8BA50_REG_YOUT 0x04 22 + #define STK8BA50_REG_ZOUT 0x06 23 + #define STK8BA50_REG_RANGE 0x0F 24 + #define STK8BA50_REG_POWMODE 0x11 25 + #define STK8BA50_REG_SWRST 0x14 26 + 27 + #define STK8BA50_MODE_NORMAL 0 28 + #define STK8BA50_MODE_SUSPEND 1 29 + #define STK8BA50_MODE_POWERBIT BIT(7) 30 + #define STK8BA50_DATA_SHIFT 6 31 + #define STK8BA50_RESET_CMD 0xB6 32 + 33 + #define STK8BA50_DRIVER_NAME "stk8ba50" 34 + 35 + #define STK8BA50_SCALE_AVAIL "0.0384 0.0767 0.1534 0.3069" 36 + 37 + /* 38 + * The accelerometer has four measurement ranges: 39 + * +/-2g; +/-4g; +/-8g; +/-16g 40 + * 41 + * Acceleration values are 10-bit, 2's complement. 42 + * Scales are calculated as following: 43 + * 44 + * scale1 = (2 + 2) * 9.81 / (2^10 - 1) = 0.0384 45 + * scale2 = (4 + 4) * 9.81 / (2^10 - 1) = 0.0767 46 + * etc. 47 + * 48 + * Scales are stored in this format: 49 + * { <register value>, <scale value> } 50 + * 51 + * Locally, the range is stored as a table index. 52 + */ 53 + static const int stk8ba50_scale_table[][2] = { 54 + {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900} 55 + }; 56 + 57 + struct stk8ba50_data { 58 + struct i2c_client *client; 59 + struct mutex lock; 60 + int range; 61 + }; 62 + 63 + #define STK8BA50_ACCEL_CHANNEL(reg, axis) { \ 64 + .type = IIO_ACCEL, \ 65 + .address = reg, \ 66 + .modified = 1, \ 67 + .channel2 = IIO_MOD_##axis, \ 68 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 69 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 70 + } 71 + 72 + static const struct iio_chan_spec stk8ba50_channels[] = { 73 + STK8BA50_ACCEL_CHANNEL(STK8BA50_REG_XOUT, X), 74 + STK8BA50_ACCEL_CHANNEL(STK8BA50_REG_YOUT, Y), 75 + STK8BA50_ACCEL_CHANNEL(STK8BA50_REG_ZOUT, Z), 76 + }; 77 + 78 + static IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL); 79 + 80 + static struct attribute *stk8ba50_attributes[] = { 81 + &iio_const_attr_in_accel_scale_available.dev_attr.attr, 82 + NULL, 83 + }; 84 + 85 + static const struct attribute_group stk8ba50_attribute_group = { 86 + .attrs = stk8ba50_attributes 87 + }; 88 + 89 + static int stk8ba50_read_accel(struct stk8ba50_data *data, u8 reg) 90 + { 91 + int ret; 92 + struct i2c_client *client = data->client; 93 + 94 + ret = i2c_smbus_read_word_data(client, reg); 95 + if (ret < 0) { 96 + dev_err(&client->dev, "register read failed\n"); 97 + return ret; 98 + } 99 + 100 + return sign_extend32(ret >> STK8BA50_DATA_SHIFT, 9); 101 + } 102 + 103 + static int stk8ba50_read_raw(struct iio_dev *indio_dev, 104 + struct iio_chan_spec const *chan, 105 + int *val, int *val2, long mask) 106 + { 107 + struct stk8ba50_data *data = iio_priv(indio_dev); 108 + 109 + switch (mask) { 110 + case IIO_CHAN_INFO_RAW: 111 + mutex_lock(&data->lock); 112 + *val = stk8ba50_read_accel(data, chan->address); 113 + mutex_unlock(&data->lock); 114 + return IIO_VAL_INT; 115 + case IIO_CHAN_INFO_SCALE: 116 + *val = 0; 117 + *val2 = stk8ba50_scale_table[data->range][1]; 118 + return IIO_VAL_INT_PLUS_MICRO; 119 + } 120 + 121 + return -EINVAL; 122 + } 123 + 124 + static int stk8ba50_write_raw(struct iio_dev *indio_dev, 125 + struct iio_chan_spec const *chan, 126 + int val, int val2, long mask) 127 + { 128 + int ret; 129 + int i; 130 + int index = -1; 131 + struct stk8ba50_data *data = iio_priv(indio_dev); 132 + 133 + switch (mask) { 134 + case IIO_CHAN_INFO_SCALE: 135 + if (val != 0) 136 + return -EINVAL; 137 + 138 + for (i = 0; i < ARRAY_SIZE(stk8ba50_scale_table); i++) 139 + if (val2 == stk8ba50_scale_table[i][1]) { 140 + index = i; 141 + break; 142 + } 143 + if (index < 0) 144 + return -EINVAL; 145 + 146 + ret = i2c_smbus_write_byte_data(data->client, 147 + STK8BA50_REG_RANGE, 148 + stk8ba50_scale_table[index][0]); 149 + if (ret < 0) 150 + dev_err(&data->client->dev, 151 + "failed to set measurement range\n"); 152 + else 153 + data->range = index; 154 + 155 + return ret; 156 + } 157 + 158 + return -EINVAL; 159 + } 160 + 161 + static const struct iio_info stk8ba50_info = { 162 + .driver_module = THIS_MODULE, 163 + .read_raw = stk8ba50_read_raw, 164 + .write_raw = stk8ba50_write_raw, 165 + .attrs = &stk8ba50_attribute_group, 166 + }; 167 + 168 + static int stk8ba50_set_power(struct stk8ba50_data *data, bool mode) 169 + { 170 + int ret; 171 + u8 masked_reg; 172 + struct i2c_client *client = data->client; 173 + 174 + ret = i2c_smbus_read_byte_data(client, STK8BA50_REG_POWMODE); 175 + if (ret < 0) 176 + goto exit_err; 177 + 178 + if (mode) 179 + masked_reg = ret | STK8BA50_MODE_POWERBIT; 180 + else 181 + masked_reg = ret & (~STK8BA50_MODE_POWERBIT); 182 + 183 + ret = i2c_smbus_write_byte_data(client, STK8BA50_REG_POWMODE, 184 + masked_reg); 185 + if (ret < 0) 186 + goto exit_err; 187 + 188 + return ret; 189 + 190 + exit_err: 191 + dev_err(&client->dev, "failed to change sensor mode\n"); 192 + return ret; 193 + } 194 + 195 + static int stk8ba50_probe(struct i2c_client *client, 196 + const struct i2c_device_id *id) 197 + { 198 + int ret; 199 + struct iio_dev *indio_dev; 200 + struct stk8ba50_data *data; 201 + 202 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 203 + if (!indio_dev) { 204 + dev_err(&client->dev, "iio allocation failed!\n"); 205 + return -ENOMEM; 206 + } 207 + 208 + data = iio_priv(indio_dev); 209 + data->client = client; 210 + i2c_set_clientdata(client, indio_dev); 211 + mutex_init(&data->lock); 212 + 213 + indio_dev->dev.parent = &client->dev; 214 + indio_dev->info = &stk8ba50_info; 215 + indio_dev->name = STK8BA50_DRIVER_NAME; 216 + indio_dev->modes = INDIO_DIRECT_MODE; 217 + indio_dev->channels = stk8ba50_channels; 218 + indio_dev->num_channels = ARRAY_SIZE(stk8ba50_channels); 219 + 220 + /* Reset all registers on startup */ 221 + ret = i2c_smbus_write_byte_data(client, 222 + STK8BA50_REG_SWRST, STK8BA50_RESET_CMD); 223 + if (ret < 0) { 224 + dev_err(&client->dev, "failed to reset sensor\n"); 225 + return ret; 226 + } 227 + 228 + /* The default range is +/-2g */ 229 + data->range = 0; 230 + 231 + ret = iio_device_register(indio_dev); 232 + if (ret < 0) { 233 + dev_err(&client->dev, "device_register failed\n"); 234 + stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 235 + } 236 + 237 + return ret; 238 + } 239 + 240 + static int stk8ba50_remove(struct i2c_client *client) 241 + { 242 + struct iio_dev *indio_dev = i2c_get_clientdata(client); 243 + 244 + iio_device_unregister(indio_dev); 245 + 246 + return stk8ba50_set_power(iio_priv(indio_dev), STK8BA50_MODE_SUSPEND); 247 + } 248 + 249 + #ifdef CONFIG_PM_SLEEP 250 + static int stk8ba50_suspend(struct device *dev) 251 + { 252 + struct stk8ba50_data *data; 253 + 254 + data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 255 + 256 + return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 257 + } 258 + 259 + static int stk8ba50_resume(struct device *dev) 260 + { 261 + struct stk8ba50_data *data; 262 + 263 + data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 264 + 265 + return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL); 266 + } 267 + 268 + static SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops, stk8ba50_suspend, stk8ba50_resume); 269 + 270 + #define STK8BA50_PM_OPS (&stk8ba50_pm_ops) 271 + #else 272 + #define STK8BA50_PM_OPS NULL 273 + #endif 274 + 275 + static const struct i2c_device_id stk8ba50_i2c_id[] = { 276 + {"stk8ba50", 0}, 277 + {} 278 + }; 279 + 280 + static const struct acpi_device_id stk8ba50_acpi_id[] = { 281 + {"STK8BA50", 0}, 282 + {} 283 + }; 284 + 285 + MODULE_DEVICE_TABLE(acpi, stk8ba50_acpi_id); 286 + 287 + static struct i2c_driver stk8ba50_driver = { 288 + .driver = { 289 + .name = "stk8ba50", 290 + .pm = STK8BA50_PM_OPS, 291 + .acpi_match_table = ACPI_PTR(stk8ba50_acpi_id), 292 + }, 293 + .probe = stk8ba50_probe, 294 + .remove = stk8ba50_remove, 295 + .id_table = stk8ba50_i2c_id, 296 + }; 297 + 298 + module_i2c_driver(stk8ba50_driver); 299 + 300 + MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); 301 + MODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver"); 302 + MODULE_LICENSE("GPL v2");
+9 -2
drivers/iio/adc/Kconfig
··· 135 135 device. Depending on platform configuration, this general purpose ADC can 136 136 be used for sampling sensors such as thermal resistors. 137 137 138 + config BERLIN2_ADC 139 + tristate "Marvell Berlin2 ADC driver" 140 + depends on ARCH_BERLIN 141 + help 142 + Marvell Berlin2 ADC driver. This ADC has 8 channels, with one used for 143 + temperature measurement. 144 + 138 145 config DA9150_GPADC 139 146 tristate "Dialog DA9150 GPADC driver support" 140 147 depends on MFD_DA9150 ··· 292 285 called ti-adc081c. 293 286 294 287 config TI_ADC128S052 295 - tristate "Texas Instruments ADC128S052" 288 + tristate "Texas Instruments ADC128S052/ADC122S021" 296 289 depends on SPI 297 290 help 298 291 If you say yes here you get support for Texas Instruments ADC128S052 299 - chip. 292 + and ADC122S021 chips. 300 293 301 294 This driver can also be built as a module. If so, the module will be 302 295 called ti-adc128s052.
+1
drivers/iio/adc/Makefile
··· 15 15 obj-$(CONFIG_AD799X) += ad799x.o 16 16 obj-$(CONFIG_AT91_ADC) += at91_adc.o 17 17 obj-$(CONFIG_AXP288_ADC) += axp288_adc.o 18 + obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o 18 19 obj-$(CONFIG_DA9150_GPADC) += da9150-gpadc.o 19 20 obj-$(CONFIG_CC10001_ADC) += cc10001_adc.o 20 21 obj-$(CONFIG_EXYNOS_ADC) += exynos_adc.o
+1 -1
drivers/iio/adc/axp288_adc.c
··· 238 238 return 0; 239 239 } 240 240 241 - static struct platform_device_id axp288_adc_id_table[] = { 241 + static const struct platform_device_id axp288_adc_id_table[] = { 242 242 { .name = "axp288_adc" }, 243 243 {}, 244 244 };
+378
drivers/iio/adc/berlin2-adc.c
··· 1 + /* 2 + * Marvell Berlin2 ADC driver 3 + * 4 + * Copyright (C) 2015 Marvell Technology Group Ltd. 5 + * 6 + * Antoine Tenart <antoine.tenart@free-electrons.com> 7 + * 8 + * This file is licensed under the terms of the GNU General Public 9 + * License version 2. This program is licensed "as is" without any 10 + * warranty of any kind, whether express or implied. 11 + */ 12 + 13 + #include <linux/iio/iio.h> 14 + #include <linux/iio/driver.h> 15 + #include <linux/iio/machine.h> 16 + #include <linux/interrupt.h> 17 + #include <linux/kernel.h> 18 + #include <linux/module.h> 19 + #include <linux/platform_device.h> 20 + #include <linux/slab.h> 21 + #include <linux/mfd/syscon.h> 22 + #include <linux/regmap.h> 23 + #include <linux/sched.h> 24 + #include <linux/wait.h> 25 + 26 + #define BERLIN2_SM_CTRL 0x14 27 + #define BERLIN2_SM_CTRL_SM_SOC_INT BIT(1) 28 + #define BERLIN2_SM_CTRL_SOC_SM_INT BIT(2) 29 + #define BERLIN2_SM_CTRL_ADC_SEL(x) (BIT(x) << 5) /* 0-15 */ 30 + #define BERLIN2_SM_CTRL_ADC_SEL_MASK (0xf << 5) 31 + #define BERLIN2_SM_CTRL_ADC_POWER BIT(9) 32 + #define BERLIN2_SM_CTRL_ADC_CLKSEL_DIV2 (0x0 << 10) 33 + #define BERLIN2_SM_CTRL_ADC_CLKSEL_DIV3 (0x1 << 10) 34 + #define BERLIN2_SM_CTRL_ADC_CLKSEL_DIV4 (0x2 << 10) 35 + #define BERLIN2_SM_CTRL_ADC_CLKSEL_DIV8 (0x3 << 10) 36 + #define BERLIN2_SM_CTRL_ADC_CLKSEL_MASK (0x3 << 10) 37 + #define BERLIN2_SM_CTRL_ADC_START BIT(12) 38 + #define BERLIN2_SM_CTRL_ADC_RESET BIT(13) 39 + #define BERLIN2_SM_CTRL_ADC_BANDGAP_RDY BIT(14) 40 + #define BERLIN2_SM_CTRL_ADC_CONT_SINGLE (0x0 << 15) 41 + #define BERLIN2_SM_CTRL_ADC_CONT_CONTINUOUS (0x1 << 15) 42 + #define BERLIN2_SM_CTRL_ADC_BUFFER_EN BIT(16) 43 + #define BERLIN2_SM_CTRL_ADC_VREF_EXT (0x0 << 17) 44 + #define BERLIN2_SM_CTRL_ADC_VREF_INT (0x1 << 17) 45 + #define BERLIN2_SM_CTRL_ADC_ROTATE BIT(19) 46 + #define BERLIN2_SM_CTRL_TSEN_EN BIT(20) 47 + #define BERLIN2_SM_CTRL_TSEN_CLK_SEL_125 (0x0 << 21) /* 1.25 MHz */ 48 + #define BERLIN2_SM_CTRL_TSEN_CLK_SEL_250 (0x1 << 21) /* 2.5 MHz */ 49 + #define BERLIN2_SM_CTRL_TSEN_MODE_0_125 (0x0 << 22) /* 0-125 C */ 50 + #define BERLIN2_SM_CTRL_TSEN_MODE_10_50 (0x1 << 22) /* 10-50 C */ 51 + #define BERLIN2_SM_CTRL_TSEN_RESET BIT(29) 52 + #define BERLIN2_SM_ADC_DATA 0x20 53 + #define BERLIN2_SM_ADC_MASK 0x3ff 54 + #define BERLIN2_SM_ADC_STATUS 0x1c 55 + #define BERLIN2_SM_ADC_STATUS_DATA_RDY(x) BIT(x) /* 0-15 */ 56 + #define BERLIN2_SM_ADC_STATUS_DATA_RDY_MASK 0xf 57 + #define BERLIN2_SM_ADC_STATUS_INT_EN(x) (BIT(x) << 16) /* 0-15 */ 58 + #define BERLIN2_SM_ADC_STATUS_INT_EN_MASK (0xf << 16) 59 + #define BERLIN2_SM_TSEN_STATUS 0x24 60 + #define BERLIN2_SM_TSEN_STATUS_DATA_RDY BIT(0) 61 + #define BERLIN2_SM_TSEN_STATUS_INT_EN BIT(1) 62 + #define BERLIN2_SM_TSEN_DATA 0x28 63 + #define BERLIN2_SM_TSEN_MASK 0xfff 64 + #define BERLIN2_SM_TSEN_CTRL 0x74 65 + #define BERLIN2_SM_TSEN_CTRL_START BIT(8) 66 + #define BERLIN2_SM_TSEN_CTRL_SETTLING_4 (0x0 << 21) /* 4 us */ 67 + #define BERLIN2_SM_TSEN_CTRL_SETTLING_12 (0x1 << 21) /* 12 us */ 68 + #define BERLIN2_SM_TSEN_CTRL_SETTLING_MASK (0x1 << 21) 69 + #define BERLIN2_SM_TSEN_CTRL_TRIM(x) ((x) << 22) 70 + #define BERLIN2_SM_TSEN_CTRL_TRIM_MASK (0xf << 22) 71 + 72 + struct berlin2_adc_priv { 73 + struct regmap *regmap; 74 + struct mutex lock; 75 + wait_queue_head_t wq; 76 + bool data_available; 77 + int data; 78 + }; 79 + 80 + #define BERLIN2_ADC_CHANNEL(n, t) \ 81 + { \ 82 + .channel = n, \ 83 + .datasheet_name = "channel"#n, \ 84 + .type = t, \ 85 + .indexed = 1, \ 86 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 87 + } 88 + 89 + static struct iio_chan_spec berlin2_adc_channels[] = { 90 + BERLIN2_ADC_CHANNEL(0, IIO_VOLTAGE), /* external input */ 91 + BERLIN2_ADC_CHANNEL(1, IIO_VOLTAGE), /* external input */ 92 + BERLIN2_ADC_CHANNEL(2, IIO_VOLTAGE), /* external input */ 93 + BERLIN2_ADC_CHANNEL(3, IIO_VOLTAGE), /* external input */ 94 + BERLIN2_ADC_CHANNEL(4, IIO_VOLTAGE), /* reserved */ 95 + BERLIN2_ADC_CHANNEL(5, IIO_VOLTAGE), /* reserved */ 96 + { /* temperature sensor */ 97 + .channel = 6, 98 + .datasheet_name = "channel6", 99 + .type = IIO_TEMP, 100 + .indexed = 0, 101 + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 102 + }, 103 + BERLIN2_ADC_CHANNEL(7, IIO_VOLTAGE), /* reserved */ 104 + IIO_CHAN_SOFT_TIMESTAMP(8), /* timestamp */ 105 + }; 106 + #define BERLIN2_N_CHANNELS ARRAY_SIZE(berlin2_adc_channels) 107 + 108 + static int berlin2_adc_read(struct iio_dev *indio_dev, int channel) 109 + { 110 + struct berlin2_adc_priv *priv = iio_priv(indio_dev); 111 + int data, ret; 112 + 113 + mutex_lock(&priv->lock); 114 + 115 + /* Configure the ADC */ 116 + regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL, 117 + BERLIN2_SM_CTRL_ADC_RESET | BERLIN2_SM_CTRL_ADC_SEL_MASK 118 + | BERLIN2_SM_CTRL_ADC_START, 119 + BERLIN2_SM_CTRL_ADC_SEL(channel) | BERLIN2_SM_CTRL_ADC_START); 120 + 121 + ret = wait_event_interruptible_timeout(priv->wq, priv->data_available, 122 + msecs_to_jiffies(1000)); 123 + 124 + /* Disable the interrupts */ 125 + regmap_update_bits(priv->regmap, BERLIN2_SM_ADC_STATUS, 126 + BERLIN2_SM_ADC_STATUS_INT_EN(channel), 0); 127 + 128 + if (ret == 0) 129 + ret = -ETIMEDOUT; 130 + if (ret < 0) { 131 + mutex_unlock(&priv->lock); 132 + return ret; 133 + } 134 + 135 + regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL, 136 + BERLIN2_SM_CTRL_ADC_START, 0); 137 + 138 + data = priv->data; 139 + priv->data_available = false; 140 + 141 + mutex_unlock(&priv->lock); 142 + 143 + return data; 144 + } 145 + 146 + static int berlin2_adc_tsen_read(struct iio_dev *indio_dev) 147 + { 148 + struct berlin2_adc_priv *priv = iio_priv(indio_dev); 149 + int data, ret; 150 + 151 + mutex_lock(&priv->lock); 152 + 153 + /* Configure the ADC */ 154 + regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL, 155 + BERLIN2_SM_CTRL_TSEN_RESET | BERLIN2_SM_CTRL_ADC_ROTATE, 156 + BERLIN2_SM_CTRL_ADC_ROTATE); 157 + 158 + /* Configure the temperature sensor */ 159 + regmap_update_bits(priv->regmap, BERLIN2_SM_TSEN_CTRL, 160 + BERLIN2_SM_TSEN_CTRL_TRIM_MASK | BERLIN2_SM_TSEN_CTRL_SETTLING_MASK 161 + | BERLIN2_SM_TSEN_CTRL_START, 162 + BERLIN2_SM_TSEN_CTRL_TRIM(3) | BERLIN2_SM_TSEN_CTRL_SETTLING_12 163 + | BERLIN2_SM_TSEN_CTRL_START); 164 + 165 + ret = wait_event_interruptible_timeout(priv->wq, priv->data_available, 166 + msecs_to_jiffies(1000)); 167 + 168 + /* Disable interrupts */ 169 + regmap_update_bits(priv->regmap, BERLIN2_SM_TSEN_STATUS, 170 + BERLIN2_SM_TSEN_STATUS_INT_EN, 0); 171 + 172 + if (ret == 0) 173 + ret = -ETIMEDOUT; 174 + if (ret < 0) { 175 + mutex_unlock(&priv->lock); 176 + return ret; 177 + } 178 + 179 + regmap_update_bits(priv->regmap, BERLIN2_SM_TSEN_CTRL, 180 + BERLIN2_SM_TSEN_CTRL_START, 0); 181 + 182 + data = priv->data; 183 + priv->data_available = false; 184 + 185 + mutex_unlock(&priv->lock); 186 + 187 + return data; 188 + } 189 + 190 + static int berlin2_adc_read_raw(struct iio_dev *indio_dev, 191 + struct iio_chan_spec const *chan, int *val, int *val2, 192 + long mask) 193 + { 194 + struct berlin2_adc_priv *priv = iio_priv(indio_dev); 195 + int temp; 196 + 197 + switch (mask) { 198 + case IIO_CHAN_INFO_RAW: 199 + if (chan->type != IIO_VOLTAGE) 200 + return -EINVAL; 201 + 202 + /* Enable the interrupts */ 203 + regmap_write(priv->regmap, BERLIN2_SM_ADC_STATUS, 204 + BERLIN2_SM_ADC_STATUS_INT_EN(chan->channel)); 205 + 206 + *val = berlin2_adc_read(indio_dev, chan->channel); 207 + if (*val < 0) 208 + return *val; 209 + 210 + return IIO_VAL_INT; 211 + case IIO_CHAN_INFO_PROCESSED: 212 + if (chan->type != IIO_TEMP) 213 + return -EINVAL; 214 + 215 + /* Enable interrupts */ 216 + regmap_write(priv->regmap, BERLIN2_SM_TSEN_STATUS, 217 + BERLIN2_SM_TSEN_STATUS_INT_EN); 218 + 219 + temp = berlin2_adc_tsen_read(indio_dev); 220 + if (temp < 0) 221 + return temp; 222 + 223 + if (temp > 2047) 224 + temp = -(4096 - temp); 225 + 226 + /* Convert to milli Celsius */ 227 + *val = ((temp * 100000) / 264 - 270000); 228 + return IIO_VAL_INT; 229 + default: 230 + break; 231 + } 232 + 233 + return -EINVAL; 234 + } 235 + 236 + static irqreturn_t berlin2_adc_irq(int irq, void *private) 237 + { 238 + struct berlin2_adc_priv *priv = iio_priv(private); 239 + unsigned val; 240 + 241 + regmap_read(priv->regmap, BERLIN2_SM_ADC_STATUS, &val); 242 + if (val & BERLIN2_SM_ADC_STATUS_DATA_RDY_MASK) { 243 + regmap_read(priv->regmap, BERLIN2_SM_ADC_DATA, &priv->data); 244 + priv->data &= BERLIN2_SM_ADC_MASK; 245 + 246 + val &= ~BERLIN2_SM_ADC_STATUS_DATA_RDY_MASK; 247 + regmap_write(priv->regmap, BERLIN2_SM_ADC_STATUS, val); 248 + 249 + priv->data_available = true; 250 + wake_up_interruptible(&priv->wq); 251 + } 252 + 253 + return IRQ_HANDLED; 254 + } 255 + 256 + static irqreturn_t berlin2_adc_tsen_irq(int irq, void *private) 257 + { 258 + struct berlin2_adc_priv *priv = iio_priv(private); 259 + unsigned val; 260 + 261 + regmap_read(priv->regmap, BERLIN2_SM_TSEN_STATUS, &val); 262 + if (val & BERLIN2_SM_TSEN_STATUS_DATA_RDY) { 263 + regmap_read(priv->regmap, BERLIN2_SM_TSEN_DATA, &priv->data); 264 + priv->data &= BERLIN2_SM_TSEN_MASK; 265 + 266 + val &= ~BERLIN2_SM_TSEN_STATUS_DATA_RDY; 267 + regmap_write(priv->regmap, BERLIN2_SM_TSEN_STATUS, val); 268 + 269 + priv->data_available = true; 270 + wake_up_interruptible(&priv->wq); 271 + } 272 + 273 + return IRQ_HANDLED; 274 + } 275 + 276 + static const struct iio_info berlin2_adc_info = { 277 + .driver_module = THIS_MODULE, 278 + .read_raw = berlin2_adc_read_raw, 279 + }; 280 + 281 + static int berlin2_adc_probe(struct platform_device *pdev) 282 + { 283 + struct iio_dev *indio_dev; 284 + struct berlin2_adc_priv *priv; 285 + struct device_node *parent_np = of_get_parent(pdev->dev.of_node); 286 + int irq, tsen_irq; 287 + int ret; 288 + 289 + indio_dev = devm_iio_device_alloc(&pdev->dev, 290 + sizeof(struct berlin2_adc_priv)); 291 + if (!indio_dev) 292 + return -ENOMEM; 293 + 294 + priv = iio_priv(indio_dev); 295 + platform_set_drvdata(pdev, indio_dev); 296 + 297 + priv->regmap = syscon_node_to_regmap(parent_np); 298 + of_node_put(parent_np); 299 + if (IS_ERR(priv->regmap)) 300 + return PTR_ERR(priv->regmap); 301 + 302 + irq = platform_get_irq_byname(pdev, "adc"); 303 + if (irq < 0) 304 + return -ENODEV; 305 + 306 + tsen_irq = platform_get_irq_byname(pdev, "tsen"); 307 + if (tsen_irq < 0) 308 + return -ENODEV; 309 + 310 + ret = devm_request_irq(&pdev->dev, irq, berlin2_adc_irq, 0, 311 + pdev->dev.driver->name, indio_dev); 312 + if (ret) 313 + return ret; 314 + 315 + ret = devm_request_irq(&pdev->dev, tsen_irq, berlin2_adc_tsen_irq, 316 + 0, pdev->dev.driver->name, indio_dev); 317 + if (ret) 318 + return ret; 319 + 320 + init_waitqueue_head(&priv->wq); 321 + mutex_init(&priv->lock); 322 + 323 + indio_dev->dev.parent = &pdev->dev; 324 + indio_dev->name = dev_name(&pdev->dev); 325 + indio_dev->modes = INDIO_DIRECT_MODE; 326 + indio_dev->info = &berlin2_adc_info; 327 + 328 + indio_dev->num_channels = BERLIN2_N_CHANNELS; 329 + indio_dev->channels = berlin2_adc_channels; 330 + 331 + /* Power up the ADC */ 332 + regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL, 333 + BERLIN2_SM_CTRL_ADC_POWER, BERLIN2_SM_CTRL_ADC_POWER); 334 + 335 + ret = iio_device_register(indio_dev); 336 + if (ret) { 337 + /* Power down the ADC */ 338 + regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL, 339 + BERLIN2_SM_CTRL_ADC_POWER, 0); 340 + return ret; 341 + } 342 + 343 + return 0; 344 + } 345 + 346 + static int berlin2_adc_remove(struct platform_device *pdev) 347 + { 348 + struct iio_dev *indio_dev = platform_get_drvdata(pdev); 349 + struct berlin2_adc_priv *priv = iio_priv(indio_dev); 350 + 351 + iio_device_unregister(indio_dev); 352 + 353 + /* Power down the ADC */ 354 + regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL, 355 + BERLIN2_SM_CTRL_ADC_POWER, 0); 356 + 357 + return 0; 358 + } 359 + 360 + static const struct of_device_id berlin2_adc_match[] = { 361 + { .compatible = "marvell,berlin2-adc", }, 362 + { }, 363 + }; 364 + MODULE_DEVICE_TABLE(of, berlin2_adc_match); 365 + 366 + static struct platform_driver berlin2_adc_driver = { 367 + .driver = { 368 + .name = "berlin2-adc", 369 + .of_match_table = berlin2_adc_match, 370 + }, 371 + .probe = berlin2_adc_probe, 372 + .remove = berlin2_adc_remove, 373 + }; 374 + module_platform_driver(berlin2_adc_driver); 375 + 376 + MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>"); 377 + MODULE_DESCRIPTION("Marvell Berlin2 ADC driver"); 378 + MODULE_LICENSE("GPL v2");
+24 -6
drivers/iio/adc/ti-adc128s052.c
··· 1 1 /* 2 2 * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com> 3 3 * 4 - * Driver for Texas Instruments' ADC128S052 ADC chip. 5 - * Datasheet can be found here: 4 + * Driver for Texas Instruments' ADC128S052 and ADC122S021 ADC chip. 5 + * Datasheets can be found here: 6 6 * http://www.ti.com/lit/ds/symlink/adc128s052.pdf 7 + * http://www.ti.com/lit/ds/symlink/adc122s021.pdf 7 8 * 8 9 * This program is free software; you can redistribute it and/or modify 9 10 * it under the terms of the GNU General Public License version 2 as ··· 16 15 #include <linux/module.h> 17 16 #include <linux/iio/iio.h> 18 17 #include <linux/regulator/consumer.h> 18 + 19 + struct adc128_configuration { 20 + const struct iio_chan_spec *channels; 21 + u8 num_channels; 22 + }; 19 23 20 24 struct adc128 { 21 25 struct spi_device *spi; ··· 98 92 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \ 99 93 } 100 94 101 - static const struct iio_chan_spec adc128_channels[] = { 95 + static const struct iio_chan_spec adc128s052_channels[] = { 102 96 ADC128_VOLTAGE_CHANNEL(0), 103 97 ADC128_VOLTAGE_CHANNEL(1), 104 98 ADC128_VOLTAGE_CHANNEL(2), ··· 107 101 ADC128_VOLTAGE_CHANNEL(5), 108 102 ADC128_VOLTAGE_CHANNEL(6), 109 103 ADC128_VOLTAGE_CHANNEL(7), 104 + }; 105 + 106 + static const struct iio_chan_spec adc122s021_channels[] = { 107 + ADC128_VOLTAGE_CHANNEL(0), 108 + ADC128_VOLTAGE_CHANNEL(1), 109 + }; 110 + 111 + static const struct adc128_configuration adc128_config[] = { 112 + { adc128s052_channels, ARRAY_SIZE(adc128s052_channels) }, 113 + { adc122s021_channels, ARRAY_SIZE(adc122s021_channels) }, 110 114 }; 111 115 112 116 static const struct iio_info adc128_info = { ··· 128 112 { 129 113 struct iio_dev *indio_dev; 130 114 struct adc128 *adc; 115 + int config = spi_get_device_id(spi)->driver_data; 131 116 int ret; 132 117 133 118 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc)); ··· 145 128 indio_dev->modes = INDIO_DIRECT_MODE; 146 129 indio_dev->info = &adc128_info; 147 130 148 - indio_dev->channels = adc128_channels; 149 - indio_dev->num_channels = ARRAY_SIZE(adc128_channels); 131 + indio_dev->channels = adc128_config[config].channels; 132 + indio_dev->num_channels = adc128_config[config].num_channels; 150 133 151 134 adc->reg = devm_regulator_get(&spi->dev, "vref"); 152 135 if (IS_ERR(adc->reg)) ··· 175 158 } 176 159 177 160 static const struct spi_device_id adc128_id[] = { 178 - { "adc128s052", 0}, 161 + { "adc128s052", 0}, /* index into adc128_config */ 162 + { "adc122s021", 1}, 179 163 { } 180 164 }; 181 165 MODULE_DEVICE_TABLE(spi, adc128_id);
+48 -6
drivers/iio/adc/ti_am335x_adc.c
··· 37 37 u8 channel_step[8]; 38 38 int buffer_en_ch_steps; 39 39 u16 data[8]; 40 + u32 open_delay[8], sample_delay[8], step_avg[8]; 40 41 }; 41 42 42 43 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg) ··· 86 85 static void tiadc_step_config(struct iio_dev *indio_dev) 87 86 { 88 87 struct tiadc_device *adc_dev = iio_priv(indio_dev); 88 + struct device *dev = adc_dev->mfd_tscadc->dev; 89 89 unsigned int stepconfig; 90 90 int i, steps = 0; 91 91 ··· 100 98 * needs to be given to ADC to digitalize data. 101 99 */ 102 100 103 - if (iio_buffer_enabled(indio_dev)) 104 - stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1 105 - | STEPCONFIG_MODE_SWCNT; 106 - else 107 - stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1; 108 101 109 102 for (i = 0; i < adc_dev->channels; i++) { 110 103 int chan; 111 104 112 105 chan = adc_dev->channel_line[i]; 106 + 107 + if (adc_dev->step_avg[i] > STEPCONFIG_AVG_16) { 108 + dev_warn(dev, "chan %d step_avg truncating to %d\n", 109 + chan, STEPCONFIG_AVG_16); 110 + adc_dev->step_avg[i] = STEPCONFIG_AVG_16; 111 + } 112 + 113 + if (adc_dev->step_avg[i]) 114 + stepconfig = 115 + STEPCONFIG_AVG(ffs(adc_dev->step_avg[i]) - 1) | 116 + STEPCONFIG_FIFO1; 117 + else 118 + stepconfig = STEPCONFIG_FIFO1; 119 + 120 + if (iio_buffer_enabled(indio_dev)) 121 + stepconfig |= STEPCONFIG_MODE_SWCNT; 122 + 113 123 tiadc_writel(adc_dev, REG_STEPCONFIG(steps), 114 124 stepconfig | STEPCONFIG_INP(chan)); 125 + 126 + if (adc_dev->open_delay[i] > STEPDELAY_OPEN_MASK) { 127 + dev_warn(dev, "chan %d open delay truncating to 0x3FFFF\n", 128 + chan); 129 + adc_dev->open_delay[i] = STEPDELAY_OPEN_MASK; 130 + } 131 + 132 + if (adc_dev->sample_delay[i] > 0xFF) { 133 + dev_warn(dev, "chan %d sample delay truncating to 0xFF\n", 134 + chan); 135 + adc_dev->sample_delay[i] = 0xFF; 136 + } 137 + 115 138 tiadc_writel(adc_dev, REG_STEPDELAY(steps), 116 - STEPCONFIG_OPENDLY); 139 + STEPDELAY_OPEN(adc_dev->open_delay[i]) | 140 + STEPDELAY_SAMPLE(adc_dev->sample_delay[i])); 141 + 117 142 adc_dev->channel_step[i] = steps; 118 143 steps++; 119 144 } ··· 435 406 436 407 of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) { 437 408 adc_dev->channel_line[channels] = val; 409 + 410 + /* Set Default values for optional DT parameters */ 411 + adc_dev->open_delay[channels] = STEPCONFIG_OPENDLY; 412 + adc_dev->sample_delay[channels] = STEPCONFIG_SAMPLEDLY; 413 + adc_dev->step_avg[channels] = 16; 414 + 438 415 channels++; 439 416 } 417 + 418 + of_property_read_u32_array(node, "ti,chan-step-avg", 419 + adc_dev->step_avg, channels); 420 + of_property_read_u32_array(node, "ti,chan-step-opendelay", 421 + adc_dev->open_delay, channels); 422 + of_property_read_u32_array(node, "ti,chan-step-sampledelay", 423 + adc_dev->sample_delay, channels); 440 424 441 425 adc_dev->channels = channels; 442 426 return 0;
+10
drivers/iio/dac/Kconfig
··· 142 142 To compile this driver as module choose M here: the module will be called 143 143 ad7303. 144 144 145 + config M62332 146 + tristate "Mitsubishi M62332 DAC driver" 147 + depends on I2C 148 + help 149 + If you say yes here you get support for the Mitsubishi M62332 150 + (I2C 8-Bit DACs with rail-to-rail outputs). 151 + 152 + This driver can also be built as a module. If so, the module 153 + will be called m62332. 154 + 145 155 config MAX517 146 156 tristate "Maxim MAX517/518/519/520/521 DAC driver" 147 157 depends on I2C
+1
drivers/iio/dac/Makefile
··· 16 16 obj-$(CONFIG_AD5791) += ad5791.o 17 17 obj-$(CONFIG_AD5686) += ad5686.o 18 18 obj-$(CONFIG_AD7303) += ad7303.o 19 + obj-$(CONFIG_M62332) += m62332.o 19 20 obj-$(CONFIG_MAX517) += max517.o 20 21 obj-$(CONFIG_MAX5821) += max5821.o 21 22 obj-$(CONFIG_MCP4725) += mcp4725.o
+269
drivers/iio/dac/m62332.c
··· 1 + /* 2 + * m62332.c - Support for Mitsubishi m62332 DAC 3 + * 4 + * Copyright (c) 2014 Dmitry Eremin-Solenikov 5 + * 6 + * Based on max517 driver: 7 + * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de> 8 + * 9 + * This program is free software; you can redistribute it and/or modify 10 + * it under the terms of the GNU General Public License as published by 11 + * the Free Software Foundation; either version 2 of the License, or 12 + * (at your option) any later version. 13 + * 14 + * This program is distributed in the hope that it will be useful, 15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 + * GNU General Public License for more details. 18 + */ 19 + 20 + #include <linux/module.h> 21 + #include <linux/slab.h> 22 + #include <linux/i2c.h> 23 + #include <linux/err.h> 24 + 25 + #include <linux/iio/iio.h> 26 + #include <linux/iio/driver.h> 27 + 28 + #include <linux/regulator/consumer.h> 29 + 30 + #define M62332_CHANNELS 2 31 + 32 + struct m62332_data { 33 + struct i2c_client *client; 34 + u16 vref_mv; 35 + struct regulator *vcc; 36 + struct mutex mutex; 37 + u8 raw[M62332_CHANNELS]; 38 + #ifdef CONFIG_PM_SLEEP 39 + u8 save[M62332_CHANNELS]; 40 + #endif 41 + }; 42 + 43 + static int m62332_set_value(struct iio_dev *indio_dev, 44 + u8 val, int channel) 45 + { 46 + struct m62332_data *data = iio_priv(indio_dev); 47 + struct i2c_client *client = data->client; 48 + u8 outbuf[2]; 49 + int res; 50 + 51 + if (val == data->raw[channel]) 52 + return 0; 53 + 54 + outbuf[0] = channel; 55 + outbuf[1] = val; 56 + 57 + mutex_lock(&data->mutex); 58 + 59 + if (val) { 60 + res = regulator_enable(data->vcc); 61 + if (res) 62 + goto out; 63 + } 64 + 65 + res = i2c_master_send(client, outbuf, 2); 66 + if (res >= 0 && res != 2) 67 + res = -EIO; 68 + if (res < 0) 69 + goto out; 70 + 71 + data->raw[channel] = val; 72 + 73 + if (!val) 74 + regulator_disable(data->vcc); 75 + 76 + mutex_unlock(&data->mutex); 77 + 78 + return 0; 79 + 80 + out: 81 + mutex_unlock(&data->mutex); 82 + 83 + return res; 84 + } 85 + 86 + static int m62332_read_raw(struct iio_dev *indio_dev, 87 + struct iio_chan_spec const *chan, 88 + int *val, 89 + int *val2, 90 + long m) 91 + { 92 + struct m62332_data *data = iio_priv(indio_dev); 93 + 94 + switch (m) { 95 + case IIO_CHAN_INFO_SCALE: 96 + /* Corresponds to Vref / 2^(bits) */ 97 + *val = data->vref_mv; 98 + *val2 = 8; 99 + return IIO_VAL_FRACTIONAL_LOG2; 100 + case IIO_CHAN_INFO_RAW: 101 + *val = data->raw[chan->channel]; 102 + return IIO_VAL_INT; 103 + case IIO_CHAN_INFO_OFFSET: 104 + *val = 1; 105 + return IIO_VAL_INT; 106 + default: 107 + break; 108 + } 109 + return -EINVAL; 110 + } 111 + 112 + static int m62332_write_raw(struct iio_dev *indio_dev, 113 + struct iio_chan_spec const *chan, int val, int val2, long mask) 114 + { 115 + int ret; 116 + 117 + switch (mask) { 118 + case IIO_CHAN_INFO_RAW: 119 + if (val < 0 || val > 255) 120 + return -EINVAL; 121 + 122 + ret = m62332_set_value(indio_dev, val, chan->channel); 123 + break; 124 + default: 125 + ret = -EINVAL; 126 + break; 127 + } 128 + 129 + return ret; 130 + } 131 + 132 + #ifdef CONFIG_PM_SLEEP 133 + static int m62332_suspend(struct device *dev) 134 + { 135 + struct i2c_client *client = to_i2c_client(dev); 136 + struct iio_dev *indio_dev = i2c_get_clientdata(client); 137 + struct m62332_data *data = iio_priv(indio_dev); 138 + int ret; 139 + 140 + data->save[0] = data->raw[0]; 141 + data->save[1] = data->raw[1]; 142 + 143 + ret = m62332_set_value(indio_dev, 0, 0); 144 + if (ret < 0) 145 + return ret; 146 + 147 + return m62332_set_value(indio_dev, 0, 1); 148 + } 149 + 150 + static int m62332_resume(struct device *dev) 151 + { 152 + struct i2c_client *client = to_i2c_client(dev); 153 + struct iio_dev *indio_dev = i2c_get_clientdata(client); 154 + struct m62332_data *data = iio_priv(indio_dev); 155 + int ret; 156 + 157 + ret = m62332_set_value(indio_dev, data->save[0], 0); 158 + if (ret < 0) 159 + return ret; 160 + 161 + return m62332_set_value(indio_dev, data->save[1], 1); 162 + } 163 + 164 + static SIMPLE_DEV_PM_OPS(m62332_pm_ops, m62332_suspend, m62332_resume); 165 + #define M62332_PM_OPS (&m62332_pm_ops) 166 + #else 167 + #define M62332_PM_OPS NULL 168 + #endif 169 + 170 + static const struct iio_info m62332_info = { 171 + .read_raw = m62332_read_raw, 172 + .write_raw = m62332_write_raw, 173 + .driver_module = THIS_MODULE, 174 + }; 175 + 176 + #define M62332_CHANNEL(chan) { \ 177 + .type = IIO_VOLTAGE, \ 178 + .indexed = 1, \ 179 + .output = 1, \ 180 + .channel = (chan), \ 181 + .datasheet_name = "CH" #chan, \ 182 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 183 + BIT(IIO_CHAN_INFO_SCALE) | \ 184 + BIT(IIO_CHAN_INFO_OFFSET), \ 185 + } 186 + 187 + static const struct iio_chan_spec m62332_channels[M62332_CHANNELS] = { 188 + M62332_CHANNEL(0), 189 + M62332_CHANNEL(1) 190 + }; 191 + 192 + static int m62332_probe(struct i2c_client *client, 193 + const struct i2c_device_id *id) 194 + { 195 + struct m62332_data *data; 196 + struct iio_dev *indio_dev; 197 + int ret; 198 + 199 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 200 + if (!indio_dev) 201 + return -ENOMEM; 202 + data = iio_priv(indio_dev); 203 + i2c_set_clientdata(client, indio_dev); 204 + data->client = client; 205 + 206 + mutex_init(&data->mutex); 207 + 208 + data->vcc = devm_regulator_get(&client->dev, "VCC"); 209 + if (IS_ERR(data->vcc)) 210 + return PTR_ERR(data->vcc); 211 + 212 + /* establish that the iio_dev is a child of the i2c device */ 213 + indio_dev->dev.parent = &client->dev; 214 + 215 + indio_dev->num_channels = M62332_CHANNELS; 216 + indio_dev->channels = m62332_channels; 217 + indio_dev->modes = INDIO_DIRECT_MODE; 218 + indio_dev->info = &m62332_info; 219 + 220 + ret = regulator_get_voltage(data->vcc); 221 + if (ret < 0) 222 + return ret; 223 + data->vref_mv = ret / 1000; /* mV */ 224 + 225 + ret = iio_map_array_register(indio_dev, client->dev.platform_data); 226 + if (ret < 0) 227 + return ret; 228 + 229 + ret = iio_device_register(indio_dev); 230 + if (ret < 0) 231 + goto err; 232 + 233 + return 0; 234 + 235 + err: 236 + iio_map_array_unregister(indio_dev); 237 + return ret; 238 + } 239 + 240 + static int m62332_remove(struct i2c_client *client) 241 + { 242 + struct iio_dev *indio_dev = i2c_get_clientdata(client); 243 + 244 + iio_device_unregister(indio_dev); 245 + iio_map_array_unregister(indio_dev); 246 + 247 + return 0; 248 + } 249 + 250 + static const struct i2c_device_id m62332_id[] = { 251 + { "m62332", }, 252 + { } 253 + }; 254 + MODULE_DEVICE_TABLE(i2c, m62332_id); 255 + 256 + static struct i2c_driver m62332_driver = { 257 + .driver = { 258 + .name = "m62332", 259 + .pm = M62332_PM_OPS, 260 + }, 261 + .probe = m62332_probe, 262 + .remove = m62332_remove, 263 + .id_table = m62332_id, 264 + }; 265 + module_i2c_driver(m62332_driver); 266 + 267 + MODULE_AUTHOR("Dmitry Eremin-Solenikov"); 268 + MODULE_DESCRIPTION("M62332 8-bit DAC"); 269 + MODULE_LICENSE("GPL v2");
+36 -31
drivers/iio/gyro/bmg160.c
··· 108 108 int slope_thres; 109 109 bool dready_trigger_on; 110 110 bool motion_trigger_on; 111 - int64_t timestamp; 112 111 }; 113 112 114 113 enum bmg160_axis { ··· 737 738 return 0; 738 739 } 739 740 740 - static int bmg160_validate_trigger(struct iio_dev *indio_dev, 741 - struct iio_trigger *trig) 742 - { 743 - struct bmg160_data *data = iio_priv(indio_dev); 744 - 745 - if (data->dready_trig != trig && data->motion_trig != trig) 746 - return -EINVAL; 747 - 748 - return 0; 749 - } 750 - 751 741 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 400 1000 2000"); 752 742 753 743 static IIO_CONST_ATTR(in_anglvel_scale_available, ··· 798 810 .write_event_value = bmg160_write_event, 799 811 .write_event_config = bmg160_write_event_config, 800 812 .read_event_config = bmg160_read_event_config, 801 - .validate_trigger = bmg160_validate_trigger, 802 813 .driver_module = THIS_MODULE, 803 814 }; 804 815 ··· 822 835 mutex_unlock(&data->mutex); 823 836 824 837 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 825 - data->timestamp); 838 + pf->timestamp); 826 839 err: 827 840 iio_trigger_notify_done(indio_dev->trig); 828 841 ··· 925 938 IIO_MOD_X, 926 939 IIO_EV_TYPE_ROC, 927 940 dir), 928 - data->timestamp); 941 + iio_get_time_ns()); 929 942 if (ret & BMG160_ANY_MOTION_BIT_Y) 930 943 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL, 931 944 0, 932 945 IIO_MOD_Y, 933 946 IIO_EV_TYPE_ROC, 934 947 dir), 935 - data->timestamp); 948 + iio_get_time_ns()); 936 949 if (ret & BMG160_ANY_MOTION_BIT_Z) 937 950 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL, 938 951 0, 939 952 IIO_MOD_Z, 940 953 IIO_EV_TYPE_ROC, 941 954 dir), 942 - data->timestamp); 955 + iio_get_time_ns()); 943 956 944 957 ack_intr_status: 945 958 if (!data->dready_trigger_on) { ··· 960 973 struct iio_dev *indio_dev = private; 961 974 struct bmg160_data *data = iio_priv(indio_dev); 962 975 963 - data->timestamp = iio_get_time_ns(); 964 - 965 976 if (data->dready_trigger_on) 966 977 iio_trigger_poll(data->dready_trig); 967 978 else if (data->motion_trigger_on) ··· 971 986 return IRQ_HANDLED; 972 987 973 988 } 989 + 990 + static int bmg160_buffer_preenable(struct iio_dev *indio_dev) 991 + { 992 + struct bmg160_data *data = iio_priv(indio_dev); 993 + 994 + return bmg160_set_power_state(data, true); 995 + } 996 + 997 + static int bmg160_buffer_postdisable(struct iio_dev *indio_dev) 998 + { 999 + struct bmg160_data *data = iio_priv(indio_dev); 1000 + 1001 + return bmg160_set_power_state(data, false); 1002 + } 1003 + 1004 + static const struct iio_buffer_setup_ops bmg160_buffer_setup_ops = { 1005 + .preenable = bmg160_buffer_preenable, 1006 + .postenable = iio_triggered_buffer_postenable, 1007 + .predisable = iio_triggered_buffer_predisable, 1008 + .postdisable = bmg160_buffer_postdisable, 1009 + }; 974 1010 975 1011 static int bmg160_gpio_probe(struct i2c_client *client, 976 1012 struct bmg160_data *data) ··· 1109 1103 data->motion_trig = NULL; 1110 1104 goto err_trigger_unregister; 1111 1105 } 1106 + } 1112 1107 1113 - ret = iio_triggered_buffer_setup(indio_dev, 1114 - NULL, 1115 - bmg160_trigger_handler, 1116 - NULL); 1117 - if (ret < 0) { 1118 - dev_err(&client->dev, 1119 - "iio triggered buffer setup failed\n"); 1120 - goto err_trigger_unregister; 1121 - } 1108 + ret = iio_triggered_buffer_setup(indio_dev, 1109 + iio_pollfunc_store_time, 1110 + bmg160_trigger_handler, 1111 + &bmg160_buffer_setup_ops); 1112 + if (ret < 0) { 1113 + dev_err(&client->dev, 1114 + "iio triggered buffer setup failed\n"); 1115 + goto err_trigger_unregister; 1122 1116 } 1123 1117 1124 1118 ret = iio_device_register(indio_dev); ··· 1141 1135 err_iio_unregister: 1142 1136 iio_device_unregister(indio_dev); 1143 1137 err_buffer_cleanup: 1144 - if (data->dready_trig) 1145 - iio_triggered_buffer_cleanup(indio_dev); 1138 + iio_triggered_buffer_cleanup(indio_dev); 1146 1139 err_trigger_unregister: 1147 1140 if (data->dready_trig) 1148 1141 iio_trigger_unregister(data->dready_trig); ··· 1161 1156 pm_runtime_put_noidle(&client->dev); 1162 1157 1163 1158 iio_device_unregister(indio_dev); 1159 + iio_triggered_buffer_cleanup(indio_dev); 1164 1160 1165 1161 if (data->dready_trig) { 1166 - iio_triggered_buffer_cleanup(indio_dev); 1167 1162 iio_trigger_unregister(data->dready_trig); 1168 1163 iio_trigger_unregister(data->motion_trig); 1169 1164 }
+7 -8
drivers/iio/gyro/hid-sensor-gyro-3d.c
··· 298 298 struct iio_dev *indio_dev; 299 299 struct gyro_3d_state *gyro_state; 300 300 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 301 - struct iio_chan_spec *channels; 302 301 303 302 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state)); 304 303 if (!indio_dev) ··· 316 317 return ret; 317 318 } 318 319 319 - channels = kmemdup(gyro_3d_channels, sizeof(gyro_3d_channels), 320 - GFP_KERNEL); 321 - if (!channels) { 320 + indio_dev->channels = kmemdup(gyro_3d_channels, 321 + sizeof(gyro_3d_channels), GFP_KERNEL); 322 + if (!indio_dev->channels) { 322 323 dev_err(&pdev->dev, "failed to duplicate channels\n"); 323 324 return -ENOMEM; 324 325 } 325 326 326 - ret = gyro_3d_parse_report(pdev, hsdev, channels, 327 - HID_USAGE_SENSOR_GYRO_3D, gyro_state); 327 + ret = gyro_3d_parse_report(pdev, hsdev, 328 + (struct iio_chan_spec *)indio_dev->channels, 329 + HID_USAGE_SENSOR_GYRO_3D, gyro_state); 328 330 if (ret) { 329 331 dev_err(&pdev->dev, "failed to setup attributes\n"); 330 332 goto error_free_dev_mem; 331 333 } 332 334 333 - indio_dev->channels = channels; 334 335 indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels); 335 336 indio_dev->dev.parent = &pdev->dev; 336 337 indio_dev->info = &gyro_3d_info; ··· 396 397 return 0; 397 398 } 398 399 399 - static struct platform_device_id hid_gyro_3d_ids[] = { 400 + static const struct platform_device_id hid_gyro_3d_ids[] = { 400 401 { 401 402 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 402 403 .name = "HID-SENSOR-200076",
+1 -1
drivers/iio/humidity/Kconfig
··· 5 5 6 6 config DHT11 7 7 tristate "DHT11 (and compatible sensors) driver" 8 - depends on GPIOLIB 8 + depends on GPIOLIB || COMPILE_TEST 9 9 help 10 10 This driver supports reading data via a single interrupt 11 11 generating GPIO line. Currently tested are DHT11 and DHT22.
+205 -123
drivers/iio/industrialio-buffer.c
··· 539 539 iio_buffer_put(buffer); 540 540 } 541 541 542 - void iio_disable_all_buffers(struct iio_dev *indio_dev) 542 + static void iio_buffer_deactivate_all(struct iio_dev *indio_dev) 543 543 { 544 544 struct iio_buffer *buffer, *_buffer; 545 - 546 - if (list_empty(&indio_dev->buffer_list)) 547 - return; 548 - 549 - if (indio_dev->setup_ops->predisable) 550 - indio_dev->setup_ops->predisable(indio_dev); 551 545 552 546 list_for_each_entry_safe(buffer, _buffer, 553 547 &indio_dev->buffer_list, buffer_list) 554 548 iio_buffer_deactivate(buffer); 555 - 556 - indio_dev->currentmode = INDIO_DIRECT_MODE; 557 - if (indio_dev->setup_ops->postdisable) 558 - indio_dev->setup_ops->postdisable(indio_dev); 559 - 560 - if (indio_dev->available_scan_masks == NULL) 561 - kfree(indio_dev->active_scan_mask); 562 549 } 563 550 564 551 static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, ··· 562 575 buffer->access->set_bytes_per_datum(buffer, bytes); 563 576 } 564 577 565 - static int __iio_update_buffers(struct iio_dev *indio_dev, 566 - struct iio_buffer *insert_buffer, 567 - struct iio_buffer *remove_buffer) 578 + static int iio_buffer_request_update(struct iio_dev *indio_dev, 579 + struct iio_buffer *buffer) 568 580 { 569 581 int ret; 570 - int success = 0; 571 - struct iio_buffer *buffer; 572 - unsigned long *compound_mask; 573 - const unsigned long *old_mask; 574 582 575 - /* Wind down existing buffers - iff there are any */ 576 - if (!list_empty(&indio_dev->buffer_list)) { 577 - if (indio_dev->setup_ops->predisable) { 578 - ret = indio_dev->setup_ops->predisable(indio_dev); 579 - if (ret) 580 - return ret; 581 - } 582 - indio_dev->currentmode = INDIO_DIRECT_MODE; 583 - if (indio_dev->setup_ops->postdisable) { 584 - ret = indio_dev->setup_ops->postdisable(indio_dev); 585 - if (ret) 586 - return ret; 583 + iio_buffer_update_bytes_per_datum(indio_dev, buffer); 584 + if (buffer->access->request_update) { 585 + ret = buffer->access->request_update(buffer); 586 + if (ret) { 587 + dev_dbg(&indio_dev->dev, 588 + "Buffer not started: buffer parameter update failed (%d)\n", 589 + ret); 590 + return ret; 587 591 } 588 592 } 589 - /* Keep a copy of current setup to allow roll back */ 590 - old_mask = indio_dev->active_scan_mask; 593 + 594 + return 0; 595 + } 596 + 597 + static void iio_free_scan_mask(struct iio_dev *indio_dev, 598 + const unsigned long *mask) 599 + { 600 + /* If the mask is dynamically allocated free it, otherwise do nothing */ 591 601 if (!indio_dev->available_scan_masks) 592 - indio_dev->active_scan_mask = NULL; 602 + kfree(mask); 603 + } 593 604 594 - if (remove_buffer) 595 - iio_buffer_deactivate(remove_buffer); 596 - if (insert_buffer) 597 - iio_buffer_activate(indio_dev, insert_buffer); 605 + struct iio_device_config { 606 + unsigned int mode; 607 + const unsigned long *scan_mask; 608 + unsigned int scan_bytes; 609 + bool scan_timestamp; 610 + }; 598 611 599 - /* If no buffers in list, we are done */ 600 - if (list_empty(&indio_dev->buffer_list)) { 601 - indio_dev->currentmode = INDIO_DIRECT_MODE; 602 - if (indio_dev->available_scan_masks == NULL) 603 - kfree(old_mask); 604 - return 0; 612 + static int iio_verify_update(struct iio_dev *indio_dev, 613 + struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer, 614 + struct iio_device_config *config) 615 + { 616 + unsigned long *compound_mask; 617 + const unsigned long *scan_mask; 618 + struct iio_buffer *buffer; 619 + bool scan_timestamp; 620 + 621 + memset(config, 0, sizeof(*config)); 622 + 623 + /* 624 + * If there is just one buffer and we are removing it there is nothing 625 + * to verify. 626 + */ 627 + if (remove_buffer && !insert_buffer && 628 + list_is_singular(&indio_dev->buffer_list)) 629 + return 0; 630 + 631 + /* Definitely possible for devices to support both of these. */ 632 + if ((indio_dev->modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) { 633 + config->mode = INDIO_BUFFER_TRIGGERED; 634 + } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) { 635 + config->mode = INDIO_BUFFER_HARDWARE; 636 + } else if (indio_dev->modes & INDIO_BUFFER_SOFTWARE) { 637 + config->mode = INDIO_BUFFER_SOFTWARE; 638 + } else { 639 + /* Can only occur on first buffer */ 640 + if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 641 + dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n"); 642 + return -EINVAL; 605 643 } 606 644 607 645 /* What scan mask do we actually have? */ 608 646 compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), 609 647 sizeof(long), GFP_KERNEL); 610 - if (compound_mask == NULL) { 611 - if (indio_dev->available_scan_masks == NULL) 612 - kfree(old_mask); 648 + if (compound_mask == NULL) 613 649 return -ENOMEM; 614 - } 615 - indio_dev->scan_timestamp = 0; 650 + 651 + scan_timestamp = false; 616 652 617 653 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 654 + if (buffer == remove_buffer) 655 + continue; 618 656 bitmap_or(compound_mask, compound_mask, buffer->scan_mask, 619 657 indio_dev->masklength); 620 - indio_dev->scan_timestamp |= buffer->scan_timestamp; 658 + scan_timestamp |= buffer->scan_timestamp; 621 659 } 660 + 661 + if (insert_buffer) { 662 + bitmap_or(compound_mask, compound_mask, 663 + insert_buffer->scan_mask, indio_dev->masklength); 664 + scan_timestamp |= insert_buffer->scan_timestamp; 665 + } 666 + 622 667 if (indio_dev->available_scan_masks) { 623 - indio_dev->active_scan_mask = 624 - iio_scan_mask_match(indio_dev->available_scan_masks, 625 - indio_dev->masklength, 626 - compound_mask); 627 - if (indio_dev->active_scan_mask == NULL) { 628 - /* 629 - * Roll back. 630 - * Note can only occur when adding a buffer. 631 - */ 632 - iio_buffer_deactivate(insert_buffer); 633 - if (old_mask) { 634 - indio_dev->active_scan_mask = old_mask; 635 - success = -EINVAL; 636 - } 637 - else { 638 - kfree(compound_mask); 639 - ret = -EINVAL; 640 - return ret; 641 - } 642 - } 668 + scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks, 669 + indio_dev->masklength, 670 + compound_mask); 671 + kfree(compound_mask); 672 + if (scan_mask == NULL) 673 + return -EINVAL; 643 674 } else { 644 - indio_dev->active_scan_mask = compound_mask; 675 + scan_mask = compound_mask; 645 676 } 677 + 678 + config->scan_bytes = iio_compute_scan_bytes(indio_dev, 679 + scan_mask, scan_timestamp); 680 + config->scan_mask = scan_mask; 681 + config->scan_timestamp = scan_timestamp; 682 + 683 + return 0; 684 + } 685 + 686 + static int iio_enable_buffers(struct iio_dev *indio_dev, 687 + struct iio_device_config *config) 688 + { 689 + int ret; 690 + 691 + indio_dev->active_scan_mask = config->scan_mask; 692 + indio_dev->scan_timestamp = config->scan_timestamp; 693 + indio_dev->scan_bytes = config->scan_bytes; 646 694 647 695 iio_update_demux(indio_dev); 648 696 ··· 685 663 if (indio_dev->setup_ops->preenable) { 686 664 ret = indio_dev->setup_ops->preenable(indio_dev); 687 665 if (ret) { 688 - printk(KERN_ERR 666 + dev_dbg(&indio_dev->dev, 689 667 "Buffer not started: buffer preenable failed (%d)\n", ret); 690 - goto error_remove_inserted; 668 + goto err_undo_config; 691 669 } 692 670 } 693 - indio_dev->scan_bytes = 694 - iio_compute_scan_bytes(indio_dev, 695 - indio_dev->active_scan_mask, 696 - indio_dev->scan_timestamp); 697 - list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 698 - iio_buffer_update_bytes_per_datum(indio_dev, buffer); 699 - if (buffer->access->request_update) { 700 - ret = buffer->access->request_update(buffer); 701 - if (ret) { 702 - printk(KERN_INFO 703 - "Buffer not started: buffer parameter update failed (%d)\n", ret); 704 - goto error_run_postdisable; 705 - } 706 - } 707 - } 671 + 708 672 if (indio_dev->info->update_scan_mode) { 709 673 ret = indio_dev->info 710 674 ->update_scan_mode(indio_dev, 711 675 indio_dev->active_scan_mask); 712 676 if (ret < 0) { 713 - printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret); 714 - goto error_run_postdisable; 677 + dev_dbg(&indio_dev->dev, 678 + "Buffer not started: update scan mode failed (%d)\n", 679 + ret); 680 + goto err_run_postdisable; 715 681 } 716 682 } 717 - /* Definitely possible for devices to support both of these. */ 718 - if ((indio_dev->modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) { 719 - indio_dev->currentmode = INDIO_BUFFER_TRIGGERED; 720 - } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) { 721 - indio_dev->currentmode = INDIO_BUFFER_HARDWARE; 722 - } else if (indio_dev->modes & INDIO_BUFFER_SOFTWARE) { 723 - indio_dev->currentmode = INDIO_BUFFER_SOFTWARE; 724 - } else { /* Should never be reached */ 725 - /* Can only occur on first buffer */ 726 - if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 727 - pr_info("Buffer not started: no trigger\n"); 728 - ret = -EINVAL; 729 - goto error_run_postdisable; 730 - } 683 + 684 + indio_dev->currentmode = config->mode; 731 685 732 686 if (indio_dev->setup_ops->postenable) { 733 687 ret = indio_dev->setup_ops->postenable(indio_dev); 734 688 if (ret) { 735 - printk(KERN_INFO 689 + dev_dbg(&indio_dev->dev, 736 690 "Buffer not started: postenable failed (%d)\n", ret); 737 - indio_dev->currentmode = INDIO_DIRECT_MODE; 738 - if (indio_dev->setup_ops->postdisable) 739 - indio_dev->setup_ops->postdisable(indio_dev); 740 - goto error_disable_all_buffers; 691 + goto err_run_postdisable; 741 692 } 742 693 } 743 694 744 - if (indio_dev->available_scan_masks) 745 - kfree(compound_mask); 746 - else 747 - kfree(old_mask); 695 + return 0; 748 696 749 - return success; 750 - 751 - error_disable_all_buffers: 697 + err_run_postdisable: 752 698 indio_dev->currentmode = INDIO_DIRECT_MODE; 753 - error_run_postdisable: 754 699 if (indio_dev->setup_ops->postdisable) 755 700 indio_dev->setup_ops->postdisable(indio_dev); 756 - error_remove_inserted: 701 + err_undo_config: 702 + indio_dev->active_scan_mask = NULL; 703 + 704 + return ret; 705 + } 706 + 707 + static int iio_disable_buffers(struct iio_dev *indio_dev) 708 + { 709 + int ret = 0; 710 + int ret2; 711 + 712 + /* Wind down existing buffers - iff there are any */ 713 + if (list_empty(&indio_dev->buffer_list)) 714 + return 0; 715 + 716 + /* 717 + * If things go wrong at some step in disable we still need to continue 718 + * to perform the other steps, otherwise we leave the device in a 719 + * inconsistent state. We return the error code for the first error we 720 + * encountered. 721 + */ 722 + 723 + if (indio_dev->setup_ops->predisable) { 724 + ret2 = indio_dev->setup_ops->predisable(indio_dev); 725 + if (ret2 && !ret) 726 + ret = ret2; 727 + } 728 + 729 + indio_dev->currentmode = INDIO_DIRECT_MODE; 730 + 731 + if (indio_dev->setup_ops->postdisable) { 732 + ret2 = indio_dev->setup_ops->postdisable(indio_dev); 733 + if (ret2 && !ret) 734 + ret = ret2; 735 + } 736 + 737 + iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask); 738 + indio_dev->active_scan_mask = NULL; 739 + 740 + return ret; 741 + } 742 + 743 + static int __iio_update_buffers(struct iio_dev *indio_dev, 744 + struct iio_buffer *insert_buffer, 745 + struct iio_buffer *remove_buffer) 746 + { 747 + struct iio_device_config new_config; 748 + int ret; 749 + 750 + ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer, 751 + &new_config); 752 + if (ret) 753 + return ret; 754 + 755 + if (insert_buffer) { 756 + ret = iio_buffer_request_update(indio_dev, insert_buffer); 757 + if (ret) 758 + goto err_free_config; 759 + } 760 + 761 + ret = iio_disable_buffers(indio_dev); 762 + if (ret) 763 + goto err_deactivate_all; 764 + 765 + if (remove_buffer) 766 + iio_buffer_deactivate(remove_buffer); 757 767 if (insert_buffer) 758 - iio_buffer_deactivate(insert_buffer); 759 - indio_dev->active_scan_mask = old_mask; 760 - kfree(compound_mask); 768 + iio_buffer_activate(indio_dev, insert_buffer); 769 + 770 + /* If no buffers in list, we are done */ 771 + if (list_empty(&indio_dev->buffer_list)) 772 + return 0; 773 + 774 + ret = iio_enable_buffers(indio_dev, &new_config); 775 + if (ret) 776 + goto err_deactivate_all; 777 + 778 + return 0; 779 + 780 + err_deactivate_all: 781 + /* 782 + * We've already verified that the config is valid earlier. If things go 783 + * wrong in either enable or disable the most likely reason is an IO 784 + * error from the device. In this case there is no good recovery 785 + * strategy. Just make sure to disable everything and leave the device 786 + * in a sane state. With a bit of luck the device might come back to 787 + * life again later and userspace can try again. 788 + */ 789 + iio_buffer_deactivate_all(indio_dev); 790 + 791 + err_free_config: 792 + iio_free_scan_mask(indio_dev, new_config.scan_mask); 761 793 return ret; 762 794 } 763 795 ··· 852 776 return ret; 853 777 } 854 778 EXPORT_SYMBOL_GPL(iio_update_buffers); 779 + 780 + void iio_disable_all_buffers(struct iio_dev *indio_dev) 781 + { 782 + iio_disable_buffers(indio_dev); 783 + iio_buffer_deactivate_all(indio_dev); 784 + } 855 785 856 786 static ssize_t iio_buffer_store_enable(struct device *dev, 857 787 struct device_attribute *attr,
+5
drivers/iio/industrialio-core.c
··· 101 101 [IIO_MOD_WALKING] = "walking", 102 102 [IIO_MOD_STILL] = "still", 103 103 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", 104 + [IIO_MOD_I] = "i", 105 + [IIO_MOD_Q] = "q", 104 106 }; 105 107 106 108 /* relies on pairs of these shared then separate */ ··· 119 117 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 120 118 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 121 119 = "filter_low_pass_3db_frequency", 120 + [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 121 + = "filter_high_pass_3db_frequency", 122 122 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 123 123 [IIO_CHAN_INFO_FREQUENCY] = "frequency", 124 124 [IIO_CHAN_INFO_PHASE] = "phase", ··· 133 129 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 134 130 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 135 131 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 132 + [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 136 133 }; 137 134 138 135 /**
+2
drivers/iio/industrialio-event.c
··· 211 211 [IIO_EV_INFO_VALUE] = "value", 212 212 [IIO_EV_INFO_HYSTERESIS] = "hysteresis", 213 213 [IIO_EV_INFO_PERIOD] = "period", 214 + [IIO_EV_INFO_HIGH_PASS_FILTER_3DB] = "high_pass_filter_3db", 215 + [IIO_EV_INFO_LOW_PASS_FILTER_3DB] = "low_pass_filter_3db", 214 216 }; 215 217 216 218 static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
+34
drivers/iio/light/Kconfig
··· 5 5 6 6 menu "Light sensors" 7 7 8 + config ACPI_ALS 9 + tristate "ACPI Ambient Light Sensor" 10 + depends on ACPI 11 + select IIO_BUFFER 12 + select IIO_TRIGGERED_BUFFER 13 + select IIO_KFIFO_BUF 14 + help 15 + Say Y here if you want to build a driver for the ACPI0008 16 + Ambient Light Sensor. 17 + 18 + To compile this driver as a module, choose M here: the module will 19 + be called acpi-als. 20 + 8 21 config ADJD_S311 9 22 tristate "ADJD-S311-CR999 digital color sensor" 10 23 select IIO_BUFFER ··· 49 36 50 37 To compile this driver as a module, choose M here: the 51 38 module will be called apds9300. 39 + 40 + config BH1750 41 + tristate "ROHM BH1750 ambient light sensor" 42 + depends on I2C 43 + help 44 + Say Y here to build support for the ROHM BH1710, BH1715, BH1721, 45 + BH1750, BH1751 ambient light sensors. 46 + 47 + To compile this driver as a module, choose M here: the module will 48 + be called bh1750. 52 49 53 50 config CM32181 54 51 depends on I2C ··· 197 174 198 175 This driver can also be built as a module. If so, the module 199 176 will be called ltr501. 177 + 178 + config STK3310 179 + tristate "STK3310 ALS and proximity sensor" 180 + depends on I2C 181 + help 182 + Say yes here to get support for the Sensortek STK3310 ambient light 183 + and proximity sensor. The STK3311 model is also supported by this 184 + driver. 185 + 186 + Choosing M will build the driver as a module. If so, the module 187 + will be called stk3310. 200 188 201 189 config TCS3414 202 190 tristate "TAOS TCS3414 digital color sensor"
+3
drivers/iio/light/Makefile
··· 3 3 # 4 4 5 5 # When adding new entries keep the list in alphabetical order 6 + obj-$(CONFIG_ACPI_ALS) += acpi-als.o 6 7 obj-$(CONFIG_ADJD_S311) += adjd_s311.o 7 8 obj-$(CONFIG_AL3320A) += al3320a.o 8 9 obj-$(CONFIG_APDS9300) += apds9300.o 10 + obj-$(CONFIG_BH1750) += bh1750.o 9 11 obj-$(CONFIG_CM32181) += cm32181.o 10 12 obj-$(CONFIG_CM3232) += cm3232.o 11 13 obj-$(CONFIG_CM3323) += cm3323.o ··· 20 18 obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o 21 19 obj-$(CONFIG_LTR501) += ltr501.o 22 20 obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o 21 + obj-$(CONFIG_STK3310) += stk3310.o 23 22 obj-$(CONFIG_TCS3414) += tcs3414.o 24 23 obj-$(CONFIG_TCS3472) += tcs3472.o 25 24 obj-$(CONFIG_TSL4531) += tsl4531.o
+231
drivers/iio/light/acpi-als.c
··· 1 + /* 2 + * ACPI Ambient Light Sensor Driver 3 + * 4 + * Based on ALS driver: 5 + * Copyright (C) 2009 Zhang Rui <rui.zhang@intel.com> 6 + * 7 + * Rework for IIO subsystem: 8 + * Copyright (C) 2012-2013 Martin Liska <marxin.liska@gmail.com> 9 + * 10 + * Final cleanup and debugging: 11 + * Copyright (C) 2013-2014 Marek Vasut <marex@denx.de> 12 + * Copyright (C) 2015 Gabriele Mazzotta <gabriele.mzt@gmail.com> 13 + * 14 + * This program is free software; you can redistribute it and/or modify it 15 + * under the terms of the GNU General Public License as published by the 16 + * Free Software Foundation; either version 2 of the License, or (at your 17 + * option) any later version. 18 + * 19 + * This program is distributed in the hope that it will be useful, but 20 + * WITHOUT ANY WARRANTY; without even the implied warranty of 21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 + * General Public License for more details. 23 + * 24 + * You should have received a copy of the GNU General Public License along 25 + * with this program; if not, write to the Free Software Foundation, Inc., 26 + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 27 + */ 28 + 29 + #include <linux/module.h> 30 + #include <linux/acpi.h> 31 + #include <linux/err.h> 32 + #include <linux/mutex.h> 33 + 34 + #include <linux/iio/iio.h> 35 + #include <linux/iio/buffer.h> 36 + #include <linux/iio/kfifo_buf.h> 37 + 38 + #define ACPI_ALS_CLASS "als" 39 + #define ACPI_ALS_DEVICE_NAME "acpi-als" 40 + #define ACPI_ALS_NOTIFY_ILLUMINANCE 0x80 41 + 42 + ACPI_MODULE_NAME("acpi-als"); 43 + 44 + /* 45 + * So far, there's only one channel in here, but the specification for 46 + * ACPI0008 says there can be more to what the block can report. Like 47 + * chromaticity and such. We are ready for incoming additions! 48 + */ 49 + static const struct iio_chan_spec acpi_als_channels[] = { 50 + { 51 + .type = IIO_LIGHT, 52 + .scan_type = { 53 + .sign = 's', 54 + .realbits = 32, 55 + .storagebits = 32, 56 + }, 57 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 58 + }, 59 + }; 60 + 61 + /* 62 + * The event buffer contains timestamp and all the data from 63 + * the ACPI0008 block. There are multiple, but so far we only 64 + * support _ALI (illuminance). Once someone adds new channels 65 + * to acpi_als_channels[], the evt_buffer below will grow 66 + * automatically. 67 + */ 68 + #define EVT_NR_SOURCES ARRAY_SIZE(acpi_als_channels) 69 + #define EVT_BUFFER_SIZE \ 70 + (sizeof(s64) + (EVT_NR_SOURCES * sizeof(s32))) 71 + 72 + struct acpi_als { 73 + struct acpi_device *device; 74 + struct mutex lock; 75 + 76 + s32 evt_buffer[EVT_BUFFER_SIZE]; 77 + }; 78 + 79 + /* 80 + * All types of properties the ACPI0008 block can report. The ALI, ALC, ALT 81 + * and ALP can all be handled by als_read_value() below, while the ALR is 82 + * special. 83 + * 84 + * The _ALR property returns tables that can be used to fine-tune the values 85 + * reported by the other props based on the particular hardware type and it's 86 + * location (it contains tables for "rainy", "bright inhouse lighting" etc.). 87 + * 88 + * So far, we support only ALI (illuminance). 89 + */ 90 + #define ACPI_ALS_ILLUMINANCE "_ALI" 91 + #define ACPI_ALS_CHROMATICITY "_ALC" 92 + #define ACPI_ALS_COLOR_TEMP "_ALT" 93 + #define ACPI_ALS_POLLING "_ALP" 94 + #define ACPI_ALS_TABLES "_ALR" 95 + 96 + static int als_read_value(struct acpi_als *als, char *prop, s32 *val) 97 + { 98 + unsigned long long temp_val; 99 + acpi_status status; 100 + 101 + status = acpi_evaluate_integer(als->device->handle, prop, NULL, 102 + &temp_val); 103 + 104 + if (ACPI_FAILURE(status)) { 105 + ACPI_EXCEPTION((AE_INFO, status, "Error reading ALS %s", prop)); 106 + return -EIO; 107 + } 108 + 109 + *val = temp_val; 110 + 111 + return 0; 112 + } 113 + 114 + static void acpi_als_notify(struct acpi_device *device, u32 event) 115 + { 116 + struct iio_dev *indio_dev = acpi_driver_data(device); 117 + struct acpi_als *als = iio_priv(indio_dev); 118 + s32 *buffer = als->evt_buffer; 119 + s64 time_ns = iio_get_time_ns(); 120 + s32 val; 121 + int ret; 122 + 123 + mutex_lock(&als->lock); 124 + 125 + memset(buffer, 0, EVT_BUFFER_SIZE); 126 + 127 + switch (event) { 128 + case ACPI_ALS_NOTIFY_ILLUMINANCE: 129 + ret = als_read_value(als, ACPI_ALS_ILLUMINANCE, &val); 130 + if (ret < 0) 131 + goto out; 132 + *buffer++ = val; 133 + break; 134 + default: 135 + /* Unhandled event */ 136 + dev_dbg(&device->dev, "Unhandled ACPI ALS event (%08x)!\n", 137 + event); 138 + goto out; 139 + } 140 + 141 + iio_push_to_buffers_with_timestamp(indio_dev, als->evt_buffer, time_ns); 142 + 143 + out: 144 + mutex_unlock(&als->lock); 145 + } 146 + 147 + static int acpi_als_read_raw(struct iio_dev *indio_dev, 148 + struct iio_chan_spec const *chan, int *val, 149 + int *val2, long mask) 150 + { 151 + struct acpi_als *als = iio_priv(indio_dev); 152 + s32 temp_val; 153 + int ret; 154 + 155 + if (mask != IIO_CHAN_INFO_RAW) 156 + return -EINVAL; 157 + 158 + /* we support only illumination (_ALI) so far. */ 159 + if (chan->type != IIO_LIGHT) 160 + return -EINVAL; 161 + 162 + ret = als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val); 163 + if (ret < 0) 164 + return ret; 165 + 166 + *val = temp_val; 167 + 168 + return IIO_VAL_INT; 169 + } 170 + 171 + static const struct iio_info acpi_als_info = { 172 + .driver_module = THIS_MODULE, 173 + .read_raw = acpi_als_read_raw, 174 + }; 175 + 176 + static int acpi_als_add(struct acpi_device *device) 177 + { 178 + struct acpi_als *als; 179 + struct iio_dev *indio_dev; 180 + struct iio_buffer *buffer; 181 + 182 + indio_dev = devm_iio_device_alloc(&device->dev, sizeof(*als)); 183 + if (!indio_dev) 184 + return -ENOMEM; 185 + 186 + als = iio_priv(indio_dev); 187 + 188 + device->driver_data = indio_dev; 189 + als->device = device; 190 + mutex_init(&als->lock); 191 + 192 + indio_dev->name = ACPI_ALS_DEVICE_NAME; 193 + indio_dev->dev.parent = &device->dev; 194 + indio_dev->info = &acpi_als_info; 195 + indio_dev->modes = INDIO_BUFFER_SOFTWARE; 196 + indio_dev->channels = acpi_als_channels; 197 + indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels); 198 + 199 + buffer = devm_iio_kfifo_allocate(&device->dev); 200 + if (!buffer) 201 + return -ENOMEM; 202 + 203 + iio_device_attach_buffer(indio_dev, buffer); 204 + 205 + return devm_iio_device_register(&device->dev, indio_dev); 206 + } 207 + 208 + static const struct acpi_device_id acpi_als_device_ids[] = { 209 + {"ACPI0008", 0}, 210 + {}, 211 + }; 212 + 213 + MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids); 214 + 215 + static struct acpi_driver acpi_als_driver = { 216 + .name = "acpi_als", 217 + .class = ACPI_ALS_CLASS, 218 + .ids = acpi_als_device_ids, 219 + .ops = { 220 + .add = acpi_als_add, 221 + .notify = acpi_als_notify, 222 + }, 223 + }; 224 + 225 + module_acpi_driver(acpi_als_driver); 226 + 227 + MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>"); 228 + MODULE_AUTHOR("Martin Liska <marxin.liska@gmail.com>"); 229 + MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); 230 + MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver"); 231 + MODULE_LICENSE("GPL");
+334
drivers/iio/light/bh1750.c
··· 1 + /* 2 + * ROHM BH1710/BH1715/BH1721/BH1750/BH1751 ambient light sensor driver 3 + * 4 + * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com> 5 + * 6 + * This program is free software; you can redistribute it and/or modify 7 + * it under the terms of the GNU General Public License version 2 as 8 + * published by the Free Software Foundation. 9 + * 10 + * Data sheets: 11 + * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1710fvc-e.pdf 12 + * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1715fvc-e.pdf 13 + * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1721fvc-e.pdf 14 + * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf 15 + * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1751fvi-e.pdf 16 + * 17 + * 7-bit I2C slave addresses: 18 + * 0x23 (ADDR pin low) 19 + * 0x5C (ADDR pin high) 20 + * 21 + */ 22 + 23 + #include <linux/delay.h> 24 + #include <linux/i2c.h> 25 + #include <linux/iio/iio.h> 26 + #include <linux/iio/sysfs.h> 27 + #include <linux/module.h> 28 + 29 + #define BH1750_POWER_DOWN 0x00 30 + #define BH1750_ONE_TIME_H_RES_MODE 0x20 /* auto-mode for BH1721 */ 31 + #define BH1750_CHANGE_INT_TIME_H_BIT 0x40 32 + #define BH1750_CHANGE_INT_TIME_L_BIT 0x60 33 + 34 + enum { 35 + BH1710, 36 + BH1721, 37 + BH1750, 38 + }; 39 + 40 + struct bh1750_chip_info; 41 + struct bh1750_data { 42 + struct i2c_client *client; 43 + struct mutex lock; 44 + const struct bh1750_chip_info *chip_info; 45 + u16 mtreg; 46 + }; 47 + 48 + struct bh1750_chip_info { 49 + u16 mtreg_min; 50 + u16 mtreg_max; 51 + u16 mtreg_default; 52 + int mtreg_to_usec; 53 + int mtreg_to_scale; 54 + 55 + /* 56 + * For BH1710/BH1721 all possible integration time values won't fit 57 + * into one page so displaying is limited to every second one. 58 + * Note, that user can still write proper values which were not 59 + * listed. 60 + */ 61 + int inc; 62 + 63 + u16 int_time_low_mask; 64 + u16 int_time_high_mask; 65 + } 66 + 67 + static const bh1750_chip_info_tbl[] = { 68 + [BH1710] = { 140, 1022, 300, 400, 250000000, 2, 0x001F, 0x03E0 }, 69 + [BH1721] = { 140, 1020, 300, 400, 250000000, 2, 0x0010, 0x03E0 }, 70 + [BH1750] = { 31, 254, 69, 1740, 57500000, 1, 0x001F, 0x00E0 }, 71 + }; 72 + 73 + static int bh1750_change_int_time(struct bh1750_data *data, int usec) 74 + { 75 + int ret; 76 + u16 val; 77 + u8 regval; 78 + const struct bh1750_chip_info *chip_info = data->chip_info; 79 + 80 + if ((usec % chip_info->mtreg_to_usec) != 0) 81 + return -EINVAL; 82 + 83 + val = usec / chip_info->mtreg_to_usec; 84 + if (val < chip_info->mtreg_min || val > chip_info->mtreg_max) 85 + return -EINVAL; 86 + 87 + ret = i2c_smbus_write_byte(data->client, BH1750_POWER_DOWN); 88 + if (ret < 0) 89 + return ret; 90 + 91 + regval = (val & chip_info->int_time_high_mask) >> 5; 92 + ret = i2c_smbus_write_byte(data->client, 93 + BH1750_CHANGE_INT_TIME_H_BIT | regval); 94 + if (ret < 0) 95 + return ret; 96 + 97 + regval = val & chip_info->int_time_low_mask; 98 + ret = i2c_smbus_write_byte(data->client, 99 + BH1750_CHANGE_INT_TIME_L_BIT | regval); 100 + if (ret < 0) 101 + return ret; 102 + 103 + data->mtreg = val; 104 + 105 + return 0; 106 + } 107 + 108 + static int bh1750_read(struct bh1750_data *data, int *val) 109 + { 110 + int ret; 111 + __be16 result; 112 + const struct bh1750_chip_info *chip_info = data->chip_info; 113 + unsigned long delay = chip_info->mtreg_to_usec * data->mtreg; 114 + 115 + /* 116 + * BH1721 will enter continuous mode on receiving this command. 117 + * Note, that this eliminates need for bh1750_resume(). 118 + */ 119 + ret = i2c_smbus_write_byte(data->client, BH1750_ONE_TIME_H_RES_MODE); 120 + if (ret < 0) 121 + return ret; 122 + 123 + usleep_range(delay + 15000, delay + 40000); 124 + 125 + ret = i2c_master_recv(data->client, (char *)&result, 2); 126 + if (ret < 0) 127 + return ret; 128 + 129 + *val = be16_to_cpu(result); 130 + 131 + return 0; 132 + } 133 + 134 + static int bh1750_read_raw(struct iio_dev *indio_dev, 135 + struct iio_chan_spec const *chan, 136 + int *val, int *val2, long mask) 137 + { 138 + int ret, tmp; 139 + struct bh1750_data *data = iio_priv(indio_dev); 140 + const struct bh1750_chip_info *chip_info = data->chip_info; 141 + 142 + switch (mask) { 143 + case IIO_CHAN_INFO_RAW: 144 + switch (chan->type) { 145 + case IIO_LIGHT: 146 + mutex_lock(&data->lock); 147 + ret = bh1750_read(data, val); 148 + mutex_unlock(&data->lock); 149 + if (ret < 0) 150 + return ret; 151 + 152 + return IIO_VAL_INT; 153 + default: 154 + return -EINVAL; 155 + } 156 + case IIO_CHAN_INFO_SCALE: 157 + tmp = chip_info->mtreg_to_scale / data->mtreg; 158 + *val = tmp / 1000000; 159 + *val2 = tmp % 1000000; 160 + return IIO_VAL_INT_PLUS_MICRO; 161 + case IIO_CHAN_INFO_INT_TIME: 162 + *val = 0; 163 + *val2 = chip_info->mtreg_to_usec * data->mtreg; 164 + return IIO_VAL_INT_PLUS_MICRO; 165 + default: 166 + return -EINVAL; 167 + } 168 + } 169 + 170 + static int bh1750_write_raw(struct iio_dev *indio_dev, 171 + struct iio_chan_spec const *chan, 172 + int val, int val2, long mask) 173 + { 174 + int ret; 175 + struct bh1750_data *data = iio_priv(indio_dev); 176 + 177 + switch (mask) { 178 + case IIO_CHAN_INFO_INT_TIME: 179 + if (val != 0) 180 + return -EINVAL; 181 + 182 + mutex_lock(&data->lock); 183 + ret = bh1750_change_int_time(data, val2); 184 + mutex_unlock(&data->lock); 185 + return ret; 186 + default: 187 + return -EINVAL; 188 + } 189 + } 190 + 191 + static ssize_t bh1750_show_int_time_available(struct device *dev, 192 + struct device_attribute *attr, char *buf) 193 + { 194 + int i; 195 + size_t len = 0; 196 + struct bh1750_data *data = iio_priv(dev_to_iio_dev(dev)); 197 + const struct bh1750_chip_info *chip_info = data->chip_info; 198 + 199 + for (i = chip_info->mtreg_min; i <= chip_info->mtreg_max; i += chip_info->inc) 200 + len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06d ", 201 + chip_info->mtreg_to_usec * i); 202 + 203 + buf[len - 1] = '\n'; 204 + 205 + return len; 206 + } 207 + 208 + static IIO_DEV_ATTR_INT_TIME_AVAIL(bh1750_show_int_time_available); 209 + 210 + static struct attribute *bh1750_attributes[] = { 211 + &iio_dev_attr_integration_time_available.dev_attr.attr, 212 + NULL, 213 + }; 214 + 215 + static struct attribute_group bh1750_attribute_group = { 216 + .attrs = bh1750_attributes, 217 + }; 218 + 219 + static const struct iio_info bh1750_info = { 220 + .driver_module = THIS_MODULE, 221 + .attrs = &bh1750_attribute_group, 222 + .read_raw = bh1750_read_raw, 223 + .write_raw = bh1750_write_raw, 224 + }; 225 + 226 + static const struct iio_chan_spec bh1750_channels[] = { 227 + { 228 + .type = IIO_LIGHT, 229 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 230 + BIT(IIO_CHAN_INFO_SCALE) | 231 + BIT(IIO_CHAN_INFO_INT_TIME) 232 + } 233 + }; 234 + 235 + static int bh1750_probe(struct i2c_client *client, 236 + const struct i2c_device_id *id) 237 + { 238 + int ret, usec; 239 + struct bh1750_data *data; 240 + struct iio_dev *indio_dev; 241 + 242 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C | 243 + I2C_FUNC_SMBUS_WRITE_BYTE)) 244 + return -ENODEV; 245 + 246 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 247 + if (!indio_dev) 248 + return -ENOMEM; 249 + 250 + data = iio_priv(indio_dev); 251 + i2c_set_clientdata(client, indio_dev); 252 + data->client = client; 253 + data->chip_info = &bh1750_chip_info_tbl[id->driver_data]; 254 + 255 + usec = data->chip_info->mtreg_to_usec * data->chip_info->mtreg_default; 256 + ret = bh1750_change_int_time(data, usec); 257 + if (ret < 0) 258 + return ret; 259 + 260 + mutex_init(&data->lock); 261 + indio_dev->dev.parent = &client->dev; 262 + indio_dev->info = &bh1750_info; 263 + indio_dev->name = id->name; 264 + indio_dev->channels = bh1750_channels; 265 + indio_dev->num_channels = ARRAY_SIZE(bh1750_channels); 266 + indio_dev->modes = INDIO_DIRECT_MODE; 267 + 268 + return iio_device_register(indio_dev); 269 + } 270 + 271 + static int bh1750_remove(struct i2c_client *client) 272 + { 273 + struct iio_dev *indio_dev = i2c_get_clientdata(client); 274 + struct bh1750_data *data = iio_priv(indio_dev); 275 + 276 + iio_device_unregister(indio_dev); 277 + 278 + mutex_lock(&data->lock); 279 + i2c_smbus_write_byte(client, BH1750_POWER_DOWN); 280 + mutex_unlock(&data->lock); 281 + 282 + return 0; 283 + } 284 + 285 + #ifdef CONFIG_PM_SLEEP 286 + static int bh1750_suspend(struct device *dev) 287 + { 288 + int ret; 289 + struct bh1750_data *data = 290 + iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 291 + 292 + /* 293 + * This is mainly for BH1721 which doesn't enter power down 294 + * mode automatically. 295 + */ 296 + mutex_lock(&data->lock); 297 + ret = i2c_smbus_write_byte(data->client, BH1750_POWER_DOWN); 298 + mutex_unlock(&data->lock); 299 + 300 + return ret; 301 + } 302 + 303 + static SIMPLE_DEV_PM_OPS(bh1750_pm_ops, bh1750_suspend, NULL); 304 + #define BH1750_PM_OPS (&bh1750_pm_ops) 305 + #else 306 + #define BH1750_PM_OPS NULL 307 + #endif 308 + 309 + static const struct i2c_device_id bh1750_id[] = { 310 + { "bh1710", BH1710 }, 311 + { "bh1715", BH1750 }, 312 + { "bh1721", BH1721 }, 313 + { "bh1750", BH1750 }, 314 + { "bh1751", BH1750 }, 315 + { } 316 + }; 317 + MODULE_DEVICE_TABLE(i2c, bh1750_id); 318 + 319 + static struct i2c_driver bh1750_driver = { 320 + .driver = { 321 + .name = "bh1750", 322 + .owner = THIS_MODULE, 323 + .pm = BH1750_PM_OPS, 324 + }, 325 + .probe = bh1750_probe, 326 + .remove = bh1750_remove, 327 + .id_table = bh1750_id, 328 + 329 + }; 330 + module_i2c_driver(bh1750_driver); 331 + 332 + MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>"); 333 + MODULE_DESCRIPTION("ROHM BH1710/BH1715/BH1721/BH1750/BH1751 als driver"); 334 + MODULE_LICENSE("GPL v2");
+7 -7
drivers/iio/light/hid-sensor-als.c
··· 263 263 struct iio_dev *indio_dev; 264 264 struct als_state *als_state; 265 265 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 266 - struct iio_chan_spec *channels; 267 266 268 267 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct als_state)); 269 268 if (!indio_dev) ··· 280 281 return ret; 281 282 } 282 283 283 - channels = kmemdup(als_channels, sizeof(als_channels), GFP_KERNEL); 284 - if (!channels) { 284 + indio_dev->channels = kmemdup(als_channels, 285 + sizeof(als_channels), GFP_KERNEL); 286 + if (!indio_dev->channels) { 285 287 dev_err(&pdev->dev, "failed to duplicate channels\n"); 286 288 return -ENOMEM; 287 289 } 288 290 289 - ret = als_parse_report(pdev, hsdev, channels, 290 - HID_USAGE_SENSOR_ALS, als_state); 291 + ret = als_parse_report(pdev, hsdev, 292 + (struct iio_chan_spec *)indio_dev->channels, 293 + HID_USAGE_SENSOR_ALS, als_state); 291 294 if (ret) { 292 295 dev_err(&pdev->dev, "failed to setup attributes\n"); 293 296 goto error_free_dev_mem; 294 297 } 295 298 296 - indio_dev->channels = channels; 297 299 indio_dev->num_channels = 298 300 ARRAY_SIZE(als_channels); 299 301 indio_dev->dev.parent = &pdev->dev; ··· 361 361 return 0; 362 362 } 363 363 364 - static struct platform_device_id hid_als_ids[] = { 364 + static const struct platform_device_id hid_als_ids[] = { 365 365 { 366 366 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 367 367 .name = "HID-SENSOR-200041",
+1 -1
drivers/iio/light/hid-sensor-prox.c
··· 350 350 return 0; 351 351 } 352 352 353 - static struct platform_device_id hid_prox_ids[] = { 353 + static const struct platform_device_id hid_prox_ids[] = { 354 354 { 355 355 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 356 356 .name = "HID-SENSOR-200011",
+52 -1
drivers/iio/light/ltr501.c
··· 66 66 67 67 #define LTR501_REGMAP_NAME "ltr501_regmap" 68 68 69 + #define LTR501_LUX_CONV(vis_coeff, vis_data, ir_coeff, ir_data) \ 70 + ((vis_coeff * vis_data) - (ir_coeff * ir_data)) 71 + 69 72 static const int int_time_mapping[] = {100000, 50000, 200000, 400000}; 70 73 71 74 static const struct reg_field reg_field_it = ··· 299 296 *val = ltr501_ps_samp_table[i].time_val; 300 297 301 298 return IIO_VAL_INT; 299 + } 300 + 301 + /* IR and visible spectrum coeff's are given in data sheet */ 302 + static unsigned long ltr501_calculate_lux(u16 vis_data, u16 ir_data) 303 + { 304 + unsigned long ratio, lux; 305 + 306 + if (vis_data == 0) 307 + return 0; 308 + 309 + /* multiply numerator by 100 to avoid handling ratio < 1 */ 310 + ratio = DIV_ROUND_UP(ir_data * 100, ir_data + vis_data); 311 + 312 + if (ratio < 45) 313 + lux = LTR501_LUX_CONV(1774, vis_data, -1105, ir_data); 314 + else if (ratio >= 45 && ratio < 64) 315 + lux = LTR501_LUX_CONV(3772, vis_data, 1336, ir_data); 316 + else if (ratio >= 64 && ratio < 85) 317 + lux = LTR501_LUX_CONV(1690, vis_data, 169, ir_data); 318 + else 319 + lux = 0; 320 + 321 + return lux / 1000; 302 322 } 303 323 304 324 static int ltr501_drdy(struct ltr501_data *data, u8 drdy_mask) ··· 574 548 .num_event_specs = _evsize,\ 575 549 } 576 550 551 + #define LTR501_LIGHT_CHANNEL() { \ 552 + .type = IIO_LIGHT, \ 553 + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \ 554 + .scan_index = -1, \ 555 + } 556 + 577 557 static const struct iio_chan_spec ltr501_channels[] = { 558 + LTR501_LIGHT_CHANNEL(), 578 559 LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0, 579 560 ltr501_als_event_spec, 580 561 ARRAY_SIZE(ltr501_als_event_spec)), ··· 609 576 }; 610 577 611 578 static const struct iio_chan_spec ltr301_channels[] = { 579 + LTR501_LIGHT_CHANNEL(), 612 580 LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0, 613 581 ltr501_als_event_spec, 614 582 ARRAY_SIZE(ltr501_als_event_spec)), ··· 630 596 int ret, i; 631 597 632 598 switch (mask) { 599 + case IIO_CHAN_INFO_PROCESSED: 600 + if (iio_buffer_enabled(indio_dev)) 601 + return -EBUSY; 602 + 603 + switch (chan->type) { 604 + case IIO_LIGHT: 605 + mutex_lock(&data->lock_als); 606 + ret = ltr501_read_als(data, buf); 607 + mutex_unlock(&data->lock_als); 608 + if (ret < 0) 609 + return ret; 610 + *val = ltr501_calculate_lux(le16_to_cpu(buf[1]), 611 + le16_to_cpu(buf[0])); 612 + return IIO_VAL_INT; 613 + default: 614 + return -EINVAL; 615 + } 633 616 case IIO_CHAN_INFO_RAW: 634 617 if (iio_buffer_enabled(indio_dev)) 635 618 return -EBUSY; ··· 916 865 return -EINVAL; 917 866 } 918 867 case IIO_PROXIMITY: 919 - switch (dir) { 920 868 if (val > LTR501_PS_THRESH_MASK) 921 869 return -EINVAL; 870 + switch (dir) { 922 871 case IIO_EV_DIR_RISING: 923 872 mutex_lock(&data->lock_ps); 924 873 ret = regmap_bulk_write(data->regmap,
+722
drivers/iio/light/stk3310.c
··· 1 + /** 2 + * Sensortek STK3310/STK3311 Ambient Light and Proximity Sensor 3 + * 4 + * Copyright (c) 2015, Intel Corporation. 5 + * 6 + * This file is subject to the terms and conditions of version 2 of 7 + * the GNU General Public License. See the file COPYING in the main 8 + * directory of this archive for more details. 9 + * 10 + * IIO driver for STK3310/STK3311. 7-bit I2C address: 0x48. 11 + */ 12 + 13 + #include <linux/acpi.h> 14 + #include <linux/i2c.h> 15 + #include <linux/interrupt.h> 16 + #include <linux/kernel.h> 17 + #include <linux/module.h> 18 + #include <linux/regmap.h> 19 + #include <linux/gpio/consumer.h> 20 + #include <linux/iio/events.h> 21 + #include <linux/iio/iio.h> 22 + #include <linux/iio/sysfs.h> 23 + 24 + #define STK3310_REG_STATE 0x00 25 + #define STK3310_REG_PSCTRL 0x01 26 + #define STK3310_REG_ALSCTRL 0x02 27 + #define STK3310_REG_INT 0x04 28 + #define STK3310_REG_THDH_PS 0x06 29 + #define STK3310_REG_THDL_PS 0x08 30 + #define STK3310_REG_FLAG 0x10 31 + #define STK3310_REG_PS_DATA_MSB 0x11 32 + #define STK3310_REG_PS_DATA_LSB 0x12 33 + #define STK3310_REG_ALS_DATA_MSB 0x13 34 + #define STK3310_REG_ALS_DATA_LSB 0x14 35 + #define STK3310_REG_ID 0x3E 36 + #define STK3310_MAX_REG 0x80 37 + 38 + #define STK3310_STATE_EN_PS 0x01 39 + #define STK3310_STATE_EN_ALS 0x02 40 + #define STK3310_STATE_STANDBY 0x00 41 + 42 + #define STK3310_CHIP_ID_VAL 0x13 43 + #define STK3311_CHIP_ID_VAL 0x1D 44 + #define STK3310_PSINT_EN 0x01 45 + #define STK3310_PS_MAX_VAL 0xFFFF 46 + #define STK3310_THRESH_MAX 0xFFFF 47 + 48 + #define STK3310_DRIVER_NAME "stk3310" 49 + #define STK3310_REGMAP_NAME "stk3310_regmap" 50 + #define STK3310_EVENT "stk3310_event" 51 + #define STK3310_GPIO "stk3310_gpio" 52 + 53 + #define STK3310_SCALE_AVAILABLE "6.4 1.6 0.4 0.1" 54 + 55 + #define STK3310_IT_AVAILABLE \ 56 + "0.000185 0.000370 0.000741 0.001480 0.002960 0.005920 0.011840 " \ 57 + "0.023680 0.047360 0.094720 0.189440 0.378880 0.757760 1.515520 " \ 58 + "3.031040 6.062080" 59 + 60 + #define STK3310_REGFIELD(name) \ 61 + do { \ 62 + data->reg_##name = \ 63 + devm_regmap_field_alloc(&client->dev, regmap, \ 64 + stk3310_reg_field_##name); \ 65 + if (IS_ERR(data->reg_##name)) { \ 66 + dev_err(&client->dev, "reg field alloc failed.\n"); \ 67 + return PTR_ERR(data->reg_##name); \ 68 + } \ 69 + } while (0) 70 + 71 + static const struct reg_field stk3310_reg_field_state = 72 + REG_FIELD(STK3310_REG_STATE, 0, 2); 73 + static const struct reg_field stk3310_reg_field_als_gain = 74 + REG_FIELD(STK3310_REG_ALSCTRL, 4, 5); 75 + static const struct reg_field stk3310_reg_field_ps_gain = 76 + REG_FIELD(STK3310_REG_PSCTRL, 4, 5); 77 + static const struct reg_field stk3310_reg_field_als_it = 78 + REG_FIELD(STK3310_REG_ALSCTRL, 0, 3); 79 + static const struct reg_field stk3310_reg_field_ps_it = 80 + REG_FIELD(STK3310_REG_PSCTRL, 0, 3); 81 + static const struct reg_field stk3310_reg_field_int_ps = 82 + REG_FIELD(STK3310_REG_INT, 0, 2); 83 + static const struct reg_field stk3310_reg_field_flag_psint = 84 + REG_FIELD(STK3310_REG_FLAG, 4, 4); 85 + static const struct reg_field stk3310_reg_field_flag_nf = 86 + REG_FIELD(STK3310_REG_FLAG, 0, 0); 87 + /* 88 + * Maximum PS values with regard to scale. Used to export the 'inverse' 89 + * PS value (high values for far objects, low values for near objects). 90 + */ 91 + static const int stk3310_ps_max[4] = { 92 + STK3310_PS_MAX_VAL / 64, 93 + STK3310_PS_MAX_VAL / 16, 94 + STK3310_PS_MAX_VAL / 4, 95 + STK3310_PS_MAX_VAL, 96 + }; 97 + 98 + static const int stk3310_scale_table[][2] = { 99 + {6, 400000}, {1, 600000}, {0, 400000}, {0, 100000} 100 + }; 101 + 102 + /* Integration time in seconds, microseconds */ 103 + static const int stk3310_it_table[][2] = { 104 + {0, 185}, {0, 370}, {0, 741}, {0, 1480}, 105 + {0, 2960}, {0, 5920}, {0, 11840}, {0, 23680}, 106 + {0, 47360}, {0, 94720}, {0, 189440}, {0, 378880}, 107 + {0, 757760}, {1, 515520}, {3, 31040}, {6, 62080}, 108 + }; 109 + 110 + struct stk3310_data { 111 + struct i2c_client *client; 112 + struct mutex lock; 113 + bool als_enabled; 114 + bool ps_enabled; 115 + u64 timestamp; 116 + struct regmap *regmap; 117 + struct regmap_field *reg_state; 118 + struct regmap_field *reg_als_gain; 119 + struct regmap_field *reg_ps_gain; 120 + struct regmap_field *reg_als_it; 121 + struct regmap_field *reg_ps_it; 122 + struct regmap_field *reg_int_ps; 123 + struct regmap_field *reg_flag_psint; 124 + struct regmap_field *reg_flag_nf; 125 + }; 126 + 127 + static const struct iio_event_spec stk3310_events[] = { 128 + /* Proximity event */ 129 + { 130 + .type = IIO_EV_TYPE_THRESH, 131 + .dir = IIO_EV_DIR_FALLING, 132 + .mask_separate = BIT(IIO_EV_INFO_VALUE) | 133 + BIT(IIO_EV_INFO_ENABLE), 134 + }, 135 + /* Out-of-proximity event */ 136 + { 137 + .type = IIO_EV_TYPE_THRESH, 138 + .dir = IIO_EV_DIR_RISING, 139 + .mask_separate = BIT(IIO_EV_INFO_VALUE) | 140 + BIT(IIO_EV_INFO_ENABLE), 141 + }, 142 + }; 143 + 144 + static const struct iio_chan_spec stk3310_channels[] = { 145 + { 146 + .type = IIO_LIGHT, 147 + .info_mask_separate = 148 + BIT(IIO_CHAN_INFO_RAW) | 149 + BIT(IIO_CHAN_INFO_SCALE) | 150 + BIT(IIO_CHAN_INFO_INT_TIME), 151 + }, 152 + { 153 + .type = IIO_PROXIMITY, 154 + .info_mask_separate = 155 + BIT(IIO_CHAN_INFO_RAW) | 156 + BIT(IIO_CHAN_INFO_SCALE) | 157 + BIT(IIO_CHAN_INFO_INT_TIME), 158 + .event_spec = stk3310_events, 159 + .num_event_specs = ARRAY_SIZE(stk3310_events), 160 + } 161 + }; 162 + 163 + static IIO_CONST_ATTR(in_illuminance_scale_available, STK3310_SCALE_AVAILABLE); 164 + 165 + static IIO_CONST_ATTR(in_proximity_scale_available, STK3310_SCALE_AVAILABLE); 166 + 167 + static IIO_CONST_ATTR(in_illuminance_integration_time_available, 168 + STK3310_IT_AVAILABLE); 169 + 170 + static IIO_CONST_ATTR(in_proximity_integration_time_available, 171 + STK3310_IT_AVAILABLE); 172 + 173 + static struct attribute *stk3310_attributes[] = { 174 + &iio_const_attr_in_illuminance_scale_available.dev_attr.attr, 175 + &iio_const_attr_in_proximity_scale_available.dev_attr.attr, 176 + &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr, 177 + &iio_const_attr_in_proximity_integration_time_available.dev_attr.attr, 178 + NULL, 179 + }; 180 + 181 + static const struct attribute_group stk3310_attribute_group = { 182 + .attrs = stk3310_attributes 183 + }; 184 + 185 + static int stk3310_get_index(const int table[][2], int table_size, 186 + int val, int val2) 187 + { 188 + int i; 189 + 190 + for (i = 0; i < table_size; i++) { 191 + if (val == table[i][0] && val2 == table[i][1]) 192 + return i; 193 + } 194 + 195 + return -EINVAL; 196 + } 197 + 198 + static int stk3310_read_event(struct iio_dev *indio_dev, 199 + const struct iio_chan_spec *chan, 200 + enum iio_event_type type, 201 + enum iio_event_direction dir, 202 + enum iio_event_info info, 203 + int *val, int *val2) 204 + { 205 + u8 reg; 206 + u16 buf; 207 + int ret; 208 + unsigned int index; 209 + struct stk3310_data *data = iio_priv(indio_dev); 210 + 211 + if (info != IIO_EV_INFO_VALUE) 212 + return -EINVAL; 213 + 214 + /* 215 + * Only proximity interrupts are implemented at the moment. 216 + * Since we're inverting proximity values, the sensor's 'high' 217 + * threshold will become our 'low' threshold, associated with 218 + * 'near' events. Similarly, the sensor's 'low' threshold will 219 + * be our 'high' threshold, associated with 'far' events. 220 + */ 221 + if (dir == IIO_EV_DIR_RISING) 222 + reg = STK3310_REG_THDL_PS; 223 + else if (dir == IIO_EV_DIR_FALLING) 224 + reg = STK3310_REG_THDH_PS; 225 + else 226 + return -EINVAL; 227 + 228 + mutex_lock(&data->lock); 229 + ret = regmap_bulk_read(data->regmap, reg, &buf, 2); 230 + mutex_unlock(&data->lock); 231 + if (ret < 0) { 232 + dev_err(&data->client->dev, "register read failed\n"); 233 + return ret; 234 + } 235 + regmap_field_read(data->reg_ps_gain, &index); 236 + *val = swab16(stk3310_ps_max[index] - buf); 237 + 238 + return IIO_VAL_INT; 239 + } 240 + 241 + static int stk3310_write_event(struct iio_dev *indio_dev, 242 + const struct iio_chan_spec *chan, 243 + enum iio_event_type type, 244 + enum iio_event_direction dir, 245 + enum iio_event_info info, 246 + int val, int val2) 247 + { 248 + u8 reg; 249 + u16 buf; 250 + int ret; 251 + unsigned int index; 252 + struct stk3310_data *data = iio_priv(indio_dev); 253 + struct i2c_client *client = data->client; 254 + 255 + regmap_field_read(data->reg_ps_gain, &index); 256 + if (val > stk3310_ps_max[index]) 257 + return -EINVAL; 258 + 259 + if (dir == IIO_EV_DIR_RISING) 260 + reg = STK3310_REG_THDL_PS; 261 + else if (dir == IIO_EV_DIR_FALLING) 262 + reg = STK3310_REG_THDH_PS; 263 + else 264 + return -EINVAL; 265 + 266 + buf = swab16(stk3310_ps_max[index] - val); 267 + ret = regmap_bulk_write(data->regmap, reg, &buf, 2); 268 + if (ret < 0) 269 + dev_err(&client->dev, "failed to set PS threshold!\n"); 270 + 271 + return ret; 272 + } 273 + 274 + static int stk3310_read_event_config(struct iio_dev *indio_dev, 275 + const struct iio_chan_spec *chan, 276 + enum iio_event_type type, 277 + enum iio_event_direction dir) 278 + { 279 + unsigned int event_val; 280 + struct stk3310_data *data = iio_priv(indio_dev); 281 + 282 + regmap_field_read(data->reg_int_ps, &event_val); 283 + 284 + return event_val; 285 + } 286 + 287 + static int stk3310_write_event_config(struct iio_dev *indio_dev, 288 + const struct iio_chan_spec *chan, 289 + enum iio_event_type type, 290 + enum iio_event_direction dir, 291 + int state) 292 + { 293 + int ret; 294 + struct stk3310_data *data = iio_priv(indio_dev); 295 + struct i2c_client *client = data->client; 296 + 297 + if (state < 0 || state > 7) 298 + return -EINVAL; 299 + 300 + /* Set INT_PS value */ 301 + mutex_lock(&data->lock); 302 + ret = regmap_field_write(data->reg_int_ps, state); 303 + if (ret < 0) 304 + dev_err(&client->dev, "failed to set interrupt mode\n"); 305 + mutex_unlock(&data->lock); 306 + 307 + return ret; 308 + } 309 + 310 + static int stk3310_read_raw(struct iio_dev *indio_dev, 311 + struct iio_chan_spec const *chan, 312 + int *val, int *val2, long mask) 313 + { 314 + u8 reg; 315 + u16 buf; 316 + int ret; 317 + unsigned int index; 318 + struct stk3310_data *data = iio_priv(indio_dev); 319 + struct i2c_client *client = data->client; 320 + 321 + switch (mask) { 322 + case IIO_CHAN_INFO_RAW: 323 + if (chan->type == IIO_LIGHT) 324 + reg = STK3310_REG_ALS_DATA_MSB; 325 + else if (chan->type == IIO_PROXIMITY) 326 + reg = STK3310_REG_PS_DATA_MSB; 327 + else 328 + return -EINVAL; 329 + mutex_lock(&data->lock); 330 + ret = regmap_bulk_read(data->regmap, reg, &buf, 2); 331 + if (ret < 0) { 332 + dev_err(&client->dev, "register read failed\n"); 333 + mutex_unlock(&data->lock); 334 + return ret; 335 + } 336 + *val = swab16(buf); 337 + if (chan->type == IIO_PROXIMITY) { 338 + /* 339 + * Invert the proximity data so we return low values 340 + * for close objects and high values for far ones. 341 + */ 342 + regmap_field_read(data->reg_ps_gain, &index); 343 + *val = stk3310_ps_max[index] - *val; 344 + } 345 + mutex_unlock(&data->lock); 346 + return IIO_VAL_INT; 347 + case IIO_CHAN_INFO_INT_TIME: 348 + if (chan->type == IIO_LIGHT) 349 + regmap_field_read(data->reg_als_it, &index); 350 + else 351 + regmap_field_read(data->reg_ps_it, &index); 352 + *val = stk3310_it_table[index][0]; 353 + *val2 = stk3310_it_table[index][1]; 354 + return IIO_VAL_INT_PLUS_MICRO; 355 + case IIO_CHAN_INFO_SCALE: 356 + if (chan->type == IIO_LIGHT) 357 + regmap_field_read(data->reg_als_gain, &index); 358 + else 359 + regmap_field_read(data->reg_ps_gain, &index); 360 + *val = stk3310_scale_table[index][0]; 361 + *val2 = stk3310_scale_table[index][1]; 362 + return IIO_VAL_INT_PLUS_MICRO; 363 + } 364 + 365 + return -EINVAL; 366 + } 367 + 368 + static int stk3310_write_raw(struct iio_dev *indio_dev, 369 + struct iio_chan_spec const *chan, 370 + int val, int val2, long mask) 371 + { 372 + int ret; 373 + unsigned int index; 374 + struct stk3310_data *data = iio_priv(indio_dev); 375 + 376 + switch (mask) { 377 + case IIO_CHAN_INFO_INT_TIME: 378 + index = stk3310_get_index(stk3310_it_table, 379 + ARRAY_SIZE(stk3310_it_table), 380 + val, val2); 381 + if (index < 0) 382 + return -EINVAL; 383 + mutex_lock(&data->lock); 384 + if (chan->type == IIO_LIGHT) 385 + ret = regmap_field_write(data->reg_als_it, index); 386 + else 387 + ret = regmap_field_write(data->reg_ps_it, index); 388 + if (ret < 0) 389 + dev_err(&data->client->dev, 390 + "sensor configuration failed\n"); 391 + mutex_unlock(&data->lock); 392 + return ret; 393 + 394 + case IIO_CHAN_INFO_SCALE: 395 + index = stk3310_get_index(stk3310_scale_table, 396 + ARRAY_SIZE(stk3310_scale_table), 397 + val, val2); 398 + if (index < 0) 399 + return -EINVAL; 400 + mutex_lock(&data->lock); 401 + if (chan->type == IIO_LIGHT) 402 + ret = regmap_field_write(data->reg_als_gain, index); 403 + else 404 + ret = regmap_field_write(data->reg_ps_gain, index); 405 + if (ret < 0) 406 + dev_err(&data->client->dev, 407 + "sensor configuration failed\n"); 408 + mutex_unlock(&data->lock); 409 + return ret; 410 + } 411 + 412 + return -EINVAL; 413 + } 414 + 415 + static const struct iio_info stk3310_info = { 416 + .driver_module = THIS_MODULE, 417 + .read_raw = stk3310_read_raw, 418 + .write_raw = stk3310_write_raw, 419 + .attrs = &stk3310_attribute_group, 420 + .read_event_value = stk3310_read_event, 421 + .write_event_value = stk3310_write_event, 422 + .read_event_config = stk3310_read_event_config, 423 + .write_event_config = stk3310_write_event_config, 424 + }; 425 + 426 + static int stk3310_set_state(struct stk3310_data *data, u8 state) 427 + { 428 + int ret; 429 + struct i2c_client *client = data->client; 430 + 431 + /* 3-bit state; 0b100 is not supported. */ 432 + if (state > 7 || state == 4) 433 + return -EINVAL; 434 + 435 + mutex_lock(&data->lock); 436 + ret = regmap_field_write(data->reg_state, state); 437 + if (ret < 0) { 438 + dev_err(&client->dev, "failed to change sensor state\n"); 439 + } else if (state != STK3310_STATE_STANDBY) { 440 + /* Don't reset the 'enabled' flags if we're going in standby */ 441 + data->ps_enabled = !!(state & 0x01); 442 + data->als_enabled = !!(state & 0x02); 443 + } 444 + mutex_unlock(&data->lock); 445 + 446 + return ret; 447 + } 448 + 449 + static int stk3310_init(struct iio_dev *indio_dev) 450 + { 451 + int ret; 452 + int chipid; 453 + u8 state; 454 + struct stk3310_data *data = iio_priv(indio_dev); 455 + struct i2c_client *client = data->client; 456 + 457 + regmap_read(data->regmap, STK3310_REG_ID, &chipid); 458 + if (chipid != STK3310_CHIP_ID_VAL && 459 + chipid != STK3311_CHIP_ID_VAL) { 460 + dev_err(&client->dev, "invalid chip id: 0x%x\n", chipid); 461 + return -ENODEV; 462 + } 463 + 464 + state = STK3310_STATE_EN_ALS | STK3310_STATE_EN_PS; 465 + ret = stk3310_set_state(data, state); 466 + if (ret < 0) { 467 + dev_err(&client->dev, "failed to enable sensor"); 468 + return ret; 469 + } 470 + 471 + /* Enable PS interrupts */ 472 + ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN); 473 + if (ret < 0) 474 + dev_err(&client->dev, "failed to enable interrupts!\n"); 475 + 476 + return ret; 477 + } 478 + 479 + static int stk3310_gpio_probe(struct i2c_client *client) 480 + { 481 + struct device *dev; 482 + struct gpio_desc *gpio; 483 + int ret; 484 + 485 + if (!client) 486 + return -EINVAL; 487 + 488 + dev = &client->dev; 489 + 490 + /* gpio interrupt pin */ 491 + gpio = devm_gpiod_get_index(dev, STK3310_GPIO, 0); 492 + if (IS_ERR(gpio)) { 493 + dev_err(dev, "acpi gpio get index failed\n"); 494 + return PTR_ERR(gpio); 495 + } 496 + 497 + ret = gpiod_direction_input(gpio); 498 + if (ret) 499 + return ret; 500 + 501 + ret = gpiod_to_irq(gpio); 502 + dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); 503 + 504 + return ret; 505 + } 506 + 507 + static bool stk3310_is_volatile_reg(struct device *dev, unsigned int reg) 508 + { 509 + switch (reg) { 510 + case STK3310_REG_ALS_DATA_MSB: 511 + case STK3310_REG_ALS_DATA_LSB: 512 + case STK3310_REG_PS_DATA_LSB: 513 + case STK3310_REG_PS_DATA_MSB: 514 + case STK3310_REG_FLAG: 515 + return true; 516 + default: 517 + return false; 518 + } 519 + } 520 + 521 + static struct regmap_config stk3310_regmap_config = { 522 + .name = STK3310_REGMAP_NAME, 523 + .reg_bits = 8, 524 + .val_bits = 8, 525 + .max_register = STK3310_MAX_REG, 526 + .cache_type = REGCACHE_RBTREE, 527 + .volatile_reg = stk3310_is_volatile_reg, 528 + }; 529 + 530 + static int stk3310_regmap_init(struct stk3310_data *data) 531 + { 532 + struct regmap *regmap; 533 + struct i2c_client *client; 534 + 535 + client = data->client; 536 + regmap = devm_regmap_init_i2c(client, &stk3310_regmap_config); 537 + if (IS_ERR(regmap)) { 538 + dev_err(&client->dev, "regmap initialization failed.\n"); 539 + return PTR_ERR(regmap); 540 + } 541 + data->regmap = regmap; 542 + 543 + STK3310_REGFIELD(state); 544 + STK3310_REGFIELD(als_gain); 545 + STK3310_REGFIELD(ps_gain); 546 + STK3310_REGFIELD(als_it); 547 + STK3310_REGFIELD(ps_it); 548 + STK3310_REGFIELD(int_ps); 549 + STK3310_REGFIELD(flag_psint); 550 + STK3310_REGFIELD(flag_nf); 551 + 552 + return 0; 553 + } 554 + 555 + static irqreturn_t stk3310_irq_handler(int irq, void *private) 556 + { 557 + struct iio_dev *indio_dev = private; 558 + struct stk3310_data *data = iio_priv(indio_dev); 559 + 560 + data->timestamp = iio_get_time_ns(); 561 + 562 + return IRQ_WAKE_THREAD; 563 + } 564 + 565 + static irqreturn_t stk3310_irq_event_handler(int irq, void *private) 566 + { 567 + int ret; 568 + unsigned int dir; 569 + u64 event; 570 + 571 + struct iio_dev *indio_dev = private; 572 + struct stk3310_data *data = iio_priv(indio_dev); 573 + 574 + /* Read FLAG_NF to figure out what threshold has been met. */ 575 + mutex_lock(&data->lock); 576 + ret = regmap_field_read(data->reg_flag_nf, &dir); 577 + if (ret < 0) { 578 + dev_err(&data->client->dev, "register read failed\n"); 579 + mutex_unlock(&data->lock); 580 + return ret; 581 + } 582 + event = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1, 583 + IIO_EV_TYPE_THRESH, 584 + (dir ? IIO_EV_DIR_RISING : 585 + IIO_EV_DIR_FALLING)); 586 + iio_push_event(indio_dev, event, data->timestamp); 587 + 588 + /* Reset the interrupt flag */ 589 + ret = regmap_field_write(data->reg_flag_psint, 0); 590 + if (ret < 0) 591 + dev_err(&data->client->dev, "failed to reset interrupts\n"); 592 + mutex_unlock(&data->lock); 593 + 594 + return IRQ_HANDLED; 595 + } 596 + 597 + static int stk3310_probe(struct i2c_client *client, 598 + const struct i2c_device_id *id) 599 + { 600 + int ret; 601 + struct iio_dev *indio_dev; 602 + struct stk3310_data *data; 603 + 604 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 605 + if (!indio_dev) { 606 + dev_err(&client->dev, "iio allocation failed!\n"); 607 + return -ENOMEM; 608 + } 609 + 610 + data = iio_priv(indio_dev); 611 + data->client = client; 612 + i2c_set_clientdata(client, indio_dev); 613 + mutex_init(&data->lock); 614 + 615 + ret = stk3310_regmap_init(data); 616 + if (ret < 0) 617 + return ret; 618 + 619 + indio_dev->dev.parent = &client->dev; 620 + indio_dev->info = &stk3310_info; 621 + indio_dev->name = STK3310_DRIVER_NAME; 622 + indio_dev->modes = INDIO_DIRECT_MODE; 623 + indio_dev->channels = stk3310_channels; 624 + indio_dev->num_channels = ARRAY_SIZE(stk3310_channels); 625 + 626 + ret = stk3310_init(indio_dev); 627 + if (ret < 0) 628 + return ret; 629 + 630 + ret = iio_device_register(indio_dev); 631 + if (ret < 0) { 632 + dev_err(&client->dev, "device_register failed\n"); 633 + stk3310_set_state(data, STK3310_STATE_STANDBY); 634 + } 635 + 636 + if (client->irq <= 0) 637 + client->irq = stk3310_gpio_probe(client); 638 + 639 + if (client->irq >= 0) { 640 + ret = devm_request_threaded_irq(&client->dev, client->irq, 641 + stk3310_irq_handler, 642 + stk3310_irq_event_handler, 643 + IRQF_TRIGGER_FALLING | 644 + IRQF_ONESHOT, 645 + STK3310_EVENT, indio_dev); 646 + if (ret < 0) 647 + dev_err(&client->dev, "request irq %d failed\n", 648 + client->irq); 649 + } 650 + 651 + return ret; 652 + } 653 + 654 + static int stk3310_remove(struct i2c_client *client) 655 + { 656 + struct iio_dev *indio_dev = i2c_get_clientdata(client); 657 + 658 + iio_device_unregister(indio_dev); 659 + return stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY); 660 + } 661 + 662 + #ifdef CONFIG_PM_SLEEP 663 + static int stk3310_suspend(struct device *dev) 664 + { 665 + struct stk3310_data *data; 666 + 667 + data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 668 + 669 + return stk3310_set_state(data, STK3310_STATE_STANDBY); 670 + } 671 + 672 + static int stk3310_resume(struct device *dev) 673 + { 674 + int state = 0; 675 + struct stk3310_data *data; 676 + 677 + data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 678 + if (data->ps_enabled) 679 + state |= STK3310_STATE_EN_PS; 680 + if (data->als_enabled) 681 + state |= STK3310_STATE_EN_ALS; 682 + 683 + return stk3310_set_state(data, state); 684 + } 685 + 686 + static SIMPLE_DEV_PM_OPS(stk3310_pm_ops, stk3310_suspend, stk3310_resume); 687 + 688 + #define STK3310_PM_OPS (&stk3310_pm_ops) 689 + #else 690 + #define STK3310_PM_OPS NULL 691 + #endif 692 + 693 + static const struct i2c_device_id stk3310_i2c_id[] = { 694 + {"STK3310", 0}, 695 + {"STK3311", 0}, 696 + {} 697 + }; 698 + 699 + static const struct acpi_device_id stk3310_acpi_id[] = { 700 + {"STK3310", 0}, 701 + {"STK3311", 0}, 702 + {} 703 + }; 704 + 705 + MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id); 706 + 707 + static struct i2c_driver stk3310_driver = { 708 + .driver = { 709 + .name = "stk3310", 710 + .pm = STK3310_PM_OPS, 711 + .acpi_match_table = ACPI_PTR(stk3310_acpi_id), 712 + }, 713 + .probe = stk3310_probe, 714 + .remove = stk3310_remove, 715 + .id_table = stk3310_i2c_id, 716 + }; 717 + 718 + module_i2c_driver(stk3310_driver); 719 + 720 + MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); 721 + MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver"); 722 + MODULE_LICENSE("GPL v2");
+27 -2
drivers/iio/magnetometer/Kconfig
··· 8 8 config AK8975 9 9 tristate "Asahi Kasei AK 3-Axis Magnetometer" 10 10 depends on I2C 11 - depends on GPIOLIB 11 + depends on GPIOLIB || COMPILE_TEST 12 12 help 13 13 Say yes here to build support for Asahi Kasei AK8975, AK8963, 14 14 AK09911 or AK09912 3-Axis Magnetometer. ··· 19 19 config AK09911 20 20 tristate "Asahi Kasei AK09911 3-axis Compass" 21 21 depends on I2C 22 - depends on GPIOLIB 22 + depends on GPIOLIB || COMPILE_TEST 23 23 select AK8975 24 24 help 25 25 Deprecated: AK09911 is now supported by AK8975 driver. ··· 46 46 help 47 47 Say yes here to build support for the HID SENSOR 48 48 Magnetometer 3D. 49 + 50 + config MMC35240 51 + tristate "MEMSIC MMC35240 3-axis magnetic sensor" 52 + select REGMAP_I2C 53 + depends on I2C 54 + help 55 + Say yes here to build support for the MEMSIC MMC35240 3-axis 56 + magnetic sensor. 57 + 58 + To compile this driver as a module, choose M here: the module 59 + will be called mmc35240. 49 60 50 61 config IIO_ST_MAGN_3AXIS 51 62 tristate "STMicroelectronics magnetometers 3-Axis Driver" ··· 86 75 tristate 87 76 depends on IIO_ST_MAGN_3AXIS 88 77 depends on IIO_ST_SENSORS_SPI 78 + 79 + config BMC150_MAGN 80 + tristate "Bosch BMC150 Magnetometer Driver" 81 + depends on I2C 82 + select IIO_BUFFER 83 + select IIO_TRIGGERED_BUFFER 84 + help 85 + Say yes here to build support for the BMC150 magnetometer. 86 + 87 + Currently this only supports the device via an i2c interface. 88 + 89 + This is a combo module with both accelerometer and magnetometer. 90 + This driver is only implementing magnetometer part, which has 91 + its own address and register map. 89 92 90 93 endmenu
+3
drivers/iio/magnetometer/Makefile
··· 6 6 obj-$(CONFIG_AK8975) += ak8975.o 7 7 obj-$(CONFIG_MAG3110) += mag3110.o 8 8 obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o 9 + obj-$(CONFIG_MMC35240) += mmc35240.o 9 10 10 11 obj-$(CONFIG_IIO_ST_MAGN_3AXIS) += st_magn.o 11 12 st_magn-y := st_magn_core.o ··· 14 13 15 14 obj-$(CONFIG_IIO_ST_MAGN_I2C_3AXIS) += st_magn_i2c.o 16 15 obj-$(CONFIG_IIO_ST_MAGN_SPI_3AXIS) += st_magn_spi.o 16 + 17 + obj-$(CONFIG_BMC150_MAGN) += bmc150_magn.o
+1109
drivers/iio/magnetometer/bmc150_magn.c
··· 1 + /* 2 + * Bosch BMC150 three-axis magnetic field sensor driver 3 + * 4 + * Copyright (c) 2015, Intel Corporation. 5 + * 6 + * This code is based on bmm050_api.c authored by contact@bosch.sensortec.com: 7 + * 8 + * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved 9 + * 10 + * This program is free software; you can redistribute it and/or modify it 11 + * under the terms and conditions of the GNU General Public License, 12 + * version 2, as published by the Free Software Foundation. 13 + * 14 + * This program is distributed in the hope it will be useful, but WITHOUT 15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 17 + * more details. 18 + */ 19 + 20 + #include <linux/module.h> 21 + #include <linux/i2c.h> 22 + #include <linux/interrupt.h> 23 + #include <linux/delay.h> 24 + #include <linux/slab.h> 25 + #include <linux/acpi.h> 26 + #include <linux/gpio/consumer.h> 27 + #include <linux/pm.h> 28 + #include <linux/pm_runtime.h> 29 + #include <linux/iio/iio.h> 30 + #include <linux/iio/sysfs.h> 31 + #include <linux/iio/buffer.h> 32 + #include <linux/iio/events.h> 33 + #include <linux/iio/trigger.h> 34 + #include <linux/iio/trigger_consumer.h> 35 + #include <linux/iio/triggered_buffer.h> 36 + #include <linux/regmap.h> 37 + 38 + #define BMC150_MAGN_DRV_NAME "bmc150_magn" 39 + #define BMC150_MAGN_IRQ_NAME "bmc150_magn_event" 40 + #define BMC150_MAGN_GPIO_INT "interrupt" 41 + 42 + #define BMC150_MAGN_REG_CHIP_ID 0x40 43 + #define BMC150_MAGN_CHIP_ID_VAL 0x32 44 + 45 + #define BMC150_MAGN_REG_X_L 0x42 46 + #define BMC150_MAGN_REG_X_M 0x43 47 + #define BMC150_MAGN_REG_Y_L 0x44 48 + #define BMC150_MAGN_REG_Y_M 0x45 49 + #define BMC150_MAGN_SHIFT_XY_L 3 50 + #define BMC150_MAGN_REG_Z_L 0x46 51 + #define BMC150_MAGN_REG_Z_M 0x47 52 + #define BMC150_MAGN_SHIFT_Z_L 1 53 + #define BMC150_MAGN_REG_RHALL_L 0x48 54 + #define BMC150_MAGN_REG_RHALL_M 0x49 55 + #define BMC150_MAGN_SHIFT_RHALL_L 2 56 + 57 + #define BMC150_MAGN_REG_INT_STATUS 0x4A 58 + 59 + #define BMC150_MAGN_REG_POWER 0x4B 60 + #define BMC150_MAGN_MASK_POWER_CTL BIT(0) 61 + 62 + #define BMC150_MAGN_REG_OPMODE_ODR 0x4C 63 + #define BMC150_MAGN_MASK_OPMODE GENMASK(2, 1) 64 + #define BMC150_MAGN_SHIFT_OPMODE 1 65 + #define BMC150_MAGN_MODE_NORMAL 0x00 66 + #define BMC150_MAGN_MODE_FORCED 0x01 67 + #define BMC150_MAGN_MODE_SLEEP 0x03 68 + #define BMC150_MAGN_MASK_ODR GENMASK(5, 3) 69 + #define BMC150_MAGN_SHIFT_ODR 3 70 + 71 + #define BMC150_MAGN_REG_INT 0x4D 72 + 73 + #define BMC150_MAGN_REG_INT_DRDY 0x4E 74 + #define BMC150_MAGN_MASK_DRDY_EN BIT(7) 75 + #define BMC150_MAGN_SHIFT_DRDY_EN 7 76 + #define BMC150_MAGN_MASK_DRDY_INT3 BIT(6) 77 + #define BMC150_MAGN_MASK_DRDY_Z_EN BIT(5) 78 + #define BMC150_MAGN_MASK_DRDY_Y_EN BIT(4) 79 + #define BMC150_MAGN_MASK_DRDY_X_EN BIT(3) 80 + #define BMC150_MAGN_MASK_DRDY_DR_POLARITY BIT(2) 81 + #define BMC150_MAGN_MASK_DRDY_LATCHING BIT(1) 82 + #define BMC150_MAGN_MASK_DRDY_INT3_POLARITY BIT(0) 83 + 84 + #define BMC150_MAGN_REG_LOW_THRESH 0x4F 85 + #define BMC150_MAGN_REG_HIGH_THRESH 0x50 86 + #define BMC150_MAGN_REG_REP_XY 0x51 87 + #define BMC150_MAGN_REG_REP_Z 0x52 88 + 89 + #define BMC150_MAGN_REG_TRIM_START 0x5D 90 + #define BMC150_MAGN_REG_TRIM_END 0x71 91 + 92 + #define BMC150_MAGN_XY_OVERFLOW_VAL -4096 93 + #define BMC150_MAGN_Z_OVERFLOW_VAL -16384 94 + 95 + /* Time from SUSPEND to SLEEP */ 96 + #define BMC150_MAGN_START_UP_TIME_MS 3 97 + 98 + #define BMC150_MAGN_AUTO_SUSPEND_DELAY_MS 2000 99 + 100 + #define BMC150_MAGN_REGVAL_TO_REPXY(regval) (((regval) * 2) + 1) 101 + #define BMC150_MAGN_REGVAL_TO_REPZ(regval) ((regval) + 1) 102 + #define BMC150_MAGN_REPXY_TO_REGVAL(rep) (((rep) - 1) / 2) 103 + #define BMC150_MAGN_REPZ_TO_REGVAL(rep) ((rep) - 1) 104 + 105 + enum bmc150_magn_axis { 106 + AXIS_X, 107 + AXIS_Y, 108 + AXIS_Z, 109 + RHALL, 110 + AXIS_XYZ_MAX = RHALL, 111 + AXIS_XYZR_MAX, 112 + }; 113 + 114 + enum bmc150_magn_power_modes { 115 + BMC150_MAGN_POWER_MODE_SUSPEND, 116 + BMC150_MAGN_POWER_MODE_SLEEP, 117 + BMC150_MAGN_POWER_MODE_NORMAL, 118 + }; 119 + 120 + struct bmc150_magn_trim_regs { 121 + s8 x1; 122 + s8 y1; 123 + __le16 reserved1; 124 + u8 reserved2; 125 + __le16 z4; 126 + s8 x2; 127 + s8 y2; 128 + __le16 reserved3; 129 + __le16 z2; 130 + __le16 z1; 131 + __le16 xyz1; 132 + __le16 z3; 133 + s8 xy2; 134 + u8 xy1; 135 + } __packed; 136 + 137 + struct bmc150_magn_data { 138 + struct i2c_client *client; 139 + /* 140 + * 1. Protect this structure. 141 + * 2. Serialize sequences that power on/off the device and access HW. 142 + */ 143 + struct mutex mutex; 144 + struct regmap *regmap; 145 + /* 4 x 32 bits for x, y z, 4 bytes align, 64 bits timestamp */ 146 + s32 buffer[6]; 147 + struct iio_trigger *dready_trig; 148 + bool dready_trigger_on; 149 + int max_odr; 150 + }; 151 + 152 + static const struct { 153 + int freq; 154 + u8 reg_val; 155 + } bmc150_magn_samp_freq_table[] = { {2, 0x01}, 156 + {6, 0x02}, 157 + {8, 0x03}, 158 + {10, 0x00}, 159 + {15, 0x04}, 160 + {20, 0x05}, 161 + {25, 0x06}, 162 + {30, 0x07} }; 163 + 164 + enum bmc150_magn_presets { 165 + LOW_POWER_PRESET, 166 + REGULAR_PRESET, 167 + ENHANCED_REGULAR_PRESET, 168 + HIGH_ACCURACY_PRESET 169 + }; 170 + 171 + static const struct bmc150_magn_preset { 172 + u8 rep_xy; 173 + u8 rep_z; 174 + u8 odr; 175 + } bmc150_magn_presets_table[] = { 176 + [LOW_POWER_PRESET] = {3, 3, 10}, 177 + [REGULAR_PRESET] = {9, 15, 10}, 178 + [ENHANCED_REGULAR_PRESET] = {15, 27, 10}, 179 + [HIGH_ACCURACY_PRESET] = {47, 83, 20}, 180 + }; 181 + 182 + #define BMC150_MAGN_DEFAULT_PRESET REGULAR_PRESET 183 + 184 + static bool bmc150_magn_is_writeable_reg(struct device *dev, unsigned int reg) 185 + { 186 + switch (reg) { 187 + case BMC150_MAGN_REG_POWER: 188 + case BMC150_MAGN_REG_OPMODE_ODR: 189 + case BMC150_MAGN_REG_INT: 190 + case BMC150_MAGN_REG_INT_DRDY: 191 + case BMC150_MAGN_REG_LOW_THRESH: 192 + case BMC150_MAGN_REG_HIGH_THRESH: 193 + case BMC150_MAGN_REG_REP_XY: 194 + case BMC150_MAGN_REG_REP_Z: 195 + return true; 196 + default: 197 + return false; 198 + }; 199 + } 200 + 201 + static bool bmc150_magn_is_volatile_reg(struct device *dev, unsigned int reg) 202 + { 203 + switch (reg) { 204 + case BMC150_MAGN_REG_X_L: 205 + case BMC150_MAGN_REG_X_M: 206 + case BMC150_MAGN_REG_Y_L: 207 + case BMC150_MAGN_REG_Y_M: 208 + case BMC150_MAGN_REG_Z_L: 209 + case BMC150_MAGN_REG_Z_M: 210 + case BMC150_MAGN_REG_RHALL_L: 211 + case BMC150_MAGN_REG_RHALL_M: 212 + case BMC150_MAGN_REG_INT_STATUS: 213 + return true; 214 + default: 215 + return false; 216 + } 217 + } 218 + 219 + static const struct regmap_config bmc150_magn_regmap_config = { 220 + .reg_bits = 8, 221 + .val_bits = 8, 222 + 223 + .max_register = BMC150_MAGN_REG_TRIM_END, 224 + .cache_type = REGCACHE_RBTREE, 225 + 226 + .writeable_reg = bmc150_magn_is_writeable_reg, 227 + .volatile_reg = bmc150_magn_is_volatile_reg, 228 + }; 229 + 230 + static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data, 231 + enum bmc150_magn_power_modes mode, 232 + bool state) 233 + { 234 + int ret; 235 + 236 + switch (mode) { 237 + case BMC150_MAGN_POWER_MODE_SUSPEND: 238 + ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_POWER, 239 + BMC150_MAGN_MASK_POWER_CTL, !state); 240 + if (ret < 0) 241 + return ret; 242 + usleep_range(BMC150_MAGN_START_UP_TIME_MS * 1000, 20000); 243 + return 0; 244 + case BMC150_MAGN_POWER_MODE_SLEEP: 245 + return regmap_update_bits(data->regmap, 246 + BMC150_MAGN_REG_OPMODE_ODR, 247 + BMC150_MAGN_MASK_OPMODE, 248 + BMC150_MAGN_MODE_SLEEP << 249 + BMC150_MAGN_SHIFT_OPMODE); 250 + case BMC150_MAGN_POWER_MODE_NORMAL: 251 + return regmap_update_bits(data->regmap, 252 + BMC150_MAGN_REG_OPMODE_ODR, 253 + BMC150_MAGN_MASK_OPMODE, 254 + BMC150_MAGN_MODE_NORMAL << 255 + BMC150_MAGN_SHIFT_OPMODE); 256 + } 257 + 258 + return -EINVAL; 259 + } 260 + 261 + static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on) 262 + { 263 + #ifdef CONFIG_PM 264 + int ret; 265 + 266 + if (on) { 267 + ret = pm_runtime_get_sync(&data->client->dev); 268 + } else { 269 + pm_runtime_mark_last_busy(&data->client->dev); 270 + ret = pm_runtime_put_autosuspend(&data->client->dev); 271 + } 272 + 273 + if (ret < 0) { 274 + dev_err(&data->client->dev, 275 + "failed to change power state to %d\n", on); 276 + if (on) 277 + pm_runtime_put_noidle(&data->client->dev); 278 + 279 + return ret; 280 + } 281 + #endif 282 + 283 + return 0; 284 + } 285 + 286 + static int bmc150_magn_get_odr(struct bmc150_magn_data *data, int *val) 287 + { 288 + int ret, reg_val; 289 + u8 i, odr_val; 290 + 291 + ret = regmap_read(data->regmap, BMC150_MAGN_REG_OPMODE_ODR, &reg_val); 292 + if (ret < 0) 293 + return ret; 294 + odr_val = (reg_val & BMC150_MAGN_MASK_ODR) >> BMC150_MAGN_SHIFT_ODR; 295 + 296 + for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) 297 + if (bmc150_magn_samp_freq_table[i].reg_val == odr_val) { 298 + *val = bmc150_magn_samp_freq_table[i].freq; 299 + return 0; 300 + } 301 + 302 + return -EINVAL; 303 + } 304 + 305 + static int bmc150_magn_set_odr(struct bmc150_magn_data *data, int val) 306 + { 307 + int ret; 308 + u8 i; 309 + 310 + for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) { 311 + if (bmc150_magn_samp_freq_table[i].freq == val) { 312 + ret = regmap_update_bits(data->regmap, 313 + BMC150_MAGN_REG_OPMODE_ODR, 314 + BMC150_MAGN_MASK_ODR, 315 + bmc150_magn_samp_freq_table[i]. 316 + reg_val << 317 + BMC150_MAGN_SHIFT_ODR); 318 + if (ret < 0) 319 + return ret; 320 + return 0; 321 + } 322 + } 323 + 324 + return -EINVAL; 325 + } 326 + 327 + static int bmc150_magn_set_max_odr(struct bmc150_magn_data *data, int rep_xy, 328 + int rep_z, int odr) 329 + { 330 + int ret, reg_val, max_odr; 331 + 332 + if (rep_xy <= 0) { 333 + ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY, 334 + &reg_val); 335 + if (ret < 0) 336 + return ret; 337 + rep_xy = BMC150_MAGN_REGVAL_TO_REPXY(reg_val); 338 + } 339 + if (rep_z <= 0) { 340 + ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z, 341 + &reg_val); 342 + if (ret < 0) 343 + return ret; 344 + rep_z = BMC150_MAGN_REGVAL_TO_REPZ(reg_val); 345 + } 346 + if (odr <= 0) { 347 + ret = bmc150_magn_get_odr(data, &odr); 348 + if (ret < 0) 349 + return ret; 350 + } 351 + /* the maximum selectable read-out frequency from datasheet */ 352 + max_odr = 1000000 / (145 * rep_xy + 500 * rep_z + 980); 353 + if (odr > max_odr) { 354 + dev_err(&data->client->dev, 355 + "Can't set oversampling with sampling freq %d\n", 356 + odr); 357 + return -EINVAL; 358 + } 359 + data->max_odr = max_odr; 360 + 361 + return 0; 362 + } 363 + 364 + static s32 bmc150_magn_compensate_x(struct bmc150_magn_trim_regs *tregs, s16 x, 365 + u16 rhall) 366 + { 367 + s16 val; 368 + u16 xyz1 = le16_to_cpu(tregs->xyz1); 369 + 370 + if (x == BMC150_MAGN_XY_OVERFLOW_VAL) 371 + return S32_MIN; 372 + 373 + if (!rhall) 374 + rhall = xyz1; 375 + 376 + val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000))); 377 + val = ((s16)((((s32)x) * ((((((((s32)tregs->xy2) * ((((s32)val) * 378 + ((s32)val)) >> 7)) + (((s32)val) * 379 + ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) * 380 + ((s32)(((s16)tregs->x2) + ((s16)0xA0)))) >> 12)) >> 13)) + 381 + (((s16)tregs->x1) << 3); 382 + 383 + return (s32)val; 384 + } 385 + 386 + static s32 bmc150_magn_compensate_y(struct bmc150_magn_trim_regs *tregs, s16 y, 387 + u16 rhall) 388 + { 389 + s16 val; 390 + u16 xyz1 = le16_to_cpu(tregs->xyz1); 391 + 392 + if (y == BMC150_MAGN_XY_OVERFLOW_VAL) 393 + return S32_MIN; 394 + 395 + if (!rhall) 396 + rhall = xyz1; 397 + 398 + val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000))); 399 + val = ((s16)((((s32)y) * ((((((((s32)tregs->xy2) * ((((s32)val) * 400 + ((s32)val)) >> 7)) + (((s32)val) * 401 + ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) * 402 + ((s32)(((s16)tregs->y2) + ((s16)0xA0)))) >> 12)) >> 13)) + 403 + (((s16)tregs->y1) << 3); 404 + 405 + return (s32)val; 406 + } 407 + 408 + static s32 bmc150_magn_compensate_z(struct bmc150_magn_trim_regs *tregs, s16 z, 409 + u16 rhall) 410 + { 411 + s32 val; 412 + u16 xyz1 = le16_to_cpu(tregs->xyz1); 413 + u16 z1 = le16_to_cpu(tregs->z1); 414 + s16 z2 = le16_to_cpu(tregs->z2); 415 + s16 z3 = le16_to_cpu(tregs->z3); 416 + s16 z4 = le16_to_cpu(tregs->z4); 417 + 418 + if (z == BMC150_MAGN_Z_OVERFLOW_VAL) 419 + return S32_MIN; 420 + 421 + val = (((((s32)(z - z4)) << 15) - ((((s32)z3) * ((s32)(((s16)rhall) - 422 + ((s16)xyz1)))) >> 2)) / (z2 + ((s16)(((((s32)z1) * 423 + ((((s16)rhall) << 1))) + (1 << 15)) >> 16)))); 424 + 425 + return val; 426 + } 427 + 428 + static int bmc150_magn_read_xyz(struct bmc150_magn_data *data, s32 *buffer) 429 + { 430 + int ret; 431 + __le16 values[AXIS_XYZR_MAX]; 432 + s16 raw_x, raw_y, raw_z; 433 + u16 rhall; 434 + struct bmc150_magn_trim_regs tregs; 435 + 436 + ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_X_L, 437 + values, sizeof(values)); 438 + if (ret < 0) 439 + return ret; 440 + 441 + raw_x = (s16)le16_to_cpu(values[AXIS_X]) >> BMC150_MAGN_SHIFT_XY_L; 442 + raw_y = (s16)le16_to_cpu(values[AXIS_Y]) >> BMC150_MAGN_SHIFT_XY_L; 443 + raw_z = (s16)le16_to_cpu(values[AXIS_Z]) >> BMC150_MAGN_SHIFT_Z_L; 444 + rhall = le16_to_cpu(values[RHALL]) >> BMC150_MAGN_SHIFT_RHALL_L; 445 + 446 + ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_TRIM_START, 447 + &tregs, sizeof(tregs)); 448 + if (ret < 0) 449 + return ret; 450 + 451 + buffer[AXIS_X] = bmc150_magn_compensate_x(&tregs, raw_x, rhall); 452 + buffer[AXIS_Y] = bmc150_magn_compensate_y(&tregs, raw_y, rhall); 453 + buffer[AXIS_Z] = bmc150_magn_compensate_z(&tregs, raw_z, rhall); 454 + 455 + return 0; 456 + } 457 + 458 + static int bmc150_magn_read_raw(struct iio_dev *indio_dev, 459 + struct iio_chan_spec const *chan, 460 + int *val, int *val2, long mask) 461 + { 462 + struct bmc150_magn_data *data = iio_priv(indio_dev); 463 + int ret, tmp; 464 + s32 values[AXIS_XYZ_MAX]; 465 + 466 + switch (mask) { 467 + case IIO_CHAN_INFO_RAW: 468 + if (iio_buffer_enabled(indio_dev)) 469 + return -EBUSY; 470 + mutex_lock(&data->mutex); 471 + 472 + ret = bmc150_magn_set_power_state(data, true); 473 + if (ret < 0) { 474 + mutex_unlock(&data->mutex); 475 + return ret; 476 + } 477 + 478 + ret = bmc150_magn_read_xyz(data, values); 479 + if (ret < 0) { 480 + bmc150_magn_set_power_state(data, false); 481 + mutex_unlock(&data->mutex); 482 + return ret; 483 + } 484 + *val = values[chan->scan_index]; 485 + 486 + ret = bmc150_magn_set_power_state(data, false); 487 + if (ret < 0) { 488 + mutex_unlock(&data->mutex); 489 + return ret; 490 + } 491 + 492 + mutex_unlock(&data->mutex); 493 + return IIO_VAL_INT; 494 + case IIO_CHAN_INFO_SCALE: 495 + /* 496 + * The API/driver performs an off-chip temperature 497 + * compensation and outputs x/y/z magnetic field data in 498 + * 16 LSB/uT to the upper application layer. 499 + */ 500 + *val = 0; 501 + *val2 = 625; 502 + return IIO_VAL_INT_PLUS_MICRO; 503 + case IIO_CHAN_INFO_SAMP_FREQ: 504 + ret = bmc150_magn_get_odr(data, val); 505 + if (ret < 0) 506 + return ret; 507 + return IIO_VAL_INT; 508 + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 509 + switch (chan->channel2) { 510 + case IIO_MOD_X: 511 + case IIO_MOD_Y: 512 + ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY, 513 + &tmp); 514 + if (ret < 0) 515 + return ret; 516 + *val = BMC150_MAGN_REGVAL_TO_REPXY(tmp); 517 + return IIO_VAL_INT; 518 + case IIO_MOD_Z: 519 + ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z, 520 + &tmp); 521 + if (ret < 0) 522 + return ret; 523 + *val = BMC150_MAGN_REGVAL_TO_REPZ(tmp); 524 + return IIO_VAL_INT; 525 + default: 526 + return -EINVAL; 527 + } 528 + default: 529 + return -EINVAL; 530 + } 531 + } 532 + 533 + static int bmc150_magn_write_raw(struct iio_dev *indio_dev, 534 + struct iio_chan_spec const *chan, 535 + int val, int val2, long mask) 536 + { 537 + struct bmc150_magn_data *data = iio_priv(indio_dev); 538 + int ret; 539 + 540 + switch (mask) { 541 + case IIO_CHAN_INFO_SAMP_FREQ: 542 + if (val > data->max_odr) 543 + return -EINVAL; 544 + mutex_lock(&data->mutex); 545 + ret = bmc150_magn_set_odr(data, val); 546 + mutex_unlock(&data->mutex); 547 + return ret; 548 + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 549 + switch (chan->channel2) { 550 + case IIO_MOD_X: 551 + case IIO_MOD_Y: 552 + if (val < 1 || val > 511) 553 + return -EINVAL; 554 + mutex_lock(&data->mutex); 555 + ret = bmc150_magn_set_max_odr(data, val, 0, 0); 556 + if (ret < 0) { 557 + mutex_unlock(&data->mutex); 558 + return ret; 559 + } 560 + ret = regmap_update_bits(data->regmap, 561 + BMC150_MAGN_REG_REP_XY, 562 + 0xFF, 563 + BMC150_MAGN_REPXY_TO_REGVAL 564 + (val)); 565 + mutex_unlock(&data->mutex); 566 + return ret; 567 + case IIO_MOD_Z: 568 + if (val < 1 || val > 256) 569 + return -EINVAL; 570 + mutex_lock(&data->mutex); 571 + ret = bmc150_magn_set_max_odr(data, 0, val, 0); 572 + if (ret < 0) { 573 + mutex_unlock(&data->mutex); 574 + return ret; 575 + } 576 + ret = regmap_update_bits(data->regmap, 577 + BMC150_MAGN_REG_REP_Z, 578 + 0xFF, 579 + BMC150_MAGN_REPZ_TO_REGVAL 580 + (val)); 581 + mutex_unlock(&data->mutex); 582 + return ret; 583 + default: 584 + return -EINVAL; 585 + } 586 + default: 587 + return -EINVAL; 588 + } 589 + } 590 + 591 + static int bmc150_magn_validate_trigger(struct iio_dev *indio_dev, 592 + struct iio_trigger *trig) 593 + { 594 + struct bmc150_magn_data *data = iio_priv(indio_dev); 595 + 596 + if (data->dready_trig != trig) 597 + return -EINVAL; 598 + 599 + return 0; 600 + } 601 + 602 + static ssize_t bmc150_magn_show_samp_freq_avail(struct device *dev, 603 + struct device_attribute *attr, 604 + char *buf) 605 + { 606 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 607 + struct bmc150_magn_data *data = iio_priv(indio_dev); 608 + size_t len = 0; 609 + u8 i; 610 + 611 + for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) { 612 + if (bmc150_magn_samp_freq_table[i].freq > data->max_odr) 613 + break; 614 + len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", 615 + bmc150_magn_samp_freq_table[i].freq); 616 + } 617 + /* replace last space with a newline */ 618 + buf[len - 1] = '\n'; 619 + 620 + return len; 621 + } 622 + 623 + static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(bmc150_magn_show_samp_freq_avail); 624 + 625 + static struct attribute *bmc150_magn_attributes[] = { 626 + &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 627 + NULL, 628 + }; 629 + 630 + static const struct attribute_group bmc150_magn_attrs_group = { 631 + .attrs = bmc150_magn_attributes, 632 + }; 633 + 634 + #define BMC150_MAGN_CHANNEL(_axis) { \ 635 + .type = IIO_MAGN, \ 636 + .modified = 1, \ 637 + .channel2 = IIO_MOD_##_axis, \ 638 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 639 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ 640 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ 641 + BIT(IIO_CHAN_INFO_SCALE), \ 642 + .scan_index = AXIS_##_axis, \ 643 + .scan_type = { \ 644 + .sign = 's', \ 645 + .realbits = 32, \ 646 + .storagebits = 32, \ 647 + .endianness = IIO_LE \ 648 + }, \ 649 + } 650 + 651 + static const struct iio_chan_spec bmc150_magn_channels[] = { 652 + BMC150_MAGN_CHANNEL(X), 653 + BMC150_MAGN_CHANNEL(Y), 654 + BMC150_MAGN_CHANNEL(Z), 655 + IIO_CHAN_SOFT_TIMESTAMP(3), 656 + }; 657 + 658 + static const struct iio_info bmc150_magn_info = { 659 + .attrs = &bmc150_magn_attrs_group, 660 + .read_raw = bmc150_magn_read_raw, 661 + .write_raw = bmc150_magn_write_raw, 662 + .validate_trigger = bmc150_magn_validate_trigger, 663 + .driver_module = THIS_MODULE, 664 + }; 665 + 666 + static const unsigned long bmc150_magn_scan_masks[] = {0x07, 0}; 667 + 668 + static irqreturn_t bmc150_magn_trigger_handler(int irq, void *p) 669 + { 670 + struct iio_poll_func *pf = p; 671 + struct iio_dev *indio_dev = pf->indio_dev; 672 + struct bmc150_magn_data *data = iio_priv(indio_dev); 673 + int ret; 674 + 675 + mutex_lock(&data->mutex); 676 + ret = bmc150_magn_read_xyz(data, data->buffer); 677 + mutex_unlock(&data->mutex); 678 + if (ret < 0) 679 + goto err; 680 + 681 + iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 682 + pf->timestamp); 683 + 684 + err: 685 + iio_trigger_notify_done(data->dready_trig); 686 + 687 + return IRQ_HANDLED; 688 + } 689 + 690 + static int bmc150_magn_init(struct bmc150_magn_data *data) 691 + { 692 + int ret, chip_id; 693 + struct bmc150_magn_preset preset; 694 + 695 + ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, 696 + false); 697 + if (ret < 0) { 698 + dev_err(&data->client->dev, 699 + "Failed to bring up device from suspend mode\n"); 700 + return ret; 701 + } 702 + 703 + ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id); 704 + if (ret < 0) { 705 + dev_err(&data->client->dev, "Failed reading chip id\n"); 706 + goto err_poweroff; 707 + } 708 + if (chip_id != BMC150_MAGN_CHIP_ID_VAL) { 709 + dev_err(&data->client->dev, "Invalid chip id 0x%x\n", ret); 710 + ret = -ENODEV; 711 + goto err_poweroff; 712 + } 713 + dev_dbg(&data->client->dev, "Chip id %x\n", ret); 714 + 715 + preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET]; 716 + ret = bmc150_magn_set_odr(data, preset.odr); 717 + if (ret < 0) { 718 + dev_err(&data->client->dev, "Failed to set ODR to %d\n", 719 + preset.odr); 720 + goto err_poweroff; 721 + } 722 + 723 + ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY, 724 + BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy)); 725 + if (ret < 0) { 726 + dev_err(&data->client->dev, "Failed to set REP XY to %d\n", 727 + preset.rep_xy); 728 + goto err_poweroff; 729 + } 730 + 731 + ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z, 732 + BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z)); 733 + if (ret < 0) { 734 + dev_err(&data->client->dev, "Failed to set REP Z to %d\n", 735 + preset.rep_z); 736 + goto err_poweroff; 737 + } 738 + 739 + ret = bmc150_magn_set_max_odr(data, preset.rep_xy, preset.rep_z, 740 + preset.odr); 741 + if (ret < 0) 742 + goto err_poweroff; 743 + 744 + ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, 745 + true); 746 + if (ret < 0) { 747 + dev_err(&data->client->dev, "Failed to power on device\n"); 748 + goto err_poweroff; 749 + } 750 + 751 + return 0; 752 + 753 + err_poweroff: 754 + bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); 755 + return ret; 756 + } 757 + 758 + static int bmc150_magn_reset_intr(struct bmc150_magn_data *data) 759 + { 760 + int tmp; 761 + 762 + /* 763 + * Data Ready (DRDY) is always cleared after 764 + * readout of data registers ends. 765 + */ 766 + return regmap_read(data->regmap, BMC150_MAGN_REG_X_L, &tmp); 767 + } 768 + 769 + static int bmc150_magn_trig_try_reen(struct iio_trigger *trig) 770 + { 771 + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 772 + struct bmc150_magn_data *data = iio_priv(indio_dev); 773 + int ret; 774 + 775 + if (!data->dready_trigger_on) 776 + return 0; 777 + 778 + mutex_lock(&data->mutex); 779 + ret = bmc150_magn_reset_intr(data); 780 + mutex_unlock(&data->mutex); 781 + 782 + return ret; 783 + } 784 + 785 + static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig, 786 + bool state) 787 + { 788 + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 789 + struct bmc150_magn_data *data = iio_priv(indio_dev); 790 + int ret = 0; 791 + 792 + mutex_lock(&data->mutex); 793 + if (state == data->dready_trigger_on) 794 + goto err_unlock; 795 + 796 + ret = bmc150_magn_set_power_state(data, state); 797 + if (ret < 0) 798 + goto err_unlock; 799 + 800 + ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY, 801 + BMC150_MAGN_MASK_DRDY_EN, 802 + state << BMC150_MAGN_SHIFT_DRDY_EN); 803 + if (ret < 0) 804 + goto err_poweroff; 805 + 806 + data->dready_trigger_on = state; 807 + 808 + if (state) { 809 + ret = bmc150_magn_reset_intr(data); 810 + if (ret < 0) 811 + goto err_poweroff; 812 + } 813 + mutex_unlock(&data->mutex); 814 + 815 + return 0; 816 + 817 + err_poweroff: 818 + bmc150_magn_set_power_state(data, false); 819 + err_unlock: 820 + mutex_unlock(&data->mutex); 821 + return ret; 822 + } 823 + 824 + static const struct iio_trigger_ops bmc150_magn_trigger_ops = { 825 + .set_trigger_state = bmc150_magn_data_rdy_trigger_set_state, 826 + .try_reenable = bmc150_magn_trig_try_reen, 827 + .owner = THIS_MODULE, 828 + }; 829 + 830 + static int bmc150_magn_gpio_probe(struct i2c_client *client) 831 + { 832 + struct device *dev; 833 + struct gpio_desc *gpio; 834 + int ret; 835 + 836 + if (!client) 837 + return -EINVAL; 838 + 839 + dev = &client->dev; 840 + 841 + /* data ready GPIO interrupt pin */ 842 + gpio = devm_gpiod_get_index(dev, BMC150_MAGN_GPIO_INT, 0); 843 + if (IS_ERR(gpio)) { 844 + dev_err(dev, "ACPI GPIO get index failed\n"); 845 + return PTR_ERR(gpio); 846 + } 847 + 848 + ret = gpiod_direction_input(gpio); 849 + if (ret) 850 + return ret; 851 + 852 + ret = gpiod_to_irq(gpio); 853 + 854 + dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); 855 + 856 + return ret; 857 + } 858 + 859 + static const char *bmc150_magn_match_acpi_device(struct device *dev) 860 + { 861 + const struct acpi_device_id *id; 862 + 863 + id = acpi_match_device(dev->driver->acpi_match_table, dev); 864 + if (!id) 865 + return NULL; 866 + 867 + return dev_name(dev); 868 + } 869 + 870 + static int bmc150_magn_probe(struct i2c_client *client, 871 + const struct i2c_device_id *id) 872 + { 873 + struct bmc150_magn_data *data; 874 + struct iio_dev *indio_dev; 875 + const char *name = NULL; 876 + int ret; 877 + 878 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 879 + if (!indio_dev) 880 + return -ENOMEM; 881 + 882 + data = iio_priv(indio_dev); 883 + i2c_set_clientdata(client, indio_dev); 884 + data->client = client; 885 + 886 + if (id) 887 + name = id->name; 888 + else if (ACPI_HANDLE(&client->dev)) 889 + name = bmc150_magn_match_acpi_device(&client->dev); 890 + else 891 + return -ENOSYS; 892 + 893 + mutex_init(&data->mutex); 894 + data->regmap = devm_regmap_init_i2c(client, &bmc150_magn_regmap_config); 895 + if (IS_ERR(data->regmap)) { 896 + dev_err(&client->dev, "Failed to allocate register map\n"); 897 + return PTR_ERR(data->regmap); 898 + } 899 + 900 + ret = bmc150_magn_init(data); 901 + if (ret < 0) 902 + return ret; 903 + 904 + indio_dev->dev.parent = &client->dev; 905 + indio_dev->channels = bmc150_magn_channels; 906 + indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels); 907 + indio_dev->available_scan_masks = bmc150_magn_scan_masks; 908 + indio_dev->name = name; 909 + indio_dev->modes = INDIO_DIRECT_MODE; 910 + indio_dev->info = &bmc150_magn_info; 911 + 912 + if (client->irq <= 0) 913 + client->irq = bmc150_magn_gpio_probe(client); 914 + 915 + if (client->irq > 0) { 916 + data->dready_trig = devm_iio_trigger_alloc(&client->dev, 917 + "%s-dev%d", 918 + indio_dev->name, 919 + indio_dev->id); 920 + if (!data->dready_trig) { 921 + ret = -ENOMEM; 922 + dev_err(&client->dev, "iio trigger alloc failed\n"); 923 + goto err_poweroff; 924 + } 925 + 926 + data->dready_trig->dev.parent = &client->dev; 927 + data->dready_trig->ops = &bmc150_magn_trigger_ops; 928 + iio_trigger_set_drvdata(data->dready_trig, indio_dev); 929 + ret = iio_trigger_register(data->dready_trig); 930 + if (ret) { 931 + dev_err(&client->dev, "iio trigger register failed\n"); 932 + goto err_poweroff; 933 + } 934 + 935 + ret = iio_triggered_buffer_setup(indio_dev, 936 + &iio_pollfunc_store_time, 937 + bmc150_magn_trigger_handler, 938 + NULL); 939 + if (ret < 0) { 940 + dev_err(&client->dev, 941 + "iio triggered buffer setup failed\n"); 942 + goto err_trigger_unregister; 943 + } 944 + 945 + ret = request_threaded_irq(client->irq, 946 + iio_trigger_generic_data_rdy_poll, 947 + NULL, 948 + IRQF_TRIGGER_RISING | IRQF_ONESHOT, 949 + BMC150_MAGN_IRQ_NAME, 950 + data->dready_trig); 951 + if (ret < 0) { 952 + dev_err(&client->dev, "request irq %d failed\n", 953 + client->irq); 954 + goto err_buffer_cleanup; 955 + } 956 + } 957 + 958 + ret = iio_device_register(indio_dev); 959 + if (ret < 0) { 960 + dev_err(&client->dev, "unable to register iio device\n"); 961 + goto err_free_irq; 962 + } 963 + 964 + ret = pm_runtime_set_active(&client->dev); 965 + if (ret) 966 + goto err_iio_unregister; 967 + 968 + pm_runtime_enable(&client->dev); 969 + pm_runtime_set_autosuspend_delay(&client->dev, 970 + BMC150_MAGN_AUTO_SUSPEND_DELAY_MS); 971 + pm_runtime_use_autosuspend(&client->dev); 972 + 973 + dev_dbg(&indio_dev->dev, "Registered device %s\n", name); 974 + 975 + return 0; 976 + 977 + err_iio_unregister: 978 + iio_device_unregister(indio_dev); 979 + err_free_irq: 980 + if (client->irq > 0) 981 + free_irq(client->irq, data->dready_trig); 982 + err_buffer_cleanup: 983 + if (data->dready_trig) 984 + iio_triggered_buffer_cleanup(indio_dev); 985 + err_trigger_unregister: 986 + if (data->dready_trig) 987 + iio_trigger_unregister(data->dready_trig); 988 + err_poweroff: 989 + bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); 990 + return ret; 991 + } 992 + 993 + static int bmc150_magn_remove(struct i2c_client *client) 994 + { 995 + struct iio_dev *indio_dev = i2c_get_clientdata(client); 996 + struct bmc150_magn_data *data = iio_priv(indio_dev); 997 + 998 + pm_runtime_disable(&client->dev); 999 + pm_runtime_set_suspended(&client->dev); 1000 + pm_runtime_put_noidle(&client->dev); 1001 + 1002 + iio_device_unregister(indio_dev); 1003 + 1004 + if (client->irq > 0) 1005 + free_irq(data->client->irq, data->dready_trig); 1006 + 1007 + if (data->dready_trig) { 1008 + iio_triggered_buffer_cleanup(indio_dev); 1009 + iio_trigger_unregister(data->dready_trig); 1010 + } 1011 + 1012 + mutex_lock(&data->mutex); 1013 + bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); 1014 + mutex_unlock(&data->mutex); 1015 + 1016 + return 0; 1017 + } 1018 + 1019 + #ifdef CONFIG_PM 1020 + static int bmc150_magn_runtime_suspend(struct device *dev) 1021 + { 1022 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1023 + struct bmc150_magn_data *data = iio_priv(indio_dev); 1024 + int ret; 1025 + 1026 + mutex_lock(&data->mutex); 1027 + ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP, 1028 + true); 1029 + mutex_unlock(&data->mutex); 1030 + if (ret < 0) { 1031 + dev_err(&data->client->dev, "powering off device failed\n"); 1032 + return ret; 1033 + } 1034 + return 0; 1035 + } 1036 + 1037 + static int bmc150_magn_runtime_resume(struct device *dev) 1038 + { 1039 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1040 + struct bmc150_magn_data *data = iio_priv(indio_dev); 1041 + 1042 + return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, 1043 + true); 1044 + } 1045 + #endif 1046 + 1047 + #ifdef CONFIG_PM_SLEEP 1048 + static int bmc150_magn_suspend(struct device *dev) 1049 + { 1050 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1051 + struct bmc150_magn_data *data = iio_priv(indio_dev); 1052 + int ret; 1053 + 1054 + mutex_lock(&data->mutex); 1055 + ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP, 1056 + true); 1057 + mutex_unlock(&data->mutex); 1058 + 1059 + return ret; 1060 + } 1061 + 1062 + static int bmc150_magn_resume(struct device *dev) 1063 + { 1064 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1065 + struct bmc150_magn_data *data = iio_priv(indio_dev); 1066 + int ret; 1067 + 1068 + mutex_lock(&data->mutex); 1069 + ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, 1070 + true); 1071 + mutex_unlock(&data->mutex); 1072 + 1073 + return ret; 1074 + } 1075 + #endif 1076 + 1077 + static const struct dev_pm_ops bmc150_magn_pm_ops = { 1078 + SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume) 1079 + SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend, 1080 + bmc150_magn_runtime_resume, NULL) 1081 + }; 1082 + 1083 + static const struct acpi_device_id bmc150_magn_acpi_match[] = { 1084 + {"BMC150B", 0}, 1085 + {}, 1086 + }; 1087 + MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match); 1088 + 1089 + static const struct i2c_device_id bmc150_magn_id[] = { 1090 + {"bmc150_magn", 0}, 1091 + {}, 1092 + }; 1093 + MODULE_DEVICE_TABLE(i2c, bmc150_magn_id); 1094 + 1095 + static struct i2c_driver bmc150_magn_driver = { 1096 + .driver = { 1097 + .name = BMC150_MAGN_DRV_NAME, 1098 + .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match), 1099 + .pm = &bmc150_magn_pm_ops, 1100 + }, 1101 + .probe = bmc150_magn_probe, 1102 + .remove = bmc150_magn_remove, 1103 + .id_table = bmc150_magn_id, 1104 + }; 1105 + module_i2c_driver(bmc150_magn_driver); 1106 + 1107 + MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>"); 1108 + MODULE_LICENSE("GPL v2"); 1109 + MODULE_DESCRIPTION("BMC150 magnetometer driver");
+1 -1
drivers/iio/magnetometer/hid-sensor-magn-3d.c
··· 510 510 return 0; 511 511 } 512 512 513 - static struct platform_device_id hid_magn_3d_ids[] = { 513 + static const struct platform_device_id hid_magn_3d_ids[] = { 514 514 { 515 515 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 516 516 .name = "HID-SENSOR-200083",
+512
drivers/iio/magnetometer/mmc35240.c
··· 1 + /* 2 + * MMC35240 - MEMSIC 3-axis Magnetic Sensor 3 + * 4 + * Copyright (c) 2015, Intel Corporation. 5 + * 6 + * This file is subject to the terms and conditions of version 2 of 7 + * the GNU General Public License. See the file COPYING in the main 8 + * directory of this archive for more details. 9 + * 10 + * IIO driver for MMC35240 (7-bit I2C slave address 0x30). 11 + * 12 + * TODO: offset, ACPI, continuous measurement mode, PM 13 + */ 14 + 15 + #include <linux/module.h> 16 + #include <linux/init.h> 17 + #include <linux/i2c.h> 18 + #include <linux/delay.h> 19 + #include <linux/regmap.h> 20 + #include <linux/acpi.h> 21 + #include <linux/pm.h> 22 + 23 + #include <linux/iio/iio.h> 24 + #include <linux/iio/sysfs.h> 25 + 26 + #define MMC35240_DRV_NAME "mmc35240" 27 + #define MMC35240_REGMAP_NAME "mmc35240_regmap" 28 + 29 + #define MMC35240_REG_XOUT_L 0x00 30 + #define MMC35240_REG_XOUT_H 0x01 31 + #define MMC35240_REG_YOUT_L 0x02 32 + #define MMC35240_REG_YOUT_H 0x03 33 + #define MMC35240_REG_ZOUT_L 0x04 34 + #define MMC35240_REG_ZOUT_H 0x05 35 + 36 + #define MMC35240_REG_STATUS 0x06 37 + #define MMC35240_REG_CTRL0 0x07 38 + #define MMC35240_REG_CTRL1 0x08 39 + 40 + #define MMC35240_REG_ID 0x20 41 + 42 + #define MMC35240_STATUS_MEAS_DONE_BIT BIT(0) 43 + 44 + #define MMC35240_CTRL0_REFILL_BIT BIT(7) 45 + #define MMC35240_CTRL0_RESET_BIT BIT(6) 46 + #define MMC35240_CTRL0_SET_BIT BIT(5) 47 + #define MMC35240_CTRL0_CMM_BIT BIT(1) 48 + #define MMC35240_CTRL0_TM_BIT BIT(0) 49 + 50 + /* output resolution bits */ 51 + #define MMC35240_CTRL1_BW0_BIT BIT(0) 52 + #define MMC35240_CTRL1_BW1_BIT BIT(1) 53 + 54 + #define MMC35240_CTRL1_BW_MASK (MMC35240_CTRL1_BW0_BIT | \ 55 + MMC35240_CTRL1_BW1_BIT) 56 + #define MMC35240_CTRL1_BW_SHIFT 0 57 + 58 + #define MMC35240_WAIT_CHARGE_PUMP 50000 /* us */ 59 + #define MMC53240_WAIT_SET_RESET 1000 /* us */ 60 + 61 + enum mmc35240_resolution { 62 + MMC35240_16_BITS_SLOW = 0, /* 100 Hz */ 63 + MMC35240_16_BITS_FAST, /* 200 Hz */ 64 + MMC35240_14_BITS, /* 333 Hz */ 65 + MMC35240_12_BITS, /* 666 Hz */ 66 + }; 67 + 68 + enum mmc35240_axis { 69 + AXIS_X = 0, 70 + AXIS_Y, 71 + AXIS_Z, 72 + }; 73 + 74 + static const struct { 75 + int sens[3]; /* sensitivity per X, Y, Z axis */ 76 + int nfo; /* null field output */ 77 + } mmc35240_props_table[] = { 78 + /* 16 bits, 100Hz ODR */ 79 + { 80 + {1024, 1024, 770}, 81 + 32768, 82 + }, 83 + /* 16 bits, 200Hz ODR */ 84 + { 85 + {1024, 1024, 770}, 86 + 32768, 87 + }, 88 + /* 14 bits, 333Hz ODR */ 89 + { 90 + {256, 256, 193}, 91 + 8192, 92 + }, 93 + /* 12 bits, 666Hz ODR */ 94 + { 95 + {64, 64, 48}, 96 + 2048, 97 + }, 98 + }; 99 + 100 + struct mmc35240_data { 101 + struct i2c_client *client; 102 + struct mutex mutex; 103 + struct regmap *regmap; 104 + enum mmc35240_resolution res; 105 + }; 106 + 107 + static const int mmc35240_samp_freq[] = {100, 200, 333, 666}; 108 + 109 + static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 333 666"); 110 + 111 + #define MMC35240_CHANNEL(_axis) { \ 112 + .type = IIO_MAGN, \ 113 + .modified = 1, \ 114 + .channel2 = IIO_MOD_ ## _axis, \ 115 + .address = AXIS_ ## _axis, \ 116 + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \ 117 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 118 + } 119 + 120 + static const struct iio_chan_spec mmc35240_channels[] = { 121 + MMC35240_CHANNEL(X), 122 + MMC35240_CHANNEL(Y), 123 + MMC35240_CHANNEL(Z), 124 + }; 125 + 126 + static struct attribute *mmc35240_attributes[] = { 127 + &iio_const_attr_sampling_frequency_available.dev_attr.attr, 128 + }; 129 + 130 + static const struct attribute_group mmc35240_attribute_group = { 131 + .attrs = mmc35240_attributes, 132 + }; 133 + 134 + static int mmc35240_get_samp_freq_index(struct mmc35240_data *data, 135 + int val, int val2) 136 + { 137 + int i; 138 + 139 + for (i = 0; i < ARRAY_SIZE(mmc35240_samp_freq); i++) 140 + if (mmc35240_samp_freq[i] == val) 141 + return i; 142 + return -EINVAL; 143 + } 144 + 145 + static int mmc35240_hw_set(struct mmc35240_data *data, bool set) 146 + { 147 + int ret; 148 + u8 coil_bit; 149 + 150 + /* 151 + * Recharge the capacitor at VCAP pin, requested to be issued 152 + * before a SET/RESET command. 153 + */ 154 + ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL0, 155 + MMC35240_CTRL0_REFILL_BIT, 156 + MMC35240_CTRL0_REFILL_BIT); 157 + if (ret < 0) 158 + return ret; 159 + usleep_range(MMC35240_WAIT_CHARGE_PUMP, MMC35240_WAIT_CHARGE_PUMP + 1); 160 + 161 + if (set) 162 + coil_bit = MMC35240_CTRL0_SET_BIT; 163 + else 164 + coil_bit = MMC35240_CTRL0_RESET_BIT; 165 + 166 + return regmap_update_bits(data->regmap, MMC35240_REG_CTRL0, 167 + MMC35240_CTRL0_REFILL_BIT, 168 + coil_bit); 169 + } 170 + 171 + static int mmc35240_init(struct mmc35240_data *data) 172 + { 173 + int ret; 174 + unsigned int reg_id; 175 + 176 + ret = regmap_read(data->regmap, MMC35240_REG_ID, &reg_id); 177 + if (ret < 0) { 178 + dev_err(&data->client->dev, "Error reading product id\n"); 179 + return ret; 180 + } 181 + 182 + dev_dbg(&data->client->dev, "MMC35240 chip id %x\n", reg_id); 183 + 184 + /* 185 + * make sure we restore sensor characteristics, by doing 186 + * a RESET/SET sequence 187 + */ 188 + ret = mmc35240_hw_set(data, false); 189 + if (ret < 0) 190 + return ret; 191 + usleep_range(MMC53240_WAIT_SET_RESET, MMC53240_WAIT_SET_RESET + 1); 192 + 193 + ret = mmc35240_hw_set(data, true); 194 + if (ret < 0) 195 + return ret; 196 + 197 + /* set default sampling frequency */ 198 + return regmap_update_bits(data->regmap, MMC35240_REG_CTRL1, 199 + MMC35240_CTRL1_BW_MASK, 200 + data->res << MMC35240_CTRL1_BW_SHIFT); 201 + } 202 + 203 + static int mmc35240_take_measurement(struct mmc35240_data *data) 204 + { 205 + int ret, tries = 100; 206 + unsigned int reg_status; 207 + 208 + ret = regmap_write(data->regmap, MMC35240_REG_CTRL0, 209 + MMC35240_CTRL0_TM_BIT); 210 + if (ret < 0) 211 + return ret; 212 + 213 + while (tries-- > 0) { 214 + ret = regmap_read(data->regmap, MMC35240_REG_STATUS, 215 + &reg_status); 216 + if (ret < 0) 217 + return ret; 218 + if (reg_status & MMC35240_STATUS_MEAS_DONE_BIT) 219 + break; 220 + msleep(20); 221 + } 222 + 223 + if (tries < 0) { 224 + dev_err(&data->client->dev, "data not ready\n"); 225 + return -EIO; 226 + } 227 + 228 + return 0; 229 + } 230 + 231 + static int mmc35240_read_measurement(struct mmc35240_data *data, __le16 buf[3]) 232 + { 233 + int ret; 234 + 235 + ret = mmc35240_take_measurement(data); 236 + if (ret < 0) 237 + return ret; 238 + 239 + return regmap_bulk_read(data->regmap, MMC35240_REG_XOUT_L, (u8 *)buf, 240 + 3 * sizeof(__le16)); 241 + } 242 + 243 + static int mmc35240_raw_to_gauss(struct mmc35240_data *data, int index, 244 + __le16 buf[], 245 + int *val, int *val2) 246 + { 247 + int raw_x, raw_y, raw_z; 248 + int sens_x, sens_y, sens_z; 249 + int nfo; 250 + 251 + raw_x = le16_to_cpu(buf[AXIS_X]); 252 + raw_y = le16_to_cpu(buf[AXIS_Y]); 253 + raw_z = le16_to_cpu(buf[AXIS_Z]); 254 + 255 + sens_x = mmc35240_props_table[data->res].sens[AXIS_X]; 256 + sens_y = mmc35240_props_table[data->res].sens[AXIS_Y]; 257 + sens_z = mmc35240_props_table[data->res].sens[AXIS_Z]; 258 + 259 + nfo = mmc35240_props_table[data->res].nfo; 260 + 261 + switch (index) { 262 + case AXIS_X: 263 + *val = (raw_x - nfo) / sens_x; 264 + *val2 = ((raw_x - nfo) % sens_x) * 1000000; 265 + break; 266 + case AXIS_Y: 267 + *val = (raw_y - nfo) / sens_y - (raw_z - nfo) / sens_z; 268 + *val2 = (((raw_y - nfo) % sens_y - (raw_z - nfo) % sens_z)) 269 + * 1000000; 270 + break; 271 + case AXIS_Z: 272 + *val = (raw_y - nfo) / sens_y + (raw_z - nfo) / sens_z; 273 + *val2 = (((raw_y - nfo) % sens_y + (raw_z - nfo) % sens_z)) 274 + * 1000000; 275 + break; 276 + default: 277 + return -EINVAL; 278 + } 279 + return 0; 280 + } 281 + 282 + static int mmc35240_read_raw(struct iio_dev *indio_dev, 283 + struct iio_chan_spec const *chan, int *val, 284 + int *val2, long mask) 285 + { 286 + struct mmc35240_data *data = iio_priv(indio_dev); 287 + int ret, i; 288 + unsigned int reg; 289 + __le16 buf[3]; 290 + 291 + switch (mask) { 292 + case IIO_CHAN_INFO_PROCESSED: 293 + mutex_lock(&data->mutex); 294 + ret = mmc35240_read_measurement(data, buf); 295 + mutex_unlock(&data->mutex); 296 + if (ret < 0) 297 + return ret; 298 + ret = mmc35240_raw_to_gauss(data, chan->address, 299 + buf, val, val2); 300 + if (ret < 0) 301 + return ret; 302 + return IIO_VAL_INT_PLUS_MICRO; 303 + case IIO_CHAN_INFO_SAMP_FREQ: 304 + mutex_lock(&data->mutex); 305 + ret = regmap_read(data->regmap, MMC35240_REG_CTRL1, &reg); 306 + mutex_unlock(&data->mutex); 307 + if (ret < 0) 308 + return ret; 309 + 310 + i = (reg & MMC35240_CTRL1_BW_MASK) >> MMC35240_CTRL1_BW_SHIFT; 311 + if (i < 0 || i > ARRAY_SIZE(mmc35240_samp_freq)) 312 + return -EINVAL; 313 + 314 + *val = mmc35240_samp_freq[i]; 315 + *val2 = 0; 316 + return IIO_VAL_INT; 317 + default: 318 + return -EINVAL; 319 + } 320 + } 321 + 322 + static int mmc35240_write_raw(struct iio_dev *indio_dev, 323 + struct iio_chan_spec const *chan, int val, 324 + int val2, long mask) 325 + { 326 + struct mmc35240_data *data = iio_priv(indio_dev); 327 + int i, ret; 328 + 329 + switch (mask) { 330 + case IIO_CHAN_INFO_SAMP_FREQ: 331 + i = mmc35240_get_samp_freq_index(data, val, val2); 332 + if (i < 0) 333 + return -EINVAL; 334 + mutex_lock(&data->mutex); 335 + ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL1, 336 + MMC35240_CTRL1_BW_MASK, 337 + i << MMC35240_CTRL1_BW_SHIFT); 338 + mutex_unlock(&data->mutex); 339 + return ret; 340 + default: 341 + return -EINVAL; 342 + } 343 + } 344 + 345 + static const struct iio_info mmc35240_info = { 346 + .driver_module = THIS_MODULE, 347 + .read_raw = mmc35240_read_raw, 348 + .write_raw = mmc35240_write_raw, 349 + .attrs = &mmc35240_attribute_group, 350 + }; 351 + 352 + static bool mmc35240_is_writeable_reg(struct device *dev, unsigned int reg) 353 + { 354 + switch (reg) { 355 + case MMC35240_REG_CTRL0: 356 + case MMC35240_REG_CTRL1: 357 + return true; 358 + default: 359 + return false; 360 + } 361 + } 362 + 363 + static bool mmc35240_is_readable_reg(struct device *dev, unsigned int reg) 364 + { 365 + switch (reg) { 366 + case MMC35240_REG_XOUT_L: 367 + case MMC35240_REG_XOUT_H: 368 + case MMC35240_REG_YOUT_L: 369 + case MMC35240_REG_YOUT_H: 370 + case MMC35240_REG_ZOUT_L: 371 + case MMC35240_REG_ZOUT_H: 372 + case MMC35240_REG_STATUS: 373 + case MMC35240_REG_ID: 374 + return true; 375 + default: 376 + return false; 377 + } 378 + } 379 + 380 + static bool mmc35240_is_volatile_reg(struct device *dev, unsigned int reg) 381 + { 382 + switch (reg) { 383 + case MMC35240_REG_CTRL0: 384 + case MMC35240_REG_CTRL1: 385 + return false; 386 + default: 387 + return true; 388 + } 389 + } 390 + 391 + static struct reg_default mmc35240_reg_defaults[] = { 392 + { MMC35240_REG_CTRL0, 0x00 }, 393 + { MMC35240_REG_CTRL1, 0x00 }, 394 + }; 395 + 396 + static const struct regmap_config mmc35240_regmap_config = { 397 + .name = MMC35240_REGMAP_NAME, 398 + 399 + .reg_bits = 8, 400 + .val_bits = 8, 401 + 402 + .max_register = MMC35240_REG_ID, 403 + .cache_type = REGCACHE_FLAT, 404 + 405 + .writeable_reg = mmc35240_is_writeable_reg, 406 + .readable_reg = mmc35240_is_readable_reg, 407 + .volatile_reg = mmc35240_is_volatile_reg, 408 + 409 + .reg_defaults = mmc35240_reg_defaults, 410 + .num_reg_defaults = ARRAY_SIZE(mmc35240_reg_defaults), 411 + }; 412 + 413 + static int mmc35240_probe(struct i2c_client *client, 414 + const struct i2c_device_id *id) 415 + { 416 + struct mmc35240_data *data; 417 + struct iio_dev *indio_dev; 418 + struct regmap *regmap; 419 + int ret; 420 + 421 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 422 + if (!indio_dev) 423 + return -ENOMEM; 424 + 425 + regmap = devm_regmap_init_i2c(client, &mmc35240_regmap_config); 426 + if (IS_ERR(regmap)) { 427 + dev_err(&client->dev, "regmap initialization failed\n"); 428 + return PTR_ERR(regmap); 429 + } 430 + 431 + data = iio_priv(indio_dev); 432 + data->client = client; 433 + data->regmap = regmap; 434 + data->res = MMC35240_16_BITS_SLOW; 435 + 436 + mutex_init(&data->mutex); 437 + 438 + indio_dev->dev.parent = &client->dev; 439 + indio_dev->info = &mmc35240_info; 440 + indio_dev->name = MMC35240_DRV_NAME; 441 + indio_dev->channels = mmc35240_channels; 442 + indio_dev->num_channels = ARRAY_SIZE(mmc35240_channels); 443 + indio_dev->modes = INDIO_DIRECT_MODE; 444 + 445 + ret = mmc35240_init(data); 446 + if (ret < 0) { 447 + dev_err(&client->dev, "mmc35240 chip init failed\n"); 448 + return ret; 449 + } 450 + return devm_iio_device_register(&client->dev, indio_dev); 451 + } 452 + 453 + #ifdef CONFIG_PM_SLEEP 454 + static int mmc35240_suspend(struct device *dev) 455 + { 456 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 457 + struct mmc35240_data *data = iio_priv(indio_dev); 458 + 459 + regcache_cache_only(data->regmap, true); 460 + 461 + return 0; 462 + } 463 + 464 + static int mmc35240_resume(struct device *dev) 465 + { 466 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 467 + struct mmc35240_data *data = iio_priv(indio_dev); 468 + int ret; 469 + 470 + regcache_mark_dirty(data->regmap); 471 + ret = regcache_sync_region(data->regmap, MMC35240_REG_CTRL0, 472 + MMC35240_REG_CTRL1); 473 + if (ret < 0) 474 + dev_err(dev, "Failed to restore control registers\n"); 475 + 476 + regcache_cache_only(data->regmap, false); 477 + 478 + return 0; 479 + } 480 + #endif 481 + 482 + static const struct dev_pm_ops mmc35240_pm_ops = { 483 + SET_SYSTEM_SLEEP_PM_OPS(mmc35240_suspend, mmc35240_resume) 484 + }; 485 + 486 + static const struct acpi_device_id mmc35240_acpi_match[] = { 487 + {"MMC35240", 0}, 488 + { }, 489 + }; 490 + MODULE_DEVICE_TABLE(acpi, mmc35240_acpi_match); 491 + 492 + static const struct i2c_device_id mmc35240_id[] = { 493 + {"MMC35240", 0}, 494 + {} 495 + }; 496 + MODULE_DEVICE_TABLE(i2c, mmc35240_id); 497 + 498 + static struct i2c_driver mmc35240_driver = { 499 + .driver = { 500 + .name = MMC35240_DRV_NAME, 501 + .pm = &mmc35240_pm_ops, 502 + .acpi_match_table = ACPI_PTR(mmc35240_acpi_match), 503 + }, 504 + .probe = mmc35240_probe, 505 + .id_table = mmc35240_id, 506 + }; 507 + 508 + module_i2c_driver(mmc35240_driver); 509 + 510 + MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>"); 511 + MODULE_DESCRIPTION("MEMSIC MMC35240 magnetic sensor driver"); 512 + MODULE_LICENSE("GPL v2");
+8 -8
drivers/iio/orientation/hid-sensor-incl-3d.c
··· 315 315 struct iio_dev *indio_dev; 316 316 struct incl_3d_state *incl_state; 317 317 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 318 - struct iio_chan_spec *channels; 319 318 320 319 indio_dev = devm_iio_device_alloc(&pdev->dev, 321 320 sizeof(struct incl_3d_state)); ··· 335 336 return ret; 336 337 } 337 338 338 - channels = kmemdup(incl_3d_channels, sizeof(incl_3d_channels), 339 - GFP_KERNEL); 340 - if (!channels) { 339 + indio_dev->channels = kmemdup(incl_3d_channels, 340 + sizeof(incl_3d_channels), GFP_KERNEL); 341 + if (!indio_dev->channels) { 341 342 dev_err(&pdev->dev, "failed to duplicate channels\n"); 342 343 return -ENOMEM; 343 344 } 344 345 345 - ret = incl_3d_parse_report(pdev, hsdev, channels, 346 - HID_USAGE_SENSOR_INCLINOMETER_3D, incl_state); 346 + ret = incl_3d_parse_report(pdev, hsdev, 347 + (struct iio_chan_spec *)indio_dev->channels, 348 + HID_USAGE_SENSOR_INCLINOMETER_3D, 349 + incl_state); 347 350 if (ret) { 348 351 dev_err(&pdev->dev, "failed to setup attributes\n"); 349 352 goto error_free_dev_mem; 350 353 } 351 354 352 - indio_dev->channels = channels; 353 355 indio_dev->num_channels = ARRAY_SIZE(incl_3d_channels); 354 356 indio_dev->dev.parent = &pdev->dev; 355 357 indio_dev->info = &incl_3d_info; ··· 417 417 return 0; 418 418 } 419 419 420 - static struct platform_device_id hid_incl_3d_ids[] = { 420 + static const struct platform_device_id hid_incl_3d_ids[] = { 421 421 { 422 422 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 423 423 .name = "HID-SENSOR-200086",
+9 -8
drivers/iio/orientation/hid-sensor-rotation.c
··· 222 222 struct iio_dev *indio_dev; 223 223 struct dev_rot_state *rot_state; 224 224 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 225 - struct iio_chan_spec *channels; 226 225 227 226 indio_dev = devm_iio_device_alloc(&pdev->dev, 228 227 sizeof(struct dev_rot_state)); ··· 242 243 return ret; 243 244 } 244 245 245 - channels = devm_kmemdup(&pdev->dev, dev_rot_channels, 246 - sizeof(dev_rot_channels), GFP_KERNEL); 247 - if (!channels) { 246 + indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels, 247 + sizeof(dev_rot_channels), 248 + GFP_KERNEL); 249 + if (!indio_dev->channels) { 248 250 dev_err(&pdev->dev, "failed to duplicate channels\n"); 249 251 return -ENOMEM; 250 252 } 251 253 252 - ret = dev_rot_parse_report(pdev, hsdev, channels, 253 - HID_USAGE_SENSOR_DEVICE_ORIENTATION, rot_state); 254 + ret = dev_rot_parse_report(pdev, hsdev, 255 + (struct iio_chan_spec *)indio_dev->channels, 256 + HID_USAGE_SENSOR_DEVICE_ORIENTATION, 257 + rot_state); 254 258 if (ret) { 255 259 dev_err(&pdev->dev, "failed to setup attributes\n"); 256 260 return ret; 257 261 } 258 262 259 - indio_dev->channels = channels; 260 263 indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels); 261 264 indio_dev->dev.parent = &pdev->dev; 262 265 indio_dev->info = &dev_rot_info; ··· 322 321 return 0; 323 322 } 324 323 325 - static struct platform_device_id hid_dev_rot_ids[] = { 324 + static const struct platform_device_id hid_dev_rot_ids[] = { 326 325 { 327 326 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 328 327 .name = "HID-SENSOR-20008a",
+7 -7
drivers/iio/pressure/hid-sensor-press.c
··· 260 260 struct iio_dev *indio_dev; 261 261 struct press_state *press_state; 262 262 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 263 - struct iio_chan_spec *channels; 264 263 265 264 indio_dev = devm_iio_device_alloc(&pdev->dev, 266 265 sizeof(struct press_state)); ··· 279 280 return ret; 280 281 } 281 282 282 - channels = kmemdup(press_channels, sizeof(press_channels), GFP_KERNEL); 283 - if (!channels) { 283 + indio_dev->channels = kmemdup(press_channels, sizeof(press_channels), 284 + GFP_KERNEL); 285 + if (!indio_dev->channels) { 284 286 dev_err(&pdev->dev, "failed to duplicate channels\n"); 285 287 return -ENOMEM; 286 288 } 287 289 288 - ret = press_parse_report(pdev, hsdev, channels, 289 - HID_USAGE_SENSOR_PRESSURE, press_state); 290 + ret = press_parse_report(pdev, hsdev, 291 + (struct iio_chan_spec *)indio_dev->channels, 292 + HID_USAGE_SENSOR_PRESSURE, press_state); 290 293 if (ret) { 291 294 dev_err(&pdev->dev, "failed to setup attributes\n"); 292 295 goto error_free_dev_mem; 293 296 } 294 297 295 - indio_dev->channels = channels; 296 298 indio_dev->num_channels = 297 299 ARRAY_SIZE(press_channels); 298 300 indio_dev->dev.parent = &pdev->dev; ··· 360 360 return 0; 361 361 } 362 362 363 - static struct platform_device_id hid_press_ids[] = { 363 + static const struct platform_device_id hid_press_ids[] = { 364 364 { 365 365 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 366 366 .name = "HID-SENSOR-200031",
+1 -3
drivers/iio/temperature/mlx90614.c
··· 254 254 mutex_unlock(&data->lock); 255 255 mlx90614_power_put(data); 256 256 257 - if (ret < 0) 258 - return ret; 259 - return 0; 257 + return ret; 260 258 default: 261 259 return -EINVAL; 262 260 }
+1 -1
drivers/staging/iio/accel/Kconfig
··· 79 79 depends on SPI 80 80 select IIO_TRIGGER if IIO_BUFFER 81 81 depends on !IIO_BUFFER || IIO_KFIFO_BUF 82 - depends on GPIOLIB 82 + depends on GPIOLIB || COMPILE_TEST 83 83 help 84 84 Say Y here to build SPI support for the ST microelectronics 85 85 accelerometer. The driver supplies direct access via sysfs files
+3 -3
drivers/staging/iio/adc/Kconfig
··· 5 5 6 6 config AD7606 7 7 tristate "Analog Devices AD7606 ADC driver" 8 - depends on GPIOLIB 8 + depends on GPIOLIB || COMPILE_TEST 9 9 select IIO_BUFFER 10 10 select IIO_TRIGGERED_BUFFER 11 11 help ··· 39 39 config AD7780 40 40 tristate "Analog Devices AD7780 and similar ADCs driver" 41 41 depends on SPI 42 - depends on GPIOLIB 42 + depends on GPIOLIB || COMPILE_TEST 43 43 select AD_SIGMA_DELTA 44 44 help 45 45 Say yes here to build support for Analog Devices AD7170, AD7171, ··· 52 52 config AD7816 53 53 tristate "Analog Devices AD7816/7/8 temperature sensor and ADC driver" 54 54 depends on SPI 55 - depends on GPIOLIB 55 + depends on GPIOLIB || COMPILE_TEST 56 56 help 57 57 Say yes here to build support for Analog Devices AD7816/7/8 58 58 temperature sensors and ADC.
+1 -1
drivers/staging/iio/addac/Kconfig
··· 5 5 6 6 config ADT7316 7 7 tristate "Analog Devices ADT7316/7/8 ADT7516/7/9 temperature sensor, ADC and DAC driver" 8 - depends on GPIOLIB 8 + depends on GPIOLIB || COMPILE_TEST 9 9 help 10 10 Say yes here to build support for Analog Devices ADT7316, ADT7317, ADT7318 11 11 and ADT7516, ADT7517, ADT7519 temperature sensors, ADC and DAC.
+2 -2
drivers/staging/iio/resolver/Kconfig
··· 16 16 config AD2S1200 17 17 tristate "Analog Devices ad2s1200/ad2s1205 driver" 18 18 depends on SPI 19 - depends on GPIOLIB 19 + depends on GPIOLIB || COMPILE_TEST 20 20 help 21 21 Say yes here to build support for Analog Devices spi resolver 22 22 to digital converters, ad2s1200 and ad2s1205, provides direct access ··· 28 28 config AD2S1210 29 29 tristate "Analog Devices ad2s1210 driver" 30 30 depends on SPI 31 - depends on GPIOLIB 31 + depends on GPIOLIB || COMPILE_TEST 32 32 help 33 33 Say yes here to build support for Analog Devices spi resolver 34 34 to digital converters, ad2s1210, provides direct access via sysfs.
+2
include/linux/iio/iio.h
··· 32 32 IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW, 33 33 IIO_CHAN_INFO_AVERAGE_RAW, 34 34 IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY, 35 + IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY, 35 36 IIO_CHAN_INFO_SAMP_FREQ, 36 37 IIO_CHAN_INFO_FREQUENCY, 37 38 IIO_CHAN_INFO_PHASE, ··· 45 44 IIO_CHAN_INFO_DEBOUNCE_COUNT, 46 45 IIO_CHAN_INFO_DEBOUNCE_TIME, 47 46 IIO_CHAN_INFO_CALIBEMISSIVITY, 47 + IIO_CHAN_INFO_OVERSAMPLING_RATIO, 48 48 }; 49 49 50 50 enum iio_shared_by {
+2
include/linux/iio/types.h
··· 17 17 IIO_EV_INFO_VALUE, 18 18 IIO_EV_INFO_HYSTERESIS, 19 19 IIO_EV_INFO_PERIOD, 20 + IIO_EV_INFO_HIGH_PASS_FILTER_3DB, 21 + IIO_EV_INFO_LOW_PASS_FILTER_3DB, 20 22 }; 21 23 22 24 #define IIO_VAL_INT 1
+2
include/uapi/linux/iio/types.h
··· 70 70 IIO_MOD_WALKING, 71 71 IIO_MOD_STILL, 72 72 IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z, 73 + IIO_MOD_I, 74 + IIO_MOD_Q, 73 75 }; 74 76 75 77 enum iio_event_type {
+2 -2
tools/iio/Makefile
··· 1 - CC = gcc 2 - CFLAGS = -Wall -g -D_GNU_SOURCE 1 + CC = $(CROSS_COMPILE)gcc 2 + CFLAGS += -Wall -g -D_GNU_SOURCE 3 3 4 4 all: iio_event_monitor lsiio generic_buffer 5 5