1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * HID Sensors Driver 4 * Copyright (c) 2017, Intel Corporation. 5 */ 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> 12 13 #include "hid-sensor-trigger.h" 14 15 struct hid_humidity_state { 16 struct hid_sensor_common common_attributes; 17 struct hid_sensor_hub_attribute_info humidity_attr; 18 struct { 19 s32 humidity_data; 20 u64 timestamp __aligned(8); 21 } scan; 22 int scale_pre_decml; 23 int scale_post_decml; 24 int scale_precision; 25 int value_offset; 26 }; 27 28 static const u32 humidity_sensitivity_addresses[] = { 29 HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY, 30 }; 31 32 /* Channel definitions */ 33 static const struct iio_chan_spec humidity_channels[] = { 34 { 35 .type = IIO_HUMIDITYRELATIVE, 36 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 37 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 38 BIT(IIO_CHAN_INFO_SCALE) | 39 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 40 BIT(IIO_CHAN_INFO_HYSTERESIS), 41 }, 42 IIO_CHAN_SOFT_TIMESTAMP(1) 43 }; 44 45 /* Adjust channel real bits based on report descriptor */ 46 static void humidity_adjust_channel_bit_mask(struct iio_chan_spec *channels, 47 int channel, int size) 48 { 49 channels[channel].scan_type.sign = 's'; 50 /* Real storage bits will change based on the report desc. */ 51 channels[channel].scan_type.realbits = size * 8; 52 /* Maximum size of a sample to capture is s32 */ 53 channels[channel].scan_type.storagebits = sizeof(s32) * 8; 54 } 55 56 static int humidity_read_raw(struct iio_dev *indio_dev, 57 struct iio_chan_spec const *chan, 58 int *val, int *val2, long mask) 59 { 60 struct hid_humidity_state *humid_st = iio_priv(indio_dev); 61 62 switch (mask) { 63 case IIO_CHAN_INFO_RAW: 64 if (chan->type != IIO_HUMIDITYRELATIVE) 65 return -EINVAL; 66 hid_sensor_power_state(&humid_st->common_attributes, true); 67 *val = sensor_hub_input_attr_get_raw_value( 68 humid_st->common_attributes.hsdev, 69 HID_USAGE_SENSOR_HUMIDITY, 70 HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY, 71 humid_st->humidity_attr.report_id, 72 SENSOR_HUB_SYNC, 73 humid_st->humidity_attr.logical_minimum < 0); 74 hid_sensor_power_state(&humid_st->common_attributes, false); 75 76 return IIO_VAL_INT; 77 78 case IIO_CHAN_INFO_SCALE: 79 *val = humid_st->scale_pre_decml; 80 *val2 = humid_st->scale_post_decml; 81 82 return humid_st->scale_precision; 83 84 case IIO_CHAN_INFO_OFFSET: 85 *val = humid_st->value_offset; 86 87 return IIO_VAL_INT; 88 89 case IIO_CHAN_INFO_SAMP_FREQ: 90 return hid_sensor_read_samp_freq_value( 91 &humid_st->common_attributes, val, val2); 92 93 case IIO_CHAN_INFO_HYSTERESIS: 94 return hid_sensor_read_raw_hyst_value( 95 &humid_st->common_attributes, val, val2); 96 97 default: 98 return -EINVAL; 99 } 100 } 101 102 static int humidity_write_raw(struct iio_dev *indio_dev, 103 struct iio_chan_spec const *chan, 104 int val, int val2, long mask) 105 { 106 struct hid_humidity_state *humid_st = iio_priv(indio_dev); 107 108 switch (mask) { 109 case IIO_CHAN_INFO_SAMP_FREQ: 110 return hid_sensor_write_samp_freq_value( 111 &humid_st->common_attributes, val, val2); 112 113 case IIO_CHAN_INFO_HYSTERESIS: 114 return hid_sensor_write_raw_hyst_value( 115 &humid_st->common_attributes, val, val2); 116 117 default: 118 return -EINVAL; 119 } 120 } 121 122 static const struct iio_info humidity_info = { 123 .read_raw = &humidity_read_raw, 124 .write_raw = &humidity_write_raw, 125 }; 126 127 /* Callback handler to send event after all samples are received and captured */ 128 static int humidity_proc_event(struct hid_sensor_hub_device *hsdev, 129 unsigned int usage_id, void *pdev) 130 { 131 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 132 struct hid_humidity_state *humid_st = iio_priv(indio_dev); 133 134 if (atomic_read(&humid_st->common_attributes.data_ready)) 135 iio_push_to_buffers_with_timestamp(indio_dev, &humid_st->scan, 136 iio_get_time_ns(indio_dev)); 137 138 return 0; 139 } 140 141 /* Capture samples in local storage */ 142 static int humidity_capture_sample(struct hid_sensor_hub_device *hsdev, 143 unsigned int usage_id, size_t raw_len, 144 char *raw_data, void *pdev) 145 { 146 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 147 struct hid_humidity_state *humid_st = iio_priv(indio_dev); 148 149 switch (usage_id) { 150 case HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY: 151 humid_st->scan.humidity_data = *(s32 *)raw_data; 152 153 return 0; 154 default: 155 return -EINVAL; 156 } 157 } 158 159 /* Parse report which is specific to an usage id */ 160 static int humidity_parse_report(struct platform_device *pdev, 161 struct hid_sensor_hub_device *hsdev, 162 struct iio_chan_spec *channels, 163 unsigned int usage_id, 164 struct hid_humidity_state *st) 165 { 166 int ret; 167 168 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT, 169 usage_id, 170 HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY, 171 &st->humidity_attr); 172 if (ret < 0) 173 return ret; 174 175 humidity_adjust_channel_bit_mask(channels, 0, st->humidity_attr.size); 176 177 st->scale_precision = hid_sensor_format_scale( 178 HID_USAGE_SENSOR_HUMIDITY, 179 &st->humidity_attr, 180 &st->scale_pre_decml, 181 &st->scale_post_decml); 182 183 return ret; 184 } 185 186 static struct hid_sensor_hub_callbacks humidity_callbacks = { 187 .send_event = &humidity_proc_event, 188 .capture_sample = &humidity_capture_sample, 189 }; 190 191 /* Function to initialize the processing for usage id */ 192 static int hid_humidity_probe(struct platform_device *pdev) 193 { 194 static const char *name = "humidity"; 195 struct iio_dev *indio_dev; 196 struct hid_humidity_state *humid_st; 197 struct iio_chan_spec *humid_chans; 198 struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev); 199 int ret; 200 201 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*humid_st)); 202 if (!indio_dev) 203 return -ENOMEM; 204 205 humid_st = iio_priv(indio_dev); 206 humid_st->common_attributes.hsdev = hsdev; 207 humid_st->common_attributes.pdev = pdev; 208 209 ret = hid_sensor_parse_common_attributes(hsdev, 210 HID_USAGE_SENSOR_HUMIDITY, 211 &humid_st->common_attributes, 212 humidity_sensitivity_addresses, 213 ARRAY_SIZE(humidity_sensitivity_addresses)); 214 if (ret) 215 return ret; 216 217 humid_chans = devm_kmemdup(&indio_dev->dev, humidity_channels, 218 sizeof(humidity_channels), GFP_KERNEL); 219 if (!humid_chans) 220 return -ENOMEM; 221 222 ret = humidity_parse_report(pdev, hsdev, humid_chans, 223 HID_USAGE_SENSOR_HUMIDITY, humid_st); 224 if (ret) 225 return ret; 226 227 indio_dev->channels = humid_chans; 228 indio_dev->num_channels = ARRAY_SIZE(humidity_channels); 229 indio_dev->info = &humidity_info; 230 indio_dev->name = name; 231 indio_dev->modes = INDIO_DIRECT_MODE; 232 233 atomic_set(&humid_st->common_attributes.data_ready, 0); 234 235 ret = hid_sensor_setup_trigger(indio_dev, name, 236 &humid_st->common_attributes); 237 if (ret) 238 return ret; 239 240 platform_set_drvdata(pdev, indio_dev); 241 242 humidity_callbacks.pdev = pdev; 243 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY, 244 &humidity_callbacks); 245 if (ret) 246 goto error_remove_trigger; 247 248 ret = iio_device_register(indio_dev); 249 if (ret) 250 goto error_remove_callback; 251 252 return ret; 253 254 error_remove_callback: 255 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY); 256 error_remove_trigger: 257 hid_sensor_remove_trigger(indio_dev, &humid_st->common_attributes); 258 return ret; 259 } 260 261 /* Function to deinitialize the processing for usage id */ 262 static int hid_humidity_remove(struct platform_device *pdev) 263 { 264 struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev); 265 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 266 struct hid_humidity_state *humid_st = iio_priv(indio_dev); 267 268 iio_device_unregister(indio_dev); 269 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY); 270 hid_sensor_remove_trigger(indio_dev, &humid_st->common_attributes); 271 272 return 0; 273 } 274 275 static const struct platform_device_id hid_humidity_ids[] = { 276 { 277 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 278 .name = "HID-SENSOR-200032", 279 }, 280 { /* sentinel */ } 281 }; 282 MODULE_DEVICE_TABLE(platform, hid_humidity_ids); 283 284 static struct platform_driver hid_humidity_platform_driver = { 285 .id_table = hid_humidity_ids, 286 .driver = { 287 .name = KBUILD_MODNAME, 288 .pm = &hid_sensor_pm_ops, 289 }, 290 .probe = hid_humidity_probe, 291 .remove = hid_humidity_remove, 292 }; 293 module_platform_driver(hid_humidity_platform_driver); 294 295 MODULE_DESCRIPTION("HID Environmental humidity sensor"); 296 MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>"); 297 MODULE_LICENSE("GPL v2"); 298