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 .driver_module
= THIS_MODULE
,
130 .read_raw
= &humidity_read_raw
,
131 .write_raw
= &humidity_write_raw
,
134 /* Callback handler to send event after all samples are received and captured */
135 static int humidity_proc_event(struct hid_sensor_hub_device
*hsdev
,
136 unsigned int usage_id
, void *pdev
)
138 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
139 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
141 if (atomic_read(&humid_st
->common_attributes
.data_ready
))
142 iio_push_to_buffers_with_timestamp(indio_dev
,
143 &humid_st
->humidity_data
,
144 iio_get_time_ns(indio_dev
));
149 /* Capture samples in local storage */
150 static int humidity_capture_sample(struct hid_sensor_hub_device
*hsdev
,
151 unsigned int usage_id
, size_t raw_len
,
152 char *raw_data
, void *pdev
)
154 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
155 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
158 case HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY
:
159 humid_st
->humidity_data
= *(s32
*)raw_data
;
167 /* Parse report which is specific to an usage id */
168 static int humidity_parse_report(struct platform_device
*pdev
,
169 struct hid_sensor_hub_device
*hsdev
,
170 struct iio_chan_spec
*channels
,
171 unsigned int usage_id
,
172 struct hid_humidity_state
*st
)
176 ret
= sensor_hub_input_get_attribute_info(hsdev
, HID_INPUT_REPORT
,
178 HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY
,
183 humidity_adjust_channel_bit_mask(channels
, 0, st
->humidity_attr
.size
);
185 st
->scale_precision
= hid_sensor_format_scale(
186 HID_USAGE_SENSOR_HUMIDITY
,
188 &st
->scale_pre_decml
,
189 &st
->scale_post_decml
);
191 /* Set Sensitivity field ids, when there is no individual modifier */
192 if (st
->common_attributes
.sensitivity
.index
< 0)
193 sensor_hub_input_get_attribute_info(hsdev
,
194 HID_FEATURE_REPORT
, usage_id
,
195 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
196 HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY
,
197 &st
->common_attributes
.sensitivity
);
202 static struct hid_sensor_hub_callbacks humidity_callbacks
= {
203 .send_event
= &humidity_proc_event
,
204 .capture_sample
= &humidity_capture_sample
,
207 /* Function to initialize the processing for usage id */
208 static int hid_humidity_probe(struct platform_device
*pdev
)
210 static const char *name
= "humidity";
211 struct iio_dev
*indio_dev
;
212 struct hid_humidity_state
*humid_st
;
213 struct iio_chan_spec
*humid_chans
;
214 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
217 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*humid_st
));
221 humid_st
= iio_priv(indio_dev
);
222 humid_st
->common_attributes
.hsdev
= hsdev
;
223 humid_st
->common_attributes
.pdev
= pdev
;
225 ret
= hid_sensor_parse_common_attributes(hsdev
,
226 HID_USAGE_SENSOR_HUMIDITY
,
227 &humid_st
->common_attributes
);
231 humid_chans
= devm_kmemdup(&indio_dev
->dev
, humidity_channels
,
232 sizeof(humidity_channels
), GFP_KERNEL
);
236 ret
= humidity_parse_report(pdev
, hsdev
, humid_chans
,
237 HID_USAGE_SENSOR_HUMIDITY
, humid_st
);
241 indio_dev
->channels
= humid_chans
;
242 indio_dev
->num_channels
= ARRAY_SIZE(humidity_channels
);
243 indio_dev
->dev
.parent
= &pdev
->dev
;
244 indio_dev
->info
= &humidity_info
;
245 indio_dev
->name
= name
;
246 indio_dev
->modes
= INDIO_DIRECT_MODE
;
248 ret
= devm_iio_triggered_buffer_setup(&pdev
->dev
, indio_dev
,
249 &iio_pollfunc_store_time
, NULL
, NULL
);
253 atomic_set(&humid_st
->common_attributes
.data_ready
, 0);
254 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
255 &humid_st
->common_attributes
);
259 platform_set_drvdata(pdev
, indio_dev
);
261 humidity_callbacks
.pdev
= pdev
;
262 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_HUMIDITY
,
263 &humidity_callbacks
);
265 goto error_remove_trigger
;
267 ret
= iio_device_register(indio_dev
);
269 goto error_remove_callback
;
273 error_remove_callback
:
274 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_HUMIDITY
);
275 error_remove_trigger
:
276 hid_sensor_remove_trigger(&humid_st
->common_attributes
);
280 /* Function to deinitialize the processing for usage id */
281 static int hid_humidity_remove(struct platform_device
*pdev
)
283 struct hid_sensor_hub_device
*hsdev
= dev_get_platdata(&pdev
->dev
);
284 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
285 struct hid_humidity_state
*humid_st
= iio_priv(indio_dev
);
287 iio_device_unregister(indio_dev
);
288 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_HUMIDITY
);
289 hid_sensor_remove_trigger(&humid_st
->common_attributes
);
294 static const struct platform_device_id hid_humidity_ids
[] = {
296 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
297 .name
= "HID-SENSOR-200032",
301 MODULE_DEVICE_TABLE(platform
, hid_humidity_ids
);
303 static struct platform_driver hid_humidity_platform_driver
= {
304 .id_table
= hid_humidity_ids
,
306 .name
= KBUILD_MODNAME
,
307 .pm
= &hid_sensor_pm_ops
,
309 .probe
= hid_humidity_probe
,
310 .remove
= hid_humidity_remove
,
312 module_platform_driver(hid_humidity_platform_driver
);
314 MODULE_DESCRIPTION("HID Environmental humidity sensor");
315 MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>");
316 MODULE_LICENSE("GPL v2");