xref: /linux/drivers/iio/light/hid-sensor-prox.c (revision ee8f7484f99db9fb80ad15af412dbf03eeb9ff57)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HID Sensors Driver
4  * Copyright (c) 2014, Intel Corporation.
5  */
6 #include <linux/bitops.h>
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/slab.h>
12 #include <linux/hid-sensor-hub.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/buffer.h>
15 #include "../common/hid-sensors/hid-sensor-trigger.h"
16 
17 static const u32 prox_usage_ids[] = {
18 	HID_USAGE_SENSOR_HUMAN_PRESENCE,
19 	HID_USAGE_SENSOR_HUMAN_PROXIMITY,
20 	HID_USAGE_SENSOR_HUMAN_ATTENTION,
21 };
22 
23 #define MAX_CHANNELS ARRAY_SIZE(prox_usage_ids)
24 
25 enum {
26 	HID_HUMAN_PRESENCE,
27 	HID_HUMAN_PROXIMITY,
28 	HID_HUMAN_ATTENTION,
29 };
30 
31 struct prox_state {
32 	struct hid_sensor_hub_callbacks callbacks;
33 	struct hid_sensor_common common_attributes;
34 	struct hid_sensor_hub_attribute_info prox_attr[MAX_CHANNELS];
35 	struct iio_chan_spec channels[MAX_CHANNELS];
36 	u32 channel2usage[MAX_CHANNELS];
37 	u32 human_presence[MAX_CHANNELS];
38 	int scale_pre_decml[MAX_CHANNELS];
39 	int scale_post_decml[MAX_CHANNELS];
40 	int scale_precision[MAX_CHANNELS];
41 	unsigned long scan_mask[2]; /* One entry plus one terminator. */
42 	int num_channels;
43 };
44 
45 static const u32 prox_sensitivity_addresses[] = {
46 	HID_USAGE_SENSOR_HUMAN_PRESENCE,
47 	HID_USAGE_SENSOR_DATA_PRESENCE,
48 };
49 
50 #define PROX_CHANNEL(_is_proximity, _channel) \
51 	{\
52 		.type = _is_proximity ? IIO_PROXIMITY : IIO_ATTENTION,\
53 		.info_mask_separate = \
54 		(_is_proximity ? BIT(IIO_CHAN_INFO_RAW) :\
55 				BIT(IIO_CHAN_INFO_PROCESSED)) |\
56 		BIT(IIO_CHAN_INFO_OFFSET) |\
57 		BIT(IIO_CHAN_INFO_SCALE) |\
58 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |\
59 		BIT(IIO_CHAN_INFO_HYSTERESIS),\
60 		.indexed = _is_proximity,\
61 		.channel = _channel,\
62 	}
63 
64 /* Channel definitions (same order as prox_usage_ids) */
65 static const struct iio_chan_spec prox_channels[] = {
66 	PROX_CHANNEL(true, HID_HUMAN_PRESENCE),
67 	PROX_CHANNEL(true, HID_HUMAN_PROXIMITY),
68 	PROX_CHANNEL(false, 0),
69 };
70 
71 /* Channel read_raw handler */
72 static int prox_read_raw(struct iio_dev *indio_dev,
73 			      struct iio_chan_spec const *chan,
74 			      int *val, int *val2,
75 			      long mask)
76 {
77 	struct prox_state *prox_state = iio_priv(indio_dev);
78 	struct hid_sensor_hub_device *hsdev;
79 	int report_id;
80 	u32 address;
81 	int ret_type;
82 	s32 min;
83 
84 	*val = 0;
85 	*val2 = 0;
86 	switch (mask) {
87 	case IIO_CHAN_INFO_RAW:
88 	case IIO_CHAN_INFO_PROCESSED:
89 		if (chan->scan_index >= prox_state->num_channels)
90 			return -EINVAL;
91 		address = prox_state->channel2usage[chan->scan_index];
92 		report_id = prox_state->prox_attr[chan->scan_index].report_id;
93 		hsdev = prox_state->common_attributes.hsdev;
94 		min = prox_state->prox_attr[chan->scan_index].logical_minimum;
95 		hid_sensor_power_state(&prox_state->common_attributes, true);
96 		*val = sensor_hub_input_attr_get_raw_value(hsdev,
97 							   hsdev->usage,
98 							   address,
99 							   report_id,
100 							   SENSOR_HUB_SYNC,
101 							   min < 0);
102 		if (prox_state->channel2usage[chan->scan_index] ==
103 		    HID_USAGE_SENSOR_HUMAN_ATTENTION)
104 			*val *= 100;
105 		hid_sensor_power_state(&prox_state->common_attributes, false);
106 		ret_type = IIO_VAL_INT;
107 		break;
108 	case IIO_CHAN_INFO_SCALE:
109 		if (chan->scan_index >= prox_state->num_channels)
110 			return -EINVAL;
111 
112 		*val = prox_state->scale_pre_decml[chan->scan_index];
113 		*val2 = prox_state->scale_post_decml[chan->scan_index];
114 		ret_type = prox_state->scale_precision[chan->scan_index];
115 		break;
116 	case IIO_CHAN_INFO_OFFSET:
117 		*val = 0;
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 				&prox_state->common_attributes, val, val2);
123 		break;
124 	case IIO_CHAN_INFO_HYSTERESIS:
125 		ret_type = hid_sensor_read_raw_hyst_value(
126 				&prox_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 prox_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 prox_state *prox_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 				&prox_state->common_attributes, val, val2);
150 		break;
151 	case IIO_CHAN_INFO_HYSTERESIS:
152 		ret = hid_sensor_write_raw_hyst_value(
153 				&prox_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 prox_info = {
163 	.read_raw = &prox_read_raw,
164 	.write_raw = &prox_write_raw,
165 };
166 
167 /* Callback handler to send event after all samples are received and captured */
168 static int prox_proc_event(struct hid_sensor_hub_device *hsdev,
169 				unsigned usage_id,
170 				void *priv)
171 {
172 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
173 	struct prox_state *prox_state = iio_priv(indio_dev);
174 
175 	dev_dbg(&indio_dev->dev, "prox_proc_event\n");
176 	if (atomic_read(&prox_state->common_attributes.data_ready)) {
177 		dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
178 		iio_push_to_buffers(indio_dev, &prox_state->human_presence);
179 	}
180 
181 	return 0;
182 }
183 
184 /* Capture samples in local storage */
185 static int prox_capture_sample(struct hid_sensor_hub_device *hsdev,
186 				unsigned usage_id,
187 				size_t raw_len, char *raw_data,
188 				void *priv)
189 {
190 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
191 	struct prox_state *prox_state = iio_priv(indio_dev);
192 	int multiplier = 1;
193 	int chan;
194 
195 	for (chan = 0; chan < prox_state->num_channels; chan++)
196 		if (prox_state->channel2usage[chan] == usage_id)
197 			break;
198 	if (chan == prox_state->num_channels)
199 		return -EINVAL;
200 
201 	if (usage_id == HID_USAGE_SENSOR_HUMAN_ATTENTION)
202 		multiplier = 100;
203 
204 	switch (raw_len) {
205 	case 1:
206 		prox_state->human_presence[chan] = *(u8 *)raw_data * multiplier;
207 		return 0;
208 	case 2:
209 		prox_state->human_presence[chan] = *(u16 *)raw_data * multiplier;
210 		return 0;
211 	case 4:
212 		prox_state->human_presence[chan] = *(u32 *)raw_data * multiplier;
213 		return 0;
214 	}
215 
216 	return -EINVAL;
217 }
218 
219 /* Parse report which is specific to an usage id*/
220 static int prox_parse_report(struct platform_device *pdev,
221 				struct hid_sensor_hub_device *hsdev,
222 				struct prox_state *st)
223 {
224 	struct iio_chan_spec *channels = st->channels;
225 	int index = 0;
226 	int ret;
227 	int i;
228 
229 	for (i = 0; i < MAX_CHANNELS; i++) {
230 		u32 usage_id = prox_usage_ids[i];
231 
232 		ret = sensor_hub_input_get_attribute_info(hsdev,
233 							  HID_INPUT_REPORT,
234 							  hsdev->usage,
235 							  usage_id,
236 							  &st->prox_attr[index]);
237 		if (ret < 0)
238 			continue;
239 		st->channel2usage[index] = usage_id;
240 		st->scan_mask[0] |= BIT(index);
241 		channels[index] = prox_channels[i];
242 		channels[index].scan_index = index;
243 		channels[index].scan_type = (struct iio_scan_type) {
244 			.format = 's',
245 			.realbits = BYTES_TO_BITS(st->prox_attr[index].size),
246 			.storagebits = BITS_PER_TYPE(u32),
247 		};
248 		dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr[index].index,
249 			st->prox_attr[index].report_id);
250 		st->scale_precision[index] =
251 			hid_sensor_format_scale(usage_id, &st->prox_attr[index],
252 						&st->scale_pre_decml[index],
253 						&st->scale_post_decml[index]);
254 		index++;
255 	}
256 
257 	if (!index)
258 		return ret;
259 
260 	st->num_channels = index;
261 
262 	return 0;
263 }
264 
265 /* Function to initialize the processing for usage id */
266 static int hid_prox_probe(struct platform_device *pdev)
267 {
268 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
269 	int ret = 0;
270 	static const char *name = "prox";
271 	struct iio_dev *indio_dev;
272 	struct prox_state *prox_state;
273 
274 	indio_dev = devm_iio_device_alloc(&pdev->dev,
275 				sizeof(struct prox_state));
276 	if (!indio_dev)
277 		return -ENOMEM;
278 	platform_set_drvdata(pdev, indio_dev);
279 
280 	prox_state = iio_priv(indio_dev);
281 	prox_state->common_attributes.hsdev = hsdev;
282 	prox_state->common_attributes.pdev = pdev;
283 
284 	ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
285 					&prox_state->common_attributes,
286 					prox_sensitivity_addresses,
287 					ARRAY_SIZE(prox_sensitivity_addresses));
288 	if (ret) {
289 		dev_err(&pdev->dev, "failed to setup common attributes\n");
290 		return ret;
291 	}
292 
293 	ret = prox_parse_report(pdev, hsdev, prox_state);
294 	if (ret) {
295 		dev_err(&pdev->dev, "failed to setup attributes\n");
296 		return ret;
297 	}
298 
299 	indio_dev->num_channels = prox_state->num_channels;
300 	indio_dev->channels = prox_state->channels;
301 	indio_dev->available_scan_masks = prox_state->scan_mask;
302 	indio_dev->info = &prox_info;
303 	indio_dev->name = name;
304 	indio_dev->modes = INDIO_DIRECT_MODE;
305 
306 	atomic_set(&prox_state->common_attributes.data_ready, 0);
307 
308 	ret = hid_sensor_setup_trigger(indio_dev, name,
309 				&prox_state->common_attributes);
310 	if (ret) {
311 		dev_err(&pdev->dev, "trigger setup failed\n");
312 		return ret;
313 	}
314 
315 	ret = iio_device_register(indio_dev);
316 	if (ret) {
317 		dev_err(&pdev->dev, "device register failed\n");
318 		goto error_remove_trigger;
319 	}
320 
321 	prox_state->callbacks.send_event = prox_proc_event;
322 	prox_state->callbacks.capture_sample = prox_capture_sample;
323 	prox_state->callbacks.pdev = pdev;
324 	ret = sensor_hub_register_callback(hsdev, hsdev->usage,
325 					   &prox_state->callbacks);
326 	if (ret < 0) {
327 		dev_err(&pdev->dev, "callback reg failed\n");
328 		goto error_iio_unreg;
329 	}
330 
331 	return ret;
332 
333 error_iio_unreg:
334 	iio_device_unregister(indio_dev);
335 error_remove_trigger:
336 	hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
337 	return ret;
338 }
339 
340 /* Function to deinitialize the processing for usage id */
341 static void hid_prox_remove(struct platform_device *pdev)
342 {
343 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
344 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
345 	struct prox_state *prox_state = iio_priv(indio_dev);
346 
347 	sensor_hub_remove_callback(hsdev, hsdev->usage);
348 	iio_device_unregister(indio_dev);
349 	hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
350 }
351 
352 static const struct platform_device_id hid_prox_ids[] = {
353 	{
354 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
355 		.name = "HID-SENSOR-200011",
356 	},
357 	{
358 		/* Format: HID-SENSOR-tag-usage_id_in_hex_lowercase */
359 		.name = "HID-SENSOR-LISS-0226",
360 	},
361 	{ }
362 };
363 MODULE_DEVICE_TABLE(platform, hid_prox_ids);
364 
365 static struct platform_driver hid_prox_platform_driver = {
366 	.id_table = hid_prox_ids,
367 	.driver = {
368 		.name	= KBUILD_MODNAME,
369 		.pm	= &hid_sensor_pm_ops,
370 	},
371 	.probe		= hid_prox_probe,
372 	.remove		= hid_prox_remove,
373 };
374 module_platform_driver(hid_prox_platform_driver);
375 
376 MODULE_DESCRIPTION("HID Sensor Proximity");
377 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
378 MODULE_LICENSE("GPL");
379 MODULE_IMPORT_NS("IIO_HID");
380