xref: /linux/drivers/iio/accel/ssp_accel_sensor.c (revision e0bf6c5ca2d3281f231c5f0c9bf145e9513644de)
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/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_ACCEL_NAME "ssp-accelerometer"
27 static const char ssp_accel_device_name[] = SSP_ACCEL_NAME;
28 
29 enum ssp_accel_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 
36 static int ssp_accel_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_ACCELEROMETER_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 
55 static int ssp_accel_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_ACCELEROMETER_SENSOR, ret);
66 		if (ret < 0)
67 			dev_err(&indio_dev->dev, "accel sensor enable fail\n");
68 
69 		return ret;
70 	default:
71 		break;
72 	}
73 
74 	return -EINVAL;
75 }
76 
77 static struct iio_info ssp_accel_iio_info = {
78 	.read_raw = &ssp_accel_read_raw,
79 	.write_raw = &ssp_accel_write_raw,
80 };
81 
82 static const unsigned long ssp_accel_scan_mask[] = { 0x7, 0, };
83 
84 static const struct iio_chan_spec ssp_acc_channels[] = {
85 	SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_X, SSP_CHANNEL_SCAN_INDEX_X),
86 	SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_Y, SSP_CHANNEL_SCAN_INDEX_Y),
87 	SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_Z, SSP_CHANNEL_SCAN_INDEX_Z),
88 	SSP_CHAN_TIMESTAMP(SSP_CHANNEL_SCAN_INDEX_TIME),
89 };
90 
91 static int ssp_process_accel_data(struct iio_dev *indio_dev, void *buf,
92 				  int64_t timestamp)
93 {
94 	return ssp_common_process_data(indio_dev, buf, SSP_ACCELEROMETER_SIZE,
95 				       timestamp);
96 }
97 
98 static const struct iio_buffer_setup_ops ssp_accel_buffer_ops = {
99 	.postenable = &ssp_common_buffer_postenable,
100 	.postdisable = &ssp_common_buffer_postdisable,
101 };
102 
103 static int ssp_accel_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_accel_data;
117 	spd->type = SSP_ACCELEROMETER_SENSOR;
118 
119 	indio_dev->name = ssp_accel_device_name;
120 	indio_dev->dev.parent = &pdev->dev;
121 	indio_dev->dev.of_node = pdev->dev.of_node;
122 	indio_dev->info = &ssp_accel_iio_info;
123 	indio_dev->modes = INDIO_BUFFER_SOFTWARE;
124 	indio_dev->channels = ssp_acc_channels;
125 	indio_dev->num_channels = ARRAY_SIZE(ssp_acc_channels);
126 	indio_dev->available_scan_masks = ssp_accel_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_accel_buffer_ops;
135 
136 	platform_set_drvdata(pdev, indio_dev);
137 
138 	ret = iio_device_register(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_ACCELEROMETER_SENSOR);
144 
145 	return 0;
146 }
147 
148 static int ssp_accel_remove(struct platform_device *pdev)
149 {
150 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
151 
152 	iio_device_unregister(indio_dev);
153 
154 	return 0;
155 }
156 
157 static struct platform_driver ssp_accel_driver = {
158 	.driver = {
159 		.name = SSP_ACCEL_NAME,
160 	},
161 	.probe = ssp_accel_probe,
162 	.remove =  ssp_accel_remove,
163 };
164 
165 module_platform_driver(ssp_accel_driver);
166 
167 MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
168 MODULE_DESCRIPTION("Samsung sensorhub accelerometers driver");
169 MODULE_LICENSE("GPL");
170