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