• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	int report_id = -1;
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 		switch (chan->scan_index) {
99 		case  CHANNEL_SCAN_INDEX_INTENSITY:
100 		case  CHANNEL_SCAN_INDEX_ILLUM:
101 			report_id = als_state->als_illum.report_id;
102 			min = als_state->als_illum.logical_minimum;
103 			address = HID_USAGE_SENSOR_LIGHT_ILLUM;
104 			break;
105 		default:
106 			report_id = -1;
107 			break;
108 		}
109 		if (report_id >= 0) {
110 			hid_sensor_power_state(&als_state->common_attributes,
111 						true);
112 			*val = sensor_hub_input_attr_get_raw_value(
113 					als_state->common_attributes.hsdev,
114 					HID_USAGE_SENSOR_ALS, address,
115 					report_id,
116 					SENSOR_HUB_SYNC,
117 					min < 0);
118 			hid_sensor_power_state(&als_state->common_attributes,
119 						false);
120 		} else {
121 			*val = 0;
122 			return -EINVAL;
123 		}
124 		ret_type = IIO_VAL_INT;
125 		break;
126 	case IIO_CHAN_INFO_SCALE:
127 		*val = als_state->scale_pre_decml;
128 		*val2 = als_state->scale_post_decml;
129 		ret_type = als_state->scale_precision;
130 		break;
131 	case IIO_CHAN_INFO_OFFSET:
132 		*val = als_state->value_offset;
133 		ret_type = IIO_VAL_INT;
134 		break;
135 	case IIO_CHAN_INFO_SAMP_FREQ:
136 		ret_type = hid_sensor_read_samp_freq_value(
137 				&als_state->common_attributes, val, val2);
138 		break;
139 	case IIO_CHAN_INFO_HYSTERESIS:
140 		ret_type = hid_sensor_read_raw_hyst_value(
141 				&als_state->common_attributes, val, val2);
142 		break;
143 	case IIO_CHAN_INFO_HYSTERESIS_RELATIVE:
144 		ret_type = hid_sensor_read_raw_hyst_rel_value(
145 				&als_state->common_attributes, val, val2);
146 		break;
147 	default:
148 		ret_type = -EINVAL;
149 		break;
150 	}
151 
152 	return ret_type;
153 }
154 
155 /* Channel write_raw handler */
als_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)156 static int als_write_raw(struct iio_dev *indio_dev,
157 			       struct iio_chan_spec const *chan,
158 			       int val,
159 			       int val2,
160 			       long mask)
161 {
162 	struct als_state *als_state = iio_priv(indio_dev);
163 	int ret = 0;
164 
165 	switch (mask) {
166 	case IIO_CHAN_INFO_SAMP_FREQ:
167 		ret = hid_sensor_write_samp_freq_value(
168 				&als_state->common_attributes, val, val2);
169 		break;
170 	case IIO_CHAN_INFO_HYSTERESIS:
171 		ret = hid_sensor_write_raw_hyst_value(
172 				&als_state->common_attributes, val, val2);
173 		break;
174 	case IIO_CHAN_INFO_HYSTERESIS_RELATIVE:
175 		ret = hid_sensor_write_raw_hyst_rel_value(
176 				&als_state->common_attributes, val, val2);
177 		break;
178 	default:
179 		ret = -EINVAL;
180 	}
181 
182 	return ret;
183 }
184 
185 static const struct iio_info als_info = {
186 	.read_raw = &als_read_raw,
187 	.write_raw = &als_write_raw,
188 };
189 
190 /* 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)191 static int als_proc_event(struct hid_sensor_hub_device *hsdev,
192 				unsigned usage_id,
193 				void *priv)
194 {
195 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
196 	struct als_state *als_state = iio_priv(indio_dev);
197 
198 	dev_dbg(&indio_dev->dev, "als_proc_event\n");
199 	if (atomic_read(&als_state->common_attributes.data_ready)) {
200 		if (!als_state->timestamp)
201 			als_state->timestamp = iio_get_time_ns(indio_dev);
202 
203 		iio_push_to_buffers_with_timestamp(indio_dev, &als_state->scan,
204 						   als_state->timestamp);
205 		als_state->timestamp = 0;
206 	}
207 
208 	return 0;
209 }
210 
211 /* 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)212 static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
213 				unsigned usage_id,
214 				size_t raw_len, char *raw_data,
215 				void *priv)
216 {
217 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
218 	struct als_state *als_state = iio_priv(indio_dev);
219 	int ret = -EINVAL;
220 	u32 sample_data = *(u32 *)raw_data;
221 
222 	switch (usage_id) {
223 	case HID_USAGE_SENSOR_LIGHT_ILLUM:
224 		als_state->scan.illum[CHANNEL_SCAN_INDEX_INTENSITY] = sample_data;
225 		als_state->scan.illum[CHANNEL_SCAN_INDEX_ILLUM] = sample_data;
226 		ret = 0;
227 		break;
228 	case HID_USAGE_SENSOR_TIME_TIMESTAMP:
229 		als_state->timestamp = hid_sensor_convert_timestamp(&als_state->common_attributes,
230 								    *(s64 *)raw_data);
231 		ret = 0;
232 		break;
233 	default:
234 		break;
235 	}
236 
237 	return ret;
238 }
239 
240 /* 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)241 static int als_parse_report(struct platform_device *pdev,
242 				struct hid_sensor_hub_device *hsdev,
243 				struct iio_chan_spec *channels,
244 				unsigned usage_id,
245 				struct als_state *st)
246 {
247 	int ret;
248 
249 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
250 			usage_id,
251 			HID_USAGE_SENSOR_LIGHT_ILLUM,
252 			&st->als_illum);
253 	if (ret < 0)
254 		return ret;
255 	als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_INTENSITY,
256 				    st->als_illum.size);
257 	als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
258 					st->als_illum.size);
259 
260 	dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
261 			st->als_illum.report_id);
262 
263 	st->scale_precision = hid_sensor_format_scale(
264 				HID_USAGE_SENSOR_ALS,
265 				&st->als_illum,
266 				&st->scale_pre_decml, &st->scale_post_decml);
267 
268 	return ret;
269 }
270 
271 /* Function to initialize the processing for usage id */
hid_als_probe(struct platform_device * pdev)272 static int hid_als_probe(struct platform_device *pdev)
273 {
274 	int ret = 0;
275 	static const char *name = "als";
276 	struct iio_dev *indio_dev;
277 	struct als_state *als_state;
278 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
279 
280 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct als_state));
281 	if (!indio_dev)
282 		return -ENOMEM;
283 	platform_set_drvdata(pdev, indio_dev);
284 
285 	als_state = iio_priv(indio_dev);
286 	als_state->common_attributes.hsdev = hsdev;
287 	als_state->common_attributes.pdev = pdev;
288 
289 	ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS,
290 					&als_state->common_attributes,
291 					als_sensitivity_addresses,
292 					ARRAY_SIZE(als_sensitivity_addresses));
293 	if (ret) {
294 		dev_err(&pdev->dev, "failed to setup common attributes\n");
295 		return ret;
296 	}
297 
298 	indio_dev->channels = devm_kmemdup(&pdev->dev, als_channels,
299 					   sizeof(als_channels), GFP_KERNEL);
300 	if (!indio_dev->channels) {
301 		dev_err(&pdev->dev, "failed to duplicate channels\n");
302 		return -ENOMEM;
303 	}
304 
305 	ret = als_parse_report(pdev, hsdev,
306 			       (struct iio_chan_spec *)indio_dev->channels,
307 			       HID_USAGE_SENSOR_ALS, als_state);
308 	if (ret) {
309 		dev_err(&pdev->dev, "failed to setup attributes\n");
310 		return ret;
311 	}
312 
313 	indio_dev->num_channels =
314 				ARRAY_SIZE(als_channels);
315 	indio_dev->info = &als_info;
316 	indio_dev->name = name;
317 	indio_dev->modes = INDIO_DIRECT_MODE;
318 
319 	atomic_set(&als_state->common_attributes.data_ready, 0);
320 
321 	ret = hid_sensor_setup_trigger(indio_dev, name,
322 				&als_state->common_attributes);
323 	if (ret < 0) {
324 		dev_err(&pdev->dev, "trigger setup failed\n");
325 		return ret;
326 	}
327 
328 	ret = iio_device_register(indio_dev);
329 	if (ret) {
330 		dev_err(&pdev->dev, "device register failed\n");
331 		goto error_remove_trigger;
332 	}
333 
334 	als_state->callbacks.send_event = als_proc_event;
335 	als_state->callbacks.capture_sample = als_capture_sample;
336 	als_state->callbacks.pdev = pdev;
337 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ALS,
338 					&als_state->callbacks);
339 	if (ret < 0) {
340 		dev_err(&pdev->dev, "callback reg failed\n");
341 		goto error_iio_unreg;
342 	}
343 
344 	return ret;
345 
346 error_iio_unreg:
347 	iio_device_unregister(indio_dev);
348 error_remove_trigger:
349 	hid_sensor_remove_trigger(indio_dev, &als_state->common_attributes);
350 	return ret;
351 }
352 
353 /* Function to deinitialize the processing for usage id */
hid_als_remove(struct platform_device * pdev)354 static int hid_als_remove(struct platform_device *pdev)
355 {
356 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
357 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
358 	struct als_state *als_state = iio_priv(indio_dev);
359 
360 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS);
361 	iio_device_unregister(indio_dev);
362 	hid_sensor_remove_trigger(indio_dev, &als_state->common_attributes);
363 
364 	return 0;
365 }
366 
367 static const struct platform_device_id hid_als_ids[] = {
368 	{
369 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
370 		.name = "HID-SENSOR-200041",
371 	},
372 	{ /* sentinel */ }
373 };
374 MODULE_DEVICE_TABLE(platform, hid_als_ids);
375 
376 static struct platform_driver hid_als_platform_driver = {
377 	.id_table = hid_als_ids,
378 	.driver = {
379 		.name	= KBUILD_MODNAME,
380 		.pm	= &hid_sensor_pm_ops,
381 	},
382 	.probe		= hid_als_probe,
383 	.remove		= hid_als_remove,
384 };
385 module_platform_driver(hid_als_platform_driver);
386 
387 MODULE_DESCRIPTION("HID Sensor ALS");
388 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
389 MODULE_LICENSE("GPL");
390 MODULE_IMPORT_NS(IIO_HID);
391