diff --git a/drivers/iio/accel/apple_aop_accel.c b/drivers/iio/accel/apple_aop_accel.c index 947762ccd20d1a..89afb5ce31519e 100644 --- a/drivers/iio/accel/apple_aop_accel.c +++ b/drivers/iio/accel/apple_aop_accel.c @@ -18,24 +18,16 @@ * in the sensor's frame; the board's mount-matrix maps it. */ -#include #include #include #include -#include #include #include -#include #include #include #include #include #include -#include - -#define AOP_PROP_REPORT_INTERVAL 0x00 -#define AOP_PROP_BATCH_INTERVAL 0x16 -#define AOP_PROP_RAW_INTERVAL 0x5c #define AOP_REPORT_MOTION 0xc2 #define AOP_MOTION_LEN 22 @@ -44,7 +36,6 @@ #define AOP_ACCEL_DEFAULT_US 100000 /* 10 Hz */ #define AOP_ACCEL_MIN_US 10000 /* 100 Hz */ #define AOP_ACCEL_MAX_US 1000000 /* 1 Hz */ -#define AOP_ACCEL_IDLE_MS 2000 /* 9.80665 m/s^2 / 65536 */ #define AOP_ACCEL_SCALE_NANO 149637 @@ -59,134 +50,37 @@ struct apple_aop_accel { const struct apple_aop_accel_hw *hw; const struct apple_aop_service *service; struct iio_mount_matrix orientation; + struct apple_aop_stream stream; - struct mutex lock; /* interval, streaming, buffer_active */ - u32 interval_us; - bool streaming; - bool buffer_active; - struct delayed_work idle_work; - - spinlock_t sample_lock; /* sample, have_sample */ s32 sample[3]; - bool have_sample; - struct completion sample_ready; IIO_DECLARE_BUFFER_WITH_TS(s32, scan, 3); }; -static int apple_aop_accel_set_property(struct apple_aop_accel *accel, u32 tag, u32 value) -{ - __le32 le = cpu_to_le32(value); - - return apple_aop_set_property(accel->service, tag, &le, sizeof(le)); -} - -static int apple_aop_accel_set_interval(struct apple_aop_accel *accel, u32 interval_us) -{ - static const u32 tags[] = { - AOP_PROP_BATCH_INTERVAL, AOP_PROP_RAW_INTERVAL, AOP_PROP_REPORT_INTERVAL, - }; - int i, ret; - - for (i = 0; i < ARRAY_SIZE(tags); i++) { - ret = apple_aop_accel_set_property(accel, tags[i], - tags[i] == AOP_PROP_RAW_INTERVAL && - accel->hw->no_raw_stream ? 0 : interval_us); - if (ret) - return ret; - } - accel->interval_us = interval_us; - accel->streaming = true; - return 0; -} - -static void apple_aop_accel_stop_streaming(struct apple_aop_accel *accel) -{ - unsigned long flags; - - apple_aop_accel_set_property(accel, AOP_PROP_REPORT_INTERVAL, 0); - accel->streaming = false; - spin_lock_irqsave(&accel->sample_lock, flags); - accel->have_sample = false; - reinit_completion(&accel->sample_ready); - spin_unlock_irqrestore(&accel->sample_lock, flags); -} - -static void apple_aop_accel_idle_work(struct work_struct *work) -{ - struct apple_aop_accel *accel = container_of(work, struct apple_aop_accel, - idle_work.work); - - mutex_lock(&accel->lock); - if (!accel->buffer_active && accel->streaming) - apple_aop_accel_stop_streaming(accel); - mutex_unlock(&accel->lock); -} - -static int apple_aop_accel_ensure_sample(struct apple_aop_accel *accel) -{ - unsigned long flags; - bool need_wait = false; - unsigned int wait_ms; - int ret = 0; - - mutex_lock(&accel->lock); - wait_ms = max(500U, 2 * (accel->interval_us / USEC_PER_MSEC)); - if (!accel->streaming) { - spin_lock_irqsave(&accel->sample_lock, flags); - accel->have_sample = false; - reinit_completion(&accel->sample_ready); - spin_unlock_irqrestore(&accel->sample_lock, flags); - - ret = apple_aop_accel_set_interval(accel, accel->interval_us); - if (ret) { - mutex_unlock(&accel->lock); - return ret; - } - need_wait = true; - } else { - spin_lock_irqsave(&accel->sample_lock, flags); - need_wait = !accel->have_sample; - spin_unlock_irqrestore(&accel->sample_lock, flags); - } - if (!accel->buffer_active) - mod_delayed_work(system_dfl_wq, &accel->idle_work, - msecs_to_jiffies(AOP_ACCEL_IDLE_MS)); - mutex_unlock(&accel->lock); - - if (need_wait) { - long rem = wait_for_completion_interruptible_timeout( - &accel->sample_ready, msecs_to_jiffies(wait_ms)); - if (rem < 0) - return rem; - if (!rem) - return -EAGAIN; - } - - return 0; -} - static void apple_aop_accel_report(void *cookie, u16 subtype, const void *data, size_t len) { struct iio_dev *indio_dev = cookie; struct apple_aop_accel *accel = iio_priv(indio_dev); unsigned long flags; + s32 sample[3]; int i; if (subtype != AOP_REPORT_MOTION || len < AOP_MOTION_LEN) return; - spin_lock_irqsave(&accel->sample_lock, flags); for (i = 0; i < 3; i++) - accel->sample[i] = get_unaligned_le32(data + AOP_MOTION_XYZ + 4 * i); - accel->have_sample = true; - complete_all(&accel->sample_ready); - spin_unlock_irqrestore(&accel->sample_lock, flags); + sample[i] = get_unaligned_le32(data + AOP_MOTION_XYZ + 4 * i); + + spin_lock_irqsave(&accel->stream.sample_lock, flags); + memcpy(accel->sample, sample, sizeof(sample)); + apple_aop_stream_sample_ready(&accel->stream); + spin_unlock_irqrestore(&accel->stream.sample_lock, flags); - if (iio_buffer_enabled(indio_dev)) { - memcpy(accel->scan, accel->sample, sizeof(accel->sample)); + if (apple_aop_stream_push_begin(&accel->stream)) { + memcpy(accel->scan, sample, sizeof(sample)); iio_push_to_buffers_with_ts(indio_dev, accel->scan, sizeof(accel->scan), iio_get_time_ns(indio_dev)); + apple_aop_stream_push_end(&accel->stream); } } @@ -196,28 +90,28 @@ static int apple_aop_accel_read_raw(struct iio_dev *indio_dev, { struct apple_aop_accel *accel = iio_priv(indio_dev); unsigned long flags; + u32 interval_us; int ret; switch (mask) { case IIO_CHAN_INFO_RAW: - ret = apple_aop_accel_ensure_sample(accel); + ret = apple_aop_stream_sample_get(&accel->stream); if (ret) return ret; - spin_lock_irqsave(&accel->sample_lock, flags); + spin_lock_irqsave(&accel->stream.sample_lock, flags); *val = accel->sample[chan->scan_index]; - ret = accel->have_sample ? IIO_VAL_INT : -EAGAIN; - spin_unlock_irqrestore(&accel->sample_lock, flags); + ret = accel->stream.have_sample ? IIO_VAL_INT : -EAGAIN; + spin_unlock_irqrestore(&accel->stream.sample_lock, flags); + apple_aop_stream_sample_put(&accel->stream); return ret; case IIO_CHAN_INFO_SCALE: *val = 0; *val2 = AOP_ACCEL_SCALE_NANO; return IIO_VAL_INT_PLUS_NANO; case IIO_CHAN_INFO_SAMP_FREQ: - mutex_lock(&accel->lock); - *val = USEC_PER_SEC / accel->interval_us; - *val2 = (u64)(USEC_PER_SEC % accel->interval_us) * USEC_PER_SEC / - accel->interval_us; - mutex_unlock(&accel->lock); + interval_us = apple_aop_stream_get_interval(&accel->stream); + *val = USEC_PER_SEC / interval_us; + *val2 = (u64)(USEC_PER_SEC % interval_us) * USEC_PER_SEC / interval_us; return IIO_VAL_INT_PLUS_MICRO; default: return -EINVAL; @@ -231,7 +125,6 @@ static int apple_aop_accel_write_raw(struct iio_dev *indio_dev, struct apple_aop_accel *accel = iio_priv(indio_dev); u64 micro_hz; u32 interval; - int ret; if (mask != IIO_CHAN_INFO_SAMP_FREQ) return -EINVAL; @@ -244,15 +137,7 @@ static int apple_aop_accel_write_raw(struct iio_dev *indio_dev, interval = clamp_t(u64, div64_u64((u64)USEC_PER_SEC * USEC_PER_SEC, micro_hz), AOP_ACCEL_MIN_US, AOP_ACCEL_MAX_US); - mutex_lock(&accel->lock); - if (accel->streaming) { - ret = apple_aop_accel_set_interval(accel, interval); - } else { - accel->interval_us = interval; - ret = 0; - } - mutex_unlock(&accel->lock); - return ret; + return apple_aop_stream_set_interval(&accel->stream, interval); } static const struct iio_mount_matrix * @@ -312,28 +197,15 @@ static const struct apple_aop_calibration apple_aop_accel_calibration[] = { static int apple_aop_accel_buffer_postenable(struct iio_dev *indio_dev) { struct apple_aop_accel *accel = iio_priv(indio_dev); - int ret = 0; - mutex_lock(&accel->lock); - accel->buffer_active = true; - cancel_delayed_work(&accel->idle_work); - if (!accel->streaming) - ret = apple_aop_accel_set_interval(accel, accel->interval_us); - if (ret) - accel->buffer_active = false; - mutex_unlock(&accel->lock); - return ret; + return apple_aop_stream_buffer_enable(&accel->stream); } static int apple_aop_accel_buffer_predisable(struct iio_dev *indio_dev) { struct apple_aop_accel *accel = iio_priv(indio_dev); - mutex_lock(&accel->lock); - accel->buffer_active = false; - if (accel->streaming) - apple_aop_accel_stop_streaming(accel); - mutex_unlock(&accel->lock); + apple_aop_stream_buffer_disable(&accel->stream); return 0; } @@ -346,11 +218,7 @@ static void apple_aop_accel_stop(void *data) { struct apple_aop_accel *accel = data; - cancel_delayed_work_sync(&accel->idle_work); - mutex_lock(&accel->lock); - if (accel->streaming) - apple_aop_accel_stop_streaming(accel); - mutex_unlock(&accel->lock); + apple_aop_stream_stop(&accel->stream); apple_aop_unlisten(accel->service); } @@ -372,11 +240,8 @@ static int apple_aop_accel_probe(struct platform_device *pdev) accel->dev = dev; accel->hw = device_get_match_data(dev); accel->service = service; - accel->interval_us = AOP_ACCEL_DEFAULT_US; - mutex_init(&accel->lock); - spin_lock_init(&accel->sample_lock); - init_completion(&accel->sample_ready); - INIT_DELAYED_WORK(&accel->idle_work, apple_aop_accel_idle_work); + apple_aop_stream_init(&accel->stream, service, AOP_ACCEL_DEFAULT_US, + !accel->hw->no_raw_stream); ret = iio_read_mount_matrix(dev, &accel->orientation); if (ret) diff --git a/drivers/iio/gyro/apple_aop_gyro.c b/drivers/iio/gyro/apple_aop_gyro.c index 4a5b2e6b3e6b14..d3c869d94123ae 100644 --- a/drivers/iio/gyro/apple_aop_gyro.c +++ b/drivers/iio/gyro/apple_aop_gyro.c @@ -13,24 +13,16 @@ * 18 s32 temperature (unit unknown, not exported) */ -#include #include #include #include -#include #include #include #include -#include #include #include #include #include -#include - -#define AOP_PROP_REPORT_INTERVAL 0x00 -#define AOP_PROP_BATCH_INTERVAL 0x16 -#define AOP_PROP_RAW_INTERVAL 0x5c #define AOP_REPORT_GYRO 0xc1 #define AOP_GYRO_LEN 22 @@ -39,7 +31,6 @@ #define AOP_GYRO_DEFAULT_US 100000 /* 10 Hz */ #define AOP_GYRO_MIN_US 10000 /* 100 Hz */ #define AOP_GYRO_MAX_US 1000000 /* 1 Hz */ -#define AOP_GYRO_IDLE_MS 2000 /* pi / 180 rad/s / 65536 = 1171270 / 2^42 rad/s */ #define AOP_GYRO_SCALE 1171270 @@ -49,132 +40,37 @@ struct apple_aop_gyro { struct device *dev; const struct apple_aop_service *service; struct iio_mount_matrix orientation; + struct apple_aop_stream stream; - struct mutex lock; /* interval, streaming, buffer_active */ - u32 interval_us; - bool streaming; - bool buffer_active; - struct delayed_work idle_work; - - spinlock_t sample_lock; /* sample, have_sample */ s32 sample[3]; - bool have_sample; - struct completion sample_ready; IIO_DECLARE_BUFFER_WITH_TS(s32, scan, 3); }; -static int apple_aop_gyro_set_u32(struct apple_aop_gyro *gyro, u32 key, u32 value) -{ - __le32 le = cpu_to_le32(value); - - return apple_aop_set_property(gyro->service, key, &le, sizeof(le)); -} - -/* No separate raw sample stream: raw interval 0. */ -static int apple_aop_gyro_set_interval(struct apple_aop_gyro *gyro, u32 interval_us) -{ - int ret; - - ret = apple_aop_gyro_set_u32(gyro, AOP_PROP_BATCH_INTERVAL, interval_us); - if (!ret) - ret = apple_aop_gyro_set_u32(gyro, AOP_PROP_RAW_INTERVAL, 0); - if (!ret) - ret = apple_aop_gyro_set_u32(gyro, AOP_PROP_REPORT_INTERVAL, interval_us); - if (!ret) { - gyro->interval_us = interval_us; - gyro->streaming = true; - } - return ret; -} - -static void apple_aop_gyro_stop_streaming(struct apple_aop_gyro *gyro) -{ - unsigned long flags; - - apple_aop_gyro_set_u32(gyro, AOP_PROP_REPORT_INTERVAL, 0); - gyro->streaming = false; - spin_lock_irqsave(&gyro->sample_lock, flags); - gyro->have_sample = false; - reinit_completion(&gyro->sample_ready); - spin_unlock_irqrestore(&gyro->sample_lock, flags); -} - -static void apple_aop_gyro_idle_work(struct work_struct *work) -{ - struct apple_aop_gyro *gyro = container_of(work, struct apple_aop_gyro, - idle_work.work); - - mutex_lock(&gyro->lock); - if (!gyro->buffer_active && gyro->streaming) - apple_aop_gyro_stop_streaming(gyro); - mutex_unlock(&gyro->lock); -} - -static int apple_aop_gyro_ensure_sample(struct apple_aop_gyro *gyro) -{ - unsigned long flags; - bool need_wait = false; - unsigned int wait_ms; - int ret = 0; - - mutex_lock(&gyro->lock); - wait_ms = max(500U, 2 * (gyro->interval_us / USEC_PER_MSEC)); - if (!gyro->streaming) { - spin_lock_irqsave(&gyro->sample_lock, flags); - gyro->have_sample = false; - reinit_completion(&gyro->sample_ready); - spin_unlock_irqrestore(&gyro->sample_lock, flags); - - ret = apple_aop_gyro_set_interval(gyro, gyro->interval_us); - if (ret) { - mutex_unlock(&gyro->lock); - return ret; - } - need_wait = true; - } else { - spin_lock_irqsave(&gyro->sample_lock, flags); - need_wait = !gyro->have_sample; - spin_unlock_irqrestore(&gyro->sample_lock, flags); - } - if (!gyro->buffer_active) - mod_delayed_work(system_dfl_wq, &gyro->idle_work, - msecs_to_jiffies(AOP_GYRO_IDLE_MS)); - mutex_unlock(&gyro->lock); - - if (need_wait) { - long rem = wait_for_completion_interruptible_timeout( - &gyro->sample_ready, msecs_to_jiffies(wait_ms)); - if (rem < 0) - return rem; - if (!rem) - return -EAGAIN; - } - - return 0; -} - static void apple_aop_gyro_report(void *cookie, u16 subtype, const void *data, size_t len) { struct iio_dev *indio_dev = cookie; struct apple_aop_gyro *gyro = iio_priv(indio_dev); unsigned long flags; + s32 sample[3]; int i; if (subtype != AOP_REPORT_GYRO || len < AOP_GYRO_LEN) return; - spin_lock_irqsave(&gyro->sample_lock, flags); for (i = 0; i < 3; i++) - gyro->sample[i] = get_unaligned_le32(data + AOP_GYRO_XYZ + 4 * i); - gyro->have_sample = true; - complete_all(&gyro->sample_ready); - spin_unlock_irqrestore(&gyro->sample_lock, flags); + sample[i] = get_unaligned_le32(data + AOP_GYRO_XYZ + 4 * i); + + spin_lock_irqsave(&gyro->stream.sample_lock, flags); + memcpy(gyro->sample, sample, sizeof(sample)); + apple_aop_stream_sample_ready(&gyro->stream); + spin_unlock_irqrestore(&gyro->stream.sample_lock, flags); - if (iio_buffer_enabled(indio_dev)) { - memcpy(gyro->scan, gyro->sample, sizeof(gyro->sample)); + if (apple_aop_stream_push_begin(&gyro->stream)) { + memcpy(gyro->scan, sample, sizeof(sample)); iio_push_to_buffers_with_ts(indio_dev, gyro->scan, sizeof(gyro->scan), iio_get_time_ns(indio_dev)); + apple_aop_stream_push_end(&gyro->stream); } } @@ -184,28 +80,28 @@ static int apple_aop_gyro_read_raw(struct iio_dev *indio_dev, { struct apple_aop_gyro *gyro = iio_priv(indio_dev); unsigned long flags; + u32 interval_us; int ret; switch (mask) { case IIO_CHAN_INFO_RAW: - ret = apple_aop_gyro_ensure_sample(gyro); + ret = apple_aop_stream_sample_get(&gyro->stream); if (ret) return ret; - spin_lock_irqsave(&gyro->sample_lock, flags); + spin_lock_irqsave(&gyro->stream.sample_lock, flags); *val = gyro->sample[chan->scan_index]; - ret = gyro->have_sample ? IIO_VAL_INT : -EAGAIN; - spin_unlock_irqrestore(&gyro->sample_lock, flags); + ret = gyro->stream.have_sample ? IIO_VAL_INT : -EAGAIN; + spin_unlock_irqrestore(&gyro->stream.sample_lock, flags); + apple_aop_stream_sample_put(&gyro->stream); return ret; case IIO_CHAN_INFO_SCALE: *val = AOP_GYRO_SCALE; *val2 = AOP_GYRO_SCALE_LOG2; return IIO_VAL_FRACTIONAL_LOG2; case IIO_CHAN_INFO_SAMP_FREQ: - mutex_lock(&gyro->lock); - *val = USEC_PER_SEC / gyro->interval_us; - *val2 = (u64)(USEC_PER_SEC % gyro->interval_us) * USEC_PER_SEC / - gyro->interval_us; - mutex_unlock(&gyro->lock); + interval_us = apple_aop_stream_get_interval(&gyro->stream); + *val = USEC_PER_SEC / interval_us; + *val2 = (u64)(USEC_PER_SEC % interval_us) * USEC_PER_SEC / interval_us; return IIO_VAL_INT_PLUS_MICRO; default: return -EINVAL; @@ -219,7 +115,6 @@ static int apple_aop_gyro_write_raw(struct iio_dev *indio_dev, struct apple_aop_gyro *gyro = iio_priv(indio_dev); u64 micro_hz; u32 interval; - int ret; if (mask != IIO_CHAN_INFO_SAMP_FREQ) return -EINVAL; @@ -232,15 +127,7 @@ static int apple_aop_gyro_write_raw(struct iio_dev *indio_dev, interval = clamp_t(u64, div64_u64((u64)USEC_PER_SEC * USEC_PER_SEC, micro_hz), AOP_GYRO_MIN_US, AOP_GYRO_MAX_US); - mutex_lock(&gyro->lock); - if (gyro->streaming) { - ret = apple_aop_gyro_set_interval(gyro, interval); - } else { - gyro->interval_us = interval; - ret = 0; - } - mutex_unlock(&gyro->lock); - return ret; + return apple_aop_stream_set_interval(&gyro->stream, interval); } static const struct iio_mount_matrix * @@ -300,28 +187,15 @@ static const struct apple_aop_calibration apple_aop_gyro_calibration[] = { static int apple_aop_gyro_buffer_postenable(struct iio_dev *indio_dev) { struct apple_aop_gyro *gyro = iio_priv(indio_dev); - int ret = 0; - mutex_lock(&gyro->lock); - gyro->buffer_active = true; - cancel_delayed_work(&gyro->idle_work); - if (!gyro->streaming) - ret = apple_aop_gyro_set_interval(gyro, gyro->interval_us); - if (ret) - gyro->buffer_active = false; - mutex_unlock(&gyro->lock); - return ret; + return apple_aop_stream_buffer_enable(&gyro->stream); } static int apple_aop_gyro_buffer_predisable(struct iio_dev *indio_dev) { struct apple_aop_gyro *gyro = iio_priv(indio_dev); - mutex_lock(&gyro->lock); - gyro->buffer_active = false; - if (gyro->streaming) - apple_aop_gyro_stop_streaming(gyro); - mutex_unlock(&gyro->lock); + apple_aop_stream_buffer_disable(&gyro->stream); return 0; } @@ -334,11 +208,7 @@ static void apple_aop_gyro_stop(void *data) { struct apple_aop_gyro *gyro = data; - cancel_delayed_work_sync(&gyro->idle_work); - mutex_lock(&gyro->lock); - if (gyro->streaming) - apple_aop_gyro_stop_streaming(gyro); - mutex_unlock(&gyro->lock); + apple_aop_stream_stop(&gyro->stream); apple_aop_unlisten(gyro->service); } @@ -359,11 +229,7 @@ static int apple_aop_gyro_probe(struct platform_device *pdev) gyro = iio_priv(indio_dev); gyro->dev = dev; gyro->service = service; - gyro->interval_us = AOP_GYRO_DEFAULT_US; - mutex_init(&gyro->lock); - spin_lock_init(&gyro->sample_lock); - init_completion(&gyro->sample_ready); - INIT_DELAYED_WORK(&gyro->idle_work, apple_aop_gyro_idle_work); + apple_aop_stream_init(&gyro->stream, service, AOP_GYRO_DEFAULT_US, false); ret = iio_read_mount_matrix(dev, &gyro->orientation); if (ret) diff --git a/drivers/iio/magnetometer/apple_aop_compass.c b/drivers/iio/magnetometer/apple_aop_compass.c index 1341b441e5bbb0..91827afbe0a3b2 100644 --- a/drivers/iio/magnetometer/apple_aop_compass.c +++ b/drivers/iio/magnetometer/apple_aop_compass.c @@ -23,24 +23,16 @@ * sensitivity matrix the firmware reports the field uncorrected. */ -#include #include #include #include -#include #include #include #include -#include #include #include #include #include -#include - -#define AOP_PROP_REPORT_INTERVAL 0x00 -#define AOP_PROP_BATCH_INTERVAL 0x16 -#define AOP_PROP_RAW_INTERVAL 0x5c #define AOP_REPORT_COMPASS 0xc3 #define AOP_COMPASS_LEN 22 @@ -49,7 +41,6 @@ #define AOP_COMPASS_DEFAULT_US 100000 /* 10 Hz */ #define AOP_COMPASS_MIN_US 10000 /* 100 Hz */ #define AOP_COMPASS_MAX_US 1000000 /* 1 Hz */ -#define AOP_COMPASS_IDLE_MS 2000 /* 1/65536 microtesla = 1/6553600 gauss */ #define AOP_COMPASS_SCALE_DIV 6553600 @@ -58,132 +49,37 @@ struct apple_aop_compass { struct device *dev; const struct apple_aop_service *service; struct iio_mount_matrix orientation; + struct apple_aop_stream stream; - struct mutex lock; /* interval, streaming, buffer_active */ - u32 interval_us; - bool streaming; - bool buffer_active; - struct delayed_work idle_work; - - spinlock_t sample_lock; /* sample, have_sample */ s32 sample[3]; - bool have_sample; - struct completion sample_ready; IIO_DECLARE_BUFFER_WITH_TS(s32, scan, 3); }; -static int apple_aop_compass_set_u32(struct apple_aop_compass *compass, u32 key, u32 value) -{ - __le32 le = cpu_to_le32(value); - - return apple_aop_set_property(compass->service, key, &le, sizeof(le)); -} - -/* No separate raw sample stream: raw interval 0. */ -static int apple_aop_compass_set_interval(struct apple_aop_compass *compass, u32 interval_us) -{ - int ret; - - ret = apple_aop_compass_set_u32(compass, AOP_PROP_BATCH_INTERVAL, interval_us); - if (!ret) - ret = apple_aop_compass_set_u32(compass, AOP_PROP_RAW_INTERVAL, 0); - if (!ret) - ret = apple_aop_compass_set_u32(compass, AOP_PROP_REPORT_INTERVAL, interval_us); - if (!ret) { - compass->interval_us = interval_us; - compass->streaming = true; - } - return ret; -} - -static void apple_aop_compass_stop_streaming(struct apple_aop_compass *compass) -{ - unsigned long flags; - - apple_aop_compass_set_u32(compass, AOP_PROP_REPORT_INTERVAL, 0); - compass->streaming = false; - spin_lock_irqsave(&compass->sample_lock, flags); - compass->have_sample = false; - reinit_completion(&compass->sample_ready); - spin_unlock_irqrestore(&compass->sample_lock, flags); -} - -static void apple_aop_compass_idle_work(struct work_struct *work) -{ - struct apple_aop_compass *compass = - container_of(work, struct apple_aop_compass, idle_work.work); - - mutex_lock(&compass->lock); - if (!compass->buffer_active && compass->streaming) - apple_aop_compass_stop_streaming(compass); - mutex_unlock(&compass->lock); -} - -static int apple_aop_compass_ensure_sample(struct apple_aop_compass *compass) -{ - unsigned long flags; - bool need_wait = false; - unsigned int wait_ms; - int ret = 0; - - mutex_lock(&compass->lock); - wait_ms = max(500U, 2 * (compass->interval_us / USEC_PER_MSEC)); - if (!compass->streaming) { - spin_lock_irqsave(&compass->sample_lock, flags); - compass->have_sample = false; - reinit_completion(&compass->sample_ready); - spin_unlock_irqrestore(&compass->sample_lock, flags); - - ret = apple_aop_compass_set_interval(compass, compass->interval_us); - if (ret) { - mutex_unlock(&compass->lock); - return ret; - } - need_wait = true; - } else { - spin_lock_irqsave(&compass->sample_lock, flags); - need_wait = !compass->have_sample; - spin_unlock_irqrestore(&compass->sample_lock, flags); - } - if (!compass->buffer_active) - mod_delayed_work(system_dfl_wq, &compass->idle_work, - msecs_to_jiffies(AOP_COMPASS_IDLE_MS)); - mutex_unlock(&compass->lock); - - if (need_wait) { - long rem = wait_for_completion_interruptible_timeout( - &compass->sample_ready, msecs_to_jiffies(wait_ms)); - if (rem < 0) - return rem; - if (!rem) - return -EAGAIN; - } - - return 0; -} - static void apple_aop_compass_report(void *cookie, u16 subtype, const void *data, size_t len) { struct iio_dev *indio_dev = cookie; struct apple_aop_compass *compass = iio_priv(indio_dev); unsigned long flags; + s32 sample[3]; int i; if (subtype != AOP_REPORT_COMPASS || len < AOP_COMPASS_LEN) return; - spin_lock_irqsave(&compass->sample_lock, flags); for (i = 0; i < 3; i++) - compass->sample[i] = get_unaligned_le32(data + AOP_COMPASS_XYZ + 4 * i); - compass->have_sample = true; - complete_all(&compass->sample_ready); - spin_unlock_irqrestore(&compass->sample_lock, flags); + sample[i] = get_unaligned_le32(data + AOP_COMPASS_XYZ + 4 * i); + + spin_lock_irqsave(&compass->stream.sample_lock, flags); + memcpy(compass->sample, sample, sizeof(sample)); + apple_aop_stream_sample_ready(&compass->stream); + spin_unlock_irqrestore(&compass->stream.sample_lock, flags); - if (iio_buffer_enabled(indio_dev)) { - memcpy(compass->scan, compass->sample, sizeof(compass->sample)); + if (apple_aop_stream_push_begin(&compass->stream)) { + memcpy(compass->scan, sample, sizeof(sample)); iio_push_to_buffers_with_ts(indio_dev, compass->scan, sizeof(compass->scan), iio_get_time_ns(indio_dev)); + apple_aop_stream_push_end(&compass->stream); } } @@ -193,28 +89,28 @@ static int apple_aop_compass_read_raw(struct iio_dev *indio_dev, { struct apple_aop_compass *compass = iio_priv(indio_dev); unsigned long flags; + u32 interval_us; int ret; switch (mask) { case IIO_CHAN_INFO_RAW: - ret = apple_aop_compass_ensure_sample(compass); + ret = apple_aop_stream_sample_get(&compass->stream); if (ret) return ret; - spin_lock_irqsave(&compass->sample_lock, flags); + spin_lock_irqsave(&compass->stream.sample_lock, flags); *val = compass->sample[chan->scan_index]; - ret = compass->have_sample ? IIO_VAL_INT : -EAGAIN; - spin_unlock_irqrestore(&compass->sample_lock, flags); + ret = compass->stream.have_sample ? IIO_VAL_INT : -EAGAIN; + spin_unlock_irqrestore(&compass->stream.sample_lock, flags); + apple_aop_stream_sample_put(&compass->stream); return ret; case IIO_CHAN_INFO_SCALE: *val = 1; *val2 = AOP_COMPASS_SCALE_DIV; return IIO_VAL_FRACTIONAL; case IIO_CHAN_INFO_SAMP_FREQ: - mutex_lock(&compass->lock); - *val = USEC_PER_SEC / compass->interval_us; - *val2 = (u64)(USEC_PER_SEC % compass->interval_us) * USEC_PER_SEC / - compass->interval_us; - mutex_unlock(&compass->lock); + interval_us = apple_aop_stream_get_interval(&compass->stream); + *val = USEC_PER_SEC / interval_us; + *val2 = (u64)(USEC_PER_SEC % interval_us) * USEC_PER_SEC / interval_us; return IIO_VAL_INT_PLUS_MICRO; default: return -EINVAL; @@ -228,7 +124,6 @@ static int apple_aop_compass_write_raw(struct iio_dev *indio_dev, struct apple_aop_compass *compass = iio_priv(indio_dev); u64 micro_hz; u32 interval; - int ret; if (mask != IIO_CHAN_INFO_SAMP_FREQ) return -EINVAL; @@ -241,15 +136,7 @@ static int apple_aop_compass_write_raw(struct iio_dev *indio_dev, interval = clamp_t(u64, div64_u64((u64)USEC_PER_SEC * USEC_PER_SEC, micro_hz), AOP_COMPASS_MIN_US, AOP_COMPASS_MAX_US); - mutex_lock(&compass->lock); - if (compass->streaming) { - ret = apple_aop_compass_set_interval(compass, interval); - } else { - compass->interval_us = interval; - ret = 0; - } - mutex_unlock(&compass->lock); - return ret; + return apple_aop_stream_set_interval(&compass->stream, interval); } static const struct iio_mount_matrix * @@ -308,28 +195,15 @@ static const struct apple_aop_calibration apple_aop_compass_calibration[] = { static int apple_aop_compass_buffer_postenable(struct iio_dev *indio_dev) { struct apple_aop_compass *compass = iio_priv(indio_dev); - int ret = 0; - mutex_lock(&compass->lock); - compass->buffer_active = true; - cancel_delayed_work(&compass->idle_work); - if (!compass->streaming) - ret = apple_aop_compass_set_interval(compass, compass->interval_us); - if (ret) - compass->buffer_active = false; - mutex_unlock(&compass->lock); - return ret; + return apple_aop_stream_buffer_enable(&compass->stream); } static int apple_aop_compass_buffer_predisable(struct iio_dev *indio_dev) { struct apple_aop_compass *compass = iio_priv(indio_dev); - mutex_lock(&compass->lock); - compass->buffer_active = false; - if (compass->streaming) - apple_aop_compass_stop_streaming(compass); - mutex_unlock(&compass->lock); + apple_aop_stream_buffer_disable(&compass->stream); return 0; } @@ -342,11 +216,7 @@ static void apple_aop_compass_stop(void *data) { struct apple_aop_compass *compass = data; - cancel_delayed_work_sync(&compass->idle_work); - mutex_lock(&compass->lock); - if (compass->streaming) - apple_aop_compass_stop_streaming(compass); - mutex_unlock(&compass->lock); + apple_aop_stream_stop(&compass->stream); apple_aop_unlisten(compass->service); } @@ -367,11 +237,7 @@ static int apple_aop_compass_probe(struct platform_device *pdev) compass = iio_priv(indio_dev); compass->dev = dev; compass->service = service; - compass->interval_us = AOP_COMPASS_DEFAULT_US; - mutex_init(&compass->lock); - spin_lock_init(&compass->sample_lock); - init_completion(&compass->sample_ready); - INIT_DELAYED_WORK(&compass->idle_work, apple_aop_compass_idle_work); + apple_aop_stream_init(&compass->stream, service, AOP_COMPASS_DEFAULT_US, false); ret = iio_read_mount_matrix(dev, &compass->orientation); if (ret) diff --git a/drivers/iio/pressure/apple_aop_pressure.c b/drivers/iio/pressure/apple_aop_pressure.c index 6e4b07fcb0d11f..02bd6a4fc907b9 100644 --- a/drivers/iio/pressure/apple_aop_pressure.c +++ b/drivers/iio/pressure/apple_aop_pressure.c @@ -12,24 +12,16 @@ * 6 s32 temperature (unit unknown, not exported) */ -#include #include #include #include -#include #include #include #include -#include #include #include #include #include -#include - -#define AOP_PROP_REPORT_INTERVAL 0x00 -#define AOP_PROP_BATCH_INTERVAL 0x16 -#define AOP_PROP_RAW_INTERVAL 0x5c #define AOP_REPORT_PRESSURE 0xc4 #define AOP_PRESSURE_LEN 10 @@ -38,135 +30,39 @@ #define AOP_PRESSURE_DEFAULT_US 100000 /* 10 Hz */ #define AOP_PRESSURE_MIN_US 10000 /* 100 Hz */ #define AOP_PRESSURE_MAX_US 1000000 /* 1 Hz */ -#define AOP_PRESSURE_IDLE_MS 2000 struct apple_aop_pressure { struct device *dev; const struct apple_aop_service *service; + struct apple_aop_stream stream; - struct mutex lock; /* interval, streaming, buffer_active */ - u32 interval_us; - bool streaming; - bool buffer_active; - struct delayed_work idle_work; - - spinlock_t sample_lock; /* sample, have_sample */ u32 sample; - bool have_sample; - struct completion sample_ready; IIO_DECLARE_BUFFER_WITH_TS(u32, scan, 1); }; -static int apple_aop_pressure_set_u32(struct apple_aop_pressure *pressure, u32 key, u32 value) -{ - __le32 le = cpu_to_le32(value); - - return apple_aop_set_property(pressure->service, key, &le, sizeof(le)); -} - -/* No separate raw sample stream: raw interval 0. */ -static int apple_aop_pressure_set_interval(struct apple_aop_pressure *pressure, u32 interval_us) -{ - int ret; - - ret = apple_aop_pressure_set_u32(pressure, AOP_PROP_BATCH_INTERVAL, interval_us); - if (!ret) - ret = apple_aop_pressure_set_u32(pressure, AOP_PROP_RAW_INTERVAL, 0); - if (!ret) - ret = apple_aop_pressure_set_u32(pressure, AOP_PROP_REPORT_INTERVAL, interval_us); - if (!ret) { - pressure->interval_us = interval_us; - pressure->streaming = true; - } - return ret; -} - -static void apple_aop_pressure_stop_streaming(struct apple_aop_pressure *pressure) -{ - unsigned long flags; - - apple_aop_pressure_set_u32(pressure, AOP_PROP_REPORT_INTERVAL, 0); - pressure->streaming = false; - spin_lock_irqsave(&pressure->sample_lock, flags); - pressure->have_sample = false; - reinit_completion(&pressure->sample_ready); - spin_unlock_irqrestore(&pressure->sample_lock, flags); -} - -static void apple_aop_pressure_idle_work(struct work_struct *work) -{ - struct apple_aop_pressure *pressure = - container_of(work, struct apple_aop_pressure, idle_work.work); - - mutex_lock(&pressure->lock); - if (!pressure->buffer_active && pressure->streaming) - apple_aop_pressure_stop_streaming(pressure); - mutex_unlock(&pressure->lock); -} - -static int apple_aop_pressure_ensure_sample(struct apple_aop_pressure *pressure) -{ - unsigned long flags; - bool need_wait = false; - unsigned int wait_ms; - int ret = 0; - - mutex_lock(&pressure->lock); - wait_ms = max(500U, 2 * (pressure->interval_us / USEC_PER_MSEC)); - if (!pressure->streaming) { - spin_lock_irqsave(&pressure->sample_lock, flags); - pressure->have_sample = false; - reinit_completion(&pressure->sample_ready); - spin_unlock_irqrestore(&pressure->sample_lock, flags); - - ret = apple_aop_pressure_set_interval(pressure, pressure->interval_us); - if (ret) { - mutex_unlock(&pressure->lock); - return ret; - } - need_wait = true; - } else { - spin_lock_irqsave(&pressure->sample_lock, flags); - need_wait = !pressure->have_sample; - spin_unlock_irqrestore(&pressure->sample_lock, flags); - } - if (!pressure->buffer_active) - mod_delayed_work(system_dfl_wq, &pressure->idle_work, - msecs_to_jiffies(AOP_PRESSURE_IDLE_MS)); - mutex_unlock(&pressure->lock); - - if (need_wait) { - long rem = wait_for_completion_interruptible_timeout( - &pressure->sample_ready, msecs_to_jiffies(wait_ms)); - if (rem < 0) - return rem; - if (!rem) - return -EAGAIN; - } - - return 0; -} - static void apple_aop_pressure_report(void *cookie, u16 subtype, const void *data, size_t len) { struct iio_dev *indio_dev = cookie; struct apple_aop_pressure *pressure = iio_priv(indio_dev); unsigned long flags; + u32 sample; if (subtype != AOP_REPORT_PRESSURE || len < AOP_PRESSURE_LEN) return; - spin_lock_irqsave(&pressure->sample_lock, flags); - pressure->sample = get_unaligned_le32(data + AOP_PRESSURE_VALUE); - pressure->have_sample = true; - complete_all(&pressure->sample_ready); - spin_unlock_irqrestore(&pressure->sample_lock, flags); + sample = get_unaligned_le32(data + AOP_PRESSURE_VALUE); + + spin_lock_irqsave(&pressure->stream.sample_lock, flags); + pressure->sample = sample; + apple_aop_stream_sample_ready(&pressure->stream); + spin_unlock_irqrestore(&pressure->stream.sample_lock, flags); - if (iio_buffer_enabled(indio_dev)) { - pressure->scan[0] = pressure->sample; + if (apple_aop_stream_push_begin(&pressure->stream)) { + pressure->scan[0] = sample; iio_push_to_buffers_with_ts(indio_dev, pressure->scan, sizeof(pressure->scan), iio_get_time_ns(indio_dev)); + apple_aop_stream_push_end(&pressure->stream); } } @@ -176,17 +72,19 @@ static int apple_aop_pressure_read_raw(struct iio_dev *indio_dev, { struct apple_aop_pressure *pressure = iio_priv(indio_dev); unsigned long flags; + u32 interval_us; int ret; switch (mask) { case IIO_CHAN_INFO_RAW: - ret = apple_aop_pressure_ensure_sample(pressure); + ret = apple_aop_stream_sample_get(&pressure->stream); if (ret) return ret; - spin_lock_irqsave(&pressure->sample_lock, flags); + spin_lock_irqsave(&pressure->stream.sample_lock, flags); *val = pressure->sample; - ret = pressure->have_sample ? IIO_VAL_INT : -EAGAIN; - spin_unlock_irqrestore(&pressure->sample_lock, flags); + ret = pressure->stream.have_sample ? IIO_VAL_INT : -EAGAIN; + spin_unlock_irqrestore(&pressure->stream.sample_lock, flags); + apple_aop_stream_sample_put(&pressure->stream); return ret; case IIO_CHAN_INFO_SCALE: /* kPa */ @@ -194,11 +92,9 @@ static int apple_aop_pressure_read_raw(struct iio_dev *indio_dev, *val2 = 16; return IIO_VAL_FRACTIONAL_LOG2; case IIO_CHAN_INFO_SAMP_FREQ: - mutex_lock(&pressure->lock); - *val = USEC_PER_SEC / pressure->interval_us; - *val2 = (u64)(USEC_PER_SEC % pressure->interval_us) * USEC_PER_SEC / - pressure->interval_us; - mutex_unlock(&pressure->lock); + interval_us = apple_aop_stream_get_interval(&pressure->stream); + *val = USEC_PER_SEC / interval_us; + *val2 = (u64)(USEC_PER_SEC % interval_us) * USEC_PER_SEC / interval_us; return IIO_VAL_INT_PLUS_MICRO; default: return -EINVAL; @@ -212,7 +108,6 @@ static int apple_aop_pressure_write_raw(struct iio_dev *indio_dev, struct apple_aop_pressure *pressure = iio_priv(indio_dev); u64 micro_hz; u32 interval; - int ret; if (mask != IIO_CHAN_INFO_SAMP_FREQ) return -EINVAL; @@ -225,15 +120,7 @@ static int apple_aop_pressure_write_raw(struct iio_dev *indio_dev, interval = clamp_t(u64, div64_u64((u64)USEC_PER_SEC * USEC_PER_SEC, micro_hz), AOP_PRESSURE_MIN_US, AOP_PRESSURE_MAX_US); - mutex_lock(&pressure->lock); - if (pressure->streaming) { - ret = apple_aop_pressure_set_interval(pressure, interval); - } else { - pressure->interval_us = interval; - ret = 0; - } - mutex_unlock(&pressure->lock); - return ret; + return apple_aop_stream_set_interval(&pressure->stream, interval); } static const struct iio_chan_spec apple_aop_pressure_channels[] = { @@ -267,28 +154,15 @@ static const struct apple_aop_calibration apple_aop_pressure_calibration[] = { static int apple_aop_pressure_buffer_postenable(struct iio_dev *indio_dev) { struct apple_aop_pressure *pressure = iio_priv(indio_dev); - int ret = 0; - mutex_lock(&pressure->lock); - pressure->buffer_active = true; - cancel_delayed_work(&pressure->idle_work); - if (!pressure->streaming) - ret = apple_aop_pressure_set_interval(pressure, pressure->interval_us); - if (ret) - pressure->buffer_active = false; - mutex_unlock(&pressure->lock); - return ret; + return apple_aop_stream_buffer_enable(&pressure->stream); } static int apple_aop_pressure_buffer_predisable(struct iio_dev *indio_dev) { struct apple_aop_pressure *pressure = iio_priv(indio_dev); - mutex_lock(&pressure->lock); - pressure->buffer_active = false; - if (pressure->streaming) - apple_aop_pressure_stop_streaming(pressure); - mutex_unlock(&pressure->lock); + apple_aop_stream_buffer_disable(&pressure->stream); return 0; } @@ -301,11 +175,7 @@ static void apple_aop_pressure_stop(void *data) { struct apple_aop_pressure *pressure = data; - cancel_delayed_work_sync(&pressure->idle_work); - mutex_lock(&pressure->lock); - if (pressure->streaming) - apple_aop_pressure_stop_streaming(pressure); - mutex_unlock(&pressure->lock); + apple_aop_stream_stop(&pressure->stream); apple_aop_unlisten(pressure->service); } @@ -326,11 +196,7 @@ static int apple_aop_pressure_probe(struct platform_device *pdev) pressure = iio_priv(indio_dev); pressure->dev = dev; pressure->service = service; - pressure->interval_us = AOP_PRESSURE_DEFAULT_US; - mutex_init(&pressure->lock); - spin_lock_init(&pressure->sample_lock); - init_completion(&pressure->sample_ready); - INIT_DELAYED_WORK(&pressure->idle_work, apple_aop_pressure_idle_work); + apple_aop_stream_init(&pressure->stream, service, AOP_PRESSURE_DEFAULT_US, false); indio_dev->name = "apple-aop-pressure"; indio_dev->info = &apple_aop_pressure_info; diff --git a/drivers/soc/apple/aop.c b/drivers/soc/apple/aop.c index 374246e668f695..bce6b88ab9fef6 100644 --- a/drivers/soc/apple/aop.c +++ b/drivers/soc/apple/aop.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -174,10 +175,16 @@ /* Service properties */ #define APPLE_AOP_PROPERTY_MAX 128 +#define APPLE_AOP_PROP_REPORT_INTERVAL 0x00 +#define APPLE_AOP_PROP_BATCH_INTERVAL 0x16 #define APPLE_AOP_PROP_CALIBRATION 0x3d +#define APPLE_AOP_PROP_RAW_INTERVAL 0x5c #define APPLE_AOP_CALIBRATION_PARTIAL 1 #define APPLE_AOP_CALIBRATION_COMPLETE 2 +#define APPLE_AOP_STREAM_WAIT_MIN_MS 500U +#define APPLE_AOP_STREAM_IDLE_MIN_MS 2000U + struct afk_qe_header { __le32 magic; __le32 size; @@ -1318,6 +1325,270 @@ int apple_aop_calibrate(const struct apple_aop_service *service, struct device * } EXPORT_SYMBOL_GPL(apple_aop_calibrate); +static int apple_aop_stream_set_u32(struct apple_aop_stream *stream, u32 key, u32 value) +{ + __le32 le = cpu_to_le32(value); + + return apple_aop_set_property(stream->service, key, &le, sizeof(le)); +} + +static unsigned int apple_aop_stream_wait_ms(u32 interval_us) +{ + return max(APPLE_AOP_STREAM_WAIT_MIN_MS, 2 * (interval_us / USEC_PER_MSEC)); +} + +static unsigned int apple_aop_stream_idle_ms(u32 interval_us) +{ + /* Leave a full second past two sample periods so 1 Hz reads do not race. */ + return max(APPLE_AOP_STREAM_IDLE_MIN_MS, + 2 * (interval_us / USEC_PER_MSEC) + 1000U); +} + +static void apple_aop_stream_arm_idle_locked(struct apple_aop_stream *stream) +{ + unsigned long delay = msecs_to_jiffies(apple_aop_stream_idle_ms(stream->interval_us)); + + stream->idle_deadline = jiffies + delay; + mod_delayed_work(system_dfl_wq, &stream->idle_work, delay); +} + +static int apple_aop_stream_start_locked(struct apple_aop_stream *stream, u32 interval_us) +{ + u32 prev_interval = stream->interval_us; + bool was_streaming = stream->streaming; + int ret; + + ret = apple_aop_stream_set_u32(stream, APPLE_AOP_PROP_BATCH_INTERVAL, interval_us); + if (ret) + return ret; + + ret = apple_aop_stream_set_u32(stream, APPLE_AOP_PROP_RAW_INTERVAL, + stream->raw_stream ? interval_us : 0); + if (ret) + goto restore_batch; + + ret = apple_aop_stream_set_u32(stream, APPLE_AOP_PROP_REPORT_INTERVAL, interval_us); + if (ret) + goto restore_raw; + + stream->interval_us = interval_us; + stream->streaming = true; + return 0; + +restore_raw: + if (was_streaming) + apple_aop_stream_set_u32(stream, APPLE_AOP_PROP_RAW_INTERVAL, + stream->raw_stream ? prev_interval : 0); +restore_batch: + if (was_streaming) + apple_aop_stream_set_u32(stream, APPLE_AOP_PROP_BATCH_INTERVAL, prev_interval); + return ret; +} + +static int apple_aop_stream_stop_locked(struct apple_aop_stream *stream) +{ + unsigned long flags; + int ret; + + ret = apple_aop_stream_set_u32(stream, APPLE_AOP_PROP_REPORT_INTERVAL, 0); + if (ret) + return ret; + + stream->streaming = false; + spin_lock_irqsave(&stream->sample_lock, flags); + stream->have_sample = false; + reinit_completion(&stream->sample_ready); + spin_unlock_irqrestore(&stream->sample_lock, flags); + return 0; +} + +static void apple_aop_stream_idle_work(struct work_struct *work) +{ + struct apple_aop_stream *stream = container_of(work, struct apple_aop_stream, + idle_work.work); + + mutex_lock(&stream->lock); + if (!stream->buffer_active && !stream->waiters && stream->streaming && + time_after_eq(jiffies, stream->idle_deadline)) + apple_aop_stream_stop_locked(stream); + mutex_unlock(&stream->lock); +} + +void apple_aop_stream_init(struct apple_aop_stream *stream, + const struct apple_aop_service *service, + u32 default_interval_us, bool raw_stream) +{ + stream->service = service; + stream->raw_stream = raw_stream; + stream->interval_us = default_interval_us; + stream->streaming = false; + stream->buffer_active = false; + stream->push_enabled = false; + stream->waiters = 0; + stream->idle_deadline = 0; + stream->have_sample = false; + mutex_init(&stream->lock); + mutex_init(&stream->push_lock); + spin_lock_init(&stream->sample_lock); + init_completion(&stream->sample_ready); + INIT_DELAYED_WORK(&stream->idle_work, apple_aop_stream_idle_work); +} +EXPORT_SYMBOL_GPL(apple_aop_stream_init); + +void apple_aop_stream_stop(struct apple_aop_stream *stream) +{ + cancel_delayed_work_sync(&stream->idle_work); + mutex_lock(&stream->lock); + mutex_lock(&stream->push_lock); + stream->push_enabled = false; + mutex_unlock(&stream->push_lock); + stream->buffer_active = false; + if (stream->streaming) + apple_aop_stream_stop_locked(stream); + mutex_unlock(&stream->lock); +} +EXPORT_SYMBOL_GPL(apple_aop_stream_stop); + +int apple_aop_stream_sample_get(struct apple_aop_stream *stream) +{ + unsigned long flags; + unsigned long start; + bool need_wait = false; + unsigned int wait_ms; + int ret = 0; + + mutex_lock(&stream->lock); + stream->waiters++; + cancel_delayed_work(&stream->idle_work); + wait_ms = apple_aop_stream_wait_ms(stream->interval_us); + if (!stream->streaming) { + spin_lock_irqsave(&stream->sample_lock, flags); + stream->have_sample = false; + reinit_completion(&stream->sample_ready); + spin_unlock_irqrestore(&stream->sample_lock, flags); + + ret = apple_aop_stream_start_locked(stream, stream->interval_us); + if (ret) + goto out_err; + need_wait = true; + } else { + spin_lock_irqsave(&stream->sample_lock, flags); + need_wait = !stream->have_sample; + spin_unlock_irqrestore(&stream->sample_lock, flags); + } + start = jiffies; + mutex_unlock(&stream->lock); + + if (need_wait) { + for (;;) { + unsigned long deadline = start + msecs_to_jiffies(wait_ms); + unsigned long timeout = time_before(jiffies, deadline) ? + deadline - jiffies : 0; + long rem; + + if (!timeout) { + ret = -EAGAIN; + break; + } + rem = wait_for_completion_interruptible_timeout(&stream->sample_ready, + timeout); + if (rem > 0 || completion_done(&stream->sample_ready)) + break; + if (rem < 0) { + ret = rem; + break; + } + mutex_lock(&stream->lock); + wait_ms = apple_aop_stream_wait_ms(stream->interval_us); + mutex_unlock(&stream->lock); + } + } + + if (!ret) + return 0; + + mutex_lock(&stream->lock); +out_err: + stream->waiters--; + if (!stream->buffer_active && !stream->waiters && stream->streaming) + apple_aop_stream_arm_idle_locked(stream); + mutex_unlock(&stream->lock); + return ret; +} +EXPORT_SYMBOL_GPL(apple_aop_stream_sample_get); + +void apple_aop_stream_sample_put(struct apple_aop_stream *stream) +{ + mutex_lock(&stream->lock); + stream->waiters--; + if (!stream->buffer_active && !stream->waiters && stream->streaming) + apple_aop_stream_arm_idle_locked(stream); + mutex_unlock(&stream->lock); +} +EXPORT_SYMBOL_GPL(apple_aop_stream_sample_put); + +int apple_aop_stream_buffer_enable(struct apple_aop_stream *stream) +{ + int ret = 0; + + mutex_lock(&stream->lock); + if (!stream->streaming) + ret = apple_aop_stream_start_locked(stream, stream->interval_us); + if (!ret) { + cancel_delayed_work(&stream->idle_work); + stream->buffer_active = true; + mutex_lock(&stream->push_lock); + stream->push_enabled = true; + mutex_unlock(&stream->push_lock); + } + mutex_unlock(&stream->lock); + return ret; +} +EXPORT_SYMBOL_GPL(apple_aop_stream_buffer_enable); + +void apple_aop_stream_buffer_disable(struct apple_aop_stream *stream) +{ + mutex_lock(&stream->lock); + mutex_lock(&stream->push_lock); + stream->push_enabled = false; + mutex_unlock(&stream->push_lock); + stream->buffer_active = false; + if (stream->streaming && !stream->waiters) { + if (apple_aop_stream_stop_locked(stream)) + apple_aop_stream_arm_idle_locked(stream); + } + mutex_unlock(&stream->lock); +} +EXPORT_SYMBOL_GPL(apple_aop_stream_buffer_disable); + +u32 apple_aop_stream_get_interval(struct apple_aop_stream *stream) +{ + u32 interval_us; + + mutex_lock(&stream->lock); + interval_us = stream->interval_us; + mutex_unlock(&stream->lock); + return interval_us; +} +EXPORT_SYMBOL_GPL(apple_aop_stream_get_interval); + +int apple_aop_stream_set_interval(struct apple_aop_stream *stream, u32 interval_us) +{ + int ret = 0; + + mutex_lock(&stream->lock); + if (stream->streaming) { + ret = apple_aop_stream_start_locked(stream, interval_us); + if (!ret && !stream->buffer_active && !stream->waiters) + apple_aop_stream_arm_idle_locked(stream); + } else { + stream->interval_us = interval_us; + } + mutex_unlock(&stream->lock); + return ret; +} +EXPORT_SYMBOL_GPL(apple_aop_stream_set_interval); + /* Driver */ static void apple_aop_free_buffers(struct apple_aop *aop) diff --git a/include/linux/soc/apple/aop.h b/include/linux/soc/apple/aop.h index a44f4c79272ce2..ff49e92efc91c0 100644 --- a/include/linux/soc/apple/aop.h +++ b/include/linux/soc/apple/aop.h @@ -5,7 +5,11 @@ #ifndef _LINUX_SOC_APPLE_AOP_H_ #define _LINUX_SOC_APPLE_AOP_H_ +#include +#include +#include #include +#include struct device; @@ -77,4 +81,61 @@ struct apple_aop_calibration { int apple_aop_calibrate(const struct apple_aop_service *service, struct device *dev, const struct apple_aop_calibration *cal, unsigned int count); +/* + * On-demand report streaming state shared by the AOP IIO sensor drivers. + * The sensor stays silent until a sysfs read or an enabled IIO buffer asks + * for samples, and stops again once the buffer is disabled or idle. + */ +struct apple_aop_stream { + const struct apple_aop_service *service; + bool raw_stream; + + struct mutex lock; /* interval_us, streaming, buffer_active, waiters, idle_deadline */ + u32 interval_us; + bool streaming; + bool buffer_active; + unsigned int waiters; + unsigned long idle_deadline; + struct delayed_work idle_work; + + struct mutex push_lock; /* serializes push_enabled with IIO buffer delivery */ + bool push_enabled; + + spinlock_t sample_lock; /* have_sample, plus driver's sample fields */ + bool have_sample; + struct completion sample_ready; +}; + +void apple_aop_stream_init(struct apple_aop_stream *stream, + const struct apple_aop_service *service, + u32 default_interval_us, bool raw_stream); +void apple_aop_stream_stop(struct apple_aop_stream *stream); +int apple_aop_stream_sample_get(struct apple_aop_stream *stream); +void apple_aop_stream_sample_put(struct apple_aop_stream *stream); +int apple_aop_stream_buffer_enable(struct apple_aop_stream *stream); +void apple_aop_stream_buffer_disable(struct apple_aop_stream *stream); +u32 apple_aop_stream_get_interval(struct apple_aop_stream *stream); +int apple_aop_stream_set_interval(struct apple_aop_stream *stream, u32 interval_us); + +/* Caller holds stream->sample_lock after updating its sample fields. */ +static inline void apple_aop_stream_sample_ready(struct apple_aop_stream *stream) +{ + stream->have_sample = true; + complete_all(&stream->sample_ready); +} + +static inline bool apple_aop_stream_push_begin(struct apple_aop_stream *stream) +{ + mutex_lock(&stream->push_lock); + if (stream->push_enabled) + return true; + mutex_unlock(&stream->push_lock); + return false; +} + +static inline void apple_aop_stream_push_end(struct apple_aop_stream *stream) +{ + mutex_unlock(&stream->push_lock); +} + #endif /* _LINUX_SOC_APPLE_AOP_H_ */