ecryptfs: convert to file_write_and_wait in ->fsync
[sfrench/cifs-2.6.git] / drivers / iio / common / hid-sensors / hid-sensor-attributes.c
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_GRAVITY_VECTOR, 0, 9, 806650000},
42         {HID_USAGE_SENSOR_GRAVITY_VECTOR,
43                 HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
44         {HID_USAGE_SENSOR_GRAVITY_VECTOR,
45                 HID_USAGE_SENSOR_UNITS_G, 9, 806650000},
46
47         {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453293},
48         {HID_USAGE_SENSOR_GYRO_3D,
49                 HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
50         {HID_USAGE_SENSOR_GYRO_3D,
51                 HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453293},
52
53         {HID_USAGE_SENSOR_COMPASS_3D, 0, 0, 1000000},
54         {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
55
56         {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 0, 17453293},
57         {HID_USAGE_SENSOR_INCLINOMETER_3D,
58                 HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453293},
59         {HID_USAGE_SENSOR_INCLINOMETER_3D,
60                 HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
61
62         {HID_USAGE_SENSOR_ALS, 0, 1, 0},
63         {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
64
65         {HID_USAGE_SENSOR_PRESSURE, 0, 100, 0},
66         {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 0, 1000000},
67
68         {HID_USAGE_SENSOR_TIME_TIMESTAMP, 0, 1000000000, 0},
69         {HID_USAGE_SENSOR_TIME_TIMESTAMP, HID_USAGE_SENSOR_UNITS_MILLISECOND,
70                 1000000, 0},
71
72         {HID_USAGE_SENSOR_DEVICE_ORIENTATION, 0, 1, 0},
73
74         {HID_USAGE_SENSOR_RELATIVE_ORIENTATION, 0, 1, 0},
75
76         {HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION, 0, 1, 0},
77
78         {HID_USAGE_SENSOR_TEMPERATURE, 0, 1000, 0},
79         {HID_USAGE_SENSOR_TEMPERATURE, HID_USAGE_SENSOR_UNITS_DEGREES, 1000, 0},
80
81         {HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
82 };
83
84 static int pow_10(unsigned power)
85 {
86         int i;
87         int ret = 1;
88         for (i = 0; i < power; ++i)
89                 ret = ret * 10;
90
91         return ret;
92 }
93
94 static void simple_div(int dividend, int divisor, int *whole,
95                                 int *micro_frac)
96 {
97         int rem;
98         int exp = 0;
99
100         *micro_frac = 0;
101         if (divisor == 0) {
102                 *whole = 0;
103                 return;
104         }
105         *whole = dividend/divisor;
106         rem = dividend % divisor;
107         if (rem) {
108                 while (rem <= divisor) {
109                         rem *= 10;
110                         exp++;
111                 }
112                 *micro_frac = (rem / divisor) * pow_10(6-exp);
113         }
114 }
115
116 static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
117 {
118         *val1 = no/pow_10(exp);
119         *val2 = no%pow_10(exp) * pow_10(6-exp);
120 }
121
122 /*
123 VTF format uses exponent and variable size format.
124 For example if the size is 2 bytes
125 0x0067 with VTF16E14 format -> +1.03
126 To convert just change to 0x67 to decimal and use two decimal as E14 stands
127 for 10^-2.
128 Negative numbers are 2's complement
129 */
130 static void convert_from_vtf_format(u32 value, int size, int exp,
131                                         int *val1, int *val2)
132 {
133         int sign = 1;
134
135         if (value & BIT(size*8 - 1)) {
136                 value =  ((1LL << (size * 8)) - value);
137                 sign = -1;
138         }
139         exp = hid_sensor_convert_exponent(exp);
140         if (exp >= 0) {
141                 *val1 = sign * value * pow_10(exp);
142                 *val2 = 0;
143         } else {
144                 split_micro_fraction(value, -exp, val1, val2);
145                 if (*val1)
146                         *val1 = sign * (*val1);
147                 else
148                         *val2 = sign * (*val2);
149         }
150 }
151
152 static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
153 {
154         u32 value;
155         int sign = 1;
156
157         if (val1 < 0 || val2 < 0)
158                 sign = -1;
159         exp = hid_sensor_convert_exponent(exp);
160         if (exp < 0) {
161                 value = abs(val1) * pow_10(-exp);
162                 value += abs(val2) / pow_10(6+exp);
163         } else
164                 value = abs(val1) / pow_10(exp);
165         if (sign < 0)
166                 value =  ((1LL << (size * 8)) - value);
167
168         return value;
169 }
170
171 s32 hid_sensor_read_poll_value(struct hid_sensor_common *st)
172 {
173         s32 value = 0;
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
180         if (ret < 0 || value < 0) {
181                 return -EINVAL;
182         } else {
183                 if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
184                         value = value * 1000;
185         }
186
187         return value;
188 }
189 EXPORT_SYMBOL(hid_sensor_read_poll_value);
190
191 int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
192                                 int *val1, int *val2)
193 {
194         s32 value;
195         int ret;
196
197         ret = sensor_hub_get_feature(st->hsdev,
198                                      st->poll.report_id,
199                                      st->poll.index, sizeof(value), &value);
200         if (ret < 0 || value < 0) {
201                 *val1 = *val2 = 0;
202                 return -EINVAL;
203         } else {
204                 if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
205                         simple_div(1000, value, val1, val2);
206                 else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
207                         simple_div(1, value, val1, val2);
208                 else {
209                         *val1 = *val2 = 0;
210                         return -EINVAL;
211                 }
212         }
213
214         return IIO_VAL_INT_PLUS_MICRO;
215 }
216 EXPORT_SYMBOL(hid_sensor_read_samp_freq_value);
217
218 int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
219                                 int val1, int val2)
220 {
221         s32 value;
222         int ret;
223
224         if (val1 < 0 || val2 < 0)
225                 return -EINVAL;
226
227         value = val1 * pow_10(6) + val2;
228         if (value) {
229                 if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
230                         value = pow_10(9)/value;
231                 else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
232                         value = pow_10(6)/value;
233                 else
234                         value = 0;
235         }
236         ret = sensor_hub_set_feature(st->hsdev, st->poll.report_id,
237                                      st->poll.index, sizeof(value), &value);
238         if (ret < 0 || value < 0)
239                 return -EINVAL;
240
241         ret = sensor_hub_get_feature(st->hsdev,
242                                      st->poll.report_id,
243                                      st->poll.index, sizeof(value), &value);
244         if (ret < 0 || value < 0)
245                 return -EINVAL;
246
247         st->poll_interval = value;
248
249         return 0;
250 }
251 EXPORT_SYMBOL(hid_sensor_write_samp_freq_value);
252
253 int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
254                                 int *val1, int *val2)
255 {
256         s32 value;
257         int ret;
258
259         ret = sensor_hub_get_feature(st->hsdev,
260                                      st->sensitivity.report_id,
261                                      st->sensitivity.index, sizeof(value),
262                                      &value);
263         if (ret < 0 || value < 0) {
264                 *val1 = *val2 = 0;
265                 return -EINVAL;
266         } else {
267                 convert_from_vtf_format(value, st->sensitivity.size,
268                                         st->sensitivity.unit_expo,
269                                         val1, val2);
270         }
271
272         return IIO_VAL_INT_PLUS_MICRO;
273 }
274 EXPORT_SYMBOL(hid_sensor_read_raw_hyst_value);
275
276 int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
277                                         int val1, int val2)
278 {
279         s32 value;
280         int ret;
281
282         if (val1 < 0 || val2 < 0)
283                 return -EINVAL;
284
285         value = convert_to_vtf_format(st->sensitivity.size,
286                                 st->sensitivity.unit_expo,
287                                 val1, val2);
288         ret = sensor_hub_set_feature(st->hsdev, st->sensitivity.report_id,
289                                      st->sensitivity.index, sizeof(value),
290                                      &value);
291         if (ret < 0 || value < 0)
292                 return -EINVAL;
293
294         ret = sensor_hub_get_feature(st->hsdev,
295                                      st->sensitivity.report_id,
296                                      st->sensitivity.index, sizeof(value),
297                                      &value);
298         if (ret < 0 || value < 0)
299                 return -EINVAL;
300
301         st->raw_hystersis = value;
302
303         return 0;
304 }
305 EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
306
307 /*
308  * This fuction applies the unit exponent to the scale.
309  * For example:
310  * 9.806650000 ->exp:2-> val0[980]val1[665000000]
311  * 9.000806000 ->exp:2-> val0[900]val1[80600000]
312  * 0.174535293 ->exp:2-> val0[17]val1[453529300]
313  * 1.001745329 ->exp:0-> val0[1]val1[1745329]
314  * 1.001745329 ->exp:2-> val0[100]val1[174532900]
315  * 1.001745329 ->exp:4-> val0[10017]val1[453290000]
316  * 9.806650000 ->exp:-2-> val0[0]val1[98066500]
317  */
318 static void adjust_exponent_nano(int *val0, int *val1, int scale0,
319                                   int scale1, int exp)
320 {
321         int i;
322         int x;
323         int res;
324         int rem;
325
326         if (exp > 0) {
327                 *val0 = scale0 * pow_10(exp);
328                 res = 0;
329                 if (exp > 9) {
330                         *val1 = 0;
331                         return;
332                 }
333                 for (i = 0; i < exp; ++i) {
334                         x = scale1 / pow_10(8 - i);
335                         res += (pow_10(exp - 1 - i) * x);
336                         scale1 = scale1 % pow_10(8 - i);
337                 }
338                 *val0 += res;
339                         *val1 = scale1 * pow_10(exp);
340         } else if (exp < 0) {
341                 exp = abs(exp);
342                 if (exp > 9) {
343                         *val0 = *val1 = 0;
344                         return;
345                 }
346                 *val0 = scale0 / pow_10(exp);
347                 rem = scale0 % pow_10(exp);
348                 res = 0;
349                 for (i = 0; i < (9 - exp); ++i) {
350                         x = scale1 / pow_10(8 - i);
351                         res += (pow_10(8 - exp - i) * x);
352                         scale1 = scale1 % pow_10(8 - i);
353                 }
354                 *val1 = rem * pow_10(9 - exp) + res;
355         } else {
356                 *val0 = scale0;
357                 *val1 = scale1;
358         }
359 }
360
361 int hid_sensor_format_scale(u32 usage_id,
362                         struct hid_sensor_hub_attribute_info *attr_info,
363                         int *val0, int *val1)
364 {
365         int i;
366         int exp;
367
368         *val0 = 1;
369         *val1 = 0;
370
371         for (i = 0; i < ARRAY_SIZE(unit_conversion); ++i) {
372                 if (unit_conversion[i].usage_id == usage_id &&
373                         unit_conversion[i].unit == attr_info->units) {
374                         exp  = hid_sensor_convert_exponent(
375                                                 attr_info->unit_expo);
376                         adjust_exponent_nano(val0, val1,
377                                         unit_conversion[i].scale_val0,
378                                         unit_conversion[i].scale_val1, exp);
379                         break;
380                 }
381         }
382
383         return IIO_VAL_INT_PLUS_NANO;
384 }
385 EXPORT_SYMBOL(hid_sensor_format_scale);
386
387 int64_t hid_sensor_convert_timestamp(struct hid_sensor_common *st,
388                                      int64_t raw_value)
389 {
390         return st->timestamp_ns_scale * raw_value;
391 }
392 EXPORT_SYMBOL(hid_sensor_convert_timestamp);
393
394 static
395 int hid_sensor_get_reporting_interval(struct hid_sensor_hub_device *hsdev,
396                                         u32 usage_id,
397                                         struct hid_sensor_common *st)
398 {
399         sensor_hub_input_get_attribute_info(hsdev,
400                                         HID_FEATURE_REPORT, usage_id,
401                                         HID_USAGE_SENSOR_PROP_REPORT_INTERVAL,
402                                         &st->poll);
403         /* Default unit of measure is milliseconds */
404         if (st->poll.units == 0)
405                 st->poll.units = HID_USAGE_SENSOR_UNITS_MILLISECOND;
406
407         st->poll_interval = -1;
408
409         return 0;
410
411 }
412
413 static void hid_sensor_get_report_latency_info(struct hid_sensor_hub_device *hsdev,
414                                                u32 usage_id,
415                                                struct hid_sensor_common *st)
416 {
417         sensor_hub_input_get_attribute_info(hsdev, HID_FEATURE_REPORT,
418                                             usage_id,
419                                             HID_USAGE_SENSOR_PROP_REPORT_LATENCY,
420                                             &st->report_latency);
421
422         hid_dbg(hsdev->hdev, "Report latency attributes: %x:%x\n",
423                 st->report_latency.index, st->report_latency.report_id);
424 }
425
426 int hid_sensor_get_report_latency(struct hid_sensor_common *st)
427 {
428         int ret;
429         int value;
430
431         ret = sensor_hub_get_feature(st->hsdev, st->report_latency.report_id,
432                                      st->report_latency.index, sizeof(value),
433                                      &value);
434         if (ret < 0)
435                 return ret;
436
437         return value;
438 }
439 EXPORT_SYMBOL(hid_sensor_get_report_latency);
440
441 int hid_sensor_set_report_latency(struct hid_sensor_common *st, int latency_ms)
442 {
443         return sensor_hub_set_feature(st->hsdev, st->report_latency.report_id,
444                                       st->report_latency.index,
445                                       sizeof(latency_ms), &latency_ms);
446 }
447 EXPORT_SYMBOL(hid_sensor_set_report_latency);
448
449 bool hid_sensor_batch_mode_supported(struct hid_sensor_common *st)
450 {
451         return st->report_latency.index > 0 && st->report_latency.report_id > 0;
452 }
453 EXPORT_SYMBOL(hid_sensor_batch_mode_supported);
454
455 int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
456                                         u32 usage_id,
457                                         struct hid_sensor_common *st)
458 {
459
460         struct hid_sensor_hub_attribute_info timestamp;
461         s32 value;
462         int ret;
463
464         hid_sensor_get_reporting_interval(hsdev, usage_id, st);
465
466         sensor_hub_input_get_attribute_info(hsdev,
467                                         HID_FEATURE_REPORT, usage_id,
468                                         HID_USAGE_SENSOR_PROP_REPORT_STATE,
469                                         &st->report_state);
470
471         sensor_hub_input_get_attribute_info(hsdev,
472                                         HID_FEATURE_REPORT, usage_id,
473                                         HID_USAGE_SENSOR_PROY_POWER_STATE,
474                                         &st->power_state);
475
476         sensor_hub_input_get_attribute_info(hsdev,
477                         HID_FEATURE_REPORT, usage_id,
478                         HID_USAGE_SENSOR_PROP_SENSITIVITY_ABS,
479                          &st->sensitivity);
480
481         st->raw_hystersis = -1;
482
483         sensor_hub_input_get_attribute_info(hsdev,
484                                             HID_INPUT_REPORT, usage_id,
485                                             HID_USAGE_SENSOR_TIME_TIMESTAMP,
486                                             &timestamp);
487         if (timestamp.index >= 0 && timestamp.report_id) {
488                 int val0, val1;
489
490                 hid_sensor_format_scale(HID_USAGE_SENSOR_TIME_TIMESTAMP,
491                                         &timestamp, &val0, &val1);
492                 st->timestamp_ns_scale = val0;
493         } else
494                 st->timestamp_ns_scale = 1000000000;
495
496         hid_sensor_get_report_latency_info(hsdev, usage_id, st);
497
498         hid_dbg(hsdev->hdev, "common attributes: %x:%x, %x:%x, %x:%x %x:%x %x:%x\n",
499                 st->poll.index, st->poll.report_id,
500                 st->report_state.index, st->report_state.report_id,
501                 st->power_state.index, st->power_state.report_id,
502                 st->sensitivity.index, st->sensitivity.report_id,
503                 timestamp.index, timestamp.report_id);
504
505         ret = sensor_hub_get_feature(hsdev,
506                                 st->power_state.report_id,
507                                 st->power_state.index, sizeof(value), &value);
508         if (ret < 0)
509                 return ret;
510         if (value < 0)
511                 return -EINVAL;
512
513         return 0;
514 }
515 EXPORT_SYMBOL(hid_sensor_parse_common_attributes);
516
517 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
518 MODULE_DESCRIPTION("HID Sensor common attribute processing");
519 MODULE_LICENSE("GPL");