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

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