xref: /linux/drivers/iio/light/hid-sensor-prox.c (revision 2cc699b3c2fe5ea84081059c88c46373cbebe630)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HID Sensors Driver
4  * Copyright (c) 2014, Intel Corporation.
5  */
6 #include <linux/device.h>
7 #include <linux/platform_device.h>
8 #include <linux/module.h>
9 #include <linux/mod_devicetable.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;
38 	int scale_post_decml;
39 	int scale_precision;
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 /* Adjust channel real bits based on report descriptor */
prox_adjust_channel_bit_mask(struct iio_chan_spec * channels,int channel,int size)71 static void prox_adjust_channel_bit_mask(struct iio_chan_spec *channels,
72 					int channel, int size)
73 {
74 	channels[channel].scan_type.sign = 's';
75 	/* Real storage bits will change based on the report desc. */
76 	channels[channel].scan_type.realbits = size * 8;
77 	/* Maximum size of a sample to capture is u32 */
78 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
79 }
80 
81 /* Channel read_raw handler */
prox_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)82 static int prox_read_raw(struct iio_dev *indio_dev,
83 			      struct iio_chan_spec const *chan,
84 			      int *val, int *val2,
85 			      long mask)
86 {
87 	struct prox_state *prox_state = iio_priv(indio_dev);
88 	struct hid_sensor_hub_device *hsdev;
89 	int report_id;
90 	u32 address;
91 	int ret_type;
92 	s32 min;
93 
94 	*val = 0;
95 	*val2 = 0;
96 	switch (mask) {
97 	case IIO_CHAN_INFO_RAW:
98 	case IIO_CHAN_INFO_PROCESSED:
99 		if (chan->scan_index >= prox_state->num_channels)
100 			return -EINVAL;
101 		address = prox_state->channel2usage[chan->scan_index];
102 		report_id = prox_state->prox_attr[chan->scan_index].report_id;
103 		hsdev = prox_state->common_attributes.hsdev;
104 		min = prox_state->prox_attr[chan->scan_index].logical_minimum;
105 		hid_sensor_power_state(&prox_state->common_attributes, true);
106 		*val = sensor_hub_input_attr_get_raw_value(hsdev,
107 							   hsdev->usage,
108 							   address,
109 							   report_id,
110 							   SENSOR_HUB_SYNC,
111 							   min < 0);
112 		if (prox_state->channel2usage[chan->scan_index] ==
113 		    HID_USAGE_SENSOR_HUMAN_ATTENTION)
114 			*val *= 100;
115 		hid_sensor_power_state(&prox_state->common_attributes, false);
116 		ret_type = IIO_VAL_INT;
117 		break;
118 	case IIO_CHAN_INFO_SCALE:
119 		*val = prox_state->scale_pre_decml;
120 		*val2 = prox_state->scale_post_decml;
121 		ret_type = prox_state->scale_precision;
122 		break;
123 	case IIO_CHAN_INFO_OFFSET:
124 		*val = hid_sensor_convert_exponent(
125 			prox_state->prox_attr[chan->scan_index].unit_expo);
126 		ret_type = IIO_VAL_INT;
127 		break;
128 	case IIO_CHAN_INFO_SAMP_FREQ:
129 		ret_type = hid_sensor_read_samp_freq_value(
130 				&prox_state->common_attributes, val, val2);
131 		break;
132 	case IIO_CHAN_INFO_HYSTERESIS:
133 		ret_type = hid_sensor_read_raw_hyst_value(
134 				&prox_state->common_attributes, val, val2);
135 		break;
136 	default:
137 		ret_type = -EINVAL;
138 		break;
139 	}
140 
141 	return ret_type;
142 }
143 
144 /* Channel write_raw handler */
prox_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)145 static int prox_write_raw(struct iio_dev *indio_dev,
146 			       struct iio_chan_spec const *chan,
147 			       int val,
148 			       int val2,
149 			       long mask)
150 {
151 	struct prox_state *prox_state = iio_priv(indio_dev);
152 	int ret = 0;
153 
154 	switch (mask) {
155 	case IIO_CHAN_INFO_SAMP_FREQ:
156 		ret = hid_sensor_write_samp_freq_value(
157 				&prox_state->common_attributes, val, val2);
158 		break;
159 	case IIO_CHAN_INFO_HYSTERESIS:
160 		ret = hid_sensor_write_raw_hyst_value(
161 				&prox_state->common_attributes, val, val2);
162 		break;
163 	default:
164 		ret = -EINVAL;
165 	}
166 
167 	return ret;
168 }
169 
170 static const struct iio_info prox_info = {
171 	.read_raw = &prox_read_raw,
172 	.write_raw = &prox_write_raw,
173 };
174 
175 /* Callback handler to send event after all samples are received and captured */
prox_proc_event(struct hid_sensor_hub_device * hsdev,unsigned usage_id,void * priv)176 static int prox_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 prox_state *prox_state = iio_priv(indio_dev);
182 
183 	dev_dbg(&indio_dev->dev, "prox_proc_event\n");
184 	if (atomic_read(&prox_state->common_attributes.data_ready)) {
185 		dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
186 		iio_push_to_buffers(indio_dev, &prox_state->human_presence);
187 	}
188 
189 	return 0;
190 }
191 
192 /* Capture samples in local storage */
prox_capture_sample(struct hid_sensor_hub_device * hsdev,unsigned usage_id,size_t raw_len,char * raw_data,void * priv)193 static int prox_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 prox_state *prox_state = iio_priv(indio_dev);
200 	int multiplier = 1;
201 	int chan;
202 
203 	for (chan = 0; chan < prox_state->num_channels; chan++)
204 		if (prox_state->channel2usage[chan] == usage_id)
205 			break;
206 	if (chan == prox_state->num_channels)
207 		return -EINVAL;
208 
209 	if (usage_id == HID_USAGE_SENSOR_HUMAN_ATTENTION)
210 		multiplier = 100;
211 
212 	switch (raw_len) {
213 	case 1:
214 		prox_state->human_presence[chan] = *(u8 *)raw_data * multiplier;
215 		return 0;
216 	case 4:
217 		prox_state->human_presence[chan] = *(u32 *)raw_data * multiplier;
218 		return 0;
219 	}
220 
221 	return -EINVAL;
222 }
223 
224 /* Parse report which is specific to an usage id*/
prox_parse_report(struct platform_device * pdev,struct hid_sensor_hub_device * hsdev,struct prox_state * st)225 static int prox_parse_report(struct platform_device *pdev,
226 				struct hid_sensor_hub_device *hsdev,
227 				struct prox_state *st)
228 {
229 	struct iio_chan_spec *channels = st->channels;
230 	int index = 0;
231 	int ret;
232 	int i;
233 
234 	for (i = 0; i < MAX_CHANNELS; i++) {
235 		u32 usage_id = prox_usage_ids[i];
236 
237 		ret = sensor_hub_input_get_attribute_info(hsdev,
238 							  HID_INPUT_REPORT,
239 							  hsdev->usage,
240 							  usage_id,
241 							  &st->prox_attr[index]);
242 		if (ret < 0)
243 			continue;
244 		st->channel2usage[index] = usage_id;
245 		st->scan_mask[0] |= BIT(index);
246 		channels[index] = prox_channels[i];
247 		channels[index].scan_index = index;
248 		prox_adjust_channel_bit_mask(channels, index,
249 					     st->prox_attr[index].size);
250 		dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr[index].index,
251 			st->prox_attr[index].report_id);
252 		index++;
253 	}
254 
255 	if (!index)
256 		return ret;
257 
258 	st->num_channels = index;
259 
260 	return 0;
261 }
262 
263 /* Function to initialize the processing for usage id */
hid_prox_probe(struct platform_device * pdev)264 static int hid_prox_probe(struct platform_device *pdev)
265 {
266 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
267 	int ret = 0;
268 	static const char *name = "prox";
269 	struct iio_dev *indio_dev;
270 	struct prox_state *prox_state;
271 
272 	indio_dev = devm_iio_device_alloc(&pdev->dev,
273 				sizeof(struct prox_state));
274 	if (!indio_dev)
275 		return -ENOMEM;
276 	platform_set_drvdata(pdev, indio_dev);
277 
278 	prox_state = iio_priv(indio_dev);
279 	prox_state->common_attributes.hsdev = hsdev;
280 	prox_state->common_attributes.pdev = pdev;
281 
282 	ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
283 					&prox_state->common_attributes,
284 					prox_sensitivity_addresses,
285 					ARRAY_SIZE(prox_sensitivity_addresses));
286 	if (ret) {
287 		dev_err(&pdev->dev, "failed to setup common attributes\n");
288 		return ret;
289 	}
290 
291 	ret = prox_parse_report(pdev, hsdev, prox_state);
292 	if (ret) {
293 		dev_err(&pdev->dev, "failed to setup attributes\n");
294 		return ret;
295 	}
296 
297 	indio_dev->num_channels = prox_state->num_channels;
298 	indio_dev->channels = prox_state->channels;
299 	indio_dev->available_scan_masks = prox_state->scan_mask;
300 	indio_dev->info = &prox_info;
301 	indio_dev->name = name;
302 	indio_dev->modes = INDIO_DIRECT_MODE;
303 
304 	atomic_set(&prox_state->common_attributes.data_ready, 0);
305 
306 	ret = hid_sensor_setup_trigger(indio_dev, name,
307 				&prox_state->common_attributes);
308 	if (ret) {
309 		dev_err(&pdev->dev, "trigger setup failed\n");
310 		return ret;
311 	}
312 
313 	ret = iio_device_register(indio_dev);
314 	if (ret) {
315 		dev_err(&pdev->dev, "device register failed\n");
316 		goto error_remove_trigger;
317 	}
318 
319 	prox_state->callbacks.send_event = prox_proc_event;
320 	prox_state->callbacks.capture_sample = prox_capture_sample;
321 	prox_state->callbacks.pdev = pdev;
322 	ret = sensor_hub_register_callback(hsdev, hsdev->usage,
323 					   &prox_state->callbacks);
324 	if (ret < 0) {
325 		dev_err(&pdev->dev, "callback reg failed\n");
326 		goto error_iio_unreg;
327 	}
328 
329 	return ret;
330 
331 error_iio_unreg:
332 	iio_device_unregister(indio_dev);
333 error_remove_trigger:
334 	hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
335 	return ret;
336 }
337 
338 /* Function to deinitialize the processing for usage id */
hid_prox_remove(struct platform_device * pdev)339 static void hid_prox_remove(struct platform_device *pdev)
340 {
341 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
342 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
343 	struct prox_state *prox_state = iio_priv(indio_dev);
344 
345 	sensor_hub_remove_callback(hsdev, hsdev->usage);
346 	iio_device_unregister(indio_dev);
347 	hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
348 }
349 
350 static const struct platform_device_id hid_prox_ids[] = {
351 	{
352 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
353 		.name = "HID-SENSOR-200011",
354 	},
355 	{
356 		/* Format: HID-SENSOR-tag-usage_id_in_hex_lowercase */
357 		.name = "HID-SENSOR-LISS-0226",
358 	},
359 	{ /* sentinel */ }
360 };
361 MODULE_DEVICE_TABLE(platform, hid_prox_ids);
362 
363 static struct platform_driver hid_prox_platform_driver = {
364 	.id_table = hid_prox_ids,
365 	.driver = {
366 		.name	= KBUILD_MODNAME,
367 		.pm	= &hid_sensor_pm_ops,
368 	},
369 	.probe		= hid_prox_probe,
370 	.remove		= hid_prox_remove,
371 };
372 module_platform_driver(hid_prox_platform_driver);
373 
374 MODULE_DESCRIPTION("HID Sensor Proximity");
375 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
376 MODULE_LICENSE("GPL");
377 MODULE_IMPORT_NS("IIO_HID");
378