1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HID Sensors Driver
4  * Copyright (c) 2012, 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 enum {
17 	CHANNEL_SCAN_INDEX_INTENSITY = 0,
18 	CHANNEL_SCAN_INDEX_ILLUM = 1,
19 	CHANNEL_SCAN_INDEX_MAX
20 };
21 
22 #define CHANNEL_SCAN_INDEX_TIMESTAMP CHANNEL_SCAN_INDEX_MAX
23 
24 struct als_state {
25 	struct hid_sensor_hub_callbacks callbacks;
26 	struct hid_sensor_common common_attributes;
27 	struct hid_sensor_hub_attribute_info als_illum;
28 	struct {
29 		u32 illum[CHANNEL_SCAN_INDEX_MAX];
30 		u64 timestamp __aligned(8);
31 	} scan;
32 	int scale_pre_decml;
33 	int scale_post_decml;
34 	int scale_precision;
35 	int value_offset;
36 	s64 timestamp;
37 };
38 
39 static const u32 als_sensitivity_addresses[] = {
40 	HID_USAGE_SENSOR_DATA_LIGHT,
41 	HID_USAGE_SENSOR_LIGHT_ILLUM,
42 };
43 
44 /* Channel definitions */
45 static const struct iio_chan_spec als_channels[] = {
46 	{
47 		.type = IIO_INTENSITY,
48 		.modified = 1,
49 		.channel2 = IIO_MOD_LIGHT_BOTH,
50 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
51 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
52 		BIT(IIO_CHAN_INFO_SCALE) |
53 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
54 		BIT(IIO_CHAN_INFO_HYSTERESIS) |
55 		BIT(IIO_CHAN_INFO_HYSTERESIS_RELATIVE),
56 		.scan_index = CHANNEL_SCAN_INDEX_INTENSITY,
57 	},
58 	{
59 		.type = IIO_LIGHT,
60 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
61 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
62 		BIT(IIO_CHAN_INFO_SCALE) |
63 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
64 		BIT(IIO_CHAN_INFO_HYSTERESIS) |
65 		BIT(IIO_CHAN_INFO_HYSTERESIS_RELATIVE),
66 		.scan_index = CHANNEL_SCAN_INDEX_ILLUM,
67 	},
68 	IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP)
69 };
70 
71 /* Adjust channel real bits based on report descriptor */
als_adjust_channel_bit_mask(struct iio_chan_spec * channels,int channel,int size)72 static void als_adjust_channel_bit_mask(struct iio_chan_spec *channels,
73 					int channel, int size)
74 {
75 	channels[channel].scan_type.sign = 's';
76 	/* Real storage bits will change based on the report desc. */
77 	channels[channel].scan_type.realbits = size * 8;
78 	/* Maximum size of a sample to capture is u32 */
79 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
80 }
81 
82 /* Channel read_raw handler */
als_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)83 static int als_read_raw(struct iio_dev *indio_dev,
84 			      struct iio_chan_spec const *chan,
85 			      int *val, int *val2,
86 			      long mask)
87 {
88 	struct als_state *als_state = iio_priv(indio_dev);
89 	struct hid_sensor_hub_device *hsdev = als_state->common_attributes.hsdev;
90 	int report_id = -1;
91 	u32 address;
92 	int ret_type;
93 	s32 min;
94 
95 	*val = 0;
96 	*val2 = 0;
97 	switch (mask) {
98 	case IIO_CHAN_INFO_RAW:
99 		switch (chan->scan_index) {
100 		case  CHANNEL_SCAN_INDEX_INTENSITY:
101 		case  CHANNEL_SCAN_INDEX_ILLUM:
102 			report_id = als_state->als_illum.report_id;
103 			min = als_state->als_illum.logical_minimum;
104 			address = HID_USAGE_SENSOR_LIGHT_ILLUM;
105 			break;
106 		default:
107 			report_id = -1;
108 			break;
109 		}
110 		if (report_id >= 0) {
111 			hid_sensor_power_state(&als_state->common_attributes,
112 						true);
113 			*val = sensor_hub_input_attr_get_raw_value(
114 					hsdev, hsdev->usage, address, report_id,
115 					SENSOR_HUB_SYNC, min < 0);
116 			hid_sensor_power_state(&als_state->common_attributes,
117 						false);
118 		} else {
119 			*val = 0;
120 			return -EINVAL;
121 		}
122 		ret_type = IIO_VAL_INT;
123 		break;
124 	case IIO_CHAN_INFO_SCALE:
125 		*val = als_state->scale_pre_decml;
126 		*val2 = als_state->scale_post_decml;
127 		ret_type = als_state->scale_precision;
128 		break;
129 	case IIO_CHAN_INFO_OFFSET:
130 		*val = als_state->value_offset;
131 		ret_type = IIO_VAL_INT;
132 		break;
133 	case IIO_CHAN_INFO_SAMP_FREQ:
134 		ret_type = hid_sensor_read_samp_freq_value(
135 				&als_state->common_attributes, val, val2);
136 		break;
137 	case IIO_CHAN_INFO_HYSTERESIS:
138 		ret_type = hid_sensor_read_raw_hyst_value(
139 				&als_state->common_attributes, val, val2);
140 		break;
141 	case IIO_CHAN_INFO_HYSTERESIS_RELATIVE:
142 		ret_type = hid_sensor_read_raw_hyst_rel_value(
143 				&als_state->common_attributes, val, val2);
144 		break;
145 	default:
146 		ret_type = -EINVAL;
147 		break;
148 	}
149 
150 	return ret_type;
151 }
152 
153 /* Channel write_raw handler */
als_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)154 static int als_write_raw(struct iio_dev *indio_dev,
155 			       struct iio_chan_spec const *chan,
156 			       int val,
157 			       int val2,
158 			       long mask)
159 {
160 	struct als_state *als_state = iio_priv(indio_dev);
161 	int ret = 0;
162 
163 	switch (mask) {
164 	case IIO_CHAN_INFO_SAMP_FREQ:
165 		ret = hid_sensor_write_samp_freq_value(
166 				&als_state->common_attributes, val, val2);
167 		break;
168 	case IIO_CHAN_INFO_HYSTERESIS:
169 		ret = hid_sensor_write_raw_hyst_value(
170 				&als_state->common_attributes, val, val2);
171 		break;
172 	case IIO_CHAN_INFO_HYSTERESIS_RELATIVE:
173 		ret = hid_sensor_write_raw_hyst_rel_value(
174 				&als_state->common_attributes, val, val2);
175 		break;
176 	default:
177 		ret = -EINVAL;
178 	}
179 
180 	return ret;
181 }
182 
183 static const struct iio_info als_info = {
184 	.read_raw = &als_read_raw,
185 	.write_raw = &als_write_raw,
186 };
187 
188 /* Callback handler to send event after all samples are received and captured */
als_proc_event(struct hid_sensor_hub_device * hsdev,unsigned usage_id,void * priv)189 static int als_proc_event(struct hid_sensor_hub_device *hsdev,
190 				unsigned usage_id,
191 				void *priv)
192 {
193 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
194 	struct als_state *als_state = iio_priv(indio_dev);
195 
196 	dev_dbg(&indio_dev->dev, "als_proc_event\n");
197 	if (atomic_read(&als_state->common_attributes.data_ready)) {
198 		if (!als_state->timestamp)
199 			als_state->timestamp = iio_get_time_ns(indio_dev);
200 
201 		iio_push_to_buffers_with_timestamp(indio_dev, &als_state->scan,
202 						   als_state->timestamp);
203 		als_state->timestamp = 0;
204 	}
205 
206 	return 0;
207 }
208 
209 /* Capture samples in local storage */
als_capture_sample(struct hid_sensor_hub_device * hsdev,unsigned usage_id,size_t raw_len,char * raw_data,void * priv)210 static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
211 				unsigned usage_id,
212 				size_t raw_len, char *raw_data,
213 				void *priv)
214 {
215 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
216 	struct als_state *als_state = iio_priv(indio_dev);
217 	int ret = -EINVAL;
218 	u32 sample_data = *(u32 *)raw_data;
219 
220 	switch (usage_id) {
221 	case HID_USAGE_SENSOR_LIGHT_ILLUM:
222 		als_state->scan.illum[CHANNEL_SCAN_INDEX_INTENSITY] = sample_data;
223 		als_state->scan.illum[CHANNEL_SCAN_INDEX_ILLUM] = sample_data;
224 		ret = 0;
225 		break;
226 	case HID_USAGE_SENSOR_TIME_TIMESTAMP:
227 		als_state->timestamp = hid_sensor_convert_timestamp(&als_state->common_attributes,
228 								    *(s64 *)raw_data);
229 		ret = 0;
230 		break;
231 	default:
232 		break;
233 	}
234 
235 	return ret;
236 }
237 
238 /* Parse report which is specific to an usage id*/
als_parse_report(struct platform_device * pdev,struct hid_sensor_hub_device * hsdev,struct iio_chan_spec * channels,unsigned usage_id,struct als_state * st)239 static int als_parse_report(struct platform_device *pdev,
240 				struct hid_sensor_hub_device *hsdev,
241 				struct iio_chan_spec *channels,
242 				unsigned usage_id,
243 				struct als_state *st)
244 {
245 	int ret;
246 
247 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
248 			usage_id,
249 			HID_USAGE_SENSOR_LIGHT_ILLUM,
250 			&st->als_illum);
251 	if (ret < 0)
252 		return ret;
253 	als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_INTENSITY,
254 				    st->als_illum.size);
255 	als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
256 					st->als_illum.size);
257 
258 	dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
259 			st->als_illum.report_id);
260 
261 	st->scale_precision = hid_sensor_format_scale(usage_id, &st->als_illum,
262 				&st->scale_pre_decml, &st->scale_post_decml);
263 
264 	return ret;
265 }
266 
267 /* Function to initialize the processing for usage id */
hid_als_probe(struct platform_device * pdev)268 static int hid_als_probe(struct platform_device *pdev)
269 {
270 	int ret = 0;
271 	static const char *name = "als";
272 	struct iio_dev *indio_dev;
273 	struct als_state *als_state;
274 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
275 
276 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct als_state));
277 	if (!indio_dev)
278 		return -ENOMEM;
279 	platform_set_drvdata(pdev, indio_dev);
280 
281 	als_state = iio_priv(indio_dev);
282 	als_state->common_attributes.hsdev = hsdev;
283 	als_state->common_attributes.pdev = pdev;
284 
285 	ret = hid_sensor_parse_common_attributes(hsdev,
286 					hsdev->usage,
287 					&als_state->common_attributes,
288 					als_sensitivity_addresses,
289 					ARRAY_SIZE(als_sensitivity_addresses));
290 	if (ret) {
291 		dev_err(&pdev->dev, "failed to setup common attributes\n");
292 		return ret;
293 	}
294 
295 	indio_dev->channels = devm_kmemdup(&pdev->dev, als_channels,
296 					   sizeof(als_channels), GFP_KERNEL);
297 	if (!indio_dev->channels) {
298 		dev_err(&pdev->dev, "failed to duplicate channels\n");
299 		return -ENOMEM;
300 	}
301 
302 	ret = als_parse_report(pdev, hsdev,
303 			       (struct iio_chan_spec *)indio_dev->channels,
304 			       hsdev->usage,
305 			       als_state);
306 	if (ret) {
307 		dev_err(&pdev->dev, "failed to setup attributes\n");
308 		return ret;
309 	}
310 
311 	indio_dev->num_channels =
312 				ARRAY_SIZE(als_channels);
313 	indio_dev->info = &als_info;
314 	indio_dev->name = name;
315 	indio_dev->modes = INDIO_DIRECT_MODE;
316 
317 	atomic_set(&als_state->common_attributes.data_ready, 0);
318 
319 	ret = hid_sensor_setup_trigger(indio_dev, name,
320 				&als_state->common_attributes);
321 	if (ret < 0) {
322 		dev_err(&pdev->dev, "trigger setup failed\n");
323 		return ret;
324 	}
325 
326 	ret = iio_device_register(indio_dev);
327 	if (ret) {
328 		dev_err(&pdev->dev, "device register failed\n");
329 		goto error_remove_trigger;
330 	}
331 
332 	als_state->callbacks.send_event = als_proc_event;
333 	als_state->callbacks.capture_sample = als_capture_sample;
334 	als_state->callbacks.pdev = pdev;
335 	ret = sensor_hub_register_callback(hsdev, hsdev->usage, &als_state->callbacks);
336 	if (ret < 0) {
337 		dev_err(&pdev->dev, "callback reg failed\n");
338 		goto error_iio_unreg;
339 	}
340 
341 	return ret;
342 
343 error_iio_unreg:
344 	iio_device_unregister(indio_dev);
345 error_remove_trigger:
346 	hid_sensor_remove_trigger(indio_dev, &als_state->common_attributes);
347 	return ret;
348 }
349 
350 /* Function to deinitialize the processing for usage id */
hid_als_remove(struct platform_device * pdev)351 static int hid_als_remove(struct platform_device *pdev)
352 {
353 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
354 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
355 	struct als_state *als_state = iio_priv(indio_dev);
356 
357 	sensor_hub_remove_callback(hsdev, hsdev->usage);
358 	iio_device_unregister(indio_dev);
359 	hid_sensor_remove_trigger(indio_dev, &als_state->common_attributes);
360 
361 	return 0;
362 }
363 
364 static const struct platform_device_id hid_als_ids[] = {
365 	{
366 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
367 		.name = "HID-SENSOR-200041",
368 	},
369 	{
370 		/* Format: HID-SENSOR-custom_sensor_tag-usage_id_in_hex_lowercase */
371 		.name = "HID-SENSOR-LISS-0041",
372 	},
373 	{ /* sentinel */ }
374 };
375 MODULE_DEVICE_TABLE(platform, hid_als_ids);
376 
377 static struct platform_driver hid_als_platform_driver = {
378 	.id_table = hid_als_ids,
379 	.driver = {
380 		.name	= KBUILD_MODNAME,
381 		.pm	= &hid_sensor_pm_ops,
382 	},
383 	.probe		= hid_als_probe,
384 	.remove		= hid_als_remove,
385 };
386 module_platform_driver(hid_als_platform_driver);
387 
388 MODULE_DESCRIPTION("HID Sensor ALS");
389 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
390 MODULE_LICENSE("GPL");
391 MODULE_IMPORT_NS(IIO_HID);
392