hid-sensor-accel-3d.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. enum accel_3d_channel {
  34. CHANNEL_SCAN_INDEX_X,
  35. CHANNEL_SCAN_INDEX_Y,
  36. CHANNEL_SCAN_INDEX_Z,
  37. ACCEL_3D_CHANNEL_MAX,
  38. };
  39. struct accel_3d_state {
  40. struct hid_sensor_hub_callbacks callbacks;
  41. struct hid_sensor_common common_attributes;
  42. struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX];
  43. /* Ensure timestamp is naturally aligned */
  44. struct {
  45. u32 accel_val[3];
  46. s64 timestamp __aligned(8);
  47. } scan;
  48. int scale_pre_decml;
  49. int scale_post_decml;
  50. int scale_precision;
  51. int value_offset;
  52. int64_t timestamp;
  53. };
  54. static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = {
  55. HID_USAGE_SENSOR_ACCEL_X_AXIS,
  56. HID_USAGE_SENSOR_ACCEL_Y_AXIS,
  57. HID_USAGE_SENSOR_ACCEL_Z_AXIS
  58. };
  59. /* Channel definitions */
  60. static const struct iio_chan_spec accel_3d_channels[] = {
  61. {
  62. .type = IIO_ACCEL,
  63. .modified = 1,
  64. .channel2 = IIO_MOD_X,
  65. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  66. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  67. BIT(IIO_CHAN_INFO_SCALE) |
  68. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  69. BIT(IIO_CHAN_INFO_HYSTERESIS),
  70. .scan_index = CHANNEL_SCAN_INDEX_X,
  71. }, {
  72. .type = IIO_ACCEL,
  73. .modified = 1,
  74. .channel2 = IIO_MOD_Y,
  75. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  76. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  77. BIT(IIO_CHAN_INFO_SCALE) |
  78. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  79. BIT(IIO_CHAN_INFO_HYSTERESIS),
  80. .scan_index = CHANNEL_SCAN_INDEX_Y,
  81. }, {
  82. .type = IIO_ACCEL,
  83. .modified = 1,
  84. .channel2 = IIO_MOD_Z,
  85. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  86. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  87. BIT(IIO_CHAN_INFO_SCALE) |
  88. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  89. BIT(IIO_CHAN_INFO_HYSTERESIS),
  90. .scan_index = CHANNEL_SCAN_INDEX_Z,
  91. },
  92. IIO_CHAN_SOFT_TIMESTAMP(3)
  93. };
  94. /* Channel definitions */
  95. static const struct iio_chan_spec gravity_channels[] = {
  96. {
  97. .type = IIO_GRAVITY,
  98. .modified = 1,
  99. .channel2 = IIO_MOD_X,
  100. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  101. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  102. BIT(IIO_CHAN_INFO_SCALE) |
  103. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  104. BIT(IIO_CHAN_INFO_HYSTERESIS),
  105. .scan_index = CHANNEL_SCAN_INDEX_X,
  106. }, {
  107. .type = IIO_GRAVITY,
  108. .modified = 1,
  109. .channel2 = IIO_MOD_Y,
  110. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  111. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  112. BIT(IIO_CHAN_INFO_SCALE) |
  113. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  114. BIT(IIO_CHAN_INFO_HYSTERESIS),
  115. .scan_index = CHANNEL_SCAN_INDEX_Y,
  116. }, {
  117. .type = IIO_GRAVITY,
  118. .modified = 1,
  119. .channel2 = IIO_MOD_Z,
  120. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  121. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  122. BIT(IIO_CHAN_INFO_SCALE) |
  123. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  124. BIT(IIO_CHAN_INFO_HYSTERESIS),
  125. .scan_index = CHANNEL_SCAN_INDEX_Z,
  126. }
  127. };
  128. /* Adjust channel real bits based on report descriptor */
  129. static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
  130. int channel, int size)
  131. {
  132. channels[channel].scan_type.sign = 's';
  133. /* Real storage bits will change based on the report desc. */
  134. channels[channel].scan_type.realbits = size * 8;
  135. /* Maximum size of a sample to capture is u32 */
  136. channels[channel].scan_type.storagebits = sizeof(u32) * 8;
  137. }
  138. /* Channel read_raw handler */
  139. static int accel_3d_read_raw(struct iio_dev *indio_dev,
  140. struct iio_chan_spec const *chan,
  141. int *val, int *val2,
  142. long mask)
  143. {
  144. struct accel_3d_state *accel_state = iio_priv(indio_dev);
  145. int report_id = -1;
  146. u32 address;
  147. int ret_type;
  148. s32 min;
  149. struct hid_sensor_hub_device *hsdev =
  150. accel_state->common_attributes.hsdev;
  151. *val = 0;
  152. *val2 = 0;
  153. switch (mask) {
  154. case 0:
  155. hid_sensor_power_state(&accel_state->common_attributes, true);
  156. report_id = accel_state->accel[chan->scan_index].report_id;
  157. min = accel_state->accel[chan->scan_index].logical_minimum;
  158. address = accel_3d_addresses[chan->scan_index];
  159. if (report_id >= 0)
  160. *val = sensor_hub_input_attr_get_raw_value(
  161. accel_state->common_attributes.hsdev,
  162. hsdev->usage, address, report_id,
  163. SENSOR_HUB_SYNC,
  164. min < 0);
  165. else {
  166. *val = 0;
  167. hid_sensor_power_state(&accel_state->common_attributes,
  168. false);
  169. return -EINVAL;
  170. }
  171. hid_sensor_power_state(&accel_state->common_attributes, false);
  172. ret_type = IIO_VAL_INT;
  173. break;
  174. case IIO_CHAN_INFO_SCALE:
  175. *val = accel_state->scale_pre_decml;
  176. *val2 = accel_state->scale_post_decml;
  177. ret_type = accel_state->scale_precision;
  178. break;
  179. case IIO_CHAN_INFO_OFFSET:
  180. *val = accel_state->value_offset;
  181. ret_type = IIO_VAL_INT;
  182. break;
  183. case IIO_CHAN_INFO_SAMP_FREQ:
  184. ret_type = hid_sensor_read_samp_freq_value(
  185. &accel_state->common_attributes, val, val2);
  186. break;
  187. case IIO_CHAN_INFO_HYSTERESIS:
  188. ret_type = hid_sensor_read_raw_hyst_value(
  189. &accel_state->common_attributes, val, val2);
  190. break;
  191. default:
  192. ret_type = -EINVAL;
  193. break;
  194. }
  195. return ret_type;
  196. }
  197. /* Channel write_raw handler */
  198. static int accel_3d_write_raw(struct iio_dev *indio_dev,
  199. struct iio_chan_spec const *chan,
  200. int val,
  201. int val2,
  202. long mask)
  203. {
  204. struct accel_3d_state *accel_state = iio_priv(indio_dev);
  205. int ret = 0;
  206. switch (mask) {
  207. case IIO_CHAN_INFO_SAMP_FREQ:
  208. ret = hid_sensor_write_samp_freq_value(
  209. &accel_state->common_attributes, val, val2);
  210. break;
  211. case IIO_CHAN_INFO_HYSTERESIS:
  212. ret = hid_sensor_write_raw_hyst_value(
  213. &accel_state->common_attributes, val, val2);
  214. break;
  215. default:
  216. ret = -EINVAL;
  217. }
  218. return ret;
  219. }
  220. static const struct iio_info accel_3d_info = {
  221. .driver_module = THIS_MODULE,
  222. .read_raw = &accel_3d_read_raw,
  223. .write_raw = &accel_3d_write_raw,
  224. };
  225. /* Function to push data to buffer */
  226. static void hid_sensor_push_data(struct iio_dev *indio_dev, void *data,
  227. int len, int64_t timestamp)
  228. {
  229. dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
  230. iio_push_to_buffers_with_timestamp(indio_dev, data, timestamp);
  231. }
  232. /* Callback handler to send event after all samples are received and captured */
  233. static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
  234. unsigned usage_id,
  235. void *priv)
  236. {
  237. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  238. struct accel_3d_state *accel_state = iio_priv(indio_dev);
  239. dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n");
  240. if (atomic_read(&accel_state->common_attributes.data_ready)) {
  241. if (!accel_state->timestamp)
  242. accel_state->timestamp = iio_get_time_ns(indio_dev);
  243. hid_sensor_push_data(indio_dev,
  244. &accel_state->scan,
  245. sizeof(accel_state->scan),
  246. accel_state->timestamp);
  247. accel_state->timestamp = 0;
  248. }
  249. return 0;
  250. }
  251. /* Capture samples in local storage */
  252. static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
  253. unsigned usage_id,
  254. size_t raw_len, char *raw_data,
  255. void *priv)
  256. {
  257. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  258. struct accel_3d_state *accel_state = iio_priv(indio_dev);
  259. int offset;
  260. int ret = -EINVAL;
  261. switch (usage_id) {
  262. case HID_USAGE_SENSOR_ACCEL_X_AXIS:
  263. case HID_USAGE_SENSOR_ACCEL_Y_AXIS:
  264. case HID_USAGE_SENSOR_ACCEL_Z_AXIS:
  265. offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS;
  266. accel_state->scan.accel_val[CHANNEL_SCAN_INDEX_X + offset] =
  267. *(u32 *)raw_data;
  268. ret = 0;
  269. break;
  270. case HID_USAGE_SENSOR_TIME_TIMESTAMP:
  271. accel_state->timestamp =
  272. hid_sensor_convert_timestamp(
  273. &accel_state->common_attributes,
  274. *(int64_t *)raw_data);
  275. break;
  276. default:
  277. break;
  278. }
  279. return ret;
  280. }
  281. /* Parse report which is specific to an usage id*/
  282. static int accel_3d_parse_report(struct platform_device *pdev,
  283. struct hid_sensor_hub_device *hsdev,
  284. struct iio_chan_spec *channels,
  285. unsigned usage_id,
  286. struct accel_3d_state *st)
  287. {
  288. int ret;
  289. int i;
  290. for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
  291. ret = sensor_hub_input_get_attribute_info(hsdev,
  292. HID_INPUT_REPORT,
  293. usage_id,
  294. HID_USAGE_SENSOR_ACCEL_X_AXIS + i,
  295. &st->accel[CHANNEL_SCAN_INDEX_X + i]);
  296. if (ret < 0)
  297. break;
  298. accel_3d_adjust_channel_bit_mask(channels,
  299. CHANNEL_SCAN_INDEX_X + i,
  300. st->accel[CHANNEL_SCAN_INDEX_X + i].size);
  301. }
  302. dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
  303. st->accel[0].index,
  304. st->accel[0].report_id,
  305. st->accel[1].index, st->accel[1].report_id,
  306. st->accel[2].index, st->accel[2].report_id);
  307. st->scale_precision = hid_sensor_format_scale(
  308. hsdev->usage,
  309. &st->accel[CHANNEL_SCAN_INDEX_X],
  310. &st->scale_pre_decml, &st->scale_post_decml);
  311. /* Set Sensitivity field ids, when there is no individual modifier */
  312. if (st->common_attributes.sensitivity.index < 0) {
  313. sensor_hub_input_get_attribute_info(hsdev,
  314. HID_FEATURE_REPORT, usage_id,
  315. HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
  316. HID_USAGE_SENSOR_DATA_ACCELERATION,
  317. &st->common_attributes.sensitivity);
  318. dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
  319. st->common_attributes.sensitivity.index,
  320. st->common_attributes.sensitivity.report_id);
  321. }
  322. return ret;
  323. }
  324. /* Function to initialize the processing for usage id */
  325. static int hid_accel_3d_probe(struct platform_device *pdev)
  326. {
  327. int ret = 0;
  328. const char *name;
  329. struct iio_dev *indio_dev;
  330. struct accel_3d_state *accel_state;
  331. const struct iio_chan_spec *channel_spec;
  332. int channel_size;
  333. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  334. indio_dev = devm_iio_device_alloc(&pdev->dev,
  335. sizeof(struct accel_3d_state));
  336. if (indio_dev == NULL)
  337. return -ENOMEM;
  338. platform_set_drvdata(pdev, indio_dev);
  339. accel_state = iio_priv(indio_dev);
  340. accel_state->common_attributes.hsdev = hsdev;
  341. accel_state->common_attributes.pdev = pdev;
  342. if (hsdev->usage == HID_USAGE_SENSOR_ACCEL_3D) {
  343. name = "accel_3d";
  344. channel_spec = accel_3d_channels;
  345. channel_size = sizeof(accel_3d_channels);
  346. indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels);
  347. } else {
  348. name = "gravity";
  349. channel_spec = gravity_channels;
  350. channel_size = sizeof(gravity_channels);
  351. indio_dev->num_channels = ARRAY_SIZE(gravity_channels);
  352. }
  353. ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
  354. &accel_state->common_attributes);
  355. if (ret) {
  356. dev_err(&pdev->dev, "failed to setup common attributes\n");
  357. return ret;
  358. }
  359. indio_dev->channels = kmemdup(channel_spec, channel_size, GFP_KERNEL);
  360. if (!indio_dev->channels) {
  361. dev_err(&pdev->dev, "failed to duplicate channels\n");
  362. return -ENOMEM;
  363. }
  364. ret = accel_3d_parse_report(pdev, hsdev,
  365. (struct iio_chan_spec *)indio_dev->channels,
  366. hsdev->usage, accel_state);
  367. if (ret) {
  368. dev_err(&pdev->dev, "failed to setup attributes\n");
  369. goto error_free_dev_mem;
  370. }
  371. indio_dev->dev.parent = &pdev->dev;
  372. indio_dev->info = &accel_3d_info;
  373. indio_dev->name = name;
  374. indio_dev->modes = INDIO_DIRECT_MODE;
  375. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  376. NULL, NULL);
  377. if (ret) {
  378. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  379. goto error_free_dev_mem;
  380. }
  381. atomic_set(&accel_state->common_attributes.data_ready, 0);
  382. ret = hid_sensor_setup_trigger(indio_dev, name,
  383. &accel_state->common_attributes);
  384. if (ret < 0) {
  385. dev_err(&pdev->dev, "trigger setup failed\n");
  386. goto error_unreg_buffer_funcs;
  387. }
  388. ret = iio_device_register(indio_dev);
  389. if (ret) {
  390. dev_err(&pdev->dev, "device register failed\n");
  391. goto error_remove_trigger;
  392. }
  393. accel_state->callbacks.send_event = accel_3d_proc_event;
  394. accel_state->callbacks.capture_sample = accel_3d_capture_sample;
  395. accel_state->callbacks.pdev = pdev;
  396. ret = sensor_hub_register_callback(hsdev, hsdev->usage,
  397. &accel_state->callbacks);
  398. if (ret < 0) {
  399. dev_err(&pdev->dev, "callback reg failed\n");
  400. goto error_iio_unreg;
  401. }
  402. return ret;
  403. error_iio_unreg:
  404. iio_device_unregister(indio_dev);
  405. error_remove_trigger:
  406. hid_sensor_remove_trigger(&accel_state->common_attributes);
  407. error_unreg_buffer_funcs:
  408. iio_triggered_buffer_cleanup(indio_dev);
  409. error_free_dev_mem:
  410. kfree(indio_dev->channels);
  411. return ret;
  412. }
  413. /* Function to deinitialize the processing for usage id */
  414. static int hid_accel_3d_remove(struct platform_device *pdev)
  415. {
  416. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  417. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  418. struct accel_3d_state *accel_state = iio_priv(indio_dev);
  419. sensor_hub_remove_callback(hsdev, hsdev->usage);
  420. iio_device_unregister(indio_dev);
  421. hid_sensor_remove_trigger(&accel_state->common_attributes);
  422. iio_triggered_buffer_cleanup(indio_dev);
  423. kfree(indio_dev->channels);
  424. return 0;
  425. }
  426. static const struct platform_device_id hid_accel_3d_ids[] = {
  427. {
  428. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  429. .name = "HID-SENSOR-200073",
  430. },
  431. { /* gravity sensor */
  432. .name = "HID-SENSOR-20007b",
  433. },
  434. { /* sentinel */ }
  435. };
  436. MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids);
  437. static struct platform_driver hid_accel_3d_platform_driver = {
  438. .id_table = hid_accel_3d_ids,
  439. .driver = {
  440. .name = KBUILD_MODNAME,
  441. .pm = &hid_sensor_pm_ops,
  442. },
  443. .probe = hid_accel_3d_probe,
  444. .remove = hid_accel_3d_remove,
  445. };
  446. module_platform_driver(hid_accel_3d_platform_driver);
  447. MODULE_DESCRIPTION("HID Sensor Accel 3D");
  448. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  449. MODULE_LICENSE("GPL");