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/platform_device.h>
13 #include "../common/hid-sensors/hid-sensor-trigger.h"
15 struct temperature_state
{
16 struct hid_sensor_common common_attributes
;
17 struct hid_sensor_hub_attribute_info temperature_attr
;
25 /* Channel definitions */
26 static const struct iio_chan_spec temperature_channels
[] = {
29 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
30 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
31 BIT(IIO_CHAN_INFO_SCALE
) |
32 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
33 BIT(IIO_CHAN_INFO_HYSTERESIS
),
35 IIO_CHAN_SOFT_TIMESTAMP(3),
38 /* Adjust channel real bits based on report descriptor */
39 static void temperature_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
40 int channel
, int size
)
42 channels
[channel
].scan_type
.sign
= 's';
43 /* Real storage bits will change based on the report desc. */
44 channels
[channel
].scan_type
.realbits
= size
* 8;
45 /* Maximum size of a sample to capture is s32 */
46 channels
[channel
].scan_type
.storagebits
= sizeof(s32
) * 8;
49 static int temperature_read_raw(struct iio_dev
*indio_dev
,
50 struct iio_chan_spec
const *chan
,
51 int *val
, int *val2
, long mask
)
53 struct temperature_state
*temp_st
= iio_priv(indio_dev
);
56 case IIO_CHAN_INFO_RAW
:
57 if (chan
->type
!= IIO_TEMP
)
59 hid_sensor_power_state(
60 &temp_st
->common_attributes
, true);
61 *val
= sensor_hub_input_attr_get_raw_value(
62 temp_st
->common_attributes
.hsdev
,
63 HID_USAGE_SENSOR_TEMPERATURE
,
64 HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE
,
65 temp_st
->temperature_attr
.report_id
,
67 temp_st
->temperature_attr
.logical_minimum
< 0);
68 hid_sensor_power_state(
69 &temp_st
->common_attributes
,
74 case IIO_CHAN_INFO_SCALE
:
75 *val
= temp_st
->scale_pre_decml
;
76 *val2
= temp_st
->scale_post_decml
;
77 return temp_st
->scale_precision
;
79 case IIO_CHAN_INFO_OFFSET
:
80 *val
= temp_st
->value_offset
;
83 case IIO_CHAN_INFO_SAMP_FREQ
:
84 return hid_sensor_read_samp_freq_value(
85 &temp_st
->common_attributes
, val
, val2
);
87 case IIO_CHAN_INFO_HYSTERESIS
:
88 return hid_sensor_read_raw_hyst_value(
89 &temp_st
->common_attributes
, val
, val2
);
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
)
99 struct temperature_state
*temp_st
= iio_priv(indio_dev
);
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
);
113 static const struct iio_info temperature_info
= {
114 .read_raw
= &temperature_read_raw
,
115 .write_raw
= &temperature_write_raw
,
118 /* Callback handler to send event after all samples are received and captured */
119 static int temperature_proc_event(struct hid_sensor_hub_device
*hsdev
,
120 unsigned int usage_id
, void *pdev
)
122 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
123 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
,
127 &temp_st
->temperature_data
,
128 iio_get_time_ns(indio_dev
));
133 /* Capture samples in local storage */
134 static int temperature_capture_sample(struct hid_sensor_hub_device
*hsdev
,
135 unsigned int usage_id
, size_t raw_len
,
136 char *raw_data
, void *pdev
)
138 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
139 struct temperature_state
*temp_st
= iio_priv(indio_dev
);
142 case HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE
:
143 temp_st
->temperature_data
= *(s32
*)raw_data
;
150 /* Parse report which is specific to an usage id*/
151 static int temperature_parse_report(struct platform_device
*pdev
,
152 struct hid_sensor_hub_device
*hsdev
,
153 struct iio_chan_spec
*channels
,
154 unsigned int usage_id
,
155 struct temperature_state
*st
)
159 ret
= sensor_hub_input_get_attribute_info(hsdev
, HID_INPUT_REPORT
,
161 HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE
,
162 &st
->temperature_attr
);
166 temperature_adjust_channel_bit_mask(channels
, 0,
167 st
->temperature_attr
.size
);
169 st
->scale_precision
= hid_sensor_format_scale(
170 HID_USAGE_SENSOR_TEMPERATURE
,
171 &st
->temperature_attr
,
172 &st
->scale_pre_decml
, &st
->scale_post_decml
);
174 /* Set Sensitivity field ids, when there is no individual modifier */
175 if (st
->common_attributes
.sensitivity
.index
< 0)
176 sensor_hub_input_get_attribute_info(hsdev
,
177 HID_FEATURE_REPORT
, usage_id
,
178 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
179 HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE
,
180 &st
->common_attributes
.sensitivity
);
185 static struct hid_sensor_hub_callbacks temperature_callbacks
= {
186 .send_event
= &temperature_proc_event
,
187 .capture_sample
= &temperature_capture_sample
,
190 /* Function to initialize the processing for usage id */
191 static int hid_temperature_probe(struct platform_device
*pdev
)
193 static const char *name
= "temperature";
194 struct iio_dev
*indio_dev
;
195 struct temperature_state
*temp_st
;
196 struct iio_chan_spec
*temp_chans
;
197 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
200 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*temp_st
));
204 temp_st
= iio_priv(indio_dev
);
205 temp_st
->common_attributes
.hsdev
= hsdev
;
206 temp_st
->common_attributes
.pdev
= pdev
;
208 ret
= hid_sensor_parse_common_attributes(hsdev
,
209 HID_USAGE_SENSOR_TEMPERATURE
,
210 &temp_st
->common_attributes
);
214 temp_chans
= devm_kmemdup(&indio_dev
->dev
, temperature_channels
,
215 sizeof(temperature_channels
), GFP_KERNEL
);
219 ret
= temperature_parse_report(pdev
, hsdev
, temp_chans
,
220 HID_USAGE_SENSOR_TEMPERATURE
, temp_st
);
224 indio_dev
->channels
= temp_chans
;
225 indio_dev
->num_channels
= ARRAY_SIZE(temperature_channels
);
226 indio_dev
->info
= &temperature_info
;
227 indio_dev
->name
= name
;
228 indio_dev
->modes
= INDIO_DIRECT_MODE
;
230 atomic_set(&temp_st
->common_attributes
.data_ready
, 0);
232 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
233 &temp_st
->common_attributes
);
237 platform_set_drvdata(pdev
, indio_dev
);
239 temperature_callbacks
.pdev
= pdev
;
240 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_TEMPERATURE
,
241 &temperature_callbacks
);
243 goto error_remove_trigger
;
245 ret
= devm_iio_device_register(indio_dev
->dev
.parent
, indio_dev
);
247 goto error_remove_callback
;
251 error_remove_callback
:
252 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_TEMPERATURE
);
253 error_remove_trigger
:
254 hid_sensor_remove_trigger(indio_dev
, &temp_st
->common_attributes
);
258 /* Function to deinitialize the processing for usage id */
259 static int hid_temperature_remove(struct platform_device
*pdev
)
261 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
262 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
263 struct temperature_state
*temp_st
= iio_priv(indio_dev
);
265 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_TEMPERATURE
);
266 hid_sensor_remove_trigger(indio_dev
, &temp_st
->common_attributes
);
271 static const struct platform_device_id hid_temperature_ids
[] = {
273 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
274 .name
= "HID-SENSOR-200033",
278 MODULE_DEVICE_TABLE(platform
, hid_temperature_ids
);
280 static struct platform_driver hid_temperature_platform_driver
= {
281 .id_table
= hid_temperature_ids
,
283 .name
= "temperature-sensor",
284 .pm
= &hid_sensor_pm_ops
,
286 .probe
= hid_temperature_probe
,
287 .remove
= hid_temperature_remove
,
289 module_platform_driver(hid_temperature_platform_driver
);
291 MODULE_DESCRIPTION("HID Environmental temperature sensor");
292 MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>");
293 MODULE_LICENSE("GPL v2");