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

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