2 * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/iio/common/ssp_sensors.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/kfifo_buf.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include "../common/ssp_sensors/ssp_iio_sensor.h"
24 #define SSP_CHANNEL_COUNT 3
26 #define SSP_GYROSCOPE_NAME "ssp-gyroscope"
27 static const char ssp_gyro_name[] = SSP_GYROSCOPE_NAME;
29 enum ssp_gyro_3d_channel {
30 SSP_CHANNEL_SCAN_INDEX_X,
31 SSP_CHANNEL_SCAN_INDEX_Y,
32 SSP_CHANNEL_SCAN_INDEX_Z,
33 SSP_CHANNEL_SCAN_INDEX_TIME,
36 static int ssp_gyro_read_raw(struct iio_dev *indio_dev,
37 struct iio_chan_spec const *chan, int *val,
41 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
44 case IIO_CHAN_INFO_SAMP_FREQ:
45 t = ssp_get_sensor_delay(data, SSP_GYROSCOPE_SENSOR);
46 ssp_convert_to_freq(t, val, val2);
47 return IIO_VAL_INT_PLUS_MICRO;
55 static int ssp_gyro_write_raw(struct iio_dev *indio_dev,
56 struct iio_chan_spec const *chan, int val,
60 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
63 case IIO_CHAN_INFO_SAMP_FREQ:
64 ret = ssp_convert_to_time(val, val2);
65 ret = ssp_change_delay(data, SSP_GYROSCOPE_SENSOR, ret);
67 dev_err(&indio_dev->dev, "gyro sensor enable fail\n");
77 static const struct iio_info ssp_gyro_iio_info = {
78 .read_raw = &ssp_gyro_read_raw,
79 .write_raw = &ssp_gyro_write_raw,
82 static const unsigned long ssp_gyro_scan_mask[] = { 0x07, 0, };
84 static const struct iio_chan_spec ssp_gyro_channels[] = {
85 SSP_CHANNEL_AG(IIO_ANGL_VEL, IIO_MOD_X, SSP_CHANNEL_SCAN_INDEX_X),
86 SSP_CHANNEL_AG(IIO_ANGL_VEL, IIO_MOD_Y, SSP_CHANNEL_SCAN_INDEX_Y),
87 SSP_CHANNEL_AG(IIO_ANGL_VEL, IIO_MOD_Z, SSP_CHANNEL_SCAN_INDEX_Z),
88 SSP_CHAN_TIMESTAMP(SSP_CHANNEL_SCAN_INDEX_TIME),
91 static int ssp_process_gyro_data(struct iio_dev *indio_dev, void *buf,
94 return ssp_common_process_data(indio_dev, buf, SSP_GYROSCOPE_SIZE,
98 static const struct iio_buffer_setup_ops ssp_gyro_buffer_ops = {
99 .postenable = &ssp_common_buffer_postenable,
100 .postdisable = &ssp_common_buffer_postdisable,
103 static int ssp_gyro_probe(struct platform_device *pdev)
106 struct iio_dev *indio_dev;
107 struct ssp_sensor_data *spd;
108 struct iio_buffer *buffer;
110 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*spd));
114 spd = iio_priv(indio_dev);
116 spd->process_data = ssp_process_gyro_data;
117 spd->type = SSP_GYROSCOPE_SENSOR;
119 indio_dev->name = ssp_gyro_name;
120 indio_dev->dev.parent = &pdev->dev;
121 indio_dev->info = &ssp_gyro_iio_info;
122 indio_dev->modes = INDIO_BUFFER_SOFTWARE;
123 indio_dev->channels = ssp_gyro_channels;
124 indio_dev->num_channels = ARRAY_SIZE(ssp_gyro_channels);
125 indio_dev->available_scan_masks = ssp_gyro_scan_mask;
127 buffer = devm_iio_kfifo_allocate(&pdev->dev);
131 iio_device_attach_buffer(indio_dev, buffer);
133 indio_dev->setup_ops = &ssp_gyro_buffer_ops;
135 platform_set_drvdata(pdev, indio_dev);
137 ret = iio_device_register(indio_dev);
141 /* ssp registering should be done after all iio setup */
142 ssp_register_consumer(indio_dev, SSP_GYROSCOPE_SENSOR);
147 static int ssp_gyro_remove(struct platform_device *pdev)
149 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
151 iio_device_unregister(indio_dev);
156 static struct platform_driver ssp_gyro_driver = {
158 .name = SSP_GYROSCOPE_NAME,
160 .probe = ssp_gyro_probe,
161 .remove = ssp_gyro_remove,
164 module_platform_driver(ssp_gyro_driver);
166 MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
167 MODULE_DESCRIPTION("Samsung sensorhub gyroscopes driver");
168 MODULE_LICENSE("GPL");