hid-sensor-temperature.c 9.0 KB

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