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 	s32 humidity_data;
19 	int scale_pre_decml;
20 	int scale_post_decml;
21 	int scale_precision;
22 	int value_offset;
23 };
24 
25 /* Channel definitions */
26 static const struct iio_chan_spec humidity_channels[] = {
27 	{
28 		.type = IIO_HUMIDITYRELATIVE,
29 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
30 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
31 			BIT(IIO_CHAN_INFO_SCALE) |
32 			BIT(IIO_CHAN_INFO_SAMP_FREQ) |
33 			BIT(IIO_CHAN_INFO_HYSTERESIS),
34 	},
35 	IIO_CHAN_SOFT_TIMESTAMP(1)
36 };
37 
38 /* Adjust channel real bits based on report descriptor */
39 static void humidity_adjust_channel_bit_mask(struct iio_chan_spec *channels,
40 					int channel, int size)
41 {
42 	channels[channel].scan_type.sign = 's';
43 	/* Real storage bits will change based on the report desc. */
44 	channels[channel].scan_type.realbits = size * 8;
45 	/* Maximum size of a sample to capture is s32 */
46 	channels[channel].scan_type.storagebits = sizeof(s32) * 8;
47 }
48 
49 static int humidity_read_raw(struct iio_dev *indio_dev,
50 				struct iio_chan_spec const *chan,
51 				int *val, int *val2, long mask)
52 {
53 	struct hid_humidity_state *humid_st = iio_priv(indio_dev);
54 
55 	switch (mask) {
56 	case IIO_CHAN_INFO_RAW:
57 		if (chan->type != IIO_HUMIDITYRELATIVE)
58 			return -EINVAL;
59 		hid_sensor_power_state(&humid_st->common_attributes, true);
60 		*val = sensor_hub_input_attr_get_raw_value(
61 				humid_st->common_attributes.hsdev,
62 				HID_USAGE_SENSOR_HUMIDITY,
63 				HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY,
64 				humid_st->humidity_attr.report_id,
65 				SENSOR_HUB_SYNC,
66 				humid_st->humidity_attr.logical_minimum < 0);
67 		hid_sensor_power_state(&humid_st->common_attributes, false);
68 
69 		return IIO_VAL_INT;
70 
71 	case IIO_CHAN_INFO_SCALE:
72 		*val = humid_st->scale_pre_decml;
73 		*val2 = humid_st->scale_post_decml;
74 
75 		return humid_st->scale_precision;
76 
77 	case IIO_CHAN_INFO_OFFSET:
78 		*val = humid_st->value_offset;
79 
80 		return IIO_VAL_INT;
81 
82 	case IIO_CHAN_INFO_SAMP_FREQ:
83 		return hid_sensor_read_samp_freq_value(
84 				&humid_st->common_attributes, val, val2);
85 
86 	case IIO_CHAN_INFO_HYSTERESIS:
87 		return hid_sensor_read_raw_hyst_value(
88 				&humid_st->common_attributes, val, val2);
89 
90 	default:
91 		return -EINVAL;
92 	}
93 }
94 
95 static int humidity_write_raw(struct iio_dev *indio_dev,
96 				struct iio_chan_spec const *chan,
97 				int val, int val2, long mask)
98 {
99 	struct hid_humidity_state *humid_st = iio_priv(indio_dev);
100 
101 	switch (mask) {
102 	case IIO_CHAN_INFO_SAMP_FREQ:
103 		return hid_sensor_write_samp_freq_value(
104 				&humid_st->common_attributes, val, val2);
105 
106 	case IIO_CHAN_INFO_HYSTERESIS:
107 		return hid_sensor_write_raw_hyst_value(
108 				&humid_st->common_attributes, val, val2);
109 
110 	default:
111 		return -EINVAL;
112 	}
113 }
114 
115 static const struct iio_info humidity_info = {
116 	.read_raw = &humidity_read_raw,
117 	.write_raw = &humidity_write_raw,
118 };
119 
120 /* Callback handler to send event after all samples are received and captured */
121 static int humidity_proc_event(struct hid_sensor_hub_device *hsdev,
122 				unsigned int usage_id, void *pdev)
123 {
124 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
125 	struct hid_humidity_state *humid_st = iio_priv(indio_dev);
126 
127 	if (atomic_read(&humid_st->common_attributes.data_ready))
128 		iio_push_to_buffers_with_timestamp(indio_dev,
129 					&humid_st->humidity_data,
130 					iio_get_time_ns(indio_dev));
131 
132 	return 0;
133 }
134 
135 /* Capture samples in local storage */
136 static int humidity_capture_sample(struct hid_sensor_hub_device *hsdev,
137 				unsigned int usage_id, size_t raw_len,
138 				char *raw_data, void *pdev)
139 {
140 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
141 	struct hid_humidity_state *humid_st = iio_priv(indio_dev);
142 
143 	switch (usage_id) {
144 	case HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY:
145 		humid_st->humidity_data = *(s32 *)raw_data;
146 
147 		return 0;
148 	default:
149 		return -EINVAL;
150 	}
151 }
152 
153 /* Parse report which is specific to an usage id */
154 static int humidity_parse_report(struct platform_device *pdev,
155 				struct hid_sensor_hub_device *hsdev,
156 				struct iio_chan_spec *channels,
157 				unsigned int usage_id,
158 				struct hid_humidity_state *st)
159 {
160 	int ret;
161 
162 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
163 					usage_id,
164 					HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY,
165 					&st->humidity_attr);
166 	if (ret < 0)
167 		return ret;
168 
169 	humidity_adjust_channel_bit_mask(channels, 0, st->humidity_attr.size);
170 
171 	st->scale_precision = hid_sensor_format_scale(
172 						HID_USAGE_SENSOR_HUMIDITY,
173 						&st->humidity_attr,
174 						&st->scale_pre_decml,
175 						&st->scale_post_decml);
176 
177 	/* Set Sensitivity field ids, when there is no individual modifier */
178 	if (st->common_attributes.sensitivity.index < 0)
179 		sensor_hub_input_get_attribute_info(hsdev,
180 			HID_FEATURE_REPORT, usage_id,
181 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
182 			HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY,
183 			&st->common_attributes.sensitivity);
184 
185 	return ret;
186 }
187 
188 static struct hid_sensor_hub_callbacks humidity_callbacks = {
189 	.send_event = &humidity_proc_event,
190 	.capture_sample = &humidity_capture_sample,
191 };
192 
193 /* Function to initialize the processing for usage id */
194 static int hid_humidity_probe(struct platform_device *pdev)
195 {
196 	static const char *name = "humidity";
197 	struct iio_dev *indio_dev;
198 	struct hid_humidity_state *humid_st;
199 	struct iio_chan_spec *humid_chans;
200 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
201 	int ret;
202 
203 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*humid_st));
204 	if (!indio_dev)
205 		return -ENOMEM;
206 
207 	humid_st = iio_priv(indio_dev);
208 	humid_st->common_attributes.hsdev = hsdev;
209 	humid_st->common_attributes.pdev = pdev;
210 
211 	ret = hid_sensor_parse_common_attributes(hsdev,
212 					HID_USAGE_SENSOR_HUMIDITY,
213 					&humid_st->common_attributes);
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