1 /*
2  * HID Sensors Driver
3  * Copyright (c) 2014, 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.
16  *
17  */
18 #include <linux/device.h>
19 #include <linux/platform_device.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/slab.h>
24 #include <linux/delay.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 #define CHANNEL_SCAN_INDEX_PRESSURE 0
34 
35 struct press_state {
36 	struct hid_sensor_hub_callbacks callbacks;
37 	struct hid_sensor_common common_attributes;
38 	struct hid_sensor_hub_attribute_info press_attr;
39 	u32 press_data;
40 	int scale_pre_decml;
41 	int scale_post_decml;
42 	int scale_precision;
43 	int value_offset;
44 };
45 
46 /* Channel definitions */
47 static const struct iio_chan_spec press_channels[] = {
48 	{
49 		.type = IIO_PRESSURE,
50 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
51 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
52 		BIT(IIO_CHAN_INFO_SCALE) |
53 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
54 		BIT(IIO_CHAN_INFO_HYSTERESIS),
55 		.scan_index = CHANNEL_SCAN_INDEX_PRESSURE,
56 	}
57 };
58 
59 /* Adjust channel real bits based on report descriptor */
60 static void press_adjust_channel_bit_mask(struct iio_chan_spec *channels,
61 					int channel, int size)
62 {
63 	channels[channel].scan_type.sign = 's';
64 	/* Real storage bits will change based on the report desc. */
65 	channels[channel].scan_type.realbits = size * 8;
66 	/* Maximum size of a sample to capture is u32 */
67 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
68 }
69 
70 /* Channel read_raw handler */
71 static int press_read_raw(struct iio_dev *indio_dev,
72 			      struct iio_chan_spec const *chan,
73 			      int *val, int *val2,
74 			      long mask)
75 {
76 	struct press_state *press_state = iio_priv(indio_dev);
77 	int report_id = -1;
78 	u32 address;
79 	int ret_type;
80 
81 	*val = 0;
82 	*val2 = 0;
83 	switch (mask) {
84 	case IIO_CHAN_INFO_RAW:
85 		switch (chan->scan_index) {
86 		case  CHANNEL_SCAN_INDEX_PRESSURE:
87 			report_id = press_state->press_attr.report_id;
88 			address =
89 			HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE;
90 			break;
91 		default:
92 			report_id = -1;
93 			break;
94 		}
95 		if (report_id >= 0) {
96 			hid_sensor_power_state(&press_state->common_attributes,
97 						true);
98 			*val = sensor_hub_input_attr_get_raw_value(
99 				press_state->common_attributes.hsdev,
100 				HID_USAGE_SENSOR_PRESSURE, address,
101 				report_id,
102 				SENSOR_HUB_SYNC);
103 			hid_sensor_power_state(&press_state->common_attributes,
104 						false);
105 		} else {
106 			*val = 0;
107 			return -EINVAL;
108 		}
109 		ret_type = IIO_VAL_INT;
110 		break;
111 	case IIO_CHAN_INFO_SCALE:
112 		*val = press_state->scale_pre_decml;
113 		*val2 = press_state->scale_post_decml;
114 		ret_type = press_state->scale_precision;
115 		break;
116 	case IIO_CHAN_INFO_OFFSET:
117 		*val = press_state->value_offset;
118 		ret_type = IIO_VAL_INT;
119 		break;
120 	case IIO_CHAN_INFO_SAMP_FREQ:
121 		ret_type = hid_sensor_read_samp_freq_value(
122 				&press_state->common_attributes, val, val2);
123 		break;
124 	case IIO_CHAN_INFO_HYSTERESIS:
125 		ret_type = hid_sensor_read_raw_hyst_value(
126 				&press_state->common_attributes, val, val2);
127 		break;
128 	default:
129 		ret_type = -EINVAL;
130 		break;
131 	}
132 
133 	return ret_type;
134 }
135 
136 /* Channel write_raw handler */
137 static int press_write_raw(struct iio_dev *indio_dev,
138 			       struct iio_chan_spec const *chan,
139 			       int val,
140 			       int val2,
141 			       long mask)
142 {
143 	struct press_state *press_state = iio_priv(indio_dev);
144 	int ret = 0;
145 
146 	switch (mask) {
147 	case IIO_CHAN_INFO_SAMP_FREQ:
148 		ret = hid_sensor_write_samp_freq_value(
149 				&press_state->common_attributes, val, val2);
150 		break;
151 	case IIO_CHAN_INFO_HYSTERESIS:
152 		ret = hid_sensor_write_raw_hyst_value(
153 				&press_state->common_attributes, val, val2);
154 		break;
155 	default:
156 		ret = -EINVAL;
157 	}
158 
159 	return ret;
160 }
161 
162 static const struct iio_info press_info = {
163 	.read_raw = &press_read_raw,
164 	.write_raw = &press_write_raw,
165 };
166 
167 /* Function to push data to buffer */
168 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
169 					int len)
170 {
171 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
172 	iio_push_to_buffers(indio_dev, data);
173 }
174 
175 /* Callback handler to send event after all samples are received and captured */
176 static int press_proc_event(struct hid_sensor_hub_device *hsdev,
177 				unsigned usage_id,
178 				void *priv)
179 {
180 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
181 	struct press_state *press_state = iio_priv(indio_dev);
182 
183 	dev_dbg(&indio_dev->dev, "press_proc_event\n");
184 	if (atomic_read(&press_state->common_attributes.data_ready))
185 		hid_sensor_push_data(indio_dev,
186 				&press_state->press_data,
187 				sizeof(press_state->press_data));
188 
189 	return 0;
190 }
191 
192 /* Capture samples in local storage */
193 static int press_capture_sample(struct hid_sensor_hub_device *hsdev,
194 				unsigned usage_id,
195 				size_t raw_len, char *raw_data,
196 				void *priv)
197 {
198 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
199 	struct press_state *press_state = iio_priv(indio_dev);
200 	int ret = -EINVAL;
201 
202 	switch (usage_id) {
203 	case HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE:
204 		press_state->press_data = *(u32 *)raw_data;
205 		ret = 0;
206 		break;
207 	default:
208 		break;
209 	}
210 
211 	return ret;
212 }
213 
214 /* Parse report which is specific to an usage id*/
215 static int press_parse_report(struct platform_device *pdev,
216 				struct hid_sensor_hub_device *hsdev,
217 				struct iio_chan_spec *channels,
218 				unsigned usage_id,
219 				struct press_state *st)
220 {
221 	int ret;
222 
223 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
224 			usage_id,
225 			HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE,
226 			&st->press_attr);
227 	if (ret < 0)
228 		return ret;
229 	press_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESSURE,
230 					st->press_attr.size);
231 
232 	dev_dbg(&pdev->dev, "press %x:%x\n", st->press_attr.index,
233 			st->press_attr.report_id);
234 
235 	st->scale_precision = hid_sensor_format_scale(
236 				HID_USAGE_SENSOR_PRESSURE,
237 				&st->press_attr,
238 				&st->scale_pre_decml, &st->scale_post_decml);
239 
240 	/* Set Sensitivity field ids, when there is no individual modifier */
241 	if (st->common_attributes.sensitivity.index < 0) {
242 		sensor_hub_input_get_attribute_info(hsdev,
243 			HID_FEATURE_REPORT, usage_id,
244 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
245 			HID_USAGE_SENSOR_DATA_ATMOSPHERIC_PRESSURE,
246 			&st->common_attributes.sensitivity);
247 		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
248 			st->common_attributes.sensitivity.index,
249 			st->common_attributes.sensitivity.report_id);
250 	}
251 	return ret;
252 }
253 
254 /* Function to initialize the processing for usage id */
255 static int hid_press_probe(struct platform_device *pdev)
256 {
257 	int ret = 0;
258 	static const char *name = "press";
259 	struct iio_dev *indio_dev;
260 	struct press_state *press_state;
261 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
262 
263 	indio_dev = devm_iio_device_alloc(&pdev->dev,
264 				sizeof(struct press_state));
265 	if (!indio_dev)
266 		return -ENOMEM;
267 	platform_set_drvdata(pdev, indio_dev);
268 
269 	press_state = iio_priv(indio_dev);
270 	press_state->common_attributes.hsdev = hsdev;
271 	press_state->common_attributes.pdev = pdev;
272 
273 	ret = hid_sensor_parse_common_attributes(hsdev,
274 					HID_USAGE_SENSOR_PRESSURE,
275 					&press_state->common_attributes);
276 	if (ret) {
277 		dev_err(&pdev->dev, "failed to setup common attributes\n");
278 		return ret;
279 	}
280 
281 	indio_dev->channels = kmemdup(press_channels, sizeof(press_channels),
282 				      GFP_KERNEL);
283 	if (!indio_dev->channels) {
284 		dev_err(&pdev->dev, "failed to duplicate channels\n");
285 		return -ENOMEM;
286 	}
287 
288 	ret = press_parse_report(pdev, hsdev,
289 				 (struct iio_chan_spec *)indio_dev->channels,
290 				 HID_USAGE_SENSOR_PRESSURE, press_state);
291 	if (ret) {
292 		dev_err(&pdev->dev, "failed to setup attributes\n");
293 		goto error_free_dev_mem;
294 	}
295 
296 	indio_dev->num_channels =
297 				ARRAY_SIZE(press_channels);
298 	indio_dev->dev.parent = &pdev->dev;
299 	indio_dev->info = &press_info;
300 	indio_dev->name = name;
301 	indio_dev->modes = INDIO_DIRECT_MODE;
302 
303 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
304 		NULL, NULL);
305 	if (ret) {
306 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
307 		goto error_free_dev_mem;
308 	}
309 	atomic_set(&press_state->common_attributes.data_ready, 0);
310 	ret = hid_sensor_setup_trigger(indio_dev, name,
311 				&press_state->common_attributes);
312 	if (ret) {
313 		dev_err(&pdev->dev, "trigger setup failed\n");
314 		goto error_unreg_buffer_funcs;
315 	}
316 
317 	ret = iio_device_register(indio_dev);
318 	if (ret) {
319 		dev_err(&pdev->dev, "device register failed\n");
320 		goto error_remove_trigger;
321 	}
322 
323 	press_state->callbacks.send_event = press_proc_event;
324 	press_state->callbacks.capture_sample = press_capture_sample;
325 	press_state->callbacks.pdev = pdev;
326 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PRESSURE,
327 					&press_state->callbacks);
328 	if (ret < 0) {
329 		dev_err(&pdev->dev, "callback reg failed\n");
330 		goto error_iio_unreg;
331 	}
332 
333 	return ret;
334 
335 error_iio_unreg:
336 	iio_device_unregister(indio_dev);
337 error_remove_trigger:
338 	hid_sensor_remove_trigger(&press_state->common_attributes);
339 error_unreg_buffer_funcs:
340 	iio_triggered_buffer_cleanup(indio_dev);
341 error_free_dev_mem:
342 	kfree(indio_dev->channels);
343 	return ret;
344 }
345 
346 /* Function to deinitialize the processing for usage id */
347 static int hid_press_remove(struct platform_device *pdev)
348 {
349 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
350 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
351 	struct press_state *press_state = iio_priv(indio_dev);
352 
353 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PRESSURE);
354 	iio_device_unregister(indio_dev);
355 	hid_sensor_remove_trigger(&press_state->common_attributes);
356 	iio_triggered_buffer_cleanup(indio_dev);
357 	kfree(indio_dev->channels);
358 
359 	return 0;
360 }
361 
362 static const struct platform_device_id hid_press_ids[] = {
363 	{
364 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
365 		.name = "HID-SENSOR-200031",
366 	},
367 	{ /* sentinel */ }
368 };
369 MODULE_DEVICE_TABLE(platform, hid_press_ids);
370 
371 static struct platform_driver hid_press_platform_driver = {
372 	.id_table = hid_press_ids,
373 	.driver = {
374 		.name	= KBUILD_MODNAME,
375 		.pm	= &hid_sensor_pm_ops,
376 	},
377 	.probe		= hid_press_probe,
378 	.remove		= hid_press_remove,
379 };
380 module_platform_driver(hid_press_platform_driver);
381 
382 MODULE_DESCRIPTION("HID Sensor Pressure");
383 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
384 MODULE_LICENSE("GPL");
385