1 /*
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>
25 #include <linux/hid-sensor-hub.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
31 #include "../common/hid-sensors/hid-sensor-trigger.h"
32 
33 enum magn_3d_channel {
34 	CHANNEL_SCAN_INDEX_X,
35 	CHANNEL_SCAN_INDEX_Y,
36 	CHANNEL_SCAN_INDEX_Z,
37 	MAGN_3D_CHANNEL_MAX,
38 };
39 
40 struct magn_3d_state {
41 	struct hid_sensor_hub_callbacks callbacks;
42 	struct hid_sensor_common common_attributes;
43 	struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX];
44 	u32 magn_val[MAGN_3D_CHANNEL_MAX];
45 };
46 
47 static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
48 	HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS,
49 	HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS,
50 	HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS
51 };
52 
53 /* Channel definitions */
54 static const struct iio_chan_spec magn_3d_channels[] = {
55 	{
56 		.type = IIO_MAGN,
57 		.modified = 1,
58 		.channel2 = IIO_MOD_X,
59 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
60 		BIT(IIO_CHAN_INFO_SCALE) |
61 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
62 		BIT(IIO_CHAN_INFO_HYSTERESIS),
63 		.scan_index = CHANNEL_SCAN_INDEX_X,
64 	}, {
65 		.type = IIO_MAGN,
66 		.modified = 1,
67 		.channel2 = IIO_MOD_Y,
68 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
69 		BIT(IIO_CHAN_INFO_SCALE) |
70 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
71 		BIT(IIO_CHAN_INFO_HYSTERESIS),
72 		.scan_index = CHANNEL_SCAN_INDEX_Y,
73 	}, {
74 		.type = IIO_MAGN,
75 		.modified = 1,
76 		.channel2 = IIO_MOD_Z,
77 		.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),
81 		.scan_index = CHANNEL_SCAN_INDEX_Z,
82 	}
83 };
84 
85 /* Adjust channel real bits based on report descriptor */
86 static void magn_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
87 						int channel, int size)
88 {
89 	channels[channel].scan_type.sign = 's';
90 	/* Real storage bits will change based on the report desc. */
91 	channels[channel].scan_type.realbits = size * 8;
92 	/* Maximum size of a sample to capture is u32 */
93 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
94 }
95 
96 /* Channel read_raw handler */
97 static int magn_3d_read_raw(struct iio_dev *indio_dev,
98 			      struct iio_chan_spec const *chan,
99 			      int *val, int *val2,
100 			      long mask)
101 {
102 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
103 	int report_id = -1;
104 	u32 address;
105 	int ret;
106 	int ret_type;
107 
108 	*val = 0;
109 	*val2 = 0;
110 	switch (mask) {
111 	case 0:
112 		report_id =
113 			magn_state->magn[chan->scan_index].report_id;
114 		address = magn_3d_addresses[chan->scan_index];
115 		if (report_id >= 0)
116 			*val = sensor_hub_input_attr_get_raw_value(
117 				magn_state->common_attributes.hsdev,
118 				HID_USAGE_SENSOR_COMPASS_3D, address,
119 				report_id);
120 		else {
121 			*val = 0;
122 			return -EINVAL;
123 		}
124 		ret_type = IIO_VAL_INT;
125 		break;
126 	case IIO_CHAN_INFO_SCALE:
127 		*val = magn_state->magn[CHANNEL_SCAN_INDEX_X].units;
128 		ret_type = IIO_VAL_INT;
129 		break;
130 	case IIO_CHAN_INFO_OFFSET:
131 		*val = hid_sensor_convert_exponent(
132 			magn_state->magn[CHANNEL_SCAN_INDEX_X].unit_expo);
133 		ret_type = IIO_VAL_INT;
134 		break;
135 	case IIO_CHAN_INFO_SAMP_FREQ:
136 		ret = hid_sensor_read_samp_freq_value(
137 			&magn_state->common_attributes, val, val2);
138 		ret_type = IIO_VAL_INT_PLUS_MICRO;
139 		break;
140 	case IIO_CHAN_INFO_HYSTERESIS:
141 		ret = hid_sensor_read_raw_hyst_value(
142 			&magn_state->common_attributes, val, val2);
143 		ret_type = IIO_VAL_INT_PLUS_MICRO;
144 		break;
145 	default:
146 		ret_type = -EINVAL;
147 		break;
148 	}
149 
150 	return ret_type;
151 }
152 
153 /* Channel write_raw handler */
154 static int magn_3d_write_raw(struct iio_dev *indio_dev,
155 			       struct iio_chan_spec const *chan,
156 			       int val,
157 			       int val2,
158 			       long mask)
159 {
160 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
161 	int ret = 0;
162 
163 	switch (mask) {
164 	case IIO_CHAN_INFO_SAMP_FREQ:
165 		ret = hid_sensor_write_samp_freq_value(
166 				&magn_state->common_attributes, val, val2);
167 		break;
168 	case IIO_CHAN_INFO_HYSTERESIS:
169 		ret = hid_sensor_write_raw_hyst_value(
170 				&magn_state->common_attributes, val, val2);
171 		break;
172 	default:
173 		ret = -EINVAL;
174 	}
175 
176 	return ret;
177 }
178 
179 static const struct iio_info magn_3d_info = {
180 	.driver_module = THIS_MODULE,
181 	.read_raw = &magn_3d_read_raw,
182 	.write_raw = &magn_3d_write_raw,
183 };
184 
185 /* Function to push data to buffer */
186 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
187 	int len)
188 {
189 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
190 	iio_push_to_buffers(indio_dev, data);
191 }
192 
193 /* Callback handler to send event after all samples are received and captured */
194 static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev,
195 				unsigned usage_id,
196 				void *priv)
197 {
198 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
199 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
200 
201 	dev_dbg(&indio_dev->dev, "magn_3d_proc_event [%d]\n",
202 				magn_state->common_attributes.data_ready);
203 	if (magn_state->common_attributes.data_ready)
204 		hid_sensor_push_data(indio_dev,
205 				magn_state->magn_val,
206 				sizeof(magn_state->magn_val));
207 
208 	return 0;
209 }
210 
211 /* Capture samples in local storage */
212 static int magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
213 				unsigned usage_id,
214 				size_t raw_len, char *raw_data,
215 				void *priv)
216 {
217 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
218 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
219 	int offset;
220 	int ret = -EINVAL;
221 
222 	switch (usage_id) {
223 	case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS:
224 	case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS:
225 	case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS:
226 		offset = usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS;
227 		magn_state->magn_val[CHANNEL_SCAN_INDEX_X + offset] =
228 						*(u32 *)raw_data;
229 		ret = 0;
230 	break;
231 	default:
232 		break;
233 	}
234 
235 	return ret;
236 }
237 
238 /* Parse report which is specific to an usage id*/
239 static int magn_3d_parse_report(struct platform_device *pdev,
240 				struct hid_sensor_hub_device *hsdev,
241 				struct iio_chan_spec *channels,
242 				unsigned usage_id,
243 				struct magn_3d_state *st)
244 {
245 	int ret;
246 	int i;
247 
248 	for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
249 		ret = sensor_hub_input_get_attribute_info(hsdev,
250 				HID_INPUT_REPORT,
251 				usage_id,
252 				HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS + i,
253 				&st->magn[CHANNEL_SCAN_INDEX_X + i]);
254 		if (ret < 0)
255 			break;
256 		magn_3d_adjust_channel_bit_mask(channels,
257 				CHANNEL_SCAN_INDEX_X + i,
258 				st->magn[CHANNEL_SCAN_INDEX_X + i].size);
259 	}
260 	dev_dbg(&pdev->dev, "magn_3d %x:%x, %x:%x, %x:%x\n",
261 			st->magn[0].index,
262 			st->magn[0].report_id,
263 			st->magn[1].index, st->magn[1].report_id,
264 			st->magn[2].index, st->magn[2].report_id);
265 
266 	/* Set Sensitivity field ids, when there is no individual modifier */
267 	if (st->common_attributes.sensitivity.index < 0) {
268 		sensor_hub_input_get_attribute_info(hsdev,
269 			HID_FEATURE_REPORT, usage_id,
270 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
271 			HID_USAGE_SENSOR_DATA_ORIENTATION,
272 			&st->common_attributes.sensitivity);
273 		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
274 			st->common_attributes.sensitivity.index,
275 			st->common_attributes.sensitivity.report_id);
276 	}
277 
278 	return ret;
279 }
280 
281 /* Function to initialize the processing for usage id */
282 static int hid_magn_3d_probe(struct platform_device *pdev)
283 {
284 	int ret = 0;
285 	static char *name = "magn_3d";
286 	struct iio_dev *indio_dev;
287 	struct magn_3d_state *magn_state;
288 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
289 	struct iio_chan_spec *channels;
290 
291 	indio_dev = devm_iio_device_alloc(&pdev->dev,
292 					  sizeof(struct magn_3d_state));
293 	if (indio_dev == NULL)
294 		return -ENOMEM;
295 
296 	platform_set_drvdata(pdev, indio_dev);
297 
298 	magn_state = iio_priv(indio_dev);
299 	magn_state->common_attributes.hsdev = hsdev;
300 	magn_state->common_attributes.pdev = pdev;
301 
302 	ret = hid_sensor_parse_common_attributes(hsdev,
303 				HID_USAGE_SENSOR_COMPASS_3D,
304 				&magn_state->common_attributes);
305 	if (ret) {
306 		dev_err(&pdev->dev, "failed to setup common attributes\n");
307 		return ret;
308 	}
309 
310 	channels = kmemdup(magn_3d_channels, sizeof(magn_3d_channels),
311 			   GFP_KERNEL);
312 	if (!channels) {
313 		dev_err(&pdev->dev, "failed to duplicate channels\n");
314 		return -ENOMEM;
315 	}
316 
317 	ret = magn_3d_parse_report(pdev, hsdev, channels,
318 				HID_USAGE_SENSOR_COMPASS_3D, magn_state);
319 	if (ret) {
320 		dev_err(&pdev->dev, "failed to setup attributes\n");
321 		goto error_free_dev_mem;
322 	}
323 
324 	indio_dev->channels = channels;
325 	indio_dev->num_channels = ARRAY_SIZE(magn_3d_channels);
326 	indio_dev->dev.parent = &pdev->dev;
327 	indio_dev->info = &magn_3d_info;
328 	indio_dev->name = name;
329 	indio_dev->modes = INDIO_DIRECT_MODE;
330 
331 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
332 		NULL, NULL);
333 	if (ret) {
334 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
335 		goto error_free_dev_mem;
336 	}
337 	magn_state->common_attributes.data_ready = false;
338 	ret = hid_sensor_setup_trigger(indio_dev, name,
339 					&magn_state->common_attributes);
340 	if (ret < 0) {
341 		dev_err(&pdev->dev, "trigger setup failed\n");
342 		goto error_unreg_buffer_funcs;
343 	}
344 
345 	ret = iio_device_register(indio_dev);
346 	if (ret) {
347 		dev_err(&pdev->dev, "device register failed\n");
348 		goto error_remove_trigger;
349 	}
350 
351 	magn_state->callbacks.send_event = magn_3d_proc_event;
352 	magn_state->callbacks.capture_sample = magn_3d_capture_sample;
353 	magn_state->callbacks.pdev = pdev;
354 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D,
355 					&magn_state->callbacks);
356 	if (ret < 0) {
357 		dev_err(&pdev->dev, "callback reg failed\n");
358 		goto error_iio_unreg;
359 	}
360 
361 	return ret;
362 
363 error_iio_unreg:
364 	iio_device_unregister(indio_dev);
365 error_remove_trigger:
366 	hid_sensor_remove_trigger(&magn_state->common_attributes);
367 error_unreg_buffer_funcs:
368 	iio_triggered_buffer_cleanup(indio_dev);
369 error_free_dev_mem:
370 	kfree(indio_dev->channels);
371 	return ret;
372 }
373 
374 /* Function to deinitialize the processing for usage id */
375 static int hid_magn_3d_remove(struct platform_device *pdev)
376 {
377 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
378 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
379 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
380 
381 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D);
382 	iio_device_unregister(indio_dev);
383 	hid_sensor_remove_trigger(&magn_state->common_attributes);
384 	iio_triggered_buffer_cleanup(indio_dev);
385 	kfree(indio_dev->channels);
386 
387 	return 0;
388 }
389 
390 static struct platform_device_id hid_magn_3d_ids[] = {
391 	{
392 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
393 		.name = "HID-SENSOR-200083",
394 	},
395 	{ /* sentinel */ }
396 };
397 MODULE_DEVICE_TABLE(platform, hid_magn_3d_ids);
398 
399 static struct platform_driver hid_magn_3d_platform_driver = {
400 	.id_table = hid_magn_3d_ids,
401 	.driver = {
402 		.name	= KBUILD_MODNAME,
403 		.owner	= THIS_MODULE,
404 	},
405 	.probe		= hid_magn_3d_probe,
406 	.remove		= hid_magn_3d_remove,
407 };
408 module_platform_driver(hid_magn_3d_platform_driver);
409 
410 MODULE_DESCRIPTION("HID Sensor Magnetometer 3D");
411 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
412 MODULE_LICENSE("GPL");
413