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

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