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