hid-sensor-magn-3d.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 magn_3d_channel {
  34. CHANNEL_SCAN_INDEX_X,
  35. CHANNEL_SCAN_INDEX_Y,
  36. CHANNEL_SCAN_INDEX_Z,
  37. CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP,
  38. CHANNEL_SCAN_INDEX_NORTH_TRUE_TILT_COMP,
  39. CHANNEL_SCAN_INDEX_NORTH_MAGN,
  40. CHANNEL_SCAN_INDEX_NORTH_TRUE,
  41. MAGN_3D_CHANNEL_MAX,
  42. };
  43. struct magn_3d_state {
  44. struct hid_sensor_hub_callbacks callbacks;
  45. struct hid_sensor_common common_attributes;
  46. struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX];
  47. /* dynamically sized array to hold sensor values */
  48. u32 *iio_vals;
  49. /* array of pointers to sensor value */
  50. u32 *magn_val_addr[MAGN_3D_CHANNEL_MAX];
  51. int scale_pre_decml;
  52. int scale_post_decml;
  53. int scale_precision;
  54. int value_offset;
  55. };
  56. static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
  57. HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS,
  58. HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS,
  59. HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS,
  60. HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH,
  61. HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH,
  62. HID_USAGE_SENSOR_ORIENT_MAGN_NORTH,
  63. HID_USAGE_SENSOR_ORIENT_TRUE_NORTH,
  64. };
  65. /* Channel definitions */
  66. static const struct iio_chan_spec magn_3d_channels[] = {
  67. {
  68. .type = IIO_MAGN,
  69. .modified = 1,
  70. .channel2 = IIO_MOD_X,
  71. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  72. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  73. BIT(IIO_CHAN_INFO_SCALE) |
  74. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  75. BIT(IIO_CHAN_INFO_HYSTERESIS),
  76. }, {
  77. .type = IIO_MAGN,
  78. .modified = 1,
  79. .channel2 = IIO_MOD_Y,
  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. }, {
  86. .type = IIO_MAGN,
  87. .modified = 1,
  88. .channel2 = IIO_MOD_Z,
  89. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  90. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  91. BIT(IIO_CHAN_INFO_SCALE) |
  92. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  93. BIT(IIO_CHAN_INFO_HYSTERESIS),
  94. }, {
  95. .type = IIO_ROT,
  96. .modified = 1,
  97. .channel2 = IIO_MOD_NORTH_MAGN_TILT_COMP,
  98. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  99. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  100. BIT(IIO_CHAN_INFO_SCALE) |
  101. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  102. BIT(IIO_CHAN_INFO_HYSTERESIS),
  103. }, {
  104. .type = IIO_ROT,
  105. .modified = 1,
  106. .channel2 = IIO_MOD_NORTH_TRUE_TILT_COMP,
  107. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  108. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  109. BIT(IIO_CHAN_INFO_SCALE) |
  110. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  111. BIT(IIO_CHAN_INFO_HYSTERESIS),
  112. }, {
  113. .type = IIO_ROT,
  114. .modified = 1,
  115. .channel2 = IIO_MOD_NORTH_MAGN,
  116. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  117. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  118. BIT(IIO_CHAN_INFO_SCALE) |
  119. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  120. BIT(IIO_CHAN_INFO_HYSTERESIS),
  121. }, {
  122. .type = IIO_ROT,
  123. .modified = 1,
  124. .channel2 = IIO_MOD_NORTH_TRUE,
  125. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  126. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  127. BIT(IIO_CHAN_INFO_SCALE) |
  128. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  129. BIT(IIO_CHAN_INFO_HYSTERESIS),
  130. }
  131. };
  132. /* Adjust channel real bits based on report descriptor */
  133. static void magn_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
  134. int channel, int size)
  135. {
  136. channels[channel].scan_type.sign = 's';
  137. /* Real storage bits will change based on the report desc. */
  138. channels[channel].scan_type.realbits = size * 8;
  139. /* Maximum size of a sample to capture is u32 */
  140. channels[channel].scan_type.storagebits = sizeof(u32) * 8;
  141. }
  142. /* Channel read_raw handler */
  143. static int magn_3d_read_raw(struct iio_dev *indio_dev,
  144. struct iio_chan_spec const *chan,
  145. int *val, int *val2,
  146. long mask)
  147. {
  148. struct magn_3d_state *magn_state = iio_priv(indio_dev);
  149. int report_id = -1;
  150. u32 address;
  151. int ret_type;
  152. *val = 0;
  153. *val2 = 0;
  154. switch (mask) {
  155. case 0:
  156. hid_sensor_power_state(&magn_state->common_attributes, true);
  157. report_id =
  158. magn_state->magn[chan->address].report_id;
  159. address = magn_3d_addresses[chan->address];
  160. if (report_id >= 0)
  161. *val = sensor_hub_input_attr_get_raw_value(
  162. magn_state->common_attributes.hsdev,
  163. HID_USAGE_SENSOR_COMPASS_3D, address,
  164. report_id,
  165. SENSOR_HUB_SYNC);
  166. else {
  167. *val = 0;
  168. hid_sensor_power_state(&magn_state->common_attributes,
  169. false);
  170. return -EINVAL;
  171. }
  172. hid_sensor_power_state(&magn_state->common_attributes, false);
  173. ret_type = IIO_VAL_INT;
  174. break;
  175. case IIO_CHAN_INFO_SCALE:
  176. *val = magn_state->scale_pre_decml;
  177. *val2 = magn_state->scale_post_decml;
  178. ret_type = magn_state->scale_precision;
  179. break;
  180. case IIO_CHAN_INFO_OFFSET:
  181. *val = magn_state->value_offset;
  182. ret_type = IIO_VAL_INT;
  183. break;
  184. case IIO_CHAN_INFO_SAMP_FREQ:
  185. ret_type = hid_sensor_read_samp_freq_value(
  186. &magn_state->common_attributes, val, val2);
  187. break;
  188. case IIO_CHAN_INFO_HYSTERESIS:
  189. ret_type = hid_sensor_read_raw_hyst_value(
  190. &magn_state->common_attributes, val, val2);
  191. break;
  192. default:
  193. ret_type = -EINVAL;
  194. break;
  195. }
  196. return ret_type;
  197. }
  198. /* Channel write_raw handler */
  199. static int magn_3d_write_raw(struct iio_dev *indio_dev,
  200. struct iio_chan_spec const *chan,
  201. int val,
  202. int val2,
  203. long mask)
  204. {
  205. struct magn_3d_state *magn_state = iio_priv(indio_dev);
  206. int ret = 0;
  207. switch (mask) {
  208. case IIO_CHAN_INFO_SAMP_FREQ:
  209. ret = hid_sensor_write_samp_freq_value(
  210. &magn_state->common_attributes, val, val2);
  211. break;
  212. case IIO_CHAN_INFO_HYSTERESIS:
  213. ret = hid_sensor_write_raw_hyst_value(
  214. &magn_state->common_attributes, val, val2);
  215. break;
  216. default:
  217. ret = -EINVAL;
  218. }
  219. return ret;
  220. }
  221. static const struct iio_info magn_3d_info = {
  222. .driver_module = THIS_MODULE,
  223. .read_raw = &magn_3d_read_raw,
  224. .write_raw = &magn_3d_write_raw,
  225. };
  226. /* Function to push data to buffer */
  227. static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data)
  228. {
  229. dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
  230. iio_push_to_buffers(indio_dev, data);
  231. }
  232. /* Callback handler to send event after all samples are received and captured */
  233. static int magn_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 magn_3d_state *magn_state = iio_priv(indio_dev);
  239. dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n");
  240. if (atomic_read(&magn_state->common_attributes.data_ready))
  241. hid_sensor_push_data(indio_dev, magn_state->iio_vals);
  242. return 0;
  243. }
  244. /* Capture samples in local storage */
  245. static int magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
  246. unsigned usage_id,
  247. size_t raw_len, char *raw_data,
  248. void *priv)
  249. {
  250. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  251. struct magn_3d_state *magn_state = iio_priv(indio_dev);
  252. int offset;
  253. int ret = 0;
  254. u32 *iio_val = NULL;
  255. switch (usage_id) {
  256. case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS:
  257. case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS:
  258. case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS:
  259. offset = (usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS)
  260. + CHANNEL_SCAN_INDEX_X;
  261. break;
  262. case HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH:
  263. case HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH:
  264. case HID_USAGE_SENSOR_ORIENT_MAGN_NORTH:
  265. case HID_USAGE_SENSOR_ORIENT_TRUE_NORTH:
  266. offset = (usage_id - HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH)
  267. + CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP;
  268. break;
  269. default:
  270. return -EINVAL;
  271. }
  272. iio_val = magn_state->magn_val_addr[offset];
  273. if (iio_val != NULL)
  274. *iio_val = *((u32 *)raw_data);
  275. else
  276. ret = -EINVAL;
  277. return ret;
  278. }
  279. /* Parse report which is specific to an usage id*/
  280. static int magn_3d_parse_report(struct platform_device *pdev,
  281. struct hid_sensor_hub_device *hsdev,
  282. struct iio_chan_spec **channels,
  283. int *chan_count,
  284. unsigned usage_id,
  285. struct magn_3d_state *st)
  286. {
  287. int i;
  288. int attr_count = 0;
  289. struct iio_chan_spec *_channels;
  290. /* Scan for each usage attribute supported */
  291. for (i = 0; i < MAGN_3D_CHANNEL_MAX; i++) {
  292. int status;
  293. u32 address = magn_3d_addresses[i];
  294. /* Check if usage attribute exists in the sensor hub device */
  295. status = sensor_hub_input_get_attribute_info(hsdev,
  296. HID_INPUT_REPORT,
  297. usage_id,
  298. address,
  299. &(st->magn[i]));
  300. if (!status)
  301. attr_count++;
  302. }
  303. if (attr_count <= 0) {
  304. dev_err(&pdev->dev,
  305. "failed to find any supported usage attributes in report\n");
  306. return -EINVAL;
  307. }
  308. dev_dbg(&pdev->dev, "magn_3d Found %d usage attributes\n",
  309. attr_count);
  310. dev_dbg(&pdev->dev, "magn_3d X: %x:%x Y: %x:%x Z: %x:%x\n",
  311. st->magn[0].index,
  312. st->magn[0].report_id,
  313. st->magn[1].index, st->magn[1].report_id,
  314. st->magn[2].index, st->magn[2].report_id);
  315. /* Setup IIO channel array */
  316. _channels = devm_kcalloc(&pdev->dev, attr_count,
  317. sizeof(struct iio_chan_spec),
  318. GFP_KERNEL);
  319. if (!_channels) {
  320. dev_err(&pdev->dev,
  321. "failed to allocate space for iio channels\n");
  322. return -ENOMEM;
  323. }
  324. st->iio_vals = devm_kcalloc(&pdev->dev, attr_count,
  325. sizeof(u32),
  326. GFP_KERNEL);
  327. if (!st->iio_vals) {
  328. dev_err(&pdev->dev,
  329. "failed to allocate space for iio values array\n");
  330. return -ENOMEM;
  331. }
  332. for (i = 0, *chan_count = 0;
  333. i < MAGN_3D_CHANNEL_MAX && *chan_count < attr_count;
  334. i++){
  335. if (st->magn[i].index >= 0) {
  336. /* Setup IIO channel struct */
  337. (_channels[*chan_count]) = magn_3d_channels[i];
  338. (_channels[*chan_count]).scan_index = *chan_count;
  339. (_channels[*chan_count]).address = i;
  340. /* Set magn_val_addr to iio value address */
  341. st->magn_val_addr[i] = &(st->iio_vals[*chan_count]);
  342. magn_3d_adjust_channel_bit_mask(_channels,
  343. *chan_count,
  344. st->magn[i].size);
  345. (*chan_count)++;
  346. }
  347. }
  348. if (*chan_count <= 0) {
  349. dev_err(&pdev->dev,
  350. "failed to find any magnetic channels setup\n");
  351. return -EINVAL;
  352. }
  353. *channels = _channels;
  354. dev_dbg(&pdev->dev, "magn_3d Setup %d IIO channels\n",
  355. *chan_count);
  356. st->scale_precision = hid_sensor_format_scale(
  357. HID_USAGE_SENSOR_COMPASS_3D,
  358. &st->magn[CHANNEL_SCAN_INDEX_X],
  359. &st->scale_pre_decml, &st->scale_post_decml);
  360. /* Set Sensitivity field ids, when there is no individual modifier */
  361. if (st->common_attributes.sensitivity.index < 0) {
  362. sensor_hub_input_get_attribute_info(hsdev,
  363. HID_FEATURE_REPORT, usage_id,
  364. HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
  365. HID_USAGE_SENSOR_DATA_ORIENTATION,
  366. &st->common_attributes.sensitivity);
  367. dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
  368. st->common_attributes.sensitivity.index,
  369. st->common_attributes.sensitivity.report_id);
  370. }
  371. return 0;
  372. }
  373. /* Function to initialize the processing for usage id */
  374. static int hid_magn_3d_probe(struct platform_device *pdev)
  375. {
  376. int ret = 0;
  377. static char *name = "magn_3d";
  378. struct iio_dev *indio_dev;
  379. struct magn_3d_state *magn_state;
  380. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  381. struct iio_chan_spec *channels;
  382. int chan_count = 0;
  383. indio_dev = devm_iio_device_alloc(&pdev->dev,
  384. sizeof(struct magn_3d_state));
  385. if (indio_dev == NULL)
  386. return -ENOMEM;
  387. platform_set_drvdata(pdev, indio_dev);
  388. magn_state = iio_priv(indio_dev);
  389. magn_state->common_attributes.hsdev = hsdev;
  390. magn_state->common_attributes.pdev = pdev;
  391. ret = hid_sensor_parse_common_attributes(hsdev,
  392. HID_USAGE_SENSOR_COMPASS_3D,
  393. &magn_state->common_attributes);
  394. if (ret) {
  395. dev_err(&pdev->dev, "failed to setup common attributes\n");
  396. return ret;
  397. }
  398. ret = magn_3d_parse_report(pdev, hsdev,
  399. &channels, &chan_count,
  400. HID_USAGE_SENSOR_COMPASS_3D, magn_state);
  401. if (ret) {
  402. dev_err(&pdev->dev, "failed to parse report\n");
  403. return ret;
  404. }
  405. indio_dev->channels = channels;
  406. indio_dev->num_channels = chan_count;
  407. indio_dev->dev.parent = &pdev->dev;
  408. indio_dev->info = &magn_3d_info;
  409. indio_dev->name = name;
  410. indio_dev->modes = INDIO_DIRECT_MODE;
  411. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  412. NULL, NULL);
  413. if (ret) {
  414. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  415. return ret;
  416. }
  417. atomic_set(&magn_state->common_attributes.data_ready, 0);
  418. ret = hid_sensor_setup_trigger(indio_dev, name,
  419. &magn_state->common_attributes);
  420. if (ret < 0) {
  421. dev_err(&pdev->dev, "trigger setup failed\n");
  422. goto error_unreg_buffer_funcs;
  423. }
  424. ret = iio_device_register(indio_dev);
  425. if (ret) {
  426. dev_err(&pdev->dev, "device register failed\n");
  427. goto error_remove_trigger;
  428. }
  429. magn_state->callbacks.send_event = magn_3d_proc_event;
  430. magn_state->callbacks.capture_sample = magn_3d_capture_sample;
  431. magn_state->callbacks.pdev = pdev;
  432. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D,
  433. &magn_state->callbacks);
  434. if (ret < 0) {
  435. dev_err(&pdev->dev, "callback reg failed\n");
  436. goto error_iio_unreg;
  437. }
  438. return ret;
  439. error_iio_unreg:
  440. iio_device_unregister(indio_dev);
  441. error_remove_trigger:
  442. hid_sensor_remove_trigger(&magn_state->common_attributes);
  443. error_unreg_buffer_funcs:
  444. iio_triggered_buffer_cleanup(indio_dev);
  445. return ret;
  446. }
  447. /* Function to deinitialize the processing for usage id */
  448. static int hid_magn_3d_remove(struct platform_device *pdev)
  449. {
  450. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  451. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  452. struct magn_3d_state *magn_state = iio_priv(indio_dev);
  453. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D);
  454. iio_device_unregister(indio_dev);
  455. hid_sensor_remove_trigger(&magn_state->common_attributes);
  456. iio_triggered_buffer_cleanup(indio_dev);
  457. return 0;
  458. }
  459. static const struct platform_device_id hid_magn_3d_ids[] = {
  460. {
  461. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  462. .name = "HID-SENSOR-200083",
  463. },
  464. { /* sentinel */ }
  465. };
  466. MODULE_DEVICE_TABLE(platform, hid_magn_3d_ids);
  467. static struct platform_driver hid_magn_3d_platform_driver = {
  468. .id_table = hid_magn_3d_ids,
  469. .driver = {
  470. .name = KBUILD_MODNAME,
  471. .pm = &hid_sensor_pm_ops,
  472. },
  473. .probe = hid_magn_3d_probe,
  474. .remove = hid_magn_3d_remove,
  475. };
  476. module_platform_driver(hid_magn_3d_platform_driver);
  477. MODULE_DESCRIPTION("HID Sensor Magnetometer 3D");
  478. MODULE_AUTHOR("Srinivas Pandruvada <[email protected]>");
  479. MODULE_LICENSE("GPL");