3 * Copyright (c) 2017, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
17 #include <linux/device.h>
18 #include <linux/hid-sensor-hub.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/triggered_buffer.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
26 #include "hid-sensor-trigger.h"
28 struct hid_humidity_state
{
29 struct hid_sensor_common common_attributes
;
30 struct hid_sensor_hub_attribute_info humidity_attr
;
38 /* Channel definitions */
39 static const struct iio_chan_spec humidity_channels
[] = {
41 .type
= IIO_HUMIDITYRELATIVE
,
42 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
43 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
44 BIT(IIO_CHAN_INFO_SCALE
) |
45 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
46 BIT(IIO_CHAN_INFO_HYSTERESIS
),
48 IIO_CHAN_SOFT_TIMESTAMP(1)
51 /* Adjust channel real bits based on report descriptor */
52 static void humidity_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
53 int channel
, int size
)
55 channels
[channel
].scan_type
.sign
= 's';
56 /* Real storage bits will change based on the report desc. */
57 channels
[channel
].scan_type
.realbits
= size
* 8;
58 /* Maximum size of a sample to capture is s32 */
59 channels
[channel
].scan_type
.storagebits
= sizeof(s32
) * 8;
62 static int humidity_read_raw(struct iio_dev
*indio_dev
,
63 struct iio_chan_spec
const *chan
,
64 int *val
, int *val2
, long mask
)
66 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
69 case IIO_CHAN_INFO_RAW
:
70 if (chan
->type
!= IIO_HUMIDITYRELATIVE
)
72 hid_sensor_power_state(&humid_st
->common_attributes
, true);
73 *val
= sensor_hub_input_attr_get_raw_value(
74 humid_st
->common_attributes
.hsdev
,
75 HID_USAGE_SENSOR_HUMIDITY
,
76 HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY
,
77 humid_st
->humidity_attr
.report_id
,
79 humid_st
->humidity_attr
.logical_minimum
< 0);
80 hid_sensor_power_state(&humid_st
->common_attributes
, false);
84 case IIO_CHAN_INFO_SCALE
:
85 *val
= humid_st
->scale_pre_decml
;
86 *val2
= humid_st
->scale_post_decml
;
88 return humid_st
->scale_precision
;
90 case IIO_CHAN_INFO_OFFSET
:
91 *val
= humid_st
->value_offset
;
95 case IIO_CHAN_INFO_SAMP_FREQ
:
96 return hid_sensor_read_samp_freq_value(
97 &humid_st
->common_attributes
, val
, val2
);
99 case IIO_CHAN_INFO_HYSTERESIS
:
100 return hid_sensor_read_raw_hyst_value(
101 &humid_st
->common_attributes
, val
, val2
);
108 static int humidity_write_raw(struct iio_dev
*indio_dev
,
109 struct iio_chan_spec
const *chan
,
110 int val
, int val2
, long mask
)
112 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
115 case IIO_CHAN_INFO_SAMP_FREQ
:
116 return hid_sensor_write_samp_freq_value(
117 &humid_st
->common_attributes
, val
, val2
);
119 case IIO_CHAN_INFO_HYSTERESIS
:
120 return hid_sensor_write_raw_hyst_value(
121 &humid_st
->common_attributes
, val
, val2
);
128 static const struct iio_info humidity_info
= {
129 .read_raw
= &humidity_read_raw
,
130 .write_raw
= &humidity_write_raw
,
133 /* Callback handler to send event after all samples are received and captured */
134 static int humidity_proc_event(struct hid_sensor_hub_device
*hsdev
,
135 unsigned int usage_id
, void *pdev
)
137 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
138 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
140 if (atomic_read(&humid_st
->common_attributes
.data_ready
))
141 iio_push_to_buffers_with_timestamp(indio_dev
,
142 &humid_st
->humidity_data
,
143 iio_get_time_ns(indio_dev
));
148 /* Capture samples in local storage */
149 static int humidity_capture_sample(struct hid_sensor_hub_device
*hsdev
,
150 unsigned int usage_id
, size_t raw_len
,
151 char *raw_data
, void *pdev
)
153 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
154 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
157 case HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY
:
158 humid_st
->humidity_data
= *(s32
*)raw_data
;
166 /* Parse report which is specific to an usage id */
167 static int humidity_parse_report(struct platform_device
*pdev
,
168 struct hid_sensor_hub_device
*hsdev
,
169 struct iio_chan_spec
*channels
,
170 unsigned int usage_id
,
171 struct hid_humidity_state
*st
)
175 ret
= sensor_hub_input_get_attribute_info(hsdev
, HID_INPUT_REPORT
,
177 HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY
,
182 humidity_adjust_channel_bit_mask(channels
, 0, st
->humidity_attr
.size
);
184 st
->scale_precision
= hid_sensor_format_scale(
185 HID_USAGE_SENSOR_HUMIDITY
,
187 &st
->scale_pre_decml
,
188 &st
->scale_post_decml
);
190 /* Set Sensitivity field ids, when there is no individual modifier */
191 if (st
->common_attributes
.sensitivity
.index
< 0)
192 sensor_hub_input_get_attribute_info(hsdev
,
193 HID_FEATURE_REPORT
, usage_id
,
194 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
195 HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY
,
196 &st
->common_attributes
.sensitivity
);
201 static struct hid_sensor_hub_callbacks humidity_callbacks
= {
202 .send_event
= &humidity_proc_event
,
203 .capture_sample
= &humidity_capture_sample
,
206 /* Function to initialize the processing for usage id */
207 static int hid_humidity_probe(struct platform_device
*pdev
)
209 static const char *name
= "humidity";
210 struct iio_dev
*indio_dev
;
211 struct hid_humidity_state
*humid_st
;
212 struct iio_chan_spec
*humid_chans
;
213 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
216 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*humid_st
));
220 humid_st
= iio_priv(indio_dev
);
221 humid_st
->common_attributes
.hsdev
= hsdev
;
222 humid_st
->common_attributes
.pdev
= pdev
;
224 ret
= hid_sensor_parse_common_attributes(hsdev
,
225 HID_USAGE_SENSOR_HUMIDITY
,
226 &humid_st
->common_attributes
);
230 humid_chans
= devm_kmemdup(&indio_dev
->dev
, humidity_channels
,
231 sizeof(humidity_channels
), GFP_KERNEL
);
235 ret
= humidity_parse_report(pdev
, hsdev
, humid_chans
,
236 HID_USAGE_SENSOR_HUMIDITY
, humid_st
);
240 indio_dev
->channels
= humid_chans
;
241 indio_dev
->num_channels
= ARRAY_SIZE(humidity_channels
);
242 indio_dev
->dev
.parent
= &pdev
->dev
;
243 indio_dev
->info
= &humidity_info
;
244 indio_dev
->name
= name
;
245 indio_dev
->modes
= INDIO_DIRECT_MODE
;
247 ret
= devm_iio_triggered_buffer_setup(&pdev
->dev
, indio_dev
,
248 &iio_pollfunc_store_time
, NULL
, NULL
);
252 atomic_set(&humid_st
->common_attributes
.data_ready
, 0);
253 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
254 &humid_st
->common_attributes
);
258 platform_set_drvdata(pdev
, indio_dev
);
260 humidity_callbacks
.pdev
= pdev
;
261 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_HUMIDITY
,
262 &humidity_callbacks
);
264 goto error_remove_trigger
;
266 ret
= iio_device_register(indio_dev
);
268 goto error_remove_callback
;
272 error_remove_callback
:
273 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_HUMIDITY
);
274 error_remove_trigger
:
275 hid_sensor_remove_trigger(&humid_st
->common_attributes
);
279 /* Function to deinitialize the processing for usage id */
280 static int hid_humidity_remove(struct platform_device
*pdev
)
282 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
283 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
284 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
286 iio_device_unregister(indio_dev
);
287 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_HUMIDITY
);
288 hid_sensor_remove_trigger(&humid_st
->common_attributes
);
293 static const struct platform_device_id hid_humidity_ids
[] = {
295 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
296 .name
= "HID-SENSOR-200032",
300 MODULE_DEVICE_TABLE(platform
, hid_humidity_ids
);
302 static struct platform_driver hid_humidity_platform_driver
= {
303 .id_table
= hid_humidity_ids
,
305 .name
= KBUILD_MODNAME
,
306 .pm
= &hid_sensor_pm_ops
,
308 .probe
= hid_humidity_probe
,
309 .remove
= hid_humidity_remove
,
311 module_platform_driver(hid_humidity_platform_driver
);
313 MODULE_DESCRIPTION("HID Environmental humidity sensor");
314 MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>");
315 MODULE_LICENSE("GPL v2");