dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / iio / gyro / ssp_gyro_sensor.c
blob2dacd8e01045382a38c0d7ddb1878ca0953d69bb
1 /*
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/buffer.h>
19 #include <linux/iio/kfifo_buf.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include "../common/ssp_sensors/ssp_iio_sensor.h"
25 #define SSP_CHANNEL_COUNT 3
27 #define SSP_GYROSCOPE_NAME "ssp-gyroscope"
28 static const char ssp_gyro_name[] = SSP_GYROSCOPE_NAME;
30 enum ssp_gyro_3d_channel {
31 SSP_CHANNEL_SCAN_INDEX_X,
32 SSP_CHANNEL_SCAN_INDEX_Y,
33 SSP_CHANNEL_SCAN_INDEX_Z,
34 SSP_CHANNEL_SCAN_INDEX_TIME,
37 static int ssp_gyro_read_raw(struct iio_dev *indio_dev,
38 struct iio_chan_spec const *chan, int *val,
39 int *val2, long mask)
41 u32 t;
42 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
44 switch (mask) {
45 case IIO_CHAN_INFO_SAMP_FREQ:
46 t = ssp_get_sensor_delay(data, SSP_GYROSCOPE_SENSOR);
47 ssp_convert_to_freq(t, val, val2);
48 return IIO_VAL_INT_PLUS_MICRO;
49 default:
50 break;
53 return -EINVAL;
56 static int ssp_gyro_write_raw(struct iio_dev *indio_dev,
57 struct iio_chan_spec const *chan, int val,
58 int val2, long mask)
60 int ret;
61 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
63 switch (mask) {
64 case IIO_CHAN_INFO_SAMP_FREQ:
65 ret = ssp_convert_to_time(val, val2);
66 ret = ssp_change_delay(data, SSP_GYROSCOPE_SENSOR, ret);
67 if (ret < 0)
68 dev_err(&indio_dev->dev, "gyro sensor enable fail\n");
70 return ret;
71 default:
72 break;
75 return -EINVAL;
78 static const struct iio_info ssp_gyro_iio_info = {
79 .read_raw = &ssp_gyro_read_raw,
80 .write_raw = &ssp_gyro_write_raw,
83 static const unsigned long ssp_gyro_scan_mask[] = { 0x07, 0, };
85 static const struct iio_chan_spec ssp_gyro_channels[] = {
86 SSP_CHANNEL_AG(IIO_ANGL_VEL, IIO_MOD_X, SSP_CHANNEL_SCAN_INDEX_X),
87 SSP_CHANNEL_AG(IIO_ANGL_VEL, IIO_MOD_Y, SSP_CHANNEL_SCAN_INDEX_Y),
88 SSP_CHANNEL_AG(IIO_ANGL_VEL, IIO_MOD_Z, SSP_CHANNEL_SCAN_INDEX_Z),
89 SSP_CHAN_TIMESTAMP(SSP_CHANNEL_SCAN_INDEX_TIME),
92 static int ssp_process_gyro_data(struct iio_dev *indio_dev, void *buf,
93 int64_t timestamp)
95 return ssp_common_process_data(indio_dev, buf, SSP_GYROSCOPE_SIZE,
96 timestamp);
99 static const struct iio_buffer_setup_ops ssp_gyro_buffer_ops = {
100 .postenable = &ssp_common_buffer_postenable,
101 .postdisable = &ssp_common_buffer_postdisable,
104 static int ssp_gyro_probe(struct platform_device *pdev)
106 int ret;
107 struct iio_dev *indio_dev;
108 struct ssp_sensor_data *spd;
109 struct iio_buffer *buffer;
111 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*spd));
112 if (!indio_dev)
113 return -ENOMEM;
115 spd = iio_priv(indio_dev);
117 spd->process_data = ssp_process_gyro_data;
118 spd->type = SSP_GYROSCOPE_SENSOR;
120 indio_dev->name = ssp_gyro_name;
121 indio_dev->dev.parent = &pdev->dev;
122 indio_dev->info = &ssp_gyro_iio_info;
123 indio_dev->modes = INDIO_BUFFER_SOFTWARE;
124 indio_dev->channels = ssp_gyro_channels;
125 indio_dev->num_channels = ARRAY_SIZE(ssp_gyro_channels);
126 indio_dev->available_scan_masks = ssp_gyro_scan_mask;
128 buffer = devm_iio_kfifo_allocate(&pdev->dev);
129 if (!buffer)
130 return -ENOMEM;
132 iio_device_attach_buffer(indio_dev, buffer);
134 indio_dev->setup_ops = &ssp_gyro_buffer_ops;
136 platform_set_drvdata(pdev, indio_dev);
138 ret = devm_iio_device_register(&pdev->dev, indio_dev);
139 if (ret < 0)
140 return ret;
142 /* ssp registering should be done after all iio setup */
143 ssp_register_consumer(indio_dev, SSP_GYROSCOPE_SENSOR);
145 return 0;
148 static struct platform_driver ssp_gyro_driver = {
149 .driver = {
150 .name = SSP_GYROSCOPE_NAME,
152 .probe = ssp_gyro_probe,
155 module_platform_driver(ssp_gyro_driver);
157 MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
158 MODULE_DESCRIPTION("Samsung sensorhub gyroscopes driver");
159 MODULE_LICENSE("GPL");