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

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