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