blob: 1f25f406c545ffe93bf03222f8f9636e0dc52f77 [file] [log] [blame]
Karol Wronaa9afcaa2015-01-28 15:05:54 +01001/*
2 * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
3 *
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.
8 *
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.
13 *
14 */
15
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"
23
24#define SSP_CHANNEL_COUNT 3
25
26#define SSP_GYROSCOPE_NAME "ssp-gyroscope"
27static const char ssp_gyro_name[] = SSP_GYROSCOPE_NAME;
28
29enum 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,
34};
35
36static int ssp_gyro_read_raw(struct iio_dev *indio_dev,
37 struct iio_chan_spec const *chan, int *val,
38 int *val2, long mask)
39{
40 u32 t;
41 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
42
43 switch (mask) {
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;
48 default:
49 break;
50 }
51
52 return -EINVAL;
53}
54
55static int ssp_gyro_write_raw(struct iio_dev *indio_dev,
56 struct iio_chan_spec const *chan, int val,
57 int val2, long mask)
58{
59 int ret;
60 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
61
62 switch (mask) {
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);
66 if (ret < 0)
67 dev_err(&indio_dev->dev, "gyro sensor enable fail\n");
68
69 return ret;
70 default:
71 break;
72 }
73
74 return -EINVAL;
75}
76
Julia Lawallbecf05e72016-09-16 12:55:26 +020077static const struct iio_info ssp_gyro_iio_info = {
Karol Wronaa9afcaa2015-01-28 15:05:54 +010078 .read_raw = &ssp_gyro_read_raw,
79 .write_raw = &ssp_gyro_write_raw,
80};
81
82static const unsigned long ssp_gyro_scan_mask[] = { 0x07, 0, };
83
84static 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),
89};
90
91static int ssp_process_gyro_data(struct iio_dev *indio_dev, void *buf,
92 int64_t timestamp)
93{
94 return ssp_common_process_data(indio_dev, buf, SSP_GYROSCOPE_SIZE,
95 timestamp);
96}
97
98static const struct iio_buffer_setup_ops ssp_gyro_buffer_ops = {
99 .postenable = &ssp_common_buffer_postenable,
100 .postdisable = &ssp_common_buffer_postdisable,
101};
102
103static int ssp_gyro_probe(struct platform_device *pdev)
104{
105 int ret;
106 struct iio_dev *indio_dev;
107 struct ssp_sensor_data *spd;
108 struct iio_buffer *buffer;
109
110 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*spd));
111 if (!indio_dev)
112 return -ENOMEM;
113
114 spd = iio_priv(indio_dev);
115
116 spd->process_data = ssp_process_gyro_data;
117 spd->type = SSP_GYROSCOPE_SENSOR;
118
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;
126
127 buffer = devm_iio_kfifo_allocate(&pdev->dev);
128 if (!buffer)
129 return -ENOMEM;
130
131 iio_device_attach_buffer(indio_dev, buffer);
132
133 indio_dev->setup_ops = &ssp_gyro_buffer_ops;
134
135 platform_set_drvdata(pdev, indio_dev);
136
137 ret = iio_device_register(indio_dev);
138 if (ret < 0)
139 return ret;
140
141 /* ssp registering should be done after all iio setup */
142 ssp_register_consumer(indio_dev, SSP_GYROSCOPE_SENSOR);
143
144 return 0;
145}
146
147static int ssp_gyro_remove(struct platform_device *pdev)
148{
149 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
150
151 iio_device_unregister(indio_dev);
152
153 return 0;
154}
155
156static struct platform_driver ssp_gyro_driver = {
157 .driver = {
158 .name = SSP_GYROSCOPE_NAME,
159 },
160 .probe = ssp_gyro_probe,
161 .remove = ssp_gyro_remove,
162};
163
164module_platform_driver(ssp_gyro_driver);
165
166MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
167MODULE_DESCRIPTION("Samsung sensorhub gyroscopes driver");
168MODULE_LICENSE("GPL");