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

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

Jonathan writes:

Second round of new device support, cleanups and fixes for IIO in the 4.10 cycle

This includes two branch merges for elements that may also go via MFD.

New device support
* cros_ec
- new driver to support these Chrome OS contiguous sensors which are behind
the Chrome OS embedded controller. Requires a few minor MFD and chrome
platform changes. One follow up fix deals with some dependency issues in
Kconfig.
* mpu-3050
- new driver and device tree bindings for this venerable device.
* st_accel
- support for the lng2dm an

Driver features
* ad7192
- Add DVdd regulator handling
* ad9832
- Add DVDD regulator handling
* at91
- Suspend and resume support
* si7020
- Device tree bindings
* ti-am335x
- DMA support - uses dma to accelerate short bursts of read back rather
than full blown DMA buffer support. Greatly improved performance.
Includes an MFD addition to give access to the address needed for DMA.
* tsl2583
- Device tree bindings

Cleanups and minor fixes
* ad7192
- Fix regulator naming to match datasheet
- Handle regulator errors correctly (so as to not break deferred probing)
- Rename reg variable to reflect which regulator it is
* ad5933
- Fix regulator naming to match datasheet
- Handle regulator errors correctly (so as to not break deferred probing)
* ad7746
- Fix a missing return value (fallout from previous patch set)
* ad7780
- Fix regulator naming to match datasheet
- Handle regulator errors correctly (so as to not break deferred probing)
* ad9832
- Fix regulator naming to match datasheet
- Handle regulator errors correctly (so as to not break deferred probing)
- Rename reg regulator to reflect which one it is
* ad9834
- Fix regulator naming to match datasheet
- Handle regulator errors correctly (so as to not break deferred probing)
* hts221
- Remove a duplicated include
* maxim thermocouple
- Handle a wrong storage side in read function. Prevent any problems that
might be introduced by additions to this driver in future.
* tsl2583 - big set from Brian Masney to drive this towards a staging
graduation.
- Convert to iio_chan_spec and read_raw / write_raw (in a couple of steps)
- Improved error handling in various functions
- Drop redundant power_state custom sysfs attribute.
- Use IIO_*_ATTR* macros for remaining attributes.
- Return an error code to userspace on invalid parameters being writen to
sysfs files.
- Add locking to various attribute accesses to remove possible races.
- Add defines for various magic numbers.
- Use smbus_read_byte_data instead of a write_byte followed by read_byte.
- Query only relevant registers in probe.
- Tidy up ordering of code comments.
- Remove a pointless power off sequence in taos_chip_on.
- Don't bother shutting down the chip when updating the lux table.
The table is held entirely in the driver and doesn't effect the chip at all.
- Drop a redundant i2c call in taos_als_calibrate where the same register
is read twice in a row.

+3656 -445
+18
Documentation/ABI/testing/sysfs-bus-iio-cros-ec
··· 1 + What: /sys/bus/iio/devices/iio:deviceX/calibrate 2 + Date: July 2015 3 + KernelVersion: 4.7 4 + Contact: linux-iio@vger.kernel.org 5 + Description: 6 + Writing '1' will perform a FOC (Fast Online Calibration). The 7 + corresponding calibration offsets can be read from *_calibbias 8 + entries. 9 + 10 + What: /sys/bus/iio/devices/iio:deviceX/location 11 + Date: July 2015 12 + KernelVersion: 4.7 13 + Contact: linux-iio@vger.kernel.org 14 + Description: 15 + This attribute returns a string with the physical location where 16 + the motion sensor is placed. For example, in a laptop a motion 17 + sensor can be located on the base or on the lid. Current valid 18 + values are 'base' and 'lid'.
+1
Documentation/devicetree/bindings/i2c/trivial-devices.txt
··· 152 152 samsung,24ad0xd1 S524AD0XF1 (128K/256K-bit Serial EEPROM for Low Power) 153 153 sgx,vz89x SGX Sensortech VZ89X Sensors 154 154 sii,s35390a 2-wire CMOS real-time clock 155 + silabs,si7020 Relative Humidity and Temperature Sensors 155 156 skyworks,sky81452 Skyworks SKY81452: Six-Channel White LED Driver with Touch Panel Bias Supply 156 157 st,24c256 i2c serial eeprom (24cxx) 157 158 st,m41t00 Serial real-time clock (RTC)
+46
Documentation/devicetree/bindings/iio/gyroscope/invensense,mpu3050.txt
··· 1 + Invensense MPU-3050 Gyroscope device tree bindings 2 + 3 + Required properties: 4 + - compatible : should be "invensense,mpu3050" 5 + - reg : the I2C address of the sensor 6 + 7 + Optional properties: 8 + - interrupt-parent : should be the phandle for the interrupt controller 9 + - interrupts : interrupt mapping for the trigger interrupt from the 10 + internal oscillator. The following IRQ modes are supported: 11 + IRQ_TYPE_EDGE_RISING, IRQ_TYPE_EDGE_FALLING, IRQ_TYPE_LEVEL_HIGH and 12 + IRQ_TYPE_LEVEL_LOW. The driver should detect and configure the hardware 13 + for the desired interrupt type. 14 + - vdd-supply : supply regulator for the main power voltage. 15 + - vlogic-supply : supply regulator for the signal voltage. 16 + - mount-matrix : see iio/mount-matrix.txt 17 + 18 + Optional subnodes: 19 + - The MPU-3050 will pass through and forward the I2C signals from the 20 + incoming I2C bus, alternatively drive traffic to a slave device (usually 21 + an accelerometer) on its own initiative. Therefore is supports a subnode 22 + i2c gate node. For details see: i2c/i2c-gate.txt 23 + 24 + Example: 25 + 26 + mpu3050@68 { 27 + compatible = "invensense,mpu3050"; 28 + reg = <0x68>; 29 + interrupt-parent = <&foo>; 30 + interrupts = <12 IRQ_TYPE_EDGE_FALLING>; 31 + vdd-supply = <&bar>; 32 + vlogic-supply = <&baz>; 33 + 34 + /* External I2C interface */ 35 + i2c-gate { 36 + #address-cells = <1>; 37 + #size-cells = <0>; 38 + 39 + fnord@18 { 40 + compatible = "fnord"; 41 + reg = <0x18>; 42 + interrupt-parent = <&foo>; 43 + interrupts = <13 IRQ_TYPE_EDGE_FALLING>; 44 + }; 45 + }; 46 + };
+26
Documentation/devicetree/bindings/iio/light/tsl2583.txt
··· 1 + * TAOS TSL 2580/2581/2583 ALS sensor 2 + 3 + Required properties: 4 + 5 + - compatible: Should be one of 6 + "amstaos,tsl2580" 7 + "amstaos,tsl2581" 8 + "amstaos,tsl2583" 9 + - reg: the I2C address of the device 10 + 11 + Optional properties: 12 + 13 + - interrupt-parent: should be the phandle for the interrupt controller 14 + - interrupts: the sole interrupt generated by the device 15 + 16 + Refer to interrupt-controller/interrupts.txt for generic interrupt client 17 + node bindings. 18 + 19 + - vcc-supply: phandle to the regulator that provides power to the sensor. 20 + 21 + Example: 22 + 23 + tsl2581@29 { 24 + compatible = "amstaos,tsl2581"; 25 + reg = <0x29>; 26 + };
+1
Documentation/devicetree/bindings/iio/st-sensors.txt
··· 42 42 - st,lsm303agr-accel 43 43 - st,lis2dh12-accel 44 44 - st,h3lis331dl-accel 45 + - st,lng2dm-accel 45 46 46 47 Gyroscopes: 47 48 - st,l3g4200d-gyro
+7
MAINTAINERS
··· 6498 6498 F: arch/x86/include/asm/pmc_core.h 6499 6499 F: drivers/platform/x86/intel_pmc_core* 6500 6500 6501 + INVENSENSE MPU-3050 GYROSCOPE DRIVER 6502 + M: Linus Walleij <linus.walleij@linaro.org> 6503 + L: linux-iio@vger.kernel.org 6504 + S: Maintained 6505 + F: drivers/iio/gyro/mpu3050* 6506 + F: Documentation/devicetree/bindings/iio/gyroscope/inv,mpu3050.txt 6507 + 6501 6508 IOC3 ETHERNET DRIVER 6502 6509 M: Ralf Baechle <ralf@linux-mips.org> 6503 6510 L: linux-mips@linux-mips.org
+2 -1
drivers/iio/accel/Kconfig
··· 127 127 help 128 128 Say yes here to build support for STMicroelectronics accelerometers: 129 129 LSM303DLH, LSM303DLHC, LIS3DH, LSM330D, LSM330DL, LSM330DLC, 130 - LIS331DLH, LSM303DL, LSM303DLM, LSM330, LIS2DH12, H3LIS331DL. 130 + LIS331DLH, LSM303DL, LSM303DLM, LSM330, LIS2DH12, H3LIS331DL, 131 + LNG2DM 131 132 132 133 This driver can also be built as a module. If so, these modules 133 134 will be created:
+1
drivers/iio/accel/st_accel.h
··· 30 30 #define LSM303AGR_ACCEL_DEV_NAME "lsm303agr_accel" 31 31 #define LIS2DH12_ACCEL_DEV_NAME "lis2dh12_accel" 32 32 #define LIS3L02DQ_ACCEL_DEV_NAME "lis3l02dq" 33 + #define LNG2DM_ACCEL_DEV_NAME "lng2dm" 33 34 34 35 /** 35 36 * struct st_sensors_platform_data - default accel platform data
+73
drivers/iio/accel/st_accel_core.c
··· 231 231 #define ST_ACCEL_7_DRDY_IRQ_INT1_MASK 0x04 232 232 #define ST_ACCEL_7_MULTIREAD_BIT false 233 233 234 + /* CUSTOM VALUES FOR SENSOR 8 */ 235 + #define ST_ACCEL_8_FS_AVL_2_GAIN IIO_G_TO_M_S_2(15600) 236 + #define ST_ACCEL_8_FS_AVL_4_GAIN IIO_G_TO_M_S_2(31200) 237 + #define ST_ACCEL_8_FS_AVL_8_GAIN IIO_G_TO_M_S_2(62500) 238 + #define ST_ACCEL_8_FS_AVL_16_GAIN IIO_G_TO_M_S_2(187500) 239 + 234 240 static const struct iio_chan_spec st_accel_8bit_channels[] = { 235 241 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 236 242 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), ··· 730 724 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 731 725 }, 732 726 .multi_read_bit = ST_ACCEL_7_MULTIREAD_BIT, 727 + .bootime = 2, 728 + }, 729 + { 730 + .wai = ST_ACCEL_1_WAI_EXP, 731 + .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 732 + .sensors_supported = { 733 + [0] = LNG2DM_ACCEL_DEV_NAME, 734 + }, 735 + .ch = (struct iio_chan_spec *)st_accel_8bit_channels, 736 + .odr = { 737 + .addr = ST_ACCEL_1_ODR_ADDR, 738 + .mask = ST_ACCEL_1_ODR_MASK, 739 + .odr_avl = { 740 + { 1, ST_ACCEL_1_ODR_AVL_1HZ_VAL, }, 741 + { 10, ST_ACCEL_1_ODR_AVL_10HZ_VAL, }, 742 + { 25, ST_ACCEL_1_ODR_AVL_25HZ_VAL, }, 743 + { 50, ST_ACCEL_1_ODR_AVL_50HZ_VAL, }, 744 + { 100, ST_ACCEL_1_ODR_AVL_100HZ_VAL, }, 745 + { 200, ST_ACCEL_1_ODR_AVL_200HZ_VAL, }, 746 + { 400, ST_ACCEL_1_ODR_AVL_400HZ_VAL, }, 747 + { 1600, ST_ACCEL_1_ODR_AVL_1600HZ_VAL, }, 748 + }, 749 + }, 750 + .pw = { 751 + .addr = ST_ACCEL_1_ODR_ADDR, 752 + .mask = ST_ACCEL_1_ODR_MASK, 753 + .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 754 + }, 755 + .enable_axis = { 756 + .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 757 + .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 758 + }, 759 + .fs = { 760 + .addr = ST_ACCEL_1_FS_ADDR, 761 + .mask = ST_ACCEL_1_FS_MASK, 762 + .fs_avl = { 763 + [0] = { 764 + .num = ST_ACCEL_FS_AVL_2G, 765 + .value = ST_ACCEL_1_FS_AVL_2_VAL, 766 + .gain = ST_ACCEL_8_FS_AVL_2_GAIN, 767 + }, 768 + [1] = { 769 + .num = ST_ACCEL_FS_AVL_4G, 770 + .value = ST_ACCEL_1_FS_AVL_4_VAL, 771 + .gain = ST_ACCEL_8_FS_AVL_4_GAIN, 772 + }, 773 + [2] = { 774 + .num = ST_ACCEL_FS_AVL_8G, 775 + .value = ST_ACCEL_1_FS_AVL_8_VAL, 776 + .gain = ST_ACCEL_8_FS_AVL_8_GAIN, 777 + }, 778 + [3] = { 779 + .num = ST_ACCEL_FS_AVL_16G, 780 + .value = ST_ACCEL_1_FS_AVL_16_VAL, 781 + .gain = ST_ACCEL_8_FS_AVL_16_GAIN, 782 + }, 783 + }, 784 + }, 785 + .drdy_irq = { 786 + .addr = ST_ACCEL_1_DRDY_IRQ_ADDR, 787 + .mask_int1 = ST_ACCEL_1_DRDY_IRQ_INT1_MASK, 788 + .mask_int2 = ST_ACCEL_1_DRDY_IRQ_INT2_MASK, 789 + .addr_ihl = ST_ACCEL_1_IHL_IRQ_ADDR, 790 + .mask_ihl = ST_ACCEL_1_IHL_IRQ_MASK, 791 + .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 792 + }, 793 + .multi_read_bit = ST_ACCEL_1_MULTIREAD_BIT, 733 794 .bootime = 2, 734 795 }, 735 796 };
+5
drivers/iio/accel/st_accel_i2c.c
··· 84 84 .compatible = "st,lis3l02dq", 85 85 .data = LIS3L02DQ_ACCEL_DEV_NAME, 86 86 }, 87 + { 88 + .compatible = "st,lng2dm-accel", 89 + .data = LNG2DM_ACCEL_DEV_NAME, 90 + }, 87 91 {}, 88 92 }; 89 93 MODULE_DEVICE_TABLE(of, st_accel_of_match); ··· 139 135 { LSM303AGR_ACCEL_DEV_NAME }, 140 136 { LIS2DH12_ACCEL_DEV_NAME }, 141 137 { LIS3L02DQ_ACCEL_DEV_NAME }, 138 + { LNG2DM_ACCEL_DEV_NAME }, 142 139 {}, 143 140 }; 144 141 MODULE_DEVICE_TABLE(i2c, st_accel_id_table);
+1
drivers/iio/accel/st_accel_spi.c
··· 60 60 { LSM303AGR_ACCEL_DEV_NAME }, 61 61 { LIS2DH12_ACCEL_DEV_NAME }, 62 62 { LIS3L02DQ_ACCEL_DEV_NAME }, 63 + { LNG2DM_ACCEL_DEV_NAME }, 63 64 {}, 64 65 }; 65 66 MODULE_DEVICE_TABLE(spi, st_accel_id_table);
+28
drivers/iio/adc/at91_adc.c
··· 30 30 #include <linux/iio/trigger.h> 31 31 #include <linux/iio/trigger_consumer.h> 32 32 #include <linux/iio/triggered_buffer.h> 33 + #include <linux/pinctrl/consumer.h> 33 34 34 35 /* Registers */ 35 36 #define AT91_ADC_CR 0x00 /* Control Register */ ··· 1348 1347 return 0; 1349 1348 } 1350 1349 1350 + #ifdef CONFIG_PM_SLEEP 1351 + static int at91_adc_suspend(struct device *dev) 1352 + { 1353 + struct iio_dev *idev = platform_get_drvdata(to_platform_device(dev)); 1354 + struct at91_adc_state *st = iio_priv(idev); 1355 + 1356 + pinctrl_pm_select_sleep_state(dev); 1357 + clk_disable_unprepare(st->clk); 1358 + 1359 + return 0; 1360 + } 1361 + 1362 + static int at91_adc_resume(struct device *dev) 1363 + { 1364 + struct iio_dev *idev = platform_get_drvdata(to_platform_device(dev)); 1365 + struct at91_adc_state *st = iio_priv(idev); 1366 + 1367 + clk_prepare_enable(st->clk); 1368 + pinctrl_pm_select_default_state(dev); 1369 + 1370 + return 0; 1371 + } 1372 + #endif 1373 + 1374 + static SIMPLE_DEV_PM_OPS(at91_adc_pm_ops, at91_adc_suspend, at91_adc_resume); 1375 + 1351 1376 static struct at91_adc_caps at91sam9260_caps = { 1352 1377 .calc_startup_ticks = calc_startup_ticks_9260, 1353 1378 .num_channels = 4, ··· 1468 1441 .driver = { 1469 1442 .name = DRIVER_NAME, 1470 1443 .of_match_table = of_match_ptr(at91_adc_dt_ids), 1444 + .pm = &at91_adc_pm_ops, 1471 1445 }, 1472 1446 }; 1473 1447
+145 -3
drivers/iio/adc/ti_am335x_adc.c
··· 30 30 #include <linux/iio/buffer.h> 31 31 #include <linux/iio/kfifo_buf.h> 32 32 33 + #include <linux/dmaengine.h> 34 + #include <linux/dma-mapping.h> 35 + 36 + #define DMA_BUFFER_SIZE SZ_2K 37 + 38 + struct tiadc_dma { 39 + struct dma_slave_config conf; 40 + struct dma_chan *chan; 41 + dma_addr_t addr; 42 + dma_cookie_t cookie; 43 + u8 *buf; 44 + int current_period; 45 + int period_size; 46 + u8 fifo_thresh; 47 + }; 48 + 33 49 struct tiadc_device { 34 50 struct ti_tscadc_dev *mfd_tscadc; 51 + struct tiadc_dma dma; 35 52 struct mutex fifo1_lock; /* to protect fifo access */ 36 53 int channels; 54 + int total_ch_enabled; 37 55 u8 channel_line[8]; 38 56 u8 channel_step[8]; 39 57 int buffer_en_ch_steps; ··· 216 198 return IRQ_HANDLED; 217 199 } 218 200 201 + static void tiadc_dma_rx_complete(void *param) 202 + { 203 + struct iio_dev *indio_dev = param; 204 + struct tiadc_device *adc_dev = iio_priv(indio_dev); 205 + struct tiadc_dma *dma = &adc_dev->dma; 206 + u8 *data; 207 + int i; 208 + 209 + data = dma->buf + dma->current_period * dma->period_size; 210 + dma->current_period = 1 - dma->current_period; /* swap the buffer ID */ 211 + 212 + for (i = 0; i < dma->period_size; i += indio_dev->scan_bytes) { 213 + iio_push_to_buffers(indio_dev, data); 214 + data += indio_dev->scan_bytes; 215 + } 216 + } 217 + 218 + static int tiadc_start_dma(struct iio_dev *indio_dev) 219 + { 220 + struct tiadc_device *adc_dev = iio_priv(indio_dev); 221 + struct tiadc_dma *dma = &adc_dev->dma; 222 + struct dma_async_tx_descriptor *desc; 223 + 224 + dma->current_period = 0; /* We start to fill period 0 */ 225 + /* 226 + * Make the fifo thresh as the multiple of total number of 227 + * channels enabled, so make sure that cyclic DMA period 228 + * length is also a multiple of total number of channels 229 + * enabled. This ensures that no invalid data is reported 230 + * to the stack via iio_push_to_buffers(). 231 + */ 232 + dma->fifo_thresh = rounddown(FIFO1_THRESHOLD + 1, 233 + adc_dev->total_ch_enabled) - 1; 234 + /* Make sure that period length is multiple of fifo thresh level */ 235 + dma->period_size = rounddown(DMA_BUFFER_SIZE / 2, 236 + (dma->fifo_thresh + 1) * sizeof(u16)); 237 + 238 + dma->conf.src_maxburst = dma->fifo_thresh + 1; 239 + dmaengine_slave_config(dma->chan, &dma->conf); 240 + 241 + desc = dmaengine_prep_dma_cyclic(dma->chan, dma->addr, 242 + dma->period_size * 2, 243 + dma->period_size, DMA_DEV_TO_MEM, 244 + DMA_PREP_INTERRUPT); 245 + if (!desc) 246 + return -EBUSY; 247 + 248 + desc->callback = tiadc_dma_rx_complete; 249 + desc->callback_param = indio_dev; 250 + 251 + dma->cookie = dmaengine_submit(desc); 252 + 253 + dma_async_issue_pending(dma->chan); 254 + 255 + tiadc_writel(adc_dev, REG_FIFO1THR, dma->fifo_thresh); 256 + tiadc_writel(adc_dev, REG_DMA1REQ, dma->fifo_thresh); 257 + tiadc_writel(adc_dev, REG_DMAENABLE_SET, DMA_FIFO1); 258 + 259 + return 0; 260 + } 261 + 219 262 static int tiadc_buffer_preenable(struct iio_dev *indio_dev) 220 263 { 221 264 struct tiadc_device *adc_dev = iio_priv(indio_dev); ··· 297 218 static int tiadc_buffer_postenable(struct iio_dev *indio_dev) 298 219 { 299 220 struct tiadc_device *adc_dev = iio_priv(indio_dev); 221 + struct tiadc_dma *dma = &adc_dev->dma; 222 + unsigned int irq_enable; 300 223 unsigned int enb = 0; 301 224 u8 bit; 302 225 303 226 tiadc_step_config(indio_dev); 304 - for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels) 227 + for_each_set_bit(bit, indio_dev->active_scan_mask, adc_dev->channels) { 305 228 enb |= (get_adc_step_bit(adc_dev, bit) << 1); 229 + adc_dev->total_ch_enabled++; 230 + } 306 231 adc_dev->buffer_en_ch_steps = enb; 232 + 233 + if (dma->chan) 234 + tiadc_start_dma(indio_dev); 307 235 308 236 am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb); 309 237 310 238 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES 311 239 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW); 312 - tiadc_writel(adc_dev, REG_IRQENABLE, IRQENB_FIFO1THRES 313 - | IRQENB_FIFO1OVRRUN); 240 + 241 + irq_enable = IRQENB_FIFO1OVRRUN; 242 + if (!dma->chan) 243 + irq_enable |= IRQENB_FIFO1THRES; 244 + tiadc_writel(adc_dev, REG_IRQENABLE, irq_enable); 314 245 315 246 return 0; 316 247 } ··· 328 239 static int tiadc_buffer_predisable(struct iio_dev *indio_dev) 329 240 { 330 241 struct tiadc_device *adc_dev = iio_priv(indio_dev); 242 + struct tiadc_dma *dma = &adc_dev->dma; 331 243 int fifo1count, i, read; 332 244 333 245 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES | 334 246 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW)); 335 247 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps); 336 248 adc_dev->buffer_en_ch_steps = 0; 249 + adc_dev->total_ch_enabled = 0; 250 + if (dma->chan) { 251 + tiadc_writel(adc_dev, REG_DMAENABLE_CLEAR, 0x2); 252 + dmaengine_terminate_async(dma->chan); 253 + } 337 254 338 255 /* Flush FIFO of leftover data in the time it takes to disable adc */ 339 256 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); ··· 525 430 .driver_module = THIS_MODULE, 526 431 }; 527 432 433 + static int tiadc_request_dma(struct platform_device *pdev, 434 + struct tiadc_device *adc_dev) 435 + { 436 + struct tiadc_dma *dma = &adc_dev->dma; 437 + dma_cap_mask_t mask; 438 + 439 + /* Default slave configuration parameters */ 440 + dma->conf.direction = DMA_DEV_TO_MEM; 441 + dma->conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; 442 + dma->conf.src_addr = adc_dev->mfd_tscadc->tscadc_phys_base + REG_FIFO1; 443 + 444 + dma_cap_zero(mask); 445 + dma_cap_set(DMA_CYCLIC, mask); 446 + 447 + /* Get a channel for RX */ 448 + dma->chan = dma_request_chan(adc_dev->mfd_tscadc->dev, "fifo1"); 449 + if (IS_ERR(dma->chan)) { 450 + int ret = PTR_ERR(dma->chan); 451 + 452 + dma->chan = NULL; 453 + return ret; 454 + } 455 + 456 + /* RX buffer */ 457 + dma->buf = dma_alloc_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE, 458 + &dma->addr, GFP_KERNEL); 459 + if (!dma->buf) 460 + goto err; 461 + 462 + return 0; 463 + err: 464 + dma_release_channel(dma->chan); 465 + return -ENOMEM; 466 + } 467 + 528 468 static int tiadc_parse_dt(struct platform_device *pdev, 529 469 struct tiadc_device *adc_dev) 530 470 { ··· 642 512 643 513 platform_set_drvdata(pdev, indio_dev); 644 514 515 + err = tiadc_request_dma(pdev, adc_dev); 516 + if (err && err == -EPROBE_DEFER) 517 + goto err_dma; 518 + 645 519 return 0; 646 520 521 + err_dma: 522 + iio_device_unregister(indio_dev); 647 523 err_buffer_unregister: 648 524 tiadc_iio_buffered_hardware_remove(indio_dev); 649 525 err_free_channels: ··· 661 525 { 662 526 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 663 527 struct tiadc_device *adc_dev = iio_priv(indio_dev); 528 + struct tiadc_dma *dma = &adc_dev->dma; 664 529 u32 step_en; 665 530 531 + if (dma->chan) { 532 + dma_free_coherent(dma->chan->device->dev, DMA_BUFFER_SIZE, 533 + dma->buf, dma->addr); 534 + dma_release_channel(dma->chan); 535 + } 666 536 iio_device_unregister(indio_dev); 667 537 tiadc_iio_buffered_hardware_remove(indio_dev); 668 538 tiadc_channels_remove(indio_dev);
+1
drivers/iio/common/Kconfig
··· 2 2 # IIO common modules 3 3 # 4 4 5 + source "drivers/iio/common/cros_ec_sensors/Kconfig" 5 6 source "drivers/iio/common/hid-sensors/Kconfig" 6 7 source "drivers/iio/common/ms_sensors/Kconfig" 7 8 source "drivers/iio/common/ssp_sensors/Kconfig"
+1
drivers/iio/common/Makefile
··· 7 7 # 8 8 9 9 # When adding new entries keep the list in alphabetical order 10 + obj-y += cros_ec_sensors/ 10 11 obj-y += hid-sensors/ 11 12 obj-y += ms_sensors/ 12 13 obj-y += ssp_sensors/
+22
drivers/iio/common/cros_ec_sensors/Kconfig
··· 1 + # 2 + # Chrome OS Embedded Controller managed sensors library 3 + # 4 + config IIO_CROS_EC_SENSORS_CORE 5 + tristate "ChromeOS EC Sensors Core" 6 + depends on SYSFS && MFD_CROS_EC 7 + select IIO_BUFFER 8 + select IIO_TRIGGERED_BUFFER 9 + help 10 + Base module for the ChromeOS EC Sensors module. 11 + Contains core functions used by other IIO CrosEC sensor 12 + drivers. 13 + Define common attributes and sysfs interrupt handler. 14 + 15 + config IIO_CROS_EC_SENSORS 16 + tristate "ChromeOS EC Contiguous Sensors" 17 + depends on IIO_CROS_EC_SENSORS_CORE 18 + help 19 + Module to handle 3d contiguous sensors like 20 + Accelerometers, Gyroscope and Magnetometer that are 21 + presented by the ChromeOS EC Sensor hub. 22 + Creates an IIO device for each functions.
+6
drivers/iio/common/cros_ec_sensors/Makefile
··· 1 + # 2 + # Makefile for sensors seen through the ChromeOS EC sensor hub. 3 + # 4 + 5 + obj-$(CONFIG_IIO_CROS_EC_SENSORS_CORE) += cros_ec_sensors_core.o 6 + obj-$(CONFIG_IIO_CROS_EC_SENSORS) += cros_ec_sensors.o
+322
drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
··· 1 + /* 2 + * cros_ec_sensors - Driver for Chrome OS Embedded Controller sensors. 3 + * 4 + * Copyright (C) 2016 Google, Inc 5 + * 6 + * This software is licensed under the terms of the GNU General Public 7 + * License version 2, as published by the Free Software Foundation, and 8 + * may be copied, distributed, and modified under those terms. 9 + * 10 + * This program is distributed in the hope that it will be useful, 11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 + * GNU General Public License for more details. 14 + * 15 + * This driver uses the cros-ec interface to communicate with the Chrome OS 16 + * EC about sensors data. Data access is presented through iio sysfs. 17 + */ 18 + 19 + #include <linux/delay.h> 20 + #include <linux/device.h> 21 + #include <linux/iio/buffer.h> 22 + #include <linux/iio/iio.h> 23 + #include <linux/iio/kfifo_buf.h> 24 + #include <linux/iio/trigger_consumer.h> 25 + #include <linux/iio/triggered_buffer.h> 26 + #include <linux/kernel.h> 27 + #include <linux/mfd/cros_ec.h> 28 + #include <linux/mfd/cros_ec_commands.h> 29 + #include <linux/module.h> 30 + #include <linux/platform_device.h> 31 + #include <linux/slab.h> 32 + #include <linux/sysfs.h> 33 + 34 + #include "cros_ec_sensors_core.h" 35 + 36 + #define CROS_EC_SENSORS_MAX_CHANNELS 4 37 + 38 + /* State data for ec_sensors iio driver. */ 39 + struct cros_ec_sensors_state { 40 + /* Shared by all sensors */ 41 + struct cros_ec_sensors_core_state core; 42 + 43 + struct iio_chan_spec channels[CROS_EC_SENSORS_MAX_CHANNELS]; 44 + }; 45 + 46 + static int cros_ec_sensors_read(struct iio_dev *indio_dev, 47 + struct iio_chan_spec const *chan, 48 + int *val, int *val2, long mask) 49 + { 50 + struct cros_ec_sensors_state *st = iio_priv(indio_dev); 51 + s16 data = 0; 52 + s64 val64; 53 + int i; 54 + int ret; 55 + int idx = chan->scan_index; 56 + 57 + mutex_lock(&st->core.cmd_lock); 58 + 59 + switch (mask) { 60 + case IIO_CHAN_INFO_RAW: 61 + ret = st->core.read_ec_sensors_data(indio_dev, 1 << idx, &data); 62 + if (ret < 0) 63 + break; 64 + 65 + *val = data; 66 + break; 67 + case IIO_CHAN_INFO_CALIBBIAS: 68 + st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET; 69 + st->core.param.sensor_offset.flags = 0; 70 + 71 + ret = cros_ec_motion_send_host_cmd(&st->core, 0); 72 + if (ret < 0) 73 + break; 74 + 75 + /* Save values */ 76 + for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++) 77 + st->core.calib[i] = 78 + st->core.resp->sensor_offset.offset[i]; 79 + 80 + *val = st->core.calib[idx]; 81 + break; 82 + case IIO_CHAN_INFO_SCALE: 83 + st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; 84 + st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE; 85 + 86 + ret = cros_ec_motion_send_host_cmd(&st->core, 0); 87 + if (ret < 0) 88 + break; 89 + 90 + val64 = st->core.resp->sensor_range.ret; 91 + switch (st->core.type) { 92 + case MOTIONSENSE_TYPE_ACCEL: 93 + /* 94 + * EC returns data in g, iio exepects m/s^2. 95 + * Do not use IIO_G_TO_M_S_2 to avoid precision loss. 96 + */ 97 + *val = div_s64(val64 * 980665, 10); 98 + *val2 = 10000 << (CROS_EC_SENSOR_BITS - 1); 99 + ret = IIO_VAL_FRACTIONAL; 100 + break; 101 + case MOTIONSENSE_TYPE_GYRO: 102 + /* 103 + * EC returns data in dps, iio expects rad/s. 104 + * Do not use IIO_DEGREE_TO_RAD to avoid precision 105 + * loss. Round to the nearest integer. 106 + */ 107 + *val = div_s64(val64 * 314159 + 9000000ULL, 1000); 108 + *val2 = 18000 << (CROS_EC_SENSOR_BITS - 1); 109 + ret = IIO_VAL_FRACTIONAL; 110 + break; 111 + case MOTIONSENSE_TYPE_MAG: 112 + /* 113 + * EC returns data in 16LSB / uT, 114 + * iio expects Gauss 115 + */ 116 + *val = val64; 117 + *val2 = 100 << (CROS_EC_SENSOR_BITS - 1); 118 + ret = IIO_VAL_FRACTIONAL; 119 + break; 120 + default: 121 + ret = -EINVAL; 122 + } 123 + break; 124 + default: 125 + ret = cros_ec_sensors_core_read(&st->core, chan, val, val2, 126 + mask); 127 + break; 128 + } 129 + mutex_unlock(&st->core.cmd_lock); 130 + 131 + return ret; 132 + } 133 + 134 + static int cros_ec_sensors_write(struct iio_dev *indio_dev, 135 + struct iio_chan_spec const *chan, 136 + int val, int val2, long mask) 137 + { 138 + struct cros_ec_sensors_state *st = iio_priv(indio_dev); 139 + int i; 140 + int ret; 141 + int idx = chan->scan_index; 142 + 143 + mutex_lock(&st->core.cmd_lock); 144 + 145 + switch (mask) { 146 + case IIO_CHAN_INFO_CALIBBIAS: 147 + st->core.calib[idx] = val; 148 + 149 + /* Send to EC for each axis, even if not complete */ 150 + st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET; 151 + st->core.param.sensor_offset.flags = 152 + MOTION_SENSE_SET_OFFSET; 153 + for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++) 154 + st->core.param.sensor_offset.offset[i] = 155 + st->core.calib[i]; 156 + st->core.param.sensor_offset.temp = 157 + EC_MOTION_SENSE_INVALID_CALIB_TEMP; 158 + 159 + ret = cros_ec_motion_send_host_cmd(&st->core, 0); 160 + break; 161 + case IIO_CHAN_INFO_SCALE: 162 + if (st->core.type == MOTIONSENSE_TYPE_MAG) { 163 + ret = -EINVAL; 164 + break; 165 + } 166 + st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; 167 + st->core.param.sensor_range.data = val; 168 + 169 + /* Always roundup, so caller gets at least what it asks for. */ 170 + st->core.param.sensor_range.roundup = 1; 171 + 172 + ret = cros_ec_motion_send_host_cmd(&st->core, 0); 173 + break; 174 + default: 175 + ret = cros_ec_sensors_core_write( 176 + &st->core, chan, val, val2, mask); 177 + break; 178 + } 179 + 180 + mutex_unlock(&st->core.cmd_lock); 181 + 182 + return ret; 183 + } 184 + 185 + static const struct iio_info ec_sensors_info = { 186 + .read_raw = &cros_ec_sensors_read, 187 + .write_raw = &cros_ec_sensors_write, 188 + .driver_module = THIS_MODULE, 189 + }; 190 + 191 + static int cros_ec_sensors_probe(struct platform_device *pdev) 192 + { 193 + struct device *dev = &pdev->dev; 194 + struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent); 195 + struct cros_ec_device *ec_device; 196 + struct iio_dev *indio_dev; 197 + struct cros_ec_sensors_state *state; 198 + struct iio_chan_spec *channel; 199 + int ret, i; 200 + 201 + if (!ec_dev || !ec_dev->ec_dev) { 202 + dev_warn(&pdev->dev, "No CROS EC device found.\n"); 203 + return -EINVAL; 204 + } 205 + ec_device = ec_dev->ec_dev; 206 + 207 + indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state)); 208 + if (!indio_dev) 209 + return -ENOMEM; 210 + 211 + ret = cros_ec_sensors_core_init(pdev, indio_dev, true); 212 + if (ret) 213 + return ret; 214 + 215 + indio_dev->info = &ec_sensors_info; 216 + state = iio_priv(indio_dev); 217 + for (channel = state->channels, i = CROS_EC_SENSOR_X; 218 + i < CROS_EC_SENSOR_MAX_AXIS; i++, channel++) { 219 + /* Common part */ 220 + channel->info_mask_separate = 221 + BIT(IIO_CHAN_INFO_RAW) | 222 + BIT(IIO_CHAN_INFO_CALIBBIAS); 223 + channel->info_mask_shared_by_all = 224 + BIT(IIO_CHAN_INFO_SCALE) | 225 + BIT(IIO_CHAN_INFO_FREQUENCY) | 226 + BIT(IIO_CHAN_INFO_SAMP_FREQ); 227 + channel->scan_type.realbits = CROS_EC_SENSOR_BITS; 228 + channel->scan_type.storagebits = CROS_EC_SENSOR_BITS; 229 + channel->scan_index = i; 230 + channel->ext_info = cros_ec_sensors_ext_info; 231 + channel->modified = 1; 232 + channel->channel2 = IIO_MOD_X + i; 233 + channel->scan_type.sign = 's'; 234 + 235 + /* Sensor specific */ 236 + switch (state->core.type) { 237 + case MOTIONSENSE_TYPE_ACCEL: 238 + channel->type = IIO_ACCEL; 239 + break; 240 + case MOTIONSENSE_TYPE_GYRO: 241 + channel->type = IIO_ANGL_VEL; 242 + break; 243 + case MOTIONSENSE_TYPE_MAG: 244 + channel->type = IIO_MAGN; 245 + break; 246 + default: 247 + dev_err(&pdev->dev, "Unknown motion sensor\n"); 248 + return -EINVAL; 249 + } 250 + } 251 + 252 + /* Timestamp */ 253 + channel->type = IIO_TIMESTAMP; 254 + channel->channel = -1; 255 + channel->scan_index = CROS_EC_SENSOR_MAX_AXIS; 256 + channel->scan_type.sign = 's'; 257 + channel->scan_type.realbits = 64; 258 + channel->scan_type.storagebits = 64; 259 + 260 + indio_dev->channels = state->channels; 261 + indio_dev->num_channels = CROS_EC_SENSORS_MAX_CHANNELS; 262 + 263 + /* There is only enough room for accel and gyro in the io space */ 264 + if ((state->core.ec->cmd_readmem != NULL) && 265 + (state->core.type != MOTIONSENSE_TYPE_MAG)) 266 + state->core.read_ec_sensors_data = cros_ec_sensors_read_lpc; 267 + else 268 + state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd; 269 + 270 + ret = iio_triggered_buffer_setup(indio_dev, NULL, 271 + cros_ec_sensors_capture, NULL); 272 + if (ret) 273 + return ret; 274 + 275 + ret = iio_device_register(indio_dev); 276 + if (ret) 277 + goto error_uninit_buffer; 278 + 279 + return 0; 280 + 281 + error_uninit_buffer: 282 + iio_triggered_buffer_cleanup(indio_dev); 283 + 284 + return ret; 285 + } 286 + 287 + static int cros_ec_sensors_remove(struct platform_device *pdev) 288 + { 289 + struct iio_dev *indio_dev = platform_get_drvdata(pdev); 290 + 291 + iio_device_unregister(indio_dev); 292 + iio_triggered_buffer_cleanup(indio_dev); 293 + 294 + return 0; 295 + } 296 + 297 + static const struct platform_device_id cros_ec_sensors_ids[] = { 298 + { 299 + .name = "cros-ec-accel", 300 + }, 301 + { 302 + .name = "cros-ec-gyro", 303 + }, 304 + { 305 + .name = "cros-ec-mag", 306 + }, 307 + { /* sentinel */ } 308 + }; 309 + MODULE_DEVICE_TABLE(platform, cros_ec_sensors_ids); 310 + 311 + static struct platform_driver cros_ec_sensors_platform_driver = { 312 + .driver = { 313 + .name = "cros-ec-sensors", 314 + }, 315 + .probe = cros_ec_sensors_probe, 316 + .remove = cros_ec_sensors_remove, 317 + .id_table = cros_ec_sensors_ids, 318 + }; 319 + module_platform_driver(cros_ec_sensors_platform_driver); 320 + 321 + MODULE_DESCRIPTION("ChromeOS EC 3-axis sensors driver"); 322 + MODULE_LICENSE("GPL v2");
+450
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
··· 1 + /* 2 + * cros_ec_sensors_core - Common function for Chrome OS EC sensor driver. 3 + * 4 + * Copyright (C) 2016 Google, Inc 5 + * 6 + * This software is licensed under the terms of the GNU General Public 7 + * License version 2, as published by the Free Software Foundation, and 8 + * may be copied, distributed, and modified under those terms. 9 + * 10 + * This program is distributed in the hope that it will be useful, 11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 + * GNU General Public License for more details. 14 + */ 15 + 16 + #include <linux/delay.h> 17 + #include <linux/device.h> 18 + #include <linux/iio/buffer.h> 19 + #include <linux/iio/iio.h> 20 + #include <linux/iio/kfifo_buf.h> 21 + #include <linux/iio/trigger_consumer.h> 22 + #include <linux/kernel.h> 23 + #include <linux/mfd/cros_ec.h> 24 + #include <linux/mfd/cros_ec_commands.h> 25 + #include <linux/module.h> 26 + #include <linux/slab.h> 27 + #include <linux/sysfs.h> 28 + #include <linux/platform_device.h> 29 + 30 + #include "cros_ec_sensors_core.h" 31 + 32 + static char *cros_ec_loc[] = { 33 + [MOTIONSENSE_LOC_BASE] = "base", 34 + [MOTIONSENSE_LOC_LID] = "lid", 35 + [MOTIONSENSE_LOC_MAX] = "unknown", 36 + }; 37 + 38 + int cros_ec_sensors_core_init(struct platform_device *pdev, 39 + struct iio_dev *indio_dev, 40 + bool physical_device) 41 + { 42 + struct device *dev = &pdev->dev; 43 + struct cros_ec_sensors_core_state *state = iio_priv(indio_dev); 44 + struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent); 45 + struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev); 46 + 47 + platform_set_drvdata(pdev, indio_dev); 48 + 49 + state->ec = ec->ec_dev; 50 + state->msg = devm_kzalloc(&pdev->dev, 51 + max((u16)sizeof(struct ec_params_motion_sense), 52 + state->ec->max_response), GFP_KERNEL); 53 + if (!state->msg) 54 + return -ENOMEM; 55 + 56 + state->resp = (struct ec_response_motion_sense *)state->msg->data; 57 + 58 + mutex_init(&state->cmd_lock); 59 + 60 + /* Set up the host command structure. */ 61 + state->msg->version = 2; 62 + state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset; 63 + state->msg->outsize = sizeof(struct ec_params_motion_sense); 64 + 65 + indio_dev->dev.parent = &pdev->dev; 66 + indio_dev->name = pdev->name; 67 + 68 + if (physical_device) { 69 + indio_dev->modes = INDIO_DIRECT_MODE; 70 + 71 + state->param.cmd = MOTIONSENSE_CMD_INFO; 72 + state->param.info.sensor_num = sensor_platform->sensor_num; 73 + if (cros_ec_motion_send_host_cmd(state, 0)) { 74 + dev_warn(dev, "Can not access sensor info\n"); 75 + return -EIO; 76 + } 77 + state->type = state->resp->info.type; 78 + state->loc = state->resp->info.location; 79 + } 80 + 81 + return 0; 82 + } 83 + EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init); 84 + 85 + int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *state, 86 + u16 opt_length) 87 + { 88 + int ret; 89 + 90 + if (opt_length) 91 + state->msg->insize = min(opt_length, state->ec->max_response); 92 + else 93 + state->msg->insize = state->ec->max_response; 94 + 95 + memcpy(state->msg->data, &state->param, sizeof(state->param)); 96 + 97 + ret = cros_ec_cmd_xfer_status(state->ec, state->msg); 98 + if (ret < 0) 99 + return -EIO; 100 + 101 + if (ret && 102 + state->resp != (struct ec_response_motion_sense *)state->msg->data) 103 + memcpy(state->resp, state->msg->data, ret); 104 + 105 + return 0; 106 + } 107 + EXPORT_SYMBOL_GPL(cros_ec_motion_send_host_cmd); 108 + 109 + static ssize_t cros_ec_sensors_calibrate(struct iio_dev *indio_dev, 110 + uintptr_t private, const struct iio_chan_spec *chan, 111 + const char *buf, size_t len) 112 + { 113 + struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 114 + int ret, i; 115 + bool calibrate; 116 + 117 + ret = strtobool(buf, &calibrate); 118 + if (ret < 0) 119 + return ret; 120 + if (!calibrate) 121 + return -EINVAL; 122 + 123 + mutex_lock(&st->cmd_lock); 124 + st->param.cmd = MOTIONSENSE_CMD_PERFORM_CALIB; 125 + ret = cros_ec_motion_send_host_cmd(st, 0); 126 + if (ret != 0) { 127 + dev_warn(&indio_dev->dev, "Unable to calibrate sensor\n"); 128 + } else { 129 + /* Save values */ 130 + for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++) 131 + st->calib[i] = st->resp->perform_calib.offset[i]; 132 + } 133 + mutex_unlock(&st->cmd_lock); 134 + 135 + return ret ? ret : len; 136 + } 137 + 138 + static ssize_t cros_ec_sensors_loc(struct iio_dev *indio_dev, 139 + uintptr_t private, const struct iio_chan_spec *chan, 140 + char *buf) 141 + { 142 + struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 143 + 144 + return snprintf(buf, PAGE_SIZE, "%s\n", cros_ec_loc[st->loc]); 145 + } 146 + 147 + const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = { 148 + { 149 + .name = "calibrate", 150 + .shared = IIO_SHARED_BY_ALL, 151 + .write = cros_ec_sensors_calibrate 152 + }, 153 + { 154 + .name = "location", 155 + .shared = IIO_SHARED_BY_ALL, 156 + .read = cros_ec_sensors_loc 157 + }, 158 + { }, 159 + }; 160 + EXPORT_SYMBOL_GPL(cros_ec_sensors_ext_info); 161 + 162 + /** 163 + * cros_ec_sensors_idx_to_reg - convert index into offset in shared memory 164 + * @st: pointer to state information for device 165 + * @idx: sensor index (should be element of enum sensor_index) 166 + * 167 + * Return: address to read at 168 + */ 169 + static unsigned int cros_ec_sensors_idx_to_reg( 170 + struct cros_ec_sensors_core_state *st, 171 + unsigned int idx) 172 + { 173 + /* 174 + * When using LPC interface, only space for 2 Accel and one Gyro. 175 + * First halfword of MOTIONSENSE_TYPE_ACCEL is used by angle. 176 + */ 177 + if (st->type == MOTIONSENSE_TYPE_ACCEL) 178 + return EC_MEMMAP_ACC_DATA + sizeof(u16) * 179 + (1 + idx + st->param.info.sensor_num * 180 + CROS_EC_SENSOR_MAX_AXIS); 181 + 182 + return EC_MEMMAP_GYRO_DATA + sizeof(u16) * idx; 183 + } 184 + 185 + static int cros_ec_sensors_cmd_read_u8(struct cros_ec_device *ec, 186 + unsigned int offset, u8 *dest) 187 + { 188 + return ec->cmd_readmem(ec, offset, 1, dest); 189 + } 190 + 191 + static int cros_ec_sensors_cmd_read_u16(struct cros_ec_device *ec, 192 + unsigned int offset, u16 *dest) 193 + { 194 + __le16 tmp; 195 + int ret = ec->cmd_readmem(ec, offset, 2, &tmp); 196 + 197 + if (ret >= 0) 198 + *dest = le16_to_cpu(tmp); 199 + 200 + return ret; 201 + } 202 + 203 + /** 204 + * cros_ec_sensors_read_until_not_busy() - read until is not busy 205 + * 206 + * @st: pointer to state information for device 207 + * 208 + * Read from EC status byte until it reads not busy. 209 + * Return: 8-bit status if ok, -errno on failure. 210 + */ 211 + static int cros_ec_sensors_read_until_not_busy( 212 + struct cros_ec_sensors_core_state *st) 213 + { 214 + struct cros_ec_device *ec = st->ec; 215 + u8 status; 216 + int ret, attempts = 0; 217 + 218 + ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, &status); 219 + if (ret < 0) 220 + return ret; 221 + 222 + while (status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) { 223 + /* Give up after enough attempts, return error. */ 224 + if (attempts++ >= 50) 225 + return -EIO; 226 + 227 + /* Small delay every so often. */ 228 + if (attempts % 5 == 0) 229 + msleep(25); 230 + 231 + ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, 232 + &status); 233 + if (ret < 0) 234 + return ret; 235 + } 236 + 237 + return status; 238 + } 239 + 240 + /** 241 + * read_ec_sensors_data_unsafe() - read acceleration data from EC shared memory 242 + * @indio_dev: pointer to IIO device 243 + * @scan_mask: bitmap of the sensor indices to scan 244 + * @data: location to store data 245 + * 246 + * This is the unsafe function for reading the EC data. It does not guarantee 247 + * that the EC will not modify the data as it is being read in. 248 + * 249 + * Return: 0 on success, -errno on failure. 250 + */ 251 + static int cros_ec_sensors_read_data_unsafe(struct iio_dev *indio_dev, 252 + unsigned long scan_mask, s16 *data) 253 + { 254 + struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 255 + struct cros_ec_device *ec = st->ec; 256 + unsigned int i; 257 + int ret; 258 + 259 + /* Read all sensors enabled in scan_mask. Each value is 2 bytes. */ 260 + for_each_set_bit(i, &scan_mask, indio_dev->masklength) { 261 + ret = cros_ec_sensors_cmd_read_u16(ec, 262 + cros_ec_sensors_idx_to_reg(st, i), 263 + data); 264 + if (ret < 0) 265 + return ret; 266 + 267 + data++; 268 + } 269 + 270 + return 0; 271 + } 272 + 273 + int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev, 274 + unsigned long scan_mask, s16 *data) 275 + { 276 + struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 277 + struct cros_ec_device *ec = st->ec; 278 + u8 samp_id = 0xff, status = 0; 279 + int ret, attempts = 0; 280 + 281 + /* 282 + * Continually read all data from EC until the status byte after 283 + * all reads reflects that the EC is not busy and the sample id 284 + * matches the sample id from before all reads. This guarantees 285 + * that data read in was not modified by the EC while reading. 286 + */ 287 + while ((status & (EC_MEMMAP_ACC_STATUS_BUSY_BIT | 288 + EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK)) != samp_id) { 289 + /* If we have tried to read too many times, return error. */ 290 + if (attempts++ >= 5) 291 + return -EIO; 292 + 293 + /* Read status byte until EC is not busy. */ 294 + status = cros_ec_sensors_read_until_not_busy(st); 295 + if (status < 0) 296 + return status; 297 + 298 + /* 299 + * Store the current sample id so that we can compare to the 300 + * sample id after reading the data. 301 + */ 302 + samp_id = status & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK; 303 + 304 + /* Read all EC data, format it, and store it into data. */ 305 + ret = cros_ec_sensors_read_data_unsafe(indio_dev, scan_mask, 306 + data); 307 + if (ret < 0) 308 + return ret; 309 + 310 + /* Read status byte. */ 311 + ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, 312 + &status); 313 + if (ret < 0) 314 + return ret; 315 + } 316 + 317 + return 0; 318 + } 319 + EXPORT_SYMBOL_GPL(cros_ec_sensors_read_lpc); 320 + 321 + int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev, 322 + unsigned long scan_mask, s16 *data) 323 + { 324 + struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 325 + int ret; 326 + unsigned int i; 327 + 328 + /* Read all sensor data through a command. */ 329 + st->param.cmd = MOTIONSENSE_CMD_DATA; 330 + ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->data)); 331 + if (ret != 0) { 332 + dev_warn(&indio_dev->dev, "Unable to read sensor data\n"); 333 + return ret; 334 + } 335 + 336 + for_each_set_bit(i, &scan_mask, indio_dev->masklength) { 337 + *data = st->resp->data.data[i]; 338 + data++; 339 + } 340 + 341 + return 0; 342 + } 343 + EXPORT_SYMBOL_GPL(cros_ec_sensors_read_cmd); 344 + 345 + irqreturn_t cros_ec_sensors_capture(int irq, void *p) 346 + { 347 + struct iio_poll_func *pf = p; 348 + struct iio_dev *indio_dev = pf->indio_dev; 349 + struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 350 + int ret; 351 + 352 + mutex_lock(&st->cmd_lock); 353 + 354 + /* Clear capture data. */ 355 + memset(st->samples, 0, indio_dev->scan_bytes); 356 + 357 + /* Read data based on which channels are enabled in scan mask. */ 358 + ret = st->read_ec_sensors_data(indio_dev, 359 + *(indio_dev->active_scan_mask), 360 + (s16 *)st->samples); 361 + if (ret < 0) 362 + goto done; 363 + 364 + iio_push_to_buffers_with_timestamp(indio_dev, st->samples, 365 + iio_get_time_ns(indio_dev)); 366 + 367 + done: 368 + /* 369 + * Tell the core we are done with this trigger and ready for the 370 + * next one. 371 + */ 372 + iio_trigger_notify_done(indio_dev->trig); 373 + 374 + mutex_unlock(&st->cmd_lock); 375 + 376 + return IRQ_HANDLED; 377 + } 378 + EXPORT_SYMBOL_GPL(cros_ec_sensors_capture); 379 + 380 + int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st, 381 + struct iio_chan_spec const *chan, 382 + int *val, int *val2, long mask) 383 + { 384 + int ret = IIO_VAL_INT; 385 + 386 + switch (mask) { 387 + case IIO_CHAN_INFO_SAMP_FREQ: 388 + st->param.cmd = MOTIONSENSE_CMD_EC_RATE; 389 + st->param.ec_rate.data = 390 + EC_MOTION_SENSE_NO_VALUE; 391 + 392 + if (cros_ec_motion_send_host_cmd(st, 0)) 393 + ret = -EIO; 394 + else 395 + *val = st->resp->ec_rate.ret; 396 + break; 397 + case IIO_CHAN_INFO_FREQUENCY: 398 + st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR; 399 + st->param.sensor_odr.data = 400 + EC_MOTION_SENSE_NO_VALUE; 401 + 402 + if (cros_ec_motion_send_host_cmd(st, 0)) 403 + ret = -EIO; 404 + else 405 + *val = st->resp->sensor_odr.ret; 406 + break; 407 + default: 408 + break; 409 + } 410 + 411 + return ret; 412 + } 413 + EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read); 414 + 415 + int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st, 416 + struct iio_chan_spec const *chan, 417 + int val, int val2, long mask) 418 + { 419 + int ret = 0; 420 + 421 + switch (mask) { 422 + case IIO_CHAN_INFO_FREQUENCY: 423 + st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR; 424 + st->param.sensor_odr.data = val; 425 + 426 + /* Always roundup, so caller gets at least what it asks for. */ 427 + st->param.sensor_odr.roundup = 1; 428 + 429 + if (cros_ec_motion_send_host_cmd(st, 0)) 430 + ret = -EIO; 431 + break; 432 + case IIO_CHAN_INFO_SAMP_FREQ: 433 + st->param.cmd = MOTIONSENSE_CMD_EC_RATE; 434 + st->param.ec_rate.data = val; 435 + 436 + if (cros_ec_motion_send_host_cmd(st, 0)) 437 + ret = -EIO; 438 + else 439 + st->curr_sampl_freq = val; 440 + break; 441 + default: 442 + ret = -EINVAL; 443 + break; 444 + } 445 + return ret; 446 + } 447 + EXPORT_SYMBOL_GPL(cros_ec_sensors_core_write); 448 + 449 + MODULE_DESCRIPTION("ChromeOS EC sensor hub core functions"); 450 + MODULE_LICENSE("GPL v2");
+175
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.h
··· 1 + /* 2 + * ChromeOS EC sensor hub 3 + * 4 + * Copyright (C) 2016 Google, Inc 5 + * 6 + * This software is licensed under the terms of the GNU General Public 7 + * License version 2, as published by the Free Software Foundation, and 8 + * may be copied, distributed, and modified under those terms. 9 + * 10 + * This program is distributed in the hope that it will be useful, 11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 + * GNU General Public License for more details. 14 + */ 15 + 16 + #ifndef __CROS_EC_SENSORS_CORE_H 17 + #define __CROS_EC_SENSORS_CORE_H 18 + 19 + #include <linux/irqreturn.h> 20 + 21 + enum { 22 + CROS_EC_SENSOR_X, 23 + CROS_EC_SENSOR_Y, 24 + CROS_EC_SENSOR_Z, 25 + CROS_EC_SENSOR_MAX_AXIS, 26 + }; 27 + 28 + /* EC returns sensor values using signed 16 bit registers */ 29 + #define CROS_EC_SENSOR_BITS 16 30 + 31 + /* 32 + * 4 16 bit channels are allowed. 33 + * Good enough for current sensors, they use up to 3 16 bit vectors. 34 + */ 35 + #define CROS_EC_SAMPLE_SIZE (sizeof(s64) * 2) 36 + 37 + /* Minimum sampling period to use when device is suspending */ 38 + #define CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY 1000 /* 1 second */ 39 + 40 + /** 41 + * struct cros_ec_sensors_core_state - state data for EC sensors IIO driver 42 + * @ec: cros EC device structure 43 + * @cmd_lock: lock used to prevent simultaneous access to the 44 + * commands. 45 + * @msg: cros EC command structure 46 + * @param: motion sensor parameters structure 47 + * @resp: motion sensor response structure 48 + * @type: type of motion sensor 49 + * @loc: location where the motion sensor is placed 50 + * @calib: calibration parameters. Note that trigger 51 + * captured data will always provide the calibrated 52 + * data 53 + * @samples: static array to hold data from a single capture. 54 + * For each channel we need 2 bytes, except for 55 + * the timestamp. The timestamp is always last and 56 + * is always 8-byte aligned. 57 + * @read_ec_sensors_data: function used for accessing sensors values 58 + * @cuur_sampl_freq: current sampling period 59 + */ 60 + struct cros_ec_sensors_core_state { 61 + struct cros_ec_device *ec; 62 + struct mutex cmd_lock; 63 + 64 + struct cros_ec_command *msg; 65 + struct ec_params_motion_sense param; 66 + struct ec_response_motion_sense *resp; 67 + 68 + enum motionsensor_type type; 69 + enum motionsensor_location loc; 70 + 71 + s16 calib[CROS_EC_SENSOR_MAX_AXIS]; 72 + 73 + u8 samples[CROS_EC_SAMPLE_SIZE]; 74 + 75 + int (*read_ec_sensors_data)(struct iio_dev *indio_dev, 76 + unsigned long scan_mask, s16 *data); 77 + 78 + int curr_sampl_freq; 79 + }; 80 + 81 + /** 82 + * cros_ec_sensors_read_lpc() - retrieve data from EC shared memory 83 + * @indio_dev: pointer to IIO device 84 + * @scan_mask: bitmap of the sensor indices to scan 85 + * @data: location to store data 86 + * 87 + * This is the safe function for reading the EC data. It guarantees that the 88 + * data sampled was not modified by the EC while being read. 89 + * 90 + * Return: 0 on success, -errno on failure. 91 + */ 92 + int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev, unsigned long scan_mask, 93 + s16 *data); 94 + 95 + /** 96 + * cros_ec_sensors_read_cmd() - retrieve data using the EC command protocol 97 + * @indio_dev: pointer to IIO device 98 + * @scan_mask: bitmap of the sensor indices to scan 99 + * @data: location to store data 100 + * 101 + * Return: 0 on success, -errno on failure. 102 + */ 103 + int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev, unsigned long scan_mask, 104 + s16 *data); 105 + 106 + /** 107 + * cros_ec_sensors_core_init() - basic initialization of the core structure 108 + * @pdev: platform device created for the sensors 109 + * @indio_dev: iio device structure of the device 110 + * @physical_device: true if the device refers to a physical device 111 + * 112 + * Return: 0 on success, -errno on failure. 113 + */ 114 + int cros_ec_sensors_core_init(struct platform_device *pdev, 115 + struct iio_dev *indio_dev, bool physical_device); 116 + 117 + /** 118 + * cros_ec_sensors_capture() - the trigger handler function 119 + * @irq: the interrupt number. 120 + * @p: a pointer to the poll function. 121 + * 122 + * On a trigger event occurring, if the pollfunc is attached then this 123 + * handler is called as a threaded interrupt (and hence may sleep). It 124 + * is responsible for grabbing data from the device and pushing it into 125 + * the associated buffer. 126 + * 127 + * Return: IRQ_HANDLED 128 + */ 129 + irqreturn_t cros_ec_sensors_capture(int irq, void *p); 130 + 131 + /** 132 + * cros_ec_motion_send_host_cmd() - send motion sense host command 133 + * @st: pointer to state information for device 134 + * @opt_length: optional length to reduce the response size, useful on the data 135 + * path. Otherwise, the maximal allowed response size is used 136 + * 137 + * When called, the sub-command is assumed to be set in param->cmd. 138 + * 139 + * Return: 0 on success, -errno on failure. 140 + */ 141 + int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *st, 142 + u16 opt_length); 143 + 144 + /** 145 + * cros_ec_sensors_core_read() - function to request a value from the sensor 146 + * @st: pointer to state information for device 147 + * @chan: channel specification structure table 148 + * @val: will contain one element making up the returned value 149 + * @val2: will contain another element making up the returned value 150 + * @mask: specifies which values to be requested 151 + * 152 + * Return: the type of value returned by the device 153 + */ 154 + int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st, 155 + struct iio_chan_spec const *chan, 156 + int *val, int *val2, long mask); 157 + 158 + /** 159 + * cros_ec_sensors_core_write() - function to write a value to the sensor 160 + * @st: pointer to state information for device 161 + * @chan: channel specification structure table 162 + * @val: first part of value to write 163 + * @val2: second part of value to write 164 + * @mask: specifies which values to write 165 + * 166 + * Return: the type of value returned by the device 167 + */ 168 + int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st, 169 + struct iio_chan_spec const *chan, 170 + int val, int val2, long mask); 171 + 172 + /* List of extended channel specification for all sensors */ 173 + extern const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[]; 174 + 175 + #endif /* __CROS_EC_SENSORS_CORE_H */
+4 -1
drivers/iio/common/hid-sensors/hid-sensor-attributes.c
··· 201 201 int ret; 202 202 203 203 if (val1 < 0 || val2 < 0) 204 - ret = -EINVAL; 204 + return -EINVAL; 205 205 206 206 value = val1 * pow_10(6) + val2; 207 207 if (value) { ··· 249 249 { 250 250 s32 value; 251 251 int ret; 252 + 253 + if (val1 < 0 || val2 < 0) 254 + return -EINVAL; 252 255 253 256 value = convert_to_vtf_format(st->sensitivity.size, 254 257 st->sensitivity.unit_expo,
+17
drivers/iio/gyro/Kconfig
··· 84 84 Say yes here to build support for the HID SENSOR 85 85 Gyroscope 3D. 86 86 87 + config MPU3050 88 + tristate 89 + select IIO_BUFFER 90 + select IIO_TRIGGERED_BUFFER 91 + select REGMAP 92 + 93 + config MPU3050_I2C 94 + tristate "Invensense MPU3050 devices on I2C" 95 + depends on !(INPUT_MPU3050=y || INPUT_MPU3050=m) 96 + select MPU3050 97 + select REGMAP_I2C 98 + select I2C_MUX 99 + help 100 + This driver supports the Invensense MPU3050 gyroscope over I2C. 101 + This driver can be built as a module. The module will be called 102 + inv-mpu3050-i2c. 103 + 87 104 config IIO_ST_GYRO_3AXIS 88 105 tristate "STMicroelectronics gyroscopes 3-Axis Driver" 89 106 depends on (I2C || SPI_MASTER) && SYSFS
+5
drivers/iio/gyro/Makefile
··· 14 14 15 15 obj-$(CONFIG_HID_SENSOR_GYRO_3D) += hid-sensor-gyro-3d.o 16 16 17 + # Currently this is rolled into one module, split it if 18 + # we ever create a separate SPI interface for MPU-3050 19 + obj-$(CONFIG_MPU3050) += mpu3050.o 20 + mpu3050-objs := mpu3050-core.o mpu3050-i2c.o 21 + 17 22 itg3200-y := itg3200_core.o 18 23 itg3200-$(CONFIG_IIO_BUFFER) += itg3200_buffer.o 19 24 obj-$(CONFIG_ITG3200) += itg3200.o
+1307
drivers/iio/gyro/mpu3050-core.c
··· 1 + /* 2 + * MPU3050 gyroscope driver 3 + * 4 + * Copyright (C) 2016 Linaro Ltd. 5 + * Author: Linus Walleij <linus.walleij@linaro.org> 6 + * 7 + * Based on the input subsystem driver, Copyright (C) 2011 Wistron Co.Ltd 8 + * Joseph Lai <joseph_lai@wistron.com> and trimmed down by 9 + * Alan Cox <alan@linux.intel.com> in turn based on bma023.c. 10 + * Device behaviour based on a misc driver posted by Nathan Royer in 2011. 11 + * 12 + * TODO: add support for setting up the low pass 3dB frequency. 13 + */ 14 + 15 + #include <linux/bitops.h> 16 + #include <linux/delay.h> 17 + #include <linux/err.h> 18 + #include <linux/iio/buffer.h> 19 + #include <linux/iio/iio.h> 20 + #include <linux/iio/sysfs.h> 21 + #include <linux/iio/trigger.h> 22 + #include <linux/iio/trigger_consumer.h> 23 + #include <linux/iio/triggered_buffer.h> 24 + #include <linux/interrupt.h> 25 + #include <linux/module.h> 26 + #include <linux/pm_runtime.h> 27 + #include <linux/random.h> 28 + #include <linux/slab.h> 29 + 30 + #include "mpu3050.h" 31 + 32 + #define MPU3050_CHIP_ID 0x69 33 + 34 + /* 35 + * Register map: anything suffixed *_H is a big-endian high byte and always 36 + * followed by the corresponding low byte (*_L) even though these are not 37 + * explicitly included in the register definitions. 38 + */ 39 + #define MPU3050_CHIP_ID_REG 0x00 40 + #define MPU3050_PRODUCT_ID_REG 0x01 41 + #define MPU3050_XG_OFFS_TC 0x05 42 + #define MPU3050_YG_OFFS_TC 0x08 43 + #define MPU3050_ZG_OFFS_TC 0x0B 44 + #define MPU3050_X_OFFS_USR_H 0x0C 45 + #define MPU3050_Y_OFFS_USR_H 0x0E 46 + #define MPU3050_Z_OFFS_USR_H 0x10 47 + #define MPU3050_FIFO_EN 0x12 48 + #define MPU3050_AUX_VDDIO 0x13 49 + #define MPU3050_SLV_ADDR 0x14 50 + #define MPU3050_SMPLRT_DIV 0x15 51 + #define MPU3050_DLPF_FS_SYNC 0x16 52 + #define MPU3050_INT_CFG 0x17 53 + #define MPU3050_AUX_ADDR 0x18 54 + #define MPU3050_INT_STATUS 0x1A 55 + #define MPU3050_TEMP_H 0x1B 56 + #define MPU3050_XOUT_H 0x1D 57 + #define MPU3050_YOUT_H 0x1F 58 + #define MPU3050_ZOUT_H 0x21 59 + #define MPU3050_DMP_CFG1 0x35 60 + #define MPU3050_DMP_CFG2 0x36 61 + #define MPU3050_BANK_SEL 0x37 62 + #define MPU3050_MEM_START_ADDR 0x38 63 + #define MPU3050_MEM_R_W 0x39 64 + #define MPU3050_FIFO_COUNT_H 0x3A 65 + #define MPU3050_FIFO_R 0x3C 66 + #define MPU3050_USR_CTRL 0x3D 67 + #define MPU3050_PWR_MGM 0x3E 68 + 69 + /* MPU memory bank read options */ 70 + #define MPU3050_MEM_PRFTCH BIT(5) 71 + #define MPU3050_MEM_USER_BANK BIT(4) 72 + /* Bits 8-11 select memory bank */ 73 + #define MPU3050_MEM_RAM_BANK_0 0 74 + #define MPU3050_MEM_RAM_BANK_1 1 75 + #define MPU3050_MEM_RAM_BANK_2 2 76 + #define MPU3050_MEM_RAM_BANK_3 3 77 + #define MPU3050_MEM_OTP_BANK_0 4 78 + 79 + #define MPU3050_AXIS_REGS(axis) (MPU3050_XOUT_H + (axis * 2)) 80 + 81 + /* Register bits */ 82 + 83 + /* FIFO Enable */ 84 + #define MPU3050_FIFO_EN_FOOTER BIT(0) 85 + #define MPU3050_FIFO_EN_AUX_ZOUT BIT(1) 86 + #define MPU3050_FIFO_EN_AUX_YOUT BIT(2) 87 + #define MPU3050_FIFO_EN_AUX_XOUT BIT(3) 88 + #define MPU3050_FIFO_EN_GYRO_ZOUT BIT(4) 89 + #define MPU3050_FIFO_EN_GYRO_YOUT BIT(5) 90 + #define MPU3050_FIFO_EN_GYRO_XOUT BIT(6) 91 + #define MPU3050_FIFO_EN_TEMP_OUT BIT(7) 92 + 93 + /* 94 + * Digital Low Pass filter (DLPF) 95 + * Full Scale (FS) 96 + * and Synchronization 97 + */ 98 + #define MPU3050_EXT_SYNC_NONE 0x00 99 + #define MPU3050_EXT_SYNC_TEMP 0x20 100 + #define MPU3050_EXT_SYNC_GYROX 0x40 101 + #define MPU3050_EXT_SYNC_GYROY 0x60 102 + #define MPU3050_EXT_SYNC_GYROZ 0x80 103 + #define MPU3050_EXT_SYNC_ACCELX 0xA0 104 + #define MPU3050_EXT_SYNC_ACCELY 0xC0 105 + #define MPU3050_EXT_SYNC_ACCELZ 0xE0 106 + #define MPU3050_EXT_SYNC_MASK 0xE0 107 + #define MPU3050_EXT_SYNC_SHIFT 5 108 + 109 + #define MPU3050_FS_250DPS 0x00 110 + #define MPU3050_FS_500DPS 0x08 111 + #define MPU3050_FS_1000DPS 0x10 112 + #define MPU3050_FS_2000DPS 0x18 113 + #define MPU3050_FS_MASK 0x18 114 + #define MPU3050_FS_SHIFT 3 115 + 116 + #define MPU3050_DLPF_CFG_256HZ_NOLPF2 0x00 117 + #define MPU3050_DLPF_CFG_188HZ 0x01 118 + #define MPU3050_DLPF_CFG_98HZ 0x02 119 + #define MPU3050_DLPF_CFG_42HZ 0x03 120 + #define MPU3050_DLPF_CFG_20HZ 0x04 121 + #define MPU3050_DLPF_CFG_10HZ 0x05 122 + #define MPU3050_DLPF_CFG_5HZ 0x06 123 + #define MPU3050_DLPF_CFG_2100HZ_NOLPF 0x07 124 + #define MPU3050_DLPF_CFG_MASK 0x07 125 + #define MPU3050_DLPF_CFG_SHIFT 0 126 + 127 + /* Interrupt config */ 128 + #define MPU3050_INT_RAW_RDY_EN BIT(0) 129 + #define MPU3050_INT_DMP_DONE_EN BIT(1) 130 + #define MPU3050_INT_MPU_RDY_EN BIT(2) 131 + #define MPU3050_INT_ANYRD_2CLEAR BIT(4) 132 + #define MPU3050_INT_LATCH_EN BIT(5) 133 + #define MPU3050_INT_OPEN BIT(6) 134 + #define MPU3050_INT_ACTL BIT(7) 135 + /* Interrupt status */ 136 + #define MPU3050_INT_STATUS_RAW_RDY BIT(0) 137 + #define MPU3050_INT_STATUS_DMP_DONE BIT(1) 138 + #define MPU3050_INT_STATUS_MPU_RDY BIT(2) 139 + #define MPU3050_INT_STATUS_FIFO_OVFLW BIT(7) 140 + /* USR_CTRL */ 141 + #define MPU3050_USR_CTRL_FIFO_EN BIT(6) 142 + #define MPU3050_USR_CTRL_AUX_IF_EN BIT(5) 143 + #define MPU3050_USR_CTRL_AUX_IF_RST BIT(3) 144 + #define MPU3050_USR_CTRL_FIFO_RST BIT(1) 145 + #define MPU3050_USR_CTRL_GYRO_RST BIT(0) 146 + /* PWR_MGM */ 147 + #define MPU3050_PWR_MGM_PLL_X 0x01 148 + #define MPU3050_PWR_MGM_PLL_Y 0x02 149 + #define MPU3050_PWR_MGM_PLL_Z 0x03 150 + #define MPU3050_PWR_MGM_CLKSEL_MASK 0x07 151 + #define MPU3050_PWR_MGM_STBY_ZG BIT(3) 152 + #define MPU3050_PWR_MGM_STBY_YG BIT(4) 153 + #define MPU3050_PWR_MGM_STBY_XG BIT(5) 154 + #define MPU3050_PWR_MGM_SLEEP BIT(6) 155 + #define MPU3050_PWR_MGM_RESET BIT(7) 156 + #define MPU3050_PWR_MGM_MASK 0xff 157 + 158 + /* 159 + * Fullscale precision is (for finest precision) +/- 250 deg/s, so the full 160 + * scale is actually 500 deg/s. All 16 bits are then used to cover this scale, 161 + * in two's complement. 162 + */ 163 + static unsigned int mpu3050_fs_precision[] = { 164 + IIO_DEGREE_TO_RAD(250), 165 + IIO_DEGREE_TO_RAD(500), 166 + IIO_DEGREE_TO_RAD(1000), 167 + IIO_DEGREE_TO_RAD(2000) 168 + }; 169 + 170 + /* 171 + * Regulator names 172 + */ 173 + static const char mpu3050_reg_vdd[] = "vdd"; 174 + static const char mpu3050_reg_vlogic[] = "vlogic"; 175 + 176 + static unsigned int mpu3050_get_freq(struct mpu3050 *mpu3050) 177 + { 178 + unsigned int freq; 179 + 180 + if (mpu3050->lpf == MPU3050_DLPF_CFG_256HZ_NOLPF2) 181 + freq = 8000; 182 + else 183 + freq = 1000; 184 + freq /= (mpu3050->divisor + 1); 185 + 186 + return freq; 187 + } 188 + 189 + static int mpu3050_start_sampling(struct mpu3050 *mpu3050) 190 + { 191 + __be16 raw_val[3]; 192 + int ret; 193 + int i; 194 + 195 + /* Reset */ 196 + ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM, 197 + MPU3050_PWR_MGM_RESET, MPU3050_PWR_MGM_RESET); 198 + if (ret) 199 + return ret; 200 + 201 + /* Turn on the Z-axis PLL */ 202 + ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM, 203 + MPU3050_PWR_MGM_CLKSEL_MASK, 204 + MPU3050_PWR_MGM_PLL_Z); 205 + if (ret) 206 + return ret; 207 + 208 + /* Write calibration offset registers */ 209 + for (i = 0; i < 3; i++) 210 + raw_val[i] = cpu_to_be16(mpu3050->calibration[i]); 211 + 212 + ret = regmap_bulk_write(mpu3050->map, MPU3050_X_OFFS_USR_H, raw_val, 213 + sizeof(raw_val)); 214 + if (ret) 215 + return ret; 216 + 217 + /* Set low pass filter (sample rate), sync and full scale */ 218 + ret = regmap_write(mpu3050->map, MPU3050_DLPF_FS_SYNC, 219 + MPU3050_EXT_SYNC_NONE << MPU3050_EXT_SYNC_SHIFT | 220 + mpu3050->fullscale << MPU3050_FS_SHIFT | 221 + mpu3050->lpf << MPU3050_DLPF_CFG_SHIFT); 222 + if (ret) 223 + return ret; 224 + 225 + /* Set up sampling frequency */ 226 + ret = regmap_write(mpu3050->map, MPU3050_SMPLRT_DIV, mpu3050->divisor); 227 + if (ret) 228 + return ret; 229 + 230 + /* 231 + * Max 50 ms start-up time after setting DLPF_FS_SYNC 232 + * according to the data sheet, then wait for the next sample 233 + * at this frequency T = 1000/f ms. 234 + */ 235 + msleep(50 + 1000 / mpu3050_get_freq(mpu3050)); 236 + 237 + return 0; 238 + } 239 + 240 + static int mpu3050_set_8khz_samplerate(struct mpu3050 *mpu3050) 241 + { 242 + int ret; 243 + u8 divisor; 244 + enum mpu3050_lpf lpf; 245 + 246 + lpf = mpu3050->lpf; 247 + divisor = mpu3050->divisor; 248 + 249 + mpu3050->lpf = LPF_256_HZ_NOLPF; /* 8 kHz base frequency */ 250 + mpu3050->divisor = 0; /* Divide by 1 */ 251 + ret = mpu3050_start_sampling(mpu3050); 252 + 253 + mpu3050->lpf = lpf; 254 + mpu3050->divisor = divisor; 255 + 256 + return ret; 257 + } 258 + 259 + static int mpu3050_read_raw(struct iio_dev *indio_dev, 260 + struct iio_chan_spec const *chan, 261 + int *val, int *val2, 262 + long mask) 263 + { 264 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 265 + int ret; 266 + __be16 raw_val; 267 + 268 + switch (mask) { 269 + case IIO_CHAN_INFO_OFFSET: 270 + switch (chan->type) { 271 + case IIO_TEMP: 272 + /* The temperature scaling is (x+23000)/280 Celsius */ 273 + *val = 23000; 274 + return IIO_VAL_INT; 275 + default: 276 + return -EINVAL; 277 + } 278 + case IIO_CHAN_INFO_CALIBBIAS: 279 + switch (chan->type) { 280 + case IIO_ANGL_VEL: 281 + *val = mpu3050->calibration[chan->scan_index-1]; 282 + return IIO_VAL_INT; 283 + default: 284 + return -EINVAL; 285 + } 286 + case IIO_CHAN_INFO_SAMP_FREQ: 287 + *val = mpu3050_get_freq(mpu3050); 288 + return IIO_VAL_INT; 289 + case IIO_CHAN_INFO_SCALE: 290 + switch (chan->type) { 291 + case IIO_TEMP: 292 + /* Millidegrees, see about temperature scaling above */ 293 + *val = 1000; 294 + *val2 = 280; 295 + return IIO_VAL_FRACTIONAL; 296 + case IIO_ANGL_VEL: 297 + /* 298 + * Convert to the corresponding full scale in 299 + * radians. All 16 bits are used with sign to 300 + * span the available scale: to account for the one 301 + * missing value if we multiply by 1/S16_MAX, instead 302 + * multiply with 2/U16_MAX. 303 + */ 304 + *val = mpu3050_fs_precision[mpu3050->fullscale] * 2; 305 + *val2 = U16_MAX; 306 + return IIO_VAL_FRACTIONAL; 307 + default: 308 + return -EINVAL; 309 + } 310 + case IIO_CHAN_INFO_RAW: 311 + /* Resume device */ 312 + pm_runtime_get_sync(mpu3050->dev); 313 + mutex_lock(&mpu3050->lock); 314 + 315 + ret = mpu3050_set_8khz_samplerate(mpu3050); 316 + if (ret) 317 + goto out_read_raw_unlock; 318 + 319 + switch (chan->type) { 320 + case IIO_TEMP: 321 + ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H, 322 + &raw_val, sizeof(raw_val)); 323 + if (ret) { 324 + dev_err(mpu3050->dev, 325 + "error reading temperature\n"); 326 + goto out_read_raw_unlock; 327 + } 328 + 329 + *val = be16_to_cpu(raw_val); 330 + ret = IIO_VAL_INT; 331 + 332 + goto out_read_raw_unlock; 333 + case IIO_ANGL_VEL: 334 + ret = regmap_bulk_read(mpu3050->map, 335 + MPU3050_AXIS_REGS(chan->scan_index-1), 336 + &raw_val, 337 + sizeof(raw_val)); 338 + if (ret) { 339 + dev_err(mpu3050->dev, 340 + "error reading axis data\n"); 341 + goto out_read_raw_unlock; 342 + } 343 + 344 + *val = be16_to_cpu(raw_val); 345 + ret = IIO_VAL_INT; 346 + 347 + goto out_read_raw_unlock; 348 + default: 349 + ret = -EINVAL; 350 + goto out_read_raw_unlock; 351 + } 352 + default: 353 + break; 354 + } 355 + 356 + return -EINVAL; 357 + 358 + out_read_raw_unlock: 359 + mutex_unlock(&mpu3050->lock); 360 + pm_runtime_mark_last_busy(mpu3050->dev); 361 + pm_runtime_put_autosuspend(mpu3050->dev); 362 + 363 + return ret; 364 + } 365 + 366 + static int mpu3050_write_raw(struct iio_dev *indio_dev, 367 + const struct iio_chan_spec *chan, 368 + int val, int val2, long mask) 369 + { 370 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 371 + /* 372 + * Couldn't figure out a way to precalculate these at compile time. 373 + */ 374 + unsigned int fs250 = 375 + DIV_ROUND_CLOSEST(mpu3050_fs_precision[0] * 1000000 * 2, 376 + U16_MAX); 377 + unsigned int fs500 = 378 + DIV_ROUND_CLOSEST(mpu3050_fs_precision[1] * 1000000 * 2, 379 + U16_MAX); 380 + unsigned int fs1000 = 381 + DIV_ROUND_CLOSEST(mpu3050_fs_precision[2] * 1000000 * 2, 382 + U16_MAX); 383 + unsigned int fs2000 = 384 + DIV_ROUND_CLOSEST(mpu3050_fs_precision[3] * 1000000 * 2, 385 + U16_MAX); 386 + 387 + switch (mask) { 388 + case IIO_CHAN_INFO_CALIBBIAS: 389 + if (chan->type != IIO_ANGL_VEL) 390 + return -EINVAL; 391 + mpu3050->calibration[chan->scan_index-1] = val; 392 + return 0; 393 + case IIO_CHAN_INFO_SAMP_FREQ: 394 + /* 395 + * The max samplerate is 8000 Hz, the minimum 396 + * 1000 / 256 ~= 4 Hz 397 + */ 398 + if (val < 4 || val > 8000) 399 + return -EINVAL; 400 + 401 + /* 402 + * Above 1000 Hz we must turn off the digital low pass filter 403 + * so we get a base frequency of 8kHz to the divider 404 + */ 405 + if (val > 1000) { 406 + mpu3050->lpf = LPF_256_HZ_NOLPF; 407 + mpu3050->divisor = DIV_ROUND_CLOSEST(8000, val) - 1; 408 + return 0; 409 + } 410 + 411 + mpu3050->lpf = LPF_188_HZ; 412 + mpu3050->divisor = DIV_ROUND_CLOSEST(1000, val) - 1; 413 + return 0; 414 + case IIO_CHAN_INFO_SCALE: 415 + if (chan->type != IIO_ANGL_VEL) 416 + return -EINVAL; 417 + /* 418 + * We support +/-250, +/-500, +/-1000 and +/2000 deg/s 419 + * which means we need to round to the closest radians 420 + * which will be roughly +/-4.3, +/-8.7, +/-17.5, +/-35 421 + * rad/s. The scale is then for the 16 bits used to cover 422 + * it 2/(2^16) of that. 423 + */ 424 + 425 + /* Just too large, set the max range */ 426 + if (val != 0) { 427 + mpu3050->fullscale = FS_2000_DPS; 428 + return 0; 429 + } 430 + 431 + /* 432 + * Now we're dealing with fractions below zero in millirad/s 433 + * do some integer interpolation and match with the closest 434 + * fullscale in the table. 435 + */ 436 + if (val2 <= fs250 || 437 + val2 < ((fs500 + fs250) / 2)) 438 + mpu3050->fullscale = FS_250_DPS; 439 + else if (val2 <= fs500 || 440 + val2 < ((fs1000 + fs500) / 2)) 441 + mpu3050->fullscale = FS_500_DPS; 442 + else if (val2 <= fs1000 || 443 + val2 < ((fs2000 + fs1000) / 2)) 444 + mpu3050->fullscale = FS_1000_DPS; 445 + else 446 + /* Catch-all */ 447 + mpu3050->fullscale = FS_2000_DPS; 448 + return 0; 449 + default: 450 + break; 451 + } 452 + 453 + return -EINVAL; 454 + } 455 + 456 + static irqreturn_t mpu3050_trigger_handler(int irq, void *p) 457 + { 458 + const struct iio_poll_func *pf = p; 459 + struct iio_dev *indio_dev = pf->indio_dev; 460 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 461 + int ret; 462 + /* 463 + * Temperature 1*16 bits 464 + * Three axes 3*16 bits 465 + * Timestamp 64 bits (4*16 bits) 466 + * Sum total 8*16 bits 467 + */ 468 + __be16 hw_values[8]; 469 + s64 timestamp; 470 + unsigned int datums_from_fifo = 0; 471 + 472 + /* 473 + * If we're using the hardware trigger, get the precise timestamp from 474 + * the top half of the threaded IRQ handler. Otherwise get the 475 + * timestamp here so it will be close in time to the actual values 476 + * read from the registers. 477 + */ 478 + if (iio_trigger_using_own(indio_dev)) 479 + timestamp = mpu3050->hw_timestamp; 480 + else 481 + timestamp = iio_get_time_ns(indio_dev); 482 + 483 + mutex_lock(&mpu3050->lock); 484 + 485 + /* Using the hardware IRQ trigger? Check the buffer then. */ 486 + if (mpu3050->hw_irq_trigger) { 487 + __be16 raw_fifocnt; 488 + u16 fifocnt; 489 + /* X, Y, Z + temperature */ 490 + unsigned int bytes_per_datum = 8; 491 + bool fifo_overflow = false; 492 + 493 + ret = regmap_bulk_read(mpu3050->map, 494 + MPU3050_FIFO_COUNT_H, 495 + &raw_fifocnt, 496 + sizeof(raw_fifocnt)); 497 + if (ret) 498 + goto out_trigger_unlock; 499 + fifocnt = be16_to_cpu(raw_fifocnt); 500 + 501 + if (fifocnt == 512) { 502 + dev_info(mpu3050->dev, 503 + "FIFO overflow! Emptying and resetting FIFO\n"); 504 + fifo_overflow = true; 505 + /* Reset and enable the FIFO */ 506 + ret = regmap_update_bits(mpu3050->map, 507 + MPU3050_USR_CTRL, 508 + MPU3050_USR_CTRL_FIFO_EN | 509 + MPU3050_USR_CTRL_FIFO_RST, 510 + MPU3050_USR_CTRL_FIFO_EN | 511 + MPU3050_USR_CTRL_FIFO_RST); 512 + if (ret) { 513 + dev_info(mpu3050->dev, "error resetting FIFO\n"); 514 + goto out_trigger_unlock; 515 + } 516 + mpu3050->pending_fifo_footer = false; 517 + } 518 + 519 + if (fifocnt) 520 + dev_dbg(mpu3050->dev, 521 + "%d bytes in the FIFO\n", 522 + fifocnt); 523 + 524 + while (!fifo_overflow && fifocnt > bytes_per_datum) { 525 + unsigned int toread; 526 + unsigned int offset; 527 + __be16 fifo_values[5]; 528 + 529 + /* 530 + * If there is a FIFO footer in the pipe, first clear 531 + * that out. This follows the complex algorithm in the 532 + * datasheet that states that you may never leave the 533 + * FIFO empty after the first reading: you have to 534 + * always leave two footer bytes in it. The footer is 535 + * in practice just two zero bytes. 536 + */ 537 + if (mpu3050->pending_fifo_footer) { 538 + toread = bytes_per_datum + 2; 539 + offset = 0; 540 + } else { 541 + toread = bytes_per_datum; 542 + offset = 1; 543 + /* Put in some dummy value */ 544 + fifo_values[0] = 0xAAAA; 545 + } 546 + 547 + ret = regmap_bulk_read(mpu3050->map, 548 + MPU3050_FIFO_R, 549 + &fifo_values[offset], 550 + toread); 551 + 552 + dev_dbg(mpu3050->dev, 553 + "%04x %04x %04x %04x %04x\n", 554 + fifo_values[0], 555 + fifo_values[1], 556 + fifo_values[2], 557 + fifo_values[3], 558 + fifo_values[4]); 559 + 560 + /* Index past the footer (fifo_values[0]) and push */ 561 + iio_push_to_buffers_with_timestamp(indio_dev, 562 + &fifo_values[1], 563 + timestamp); 564 + 565 + fifocnt -= toread; 566 + datums_from_fifo++; 567 + mpu3050->pending_fifo_footer = true; 568 + 569 + /* 570 + * If we're emptying the FIFO, just make sure to 571 + * check if something new appeared. 572 + */ 573 + if (fifocnt < bytes_per_datum) { 574 + ret = regmap_bulk_read(mpu3050->map, 575 + MPU3050_FIFO_COUNT_H, 576 + &raw_fifocnt, 577 + sizeof(raw_fifocnt)); 578 + if (ret) 579 + goto out_trigger_unlock; 580 + fifocnt = be16_to_cpu(raw_fifocnt); 581 + } 582 + 583 + if (fifocnt < bytes_per_datum) 584 + dev_dbg(mpu3050->dev, 585 + "%d bytes left in the FIFO\n", 586 + fifocnt); 587 + 588 + /* 589 + * At this point, the timestamp that triggered the 590 + * hardware interrupt is no longer valid for what 591 + * we are reading (the interrupt likely fired for 592 + * the value on the top of the FIFO), so set the 593 + * timestamp to zero and let userspace deal with it. 594 + */ 595 + timestamp = 0; 596 + } 597 + } 598 + 599 + /* 600 + * If we picked some datums from the FIFO that's enough, else 601 + * fall through and just read from the current value registers. 602 + * This happens in two cases: 603 + * 604 + * - We are using some other trigger (external, like an HRTimer) 605 + * than the sensor's own sample generator. In this case the 606 + * sensor is just set to the max sampling frequency and we give 607 + * the trigger a copy of the latest value every time we get here. 608 + * 609 + * - The hardware trigger is active but unused and we actually use 610 + * another trigger which calls here with a frequency higher 611 + * than what the device provides data. We will then just read 612 + * duplicate values directly from the hardware registers. 613 + */ 614 + if (datums_from_fifo) { 615 + dev_dbg(mpu3050->dev, 616 + "read %d datums from the FIFO\n", 617 + datums_from_fifo); 618 + goto out_trigger_unlock; 619 + } 620 + 621 + ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H, &hw_values, 622 + sizeof(hw_values)); 623 + if (ret) { 624 + dev_err(mpu3050->dev, 625 + "error reading axis data\n"); 626 + goto out_trigger_unlock; 627 + } 628 + 629 + iio_push_to_buffers_with_timestamp(indio_dev, hw_values, timestamp); 630 + 631 + out_trigger_unlock: 632 + mutex_unlock(&mpu3050->lock); 633 + iio_trigger_notify_done(indio_dev->trig); 634 + 635 + return IRQ_HANDLED; 636 + } 637 + 638 + static int mpu3050_buffer_preenable(struct iio_dev *indio_dev) 639 + { 640 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 641 + 642 + pm_runtime_get_sync(mpu3050->dev); 643 + 644 + /* Unless we have OUR trigger active, run at full speed */ 645 + if (!mpu3050->hw_irq_trigger) 646 + return mpu3050_set_8khz_samplerate(mpu3050); 647 + 648 + return 0; 649 + } 650 + 651 + static int mpu3050_buffer_postdisable(struct iio_dev *indio_dev) 652 + { 653 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 654 + 655 + pm_runtime_mark_last_busy(mpu3050->dev); 656 + pm_runtime_put_autosuspend(mpu3050->dev); 657 + 658 + return 0; 659 + } 660 + 661 + static const struct iio_buffer_setup_ops mpu3050_buffer_setup_ops = { 662 + .preenable = mpu3050_buffer_preenable, 663 + .postenable = iio_triggered_buffer_postenable, 664 + .predisable = iio_triggered_buffer_predisable, 665 + .postdisable = mpu3050_buffer_postdisable, 666 + }; 667 + 668 + static const struct iio_mount_matrix * 669 + mpu3050_get_mount_matrix(const struct iio_dev *indio_dev, 670 + const struct iio_chan_spec *chan) 671 + { 672 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 673 + 674 + return &mpu3050->orientation; 675 + } 676 + 677 + static const struct iio_chan_spec_ext_info mpu3050_ext_info[] = { 678 + IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, mpu3050_get_mount_matrix), 679 + { }, 680 + }; 681 + 682 + #define MPU3050_AXIS_CHANNEL(axis, index) \ 683 + { \ 684 + .type = IIO_ANGL_VEL, \ 685 + .modified = 1, \ 686 + .channel2 = IIO_MOD_##axis, \ 687 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 688 + BIT(IIO_CHAN_INFO_CALIBBIAS), \ 689 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 690 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\ 691 + .ext_info = mpu3050_ext_info, \ 692 + .scan_index = index, \ 693 + .scan_type = { \ 694 + .sign = 's', \ 695 + .realbits = 16, \ 696 + .storagebits = 16, \ 697 + .endianness = IIO_BE, \ 698 + }, \ 699 + } 700 + 701 + static const struct iio_chan_spec mpu3050_channels[] = { 702 + { 703 + .type = IIO_TEMP, 704 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 705 + BIT(IIO_CHAN_INFO_SCALE) | 706 + BIT(IIO_CHAN_INFO_OFFSET), 707 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 708 + .scan_index = 0, 709 + .scan_type = { 710 + .sign = 's', 711 + .realbits = 16, 712 + .storagebits = 16, 713 + .endianness = IIO_BE, 714 + }, 715 + }, 716 + MPU3050_AXIS_CHANNEL(X, 1), 717 + MPU3050_AXIS_CHANNEL(Y, 2), 718 + MPU3050_AXIS_CHANNEL(Z, 3), 719 + IIO_CHAN_SOFT_TIMESTAMP(4), 720 + }; 721 + 722 + /* Four channels apart from timestamp, scan mask = 0x0f */ 723 + static const unsigned long mpu3050_scan_masks[] = { 0xf, 0 }; 724 + 725 + /* 726 + * These are just the hardcoded factors resulting from the more elaborate 727 + * calculations done with fractions in the scale raw get/set functions. 728 + */ 729 + static IIO_CONST_ATTR(anglevel_scale_available, 730 + "0.000122070 " 731 + "0.000274658 " 732 + "0.000518798 " 733 + "0.001068115"); 734 + 735 + static struct attribute *mpu3050_attributes[] = { 736 + &iio_const_attr_anglevel_scale_available.dev_attr.attr, 737 + NULL, 738 + }; 739 + 740 + static const struct attribute_group mpu3050_attribute_group = { 741 + .attrs = mpu3050_attributes, 742 + }; 743 + 744 + static const struct iio_info mpu3050_info = { 745 + .driver_module = THIS_MODULE, 746 + .read_raw = mpu3050_read_raw, 747 + .write_raw = mpu3050_write_raw, 748 + .attrs = &mpu3050_attribute_group, 749 + .driver_module = THIS_MODULE, 750 + }; 751 + 752 + /** 753 + * mpu3050_read_mem() - read MPU-3050 internal memory 754 + * @mpu3050: device to read from 755 + * @bank: target bank 756 + * @addr: target address 757 + * @len: number of bytes 758 + * @buf: the buffer to store the read bytes in 759 + */ 760 + static int mpu3050_read_mem(struct mpu3050 *mpu3050, 761 + u8 bank, 762 + u8 addr, 763 + u8 len, 764 + u8 *buf) 765 + { 766 + int ret; 767 + 768 + ret = regmap_write(mpu3050->map, 769 + MPU3050_BANK_SEL, 770 + bank); 771 + if (ret) 772 + return ret; 773 + 774 + ret = regmap_write(mpu3050->map, 775 + MPU3050_MEM_START_ADDR, 776 + addr); 777 + if (ret) 778 + return ret; 779 + 780 + return regmap_bulk_read(mpu3050->map, 781 + MPU3050_MEM_R_W, 782 + buf, 783 + len); 784 + } 785 + 786 + static int mpu3050_hw_init(struct mpu3050 *mpu3050) 787 + { 788 + int ret; 789 + u8 otp[8]; 790 + 791 + /* Reset */ 792 + ret = regmap_update_bits(mpu3050->map, 793 + MPU3050_PWR_MGM, 794 + MPU3050_PWR_MGM_RESET, 795 + MPU3050_PWR_MGM_RESET); 796 + if (ret) 797 + return ret; 798 + 799 + /* Turn on the PLL */ 800 + ret = regmap_update_bits(mpu3050->map, 801 + MPU3050_PWR_MGM, 802 + MPU3050_PWR_MGM_CLKSEL_MASK, 803 + MPU3050_PWR_MGM_PLL_Z); 804 + if (ret) 805 + return ret; 806 + 807 + /* Disable IRQs */ 808 + ret = regmap_write(mpu3050->map, 809 + MPU3050_INT_CFG, 810 + 0); 811 + if (ret) 812 + return ret; 813 + 814 + /* Read out the 8 bytes of OTP (one-time-programmable) memory */ 815 + ret = mpu3050_read_mem(mpu3050, 816 + (MPU3050_MEM_PRFTCH | 817 + MPU3050_MEM_USER_BANK | 818 + MPU3050_MEM_OTP_BANK_0), 819 + 0, 820 + sizeof(otp), 821 + otp); 822 + if (ret) 823 + return ret; 824 + 825 + /* This is device-unique data so it goes into the entropy pool */ 826 + add_device_randomness(otp, sizeof(otp)); 827 + 828 + dev_info(mpu3050->dev, 829 + "die ID: %04X, wafer ID: %02X, A lot ID: %04X, " 830 + "W lot ID: %03X, WP ID: %01X, rev ID: %02X\n", 831 + /* Die ID, bits 0-12 */ 832 + (otp[1] << 8 | otp[0]) & 0x1fff, 833 + /* Wafer ID, bits 13-17 */ 834 + ((otp[2] << 8 | otp[1]) & 0x03e0) >> 5, 835 + /* A lot ID, bits 18-33 */ 836 + ((otp[4] << 16 | otp[3] << 8 | otp[2]) & 0x3fffc) >> 2, 837 + /* W lot ID, bits 34-45 */ 838 + ((otp[5] << 8 | otp[4]) & 0x3ffc) >> 2, 839 + /* WP ID, bits 47-49 */ 840 + ((otp[6] << 8 | otp[5]) & 0x0380) >> 7, 841 + /* rev ID, bits 50-55 */ 842 + otp[6] >> 2); 843 + 844 + return 0; 845 + } 846 + 847 + static int mpu3050_power_up(struct mpu3050 *mpu3050) 848 + { 849 + int ret; 850 + 851 + ret = regulator_bulk_enable(ARRAY_SIZE(mpu3050->regs), mpu3050->regs); 852 + if (ret) { 853 + dev_err(mpu3050->dev, "cannot enable regulators\n"); 854 + return ret; 855 + } 856 + /* 857 + * 20-100 ms start-up time for register read/write according to 858 + * the datasheet, be on the safe side and wait 200 ms. 859 + */ 860 + msleep(200); 861 + 862 + /* Take device out of sleep mode */ 863 + ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM, 864 + MPU3050_PWR_MGM_SLEEP, 0); 865 + if (ret) { 866 + dev_err(mpu3050->dev, "error setting power mode\n"); 867 + return ret; 868 + } 869 + msleep(10); 870 + 871 + return 0; 872 + } 873 + 874 + static int mpu3050_power_down(struct mpu3050 *mpu3050) 875 + { 876 + int ret; 877 + 878 + /* 879 + * Put MPU-3050 into sleep mode before cutting regulators. 880 + * This is important, because we may not be the sole user 881 + * of the regulator so the power may stay on after this, and 882 + * then we would be wasting power unless we go to sleep mode 883 + * first. 884 + */ 885 + ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM, 886 + MPU3050_PWR_MGM_SLEEP, MPU3050_PWR_MGM_SLEEP); 887 + if (ret) 888 + dev_err(mpu3050->dev, "error putting to sleep\n"); 889 + 890 + ret = regulator_bulk_disable(ARRAY_SIZE(mpu3050->regs), mpu3050->regs); 891 + if (ret) 892 + dev_err(mpu3050->dev, "error disabling regulators\n"); 893 + 894 + return 0; 895 + } 896 + 897 + static irqreturn_t mpu3050_irq_handler(int irq, void *p) 898 + { 899 + struct iio_trigger *trig = p; 900 + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 901 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 902 + 903 + if (!mpu3050->hw_irq_trigger) 904 + return IRQ_NONE; 905 + 906 + /* Get the time stamp as close in time as possible */ 907 + mpu3050->hw_timestamp = iio_get_time_ns(indio_dev); 908 + 909 + return IRQ_WAKE_THREAD; 910 + } 911 + 912 + static irqreturn_t mpu3050_irq_thread(int irq, void *p) 913 + { 914 + struct iio_trigger *trig = p; 915 + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 916 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 917 + unsigned int val; 918 + int ret; 919 + 920 + /* ACK IRQ and check if it was from us */ 921 + ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val); 922 + if (ret) { 923 + dev_err(mpu3050->dev, "error reading IRQ status\n"); 924 + return IRQ_HANDLED; 925 + } 926 + if (!(val & MPU3050_INT_STATUS_RAW_RDY)) 927 + return IRQ_NONE; 928 + 929 + iio_trigger_poll_chained(p); 930 + 931 + return IRQ_HANDLED; 932 + } 933 + 934 + /** 935 + * mpu3050_drdy_trigger_set_state() - set data ready interrupt state 936 + * @trig: trigger instance 937 + * @enable: true if trigger should be enabled, false to disable 938 + */ 939 + static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig, 940 + bool enable) 941 + { 942 + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 943 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 944 + unsigned int val; 945 + int ret; 946 + 947 + /* Disabling trigger: disable interrupt and return */ 948 + if (!enable) { 949 + /* Disable all interrupts */ 950 + ret = regmap_write(mpu3050->map, 951 + MPU3050_INT_CFG, 952 + 0); 953 + if (ret) 954 + dev_err(mpu3050->dev, "error disabling IRQ\n"); 955 + 956 + /* Clear IRQ flag */ 957 + ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val); 958 + if (ret) 959 + dev_err(mpu3050->dev, "error clearing IRQ status\n"); 960 + 961 + /* Disable all things in the FIFO and reset it */ 962 + ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0); 963 + if (ret) 964 + dev_err(mpu3050->dev, "error disabling FIFO\n"); 965 + 966 + ret = regmap_write(mpu3050->map, MPU3050_USR_CTRL, 967 + MPU3050_USR_CTRL_FIFO_RST); 968 + if (ret) 969 + dev_err(mpu3050->dev, "error resetting FIFO\n"); 970 + 971 + pm_runtime_mark_last_busy(mpu3050->dev); 972 + pm_runtime_put_autosuspend(mpu3050->dev); 973 + mpu3050->hw_irq_trigger = false; 974 + 975 + return 0; 976 + } else { 977 + /* Else we're enabling the trigger from this point */ 978 + pm_runtime_get_sync(mpu3050->dev); 979 + mpu3050->hw_irq_trigger = true; 980 + 981 + /* Disable all things in the FIFO */ 982 + ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0); 983 + if (ret) 984 + return ret; 985 + 986 + /* Reset and enable the FIFO */ 987 + ret = regmap_update_bits(mpu3050->map, MPU3050_USR_CTRL, 988 + MPU3050_USR_CTRL_FIFO_EN | 989 + MPU3050_USR_CTRL_FIFO_RST, 990 + MPU3050_USR_CTRL_FIFO_EN | 991 + MPU3050_USR_CTRL_FIFO_RST); 992 + if (ret) 993 + return ret; 994 + 995 + mpu3050->pending_fifo_footer = false; 996 + 997 + /* Turn on the FIFO for temp+X+Y+Z */ 998 + ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 999 + MPU3050_FIFO_EN_TEMP_OUT | 1000 + MPU3050_FIFO_EN_GYRO_XOUT | 1001 + MPU3050_FIFO_EN_GYRO_YOUT | 1002 + MPU3050_FIFO_EN_GYRO_ZOUT | 1003 + MPU3050_FIFO_EN_FOOTER); 1004 + if (ret) 1005 + return ret; 1006 + 1007 + /* Configure the sample engine */ 1008 + ret = mpu3050_start_sampling(mpu3050); 1009 + if (ret) 1010 + return ret; 1011 + 1012 + /* Clear IRQ flag */ 1013 + ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val); 1014 + if (ret) 1015 + dev_err(mpu3050->dev, "error clearing IRQ status\n"); 1016 + 1017 + /* Give us interrupts whenever there is new data ready */ 1018 + val = MPU3050_INT_RAW_RDY_EN; 1019 + 1020 + if (mpu3050->irq_actl) 1021 + val |= MPU3050_INT_ACTL; 1022 + if (mpu3050->irq_latch) 1023 + val |= MPU3050_INT_LATCH_EN; 1024 + if (mpu3050->irq_opendrain) 1025 + val |= MPU3050_INT_OPEN; 1026 + 1027 + ret = regmap_write(mpu3050->map, MPU3050_INT_CFG, val); 1028 + if (ret) 1029 + return ret; 1030 + } 1031 + 1032 + return 0; 1033 + } 1034 + 1035 + static const struct iio_trigger_ops mpu3050_trigger_ops = { 1036 + .owner = THIS_MODULE, 1037 + .set_trigger_state = mpu3050_drdy_trigger_set_state, 1038 + }; 1039 + 1040 + static int mpu3050_trigger_probe(struct iio_dev *indio_dev, int irq) 1041 + { 1042 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 1043 + unsigned long irq_trig; 1044 + int ret; 1045 + 1046 + mpu3050->trig = devm_iio_trigger_alloc(&indio_dev->dev, 1047 + "%s-dev%d", 1048 + indio_dev->name, 1049 + indio_dev->id); 1050 + if (!mpu3050->trig) 1051 + return -ENOMEM; 1052 + 1053 + /* Check if IRQ is open drain */ 1054 + if (of_property_read_bool(mpu3050->dev->of_node, "drive-open-drain")) 1055 + mpu3050->irq_opendrain = true; 1056 + 1057 + irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq)); 1058 + /* 1059 + * Configure the interrupt generator hardware to supply whatever 1060 + * the interrupt is configured for, edges low/high level low/high, 1061 + * we can provide it all. 1062 + */ 1063 + switch (irq_trig) { 1064 + case IRQF_TRIGGER_RISING: 1065 + dev_info(&indio_dev->dev, 1066 + "pulse interrupts on the rising edge\n"); 1067 + if (mpu3050->irq_opendrain) { 1068 + dev_info(&indio_dev->dev, 1069 + "rising edge incompatible with open drain\n"); 1070 + mpu3050->irq_opendrain = false; 1071 + } 1072 + break; 1073 + case IRQF_TRIGGER_FALLING: 1074 + mpu3050->irq_actl = true; 1075 + dev_info(&indio_dev->dev, 1076 + "pulse interrupts on the falling edge\n"); 1077 + break; 1078 + case IRQF_TRIGGER_HIGH: 1079 + mpu3050->irq_latch = true; 1080 + dev_info(&indio_dev->dev, 1081 + "interrupts active high level\n"); 1082 + if (mpu3050->irq_opendrain) { 1083 + dev_info(&indio_dev->dev, 1084 + "active high incompatible with open drain\n"); 1085 + mpu3050->irq_opendrain = false; 1086 + } 1087 + /* 1088 + * With level IRQs, we mask the IRQ until it is processed, 1089 + * but with edge IRQs (pulses) we can queue several interrupts 1090 + * in the top half. 1091 + */ 1092 + irq_trig |= IRQF_ONESHOT; 1093 + break; 1094 + case IRQF_TRIGGER_LOW: 1095 + mpu3050->irq_latch = true; 1096 + mpu3050->irq_actl = true; 1097 + irq_trig |= IRQF_ONESHOT; 1098 + dev_info(&indio_dev->dev, 1099 + "interrupts active low level\n"); 1100 + break; 1101 + default: 1102 + /* This is the most preferred mode, if possible */ 1103 + dev_err(&indio_dev->dev, 1104 + "unsupported IRQ trigger specified (%lx), enforce " 1105 + "rising edge\n", irq_trig); 1106 + irq_trig = IRQF_TRIGGER_RISING; 1107 + break; 1108 + } 1109 + 1110 + /* An open drain line can be shared with several devices */ 1111 + if (mpu3050->irq_opendrain) 1112 + irq_trig |= IRQF_SHARED; 1113 + 1114 + ret = request_threaded_irq(irq, 1115 + mpu3050_irq_handler, 1116 + mpu3050_irq_thread, 1117 + irq_trig, 1118 + mpu3050->trig->name, 1119 + mpu3050->trig); 1120 + if (ret) { 1121 + dev_err(mpu3050->dev, 1122 + "can't get IRQ %d, error %d\n", irq, ret); 1123 + return ret; 1124 + } 1125 + 1126 + mpu3050->irq = irq; 1127 + mpu3050->trig->dev.parent = mpu3050->dev; 1128 + mpu3050->trig->ops = &mpu3050_trigger_ops; 1129 + iio_trigger_set_drvdata(mpu3050->trig, indio_dev); 1130 + 1131 + ret = iio_trigger_register(mpu3050->trig); 1132 + if (ret) 1133 + return ret; 1134 + 1135 + indio_dev->trig = iio_trigger_get(mpu3050->trig); 1136 + 1137 + return 0; 1138 + } 1139 + 1140 + int mpu3050_common_probe(struct device *dev, 1141 + struct regmap *map, 1142 + int irq, 1143 + const char *name) 1144 + { 1145 + struct iio_dev *indio_dev; 1146 + struct mpu3050 *mpu3050; 1147 + unsigned int val; 1148 + int ret; 1149 + 1150 + indio_dev = devm_iio_device_alloc(dev, sizeof(*mpu3050)); 1151 + if (!indio_dev) 1152 + return -ENOMEM; 1153 + mpu3050 = iio_priv(indio_dev); 1154 + 1155 + mpu3050->dev = dev; 1156 + mpu3050->map = map; 1157 + mutex_init(&mpu3050->lock); 1158 + /* Default fullscale: 2000 degrees per second */ 1159 + mpu3050->fullscale = FS_2000_DPS; 1160 + /* 1 kHz, divide by 100, default frequency = 10 Hz */ 1161 + mpu3050->lpf = MPU3050_DLPF_CFG_188HZ; 1162 + mpu3050->divisor = 99; 1163 + 1164 + /* Read the mounting matrix, if present */ 1165 + ret = of_iio_read_mount_matrix(dev, "mount-matrix", 1166 + &mpu3050->orientation); 1167 + if (ret) 1168 + return ret; 1169 + 1170 + /* Fetch and turn on regulators */ 1171 + mpu3050->regs[0].supply = mpu3050_reg_vdd; 1172 + mpu3050->regs[1].supply = mpu3050_reg_vlogic; 1173 + ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(mpu3050->regs), 1174 + mpu3050->regs); 1175 + if (ret) { 1176 + dev_err(dev, "Cannot get regulators\n"); 1177 + return ret; 1178 + } 1179 + 1180 + ret = mpu3050_power_up(mpu3050); 1181 + if (ret) 1182 + return ret; 1183 + 1184 + ret = regmap_read(map, MPU3050_CHIP_ID_REG, &val); 1185 + if (ret) { 1186 + dev_err(dev, "could not read device ID\n"); 1187 + ret = -ENODEV; 1188 + 1189 + goto err_power_down; 1190 + } 1191 + 1192 + if (val != MPU3050_CHIP_ID) { 1193 + dev_err(dev, "unsupported chip id %02x\n", (u8)val); 1194 + ret = -ENODEV; 1195 + goto err_power_down; 1196 + } 1197 + 1198 + ret = regmap_read(map, MPU3050_PRODUCT_ID_REG, &val); 1199 + if (ret) { 1200 + dev_err(dev, "could not read device ID\n"); 1201 + ret = -ENODEV; 1202 + 1203 + goto err_power_down; 1204 + } 1205 + dev_info(dev, "found MPU-3050 part no: %d, version: %d\n", 1206 + ((val >> 4) & 0xf), (val & 0xf)); 1207 + 1208 + ret = mpu3050_hw_init(mpu3050); 1209 + if (ret) 1210 + goto err_power_down; 1211 + 1212 + indio_dev->dev.parent = dev; 1213 + indio_dev->channels = mpu3050_channels; 1214 + indio_dev->num_channels = ARRAY_SIZE(mpu3050_channels); 1215 + indio_dev->info = &mpu3050_info; 1216 + indio_dev->available_scan_masks = mpu3050_scan_masks; 1217 + indio_dev->modes = INDIO_DIRECT_MODE; 1218 + indio_dev->name = name; 1219 + 1220 + ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time, 1221 + mpu3050_trigger_handler, 1222 + &mpu3050_buffer_setup_ops); 1223 + if (ret) { 1224 + dev_err(dev, "triggered buffer setup failed\n"); 1225 + goto err_power_down; 1226 + } 1227 + 1228 + ret = iio_device_register(indio_dev); 1229 + if (ret) { 1230 + dev_err(dev, "device register failed\n"); 1231 + goto err_cleanup_buffer; 1232 + } 1233 + 1234 + dev_set_drvdata(dev, indio_dev); 1235 + 1236 + /* Check if we have an assigned IRQ to use as trigger */ 1237 + if (irq) { 1238 + ret = mpu3050_trigger_probe(indio_dev, irq); 1239 + if (ret) 1240 + dev_err(dev, "failed to register trigger\n"); 1241 + } 1242 + 1243 + /* Enable runtime PM */ 1244 + pm_runtime_get_noresume(dev); 1245 + pm_runtime_set_active(dev); 1246 + pm_runtime_enable(dev); 1247 + /* 1248 + * Set autosuspend to two orders of magnitude larger than the 1249 + * start-up time. 100ms start-up time means 10000ms autosuspend, 1250 + * i.e. 10 seconds. 1251 + */ 1252 + pm_runtime_set_autosuspend_delay(dev, 10000); 1253 + pm_runtime_use_autosuspend(dev); 1254 + pm_runtime_put(dev); 1255 + 1256 + return 0; 1257 + 1258 + err_cleanup_buffer: 1259 + iio_triggered_buffer_cleanup(indio_dev); 1260 + err_power_down: 1261 + mpu3050_power_down(mpu3050); 1262 + 1263 + return ret; 1264 + } 1265 + EXPORT_SYMBOL(mpu3050_common_probe); 1266 + 1267 + int mpu3050_common_remove(struct device *dev) 1268 + { 1269 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 1270 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 1271 + 1272 + pm_runtime_get_sync(dev); 1273 + pm_runtime_put_noidle(dev); 1274 + pm_runtime_disable(dev); 1275 + iio_triggered_buffer_cleanup(indio_dev); 1276 + if (mpu3050->irq) 1277 + free_irq(mpu3050->irq, mpu3050); 1278 + iio_device_unregister(indio_dev); 1279 + mpu3050_power_down(mpu3050); 1280 + 1281 + return 0; 1282 + } 1283 + EXPORT_SYMBOL(mpu3050_common_remove); 1284 + 1285 + #ifdef CONFIG_PM 1286 + static int mpu3050_runtime_suspend(struct device *dev) 1287 + { 1288 + return mpu3050_power_down(iio_priv(dev_get_drvdata(dev))); 1289 + } 1290 + 1291 + static int mpu3050_runtime_resume(struct device *dev) 1292 + { 1293 + return mpu3050_power_up(iio_priv(dev_get_drvdata(dev))); 1294 + } 1295 + #endif /* CONFIG_PM */ 1296 + 1297 + const struct dev_pm_ops mpu3050_dev_pm_ops = { 1298 + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1299 + pm_runtime_force_resume) 1300 + SET_RUNTIME_PM_OPS(mpu3050_runtime_suspend, 1301 + mpu3050_runtime_resume, NULL) 1302 + }; 1303 + EXPORT_SYMBOL(mpu3050_dev_pm_ops); 1304 + 1305 + MODULE_AUTHOR("Linus Walleij"); 1306 + MODULE_DESCRIPTION("MPU3050 gyroscope driver"); 1307 + MODULE_LICENSE("GPL");
+124
drivers/iio/gyro/mpu3050-i2c.c
··· 1 + #include <linux/err.h> 2 + #include <linux/i2c.h> 3 + #include <linux/i2c-mux.h> 4 + #include <linux/iio/iio.h> 5 + #include <linux/module.h> 6 + #include <linux/regmap.h> 7 + #include <linux/pm_runtime.h> 8 + 9 + #include "mpu3050.h" 10 + 11 + static const struct regmap_config mpu3050_i2c_regmap_config = { 12 + .reg_bits = 8, 13 + .val_bits = 8, 14 + }; 15 + 16 + static int mpu3050_i2c_bypass_select(struct i2c_mux_core *mux, u32 chan_id) 17 + { 18 + struct mpu3050 *mpu3050 = i2c_mux_priv(mux); 19 + 20 + /* Just power up the device, that is all that is needed */ 21 + pm_runtime_get_sync(mpu3050->dev); 22 + return 0; 23 + } 24 + 25 + static int mpu3050_i2c_bypass_deselect(struct i2c_mux_core *mux, u32 chan_id) 26 + { 27 + struct mpu3050 *mpu3050 = i2c_mux_priv(mux); 28 + 29 + pm_runtime_mark_last_busy(mpu3050->dev); 30 + pm_runtime_put_autosuspend(mpu3050->dev); 31 + return 0; 32 + } 33 + 34 + static int mpu3050_i2c_probe(struct i2c_client *client, 35 + const struct i2c_device_id *id) 36 + { 37 + struct regmap *regmap; 38 + const char *name; 39 + struct mpu3050 *mpu3050; 40 + int ret; 41 + 42 + if (!i2c_check_functionality(client->adapter, 43 + I2C_FUNC_SMBUS_I2C_BLOCK)) 44 + return -EOPNOTSUPP; 45 + 46 + if (id) 47 + name = id->name; 48 + else 49 + return -ENODEV; 50 + 51 + regmap = devm_regmap_init_i2c(client, &mpu3050_i2c_regmap_config); 52 + if (IS_ERR(regmap)) { 53 + dev_err(&client->dev, "Failed to register i2c regmap %d\n", 54 + (int)PTR_ERR(regmap)); 55 + return PTR_ERR(regmap); 56 + } 57 + 58 + ret = mpu3050_common_probe(&client->dev, regmap, client->irq, name); 59 + if (ret) 60 + return ret; 61 + 62 + /* The main driver is up, now register the I2C mux */ 63 + mpu3050 = iio_priv(dev_get_drvdata(&client->dev)); 64 + mpu3050->i2cmux = i2c_mux_alloc(client->adapter, &client->dev, 65 + 1, 0, I2C_MUX_LOCKED | I2C_MUX_GATE, 66 + mpu3050_i2c_bypass_select, 67 + mpu3050_i2c_bypass_deselect); 68 + /* Just fail the mux, there is no point in killing the driver */ 69 + if (!mpu3050->i2cmux) 70 + dev_err(&client->dev, "failed to allocate I2C mux\n"); 71 + else { 72 + mpu3050->i2cmux->priv = mpu3050; 73 + ret = i2c_mux_add_adapter(mpu3050->i2cmux, 0, 0, 0); 74 + if (ret) 75 + dev_err(&client->dev, "failed to add I2C mux\n"); 76 + } 77 + 78 + return 0; 79 + } 80 + 81 + static int mpu3050_i2c_remove(struct i2c_client *client) 82 + { 83 + struct iio_dev *indio_dev = dev_get_drvdata(&client->dev); 84 + struct mpu3050 *mpu3050 = iio_priv(indio_dev); 85 + 86 + if (mpu3050->i2cmux) 87 + i2c_mux_del_adapters(mpu3050->i2cmux); 88 + 89 + return mpu3050_common_remove(&client->dev); 90 + } 91 + 92 + /* 93 + * device id table is used to identify what device can be 94 + * supported by this driver 95 + */ 96 + static const struct i2c_device_id mpu3050_i2c_id[] = { 97 + { "mpu3050" }, 98 + {} 99 + }; 100 + MODULE_DEVICE_TABLE(i2c, mpu3050_i2c_id); 101 + 102 + static const struct of_device_id mpu3050_i2c_of_match[] = { 103 + { .compatible = "invensense,mpu3050", .data = "mpu3050" }, 104 + /* Deprecated vendor ID from the Input driver */ 105 + { .compatible = "invn,mpu3050", .data = "mpu3050" }, 106 + { }, 107 + }; 108 + MODULE_DEVICE_TABLE(of, mpu3050_i2c_of_match); 109 + 110 + static struct i2c_driver mpu3050_i2c_driver = { 111 + .probe = mpu3050_i2c_probe, 112 + .remove = mpu3050_i2c_remove, 113 + .id_table = mpu3050_i2c_id, 114 + .driver = { 115 + .of_match_table = mpu3050_i2c_of_match, 116 + .name = "mpu3050-i2c", 117 + .pm = &mpu3050_dev_pm_ops, 118 + }, 119 + }; 120 + module_i2c_driver(mpu3050_i2c_driver); 121 + 122 + MODULE_AUTHOR("Linus Walleij"); 123 + MODULE_DESCRIPTION("Invensense MPU3050 gyroscope driver"); 124 + MODULE_LICENSE("GPL");
+96
drivers/iio/gyro/mpu3050.h
··· 1 + #include <linux/iio/iio.h> 2 + #include <linux/mutex.h> 3 + #include <linux/regmap.h> 4 + #include <linux/regulator/consumer.h> 5 + #include <linux/i2c.h> 6 + 7 + /** 8 + * enum mpu3050_fullscale - indicates the full range of the sensor in deg/sec 9 + */ 10 + enum mpu3050_fullscale { 11 + FS_250_DPS = 0, 12 + FS_500_DPS, 13 + FS_1000_DPS, 14 + FS_2000_DPS, 15 + }; 16 + 17 + /** 18 + * enum mpu3050_lpf - indicates the low pass filter width 19 + */ 20 + enum mpu3050_lpf { 21 + /* This implicity sets sample frequency to 8 kHz */ 22 + LPF_256_HZ_NOLPF = 0, 23 + /* All others sets the sample frequency to 1 kHz */ 24 + LPF_188_HZ, 25 + LPF_98_HZ, 26 + LPF_42_HZ, 27 + LPF_20_HZ, 28 + LPF_10_HZ, 29 + LPF_5_HZ, 30 + LPF_2100_HZ_NOLPF, 31 + }; 32 + 33 + enum mpu3050_axis { 34 + AXIS_X = 0, 35 + AXIS_Y, 36 + AXIS_Z, 37 + AXIS_MAX, 38 + }; 39 + 40 + /** 41 + * struct mpu3050 - instance state container for the device 42 + * @dev: parent device for this instance 43 + * @orientation: mounting matrix, flipped axis etc 44 + * @map: regmap to reach the registers 45 + * @lock: serialization lock to marshal all requests 46 + * @irq: the IRQ used for this device 47 + * @regs: the regulators to power this device 48 + * @fullscale: the current fullscale setting for the device 49 + * @lpf: digital low pass filter setting for the device 50 + * @divisor: base frequency divider: divides 8 or 1 kHz 51 + * @calibration: the three signed 16-bit calibration settings that 52 + * get written into the offset registers for each axis to compensate 53 + * for DC offsets 54 + * @trig: trigger for the MPU-3050 interrupt, if present 55 + * @hw_irq_trigger: hardware interrupt trigger is in use 56 + * @irq_actl: interrupt is active low 57 + * @irq_latch: latched IRQ, this means that it is a level IRQ 58 + * @irq_opendrain: the interrupt line shall be configured open drain 59 + * @pending_fifo_footer: tells us if there is a pending footer in the FIFO 60 + * that we have to read out first when handling the FIFO 61 + * @hw_timestamp: latest hardware timestamp from the trigger IRQ, when in 62 + * use 63 + * @i2cmux: an I2C mux reflecting the fact that this sensor is a hub with 64 + * a pass-through I2C interface coming out of it: this device needs to be 65 + * powered up in order to reach devices on the other side of this mux 66 + */ 67 + struct mpu3050 { 68 + struct device *dev; 69 + struct iio_mount_matrix orientation; 70 + struct regmap *map; 71 + struct mutex lock; 72 + int irq; 73 + struct regulator_bulk_data regs[2]; 74 + enum mpu3050_fullscale fullscale; 75 + enum mpu3050_lpf lpf; 76 + u8 divisor; 77 + s16 calibration[3]; 78 + struct iio_trigger *trig; 79 + bool hw_irq_trigger; 80 + bool irq_actl; 81 + bool irq_latch; 82 + bool irq_opendrain; 83 + bool pending_fifo_footer; 84 + s64 hw_timestamp; 85 + struct i2c_mux_core *i2cmux; 86 + }; 87 + 88 + /* Probe called from different transports */ 89 + int mpu3050_common_probe(struct device *dev, 90 + struct regmap *map, 91 + int irq, 92 + const char *name); 93 + int mpu3050_common_remove(struct device *dev); 94 + 95 + /* PM ops */ 96 + extern const struct dev_pm_ops mpu3050_dev_pm_ops;
-1
drivers/iio/humidity/hts221_buffer.c
··· 15 15 16 16 #include <linux/iio/iio.h> 17 17 #include <linux/iio/trigger.h> 18 - #include <linux/interrupt.h> 19 18 #include <linux/iio/events.h> 20 19 #include <linux/iio/trigger_consumer.h> 21 20 #include <linux/iio/triggered_buffer.h>
+10 -1
drivers/iio/humidity/si7020.c
··· 154 154 }; 155 155 MODULE_DEVICE_TABLE(i2c, si7020_id); 156 156 157 + static const struct of_device_id si7020_dt_ids[] = { 158 + { .compatible = "silabs,si7020" }, 159 + { } 160 + }; 161 + MODULE_DEVICE_TABLE(of, si7020_dt_ids); 162 + 157 163 static struct i2c_driver si7020_driver = { 158 - .driver.name = "si7020", 164 + .driver = { 165 + .name = "si7020", 166 + .of_match_table = of_match_ptr(si7020_dt_ids), 167 + }, 159 168 .probe = si7020_probe, 160 169 .id_table = si7020_id, 161 170 };
+2
drivers/iio/temperature/maxim_thermocouple.c
··· 136 136 ret = spi_read(data->spi, (void *)&buf32, storage_bytes); 137 137 *val = be32_to_cpu(buf32); 138 138 break; 139 + default: 140 + ret = -EINVAL; 139 141 } 140 142 141 143 if (ret)
+1
drivers/mfd/ti_am335x_tscadc.c
··· 183 183 tscadc->irq = err; 184 184 185 185 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 186 + tscadc->tscadc_phys_base = res->start; 186 187 tscadc->tscadc_base = devm_ioremap_resource(&pdev->dev, res); 187 188 if (IS_ERR(tscadc->tscadc_base)) 188 189 return PTR_ERR(tscadc->tscadc_base);
+159
drivers/platform/chrome/cros_ec_dev.c
··· 18 18 */ 19 19 20 20 #include <linux/fs.h> 21 + #include <linux/mfd/core.h> 21 22 #include <linux/module.h> 22 23 #include <linux/platform_device.h> 23 24 #include <linux/slab.h> ··· 86 85 exit: 87 86 kfree(msg); 88 87 return ret; 88 + } 89 + 90 + static int cros_ec_check_features(struct cros_ec_dev *ec, int feature) 91 + { 92 + struct cros_ec_command *msg; 93 + int ret; 94 + 95 + if (ec->features[0] == -1U && ec->features[1] == -1U) { 96 + /* features bitmap not read yet */ 97 + 98 + msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL); 99 + if (!msg) 100 + return -ENOMEM; 101 + 102 + msg->version = 0; 103 + msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset; 104 + msg->insize = sizeof(ec->features); 105 + msg->outsize = 0; 106 + 107 + ret = cros_ec_cmd_xfer(ec->ec_dev, msg); 108 + if (ret < 0 || msg->result != EC_RES_SUCCESS) { 109 + dev_warn(ec->dev, "cannot get EC features: %d/%d\n", 110 + ret, msg->result); 111 + memset(ec->features, 0, sizeof(ec->features)); 112 + } 113 + 114 + memcpy(ec->features, msg->data, sizeof(ec->features)); 115 + 116 + dev_dbg(ec->dev, "EC features %08x %08x\n", 117 + ec->features[0], ec->features[1]); 118 + 119 + kfree(msg); 120 + } 121 + 122 + return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature); 89 123 } 90 124 91 125 /* Device file ops */ ··· 266 230 kfree(ec); 267 231 } 268 232 233 + static void cros_ec_sensors_register(struct cros_ec_dev *ec) 234 + { 235 + /* 236 + * Issue a command to get the number of sensor reported. 237 + * Build an array of sensors driver and register them all. 238 + */ 239 + int ret, i, id, sensor_num; 240 + struct mfd_cell *sensor_cells; 241 + struct cros_ec_sensor_platform *sensor_platforms; 242 + int sensor_type[MOTIONSENSE_TYPE_MAX]; 243 + struct ec_params_motion_sense *params; 244 + struct ec_response_motion_sense *resp; 245 + struct cros_ec_command *msg; 246 + 247 + msg = kzalloc(sizeof(struct cros_ec_command) + 248 + max(sizeof(*params), sizeof(*resp)), GFP_KERNEL); 249 + if (msg == NULL) 250 + return; 251 + 252 + msg->version = 2; 253 + msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset; 254 + msg->outsize = sizeof(*params); 255 + msg->insize = sizeof(*resp); 256 + 257 + params = (struct ec_params_motion_sense *)msg->data; 258 + params->cmd = MOTIONSENSE_CMD_DUMP; 259 + 260 + ret = cros_ec_cmd_xfer(ec->ec_dev, msg); 261 + if (ret < 0 || msg->result != EC_RES_SUCCESS) { 262 + dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n", 263 + ret, msg->result); 264 + goto error; 265 + } 266 + 267 + resp = (struct ec_response_motion_sense *)msg->data; 268 + sensor_num = resp->dump.sensor_count; 269 + /* Allocate 2 extra sensors in case lid angle or FIFO are needed */ 270 + sensor_cells = kzalloc(sizeof(struct mfd_cell) * (sensor_num + 2), 271 + GFP_KERNEL); 272 + if (sensor_cells == NULL) 273 + goto error; 274 + 275 + sensor_platforms = kzalloc(sizeof(struct cros_ec_sensor_platform) * 276 + (sensor_num + 1), GFP_KERNEL); 277 + if (sensor_platforms == NULL) 278 + goto error_platforms; 279 + 280 + memset(sensor_type, 0, sizeof(sensor_type)); 281 + id = 0; 282 + for (i = 0; i < sensor_num; i++) { 283 + params->cmd = MOTIONSENSE_CMD_INFO; 284 + params->info.sensor_num = i; 285 + ret = cros_ec_cmd_xfer(ec->ec_dev, msg); 286 + if (ret < 0 || msg->result != EC_RES_SUCCESS) { 287 + dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n", 288 + i, ret, msg->result); 289 + continue; 290 + } 291 + switch (resp->info.type) { 292 + case MOTIONSENSE_TYPE_ACCEL: 293 + sensor_cells[id].name = "cros-ec-accel"; 294 + break; 295 + case MOTIONSENSE_TYPE_GYRO: 296 + sensor_cells[id].name = "cros-ec-gyro"; 297 + break; 298 + case MOTIONSENSE_TYPE_MAG: 299 + sensor_cells[id].name = "cros-ec-mag"; 300 + break; 301 + case MOTIONSENSE_TYPE_PROX: 302 + sensor_cells[id].name = "cros-ec-prox"; 303 + break; 304 + case MOTIONSENSE_TYPE_LIGHT: 305 + sensor_cells[id].name = "cros-ec-light"; 306 + break; 307 + case MOTIONSENSE_TYPE_ACTIVITY: 308 + sensor_cells[id].name = "cros-ec-activity"; 309 + break; 310 + default: 311 + dev_warn(ec->dev, "unknown type %d\n", resp->info.type); 312 + continue; 313 + } 314 + sensor_platforms[id].sensor_num = i; 315 + sensor_cells[id].id = sensor_type[resp->info.type]; 316 + sensor_cells[id].platform_data = &sensor_platforms[id]; 317 + sensor_cells[id].pdata_size = 318 + sizeof(struct cros_ec_sensor_platform); 319 + 320 + sensor_type[resp->info.type]++; 321 + id++; 322 + } 323 + if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2) { 324 + sensor_platforms[id].sensor_num = sensor_num; 325 + 326 + sensor_cells[id].name = "cros-ec-angle"; 327 + sensor_cells[id].id = 0; 328 + sensor_cells[id].platform_data = &sensor_platforms[id]; 329 + sensor_cells[id].pdata_size = 330 + sizeof(struct cros_ec_sensor_platform); 331 + id++; 332 + } 333 + if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) { 334 + sensor_cells[id].name = "cros-ec-ring"; 335 + id++; 336 + } 337 + 338 + ret = mfd_add_devices(ec->dev, 0, sensor_cells, id, 339 + NULL, 0, NULL); 340 + if (ret) 341 + dev_err(ec->dev, "failed to add EC sensors\n"); 342 + 343 + kfree(sensor_platforms); 344 + error_platforms: 345 + kfree(sensor_cells); 346 + error: 347 + kfree(msg); 348 + } 349 + 269 350 static int ec_device_probe(struct platform_device *pdev) 270 351 { 271 352 int retval = -ENOMEM; ··· 398 245 ec->ec_dev = dev_get_drvdata(dev->parent); 399 246 ec->dev = dev; 400 247 ec->cmd_offset = ec_platform->cmd_offset; 248 + ec->features[0] = -1U; /* Not cached yet */ 249 + ec->features[1] = -1U; /* Not cached yet */ 401 250 device_initialize(&ec->class_dev); 402 251 cdev_init(&ec->cdev, &fops); 403 252 ··· 436 281 dev_err(dev, "device_register failed => %d\n", retval); 437 282 goto dev_reg_failed; 438 283 } 284 + 285 + /* check whether this EC is a sensor hub. */ 286 + if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE)) 287 + cros_ec_sensors_register(ec); 439 288 440 289 return 0; 441 290
+30 -13
drivers/staging/iio/adc/ad7192.c
··· 152 152 */ 153 153 154 154 struct ad7192_state { 155 - struct regulator *reg; 155 + struct regulator *avdd; 156 + struct regulator *dvdd; 156 157 u16 int_vref_mv; 157 158 u32 mclk; 158 159 u32 f_order; ··· 634 633 635 634 st = iio_priv(indio_dev); 636 635 637 - st->reg = devm_regulator_get(&spi->dev, "vcc"); 638 - if (!IS_ERR(st->reg)) { 639 - ret = regulator_enable(st->reg); 640 - if (ret) 641 - return ret; 636 + st->avdd = devm_regulator_get(&spi->dev, "avdd"); 637 + if (IS_ERR(st->avdd)) 638 + return PTR_ERR(st->avdd); 642 639 643 - voltage_uv = regulator_get_voltage(st->reg); 640 + ret = regulator_enable(st->avdd); 641 + if (ret) { 642 + dev_err(&spi->dev, "Failed to enable specified AVdd supply\n"); 643 + return ret; 644 644 } 645 + 646 + st->dvdd = devm_regulator_get(&spi->dev, "dvdd"); 647 + if (IS_ERR(st->dvdd)) { 648 + ret = PTR_ERR(st->dvdd); 649 + goto error_disable_avdd; 650 + } 651 + 652 + ret = regulator_enable(st->dvdd); 653 + if (ret) { 654 + dev_err(&spi->dev, "Failed to enable specified DVdd supply\n"); 655 + goto error_disable_avdd; 656 + } 657 + 658 + voltage_uv = regulator_get_voltage(st->avdd); 645 659 646 660 if (pdata->vref_mv) 647 661 st->int_vref_mv = pdata->vref_mv; ··· 691 675 692 676 ret = ad_sd_setup_buffer_and_trigger(indio_dev); 693 677 if (ret) 694 - goto error_disable_reg; 678 + goto error_disable_dvdd; 695 679 696 680 ret = ad7192_setup(st, pdata); 697 681 if (ret) ··· 704 688 705 689 error_remove_trigger: 706 690 ad_sd_cleanup_buffer_and_trigger(indio_dev); 707 - error_disable_reg: 708 - if (!IS_ERR(st->reg)) 709 - regulator_disable(st->reg); 691 + error_disable_dvdd: 692 + regulator_disable(st->dvdd); 693 + error_disable_avdd: 694 + regulator_disable(st->avdd); 710 695 711 696 return ret; 712 697 } ··· 720 703 iio_device_unregister(indio_dev); 721 704 ad_sd_cleanup_buffer_and_trigger(indio_dev); 722 705 723 - if (!IS_ERR(st->reg)) 724 - regulator_disable(st->reg); 706 + regulator_disable(st->dvdd); 707 + regulator_disable(st->avdd); 725 708 726 709 return 0; 727 710 }
+10 -10
drivers/staging/iio/adc/ad7780.c
··· 173 173 174 174 ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info); 175 175 176 - st->reg = devm_regulator_get(&spi->dev, "vcc"); 177 - if (!IS_ERR(st->reg)) { 178 - ret = regulator_enable(st->reg); 179 - if (ret) 180 - return ret; 176 + st->reg = devm_regulator_get(&spi->dev, "avdd"); 177 + if (IS_ERR(st->reg)) 178 + return PTR_ERR(st->reg); 181 179 182 - voltage_uv = regulator_get_voltage(st->reg); 180 + ret = regulator_enable(st->reg); 181 + if (ret) { 182 + dev_err(&spi->dev, "Failed to enable specified AVdd supply\n"); 183 + return ret; 183 184 } 185 + voltage_uv = regulator_get_voltage(st->reg); 184 186 185 187 st->chip_info = 186 188 &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data]; ··· 224 222 error_cleanup_buffer_and_trigger: 225 223 ad_sd_cleanup_buffer_and_trigger(indio_dev); 226 224 error_disable_reg: 227 - if (!IS_ERR(st->reg)) 228 - regulator_disable(st->reg); 225 + regulator_disable(st->reg); 229 226 230 227 return ret; 231 228 } ··· 237 236 iio_device_unregister(indio_dev); 238 237 ad_sd_cleanup_buffer_and_trigger(indio_dev); 239 238 240 - if (!IS_ERR(st->reg)) 241 - regulator_disable(st->reg); 239 + regulator_disable(st->reg); 242 240 243 241 return 0; 244 242 }
+23 -16
drivers/staging/iio/cdc/ad7746.c
··· 70 70 #define AD7746_EXCSETUP_EXCLVL(x) (((x) & 0x3) << 0) 71 71 72 72 /* Config Register Bit Designations (AD7746_REG_CFG) */ 73 - #define AD7746_CONF_VTFS(x) ((x) << 6) 74 - #define AD7746_CONF_CAPFS(x) ((x) << 3) 73 + #define AD7746_CONF_VTFS_SHIFT 6 74 + #define AD7746_CONF_CAPFS_SHIFT 3 75 + #define AD7746_CONF_VTFS_MASK GENMASK(7, 6) 76 + #define AD7746_CONF_CAPFS_MASK GENMASK(5, 3) 75 77 #define AD7746_CONF_MODE_IDLE (0 << 0) 76 78 #define AD7746_CONF_MODE_CONT_CONV (1 << 0) 77 79 #define AD7746_CONF_MODE_SINGLE_CONV (2 << 0) ··· 219 217 struct iio_chan_spec const *chan) 220 218 { 221 219 struct ad7746_chip_info *chip = iio_priv(indio_dev); 222 - int ret, delay; 220 + int ret, delay, idx; 223 221 u8 vt_setup, cap_setup; 224 222 225 223 switch (chan->type) { 226 224 case IIO_CAPACITANCE: 227 225 cap_setup = (chan->address & 0xFF) | AD7746_CAPSETUP_CAPEN; 228 226 vt_setup = chip->vt_setup & ~AD7746_VTSETUP_VTEN; 229 - delay = ad7746_cap_filter_rate_table[(chip->config >> 3) & 230 - 0x7][1]; 227 + idx = (chip->config & AD7746_CONF_CAPFS_MASK) >> 228 + AD7746_CONF_CAPFS_SHIFT; 229 + delay = ad7746_cap_filter_rate_table[idx][1]; 231 230 232 231 if (chip->capdac_set != chan->channel) { 233 232 ret = i2c_smbus_write_byte_data(chip->client, ··· 249 246 case IIO_TEMP: 250 247 vt_setup = (chan->address & 0xFF) | AD7746_VTSETUP_VTEN; 251 248 cap_setup = chip->cap_setup & ~AD7746_CAPSETUP_CAPEN; 252 - delay = ad7746_cap_filter_rate_table[(chip->config >> 6) & 253 - 0x3][1]; 249 + idx = (chip->config & AD7746_CONF_VTFS_MASK) >> 250 + AD7746_CONF_VTFS_SHIFT; 251 + delay = ad7746_cap_filter_rate_table[idx][1]; 254 252 break; 255 253 default: 256 254 return -EINVAL; ··· 373 369 if (i >= ARRAY_SIZE(ad7746_cap_filter_rate_table)) 374 370 i = ARRAY_SIZE(ad7746_cap_filter_rate_table) - 1; 375 371 376 - chip->config &= ~AD7746_CONF_CAPFS(0x7); 377 - chip->config |= AD7746_CONF_CAPFS(i); 372 + chip->config &= ~AD7746_CONF_CAPFS_MASK; 373 + chip->config |= i << AD7746_CONF_CAPFS_SHIFT; 378 374 379 375 return 0; 380 376 } ··· 391 387 if (i >= ARRAY_SIZE(ad7746_vt_filter_rate_table)) 392 388 i = ARRAY_SIZE(ad7746_vt_filter_rate_table) - 1; 393 389 394 - chip->config &= ~AD7746_CONF_VTFS(0x3); 395 - chip->config |= AD7746_CONF_VTFS(i); 390 + chip->config &= ~AD7746_CONF_VTFS_MASK; 391 + chip->config |= i << AD7746_CONF_VTFS_SHIFT; 396 392 397 393 return 0; 398 394 } ··· 531 527 long mask) 532 528 { 533 529 struct ad7746_chip_info *chip = iio_priv(indio_dev); 534 - int ret, delay; 530 + int ret, delay, idx; 535 531 u8 regval, reg; 536 532 537 533 mutex_lock(&indio_dev->mlock); ··· 639 635 case IIO_CHAN_INFO_SAMP_FREQ: 640 636 switch (chan->type) { 641 637 case IIO_CAPACITANCE: 642 - *val = ad7746_cap_filter_rate_table[ 643 - (chip->config >> 3) & 0x7][0]; 638 + idx = (chip->config & AD7746_CONF_CAPFS_MASK) >> 639 + AD7746_CONF_CAPFS_SHIFT; 640 + *val = ad7746_cap_filter_rate_table[idx][0]; 644 641 ret = IIO_VAL_INT; 645 642 break; 646 643 case IIO_VOLTAGE: 647 - *val = ad7746_vt_filter_rate_table[ 648 - (chip->config >> 6) & 0x3][0]; 644 + idx = (chip->config & AD7746_CONF_VTFS_MASK) >> 645 + AD7746_CONF_VTFS_SHIFT; 646 + *val = ad7746_vt_filter_rate_table[idx][0]; 647 + ret = IIO_VAL_INT; 649 648 break; 650 649 default: 651 650 ret = -EINVAL;
+35 -21
drivers/staging/iio/frequency/ad9832.c
··· 204 204 struct ad9832_platform_data *pdata = dev_get_platdata(&spi->dev); 205 205 struct iio_dev *indio_dev; 206 206 struct ad9832_state *st; 207 - struct regulator *reg; 208 207 int ret; 209 208 210 209 if (!pdata) { ··· 211 212 return -ENODEV; 212 213 } 213 214 214 - reg = devm_regulator_get(&spi->dev, "vcc"); 215 - if (!IS_ERR(reg)) { 216 - ret = regulator_enable(reg); 217 - if (ret) 218 - return ret; 215 + st->avdd = devm_regulator_get(&spi->dev, "avdd"); 216 + if (IS_ERR(st->avdd)) 217 + return PTR_ERR(st->avdd); 218 + 219 + ret = regulator_enable(st->avdd); 220 + if (ret) { 221 + dev_err(&spi->dev, "Failed to enable specified AVDD supply\n"); 222 + return ret; 223 + } 224 + 225 + st->dvdd = devm_regulator_get(&spi->dev, "dvdd"); 226 + if (IS_ERR(st->dvdd)) { 227 + ret = PTR_ERR(st->dvdd); 228 + goto error_disable_avdd; 229 + } 230 + 231 + ret = regulator_enable(st->dvdd); 232 + if (ret) { 233 + dev_err(&spi->dev, "Failed to enable specified DVDD supply\n"); 234 + goto error_disable_avdd; 219 235 } 220 236 221 237 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 222 238 if (!indio_dev) { 223 239 ret = -ENOMEM; 224 - goto error_disable_reg; 240 + goto error_disable_dvdd; 225 241 } 226 242 spi_set_drvdata(spi, indio_dev); 227 243 st = iio_priv(indio_dev); 228 - st->reg = reg; 229 244 st->mclk = pdata->mclk; 230 245 st->spi = spi; 231 246 ··· 290 277 ret = spi_sync(st->spi, &st->msg); 291 278 if (ret) { 292 279 dev_err(&spi->dev, "device init failed\n"); 293 - goto error_disable_reg; 280 + goto error_disable_dvdd; 294 281 } 295 282 296 283 ret = ad9832_write_frequency(st, AD9832_FREQ0HM, pdata->freq0); 297 284 if (ret) 298 - goto error_disable_reg; 285 + goto error_disable_dvdd; 299 286 300 287 ret = ad9832_write_frequency(st, AD9832_FREQ1HM, pdata->freq1); 301 288 if (ret) 302 - goto error_disable_reg; 289 + goto error_disable_dvdd; 303 290 304 291 ret = ad9832_write_phase(st, AD9832_PHASE0H, pdata->phase0); 305 292 if (ret) 306 - goto error_disable_reg; 293 + goto error_disable_dvdd; 307 294 308 295 ret = ad9832_write_phase(st, AD9832_PHASE1H, pdata->phase1); 309 296 if (ret) 310 - goto error_disable_reg; 297 + goto error_disable_dvdd; 311 298 312 299 ret = ad9832_write_phase(st, AD9832_PHASE2H, pdata->phase2); 313 300 if (ret) 314 - goto error_disable_reg; 301 + goto error_disable_dvdd; 315 302 316 303 ret = ad9832_write_phase(st, AD9832_PHASE3H, pdata->phase3); 317 304 if (ret) 318 - goto error_disable_reg; 305 + goto error_disable_dvdd; 319 306 320 307 ret = iio_device_register(indio_dev); 321 308 if (ret) 322 - goto error_disable_reg; 309 + goto error_disable_dvdd; 323 310 324 311 return 0; 325 312 326 - error_disable_reg: 327 - if (!IS_ERR(reg)) 328 - regulator_disable(reg); 313 + error_disable_dvdd: 314 + regulator_disable(st->dvdd); 315 + error_disable_avdd: 316 + regulator_disable(st->avdd); 329 317 330 318 return ret; 331 319 } ··· 337 323 struct ad9832_state *st = iio_priv(indio_dev); 338 324 339 325 iio_device_unregister(indio_dev); 340 - if (!IS_ERR(st->reg)) 341 - regulator_disable(st->reg); 326 + regulator_disable(st->dvdd); 327 + regulator_disable(st->avdd); 342 328 343 329 return 0; 344 330 }
+4 -2
drivers/staging/iio/frequency/ad9832.h
··· 58 58 /** 59 59 * struct ad9832_state - driver instance specific data 60 60 * @spi: spi_device 61 - * @reg: supply regulator 61 + * @avdd: supply regulator for the analog section 62 + * @dvdd: supply regulator for the digital section 62 63 * @mclk: external master clock 63 64 * @ctrl_fp: cached frequency/phase control word 64 65 * @ctrl_ss: cached sync/selsrc control word ··· 77 76 78 77 struct ad9832_state { 79 78 struct spi_device *spi; 80 - struct regulator *reg; 79 + struct regulator *avdd; 80 + struct regulator *dvdd; 81 81 unsigned long mclk; 82 82 unsigned short ctrl_fp; 83 83 unsigned short ctrl_ss;
+10 -9
drivers/staging/iio/frequency/ad9834.c
··· 329 329 return -ENODEV; 330 330 } 331 331 332 - reg = devm_regulator_get(&spi->dev, "vcc"); 333 - if (!IS_ERR(reg)) { 334 - ret = regulator_enable(reg); 335 - if (ret) 336 - return ret; 332 + reg = devm_regulator_get(&spi->dev, "avdd"); 333 + if (IS_ERR(reg)) 334 + return PTR_ERR(reg); 335 + 336 + ret = regulator_enable(reg); 337 + if (ret) { 338 + dev_err(&spi->dev, "Failed to enable specified AVDD supply\n"); 339 + return ret; 337 340 } 338 341 339 342 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); ··· 419 416 return 0; 420 417 421 418 error_disable_reg: 422 - if (!IS_ERR(reg)) 423 - regulator_disable(reg); 419 + regulator_disable(reg); 424 420 425 421 return ret; 426 422 } ··· 430 428 struct ad9834_state *st = iio_priv(indio_dev); 431 429 432 430 iio_device_unregister(indio_dev); 433 - if (!IS_ERR(st->reg)) 434 - regulator_disable(st->reg); 431 + regulator_disable(st->reg); 435 432 436 433 return 0; 437 434 }
+11 -10
drivers/staging/iio/impedance-analyzer/ad5933.c
··· 726 726 if (!pdata) 727 727 pdata = &ad5933_default_pdata; 728 728 729 - st->reg = devm_regulator_get(&client->dev, "vcc"); 730 - if (!IS_ERR(st->reg)) { 731 - ret = regulator_enable(st->reg); 732 - if (ret) 733 - return ret; 734 - voltage_uv = regulator_get_voltage(st->reg); 729 + st->reg = devm_regulator_get(&client->dev, "vdd"); 730 + if (IS_ERR(st->reg)) 731 + return PTR_ERR(st->reg); 732 + 733 + ret = regulator_enable(st->reg); 734 + if (ret) { 735 + dev_err(&client->dev, "Failed to enable specified VDD supply\n"); 736 + return ret; 735 737 } 738 + voltage_uv = regulator_get_voltage(st->reg); 736 739 737 740 if (voltage_uv) 738 741 st->vref_mv = voltage_uv / 1000; ··· 778 775 error_unreg_ring: 779 776 iio_kfifo_free(indio_dev->buffer); 780 777 error_disable_reg: 781 - if (!IS_ERR(st->reg)) 782 - regulator_disable(st->reg); 778 + regulator_disable(st->reg); 783 779 784 780 return ret; 785 781 } ··· 790 788 791 789 iio_device_unregister(indio_dev); 792 790 iio_kfifo_free(indio_dev->buffer); 793 - if (!IS_ERR(st->reg)) 794 - regulator_disable(st->reg); 791 + regulator_disable(st->reg); 795 792 796 793 return 0; 797 794 }
+281 -352
drivers/staging/iio/light/tsl2583.c
··· 29 29 #include <linux/slab.h> 30 30 #include <linux/module.h> 31 31 #include <linux/iio/iio.h> 32 - 33 - #define TSL258X_MAX_DEVICE_REGS 32 32 + #include <linux/iio/sysfs.h> 34 33 35 34 /* Triton register offsets */ 36 35 #define TSL258X_REG_MAX 8 ··· 63 64 64 65 /* Lux calculation constants */ 65 66 #define TSL258X_LUX_CALC_OVER_FLOW 65535 67 + 68 + #define TSL2583_CHIP_ID 0x90 69 + #define TSL2583_CHIP_ID_MASK 0xf0 66 70 67 71 enum { 68 72 TSL258X_CHIP_UNKNOWN = 0, ··· 126 124 struct gainadj { 127 125 s16 ch0; 128 126 s16 ch1; 127 + s16 mean; 129 128 }; 130 129 131 130 /* Index = (0 - 3) Used to validate the gain selection index */ 132 131 static const struct gainadj gainadj[] = { 133 - { 1, 1 }, 134 - { 8, 8 }, 135 - { 16, 16 }, 136 - { 107, 115 } 132 + { 1, 1, 1 }, 133 + { 8, 8, 8 }, 134 + { 16, 16, 16 }, 135 + { 107, 115, 111 } 137 136 }; 138 137 139 138 /* ··· 143 140 */ 144 141 static void taos_defaults(struct tsl2583_chip *chip) 145 142 { 146 - /* Operational parameters */ 143 + /* 144 + * The integration time must be a multiple of 50ms and within the 145 + * range [50, 600] ms. 146 + */ 147 147 chip->taos_settings.als_time = 100; 148 - /* must be a multiple of 50mS */ 148 + 149 + /* 150 + * This is an index into the gainadj table. Assume clear glass as the 151 + * default. 152 + */ 149 153 chip->taos_settings.als_gain = 0; 150 - /* this is actually an index into the gain table */ 151 - /* assume clear glass as default */ 154 + 155 + /* Default gain trim to account for aperture effects */ 152 156 chip->taos_settings.als_gain_trim = 1000; 153 - /* default gain trim to account for aperture effects */ 154 - chip->taos_settings.als_cal_target = 130; 157 + 155 158 /* Known external ALS reading used for calibration */ 156 - } 157 - 158 - /* 159 - * Read a number of bytes starting at register (reg) location. 160 - * Return 0, or i2c_smbus_write_byte ERROR code. 161 - */ 162 - static int 163 - taos_i2c_read(struct i2c_client *client, u8 reg, u8 *val, unsigned int len) 164 - { 165 - int i, ret; 166 - 167 - for (i = 0; i < len; i++) { 168 - /* select register to write */ 169 - ret = i2c_smbus_write_byte(client, (TSL258X_CMD_REG | reg)); 170 - if (ret < 0) { 171 - dev_err(&client->dev, 172 - "taos_i2c_read failed to write register %x\n", 173 - reg); 174 - return ret; 175 - } 176 - /* read the data */ 177 - *val = i2c_smbus_read_byte(client); 178 - val++; 179 - reg++; 180 - } 181 - return 0; 159 + chip->taos_settings.als_cal_target = 130; 182 160 } 183 161 184 162 /* ··· 188 204 u32 ch0lux = 0; 189 205 u32 ch1lux = 0; 190 206 191 - if (mutex_trylock(&chip->als_mutex) == 0) { 192 - dev_info(&chip->client->dev, "taos_get_lux device is busy\n"); 193 - return chip->als_cur_info.lux; /* busy, so return LAST VALUE */ 194 - } 195 - 196 207 if (chip->taos_chip_status != TSL258X_CHIP_WORKING) { 197 208 /* device is not enabled */ 198 209 dev_err(&chip->client->dev, "taos_get_lux device is not enabled\n"); 199 210 ret = -EBUSY; 200 - goto out_unlock; 211 + goto done; 201 212 } 202 213 203 - ret = taos_i2c_read(chip->client, (TSL258X_CMD_REG), &buf[0], 1); 214 + ret = i2c_smbus_read_byte_data(chip->client, TSL258X_CMD_REG); 204 215 if (ret < 0) { 205 216 dev_err(&chip->client->dev, "taos_get_lux failed to read CMD_REG\n"); 206 - goto out_unlock; 217 + goto done; 207 218 } 219 + 208 220 /* is data new & valid */ 209 - if (!(buf[0] & TSL258X_STA_ADC_INTR)) { 221 + if (!(ret & TSL258X_STA_ADC_INTR)) { 210 222 dev_err(&chip->client->dev, "taos_get_lux data not valid\n"); 211 223 ret = chip->als_cur_info.lux; /* return LAST VALUE */ 212 - goto out_unlock; 224 + goto done; 213 225 } 214 226 215 227 for (i = 0; i < 4; i++) { 216 228 int reg = TSL258X_CMD_REG | (TSL258X_ALS_CHAN0LO + i); 217 229 218 - ret = taos_i2c_read(chip->client, reg, &buf[i], 1); 230 + ret = i2c_smbus_read_byte_data(chip->client, reg); 219 231 if (ret < 0) { 220 232 dev_err(&chip->client->dev, 221 233 "taos_get_lux failed to read register %x\n", 222 234 reg); 223 - goto out_unlock; 235 + goto done; 224 236 } 237 + buf[i] = ret; 225 238 } 226 239 227 240 /* ··· 233 252 dev_err(&chip->client->dev, 234 253 "taos_i2c_write_command failed in taos_get_lux, err = %d\n", 235 254 ret); 236 - goto out_unlock; /* have no data, so return failure */ 255 + goto done; /* have no data, so return failure */ 237 256 } 238 257 239 258 /* extract ALS/lux data */ ··· 250 269 /* have no data, so return LAST VALUE */ 251 270 ret = 0; 252 271 chip->als_cur_info.lux = 0; 253 - goto out_unlock; 272 + goto done; 254 273 } 255 274 /* calculate ratio */ 256 275 ratio = (ch1 << 15) / ch0; ··· 276 295 dev_dbg(&chip->client->dev, "No Data - Return last value\n"); 277 296 ret = 0; 278 297 chip->als_cur_info.lux = 0; 279 - goto out_unlock; 298 + goto done; 280 299 } 281 300 282 301 /* adjust for active time scale */ ··· 308 327 chip->als_cur_info.lux = lux; 309 328 ret = lux; 310 329 311 - out_unlock: 312 - mutex_unlock(&chip->als_mutex); 330 + done: 313 331 return ret; 314 332 } 315 333 ··· 320 340 static int taos_als_calibrate(struct iio_dev *indio_dev) 321 341 { 322 342 struct tsl2583_chip *chip = iio_priv(indio_dev); 323 - u8 reg_val; 324 343 unsigned int gain_trim_val; 325 344 int ret; 326 345 int lux_val; 327 346 328 - ret = i2c_smbus_write_byte(chip->client, 329 - (TSL258X_CMD_REG | TSL258X_CNTRL)); 347 + ret = i2c_smbus_read_byte_data(chip->client, 348 + TSL258X_CMD_REG | TSL258X_CNTRL); 330 349 if (ret < 0) { 331 350 dev_err(&chip->client->dev, 332 - "taos_als_calibrate failed to reach the CNTRL register, ret=%d\n", 333 - ret); 351 + "%s failed to read from the CNTRL register\n", 352 + __func__); 334 353 return ret; 335 354 } 336 355 337 - reg_val = i2c_smbus_read_byte(chip->client); 338 - if ((reg_val & (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON)) 356 + if ((ret & (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON)) 339 357 != (TSL258X_CNTL_ADC_ENBL | TSL258X_CNTL_PWR_ON)) { 340 358 dev_err(&chip->client->dev, 341 359 "taos_als_calibrate failed: device not powered on with ADC enabled\n"); 342 - return -1; 343 - } 344 - 345 - ret = i2c_smbus_write_byte(chip->client, 346 - (TSL258X_CMD_REG | TSL258X_CNTRL)); 347 - if (ret < 0) { 348 - dev_err(&chip->client->dev, 349 - "taos_als_calibrate failed to reach the STATUS register, ret=%d\n", 350 - ret); 351 - return ret; 352 - } 353 - reg_val = i2c_smbus_read_byte(chip->client); 354 - 355 - if ((reg_val & TSL258X_STA_ADC_VALID) != TSL258X_STA_ADC_VALID) { 360 + return -EINVAL; 361 + } else if ((ret & TSL258X_STA_ADC_VALID) != TSL258X_STA_ADC_VALID) { 356 362 dev_err(&chip->client->dev, 357 363 "taos_als_calibrate failed: STATUS - ADC not valid.\n"); 358 364 return -ENODATA; ··· 399 433 chip->als_saturation = als_count * 922; /* 90% of full scale */ 400 434 chip->als_time_scale = (als_time + 25) / 50; 401 435 402 - /* 403 - * TSL258x Specific power-on / adc enable sequence 404 - * Power on the device 1st. 405 - */ 406 - utmp = TSL258X_CNTL_PWR_ON; 407 - ret = i2c_smbus_write_byte_data(chip->client, 408 - TSL258X_CMD_REG | TSL258X_CNTRL, utmp); 409 - if (ret < 0) { 410 - dev_err(&chip->client->dev, "taos_chip_on failed on CNTRL reg.\n"); 411 - return ret; 412 - } 436 + /* Power on the device; ADC off. */ 437 + chip->taos_config[TSL258X_CNTRL] = TSL258X_CNTL_PWR_ON; 413 438 414 439 /* 415 440 * Use the following shadow copy for our delay before enabling ADC. ··· 448 491 449 492 /* Sysfs Interface Functions */ 450 493 451 - static ssize_t taos_power_state_show(struct device *dev, 452 - struct device_attribute *attr, char *buf) 494 + static ssize_t in_illuminance_input_target_show(struct device *dev, 495 + struct device_attribute *attr, 496 + char *buf) 453 497 { 454 498 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 455 499 struct tsl2583_chip *chip = iio_priv(indio_dev); 456 - 457 - return sprintf(buf, "%d\n", chip->taos_chip_status); 458 - } 459 - 460 - static ssize_t taos_power_state_store(struct device *dev, 461 - struct device_attribute *attr, 462 - const char *buf, size_t len) 463 - { 464 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 465 - int value; 466 - 467 - if (kstrtoint(buf, 0, &value)) 468 - return -EINVAL; 469 - 470 - if (!value) 471 - taos_chip_off(indio_dev); 472 - else 473 - taos_chip_on(indio_dev); 474 - 475 - return len; 476 - } 477 - 478 - static ssize_t taos_gain_show(struct device *dev, 479 - struct device_attribute *attr, char *buf) 480 - { 481 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 482 - struct tsl2583_chip *chip = iio_priv(indio_dev); 483 - char gain[4] = {0}; 484 - 485 - switch (chip->taos_settings.als_gain) { 486 - case 0: 487 - strcpy(gain, "001"); 488 - break; 489 - case 1: 490 - strcpy(gain, "008"); 491 - break; 492 - case 2: 493 - strcpy(gain, "016"); 494 - break; 495 - case 3: 496 - strcpy(gain, "111"); 497 - break; 498 - } 499 - 500 - return sprintf(buf, "%s\n", gain); 501 - } 502 - 503 - static ssize_t taos_gain_store(struct device *dev, 504 - struct device_attribute *attr, 505 - const char *buf, size_t len) 506 - { 507 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 508 - struct tsl2583_chip *chip = iio_priv(indio_dev); 509 - int value; 510 - 511 - if (kstrtoint(buf, 0, &value)) 512 - return -EINVAL; 513 - 514 - switch (value) { 515 - case 1: 516 - chip->taos_settings.als_gain = 0; 517 - break; 518 - case 8: 519 - chip->taos_settings.als_gain = 1; 520 - break; 521 - case 16: 522 - chip->taos_settings.als_gain = 2; 523 - break; 524 - case 111: 525 - chip->taos_settings.als_gain = 3; 526 - break; 527 - default: 528 - dev_err(dev, "Invalid Gain Index (must be 1,8,16,111)\n"); 529 - return -1; 530 - } 531 - 532 - return len; 533 - } 534 - 535 - static ssize_t taos_gain_available_show(struct device *dev, 536 - struct device_attribute *attr, 537 - char *buf) 538 - { 539 - return sprintf(buf, "%s\n", "1 8 16 111"); 540 - } 541 - 542 - static ssize_t taos_als_time_show(struct device *dev, 543 - struct device_attribute *attr, char *buf) 544 - { 545 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 546 - struct tsl2583_chip *chip = iio_priv(indio_dev); 547 - 548 - return sprintf(buf, "%d\n", chip->taos_settings.als_time); 549 - } 550 - 551 - static ssize_t taos_als_time_store(struct device *dev, 552 - struct device_attribute *attr, 553 - const char *buf, size_t len) 554 - { 555 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 556 - struct tsl2583_chip *chip = iio_priv(indio_dev); 557 - int value; 558 - 559 - if (kstrtoint(buf, 0, &value)) 560 - return -EINVAL; 561 - 562 - if ((value < 50) || (value > 650)) 563 - return -EINVAL; 564 - 565 - if (value % 50) 566 - return -EINVAL; 567 - 568 - chip->taos_settings.als_time = value; 569 - 570 - return len; 571 - } 572 - 573 - static ssize_t taos_als_time_available_show(struct device *dev, 574 - struct device_attribute *attr, 575 - char *buf) 576 - { 577 - return sprintf(buf, "%s\n", 578 - "50 100 150 200 250 300 350 400 450 500 550 600 650"); 579 - } 580 - 581 - static ssize_t taos_als_trim_show(struct device *dev, 582 - struct device_attribute *attr, char *buf) 583 - { 584 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 585 - struct tsl2583_chip *chip = iio_priv(indio_dev); 586 - 587 - return sprintf(buf, "%d\n", chip->taos_settings.als_gain_trim); 588 - } 589 - 590 - static ssize_t taos_als_trim_store(struct device *dev, 591 - struct device_attribute *attr, 592 - const char *buf, size_t len) 593 - { 594 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 595 - struct tsl2583_chip *chip = iio_priv(indio_dev); 596 - int value; 597 - 598 - if (kstrtoint(buf, 0, &value)) 599 - return -EINVAL; 600 - 601 - if (value) 602 - chip->taos_settings.als_gain_trim = value; 603 - 604 - return len; 605 - } 606 - 607 - static ssize_t taos_als_cal_target_show(struct device *dev, 608 - struct device_attribute *attr, 609 - char *buf) 610 - { 611 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 612 - struct tsl2583_chip *chip = iio_priv(indio_dev); 613 - 614 - return sprintf(buf, "%d\n", chip->taos_settings.als_cal_target); 615 - } 616 - 617 - static ssize_t taos_als_cal_target_store(struct device *dev, 618 - struct device_attribute *attr, 619 - const char *buf, size_t len) 620 - { 621 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 622 - struct tsl2583_chip *chip = iio_priv(indio_dev); 623 - int value; 624 - 625 - if (kstrtoint(buf, 0, &value)) 626 - return -EINVAL; 627 - 628 - if (value) 629 - chip->taos_settings.als_cal_target = value; 630 - 631 - return len; 632 - } 633 - 634 - static ssize_t taos_lux_show(struct device *dev, struct device_attribute *attr, 635 - char *buf) 636 - { 637 500 int ret; 638 501 639 - ret = taos_get_lux(dev_to_iio_dev(dev)); 640 - if (ret < 0) 641 - return ret; 502 + mutex_lock(&chip->als_mutex); 503 + ret = sprintf(buf, "%d\n", chip->taos_settings.als_cal_target); 504 + mutex_unlock(&chip->als_mutex); 642 505 643 - return sprintf(buf, "%d\n", ret); 506 + return ret; 644 507 } 645 508 646 - static ssize_t taos_do_calibrate(struct device *dev, 647 - struct device_attribute *attr, 648 - const char *buf, size_t len) 509 + static ssize_t in_illuminance_input_target_store(struct device *dev, 510 + struct device_attribute *attr, 511 + const char *buf, size_t len) 649 512 { 650 513 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 514 + struct tsl2583_chip *chip = iio_priv(indio_dev); 651 515 int value; 652 516 653 - if (kstrtoint(buf, 0, &value)) 517 + if (kstrtoint(buf, 0, &value) || !value) 654 518 return -EINVAL; 655 519 656 - if (value == 1) 657 - taos_als_calibrate(indio_dev); 520 + mutex_lock(&chip->als_mutex); 521 + chip->taos_settings.als_cal_target = value; 522 + mutex_unlock(&chip->als_mutex); 658 523 659 524 return len; 660 525 } 661 526 662 - static ssize_t taos_luxtable_show(struct device *dev, 663 - struct device_attribute *attr, char *buf) 527 + static ssize_t in_illuminance_calibrate_store(struct device *dev, 528 + struct device_attribute *attr, 529 + const char *buf, size_t len) 530 + { 531 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 532 + struct tsl2583_chip *chip = iio_priv(indio_dev); 533 + int value; 534 + 535 + if (kstrtoint(buf, 0, &value) || value != 1) 536 + return -EINVAL; 537 + 538 + mutex_lock(&chip->als_mutex); 539 + taos_als_calibrate(indio_dev); 540 + mutex_unlock(&chip->als_mutex); 541 + 542 + return len; 543 + } 544 + 545 + static ssize_t in_illuminance_lux_table_show(struct device *dev, 546 + struct device_attribute *attr, 547 + char *buf) 664 548 { 665 549 int i; 666 550 int offset = 0; ··· 525 727 return offset; 526 728 } 527 729 528 - static ssize_t taos_luxtable_store(struct device *dev, 529 - struct device_attribute *attr, 530 - const char *buf, size_t len) 730 + static ssize_t in_illuminance_lux_table_store(struct device *dev, 731 + struct device_attribute *attr, 732 + const char *buf, size_t len) 531 733 { 532 734 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 533 735 struct tsl2583_chip *chip = iio_priv(indio_dev); 534 736 int value[ARRAY_SIZE(taos_device_lux) * 3 + 1]; 535 - int n; 737 + int n, ret = -EINVAL; 738 + 739 + mutex_lock(&chip->als_mutex); 536 740 537 741 get_options(buf, ARRAY_SIZE(value), value); 538 742 ··· 546 746 n = value[0]; 547 747 if ((n % 3) || n < 6 || n > ((ARRAY_SIZE(taos_device_lux) - 1) * 3)) { 548 748 dev_info(dev, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n); 549 - return -EINVAL; 749 + goto done; 550 750 } 551 751 if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) { 552 752 dev_info(dev, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n); 553 - return -EINVAL; 753 + goto done; 554 754 } 555 - 556 - if (chip->taos_chip_status == TSL258X_CHIP_WORKING) 557 - taos_chip_off(indio_dev); 558 755 559 756 /* Zero out the table */ 560 757 memset(taos_device_lux, 0, sizeof(taos_device_lux)); 561 758 memcpy(taos_device_lux, &value[1], (value[0] * 4)); 562 759 563 - taos_chip_on(indio_dev); 760 + ret = len; 564 761 565 - return len; 762 + done: 763 + mutex_unlock(&chip->als_mutex); 764 + 765 + return ret; 566 766 } 567 767 568 - static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, 569 - taos_power_state_show, taos_power_state_store); 570 - 571 - static DEVICE_ATTR(illuminance0_calibscale, S_IRUGO | S_IWUSR, 572 - taos_gain_show, taos_gain_store); 573 - static DEVICE_ATTR(illuminance0_calibscale_available, S_IRUGO, 574 - taos_gain_available_show, NULL); 575 - 576 - static DEVICE_ATTR(illuminance0_integration_time, S_IRUGO | S_IWUSR, 577 - taos_als_time_show, taos_als_time_store); 578 - static DEVICE_ATTR(illuminance0_integration_time_available, S_IRUGO, 579 - taos_als_time_available_show, NULL); 580 - 581 - static DEVICE_ATTR(illuminance0_calibbias, S_IRUGO | S_IWUSR, 582 - taos_als_trim_show, taos_als_trim_store); 583 - 584 - static DEVICE_ATTR(illuminance0_input_target, S_IRUGO | S_IWUSR, 585 - taos_als_cal_target_show, taos_als_cal_target_store); 586 - 587 - static DEVICE_ATTR(illuminance0_input, S_IRUGO, taos_lux_show, NULL); 588 - static DEVICE_ATTR(illuminance0_calibrate, S_IWUSR, NULL, taos_do_calibrate); 589 - static DEVICE_ATTR(illuminance0_lux_table, S_IRUGO | S_IWUSR, 590 - taos_luxtable_show, taos_luxtable_store); 768 + static IIO_CONST_ATTR(in_illuminance_calibscale_available, "1 8 16 111"); 769 + static IIO_CONST_ATTR(in_illuminance_integration_time_available, 770 + "0.000050 0.000100 0.000150 0.000200 0.000250 0.000300 0.000350 0.000400 0.000450 0.000500 0.000550 0.000600 0.000650"); 771 + static IIO_DEVICE_ATTR_RW(in_illuminance_input_target, 0); 772 + static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate, 0); 773 + static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table, 0); 591 774 592 775 static struct attribute *sysfs_attrs_ctrl[] = { 593 - &dev_attr_power_state.attr, 594 - &dev_attr_illuminance0_calibscale.attr, /* Gain */ 595 - &dev_attr_illuminance0_calibscale_available.attr, 596 - &dev_attr_illuminance0_integration_time.attr, /* I time*/ 597 - &dev_attr_illuminance0_integration_time_available.attr, 598 - &dev_attr_illuminance0_calibbias.attr, /* trim */ 599 - &dev_attr_illuminance0_input_target.attr, 600 - &dev_attr_illuminance0_input.attr, 601 - &dev_attr_illuminance0_calibrate.attr, 602 - &dev_attr_illuminance0_lux_table.attr, 776 + &iio_const_attr_in_illuminance_calibscale_available.dev_attr.attr, 777 + &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr, 778 + &iio_dev_attr_in_illuminance_input_target.dev_attr.attr, 779 + &iio_dev_attr_in_illuminance_calibrate.dev_attr.attr, 780 + &iio_dev_attr_in_illuminance_lux_table.dev_attr.attr, 603 781 NULL 604 782 }; 605 783 ··· 585 807 .attrs = sysfs_attrs_ctrl, 586 808 }; 587 809 588 - /* Use the default register values to identify the Taos device */ 589 - static int taos_tsl258x_device(unsigned char *bufp) 810 + static const struct iio_chan_spec tsl2583_channels[] = { 811 + { 812 + .type = IIO_LIGHT, 813 + .modified = 1, 814 + .channel2 = IIO_MOD_LIGHT_IR, 815 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 816 + }, 817 + { 818 + .type = IIO_LIGHT, 819 + .modified = 1, 820 + .channel2 = IIO_MOD_LIGHT_BOTH, 821 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 822 + }, 823 + { 824 + .type = IIO_LIGHT, 825 + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 826 + BIT(IIO_CHAN_INFO_CALIBBIAS) | 827 + BIT(IIO_CHAN_INFO_CALIBSCALE) | 828 + BIT(IIO_CHAN_INFO_INT_TIME), 829 + }, 830 + }; 831 + 832 + static int tsl2583_read_raw(struct iio_dev *indio_dev, 833 + struct iio_chan_spec const *chan, 834 + int *val, int *val2, long mask) 590 835 { 591 - return ((bufp[TSL258X_CHIPID] & 0xf0) == 0x90); 836 + struct tsl2583_chip *chip = iio_priv(indio_dev); 837 + int ret = -EINVAL; 838 + 839 + mutex_lock(&chip->als_mutex); 840 + 841 + if (chip->taos_chip_status != TSL258X_CHIP_WORKING) { 842 + ret = -EBUSY; 843 + goto read_done; 844 + } 845 + 846 + switch (mask) { 847 + case IIO_CHAN_INFO_RAW: 848 + if (chan->type == IIO_LIGHT) { 849 + ret = taos_get_lux(indio_dev); 850 + if (ret < 0) 851 + goto read_done; 852 + 853 + /* 854 + * From page 20 of the TSL2581, TSL2583 data 855 + * sheet (TAOS134 − MARCH 2011): 856 + * 857 + * One of the photodiodes (channel 0) is 858 + * sensitive to both visible and infrared light, 859 + * while the second photodiode (channel 1) is 860 + * sensitive primarily to infrared light. 861 + */ 862 + if (chan->channel2 == IIO_MOD_LIGHT_BOTH) 863 + *val = chip->als_cur_info.als_ch0; 864 + else 865 + *val = chip->als_cur_info.als_ch1; 866 + 867 + ret = IIO_VAL_INT; 868 + } 869 + break; 870 + case IIO_CHAN_INFO_PROCESSED: 871 + if (chan->type == IIO_LIGHT) { 872 + ret = taos_get_lux(indio_dev); 873 + if (ret < 0) 874 + goto read_done; 875 + 876 + *val = ret; 877 + ret = IIO_VAL_INT; 878 + } 879 + break; 880 + case IIO_CHAN_INFO_CALIBBIAS: 881 + if (chan->type == IIO_LIGHT) { 882 + *val = chip->taos_settings.als_gain_trim; 883 + ret = IIO_VAL_INT; 884 + } 885 + break; 886 + case IIO_CHAN_INFO_CALIBSCALE: 887 + if (chan->type == IIO_LIGHT) { 888 + *val = gainadj[chip->taos_settings.als_gain].mean; 889 + ret = IIO_VAL_INT; 890 + } 891 + break; 892 + case IIO_CHAN_INFO_INT_TIME: 893 + if (chan->type == IIO_LIGHT) { 894 + *val = 0; 895 + *val2 = chip->taos_settings.als_time; 896 + ret = IIO_VAL_INT_PLUS_MICRO; 897 + } 898 + break; 899 + default: 900 + break; 901 + } 902 + 903 + read_done: 904 + mutex_unlock(&chip->als_mutex); 905 + 906 + return ret; 907 + } 908 + 909 + static int tsl2583_write_raw(struct iio_dev *indio_dev, 910 + struct iio_chan_spec const *chan, 911 + int val, int val2, long mask) 912 + { 913 + struct tsl2583_chip *chip = iio_priv(indio_dev); 914 + int ret = -EINVAL; 915 + 916 + mutex_lock(&chip->als_mutex); 917 + 918 + if (chip->taos_chip_status != TSL258X_CHIP_WORKING) { 919 + ret = -EBUSY; 920 + goto write_done; 921 + } 922 + 923 + switch (mask) { 924 + case IIO_CHAN_INFO_CALIBBIAS: 925 + if (chan->type == IIO_LIGHT) { 926 + chip->taos_settings.als_gain_trim = val; 927 + ret = 0; 928 + } 929 + break; 930 + case IIO_CHAN_INFO_CALIBSCALE: 931 + if (chan->type == IIO_LIGHT) { 932 + int i; 933 + 934 + for (i = 0; i < ARRAY_SIZE(gainadj); i++) { 935 + if (gainadj[i].mean == val) { 936 + chip->taos_settings.als_gain = i; 937 + ret = 0; 938 + break; 939 + } 940 + } 941 + } 942 + break; 943 + case IIO_CHAN_INFO_INT_TIME: 944 + if (chan->type == IIO_LIGHT && !val && val2 >= 50 && 945 + val2 <= 650 && !(val2 % 50)) { 946 + chip->taos_settings.als_time = val2; 947 + ret = 0; 948 + } 949 + break; 950 + default: 951 + break; 952 + } 953 + 954 + write_done: 955 + mutex_unlock(&chip->als_mutex); 956 + 957 + return ret; 592 958 } 593 959 594 960 static const struct iio_info tsl2583_info = { 595 961 .attrs = &tsl2583_attribute_group, 596 962 .driver_module = THIS_MODULE, 963 + .read_raw = tsl2583_read_raw, 964 + .write_raw = tsl2583_write_raw, 597 965 }; 598 966 599 967 /* ··· 749 825 static int taos_probe(struct i2c_client *clientp, 750 826 const struct i2c_device_id *idp) 751 827 { 752 - int i, ret; 753 - unsigned char buf[TSL258X_MAX_DEVICE_REGS]; 828 + int ret; 754 829 struct tsl2583_chip *chip; 755 830 struct iio_dev *indio_dev; 756 831 ··· 770 847 chip->taos_chip_status = TSL258X_CHIP_UNKNOWN; 771 848 memcpy(chip->taos_config, taos_config, sizeof(chip->taos_config)); 772 849 773 - for (i = 0; i < TSL258X_MAX_DEVICE_REGS; i++) { 774 - ret = i2c_smbus_write_byte(clientp, 775 - (TSL258X_CMD_REG | (TSL258X_CNTRL + i))); 776 - if (ret < 0) { 777 - dev_err(&clientp->dev, 778 - "i2c_smbus_write_byte to cmd reg failed in taos_probe(), err = %d\n", 779 - ret); 780 - return ret; 781 - } 782 - ret = i2c_smbus_read_byte(clientp); 783 - if (ret < 0) { 784 - dev_err(&clientp->dev, 785 - "i2c_smbus_read_byte from reg failed in taos_probe(), err = %d\n", 786 - ret); 787 - return ret; 788 - } 789 - buf[i] = ret; 850 + ret = i2c_smbus_read_byte_data(clientp, 851 + TSL258X_CMD_REG | TSL258X_CHIPID); 852 + if (ret < 0) { 853 + dev_err(&clientp->dev, 854 + "%s failed to read the chip ID register\n", __func__); 855 + return ret; 790 856 } 791 857 792 - if (!taos_tsl258x_device(buf)) { 793 - dev_info(&clientp->dev, 794 - "i2c device found but does not match expected id in taos_probe()\n"); 858 + if ((ret & TSL2583_CHIP_ID_MASK) != TSL2583_CHIP_ID) { 859 + dev_info(&clientp->dev, "%s received an unknown chip ID %x\n", 860 + __func__, ret); 795 861 return -EINVAL; 796 862 } 797 863 ··· 793 881 } 794 882 795 883 indio_dev->info = &tsl2583_info; 884 + indio_dev->channels = tsl2583_channels; 885 + indio_dev->num_channels = ARRAY_SIZE(tsl2583_channels); 796 886 indio_dev->dev.parent = &clientp->dev; 797 887 indio_dev->modes = INDIO_DIRECT_MODE; 798 888 indio_dev->name = chip->client->name; ··· 808 894 taos_defaults(chip); 809 895 810 896 /* Make sure the chip is on */ 811 - taos_chip_on(indio_dev); 897 + ret = taos_chip_on(indio_dev); 898 + if (ret < 0) 899 + return ret; 812 900 813 901 dev_info(&clientp->dev, "Light sensor found.\n"); 814 902 return 0; ··· 863 947 }; 864 948 MODULE_DEVICE_TABLE(i2c, taos_idtable); 865 949 950 + #ifdef CONFIG_OF 951 + static const struct of_device_id taos2583_of_match[] = { 952 + { .compatible = "amstaos,tsl2580", }, 953 + { .compatible = "amstaos,tsl2581", }, 954 + { .compatible = "amstaos,tsl2583", }, 955 + { }, 956 + }; 957 + MODULE_DEVICE_TABLE(of, taos2583_of_match); 958 + #else 959 + #define taos2583_of_match NULL 960 + #endif 961 + 866 962 /* Driver definition */ 867 963 static struct i2c_driver taos_driver = { 868 964 .driver = { 869 965 .name = "tsl2583", 870 966 .pm = TAOS_PM_OPS, 967 + .of_match_table = taos2583_of_match, 871 968 }, 872 969 .id_table = taos_idtable, 873 970 .probe = taos_probe,
+10
include/linux/mfd/cros_ec.h
··· 148 148 int event_size; 149 149 }; 150 150 151 + /** 152 + * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information 153 + * 154 + * @sensor_num: Id of the sensor, as reported by the EC. 155 + */ 156 + struct cros_ec_sensor_platform { 157 + u8 sensor_num; 158 + }; 159 + 151 160 /* struct cros_ec_platform - ChromeOS EC platform information 152 161 * 153 162 * @ec_name: name of EC device (e.g. 'cros-ec', 'cros-pd', ...) ··· 184 175 struct cros_ec_device *ec_dev; 185 176 struct device *dev; 186 177 u16 cmd_offset; 178 + u32 features[2]; 187 179 }; 188 180 189 181 /**
+178 -5
include/linux/mfd/cros_ec_commands.h
··· 713 713 /* More than one command can use these structs to get/set paramters. */ 714 714 #define EC_CMD_GSV_PAUSE_IN_S5 0x0c 715 715 716 + /*****************************************************************************/ 717 + /* List the features supported by the firmware */ 718 + #define EC_CMD_GET_FEATURES 0x0d 719 + 720 + /* Supported features */ 721 + enum ec_feature_code { 722 + /* 723 + * This image contains a limited set of features. Another image 724 + * in RW partition may support more features. 725 + */ 726 + EC_FEATURE_LIMITED = 0, 727 + /* 728 + * Commands for probing/reading/writing/erasing the flash in the 729 + * EC are present. 730 + */ 731 + EC_FEATURE_FLASH = 1, 732 + /* 733 + * Can control the fan speed directly. 734 + */ 735 + EC_FEATURE_PWM_FAN = 2, 736 + /* 737 + * Can control the intensity of the keyboard backlight. 738 + */ 739 + EC_FEATURE_PWM_KEYB = 3, 740 + /* 741 + * Support Google lightbar, introduced on Pixel. 742 + */ 743 + EC_FEATURE_LIGHTBAR = 4, 744 + /* Control of LEDs */ 745 + EC_FEATURE_LED = 5, 746 + /* Exposes an interface to control gyro and sensors. 747 + * The host goes through the EC to access these sensors. 748 + * In addition, the EC may provide composite sensors, like lid angle. 749 + */ 750 + EC_FEATURE_MOTION_SENSE = 6, 751 + /* The keyboard is controlled by the EC */ 752 + EC_FEATURE_KEYB = 7, 753 + /* The AP can use part of the EC flash as persistent storage. */ 754 + EC_FEATURE_PSTORE = 8, 755 + /* The EC monitors BIOS port 80h, and can return POST codes. */ 756 + EC_FEATURE_PORT80 = 9, 757 + /* 758 + * Thermal management: include TMP specific commands. 759 + * Higher level than direct fan control. 760 + */ 761 + EC_FEATURE_THERMAL = 10, 762 + /* Can switch the screen backlight on/off */ 763 + EC_FEATURE_BKLIGHT_SWITCH = 11, 764 + /* Can switch the wifi module on/off */ 765 + EC_FEATURE_WIFI_SWITCH = 12, 766 + /* Monitor host events, through for example SMI or SCI */ 767 + EC_FEATURE_HOST_EVENTS = 13, 768 + /* The EC exposes GPIO commands to control/monitor connected devices. */ 769 + EC_FEATURE_GPIO = 14, 770 + /* The EC can send i2c messages to downstream devices. */ 771 + EC_FEATURE_I2C = 15, 772 + /* Command to control charger are included */ 773 + EC_FEATURE_CHARGER = 16, 774 + /* Simple battery support. */ 775 + EC_FEATURE_BATTERY = 17, 776 + /* 777 + * Support Smart battery protocol 778 + * (Common Smart Battery System Interface Specification) 779 + */ 780 + EC_FEATURE_SMART_BATTERY = 18, 781 + /* EC can dectect when the host hangs. */ 782 + EC_FEATURE_HANG_DETECT = 19, 783 + /* Report power information, for pit only */ 784 + EC_FEATURE_PMU = 20, 785 + /* Another Cros EC device is present downstream of this one */ 786 + EC_FEATURE_SUB_MCU = 21, 787 + /* Support USB Power delivery (PD) commands */ 788 + EC_FEATURE_USB_PD = 22, 789 + /* Control USB multiplexer, for audio through USB port for instance. */ 790 + EC_FEATURE_USB_MUX = 23, 791 + /* Motion Sensor code has an internal software FIFO */ 792 + EC_FEATURE_MOTION_SENSE_FIFO = 24, 793 + }; 794 + 795 + #define EC_FEATURE_MASK_0(event_code) (1UL << (event_code % 32)) 796 + #define EC_FEATURE_MASK_1(event_code) (1UL << (event_code - 32)) 797 + struct ec_response_get_features { 798 + uint32_t flags[2]; 799 + } __packed; 716 800 717 801 /*****************************************************************************/ 718 802 /* Flash commands */ ··· 1399 1315 */ 1400 1316 MOTIONSENSE_CMD_KB_WAKE_ANGLE = 5, 1401 1317 1318 + /* 1319 + * Returns a single sensor data. 1320 + */ 1321 + MOTIONSENSE_CMD_DATA = 6, 1322 + 1323 + /* 1324 + * Perform low level calibration.. On sensors that support it, ask to 1325 + * do offset calibration. 1326 + */ 1327 + MOTIONSENSE_CMD_PERFORM_CALIB = 10, 1328 + 1329 + /* 1330 + * Sensor Offset command is a setter/getter command for the offset used 1331 + * for calibration. The offsets can be calculated by the host, or via 1332 + * PERFORM_CALIB command. 1333 + */ 1334 + MOTIONSENSE_CMD_SENSOR_OFFSET = 11, 1335 + 1402 1336 /* Number of motionsense sub-commands. */ 1403 1337 MOTIONSENSE_NUM_CMDS 1404 1338 }; ··· 1437 1335 enum motionsensor_type { 1438 1336 MOTIONSENSE_TYPE_ACCEL = 0, 1439 1337 MOTIONSENSE_TYPE_GYRO = 1, 1338 + MOTIONSENSE_TYPE_MAG = 2, 1339 + MOTIONSENSE_TYPE_PROX = 3, 1340 + MOTIONSENSE_TYPE_LIGHT = 4, 1341 + MOTIONSENSE_TYPE_ACTIVITY = 5, 1342 + MOTIONSENSE_TYPE_MAX 1440 1343 }; 1441 1344 1442 1345 /* List of motion sensor locations. */ 1443 1346 enum motionsensor_location { 1444 1347 MOTIONSENSE_LOC_BASE = 0, 1445 1348 MOTIONSENSE_LOC_LID = 1, 1349 + MOTIONSENSE_LOC_MAX, 1446 1350 }; 1447 1351 1448 1352 /* List of motion sensor chips. */ ··· 1469 1361 */ 1470 1362 #define EC_MOTION_SENSE_NO_VALUE -1 1471 1363 1364 + #define EC_MOTION_SENSE_INVALID_CALIB_TEMP 0x8000 1365 + 1366 + /* Set Calibration information */ 1367 + #define MOTION_SENSE_SET_OFFSET 1 1368 + 1369 + struct ec_response_motion_sensor_data { 1370 + /* Flags for each sensor. */ 1371 + uint8_t flags; 1372 + /* Sensor number the data comes from */ 1373 + uint8_t sensor_num; 1374 + /* Each sensor is up to 3-axis. */ 1375 + union { 1376 + int16_t data[3]; 1377 + struct { 1378 + uint16_t rsvd; 1379 + uint32_t timestamp; 1380 + } __packed; 1381 + struct { 1382 + uint8_t activity; /* motionsensor_activity */ 1383 + uint8_t state; 1384 + int16_t add_info[2]; 1385 + }; 1386 + }; 1387 + } __packed; 1388 + 1472 1389 struct ec_params_motion_sense { 1473 1390 uint8_t cmd; 1474 1391 union { ··· 1511 1378 int16_t data; 1512 1379 } ec_rate, kb_wake_angle; 1513 1380 1381 + /* Used for MOTIONSENSE_CMD_SENSOR_OFFSET */ 1382 + struct { 1383 + uint8_t sensor_num; 1384 + 1385 + /* 1386 + * bit 0: If set (MOTION_SENSE_SET_OFFSET), set 1387 + * the calibration information in the EC. 1388 + * If unset, just retrieve calibration information. 1389 + */ 1390 + uint16_t flags; 1391 + 1392 + /* 1393 + * Temperature at calibration, in units of 0.01 C 1394 + * 0x8000: invalid / unknown. 1395 + * 0x0: 0C 1396 + * 0x7fff: +327.67C 1397 + */ 1398 + int16_t temp; 1399 + 1400 + /* 1401 + * Offset for calibration. 1402 + * Unit: 1403 + * Accelerometer: 1/1024 g 1404 + * Gyro: 1/1024 deg/s 1405 + * Compass: 1/16 uT 1406 + */ 1407 + int16_t offset[3]; 1408 + } __packed sensor_offset; 1409 + 1514 1410 /* Used for MOTIONSENSE_CMD_INFO. */ 1515 1411 struct { 1516 - /* Should be element of enum motionsensor_id. */ 1517 1412 uint8_t sensor_num; 1518 1413 } info; 1519 1414 ··· 1571 1410 /* Flags representing the motion sensor module. */ 1572 1411 uint8_t module_flags; 1573 1412 1574 - /* Flags for each sensor in enum motionsensor_id. */ 1575 - uint8_t sensor_flags[EC_MOTION_SENSOR_COUNT]; 1413 + /* Number of sensors managed directly by the EC. */ 1414 + uint8_t sensor_count; 1576 1415 1577 - /* Array of all sensor data. Each sensor is 3-axis. */ 1578 - int16_t data[3*EC_MOTION_SENSOR_COUNT]; 1416 + /* 1417 + * Sensor data is truncated if response_max is too small 1418 + * for holding all the data. 1419 + */ 1420 + struct ec_response_motion_sensor_data sensor[0]; 1579 1421 } dump; 1580 1422 1581 1423 /* Used for MOTIONSENSE_CMD_INFO. */ ··· 1593 1429 uint8_t chip; 1594 1430 } info; 1595 1431 1432 + /* Used for MOTIONSENSE_CMD_DATA */ 1433 + struct ec_response_motion_sensor_data data; 1434 + 1596 1435 /* 1597 1436 * Used for MOTIONSENSE_CMD_EC_RATE, MOTIONSENSE_CMD_SENSOR_ODR, 1598 1437 * MOTIONSENSE_CMD_SENSOR_RANGE, and ··· 1605 1438 /* Current value of the parameter queried. */ 1606 1439 int32_t ret; 1607 1440 } ec_rate, sensor_odr, sensor_range, kb_wake_angle; 1441 + 1442 + /* Used for MOTIONSENSE_CMD_SENSOR_OFFSET */ 1443 + struct { 1444 + int16_t temp; 1445 + int16_t offset[3]; 1446 + } sensor_offset, perform_calib; 1608 1447 }; 1609 1448 } __packed; 1610 1449
+8
include/linux/mfd/ti_am335x_tscadc.h
··· 23 23 #define REG_IRQENABLE 0x02C 24 24 #define REG_IRQCLR 0x030 25 25 #define REG_IRQWAKEUP 0x034 26 + #define REG_DMAENABLE_SET 0x038 27 + #define REG_DMAENABLE_CLEAR 0x03c 26 28 #define REG_CTRL 0x040 27 29 #define REG_ADCFSM 0x044 28 30 #define REG_CLKDIV 0x04C ··· 38 36 #define REG_FIFO0THR 0xE8 39 37 #define REG_FIFO1CNT 0xF0 40 38 #define REG_FIFO1THR 0xF4 39 + #define REG_DMA1REQ 0xF8 41 40 #define REG_FIFO0 0x100 42 41 #define REG_FIFO1 0x200 43 42 ··· 129 126 #define FIFOREAD_DATA_MASK (0xfff << 0) 130 127 #define FIFOREAD_CHNLID_MASK (0xf << 16) 131 128 129 + /* DMA ENABLE/CLEAR Register */ 130 + #define DMA_FIFO0 BIT(0) 131 + #define DMA_FIFO1 BIT(1) 132 + 132 133 /* Sequencer Status */ 133 134 #define SEQ_STATUS BIT(5) 134 135 #define CHARGE_STEP 0x11 ··· 162 155 struct device *dev; 163 156 struct regmap *regmap; 164 157 void __iomem *tscadc_base; 158 + phys_addr_t tscadc_phys_base; 165 159 int irq; 166 160 int used_cells; /* 1-2 */ 167 161 int tsc_wires;