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 "hid-sensor-trigger.h"
17 struct hid_humidity_state
{
18 struct hid_sensor_common common_attributes
;
19 struct hid_sensor_hub_attribute_info humidity_attr
;
27 /* Channel definitions */
28 static const struct iio_chan_spec humidity_channels
[] = {
30 .type
= IIO_HUMIDITYRELATIVE
,
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(1)
40 /* Adjust channel real bits based on report descriptor */
41 static void humidity_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 humidity_read_raw(struct iio_dev
*indio_dev
,
52 struct iio_chan_spec
const *chan
,
53 int *val
, int *val2
, long mask
)
55 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
58 case IIO_CHAN_INFO_RAW
:
59 if (chan
->type
!= IIO_HUMIDITYRELATIVE
)
61 hid_sensor_power_state(&humid_st
->common_attributes
, true);
62 *val
= sensor_hub_input_attr_get_raw_value(
63 humid_st
->common_attributes
.hsdev
,
64 HID_USAGE_SENSOR_HUMIDITY
,
65 HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY
,
66 humid_st
->humidity_attr
.report_id
,
68 humid_st
->humidity_attr
.logical_minimum
< 0);
69 hid_sensor_power_state(&humid_st
->common_attributes
, false);
73 case IIO_CHAN_INFO_SCALE
:
74 *val
= humid_st
->scale_pre_decml
;
75 *val2
= humid_st
->scale_post_decml
;
77 return humid_st
->scale_precision
;
79 case IIO_CHAN_INFO_OFFSET
:
80 *val
= humid_st
->value_offset
;
84 case IIO_CHAN_INFO_SAMP_FREQ
:
85 return hid_sensor_read_samp_freq_value(
86 &humid_st
->common_attributes
, val
, val2
);
88 case IIO_CHAN_INFO_HYSTERESIS
:
89 return hid_sensor_read_raw_hyst_value(
90 &humid_st
->common_attributes
, val
, val2
);
97 static int humidity_write_raw(struct iio_dev
*indio_dev
,
98 struct iio_chan_spec
const *chan
,
99 int val
, int val2
, long mask
)
101 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
104 case IIO_CHAN_INFO_SAMP_FREQ
:
105 return hid_sensor_write_samp_freq_value(
106 &humid_st
->common_attributes
, val
, val2
);
108 case IIO_CHAN_INFO_HYSTERESIS
:
109 return hid_sensor_write_raw_hyst_value(
110 &humid_st
->common_attributes
, val
, val2
);
117 static const struct iio_info humidity_info
= {
118 .read_raw
= &humidity_read_raw
,
119 .write_raw
= &humidity_write_raw
,
122 /* Callback handler to send event after all samples are received and captured */
123 static int humidity_proc_event(struct hid_sensor_hub_device
*hsdev
,
124 unsigned int usage_id
, void *pdev
)
126 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
127 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
129 if (atomic_read(&humid_st
->common_attributes
.data_ready
))
130 iio_push_to_buffers_with_timestamp(indio_dev
,
131 &humid_st
->humidity_data
,
132 iio_get_time_ns(indio_dev
));
137 /* Capture samples in local storage */
138 static int humidity_capture_sample(struct hid_sensor_hub_device
*hsdev
,
139 unsigned int usage_id
, size_t raw_len
,
140 char *raw_data
, void *pdev
)
142 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
143 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
146 case HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY
:
147 humid_st
->humidity_data
= *(s32
*)raw_data
;
155 /* Parse report which is specific to an usage id */
156 static int humidity_parse_report(struct platform_device
*pdev
,
157 struct hid_sensor_hub_device
*hsdev
,
158 struct iio_chan_spec
*channels
,
159 unsigned int usage_id
,
160 struct hid_humidity_state
*st
)
164 ret
= sensor_hub_input_get_attribute_info(hsdev
, HID_INPUT_REPORT
,
166 HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY
,
171 humidity_adjust_channel_bit_mask(channels
, 0, st
->humidity_attr
.size
);
173 st
->scale_precision
= hid_sensor_format_scale(
174 HID_USAGE_SENSOR_HUMIDITY
,
176 &st
->scale_pre_decml
,
177 &st
->scale_post_decml
);
179 /* Set Sensitivity field ids, when there is no individual modifier */
180 if (st
->common_attributes
.sensitivity
.index
< 0)
181 sensor_hub_input_get_attribute_info(hsdev
,
182 HID_FEATURE_REPORT
, usage_id
,
183 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
184 HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY
,
185 &st
->common_attributes
.sensitivity
);
190 static struct hid_sensor_hub_callbacks humidity_callbacks
= {
191 .send_event
= &humidity_proc_event
,
192 .capture_sample
= &humidity_capture_sample
,
195 /* Function to initialize the processing for usage id */
196 static int hid_humidity_probe(struct platform_device
*pdev
)
198 static const char *name
= "humidity";
199 struct iio_dev
*indio_dev
;
200 struct hid_humidity_state
*humid_st
;
201 struct iio_chan_spec
*humid_chans
;
202 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
205 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*humid_st
));
209 humid_st
= iio_priv(indio_dev
);
210 humid_st
->common_attributes
.hsdev
= hsdev
;
211 humid_st
->common_attributes
.pdev
= pdev
;
213 ret
= hid_sensor_parse_common_attributes(hsdev
,
214 HID_USAGE_SENSOR_HUMIDITY
,
215 &humid_st
->common_attributes
);
219 humid_chans
= devm_kmemdup(&indio_dev
->dev
, humidity_channels
,
220 sizeof(humidity_channels
), GFP_KERNEL
);
224 ret
= humidity_parse_report(pdev
, hsdev
, humid_chans
,
225 HID_USAGE_SENSOR_HUMIDITY
, humid_st
);
229 indio_dev
->channels
= humid_chans
;
230 indio_dev
->num_channels
= ARRAY_SIZE(humidity_channels
);
231 indio_dev
->dev
.parent
= &pdev
->dev
;
232 indio_dev
->info
= &humidity_info
;
233 indio_dev
->name
= name
;
234 indio_dev
->modes
= INDIO_DIRECT_MODE
;
236 ret
= devm_iio_triggered_buffer_setup(&pdev
->dev
, indio_dev
,
237 &iio_pollfunc_store_time
, NULL
, NULL
);
241 atomic_set(&humid_st
->common_attributes
.data_ready
, 0);
242 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
243 &humid_st
->common_attributes
);
247 platform_set_drvdata(pdev
, indio_dev
);
249 humidity_callbacks
.pdev
= pdev
;
250 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_HUMIDITY
,
251 &humidity_callbacks
);
253 goto error_remove_trigger
;
255 ret
= iio_device_register(indio_dev
);
257 goto error_remove_callback
;
261 error_remove_callback
:
262 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_HUMIDITY
);
263 error_remove_trigger
:
264 hid_sensor_remove_trigger(&humid_st
->common_attributes
);
268 /* Function to deinitialize the processing for usage id */
269 static int hid_humidity_remove(struct platform_device
*pdev
)
271 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
272 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
273 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
275 iio_device_unregister(indio_dev
);
276 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_HUMIDITY
);
277 hid_sensor_remove_trigger(&humid_st
->common_attributes
);
282 static const struct platform_device_id hid_humidity_ids
[] = {
284 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
285 .name
= "HID-SENSOR-200032",
289 MODULE_DEVICE_TABLE(platform
, hid_humidity_ids
);
291 static struct platform_driver hid_humidity_platform_driver
= {
292 .id_table
= hid_humidity_ids
,
294 .name
= KBUILD_MODNAME
,
295 .pm
= &hid_sensor_pm_ops
,
297 .probe
= hid_humidity_probe
,
298 .remove
= hid_humidity_remove
,
300 module_platform_driver(hid_humidity_platform_driver
);
302 MODULE_DESCRIPTION("HID Environmental humidity sensor");
303 MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>");
304 MODULE_LICENSE("GPL v2");