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/kfifo_buf.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include "ssp_iio_sensor.h"
21 
22 /**
23  * ssp_common_buffer_postenable() - generic postenable callback for ssp buffer
24  *
25  * @indio_dev:		iio device
26  *
27  * Returns 0 or negative value in case of error
28  */
29 int ssp_common_buffer_postenable(struct iio_dev *indio_dev)
30 {
31 	struct ssp_sensor_data *spd = iio_priv(indio_dev);
32 	struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
33 
34 	/* the allocation is made in post because scan size is known in this
35 	 * moment
36 	 * */
37 	spd->buffer = kmalloc(indio_dev->scan_bytes, GFP_KERNEL | GFP_DMA);
38 	if (!spd->buffer)
39 		return -ENOMEM;
40 
41 	return ssp_enable_sensor(data, spd->type,
42 				 ssp_get_sensor_delay(data, spd->type));
43 }
44 EXPORT_SYMBOL(ssp_common_buffer_postenable);
45 
46 /**
47  * ssp_common_buffer_postdisable() - generic postdisable callback for ssp buffer
48  *
49  * @indio_dev:		iio device
50  *
51  * Returns 0 or negative value in case of error
52  */
53 int ssp_common_buffer_postdisable(struct iio_dev *indio_dev)
54 {
55 	int ret;
56 	struct ssp_sensor_data *spd = iio_priv(indio_dev);
57 	struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
58 
59 	ret = ssp_disable_sensor(data, spd->type);
60 	if (ret < 0)
61 		return ret;
62 
63 	kfree(spd->buffer);
64 
65 	return ret;
66 }
67 EXPORT_SYMBOL(ssp_common_buffer_postdisable);
68 
69 /**
70  * ssp_common_process_data() - Common process data callback for ssp sensors
71  *
72  * @indio_dev:		iio device
73  * @buf:		source buffer
74  * @len:		sensor data length
75  * @timestamp:		system timestamp
76  *
77  * Returns 0 or negative value in case of error
78  */
79 int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
80 			    unsigned int len, int64_t timestamp)
81 {
82 	__le32 time;
83 	int64_t calculated_time;
84 	struct ssp_sensor_data *spd = iio_priv(indio_dev);
85 
86 	if (indio_dev->scan_bytes == 0)
87 		return 0;
88 
89 	/*
90 	 * it always sends full set of samples, remember about available masks
91 	 */
92 	memcpy(spd->buffer, buf, len);
93 
94 	if (indio_dev->scan_timestamp) {
95 		memcpy(&time, &((char *)buf)[len], SSP_TIME_SIZE);
96 		calculated_time =
97 			timestamp + (int64_t)le32_to_cpu(time) * 1000000;
98 	}
99 
100 	return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,
101 						  calculated_time);
102 }
103 EXPORT_SYMBOL(ssp_common_process_data);
104 
105 MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
106 MODULE_DESCRIPTION("Samsung sensorhub commons");
107 MODULE_LICENSE("GPL");
108