xref: /linux/drivers/iio/light/hid-sensor-prox.c (revision c26f4fbd58375bd6ef74f95eb73d61762ad97c59)
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[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 /* 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 		if (chan->scan_index >= prox_state->num_channels)
120 			return -EINVAL;
121 
122 		*val = prox_state->scale_pre_decml[chan->scan_index];
123 		*val2 = prox_state->scale_post_decml[chan->scan_index];
124 		ret_type = prox_state->scale_precision[chan->scan_index];
125 		break;
126 	case IIO_CHAN_INFO_OFFSET:
127 		*val = 0;
128 		ret_type = IIO_VAL_INT;
129 		break;
130 	case IIO_CHAN_INFO_SAMP_FREQ:
131 		ret_type = hid_sensor_read_samp_freq_value(
132 				&prox_state->common_attributes, val, val2);
133 		break;
134 	case IIO_CHAN_INFO_HYSTERESIS:
135 		ret_type = hid_sensor_read_raw_hyst_value(
136 				&prox_state->common_attributes, val, val2);
137 		break;
138 	default:
139 		ret_type = -EINVAL;
140 		break;
141 	}
142 
143 	return ret_type;
144 }
145 
146 /* Channel write_raw handler */
prox_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)147 static int prox_write_raw(struct iio_dev *indio_dev,
148 			       struct iio_chan_spec const *chan,
149 			       int val,
150 			       int val2,
151 			       long mask)
152 {
153 	struct prox_state *prox_state = iio_priv(indio_dev);
154 	int ret = 0;
155 
156 	switch (mask) {
157 	case IIO_CHAN_INFO_SAMP_FREQ:
158 		ret = hid_sensor_write_samp_freq_value(
159 				&prox_state->common_attributes, val, val2);
160 		break;
161 	case IIO_CHAN_INFO_HYSTERESIS:
162 		ret = hid_sensor_write_raw_hyst_value(
163 				&prox_state->common_attributes, val, val2);
164 		break;
165 	default:
166 		ret = -EINVAL;
167 	}
168 
169 	return ret;
170 }
171 
172 static const struct iio_info prox_info = {
173 	.read_raw = &prox_read_raw,
174 	.write_raw = &prox_write_raw,
175 };
176 
177 /* 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)178 static int prox_proc_event(struct hid_sensor_hub_device *hsdev,
179 				unsigned usage_id,
180 				void *priv)
181 {
182 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
183 	struct prox_state *prox_state = iio_priv(indio_dev);
184 
185 	dev_dbg(&indio_dev->dev, "prox_proc_event\n");
186 	if (atomic_read(&prox_state->common_attributes.data_ready)) {
187 		dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
188 		iio_push_to_buffers(indio_dev, &prox_state->human_presence);
189 	}
190 
191 	return 0;
192 }
193 
194 /* 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)195 static int prox_capture_sample(struct hid_sensor_hub_device *hsdev,
196 				unsigned usage_id,
197 				size_t raw_len, char *raw_data,
198 				void *priv)
199 {
200 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
201 	struct prox_state *prox_state = iio_priv(indio_dev);
202 	int multiplier = 1;
203 	int chan;
204 
205 	for (chan = 0; chan < prox_state->num_channels; chan++)
206 		if (prox_state->channel2usage[chan] == usage_id)
207 			break;
208 	if (chan == prox_state->num_channels)
209 		return -EINVAL;
210 
211 	if (usage_id == HID_USAGE_SENSOR_HUMAN_ATTENTION)
212 		multiplier = 100;
213 
214 	switch (raw_len) {
215 	case 1:
216 		prox_state->human_presence[chan] = *(u8 *)raw_data * multiplier;
217 		return 0;
218 	case 2:
219 		prox_state->human_presence[chan] = *(u16 *)raw_data * multiplier;
220 		return 0;
221 	case 4:
222 		prox_state->human_presence[chan] = *(u32 *)raw_data * multiplier;
223 		return 0;
224 	}
225 
226 	return -EINVAL;
227 }
228 
229 /* 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)230 static int prox_parse_report(struct platform_device *pdev,
231 				struct hid_sensor_hub_device *hsdev,
232 				struct prox_state *st)
233 {
234 	struct iio_chan_spec *channels = st->channels;
235 	int index = 0;
236 	int ret;
237 	int i;
238 
239 	for (i = 0; i < MAX_CHANNELS; i++) {
240 		u32 usage_id = prox_usage_ids[i];
241 
242 		ret = sensor_hub_input_get_attribute_info(hsdev,
243 							  HID_INPUT_REPORT,
244 							  hsdev->usage,
245 							  usage_id,
246 							  &st->prox_attr[index]);
247 		if (ret < 0)
248 			continue;
249 		st->channel2usage[index] = usage_id;
250 		st->scan_mask[0] |= BIT(index);
251 		channels[index] = prox_channels[i];
252 		channels[index].scan_index = index;
253 		prox_adjust_channel_bit_mask(channels, index,
254 					     st->prox_attr[index].size);
255 		dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr[index].index,
256 			st->prox_attr[index].report_id);
257 		st->scale_precision[index] =
258 			hid_sensor_format_scale(usage_id, &st->prox_attr[index],
259 						&st->scale_pre_decml[index],
260 						&st->scale_post_decml[index]);
261 		index++;
262 	}
263 
264 	if (!index)
265 		return ret;
266 
267 	st->num_channels = index;
268 
269 	return 0;
270 }
271 
272 /* Function to initialize the processing for usage id */
hid_prox_probe(struct platform_device * pdev)273 static int hid_prox_probe(struct platform_device *pdev)
274 {
275 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
276 	int ret = 0;
277 	static const char *name = "prox";
278 	struct iio_dev *indio_dev;
279 	struct prox_state *prox_state;
280 
281 	indio_dev = devm_iio_device_alloc(&pdev->dev,
282 				sizeof(struct prox_state));
283 	if (!indio_dev)
284 		return -ENOMEM;
285 	platform_set_drvdata(pdev, indio_dev);
286 
287 	prox_state = iio_priv(indio_dev);
288 	prox_state->common_attributes.hsdev = hsdev;
289 	prox_state->common_attributes.pdev = pdev;
290 
291 	ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
292 					&prox_state->common_attributes,
293 					prox_sensitivity_addresses,
294 					ARRAY_SIZE(prox_sensitivity_addresses));
295 	if (ret) {
296 		dev_err(&pdev->dev, "failed to setup common attributes\n");
297 		return ret;
298 	}
299 
300 	ret = prox_parse_report(pdev, hsdev, prox_state);
301 	if (ret) {
302 		dev_err(&pdev->dev, "failed to setup attributes\n");
303 		return ret;
304 	}
305 
306 	indio_dev->num_channels = prox_state->num_channels;
307 	indio_dev->channels = prox_state->channels;
308 	indio_dev->available_scan_masks = prox_state->scan_mask;
309 	indio_dev->info = &prox_info;
310 	indio_dev->name = name;
311 	indio_dev->modes = INDIO_DIRECT_MODE;
312 
313 	atomic_set(&prox_state->common_attributes.data_ready, 0);
314 
315 	ret = hid_sensor_setup_trigger(indio_dev, name,
316 				&prox_state->common_attributes);
317 	if (ret) {
318 		dev_err(&pdev->dev, "trigger setup failed\n");
319 		return ret;
320 	}
321 
322 	ret = iio_device_register(indio_dev);
323 	if (ret) {
324 		dev_err(&pdev->dev, "device register failed\n");
325 		goto error_remove_trigger;
326 	}
327 
328 	prox_state->callbacks.send_event = prox_proc_event;
329 	prox_state->callbacks.capture_sample = prox_capture_sample;
330 	prox_state->callbacks.pdev = pdev;
331 	ret = sensor_hub_register_callback(hsdev, hsdev->usage,
332 					   &prox_state->callbacks);
333 	if (ret < 0) {
334 		dev_err(&pdev->dev, "callback reg failed\n");
335 		goto error_iio_unreg;
336 	}
337 
338 	return ret;
339 
340 error_iio_unreg:
341 	iio_device_unregister(indio_dev);
342 error_remove_trigger:
343 	hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
344 	return ret;
345 }
346 
347 /* Function to deinitialize the processing for usage id */
hid_prox_remove(struct platform_device * pdev)348 static void hid_prox_remove(struct platform_device *pdev)
349 {
350 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
351 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
352 	struct prox_state *prox_state = iio_priv(indio_dev);
353 
354 	sensor_hub_remove_callback(hsdev, hsdev->usage);
355 	iio_device_unregister(indio_dev);
356 	hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
357 }
358 
359 static const struct platform_device_id hid_prox_ids[] = {
360 	{
361 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
362 		.name = "HID-SENSOR-200011",
363 	},
364 	{
365 		/* Format: HID-SENSOR-tag-usage_id_in_hex_lowercase */
366 		.name = "HID-SENSOR-LISS-0226",
367 	},
368 	{ }
369 };
370 MODULE_DEVICE_TABLE(platform, hid_prox_ids);
371 
372 static struct platform_driver hid_prox_platform_driver = {
373 	.id_table = hid_prox_ids,
374 	.driver = {
375 		.name	= KBUILD_MODNAME,
376 		.pm	= &hid_sensor_pm_ops,
377 	},
378 	.probe		= hid_prox_probe,
379 	.remove		= hid_prox_remove,
380 };
381 module_platform_driver(hid_prox_platform_driver);
382 
383 MODULE_DESCRIPTION("HID Sensor Proximity");
384 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
385 MODULE_LICENSE("GPL");
386 MODULE_IMPORT_NS("IIO_HID");
387