1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
6 #include <linux/iio/common/ssp_sensors.h>
7 #include <linux/iio/iio.h>
8 #include <linux/iio/buffer.h>
9 #include <linux/iio/kfifo_buf.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include "../common/ssp_sensors/ssp_iio_sensor.h"
15 #define SSP_CHANNEL_COUNT 3
17 #define SSP_GYROSCOPE_NAME "ssp-gyroscope"
18 static const char ssp_gyro_name
[] = SSP_GYROSCOPE_NAME
;
20 enum ssp_gyro_3d_channel
{
21 SSP_CHANNEL_SCAN_INDEX_X
,
22 SSP_CHANNEL_SCAN_INDEX_Y
,
23 SSP_CHANNEL_SCAN_INDEX_Z
,
24 SSP_CHANNEL_SCAN_INDEX_TIME
,
27 static int ssp_gyro_read_raw(struct iio_dev
*indio_dev
,
28 struct iio_chan_spec
const *chan
, int *val
,
32 struct ssp_data
*data
= dev_get_drvdata(indio_dev
->dev
.parent
->parent
);
35 case IIO_CHAN_INFO_SAMP_FREQ
:
36 t
= ssp_get_sensor_delay(data
, SSP_GYROSCOPE_SENSOR
);
37 ssp_convert_to_freq(t
, val
, val2
);
38 return IIO_VAL_INT_PLUS_MICRO
;
46 static int ssp_gyro_write_raw(struct iio_dev
*indio_dev
,
47 struct iio_chan_spec
const *chan
, int val
,
51 struct ssp_data
*data
= dev_get_drvdata(indio_dev
->dev
.parent
->parent
);
54 case IIO_CHAN_INFO_SAMP_FREQ
:
55 ret
= ssp_convert_to_time(val
, val2
);
56 ret
= ssp_change_delay(data
, SSP_GYROSCOPE_SENSOR
, ret
);
58 dev_err(&indio_dev
->dev
, "gyro sensor enable fail\n");
68 static const struct iio_info ssp_gyro_iio_info
= {
69 .read_raw
= &ssp_gyro_read_raw
,
70 .write_raw
= &ssp_gyro_write_raw
,
73 static const unsigned long ssp_gyro_scan_mask
[] = { 0x07, 0, };
75 static const struct iio_chan_spec ssp_gyro_channels
[] = {
76 SSP_CHANNEL_AG(IIO_ANGL_VEL
, IIO_MOD_X
, SSP_CHANNEL_SCAN_INDEX_X
),
77 SSP_CHANNEL_AG(IIO_ANGL_VEL
, IIO_MOD_Y
, SSP_CHANNEL_SCAN_INDEX_Y
),
78 SSP_CHANNEL_AG(IIO_ANGL_VEL
, IIO_MOD_Z
, SSP_CHANNEL_SCAN_INDEX_Z
),
79 SSP_CHAN_TIMESTAMP(SSP_CHANNEL_SCAN_INDEX_TIME
),
82 static int ssp_process_gyro_data(struct iio_dev
*indio_dev
, void *buf
,
85 return ssp_common_process_data(indio_dev
, buf
, SSP_GYROSCOPE_SIZE
,
89 static const struct iio_buffer_setup_ops ssp_gyro_buffer_ops
= {
90 .postenable
= &ssp_common_buffer_postenable
,
91 .postdisable
= &ssp_common_buffer_postdisable
,
94 static int ssp_gyro_probe(struct platform_device
*pdev
)
97 struct iio_dev
*indio_dev
;
98 struct ssp_sensor_data
*spd
;
100 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*spd
));
104 spd
= iio_priv(indio_dev
);
106 spd
->process_data
= ssp_process_gyro_data
;
107 spd
->type
= SSP_GYROSCOPE_SENSOR
;
109 indio_dev
->name
= ssp_gyro_name
;
110 indio_dev
->info
= &ssp_gyro_iio_info
;
111 indio_dev
->channels
= ssp_gyro_channels
;
112 indio_dev
->num_channels
= ARRAY_SIZE(ssp_gyro_channels
);
113 indio_dev
->available_scan_masks
= ssp_gyro_scan_mask
;
115 ret
= devm_iio_kfifo_buffer_setup(&pdev
->dev
, indio_dev
,
116 &ssp_gyro_buffer_ops
);
120 platform_set_drvdata(pdev
, indio_dev
);
122 ret
= devm_iio_device_register(&pdev
->dev
, indio_dev
);
126 /* ssp registering should be done after all iio setup */
127 ssp_register_consumer(indio_dev
, SSP_GYROSCOPE_SENSOR
);
132 static struct platform_driver ssp_gyro_driver
= {
134 .name
= SSP_GYROSCOPE_NAME
,
136 .probe
= ssp_gyro_probe
,
139 module_platform_driver(ssp_gyro_driver
);
141 MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
142 MODULE_DESCRIPTION("Samsung sensorhub gyroscopes driver");
143 MODULE_LICENSE("GPL");
144 MODULE_IMPORT_NS("IIO_SSP_SENSORS");