1 /*
2  * HID Sensors Driver
3  * Copyright (c) 2012, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  */
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
25 #include <linux/delay.h>
26 #include <linux/hid-sensor-hub.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger_consumer.h>
31 #include <linux/iio/triggered_buffer.h>
32 #include "../common/hid-sensors/hid-sensor-trigger.h"
33 
34 #define CHANNEL_SCAN_INDEX_ILLUM 0
35 
36 struct als_state {
37 	struct hid_sensor_hub_callbacks callbacks;
38 	struct hid_sensor_common common_attributes;
39 	struct hid_sensor_hub_attribute_info als_illum;
40 	u32 illum;
41 	int scale_pre_decml;
42 	int scale_post_decml;
43 	int scale_precision;
44 	int value_offset;
45 };
46 
47 /* Channel definitions */
48 static const struct iio_chan_spec als_channels[] = {
49 	{
50 		.type = IIO_INTENSITY,
51 		.modified = 1,
52 		.channel2 = IIO_MOD_LIGHT_BOTH,
53 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
54 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
55 		BIT(IIO_CHAN_INFO_SCALE) |
56 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
57 		BIT(IIO_CHAN_INFO_HYSTERESIS),
58 		.scan_index = CHANNEL_SCAN_INDEX_ILLUM,
59 	}
60 };
61 
62 /* Adjust channel real bits based on report descriptor */
63 static void als_adjust_channel_bit_mask(struct iio_chan_spec *channels,
64 					int channel, int size)
65 {
66 	channels[channel].scan_type.sign = 's';
67 	/* Real storage bits will change based on the report desc. */
68 	channels[channel].scan_type.realbits = size * 8;
69 	/* Maximum size of a sample to capture is u32 */
70 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
71 }
72 
73 /* Channel read_raw handler */
74 static int als_read_raw(struct iio_dev *indio_dev,
75 			      struct iio_chan_spec const *chan,
76 			      int *val, int *val2,
77 			      long mask)
78 {
79 	struct als_state *als_state = iio_priv(indio_dev);
80 	int report_id = -1;
81 	u32 address;
82 	int ret;
83 	int ret_type;
84 	s32 poll_value;
85 
86 	*val = 0;
87 	*val2 = 0;
88 	switch (mask) {
89 	case 0:
90 		switch (chan->scan_index) {
91 		case  CHANNEL_SCAN_INDEX_ILLUM:
92 			report_id = als_state->als_illum.report_id;
93 			address =
94 			HID_USAGE_SENSOR_LIGHT_ILLUM;
95 			break;
96 		default:
97 			report_id = -1;
98 			break;
99 		}
100 		if (report_id >= 0) {
101 			poll_value = hid_sensor_read_poll_value(
102 						&als_state->common_attributes);
103 			if (poll_value < 0)
104 				return -EINVAL;
105 
106 			hid_sensor_power_state(&als_state->common_attributes,
107 						true);
108 			msleep_interruptible(poll_value * 2);
109 
110 			*val = sensor_hub_input_attr_get_raw_value(
111 					als_state->common_attributes.hsdev,
112 					HID_USAGE_SENSOR_ALS, address,
113 					report_id);
114 			hid_sensor_power_state(&als_state->common_attributes,
115 						false);
116 		} else {
117 			*val = 0;
118 			return -EINVAL;
119 		}
120 		ret_type = IIO_VAL_INT;
121 		break;
122 	case IIO_CHAN_INFO_SCALE:
123 		*val = als_state->scale_pre_decml;
124 		*val2 = als_state->scale_post_decml;
125 		ret_type = als_state->scale_precision;
126 		break;
127 	case IIO_CHAN_INFO_OFFSET:
128 		*val = als_state->value_offset;
129 		ret_type = IIO_VAL_INT;
130 		break;
131 	case IIO_CHAN_INFO_SAMP_FREQ:
132 		ret = hid_sensor_read_samp_freq_value(
133 				&als_state->common_attributes, val, val2);
134 		ret_type = IIO_VAL_INT_PLUS_MICRO;
135 		break;
136 	case IIO_CHAN_INFO_HYSTERESIS:
137 		ret = hid_sensor_read_raw_hyst_value(
138 				&als_state->common_attributes, val, val2);
139 		ret_type = IIO_VAL_INT_PLUS_MICRO;
140 		break;
141 	default:
142 		ret_type = -EINVAL;
143 		break;
144 	}
145 
146 	return ret_type;
147 }
148 
149 /* Channel write_raw handler */
150 static int als_write_raw(struct iio_dev *indio_dev,
151 			       struct iio_chan_spec const *chan,
152 			       int val,
153 			       int val2,
154 			       long mask)
155 {
156 	struct als_state *als_state = iio_priv(indio_dev);
157 	int ret = 0;
158 
159 	switch (mask) {
160 	case IIO_CHAN_INFO_SAMP_FREQ:
161 		ret = hid_sensor_write_samp_freq_value(
162 				&als_state->common_attributes, val, val2);
163 		break;
164 	case IIO_CHAN_INFO_HYSTERESIS:
165 		ret = hid_sensor_write_raw_hyst_value(
166 				&als_state->common_attributes, val, val2);
167 		break;
168 	default:
169 		ret = -EINVAL;
170 	}
171 
172 	return ret;
173 }
174 
175 static const struct iio_info als_info = {
176 	.driver_module = THIS_MODULE,
177 	.read_raw = &als_read_raw,
178 	.write_raw = &als_write_raw,
179 };
180 
181 /* Function to push data to buffer */
182 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
183 					int len)
184 {
185 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
186 	iio_push_to_buffers(indio_dev, data);
187 }
188 
189 /* Callback handler to send event after all samples are received and captured */
190 static int als_proc_event(struct hid_sensor_hub_device *hsdev,
191 				unsigned usage_id,
192 				void *priv)
193 {
194 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
195 	struct als_state *als_state = iio_priv(indio_dev);
196 
197 	dev_dbg(&indio_dev->dev, "als_proc_event\n");
198 	if (atomic_read(&als_state->common_attributes.data_ready))
199 		hid_sensor_push_data(indio_dev,
200 				&als_state->illum,
201 				sizeof(als_state->illum));
202 
203 	return 0;
204 }
205 
206 /* Capture samples in local storage */
207 static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
208 				unsigned usage_id,
209 				size_t raw_len, char *raw_data,
210 				void *priv)
211 {
212 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
213 	struct als_state *als_state = iio_priv(indio_dev);
214 	int ret = -EINVAL;
215 
216 	switch (usage_id) {
217 	case HID_USAGE_SENSOR_LIGHT_ILLUM:
218 		als_state->illum = *(u32 *)raw_data;
219 		ret = 0;
220 		break;
221 	default:
222 		break;
223 	}
224 
225 	return ret;
226 }
227 
228 /* Parse report which is specific to an usage id*/
229 static int als_parse_report(struct platform_device *pdev,
230 				struct hid_sensor_hub_device *hsdev,
231 				struct iio_chan_spec *channels,
232 				unsigned usage_id,
233 				struct als_state *st)
234 {
235 	int ret;
236 
237 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
238 			usage_id,
239 			HID_USAGE_SENSOR_LIGHT_ILLUM,
240 			&st->als_illum);
241 	if (ret < 0)
242 		return ret;
243 	als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
244 					st->als_illum.size);
245 
246 	dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
247 			st->als_illum.report_id);
248 
249 	st->scale_precision = hid_sensor_format_scale(
250 				HID_USAGE_SENSOR_ALS,
251 				&st->als_illum,
252 				&st->scale_pre_decml, &st->scale_post_decml);
253 
254 	/* Set Sensitivity field ids, when there is no individual modifier */
255 	if (st->common_attributes.sensitivity.index < 0) {
256 		sensor_hub_input_get_attribute_info(hsdev,
257 			HID_FEATURE_REPORT, usage_id,
258 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
259 			HID_USAGE_SENSOR_DATA_LIGHT,
260 			&st->common_attributes.sensitivity);
261 		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
262 			st->common_attributes.sensitivity.index,
263 			st->common_attributes.sensitivity.report_id);
264 	}
265 	return ret;
266 }
267 
268 /* Function to initialize the processing for usage id */
269 static int hid_als_probe(struct platform_device *pdev)
270 {
271 	int ret = 0;
272 	static const char *name = "als";
273 	struct iio_dev *indio_dev;
274 	struct als_state *als_state;
275 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
276 	struct iio_chan_spec *channels;
277 
278 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct als_state));
279 	if (!indio_dev)
280 		return -ENOMEM;
281 	platform_set_drvdata(pdev, indio_dev);
282 
283 	als_state = iio_priv(indio_dev);
284 	als_state->common_attributes.hsdev = hsdev;
285 	als_state->common_attributes.pdev = pdev;
286 
287 	ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS,
288 					&als_state->common_attributes);
289 	if (ret) {
290 		dev_err(&pdev->dev, "failed to setup common attributes\n");
291 		return ret;
292 	}
293 
294 	channels = kmemdup(als_channels, sizeof(als_channels), GFP_KERNEL);
295 	if (!channels) {
296 		dev_err(&pdev->dev, "failed to duplicate channels\n");
297 		return -ENOMEM;
298 	}
299 
300 	ret = als_parse_report(pdev, hsdev, channels,
301 				HID_USAGE_SENSOR_ALS, als_state);
302 	if (ret) {
303 		dev_err(&pdev->dev, "failed to setup attributes\n");
304 		goto error_free_dev_mem;
305 	}
306 
307 	indio_dev->channels = channels;
308 	indio_dev->num_channels =
309 				ARRAY_SIZE(als_channels);
310 	indio_dev->dev.parent = &pdev->dev;
311 	indio_dev->info = &als_info;
312 	indio_dev->name = name;
313 	indio_dev->modes = INDIO_DIRECT_MODE;
314 
315 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
316 		NULL, NULL);
317 	if (ret) {
318 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
319 		goto error_free_dev_mem;
320 	}
321 	atomic_set(&als_state->common_attributes.data_ready, 0);
322 	ret = hid_sensor_setup_trigger(indio_dev, name,
323 				&als_state->common_attributes);
324 	if (ret < 0) {
325 		dev_err(&pdev->dev, "trigger setup failed\n");
326 		goto error_unreg_buffer_funcs;
327 	}
328 
329 	ret = iio_device_register(indio_dev);
330 	if (ret) {
331 		dev_err(&pdev->dev, "device register failed\n");
332 		goto error_remove_trigger;
333 	}
334 
335 	als_state->callbacks.send_event = als_proc_event;
336 	als_state->callbacks.capture_sample = als_capture_sample;
337 	als_state->callbacks.pdev = pdev;
338 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ALS,
339 					&als_state->callbacks);
340 	if (ret < 0) {
341 		dev_err(&pdev->dev, "callback reg failed\n");
342 		goto error_iio_unreg;
343 	}
344 
345 	return ret;
346 
347 error_iio_unreg:
348 	iio_device_unregister(indio_dev);
349 error_remove_trigger:
350 	hid_sensor_remove_trigger(&als_state->common_attributes);
351 error_unreg_buffer_funcs:
352 	iio_triggered_buffer_cleanup(indio_dev);
353 error_free_dev_mem:
354 	kfree(indio_dev->channels);
355 	return ret;
356 }
357 
358 /* Function to deinitialize the processing for usage id */
359 static int hid_als_remove(struct platform_device *pdev)
360 {
361 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
362 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
363 	struct als_state *als_state = iio_priv(indio_dev);
364 
365 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS);
366 	iio_device_unregister(indio_dev);
367 	hid_sensor_remove_trigger(&als_state->common_attributes);
368 	iio_triggered_buffer_cleanup(indio_dev);
369 	kfree(indio_dev->channels);
370 
371 	return 0;
372 }
373 
374 static struct platform_device_id hid_als_ids[] = {
375 	{
376 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
377 		.name = "HID-SENSOR-200041",
378 	},
379 	{ /* sentinel */ }
380 };
381 MODULE_DEVICE_TABLE(platform, hid_als_ids);
382 
383 static struct platform_driver hid_als_platform_driver = {
384 	.id_table = hid_als_ids,
385 	.driver = {
386 		.name	= KBUILD_MODNAME,
387 		.owner	= THIS_MODULE,
388 	},
389 	.probe		= hid_als_probe,
390 	.remove		= hid_als_remove,
391 };
392 module_platform_driver(hid_als_platform_driver);
393 
394 MODULE_DESCRIPTION("HID Sensor ALS");
395 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
396 MODULE_LICENSE("GPL");
397