• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 static struct {
30 	u32 usage_id;
31 	int unit; /* 0 for default others from HID sensor spec */
32 	int scale_val0; /* scale, whole number */
33 	int scale_val1; /* scale, fraction in nanos */
34 } unit_conversion[] = {
35 	{HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650000},
36 	{HID_USAGE_SENSOR_ACCEL_3D,
37 		HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
38 	{HID_USAGE_SENSOR_ACCEL_3D,
39 		HID_USAGE_SENSOR_UNITS_G, 9, 806650000},
40 
41 	{HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453293},
42 	{HID_USAGE_SENSOR_GYRO_3D,
43 		HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
44 	{HID_USAGE_SENSOR_GYRO_3D,
45 		HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453293},
46 
47 	{HID_USAGE_SENSOR_COMPASS_3D, 0, 0, 1000000},
48 	{HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
49 
50 	{HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 0, 17453293},
51 	{HID_USAGE_SENSOR_INCLINOMETER_3D,
52 		HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453293},
53 	{HID_USAGE_SENSOR_INCLINOMETER_3D,
54 		HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
55 
56 	{HID_USAGE_SENSOR_ALS, 0, 1, 0},
57 	{HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
58 
59 	{HID_USAGE_SENSOR_PRESSURE, 0, 100, 0},
60 	{HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 0, 1000000},
61 };
62 
pow_10(unsigned power)63 static int pow_10(unsigned power)
64 {
65 	int i;
66 	int ret = 1;
67 	for (i = 0; i < power; ++i)
68 		ret = ret * 10;
69 
70 	return ret;
71 }
72 
simple_div(int dividend,int divisor,int * whole,int * micro_frac)73 static void simple_div(int dividend, int divisor, int *whole,
74 				int *micro_frac)
75 {
76 	int rem;
77 	int exp = 0;
78 
79 	*micro_frac = 0;
80 	if (divisor == 0) {
81 		*whole = 0;
82 		return;
83 	}
84 	*whole = dividend/divisor;
85 	rem = dividend % divisor;
86 	if (rem) {
87 		while (rem <= divisor) {
88 			rem *= 10;
89 			exp++;
90 		}
91 		*micro_frac = (rem / divisor) * pow_10(6-exp);
92 	}
93 }
94 
split_micro_fraction(unsigned int no,int exp,int * val1,int * val2)95 static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
96 {
97 	*val1 = no/pow_10(exp);
98 	*val2 = no%pow_10(exp) * pow_10(6-exp);
99 }
100 
101 /*
102 VTF format uses exponent and variable size format.
103 For example if the size is 2 bytes
104 0x0067 with VTF16E14 format -> +1.03
105 To convert just change to 0x67 to decimal and use two decimal as E14 stands
106 for 10^-2.
107 Negative numbers are 2's complement
108 */
convert_from_vtf_format(u32 value,int size,int exp,int * val1,int * val2)109 static void convert_from_vtf_format(u32 value, int size, int exp,
110 					int *val1, int *val2)
111 {
112 	int sign = 1;
113 
114 	if (value & BIT(size*8 - 1)) {
115 		value =  ((1LL << (size * 8)) - value);
116 		sign = -1;
117 	}
118 	exp = hid_sensor_convert_exponent(exp);
119 	if (exp >= 0) {
120 		*val1 = sign * value * pow_10(exp);
121 		*val2 = 0;
122 	} else {
123 		split_micro_fraction(value, -exp, val1, val2);
124 		if (*val1)
125 			*val1 = sign * (*val1);
126 		else
127 			*val2 = sign * (*val2);
128 	}
129 }
130 
convert_to_vtf_format(int size,int exp,int val1,int val2)131 static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
132 {
133 	u32 value;
134 	int sign = 1;
135 
136 	if (val1 < 0 || val2 < 0)
137 		sign = -1;
138 	exp = hid_sensor_convert_exponent(exp);
139 	if (exp < 0) {
140 		value = abs(val1) * pow_10(-exp);
141 		value += abs(val2) / pow_10(6+exp);
142 	} else
143 		value = abs(val1) / pow_10(exp);
144 	if (sign < 0)
145 		value =  ((1LL << (size * 8)) - value);
146 
147 	return value;
148 }
149 
hid_sensor_read_poll_value(struct hid_sensor_common * st)150 s32 hid_sensor_read_poll_value(struct hid_sensor_common *st)
151 {
152 	s32 value = 0;
153 	int ret;
154 
155 	ret = sensor_hub_get_feature(st->hsdev,
156 				     st->poll.report_id,
157 				     st->poll.index, sizeof(value), &value);
158 
159 	if (ret < 0 || value < 0) {
160 		return -EINVAL;
161 	} else {
162 		if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
163 			value = value * 1000;
164 	}
165 
166 	return value;
167 }
168 EXPORT_SYMBOL(hid_sensor_read_poll_value);
169 
hid_sensor_read_samp_freq_value(struct hid_sensor_common * st,int * val1,int * val2)170 int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
171 				int *val1, int *val2)
172 {
173 	s32 value;
174 	int ret;
175 
176 	ret = sensor_hub_get_feature(st->hsdev,
177 				     st->poll.report_id,
178 				     st->poll.index, sizeof(value), &value);
179 	if (ret < 0 || value < 0) {
180 		*val1 = *val2 = 0;
181 		return -EINVAL;
182 	} else {
183 		if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
184 			simple_div(1000, value, val1, val2);
185 		else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
186 			simple_div(1, value, val1, val2);
187 		else {
188 			*val1 = *val2 = 0;
189 			return -EINVAL;
190 		}
191 	}
192 
193 	return IIO_VAL_INT_PLUS_MICRO;
194 }
195 EXPORT_SYMBOL(hid_sensor_read_samp_freq_value);
196 
hid_sensor_write_samp_freq_value(struct hid_sensor_common * st,int val1,int val2)197 int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
198 				int val1, int val2)
199 {
200 	s32 value;
201 	int ret;
202 
203 	if (val1 < 0 || val2 < 0)
204 		ret = -EINVAL;
205 
206 	value = val1 * pow_10(6) + val2;
207 	if (value) {
208 		if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
209 			value = pow_10(9)/value;
210 		else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
211 			value = pow_10(6)/value;
212 		else
213 			value = 0;
214 	}
215 	ret = sensor_hub_set_feature(st->hsdev, st->poll.report_id,
216 				     st->poll.index, sizeof(value), &value);
217 	if (ret < 0 || value < 0)
218 		return -EINVAL;
219 
220 	ret = sensor_hub_get_feature(st->hsdev,
221 				     st->poll.report_id,
222 				     st->poll.index, sizeof(value), &value);
223 	if (ret < 0 || value < 0)
224 		return -EINVAL;
225 
226 	st->poll_interval = value;
227 
228 	return 0;
229 }
230 EXPORT_SYMBOL(hid_sensor_write_samp_freq_value);
231 
hid_sensor_read_raw_hyst_value(struct hid_sensor_common * st,int * val1,int * val2)232 int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
233 				int *val1, int *val2)
234 {
235 	s32 value;
236 	int ret;
237 
238 	ret = sensor_hub_get_feature(st->hsdev,
239 				     st->sensitivity.report_id,
240 				     st->sensitivity.index, sizeof(value),
241 				     &value);
242 	if (ret < 0 || value < 0) {
243 		*val1 = *val2 = 0;
244 		return -EINVAL;
245 	} else {
246 		convert_from_vtf_format(value, st->sensitivity.size,
247 					st->sensitivity.unit_expo,
248 					val1, val2);
249 	}
250 
251 	return IIO_VAL_INT_PLUS_MICRO;
252 }
253 EXPORT_SYMBOL(hid_sensor_read_raw_hyst_value);
254 
hid_sensor_write_raw_hyst_value(struct hid_sensor_common * st,int val1,int val2)255 int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
256 					int val1, int val2)
257 {
258 	s32 value;
259 	int ret;
260 
261 	value = convert_to_vtf_format(st->sensitivity.size,
262 				st->sensitivity.unit_expo,
263 				val1, val2);
264 	ret = sensor_hub_set_feature(st->hsdev, st->sensitivity.report_id,
265 				     st->sensitivity.index, sizeof(value),
266 				     &value);
267 	if (ret < 0 || value < 0)
268 		return -EINVAL;
269 
270 	ret = sensor_hub_get_feature(st->hsdev,
271 				     st->sensitivity.report_id,
272 				     st->sensitivity.index, sizeof(value),
273 				     &value);
274 	if (ret < 0 || value < 0)
275 		return -EINVAL;
276 
277 	st->raw_hystersis = value;
278 
279 	return 0;
280 }
281 EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
282 
283 /*
284  * This fuction applies the unit exponent to the scale.
285  * For example:
286  * 9.806650000 ->exp:2-> val0[980]val1[665000000]
287  * 9.000806000 ->exp:2-> val0[900]val1[80600000]
288  * 0.174535293 ->exp:2-> val0[17]val1[453529300]
289  * 1.001745329 ->exp:0-> val0[1]val1[1745329]
290  * 1.001745329 ->exp:2-> val0[100]val1[174532900]
291  * 1.001745329 ->exp:4-> val0[10017]val1[453290000]
292  * 9.806650000 ->exp:-2-> val0[0]val1[98066500]
293  */
adjust_exponent_nano(int * val0,int * val1,int scale0,int scale1,int exp)294 static void adjust_exponent_nano(int *val0, int *val1, int scale0,
295 				  int scale1, int exp)
296 {
297 	int i;
298 	int x;
299 	int res;
300 	int rem;
301 
302 	if (exp > 0) {
303 		*val0 = scale0 * pow_10(exp);
304 		res = 0;
305 		if (exp > 9) {
306 			*val1 = 0;
307 			return;
308 		}
309 		for (i = 0; i < exp; ++i) {
310 			x = scale1 / pow_10(8 - i);
311 			res += (pow_10(exp - 1 - i) * x);
312 			scale1 = scale1 % pow_10(8 - i);
313 		}
314 		*val0 += res;
315 			*val1 = scale1 * pow_10(exp);
316 	} else if (exp < 0) {
317 		exp = abs(exp);
318 		if (exp > 9) {
319 			*val0 = *val1 = 0;
320 			return;
321 		}
322 		*val0 = scale0 / pow_10(exp);
323 		rem = scale0 % pow_10(exp);
324 		res = 0;
325 		for (i = 0; i < (9 - exp); ++i) {
326 			x = scale1 / pow_10(8 - i);
327 			res += (pow_10(8 - exp - i) * x);
328 			scale1 = scale1 % pow_10(8 - i);
329 		}
330 		*val1 = rem * pow_10(9 - exp) + res;
331 	} else {
332 		*val0 = scale0;
333 		*val1 = scale1;
334 	}
335 }
336 
hid_sensor_format_scale(u32 usage_id,struct hid_sensor_hub_attribute_info * attr_info,int * val0,int * val1)337 int hid_sensor_format_scale(u32 usage_id,
338 			struct hid_sensor_hub_attribute_info *attr_info,
339 			int *val0, int *val1)
340 {
341 	int i;
342 	int exp;
343 
344 	*val0 = 1;
345 	*val1 = 0;
346 
347 	for (i = 0; i < ARRAY_SIZE(unit_conversion); ++i) {
348 		if (unit_conversion[i].usage_id == usage_id &&
349 			unit_conversion[i].unit == attr_info->units) {
350 			exp  = hid_sensor_convert_exponent(
351 						attr_info->unit_expo);
352 			adjust_exponent_nano(val0, val1,
353 					unit_conversion[i].scale_val0,
354 					unit_conversion[i].scale_val1, exp);
355 			break;
356 		}
357 	}
358 
359 	return IIO_VAL_INT_PLUS_NANO;
360 }
361 EXPORT_SYMBOL(hid_sensor_format_scale);
362 
363 static
hid_sensor_get_reporting_interval(struct hid_sensor_hub_device * hsdev,u32 usage_id,struct hid_sensor_common * st)364 int hid_sensor_get_reporting_interval(struct hid_sensor_hub_device *hsdev,
365 					u32 usage_id,
366 					struct hid_sensor_common *st)
367 {
368 	sensor_hub_input_get_attribute_info(hsdev,
369 					HID_FEATURE_REPORT, usage_id,
370 					HID_USAGE_SENSOR_PROP_REPORT_INTERVAL,
371 					&st->poll);
372 	/* Default unit of measure is milliseconds */
373 	if (st->poll.units == 0)
374 		st->poll.units = HID_USAGE_SENSOR_UNITS_MILLISECOND;
375 
376 	st->poll_interval = -1;
377 
378 	return 0;
379 
380 }
381 
hid_sensor_parse_common_attributes(struct hid_sensor_hub_device * hsdev,u32 usage_id,struct hid_sensor_common * st)382 int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
383 					u32 usage_id,
384 					struct hid_sensor_common *st)
385 {
386 
387 
388 	hid_sensor_get_reporting_interval(hsdev, usage_id, st);
389 
390 	sensor_hub_input_get_attribute_info(hsdev,
391 					HID_FEATURE_REPORT, usage_id,
392 					HID_USAGE_SENSOR_PROP_REPORT_STATE,
393 					&st->report_state);
394 
395 	sensor_hub_input_get_attribute_info(hsdev,
396 					HID_FEATURE_REPORT, usage_id,
397 					HID_USAGE_SENSOR_PROY_POWER_STATE,
398 					&st->power_state);
399 
400 	st->raw_hystersis = -1;
401 
402 	sensor_hub_input_get_attribute_info(hsdev,
403 			HID_FEATURE_REPORT, usage_id,
404 			HID_USAGE_SENSOR_PROP_SENSITIVITY_ABS,
405 			 &st->sensitivity);
406 
407 	hid_dbg(hsdev->hdev, "common attributes: %x:%x, %x:%x, %x:%x %x:%x\n",
408 			st->poll.index, st->poll.report_id,
409 			st->report_state.index, st->report_state.report_id,
410 			st->power_state.index, st->power_state.report_id,
411 			st->sensitivity.index, st->sensitivity.report_id);
412 
413 	return 0;
414 }
415 EXPORT_SYMBOL(hid_sensor_parse_common_attributes);
416 
417 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
418 MODULE_DESCRIPTION("HID Sensor common attribute processing");
419 MODULE_LICENSE("GPL");
420