blob: 2238a26aba637d0afd5adb14cd01ad3f729fc931 [file] [log] [blame]
srinivas pandruvada45fe6f72012-09-05 13:56:00 +01001/*
2 * HID Sensors Driver
3 * Copyright (c) 2012, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19#include <linux/device.h>
20#include <linux/platform_device.h>
21#include <linux/module.h>
22#include <linux/interrupt.h>
23#include <linux/irq.h>
24#include <linux/slab.h>
Srinivas Pandruvadac6399452014-04-19 00:22:00 +010025#include <linux/delay.h>
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010026#include <linux/hid-sensor-hub.h>
27#include <linux/iio/iio.h>
28#include <linux/iio/sysfs.h>
29#include <linux/iio/buffer.h>
30#include <linux/iio/trigger_consumer.h>
31#include <linux/iio/triggered_buffer.h>
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010032#include "../common/hid-sensors/hid-sensor-trigger.h"
33
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010034enum accel_3d_channel {
35 CHANNEL_SCAN_INDEX_X,
36 CHANNEL_SCAN_INDEX_Y,
37 CHANNEL_SCAN_INDEX_Z,
38 ACCEL_3D_CHANNEL_MAX,
39};
40
41struct accel_3d_state {
42 struct hid_sensor_hub_callbacks callbacks;
Alexander Hollere07c6d12012-12-15 12:45:00 +000043 struct hid_sensor_common common_attributes;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010044 struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX];
Srinivas Pandruvadaa96cd0f2016-11-28 14:41:16 -080045 /* Reserve for 3 channels + padding + timestamp */
46 u32 accel_val[ACCEL_3D_CHANNEL_MAX + 3];
Srinivas Pandruvada73a24982014-04-19 00:22:00 +010047 int scale_pre_decml;
48 int scale_post_decml;
49 int scale_precision;
50 int value_offset;
Srinivas Pandruvadaa96cd0f2016-11-28 14:41:16 -080051 int64_t timestamp;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010052};
53
54static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = {
55 HID_USAGE_SENSOR_ACCEL_X_AXIS,
56 HID_USAGE_SENSOR_ACCEL_Y_AXIS,
57 HID_USAGE_SENSOR_ACCEL_Z_AXIS
58};
59
60/* Channel definitions */
61static const struct iio_chan_spec accel_3d_channels[] = {
62 {
63 .type = IIO_ACCEL,
64 .modified = 1,
65 .channel2 = IIO_MOD_X,
Srinivas Pandruvadac6399452014-04-19 00:22:00 +010066 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jonathan Cameronecbe18f2013-02-27 19:03:04 +000067 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
68 BIT(IIO_CHAN_INFO_SCALE) |
69 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
70 BIT(IIO_CHAN_INFO_HYSTERESIS),
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010071 .scan_index = CHANNEL_SCAN_INDEX_X,
72 }, {
73 .type = IIO_ACCEL,
74 .modified = 1,
75 .channel2 = IIO_MOD_Y,
Srinivas Pandruvadac6399452014-04-19 00:22:00 +010076 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jonathan Cameronecbe18f2013-02-27 19:03:04 +000077 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
78 BIT(IIO_CHAN_INFO_SCALE) |
79 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
80 BIT(IIO_CHAN_INFO_HYSTERESIS),
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010081 .scan_index = CHANNEL_SCAN_INDEX_Y,
82 }, {
83 .type = IIO_ACCEL,
84 .modified = 1,
85 .channel2 = IIO_MOD_Z,
Srinivas Pandruvadac6399452014-04-19 00:22:00 +010086 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jonathan Cameronecbe18f2013-02-27 19:03:04 +000087 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
88 BIT(IIO_CHAN_INFO_SCALE) |
89 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
90 BIT(IIO_CHAN_INFO_HYSTERESIS),
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010091 .scan_index = CHANNEL_SCAN_INDEX_Z,
Srinivas Pandruvadaa96cd0f2016-11-28 14:41:16 -080092 },
93 IIO_CHAN_SOFT_TIMESTAMP(3)
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010094};
95
Song Hongyan0e377f32017-01-05 18:24:04 +080096/* Channel definitions */
97static const struct iio_chan_spec gravity_channels[] = {
98 {
99 .type = IIO_GRAVITY,
100 .modified = 1,
101 .channel2 = IIO_MOD_X,
102 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
103 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
104 BIT(IIO_CHAN_INFO_SCALE) |
105 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
106 BIT(IIO_CHAN_INFO_HYSTERESIS),
107 .scan_index = CHANNEL_SCAN_INDEX_X,
108 }, {
109 .type = IIO_GRAVITY,
110 .modified = 1,
111 .channel2 = IIO_MOD_Y,
112 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
113 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
114 BIT(IIO_CHAN_INFO_SCALE) |
115 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
116 BIT(IIO_CHAN_INFO_HYSTERESIS),
117 .scan_index = CHANNEL_SCAN_INDEX_Y,
118 }, {
119 .type = IIO_GRAVITY,
120 .modified = 1,
121 .channel2 = IIO_MOD_Z,
122 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
123 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
124 BIT(IIO_CHAN_INFO_SCALE) |
125 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
126 BIT(IIO_CHAN_INFO_HYSTERESIS),
127 .scan_index = CHANNEL_SCAN_INDEX_Z,
128 }
129};
130
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100131/* Adjust channel real bits based on report descriptor */
132static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
133 int channel, int size)
134{
135 channels[channel].scan_type.sign = 's';
136 /* Real storage bits will change based on the report desc. */
137 channels[channel].scan_type.realbits = size * 8;
138 /* Maximum size of a sample to capture is u32 */
139 channels[channel].scan_type.storagebits = sizeof(u32) * 8;
140}
141
142/* Channel read_raw handler */
143static int accel_3d_read_raw(struct iio_dev *indio_dev,
144 struct iio_chan_spec const *chan,
145 int *val, int *val2,
146 long mask)
147{
148 struct accel_3d_state *accel_state = iio_priv(indio_dev);
149 int report_id = -1;
150 u32 address;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100151 int ret_type;
Song Hongyan0e377f32017-01-05 18:24:04 +0800152 struct hid_sensor_hub_device *hsdev =
153 accel_state->common_attributes.hsdev;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100154
155 *val = 0;
156 *val2 = 0;
157 switch (mask) {
158 case 0:
Srinivas Pandruvadac6399452014-04-19 00:22:00 +0100159 hid_sensor_power_state(&accel_state->common_attributes, true);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100160 report_id = accel_state->accel[chan->scan_index].report_id;
161 address = accel_3d_addresses[chan->scan_index];
162 if (report_id >= 0)
163 *val = sensor_hub_input_attr_get_raw_value(
Srinivas Pandruvadac6399452014-04-19 00:22:00 +0100164 accel_state->common_attributes.hsdev,
Song Hongyan0e377f32017-01-05 18:24:04 +0800165 hsdev->usage, address, report_id,
Srinivas Pandruvadab3f47372015-02-19 15:33:56 -0800166 SENSOR_HUB_SYNC);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100167 else {
168 *val = 0;
Srinivas Pandruvadac6399452014-04-19 00:22:00 +0100169 hid_sensor_power_state(&accel_state->common_attributes,
170 false);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100171 return -EINVAL;
172 }
Srinivas Pandruvadac6399452014-04-19 00:22:00 +0100173 hid_sensor_power_state(&accel_state->common_attributes, false);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100174 ret_type = IIO_VAL_INT;
175 break;
176 case IIO_CHAN_INFO_SCALE:
Srinivas Pandruvada73a24982014-04-19 00:22:00 +0100177 *val = accel_state->scale_pre_decml;
178 *val2 = accel_state->scale_post_decml;
179 ret_type = accel_state->scale_precision;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100180 break;
181 case IIO_CHAN_INFO_OFFSET:
Srinivas Pandruvada73a24982014-04-19 00:22:00 +0100182 *val = accel_state->value_offset;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100183 ret_type = IIO_VAL_INT;
184 break;
185 case IIO_CHAN_INFO_SAMP_FREQ:
Sachin Kamat2f084c12014-03-07 07:44:00 +0000186 ret_type = hid_sensor_read_samp_freq_value(
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100187 &accel_state->common_attributes, val, val2);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100188 break;
189 case IIO_CHAN_INFO_HYSTERESIS:
Sachin Kamat2f084c12014-03-07 07:44:00 +0000190 ret_type = hid_sensor_read_raw_hyst_value(
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100191 &accel_state->common_attributes, val, val2);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100192 break;
193 default:
194 ret_type = -EINVAL;
195 break;
196 }
197
198 return ret_type;
199}
200
201/* Channel write_raw handler */
202static int accel_3d_write_raw(struct iio_dev *indio_dev,
203 struct iio_chan_spec const *chan,
204 int val,
205 int val2,
206 long mask)
207{
208 struct accel_3d_state *accel_state = iio_priv(indio_dev);
209 int ret = 0;
210
211 switch (mask) {
212 case IIO_CHAN_INFO_SAMP_FREQ:
213 ret = hid_sensor_write_samp_freq_value(
214 &accel_state->common_attributes, val, val2);
215 break;
216 case IIO_CHAN_INFO_HYSTERESIS:
217 ret = hid_sensor_write_raw_hyst_value(
218 &accel_state->common_attributes, val, val2);
219 break;
220 default:
221 ret = -EINVAL;
222 }
223
224 return ret;
225}
226
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100227static const struct iio_info accel_3d_info = {
228 .driver_module = THIS_MODULE,
229 .read_raw = &accel_3d_read_raw,
230 .write_raw = &accel_3d_write_raw,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100231};
232
233/* Function to push data to buffer */
Srinivas Pandruvadaa96cd0f2016-11-28 14:41:16 -0800234static void hid_sensor_push_data(struct iio_dev *indio_dev, void *data,
235 int len, int64_t timestamp)
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100236{
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100237 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
Srinivas Pandruvadaa96cd0f2016-11-28 14:41:16 -0800238 iio_push_to_buffers_with_timestamp(indio_dev, data, timestamp);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100239}
240
241/* Callback handler to send event after all samples are received and captured */
242static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
243 unsigned usage_id,
244 void *priv)
245{
246 struct iio_dev *indio_dev = platform_get_drvdata(priv);
247 struct accel_3d_state *accel_state = iio_priv(indio_dev);
248
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100249 dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n");
Srinivas Pandruvadaa96cd0f2016-11-28 14:41:16 -0800250 if (atomic_read(&accel_state->common_attributes.data_ready)) {
251 if (!accel_state->timestamp)
252 accel_state->timestamp = iio_get_time_ns(indio_dev);
253
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100254 hid_sensor_push_data(indio_dev,
Srinivas Pandruvadaa96cd0f2016-11-28 14:41:16 -0800255 accel_state->accel_val,
256 sizeof(accel_state->accel_val),
257 accel_state->timestamp);
258
259 accel_state->timestamp = 0;
260 }
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100261
262 return 0;
263}
264
265/* Capture samples in local storage */
266static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
267 unsigned usage_id,
268 size_t raw_len, char *raw_data,
269 void *priv)
270{
271 struct iio_dev *indio_dev = platform_get_drvdata(priv);
272 struct accel_3d_state *accel_state = iio_priv(indio_dev);
273 int offset;
274 int ret = -EINVAL;
275
276 switch (usage_id) {
277 case HID_USAGE_SENSOR_ACCEL_X_AXIS:
278 case HID_USAGE_SENSOR_ACCEL_Y_AXIS:
279 case HID_USAGE_SENSOR_ACCEL_Z_AXIS:
280 offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS;
281 accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] =
282 *(u32 *)raw_data;
283 ret = 0;
284 break;
Srinivas Pandruvadaa96cd0f2016-11-28 14:41:16 -0800285 case HID_USAGE_SENSOR_TIME_TIMESTAMP:
286 accel_state->timestamp =
287 hid_sensor_convert_timestamp(
288 &accel_state->common_attributes,
289 *(int64_t *)raw_data);
290 break;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100291 default:
292 break;
293 }
294
295 return ret;
296}
297
298/* Parse report which is specific to an usage id*/
299static int accel_3d_parse_report(struct platform_device *pdev,
300 struct hid_sensor_hub_device *hsdev,
301 struct iio_chan_spec *channels,
302 unsigned usage_id,
303 struct accel_3d_state *st)
304{
305 int ret;
306 int i;
307
308 for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
309 ret = sensor_hub_input_get_attribute_info(hsdev,
310 HID_INPUT_REPORT,
311 usage_id,
312 HID_USAGE_SENSOR_ACCEL_X_AXIS + i,
313 &st->accel[CHANNEL_SCAN_INDEX_X + i]);
314 if (ret < 0)
315 break;
316 accel_3d_adjust_channel_bit_mask(channels,
317 CHANNEL_SCAN_INDEX_X + i,
318 st->accel[CHANNEL_SCAN_INDEX_X + i].size);
319 }
320 dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
321 st->accel[0].index,
322 st->accel[0].report_id,
323 st->accel[1].index, st->accel[1].report_id,
324 st->accel[2].index, st->accel[2].report_id);
325
Srinivas Pandruvada73a24982014-04-19 00:22:00 +0100326 st->scale_precision = hid_sensor_format_scale(
Song Hongyan0e377f32017-01-05 18:24:04 +0800327 hsdev->usage,
Srinivas Pandruvada73a24982014-04-19 00:22:00 +0100328 &st->accel[CHANNEL_SCAN_INDEX_X],
329 &st->scale_pre_decml, &st->scale_post_decml);
330
Srinivas Pandruvada64528d02013-10-30 22:48:00 +0000331 /* Set Sensitivity field ids, when there is no individual modifier */
332 if (st->common_attributes.sensitivity.index < 0) {
333 sensor_hub_input_get_attribute_info(hsdev,
334 HID_FEATURE_REPORT, usage_id,
335 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
336 HID_USAGE_SENSOR_DATA_ACCELERATION,
337 &st->common_attributes.sensitivity);
338 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
339 st->common_attributes.sensitivity.index,
340 st->common_attributes.sensitivity.report_id);
341 }
342
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100343 return ret;
344}
345
346/* Function to initialize the processing for usage id */
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800347static int hid_accel_3d_probe(struct platform_device *pdev)
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100348{
349 int ret = 0;
Julia Lawall40f4b1f2017-05-04 22:10:52 +0200350 const char *name;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100351 struct iio_dev *indio_dev;
352 struct accel_3d_state *accel_state;
Song Hongyan0e377f32017-01-05 18:24:04 +0800353 const struct iio_chan_spec *channel_spec;
354 int channel_size;
355
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100356 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100357
Sachin Kamat6b545ee2013-07-23 07:47:00 +0100358 indio_dev = devm_iio_device_alloc(&pdev->dev,
359 sizeof(struct accel_3d_state));
360 if (indio_dev == NULL)
361 return -ENOMEM;
362
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100363 platform_set_drvdata(pdev, indio_dev);
364
365 accel_state = iio_priv(indio_dev);
366 accel_state->common_attributes.hsdev = hsdev;
367 accel_state->common_attributes.pdev = pdev;
368
Song Hongyan0e377f32017-01-05 18:24:04 +0800369 if (hsdev->usage == HID_USAGE_SENSOR_ACCEL_3D) {
370 name = "accel_3d";
371 channel_spec = accel_3d_channels;
372 channel_size = sizeof(accel_3d_channels);
Srinivas Pandruvada862d1d82017-03-29 16:23:35 -0700373 indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels);
Song Hongyan0e377f32017-01-05 18:24:04 +0800374 } else {
375 name = "gravity";
376 channel_spec = gravity_channels;
377 channel_size = sizeof(gravity_channels);
Srinivas Pandruvada862d1d82017-03-29 16:23:35 -0700378 indio_dev->num_channels = ARRAY_SIZE(gravity_channels);
Song Hongyan0e377f32017-01-05 18:24:04 +0800379 }
380 ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100381 &accel_state->common_attributes);
382 if (ret) {
383 dev_err(&pdev->dev, "failed to setup common attributes\n");
Sachin Kamat6b545ee2013-07-23 07:47:00 +0100384 return ret;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100385 }
Song Hongyan0e377f32017-01-05 18:24:04 +0800386 indio_dev->channels = kmemdup(channel_spec, channel_size, GFP_KERNEL);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100387
Fabio Estevamb136faf2015-05-13 15:06:09 -0300388 if (!indio_dev->channels) {
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100389 dev_err(&pdev->dev, "failed to duplicate channels\n");
Sachin Kamat6b545ee2013-07-23 07:47:00 +0100390 return -ENOMEM;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100391 }
Fabio Estevamb136faf2015-05-13 15:06:09 -0300392 ret = accel_3d_parse_report(pdev, hsdev,
Song Hongyan0e377f32017-01-05 18:24:04 +0800393 (struct iio_chan_spec *)indio_dev->channels,
394 hsdev->usage, accel_state);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100395 if (ret) {
396 dev_err(&pdev->dev, "failed to setup attributes\n");
397 goto error_free_dev_mem;
398 }
399
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100400 indio_dev->dev.parent = &pdev->dev;
401 indio_dev->info = &accel_3d_info;
402 indio_dev->name = name;
403 indio_dev->modes = INDIO_DIRECT_MODE;
404
405 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
406 NULL, NULL);
407 if (ret) {
408 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
409 goto error_free_dev_mem;
410 }
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100411 atomic_set(&accel_state->common_attributes.data_ready, 0);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100412 ret = hid_sensor_setup_trigger(indio_dev, name,
413 &accel_state->common_attributes);
414 if (ret < 0) {
415 dev_err(&pdev->dev, "trigger setup failed\n");
416 goto error_unreg_buffer_funcs;
417 }
418
419 ret = iio_device_register(indio_dev);
420 if (ret) {
421 dev_err(&pdev->dev, "device register failed\n");
422 goto error_remove_trigger;
423 }
424
425 accel_state->callbacks.send_event = accel_3d_proc_event;
426 accel_state->callbacks.capture_sample = accel_3d_capture_sample;
427 accel_state->callbacks.pdev = pdev;
Song Hongyan0e377f32017-01-05 18:24:04 +0800428 ret = sensor_hub_register_callback(hsdev, hsdev->usage,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100429 &accel_state->callbacks);
430 if (ret < 0) {
431 dev_err(&pdev->dev, "callback reg failed\n");
432 goto error_iio_unreg;
433 }
434
435 return ret;
436
437error_iio_unreg:
438 iio_device_unregister(indio_dev);
439error_remove_trigger:
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000440 hid_sensor_remove_trigger(&accel_state->common_attributes);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100441error_unreg_buffer_funcs:
442 iio_triggered_buffer_cleanup(indio_dev);
443error_free_dev_mem:
444 kfree(indio_dev->channels);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100445 return ret;
446}
447
448/* Function to deinitialize the processing for usage id */
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800449static int hid_accel_3d_remove(struct platform_device *pdev)
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100450{
451 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
452 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000453 struct accel_3d_state *accel_state = iio_priv(indio_dev);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100454
Song Hongyan0e377f32017-01-05 18:24:04 +0800455 sensor_hub_remove_callback(hsdev, hsdev->usage);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100456 iio_device_unregister(indio_dev);
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000457 hid_sensor_remove_trigger(&accel_state->common_attributes);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100458 iio_triggered_buffer_cleanup(indio_dev);
459 kfree(indio_dev->channels);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100460
461 return 0;
462}
463
Krzysztof Kozlowski76ec50a2015-05-02 00:53:30 +0900464static const struct platform_device_id hid_accel_3d_ids[] = {
Alexander Holler66c721f2013-07-10 09:31:00 +0100465 {
466 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
467 .name = "HID-SENSOR-200073",
468 },
Song Hongyan0e377f32017-01-05 18:24:04 +0800469 { /* gravity sensor */
470 .name = "HID-SENSOR-20007b",
471 },
Alexander Holler66c721f2013-07-10 09:31:00 +0100472 { /* sentinel */ }
473};
474MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids);
475
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100476static struct platform_driver hid_accel_3d_platform_driver = {
Alexander Holler66c721f2013-07-10 09:31:00 +0100477 .id_table = hid_accel_3d_ids,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100478 .driver = {
Alexander Holler66c721f2013-07-10 09:31:00 +0100479 .name = KBUILD_MODNAME,
Srinivas Pandruvadacbbcc932015-01-07 10:55:54 -0800480 .pm = &hid_sensor_pm_ops,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100481 },
482 .probe = hid_accel_3d_probe,
483 .remove = hid_accel_3d_remove,
484};
485module_platform_driver(hid_accel_3d_platform_driver);
486
487MODULE_DESCRIPTION("HID Sensor Accel 3D");
488MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
489MODULE_LICENSE("GPL");