hid-sensor-temperature.c 8.9 KB

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