Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / iio / temperature / hid-sensor-temperature.c
blob692520e1c497bbeda538e217b6a09f43ee26aadc
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * HID Sensors Driver
4 * Copyright (c) 2017, Intel Corporation.
5 */
6 #include <linux/device.h>
7 #include <linux/hid-sensor-hub.h>
8 #include <linux/iio/buffer.h>
9 #include <linux/iio/iio.h>
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/platform_device.h>
14 #include "../common/hid-sensors/hid-sensor-trigger.h"
16 struct temperature_state {
17 struct hid_sensor_common common_attributes;
18 struct hid_sensor_hub_attribute_info temperature_attr;
19 struct {
20 s32 temperature_data;
21 aligned_s64 timestamp;
22 } scan;
23 int scale_pre_decml;
24 int scale_post_decml;
25 int scale_precision;
26 int value_offset;
29 static const u32 temperature_sensitivity_addresses[] = {
30 HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
33 /* Channel definitions */
34 static const struct iio_chan_spec temperature_channels[] = {
36 .type = IIO_TEMP,
37 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
38 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
39 BIT(IIO_CHAN_INFO_SCALE) |
40 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
41 BIT(IIO_CHAN_INFO_HYSTERESIS),
43 IIO_CHAN_SOFT_TIMESTAMP(1),
46 /* Adjust channel real bits based on report descriptor */
47 static void temperature_adjust_channel_bit_mask(struct iio_chan_spec *channels,
48 int channel, int size)
50 channels[channel].scan_type.sign = 's';
51 /* Real storage bits will change based on the report desc. */
52 channels[channel].scan_type.realbits = size * 8;
53 /* Maximum size of a sample to capture is s32 */
54 channels[channel].scan_type.storagebits = sizeof(s32) * 8;
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)
61 struct temperature_state *temp_st = iio_priv(indio_dev);
63 switch (mask) {
64 case IIO_CHAN_INFO_RAW:
65 if (chan->type != IIO_TEMP)
66 return -EINVAL;
67 hid_sensor_power_state(
68 &temp_st->common_attributes, true);
69 *val = sensor_hub_input_attr_get_raw_value(
70 temp_st->common_attributes.hsdev,
71 HID_USAGE_SENSOR_TEMPERATURE,
72 HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
73 temp_st->temperature_attr.report_id,
74 SENSOR_HUB_SYNC,
75 temp_st->temperature_attr.logical_minimum < 0);
76 hid_sensor_power_state(
77 &temp_st->common_attributes,
78 false);
80 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;
87 case IIO_CHAN_INFO_OFFSET:
88 *val = temp_st->value_offset;
89 return IIO_VAL_INT;
91 case IIO_CHAN_INFO_SAMP_FREQ:
92 return hid_sensor_read_samp_freq_value(
93 &temp_st->common_attributes, val, val2);
95 case IIO_CHAN_INFO_HYSTERESIS:
96 return hid_sensor_read_raw_hyst_value(
97 &temp_st->common_attributes, val, val2);
98 default:
99 return -EINVAL;
103 static int temperature_write_raw(struct iio_dev *indio_dev,
104 struct iio_chan_spec const *chan,
105 int val, int val2, long mask)
107 struct temperature_state *temp_st = iio_priv(indio_dev);
109 switch (mask) {
110 case IIO_CHAN_INFO_SAMP_FREQ:
111 return hid_sensor_write_samp_freq_value(
112 &temp_st->common_attributes, val, val2);
113 case IIO_CHAN_INFO_HYSTERESIS:
114 return hid_sensor_write_raw_hyst_value(
115 &temp_st->common_attributes, val, val2);
116 default:
117 return -EINVAL;
121 static const struct iio_info temperature_info = {
122 .read_raw = &temperature_read_raw,
123 .write_raw = &temperature_write_raw,
126 /* Callback handler to send event after all samples are received and captured */
127 static int temperature_proc_event(struct hid_sensor_hub_device *hsdev,
128 unsigned int usage_id, void *pdev)
130 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
131 struct temperature_state *temp_st = iio_priv(indio_dev);
133 if (atomic_read(&temp_st->common_attributes.data_ready))
134 iio_push_to_buffers_with_timestamp(indio_dev, &temp_st->scan,
135 iio_get_time_ns(indio_dev));
137 return 0;
140 /* Capture samples in local storage */
141 static int temperature_capture_sample(struct hid_sensor_hub_device *hsdev,
142 unsigned int usage_id, size_t raw_len,
143 char *raw_data, void *pdev)
145 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
146 struct temperature_state *temp_st = iio_priv(indio_dev);
148 switch (usage_id) {
149 case HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE:
150 temp_st->scan.temperature_data = *(s32 *)raw_data;
151 return 0;
152 default:
153 return -EINVAL;
157 /* Parse report which is specific to an usage id*/
158 static int temperature_parse_report(struct platform_device *pdev,
159 struct hid_sensor_hub_device *hsdev,
160 struct iio_chan_spec *channels,
161 unsigned int usage_id,
162 struct temperature_state *st)
164 int ret;
166 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
167 usage_id,
168 HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
169 &st->temperature_attr);
170 if (ret < 0)
171 return ret;
173 temperature_adjust_channel_bit_mask(channels, 0,
174 st->temperature_attr.size);
176 st->scale_precision = hid_sensor_format_scale(
177 HID_USAGE_SENSOR_TEMPERATURE,
178 &st->temperature_attr,
179 &st->scale_pre_decml, &st->scale_post_decml);
181 return ret;
184 static struct hid_sensor_hub_callbacks temperature_callbacks = {
185 .send_event = &temperature_proc_event,
186 .capture_sample = &temperature_capture_sample,
189 /* Function to initialize the processing for usage id */
190 static int hid_temperature_probe(struct platform_device *pdev)
192 static const char *name = "temperature";
193 struct iio_dev *indio_dev;
194 struct temperature_state *temp_st;
195 struct iio_chan_spec *temp_chans;
196 struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
197 int ret;
199 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*temp_st));
200 if (!indio_dev)
201 return -ENOMEM;
203 temp_st = iio_priv(indio_dev);
204 temp_st->common_attributes.hsdev = hsdev;
205 temp_st->common_attributes.pdev = pdev;
207 ret = hid_sensor_parse_common_attributes(hsdev,
208 HID_USAGE_SENSOR_TEMPERATURE,
209 &temp_st->common_attributes,
210 temperature_sensitivity_addresses,
211 ARRAY_SIZE(temperature_sensitivity_addresses));
212 if (ret)
213 return ret;
215 temp_chans = devm_kmemdup(&indio_dev->dev, temperature_channels,
216 sizeof(temperature_channels), GFP_KERNEL);
217 if (!temp_chans)
218 return -ENOMEM;
220 ret = temperature_parse_report(pdev, hsdev, temp_chans,
221 HID_USAGE_SENSOR_TEMPERATURE, temp_st);
222 if (ret)
223 return ret;
225 indio_dev->channels = temp_chans;
226 indio_dev->num_channels = ARRAY_SIZE(temperature_channels);
227 indio_dev->info = &temperature_info;
228 indio_dev->name = name;
229 indio_dev->modes = INDIO_DIRECT_MODE;
231 atomic_set(&temp_st->common_attributes.data_ready, 0);
233 ret = hid_sensor_setup_trigger(indio_dev, name,
234 &temp_st->common_attributes);
235 if (ret)
236 return ret;
238 platform_set_drvdata(pdev, indio_dev);
240 temperature_callbacks.pdev = pdev;
241 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE,
242 &temperature_callbacks);
243 if (ret)
244 goto error_remove_trigger;
246 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
247 if (ret)
248 goto error_remove_callback;
250 return ret;
252 error_remove_callback:
253 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE);
254 error_remove_trigger:
255 hid_sensor_remove_trigger(indio_dev, &temp_st->common_attributes);
256 return ret;
259 /* Function to deinitialize the processing for usage id */
260 static void hid_temperature_remove(struct platform_device *pdev)
262 struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
263 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
264 struct temperature_state *temp_st = iio_priv(indio_dev);
266 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE);
267 hid_sensor_remove_trigger(indio_dev, &temp_st->common_attributes);
270 static const struct platform_device_id hid_temperature_ids[] = {
272 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
273 .name = "HID-SENSOR-200033",
275 { /* sentinel */ }
277 MODULE_DEVICE_TABLE(platform, hid_temperature_ids);
279 static struct platform_driver hid_temperature_platform_driver = {
280 .id_table = hid_temperature_ids,
281 .driver = {
282 .name = "temperature-sensor",
283 .pm = &hid_sensor_pm_ops,
285 .probe = hid_temperature_probe,
286 .remove = hid_temperature_remove,
288 module_platform_driver(hid_temperature_platform_driver);
290 MODULE_DESCRIPTION("HID Environmental temperature sensor");
291 MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>");
292 MODULE_LICENSE("GPL v2");
293 MODULE_IMPORT_NS("IIO_HID");