Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 26 additions & 161 deletions drivers/iio/accel/apple_aop_accel.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,16 @@
* in the sensor's frame; the board's mount-matrix maps it.
*/

#include <linux/completion.h>
#include <linux/iio/buffer.h>
#include <linux/iio/iio.h>
#include <linux/iio/kfifo_buf.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/soc/apple/aop.h>
#include <linux/spinlock.h>
#include <linux/unaligned.h>
#include <linux/workqueue.h>

#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
Expand All @@ -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
Expand All @@ -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);
}
}

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 *
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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)
Expand Down
Loading
Loading