1 /*
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/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"
24 
25 #define SSP_CHANNEL_COUNT 3
26 
27 #define SSP_GYROSCOPE_NAME "ssp-gyroscope"
28 static const char ssp_gyro_name[] = SSP_GYROSCOPE_NAME;
29 
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,
35 };
36 
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)
40 {
41 	u32 t;
42 	struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
43 
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;
51 	}
52 
53 	return -EINVAL;
54 }
55 
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)
59 {
60 	int ret;
61 	struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
62 
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");
69 
70 		return ret;
71 	default:
72 		break;
73 	}
74 
75 	return -EINVAL;
76 }
77 
78 static const struct iio_info ssp_gyro_iio_info = {
79 	.read_raw = &ssp_gyro_read_raw,
80 	.write_raw = &ssp_gyro_write_raw,
81 };
82 
83 static const unsigned long ssp_gyro_scan_mask[] = { 0x07, 0, };
84 
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),
90 };
91 
92 static int ssp_process_gyro_data(struct iio_dev *indio_dev, void *buf,
93 				 int64_t timestamp)
94 {
95 	return ssp_common_process_data(indio_dev, buf, SSP_GYROSCOPE_SIZE,
96 				       timestamp);
97 }
98 
99 static const struct iio_buffer_setup_ops ssp_gyro_buffer_ops = {
100 	.postenable = &ssp_common_buffer_postenable,
101 	.postdisable = &ssp_common_buffer_postdisable,
102 };
103 
104 static int ssp_gyro_probe(struct platform_device *pdev)
105 {
106 	int ret;
107 	struct iio_dev *indio_dev;
108 	struct ssp_sensor_data *spd;
109 	struct iio_buffer *buffer;
110 
111 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*spd));
112 	if (!indio_dev)
113 		return -ENOMEM;
114 
115 	spd = iio_priv(indio_dev);
116 
117 	spd->process_data = ssp_process_gyro_data;
118 	spd->type = SSP_GYROSCOPE_SENSOR;
119 
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;
127 
128 	buffer = devm_iio_kfifo_allocate(&pdev->dev);
129 	if (!buffer)
130 		return -ENOMEM;
131 
132 	iio_device_attach_buffer(indio_dev, buffer);
133 
134 	indio_dev->setup_ops = &ssp_gyro_buffer_ops;
135 
136 	platform_set_drvdata(pdev, indio_dev);
137 
138 	ret = devm_iio_device_register(&pdev->dev, indio_dev);
139 	if (ret < 0)
140 		return ret;
141 
142 	/* ssp registering should be done after all iio setup */
143 	ssp_register_consumer(indio_dev, SSP_GYROSCOPE_SENSOR);
144 
145 	return 0;
146 }
147 
148 static struct platform_driver ssp_gyro_driver = {
149 	.driver = {
150 		.name = SSP_GYROSCOPE_NAME,
151 	},
152 	.probe = ssp_gyro_probe,
153 };
154 
155 module_platform_driver(ssp_gyro_driver);
156 
157 MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
158 MODULE_DESCRIPTION("Samsung sensorhub gyroscopes driver");
159 MODULE_LICENSE("GPL");
160