hid-sensor-temperature.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * HID Sensors Driver
  3. * Copyright (c) 2017, 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.
  16. */
  17. #include <linux/device.h>
  18. #include <linux/hid-sensor-hub.h>
  19. #include <linux/iio/buffer.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/triggered_buffer.h>
  22. #include <linux/iio/trigger_consumer.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include "../common/hid-sensors/hid-sensor-trigger.h"
  26. struct temperature_state {
  27. struct hid_sensor_common common_attributes;
  28. struct hid_sensor_hub_attribute_info temperature_attr;
  29. s32 temperature_data;
  30. int scale_pre_decml;
  31. int scale_post_decml;
  32. int scale_precision;
  33. int value_offset;
  34. };
  35. /* Channel definitions */
  36. static const struct iio_chan_spec temperature_channels[] = {
  37. {
  38. .type = IIO_TEMP,
  39. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  40. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  41. BIT(IIO_CHAN_INFO_SCALE) |
  42. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  43. BIT(IIO_CHAN_INFO_HYSTERESIS),
  44. },
  45. IIO_CHAN_SOFT_TIMESTAMP(3),
  46. };
  47. /* Adjust channel real bits based on report descriptor */
  48. static void temperature_adjust_channel_bit_mask(struct iio_chan_spec *channels,
  49. int channel, int size)
  50. {
  51. channels[channel].scan_type.sign = 's';
  52. /* Real storage bits will change based on the report desc. */
  53. channels[channel].scan_type.realbits = size * 8;
  54. /* Maximum size of a sample to capture is s32 */
  55. channels[channel].scan_type.storagebits = sizeof(s32) * 8;
  56. }
  57. static int temperature_read_raw(struct iio_dev *indio_dev,
  58. struct iio_chan_spec const *chan,
  59. int *val, int *val2, long mask)
  60. {
  61. struct temperature_state *temp_st = iio_priv(indio_dev);
  62. switch (mask) {
  63. case IIO_CHAN_INFO_RAW:
  64. if (chan->type != IIO_TEMP)
  65. return -EINVAL;
  66. hid_sensor_power_state(
  67. &temp_st->common_attributes, true);
  68. *val = sensor_hub_input_attr_get_raw_value(
  69. temp_st->common_attributes.hsdev,
  70. HID_USAGE_SENSOR_TEMPERATURE,
  71. HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
  72. temp_st->temperature_attr.report_id,
  73. SENSOR_HUB_SYNC);
  74. hid_sensor_power_state(
  75. &temp_st->common_attributes,
  76. false);
  77. return IIO_VAL_INT;
  78. case IIO_CHAN_INFO_SCALE:
  79. *val = temp_st->scale_pre_decml;
  80. *val2 = temp_st->scale_post_decml;
  81. return temp_st->scale_precision;
  82. case IIO_CHAN_INFO_OFFSET:
  83. *val = temp_st->value_offset;
  84. return IIO_VAL_INT;
  85. case IIO_CHAN_INFO_SAMP_FREQ:
  86. return hid_sensor_read_samp_freq_value(
  87. &temp_st->common_attributes, val, val2);
  88. case IIO_CHAN_INFO_HYSTERESIS:
  89. return hid_sensor_read_raw_hyst_value(
  90. &temp_st->common_attributes, val, val2);
  91. default:
  92. return -EINVAL;
  93. }
  94. }
  95. static int temperature_write_raw(struct iio_dev *indio_dev,
  96. struct iio_chan_spec const *chan,
  97. int val, int val2, long mask)
  98. {
  99. struct temperature_state *temp_st = iio_priv(indio_dev);
  100. switch (mask) {
  101. case IIO_CHAN_INFO_SAMP_FREQ:
  102. return hid_sensor_write_samp_freq_value(
  103. &temp_st->common_attributes, val, val2);
  104. case IIO_CHAN_INFO_HYSTERESIS:
  105. return hid_sensor_write_raw_hyst_value(
  106. &temp_st->common_attributes, val, val2);
  107. default:
  108. return -EINVAL;
  109. }
  110. }
  111. static const struct iio_info temperature_info = {
  112. .read_raw = &temperature_read_raw,
  113. .write_raw = &temperature_write_raw,
  114. };
  115. /* Callback handler to send event after all samples are received and captured */
  116. static int temperature_proc_event(struct hid_sensor_hub_device *hsdev,
  117. unsigned int usage_id, void *pdev)
  118. {
  119. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  120. struct temperature_state *temp_st = iio_priv(indio_dev);
  121. if (atomic_read(&temp_st->common_attributes.data_ready))
  122. iio_push_to_buffers_with_timestamp(indio_dev,
  123. &temp_st->temperature_data,
  124. iio_get_time_ns(indio_dev));
  125. return 0;
  126. }
  127. /* Capture samples in local storage */
  128. static int temperature_capture_sample(struct hid_sensor_hub_device *hsdev,
  129. unsigned int usage_id, size_t raw_len,
  130. char *raw_data, void *pdev)
  131. {
  132. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  133. struct temperature_state *temp_st = iio_priv(indio_dev);
  134. switch (usage_id) {
  135. case HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE:
  136. temp_st->temperature_data = *(s32 *)raw_data;
  137. return 0;
  138. default:
  139. return -EINVAL;
  140. }
  141. }
  142. /* Parse report which is specific to an usage id*/
  143. static int temperature_parse_report(struct platform_device *pdev,
  144. struct hid_sensor_hub_device *hsdev,
  145. struct iio_chan_spec *channels,
  146. unsigned int usage_id,
  147. struct temperature_state *st)
  148. {
  149. int ret;
  150. ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
  151. usage_id,
  152. HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
  153. &st->temperature_attr);
  154. if (ret < 0)
  155. return ret;
  156. temperature_adjust_channel_bit_mask(channels, 0,
  157. st->temperature_attr.size);
  158. st->scale_precision = hid_sensor_format_scale(
  159. HID_USAGE_SENSOR_TEMPERATURE,
  160. &st->temperature_attr,
  161. &st->scale_pre_decml, &st->scale_post_decml);
  162. /* Set Sensitivity field ids, when there is no individual modifier */
  163. if (st->common_attributes.sensitivity.index < 0)
  164. sensor_hub_input_get_attribute_info(hsdev,
  165. HID_FEATURE_REPORT, usage_id,
  166. HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
  167. HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
  168. &st->common_attributes.sensitivity);
  169. return ret;
  170. }
  171. static struct hid_sensor_hub_callbacks temperature_callbacks = {
  172. .send_event = &temperature_proc_event,
  173. .capture_sample = &temperature_capture_sample,
  174. };
  175. /* Function to initialize the processing for usage id */
  176. static int hid_temperature_probe(struct platform_device *pdev)
  177. {
  178. static const char *name = "temperature";
  179. struct iio_dev *indio_dev;
  180. struct temperature_state *temp_st;
  181. struct iio_chan_spec *temp_chans;
  182. struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
  183. int ret;
  184. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*temp_st));
  185. if (!indio_dev)
  186. return -ENOMEM;
  187. temp_st = iio_priv(indio_dev);
  188. temp_st->common_attributes.hsdev = hsdev;
  189. temp_st->common_attributes.pdev = pdev;
  190. ret = hid_sensor_parse_common_attributes(hsdev,
  191. HID_USAGE_SENSOR_TEMPERATURE,
  192. &temp_st->common_attributes);
  193. if (ret)
  194. return ret;
  195. temp_chans = devm_kmemdup(&indio_dev->dev, temperature_channels,
  196. sizeof(temperature_channels), GFP_KERNEL);
  197. if (!temp_chans)
  198. return -ENOMEM;
  199. ret = temperature_parse_report(pdev, hsdev, temp_chans,
  200. HID_USAGE_SENSOR_TEMPERATURE, temp_st);
  201. if (ret)
  202. return ret;
  203. indio_dev->channels = temp_chans;
  204. indio_dev->num_channels = ARRAY_SIZE(temperature_channels);
  205. indio_dev->dev.parent = &pdev->dev;
  206. indio_dev->info = &temperature_info;
  207. indio_dev->name = name;
  208. indio_dev->modes = INDIO_DIRECT_MODE;
  209. ret = devm_iio_triggered_buffer_setup(&pdev->dev, indio_dev,
  210. &iio_pollfunc_store_time, NULL, NULL);
  211. if (ret)
  212. return ret;
  213. atomic_set(&temp_st->common_attributes.data_ready, 0);
  214. ret = hid_sensor_setup_trigger(indio_dev, name,
  215. &temp_st->common_attributes);
  216. if (ret)
  217. return ret;
  218. platform_set_drvdata(pdev, indio_dev);
  219. temperature_callbacks.pdev = pdev;
  220. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE,
  221. &temperature_callbacks);
  222. if (ret)
  223. goto error_remove_trigger;
  224. ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
  225. if (ret)
  226. goto error_remove_callback;
  227. return ret;
  228. error_remove_callback:
  229. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE);
  230. error_remove_trigger:
  231. hid_sensor_remove_trigger(&temp_st->common_attributes);
  232. return ret;
  233. }
  234. /* Function to deinitialize the processing for usage id */
  235. static int hid_temperature_remove(struct platform_device *pdev)
  236. {
  237. struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
  238. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  239. struct temperature_state *temp_st = iio_priv(indio_dev);
  240. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE);
  241. hid_sensor_remove_trigger(&temp_st->common_attributes);
  242. return 0;
  243. }
  244. static const struct platform_device_id hid_temperature_ids[] = {
  245. {
  246. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  247. .name = "HID-SENSOR-200033",
  248. },
  249. { /* sentinel */ }
  250. };
  251. MODULE_DEVICE_TABLE(platform, hid_temperature_ids);
  252. static struct platform_driver hid_temperature_platform_driver = {
  253. .id_table = hid_temperature_ids,
  254. .driver = {
  255. .name = "temperature-sensor",
  256. .pm = &hid_sensor_pm_ops,
  257. },
  258. .probe = hid_temperature_probe,
  259. .remove = hid_temperature_remove,
  260. };
  261. module_platform_driver(hid_temperature_platform_driver);
  262. MODULE_DESCRIPTION("HID Environmental temperature sensor");
  263. MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>");
  264. MODULE_LICENSE("GPL v2");