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/hid-sensor-hub.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
31 #include "../common/hid-sensors/hid-sensor-attributes.h"
32 #include "../common/hid-sensors/hid-sensor-trigger.h"
33 
34 /*Format: HID-SENSOR-usage_id_in_hex*/
35 /*Usage ID from spec for Accelerometer-3D: 0x200041*/
36 #define DRIVER_NAME "HID-SENSOR-200041"
37 
38 #define CHANNEL_SCAN_INDEX_ILLUM 0
39 
40 struct als_state {
41 	struct hid_sensor_hub_callbacks callbacks;
42 	struct hid_sensor_iio_common common_attributes;
43 	struct hid_sensor_hub_attribute_info als_illum;
44 	u32 illum;
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 = IIO_CHAN_INFO_OFFSET_SHARED_BIT |
54 		IIO_CHAN_INFO_SCALE_SHARED_BIT |
55 		IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT |
56 		IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT,
57 		.scan_index = CHANNEL_SCAN_INDEX_ILLUM,
58 	}
59 };
60 
61 /* Adjust channel real bits based on report descriptor */
62 static void als_adjust_channel_bit_mask(struct iio_chan_spec *channels,
63 					int channel, int size)
64 {
65 	channels[channel].scan_type.sign = 's';
66 	/* Real storage bits will change based on the report desc. */
67 	channels[channel].scan_type.realbits = size * 8;
68 	/* Maximum size of a sample to capture is u32 */
69 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
70 }
71 
72 /* Channel read_raw handler */
73 static int als_read_raw(struct iio_dev *indio_dev,
74 			      struct iio_chan_spec const *chan,
75 			      int *val, int *val2,
76 			      long mask)
77 {
78 	struct als_state *als_state = iio_priv(indio_dev);
79 	int report_id = -1;
80 	u32 address;
81 	int ret;
82 	int ret_type;
83 
84 	*val = 0;
85 	*val2 = 0;
86 	switch (mask) {
87 	case 0:
88 		switch (chan->scan_index) {
89 		case  CHANNEL_SCAN_INDEX_ILLUM:
90 			report_id = als_state->als_illum.report_id;
91 			address =
92 			HID_USAGE_SENSOR_LIGHT_ILLUM;
93 			break;
94 		default:
95 			report_id = -1;
96 			break;
97 		}
98 		if (report_id >= 0)
99 			*val = sensor_hub_input_attr_get_raw_value(
100 				als_state->common_attributes.hsdev,
101 				HID_USAGE_SENSOR_ALS, address,
102 				report_id);
103 		else {
104 			*val = 0;
105 			return -EINVAL;
106 		}
107 		ret_type = IIO_VAL_INT;
108 		break;
109 	case IIO_CHAN_INFO_SCALE:
110 		*val = als_state->als_illum.units;
111 		ret_type = IIO_VAL_INT;
112 		break;
113 	case IIO_CHAN_INFO_OFFSET:
114 		*val = hid_sensor_convert_exponent(
115 				als_state->als_illum.unit_expo);
116 		ret_type = IIO_VAL_INT;
117 		break;
118 	case IIO_CHAN_INFO_SAMP_FREQ:
119 		ret = hid_sensor_read_samp_freq_value(
120 				&als_state->common_attributes, val, val2);
121 		ret_type = IIO_VAL_INT_PLUS_MICRO;
122 		break;
123 	case IIO_CHAN_INFO_HYSTERESIS:
124 		ret = hid_sensor_read_raw_hyst_value(
125 				&als_state->common_attributes, val, val2);
126 		ret_type = IIO_VAL_INT_PLUS_MICRO;
127 		break;
128 	default:
129 		ret_type = -EINVAL;
130 		break;
131 	}
132 
133 	return ret_type;
134 }
135 
136 /* Channel write_raw handler */
137 static int als_write_raw(struct iio_dev *indio_dev,
138 			       struct iio_chan_spec const *chan,
139 			       int val,
140 			       int val2,
141 			       long mask)
142 {
143 	struct als_state *als_state = iio_priv(indio_dev);
144 	int ret = 0;
145 
146 	switch (mask) {
147 	case IIO_CHAN_INFO_SAMP_FREQ:
148 		ret = hid_sensor_write_samp_freq_value(
149 				&als_state->common_attributes, val, val2);
150 		break;
151 	case IIO_CHAN_INFO_HYSTERESIS:
152 		ret = hid_sensor_write_raw_hyst_value(
153 				&als_state->common_attributes, val, val2);
154 		break;
155 	default:
156 		ret = -EINVAL;
157 	}
158 
159 	return ret;
160 }
161 
162 static int als_write_raw_get_fmt(struct iio_dev *indio_dev,
163 			       struct iio_chan_spec const *chan,
164 			       long mask)
165 {
166 	return IIO_VAL_INT_PLUS_MICRO;
167 }
168 
169 static const struct iio_info als_info = {
170 	.driver_module = THIS_MODULE,
171 	.read_raw = &als_read_raw,
172 	.write_raw = &als_write_raw,
173 	.write_raw_get_fmt = &als_write_raw_get_fmt,
174 };
175 
176 /* Function to push data to buffer */
177 static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
178 {
179 	struct iio_buffer *buffer = indio_dev->buffer;
180 	int datum_sz;
181 
182 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
183 	if (!buffer) {
184 		dev_err(&indio_dev->dev, "Buffer == NULL\n");
185 		return;
186 	}
187 	datum_sz = buffer->access->get_bytes_per_datum(buffer);
188 	if (len > datum_sz) {
189 		dev_err(&indio_dev->dev, "Datum size mismatch %d:%d\n", len,
190 				datum_sz);
191 		return;
192 	}
193 	iio_push_to_buffer(buffer, (u8 *)data);
194 }
195 
196 /* Callback handler to send event after all samples are received and captured */
197 static int als_proc_event(struct hid_sensor_hub_device *hsdev,
198 				unsigned usage_id,
199 				void *priv)
200 {
201 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
202 	struct als_state *als_state = iio_priv(indio_dev);
203 
204 	dev_dbg(&indio_dev->dev, "als_proc_event [%d]\n",
205 				als_state->common_attributes.data_ready);
206 	if (als_state->common_attributes.data_ready)
207 		hid_sensor_push_data(indio_dev,
208 				(u8 *)&als_state->illum,
209 				sizeof(als_state->illum));
210 
211 	return 0;
212 }
213 
214 /* Capture samples in local storage */
215 static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
216 				unsigned usage_id,
217 				size_t raw_len, char *raw_data,
218 				void *priv)
219 {
220 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
221 	struct als_state *als_state = iio_priv(indio_dev);
222 	int ret = -EINVAL;
223 
224 	switch (usage_id) {
225 	case HID_USAGE_SENSOR_LIGHT_ILLUM:
226 		als_state->illum = *(u32 *)raw_data;
227 		ret = 0;
228 		break;
229 	default:
230 		break;
231 	}
232 
233 	return ret;
234 }
235 
236 /* Parse report which is specific to an usage id*/
237 static int als_parse_report(struct platform_device *pdev,
238 				struct hid_sensor_hub_device *hsdev,
239 				struct iio_chan_spec *channels,
240 				unsigned usage_id,
241 				struct als_state *st)
242 {
243 	int ret;
244 
245 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
246 			usage_id,
247 			HID_USAGE_SENSOR_LIGHT_ILLUM,
248 			&st->als_illum);
249 	if (ret < 0)
250 		return ret;
251 	als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
252 					st->als_illum.size);
253 
254 	dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
255 			st->als_illum.report_id);
256 
257 	return ret;
258 }
259 
260 /* Function to initialize the processing for usage id */
261 static int __devinit hid_als_probe(struct platform_device *pdev)
262 {
263 	int ret = 0;
264 	static const char *name = "als";
265 	struct iio_dev *indio_dev;
266 	struct als_state *als_state;
267 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
268 	struct iio_chan_spec *channels;
269 
270 	indio_dev = iio_device_alloc(sizeof(struct als_state));
271 	if (indio_dev == NULL) {
272 		ret = -ENOMEM;
273 		goto error_ret;
274 	}
275 	platform_set_drvdata(pdev, indio_dev);
276 
277 	als_state = iio_priv(indio_dev);
278 	als_state->common_attributes.hsdev = hsdev;
279 	als_state->common_attributes.pdev = pdev;
280 
281 	ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS,
282 					&als_state->common_attributes);
283 	if (ret) {
284 		dev_err(&pdev->dev, "failed to setup common attributes\n");
285 		goto error_free_dev;
286 	}
287 
288 	channels = kmemdup(als_channels,
289 					sizeof(als_channels),
290 					GFP_KERNEL);
291 	if (!channels) {
292 		dev_err(&pdev->dev, "failed to duplicate channels\n");
293 		goto error_free_dev;
294 	}
295 
296 	ret = als_parse_report(pdev, hsdev, channels,
297 				HID_USAGE_SENSOR_ALS, als_state);
298 	if (ret) {
299 		dev_err(&pdev->dev, "failed to setup attributes\n");
300 		goto error_free_dev_mem;
301 	}
302 
303 	indio_dev->channels = channels;
304 	indio_dev->num_channels =
305 				ARRAY_SIZE(als_channels);
306 	indio_dev->dev.parent = &pdev->dev;
307 	indio_dev->info = &als_info;
308 	indio_dev->name = name;
309 	indio_dev->modes = INDIO_DIRECT_MODE;
310 
311 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
312 		NULL, NULL);
313 	if (ret) {
314 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
315 		goto error_free_dev_mem;
316 	}
317 	als_state->common_attributes.data_ready = false;
318 	ret = hid_sensor_setup_trigger(indio_dev, name,
319 				&als_state->common_attributes);
320 	if (ret < 0) {
321 		dev_err(&pdev->dev, "trigger setup failed\n");
322 		goto error_unreg_buffer_funcs;
323 	}
324 
325 	ret = iio_device_register(indio_dev);
326 	if (ret) {
327 		dev_err(&pdev->dev, "device register failed\n");
328 		goto error_remove_trigger;
329 	}
330 
331 	als_state->callbacks.send_event = als_proc_event;
332 	als_state->callbacks.capture_sample = als_capture_sample;
333 	als_state->callbacks.pdev = pdev;
334 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ALS,
335 					&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);
347 error_unreg_buffer_funcs:
348 	iio_triggered_buffer_cleanup(indio_dev);
349 error_free_dev_mem:
350 	kfree(indio_dev->channels);
351 error_free_dev:
352 	iio_device_free(indio_dev);
353 error_ret:
354 	return ret;
355 }
356 
357 /* Function to deinitialize the processing for usage id */
358 static int __devinit hid_als_remove(struct platform_device *pdev)
359 {
360 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
361 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
362 
363 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS);
364 	iio_device_unregister(indio_dev);
365 	hid_sensor_remove_trigger(indio_dev);
366 	iio_triggered_buffer_cleanup(indio_dev);
367 	kfree(indio_dev->channels);
368 	iio_device_free(indio_dev);
369 
370 	return 0;
371 }
372 
373 static struct platform_driver hid_als_platform_driver = {
374 	.driver = {
375 		.name	= DRIVER_NAME,
376 		.owner	= THIS_MODULE,
377 	},
378 	.probe		= hid_als_probe,
379 	.remove		= hid_als_remove,
380 };
381 module_platform_driver(hid_als_platform_driver);
382 
383 MODULE_DESCRIPTION("HID Sensor ALS");
384 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
385 MODULE_LICENSE("GPL");
386