Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * HID Sensors Driver
4 * Copyright (c) 2014, Intel Corporation.
5 */
6
7#include <linux/device.h>
8#include <linux/platform_device.h>
9#include <linux/module.h>
10#include <linux/interrupt.h>
11#include <linux/irq.h>
12#include <linux/slab.h>
13#include <linux/hid-sensor-hub.h>
14#include <linux/iio/iio.h>
15#include <linux/iio/sysfs.h>
16#include <linux/iio/buffer.h>
17#include <linux/iio/trigger_consumer.h>
18#include <linux/iio/triggered_buffer.h>
19#include "../common/hid-sensors/hid-sensor-trigger.h"
20
21struct dev_rot_state {
22 struct hid_sensor_hub_callbacks callbacks;
23 struct hid_sensor_common common_attributes;
24 struct hid_sensor_hub_attribute_info quaternion;
25 u32 sampled_vals[4];
26 int scale_pre_decml;
27 int scale_post_decml;
28 int scale_precision;
29 int value_offset;
30};
31
32/* Channel definitions */
33static const struct iio_chan_spec dev_rot_channels[] = {
34 {
35 .type = IIO_ROT,
36 .modified = 1,
37 .channel2 = IIO_MOD_QUATERNION,
38 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
39 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
40 BIT(IIO_CHAN_INFO_OFFSET) |
41 BIT(IIO_CHAN_INFO_SCALE) |
42 BIT(IIO_CHAN_INFO_HYSTERESIS)
43 }
44};
45
46/* Adjust channel real bits based on report descriptor */
47static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan,
48 int size)
49{
50 chan->scan_type.sign = 's';
51 /* Real storage bits will change based on the report desc. */
52 chan->scan_type.realbits = size * 8;
53 /* Maximum size of a sample to capture is u32 */
54 chan->scan_type.storagebits = sizeof(u32) * 8;
55 chan->scan_type.repeat = 4;
56}
57
58/* Channel read_raw handler */
59static int dev_rot_read_raw(struct iio_dev *indio_dev,
60 struct iio_chan_spec const *chan,
61 int size, int *vals, int *val_len,
62 long mask)
63{
64 struct dev_rot_state *rot_state = iio_priv(indio_dev);
65 int ret_type;
66 int i;
67
68 vals[0] = 0;
69 vals[1] = 0;
70
71 switch (mask) {
72 case IIO_CHAN_INFO_RAW:
73 if (size >= 4) {
74 for (i = 0; i < 4; ++i)
75 vals[i] = rot_state->sampled_vals[i];
76 ret_type = IIO_VAL_INT_MULTIPLE;
77 *val_len = 4;
78 } else
79 ret_type = -EINVAL;
80 break;
81 case IIO_CHAN_INFO_SCALE:
82 vals[0] = rot_state->scale_pre_decml;
83 vals[1] = rot_state->scale_post_decml;
84 return rot_state->scale_precision;
85
86 case IIO_CHAN_INFO_OFFSET:
87 *vals = rot_state->value_offset;
88 return IIO_VAL_INT;
89
90 case IIO_CHAN_INFO_SAMP_FREQ:
91 ret_type = hid_sensor_read_samp_freq_value(
92 &rot_state->common_attributes, &vals[0], &vals[1]);
93 break;
94 case IIO_CHAN_INFO_HYSTERESIS:
95 ret_type = hid_sensor_read_raw_hyst_value(
96 &rot_state->common_attributes, &vals[0], &vals[1]);
97 break;
98 default:
99 ret_type = -EINVAL;
100 break;
101 }
102
103 return ret_type;
104}
105
106/* Channel write_raw handler */
107static int dev_rot_write_raw(struct iio_dev *indio_dev,
108 struct iio_chan_spec const *chan,
109 int val,
110 int val2,
111 long mask)
112{
113 struct dev_rot_state *rot_state = iio_priv(indio_dev);
114 int ret;
115
116 switch (mask) {
117 case IIO_CHAN_INFO_SAMP_FREQ:
118 ret = hid_sensor_write_samp_freq_value(
119 &rot_state->common_attributes, val, val2);
120 break;
121 case IIO_CHAN_INFO_HYSTERESIS:
122 ret = hid_sensor_write_raw_hyst_value(
123 &rot_state->common_attributes, val, val2);
124 break;
125 default:
126 ret = -EINVAL;
127 }
128
129 return ret;
130}
131
132static const struct iio_info dev_rot_info = {
133 .read_raw_multi = &dev_rot_read_raw,
134 .write_raw = &dev_rot_write_raw,
135};
136
137/* Function to push data to buffer */
138static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
139{
140 dev_dbg(&indio_dev->dev, "hid_sensor_push_data >>\n");
141 iio_push_to_buffers(indio_dev, (u8 *)data);
142 dev_dbg(&indio_dev->dev, "hid_sensor_push_data <<\n");
143
144}
145
146/* Callback handler to send event after all samples are received and captured */
147static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
148 unsigned usage_id,
149 void *priv)
150{
151 struct iio_dev *indio_dev = platform_get_drvdata(priv);
152 struct dev_rot_state *rot_state = iio_priv(indio_dev);
153
154 dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
155 if (atomic_read(&rot_state->common_attributes.data_ready))
156 hid_sensor_push_data(indio_dev,
157 (u8 *)rot_state->sampled_vals,
158 sizeof(rot_state->sampled_vals));
159
160 return 0;
161}
162
163/* Capture samples in local storage */
164static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
165 unsigned usage_id,
166 size_t raw_len, char *raw_data,
167 void *priv)
168{
169 struct iio_dev *indio_dev = platform_get_drvdata(priv);
170 struct dev_rot_state *rot_state = iio_priv(indio_dev);
171
172 if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
173 memcpy(rot_state->sampled_vals, raw_data,
174 sizeof(rot_state->sampled_vals));
175 dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
176 sizeof(rot_state->sampled_vals));
177 }
178
179 return 0;
180}
181
182/* Parse report which is specific to an usage id*/
183static int dev_rot_parse_report(struct platform_device *pdev,
184 struct hid_sensor_hub_device *hsdev,
185 struct iio_chan_spec *channels,
186 unsigned usage_id,
187 struct dev_rot_state *st)
188{
189 int ret;
190
191 ret = sensor_hub_input_get_attribute_info(hsdev,
192 HID_INPUT_REPORT,
193 usage_id,
194 HID_USAGE_SENSOR_ORIENT_QUATERNION,
195 &st->quaternion);
196 if (ret)
197 return ret;
198
199 dev_rot_adjust_channel_bit_mask(&channels[0],
200 st->quaternion.size / 4);
201
202 dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
203 st->quaternion.report_id);
204
205 dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
206 st->quaternion.size);
207
208 st->scale_precision = hid_sensor_format_scale(
209 hsdev->usage,
210 &st->quaternion,
211 &st->scale_pre_decml, &st->scale_post_decml);
212
213 /* Set Sensitivity field ids, when there is no individual modifier */
214 if (st->common_attributes.sensitivity.index < 0) {
215 sensor_hub_input_get_attribute_info(hsdev,
216 HID_FEATURE_REPORT, usage_id,
217 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
218 HID_USAGE_SENSOR_DATA_ORIENTATION,
219 &st->common_attributes.sensitivity);
220 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
221 st->common_attributes.sensitivity.index,
222 st->common_attributes.sensitivity.report_id);
223 }
224
225 return 0;
226}
227
228/* Function to initialize the processing for usage id */
229static int hid_dev_rot_probe(struct platform_device *pdev)
230{
231 int ret;
232 char *name;
233 struct iio_dev *indio_dev;
234 struct dev_rot_state *rot_state;
235 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
236
237 indio_dev = devm_iio_device_alloc(&pdev->dev,
238 sizeof(struct dev_rot_state));
239 if (indio_dev == NULL)
240 return -ENOMEM;
241
242 platform_set_drvdata(pdev, indio_dev);
243
244 rot_state = iio_priv(indio_dev);
245 rot_state->common_attributes.hsdev = hsdev;
246 rot_state->common_attributes.pdev = pdev;
247
248 switch (hsdev->usage) {
249 case HID_USAGE_SENSOR_DEVICE_ORIENTATION:
250 name = "dev_rotation";
251 break;
252 case HID_USAGE_SENSOR_RELATIVE_ORIENTATION:
253 name = "relative_orientation";
254 break;
255 case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION:
256 name = "geomagnetic_orientation";
257 break;
258 default:
259 return -EINVAL;
260 }
261
262 ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
263 &rot_state->common_attributes);
264 if (ret) {
265 dev_err(&pdev->dev, "failed to setup common attributes\n");
266 return ret;
267 }
268
269 indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels,
270 sizeof(dev_rot_channels),
271 GFP_KERNEL);
272 if (!indio_dev->channels) {
273 dev_err(&pdev->dev, "failed to duplicate channels\n");
274 return -ENOMEM;
275 }
276
277 ret = dev_rot_parse_report(pdev, hsdev,
278 (struct iio_chan_spec *)indio_dev->channels,
279 hsdev->usage, rot_state);
280 if (ret) {
281 dev_err(&pdev->dev, "failed to setup attributes\n");
282 return ret;
283 }
284
285 indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
286 indio_dev->dev.parent = &pdev->dev;
287 indio_dev->info = &dev_rot_info;
288 indio_dev->name = name;
289 indio_dev->modes = INDIO_DIRECT_MODE;
290
291 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
292 NULL, NULL);
293 if (ret) {
294 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
295 return ret;
296 }
297 atomic_set(&rot_state->common_attributes.data_ready, 0);
298 ret = hid_sensor_setup_trigger(indio_dev, name,
299 &rot_state->common_attributes);
300 if (ret) {
301 dev_err(&pdev->dev, "trigger setup failed\n");
302 goto error_unreg_buffer_funcs;
303 }
304
305 ret = iio_device_register(indio_dev);
306 if (ret) {
307 dev_err(&pdev->dev, "device register failed\n");
308 goto error_remove_trigger;
309 }
310
311 rot_state->callbacks.send_event = dev_rot_proc_event;
312 rot_state->callbacks.capture_sample = dev_rot_capture_sample;
313 rot_state->callbacks.pdev = pdev;
314 ret = sensor_hub_register_callback(hsdev, hsdev->usage,
315 &rot_state->callbacks);
316 if (ret) {
317 dev_err(&pdev->dev, "callback reg failed\n");
318 goto error_iio_unreg;
319 }
320
321 return 0;
322
323error_iio_unreg:
324 iio_device_unregister(indio_dev);
325error_remove_trigger:
326 hid_sensor_remove_trigger(&rot_state->common_attributes);
327error_unreg_buffer_funcs:
328 iio_triggered_buffer_cleanup(indio_dev);
329 return ret;
330}
331
332/* Function to deinitialize the processing for usage id */
333static int hid_dev_rot_remove(struct platform_device *pdev)
334{
335 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
336 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
337 struct dev_rot_state *rot_state = iio_priv(indio_dev);
338
339 sensor_hub_remove_callback(hsdev, hsdev->usage);
340 iio_device_unregister(indio_dev);
341 hid_sensor_remove_trigger(&rot_state->common_attributes);
342 iio_triggered_buffer_cleanup(indio_dev);
343
344 return 0;
345}
346
347static const struct platform_device_id hid_dev_rot_ids[] = {
348 {
349 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
350 .name = "HID-SENSOR-20008a",
351 },
352 {
353 /* Relative orientation(AG) sensor */
354 .name = "HID-SENSOR-20008e",
355 },
356 {
357 /* Geomagnetic orientation(AM) sensor */
358 .name = "HID-SENSOR-2000c1",
359 },
360 { /* sentinel */ }
361};
362MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
363
364static struct platform_driver hid_dev_rot_platform_driver = {
365 .id_table = hid_dev_rot_ids,
366 .driver = {
367 .name = KBUILD_MODNAME,
368 .pm = &hid_sensor_pm_ops,
369 },
370 .probe = hid_dev_rot_probe,
371 .remove = hid_dev_rot_remove,
372};
373module_platform_driver(hid_dev_rot_platform_driver);
374
375MODULE_DESCRIPTION("HID Sensor Device Rotation");
376MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
377MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * HID Sensors Driver
4 * Copyright (c) 2014, Intel Corporation.
5 */
6
7#include <linux/device.h>
8#include <linux/platform_device.h>
9#include <linux/module.h>
10#include <linux/mod_devicetable.h>
11#include <linux/hid-sensor-hub.h>
12#include <linux/iio/iio.h>
13#include <linux/iio/sysfs.h>
14#include <linux/iio/buffer.h>
15#include "../common/hid-sensors/hid-sensor-trigger.h"
16
17struct dev_rot_state {
18 struct hid_sensor_hub_callbacks callbacks;
19 struct hid_sensor_common common_attributes;
20 struct hid_sensor_hub_attribute_info quaternion;
21 struct {
22 s32 sampled_vals[4] __aligned(16);
23 u64 timestamp __aligned(8);
24 } scan;
25 int scale_pre_decml;
26 int scale_post_decml;
27 int scale_precision;
28 int value_offset;
29 s64 timestamp;
30};
31
32static const u32 rotation_sensitivity_addresses[] = {
33 HID_USAGE_SENSOR_DATA_ORIENTATION,
34 HID_USAGE_SENSOR_ORIENT_QUATERNION,
35};
36
37/* Channel definitions */
38static const struct iio_chan_spec dev_rot_channels[] = {
39 {
40 .type = IIO_ROT,
41 .modified = 1,
42 .channel2 = IIO_MOD_QUATERNION,
43 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
44 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
45 BIT(IIO_CHAN_INFO_OFFSET) |
46 BIT(IIO_CHAN_INFO_SCALE) |
47 BIT(IIO_CHAN_INFO_HYSTERESIS),
48 .scan_index = 0
49 },
50 IIO_CHAN_SOFT_TIMESTAMP(1)
51};
52
53/* Adjust channel real bits based on report descriptor */
54static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan,
55 int size)
56{
57 chan->scan_type.sign = 's';
58 /* Real storage bits will change based on the report desc. */
59 chan->scan_type.realbits = size * 8;
60 /* Maximum size of a sample to capture is u32 */
61 chan->scan_type.storagebits = sizeof(u32) * 8;
62 chan->scan_type.repeat = 4;
63}
64
65/* Channel read_raw handler */
66static int dev_rot_read_raw(struct iio_dev *indio_dev,
67 struct iio_chan_spec const *chan,
68 int size, int *vals, int *val_len,
69 long mask)
70{
71 struct dev_rot_state *rot_state = iio_priv(indio_dev);
72 int ret_type;
73 int i;
74
75 vals[0] = 0;
76 vals[1] = 0;
77
78 switch (mask) {
79 case IIO_CHAN_INFO_RAW:
80 if (size >= 4) {
81 for (i = 0; i < 4; ++i)
82 vals[i] = rot_state->scan.sampled_vals[i];
83 ret_type = IIO_VAL_INT_MULTIPLE;
84 *val_len = 4;
85 } else
86 ret_type = -EINVAL;
87 break;
88 case IIO_CHAN_INFO_SCALE:
89 vals[0] = rot_state->scale_pre_decml;
90 vals[1] = rot_state->scale_post_decml;
91 return rot_state->scale_precision;
92
93 case IIO_CHAN_INFO_OFFSET:
94 *vals = rot_state->value_offset;
95 return IIO_VAL_INT;
96
97 case IIO_CHAN_INFO_SAMP_FREQ:
98 ret_type = hid_sensor_read_samp_freq_value(
99 &rot_state->common_attributes, &vals[0], &vals[1]);
100 break;
101 case IIO_CHAN_INFO_HYSTERESIS:
102 ret_type = hid_sensor_read_raw_hyst_value(
103 &rot_state->common_attributes, &vals[0], &vals[1]);
104 break;
105 default:
106 ret_type = -EINVAL;
107 break;
108 }
109
110 return ret_type;
111}
112
113/* Channel write_raw handler */
114static int dev_rot_write_raw(struct iio_dev *indio_dev,
115 struct iio_chan_spec const *chan,
116 int val,
117 int val2,
118 long mask)
119{
120 struct dev_rot_state *rot_state = iio_priv(indio_dev);
121 int ret;
122
123 switch (mask) {
124 case IIO_CHAN_INFO_SAMP_FREQ:
125 ret = hid_sensor_write_samp_freq_value(
126 &rot_state->common_attributes, val, val2);
127 break;
128 case IIO_CHAN_INFO_HYSTERESIS:
129 ret = hid_sensor_write_raw_hyst_value(
130 &rot_state->common_attributes, val, val2);
131 break;
132 default:
133 ret = -EINVAL;
134 }
135
136 return ret;
137}
138
139static const struct iio_info dev_rot_info = {
140 .read_raw_multi = &dev_rot_read_raw,
141 .write_raw = &dev_rot_write_raw,
142};
143
144/* Callback handler to send event after all samples are received and captured */
145static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
146 unsigned usage_id,
147 void *priv)
148{
149 struct iio_dev *indio_dev = platform_get_drvdata(priv);
150 struct dev_rot_state *rot_state = iio_priv(indio_dev);
151
152 dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
153 if (atomic_read(&rot_state->common_attributes.data_ready)) {
154 if (!rot_state->timestamp)
155 rot_state->timestamp = iio_get_time_ns(indio_dev);
156
157 iio_push_to_buffers_with_timestamp(indio_dev, &rot_state->scan,
158 rot_state->timestamp);
159
160 rot_state->timestamp = 0;
161 }
162
163 return 0;
164}
165
166/* Capture samples in local storage */
167static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
168 unsigned usage_id,
169 size_t raw_len, char *raw_data,
170 void *priv)
171{
172 struct iio_dev *indio_dev = platform_get_drvdata(priv);
173 struct dev_rot_state *rot_state = iio_priv(indio_dev);
174
175 if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
176 if (raw_len / 4 == sizeof(s16)) {
177 rot_state->scan.sampled_vals[0] = ((s16 *)raw_data)[0];
178 rot_state->scan.sampled_vals[1] = ((s16 *)raw_data)[1];
179 rot_state->scan.sampled_vals[2] = ((s16 *)raw_data)[2];
180 rot_state->scan.sampled_vals[3] = ((s16 *)raw_data)[3];
181 } else {
182 memcpy(&rot_state->scan.sampled_vals, raw_data,
183 sizeof(rot_state->scan.sampled_vals));
184 }
185
186 dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
187 sizeof(rot_state->scan.sampled_vals));
188 } else if (usage_id == HID_USAGE_SENSOR_TIME_TIMESTAMP) {
189 rot_state->timestamp = hid_sensor_convert_timestamp(&rot_state->common_attributes,
190 *(s64 *)raw_data);
191 }
192
193 return 0;
194}
195
196/* Parse report which is specific to an usage id*/
197static int dev_rot_parse_report(struct platform_device *pdev,
198 struct hid_sensor_hub_device *hsdev,
199 struct iio_chan_spec *channels,
200 unsigned usage_id,
201 struct dev_rot_state *st)
202{
203 int ret;
204
205 ret = sensor_hub_input_get_attribute_info(hsdev,
206 HID_INPUT_REPORT,
207 usage_id,
208 HID_USAGE_SENSOR_ORIENT_QUATERNION,
209 &st->quaternion);
210 if (ret)
211 return ret;
212
213 dev_rot_adjust_channel_bit_mask(&channels[0],
214 st->quaternion.size / 4);
215
216 dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
217 st->quaternion.report_id);
218
219 dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
220 st->quaternion.size);
221
222 st->scale_precision = hid_sensor_format_scale(
223 hsdev->usage,
224 &st->quaternion,
225 &st->scale_pre_decml, &st->scale_post_decml);
226
227 return 0;
228}
229
230/* Function to initialize the processing for usage id */
231static int hid_dev_rot_probe(struct platform_device *pdev)
232{
233 int ret;
234 char *name;
235 struct iio_dev *indio_dev;
236 struct dev_rot_state *rot_state;
237 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
238
239 indio_dev = devm_iio_device_alloc(&pdev->dev,
240 sizeof(struct dev_rot_state));
241 if (indio_dev == NULL)
242 return -ENOMEM;
243
244 platform_set_drvdata(pdev, indio_dev);
245
246 rot_state = iio_priv(indio_dev);
247 rot_state->common_attributes.hsdev = hsdev;
248 rot_state->common_attributes.pdev = pdev;
249
250 switch (hsdev->usage) {
251 case HID_USAGE_SENSOR_DEVICE_ORIENTATION:
252 name = "dev_rotation";
253 break;
254 case HID_USAGE_SENSOR_RELATIVE_ORIENTATION:
255 name = "relative_orientation";
256 break;
257 case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION:
258 name = "geomagnetic_orientation";
259 break;
260 default:
261 return -EINVAL;
262 }
263
264 ret = hid_sensor_parse_common_attributes(hsdev,
265 hsdev->usage,
266 &rot_state->common_attributes,
267 rotation_sensitivity_addresses,
268 ARRAY_SIZE(rotation_sensitivity_addresses));
269 if (ret) {
270 dev_err(&pdev->dev, "failed to setup common attributes\n");
271 return ret;
272 }
273
274 indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels,
275 sizeof(dev_rot_channels),
276 GFP_KERNEL);
277 if (!indio_dev->channels) {
278 dev_err(&pdev->dev, "failed to duplicate channels\n");
279 return -ENOMEM;
280 }
281
282 ret = dev_rot_parse_report(pdev, hsdev,
283 (struct iio_chan_spec *)indio_dev->channels,
284 hsdev->usage, rot_state);
285 if (ret) {
286 dev_err(&pdev->dev, "failed to setup attributes\n");
287 return ret;
288 }
289
290 indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
291 indio_dev->info = &dev_rot_info;
292 indio_dev->name = name;
293 indio_dev->modes = INDIO_DIRECT_MODE;
294
295 atomic_set(&rot_state->common_attributes.data_ready, 0);
296
297 ret = hid_sensor_setup_trigger(indio_dev, name,
298 &rot_state->common_attributes);
299 if (ret) {
300 dev_err(&pdev->dev, "trigger setup failed\n");
301 return ret;
302 }
303
304 ret = iio_device_register(indio_dev);
305 if (ret) {
306 dev_err(&pdev->dev, "device register failed\n");
307 goto error_remove_trigger;
308 }
309
310 rot_state->callbacks.send_event = dev_rot_proc_event;
311 rot_state->callbacks.capture_sample = dev_rot_capture_sample;
312 rot_state->callbacks.pdev = pdev;
313 ret = sensor_hub_register_callback(hsdev, hsdev->usage,
314 &rot_state->callbacks);
315 if (ret) {
316 dev_err(&pdev->dev, "callback reg failed\n");
317 goto error_iio_unreg;
318 }
319
320 return 0;
321
322error_iio_unreg:
323 iio_device_unregister(indio_dev);
324error_remove_trigger:
325 hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
326 return ret;
327}
328
329/* Function to deinitialize the processing for usage id */
330static int hid_dev_rot_remove(struct platform_device *pdev)
331{
332 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
333 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
334 struct dev_rot_state *rot_state = iio_priv(indio_dev);
335
336 sensor_hub_remove_callback(hsdev, hsdev->usage);
337 iio_device_unregister(indio_dev);
338 hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
339
340 return 0;
341}
342
343static const struct platform_device_id hid_dev_rot_ids[] = {
344 {
345 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
346 .name = "HID-SENSOR-20008a",
347 },
348 {
349 /* Relative orientation(AG) sensor */
350 .name = "HID-SENSOR-20008e",
351 },
352 {
353 /* Geomagnetic orientation(AM) sensor */
354 .name = "HID-SENSOR-2000c1",
355 },
356 { /* sentinel */ }
357};
358MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
359
360static struct platform_driver hid_dev_rot_platform_driver = {
361 .id_table = hid_dev_rot_ids,
362 .driver = {
363 .name = KBUILD_MODNAME,
364 .pm = &hid_sensor_pm_ops,
365 },
366 .probe = hid_dev_rot_probe,
367 .remove = hid_dev_rot_remove,
368};
369module_platform_driver(hid_dev_rot_platform_driver);
370
371MODULE_DESCRIPTION("HID Sensor Device Rotation");
372MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
373MODULE_LICENSE("GPL");
374MODULE_IMPORT_NS(IIO_HID);