1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2017, Intel Corporation.
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
;
21 aligned_s64 timestamp
;
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
[] = {
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
);
64 case IIO_CHAN_INFO_RAW
:
65 if (chan
->type
!= IIO_TEMP
)
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
,
75 temp_st
->temperature_attr
.logical_minimum
< 0);
76 hid_sensor_power_state(
77 &temp_st
->common_attributes
,
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
;
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
);
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
);
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
);
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
));
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
);
149 case HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE
:
150 temp_st
->scan
.temperature_data
= *(s32
*)raw_data
;
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
)
166 ret
= sensor_hub_input_get_attribute_info(hsdev
, HID_INPUT_REPORT
,
168 HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE
,
169 &st
->temperature_attr
);
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
);
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
);
199 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*temp_st
));
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
));
215 temp_chans
= devm_kmemdup(&indio_dev
->dev
, temperature_channels
,
216 sizeof(temperature_channels
), GFP_KERNEL
);
220 ret
= temperature_parse_report(pdev
, hsdev
, temp_chans
,
221 HID_USAGE_SENSOR_TEMPERATURE
, temp_st
);
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
);
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
);
244 goto error_remove_trigger
;
246 ret
= devm_iio_device_register(indio_dev
->dev
.parent
, indio_dev
);
248 goto error_remove_callback
;
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
);
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",
277 MODULE_DEVICE_TABLE(platform
, hid_temperature_ids
);
279 static struct platform_driver hid_temperature_platform_driver
= {
280 .id_table
= hid_temperature_ids
,
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");