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/iio/triggered_buffer.h>
11 #include <linux/iio/trigger_consumer.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
15 #include "../common/hid-sensors/hid-sensor-trigger.h"
17 struct temperature_state
{
18 struct hid_sensor_common common_attributes
;
19 struct hid_sensor_hub_attribute_info temperature_attr
;
27 /* Channel definitions */
28 static const struct iio_chan_spec temperature_channels
[] = {
31 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
32 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
33 BIT(IIO_CHAN_INFO_SCALE
) |
34 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
35 BIT(IIO_CHAN_INFO_HYSTERESIS
),
37 IIO_CHAN_SOFT_TIMESTAMP(3),
40 /* Adjust channel real bits based on report descriptor */
41 static void temperature_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
42 int channel
, int size
)
44 channels
[channel
].scan_type
.sign
= 's';
45 /* Real storage bits will change based on the report desc. */
46 channels
[channel
].scan_type
.realbits
= size
* 8;
47 /* Maximum size of a sample to capture is s32 */
48 channels
[channel
].scan_type
.storagebits
= sizeof(s32
) * 8;
51 static int temperature_read_raw(struct iio_dev
*indio_dev
,
52 struct iio_chan_spec
const *chan
,
53 int *val
, int *val2
, long mask
)
55 struct temperature_state
*temp_st
= iio_priv(indio_dev
);
58 case IIO_CHAN_INFO_RAW
:
59 if (chan
->type
!= IIO_TEMP
)
61 hid_sensor_power_state(
62 &temp_st
->common_attributes
, true);
63 *val
= sensor_hub_input_attr_get_raw_value(
64 temp_st
->common_attributes
.hsdev
,
65 HID_USAGE_SENSOR_TEMPERATURE
,
66 HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE
,
67 temp_st
->temperature_attr
.report_id
,
69 temp_st
->temperature_attr
.logical_minimum
< 0);
70 hid_sensor_power_state(
71 &temp_st
->common_attributes
,
76 case IIO_CHAN_INFO_SCALE
:
77 *val
= temp_st
->scale_pre_decml
;
78 *val2
= temp_st
->scale_post_decml
;
79 return temp_st
->scale_precision
;
81 case IIO_CHAN_INFO_OFFSET
:
82 *val
= temp_st
->value_offset
;
85 case IIO_CHAN_INFO_SAMP_FREQ
:
86 return hid_sensor_read_samp_freq_value(
87 &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
);
97 static int temperature_write_raw(struct iio_dev
*indio_dev
,
98 struct iio_chan_spec
const *chan
,
99 int val
, int val2
, long mask
)
101 struct temperature_state
*temp_st
= iio_priv(indio_dev
);
104 case IIO_CHAN_INFO_SAMP_FREQ
:
105 return hid_sensor_write_samp_freq_value(
106 &temp_st
->common_attributes
, val
, val2
);
107 case IIO_CHAN_INFO_HYSTERESIS
:
108 return hid_sensor_write_raw_hyst_value(
109 &temp_st
->common_attributes
, val
, val2
);
115 static const struct iio_info temperature_info
= {
116 .read_raw
= &temperature_read_raw
,
117 .write_raw
= &temperature_write_raw
,
120 /* Callback handler to send event after all samples are received and captured */
121 static int temperature_proc_event(struct hid_sensor_hub_device
*hsdev
,
122 unsigned int usage_id
, void *pdev
)
124 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
125 struct temperature_state
*temp_st
= iio_priv(indio_dev
);
127 if (atomic_read(&temp_st
->common_attributes
.data_ready
))
128 iio_push_to_buffers_with_timestamp(indio_dev
,
129 &temp_st
->temperature_data
,
130 iio_get_time_ns(indio_dev
));
135 /* Capture samples in local storage */
136 static int temperature_capture_sample(struct hid_sensor_hub_device
*hsdev
,
137 unsigned int usage_id
, size_t raw_len
,
138 char *raw_data
, void *pdev
)
140 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
141 struct temperature_state
*temp_st
= iio_priv(indio_dev
);
144 case HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE
:
145 temp_st
->temperature_data
= *(s32
*)raw_data
;
152 /* Parse report which is specific to an usage id*/
153 static int temperature_parse_report(struct platform_device
*pdev
,
154 struct hid_sensor_hub_device
*hsdev
,
155 struct iio_chan_spec
*channels
,
156 unsigned int usage_id
,
157 struct temperature_state
*st
)
161 ret
= sensor_hub_input_get_attribute_info(hsdev
, HID_INPUT_REPORT
,
163 HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE
,
164 &st
->temperature_attr
);
168 temperature_adjust_channel_bit_mask(channels
, 0,
169 st
->temperature_attr
.size
);
171 st
->scale_precision
= hid_sensor_format_scale(
172 HID_USAGE_SENSOR_TEMPERATURE
,
173 &st
->temperature_attr
,
174 &st
->scale_pre_decml
, &st
->scale_post_decml
);
176 /* Set Sensitivity field ids, when there is no individual modifier */
177 if (st
->common_attributes
.sensitivity
.index
< 0)
178 sensor_hub_input_get_attribute_info(hsdev
,
179 HID_FEATURE_REPORT
, usage_id
,
180 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
181 HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE
,
182 &st
->common_attributes
.sensitivity
);
187 static struct hid_sensor_hub_callbacks temperature_callbacks
= {
188 .send_event
= &temperature_proc_event
,
189 .capture_sample
= &temperature_capture_sample
,
192 /* Function to initialize the processing for usage id */
193 static int hid_temperature_probe(struct platform_device
*pdev
)
195 static const char *name
= "temperature";
196 struct iio_dev
*indio_dev
;
197 struct temperature_state
*temp_st
;
198 struct iio_chan_spec
*temp_chans
;
199 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
202 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*temp_st
));
206 temp_st
= iio_priv(indio_dev
);
207 temp_st
->common_attributes
.hsdev
= hsdev
;
208 temp_st
->common_attributes
.pdev
= pdev
;
210 ret
= hid_sensor_parse_common_attributes(hsdev
,
211 HID_USAGE_SENSOR_TEMPERATURE
,
212 &temp_st
->common_attributes
);
216 temp_chans
= devm_kmemdup(&indio_dev
->dev
, temperature_channels
,
217 sizeof(temperature_channels
), GFP_KERNEL
);
221 ret
= temperature_parse_report(pdev
, hsdev
, temp_chans
,
222 HID_USAGE_SENSOR_TEMPERATURE
, temp_st
);
226 indio_dev
->channels
= temp_chans
;
227 indio_dev
->num_channels
= ARRAY_SIZE(temperature_channels
);
228 indio_dev
->dev
.parent
= &pdev
->dev
;
229 indio_dev
->info
= &temperature_info
;
230 indio_dev
->name
= name
;
231 indio_dev
->modes
= INDIO_DIRECT_MODE
;
233 ret
= devm_iio_triggered_buffer_setup(&pdev
->dev
, indio_dev
,
234 &iio_pollfunc_store_time
, NULL
, NULL
);
238 atomic_set(&temp_st
->common_attributes
.data_ready
, 0);
239 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
240 &temp_st
->common_attributes
);
244 platform_set_drvdata(pdev
, indio_dev
);
246 temperature_callbacks
.pdev
= pdev
;
247 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_TEMPERATURE
,
248 &temperature_callbacks
);
250 goto error_remove_trigger
;
252 ret
= devm_iio_device_register(indio_dev
->dev
.parent
, indio_dev
);
254 goto error_remove_callback
;
258 error_remove_callback
:
259 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_TEMPERATURE
);
260 error_remove_trigger
:
261 hid_sensor_remove_trigger(&temp_st
->common_attributes
);
265 /* Function to deinitialize the processing for usage id */
266 static int hid_temperature_remove(struct platform_device
*pdev
)
268 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
269 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
270 struct temperature_state
*temp_st
= iio_priv(indio_dev
);
272 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_TEMPERATURE
);
273 hid_sensor_remove_trigger(&temp_st
->common_attributes
);
278 static const struct platform_device_id hid_temperature_ids
[] = {
280 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
281 .name
= "HID-SENSOR-200033",
285 MODULE_DEVICE_TABLE(platform
, hid_temperature_ids
);
287 static struct platform_driver hid_temperature_platform_driver
= {
288 .id_table
= hid_temperature_ids
,
290 .name
= "temperature-sensor",
291 .pm
= &hid_sensor_pm_ops
,
293 .probe
= hid_temperature_probe
,
294 .remove
= hid_temperature_remove
,
296 module_platform_driver(hid_temperature_platform_driver
);
298 MODULE_DESCRIPTION("HID Environmental temperature sensor");
299 MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>");
300 MODULE_LICENSE("GPL v2");