xref: /linux/drivers/iio/temperature/hid-sensor-temperature.c (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
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/iio/triggered_buffer.h>
11 #include <linux/iio/trigger_consumer.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 
15 #include "../common/hid-sensors/hid-sensor-trigger.h"
16 
17 struct temperature_state {
18 	struct hid_sensor_common common_attributes;
19 	struct hid_sensor_hub_attribute_info temperature_attr;
20 	s32 temperature_data;
21 	int scale_pre_decml;
22 	int scale_post_decml;
23 	int scale_precision;
24 	int value_offset;
25 };
26 
27 /* Channel definitions */
28 static const struct iio_chan_spec temperature_channels[] = {
29 	{
30 		.type = IIO_TEMP,
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),
36 	},
37 	IIO_CHAN_SOFT_TIMESTAMP(3),
38 };
39 
40 /* Adjust channel real bits based on report descriptor */
41 static void temperature_adjust_channel_bit_mask(struct iio_chan_spec *channels,
42 					int channel, int size)
43 {
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;
49 }
50 
51 static int temperature_read_raw(struct iio_dev *indio_dev,
52 				struct iio_chan_spec const *chan,
53 				int *val, int *val2, long mask)
54 {
55 	struct temperature_state *temp_st = iio_priv(indio_dev);
56 
57 	switch (mask) {
58 	case IIO_CHAN_INFO_RAW:
59 		if (chan->type != IIO_TEMP)
60 			return -EINVAL;
61 		hid_sensor_power_state(
62 			&temp_st->common_attributes, true);
63 		*val = sensor_hub_input_attr_get_raw_value(
64 			temp_st->common_attributes.hsdev,
65 			HID_USAGE_SENSOR_TEMPERATURE,
66 			HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
67 			temp_st->temperature_attr.report_id,
68 			SENSOR_HUB_SYNC,
69 			temp_st->temperature_attr.logical_minimum < 0);
70 		hid_sensor_power_state(
71 				&temp_st->common_attributes,
72 				false);
73 
74 		return IIO_VAL_INT;
75 
76 	case IIO_CHAN_INFO_SCALE:
77 		*val = temp_st->scale_pre_decml;
78 		*val2 = temp_st->scale_post_decml;
79 		return temp_st->scale_precision;
80 
81 	case IIO_CHAN_INFO_OFFSET:
82 		*val = temp_st->value_offset;
83 		return IIO_VAL_INT;
84 
85 	case IIO_CHAN_INFO_SAMP_FREQ:
86 		return hid_sensor_read_samp_freq_value(
87 				&temp_st->common_attributes, val, val2);
88 
89 	case IIO_CHAN_INFO_HYSTERESIS:
90 		return hid_sensor_read_raw_hyst_value(
91 				&temp_st->common_attributes, val, val2);
92 	default:
93 		return -EINVAL;
94 	}
95 }
96 
97 static int temperature_write_raw(struct iio_dev *indio_dev,
98 				struct iio_chan_spec const *chan,
99 				int val, int val2, long mask)
100 {
101 	struct temperature_state *temp_st = iio_priv(indio_dev);
102 
103 	switch (mask) {
104 	case IIO_CHAN_INFO_SAMP_FREQ:
105 		return hid_sensor_write_samp_freq_value(
106 				&temp_st->common_attributes, val, val2);
107 	case IIO_CHAN_INFO_HYSTERESIS:
108 		return hid_sensor_write_raw_hyst_value(
109 				&temp_st->common_attributes, val, val2);
110 	default:
111 		return -EINVAL;
112 	}
113 }
114 
115 static const struct iio_info temperature_info = {
116 	.read_raw = &temperature_read_raw,
117 	.write_raw = &temperature_write_raw,
118 };
119 
120 /* Callback handler to send event after all samples are received and captured */
121 static int temperature_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 temperature_state *temp_st = iio_priv(indio_dev);
126 
127 	if (atomic_read(&temp_st->common_attributes.data_ready))
128 		iio_push_to_buffers_with_timestamp(indio_dev,
129 				&temp_st->temperature_data,
130 				iio_get_time_ns(indio_dev));
131 
132 	return 0;
133 }
134 
135 /* Capture samples in local storage */
136 static int temperature_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 temperature_state *temp_st = iio_priv(indio_dev);
142 
143 	switch (usage_id) {
144 	case HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE:
145 		temp_st->temperature_data = *(s32 *)raw_data;
146 		return 0;
147 	default:
148 		return -EINVAL;
149 	}
150 }
151 
152 /* Parse report which is specific to an usage id*/
153 static int temperature_parse_report(struct platform_device *pdev,
154 				struct hid_sensor_hub_device *hsdev,
155 				struct iio_chan_spec *channels,
156 				unsigned int usage_id,
157 				struct temperature_state *st)
158 {
159 	int ret;
160 
161 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
162 			usage_id,
163 			HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
164 			&st->temperature_attr);
165 	if (ret < 0)
166 		return ret;
167 
168 	temperature_adjust_channel_bit_mask(channels, 0,
169 					st->temperature_attr.size);
170 
171 	st->scale_precision = hid_sensor_format_scale(
172 				HID_USAGE_SENSOR_TEMPERATURE,
173 				&st->temperature_attr,
174 				&st->scale_pre_decml, &st->scale_post_decml);
175 
176 	/* Set Sensitivity field ids, when there is no individual modifier */
177 	if (st->common_attributes.sensitivity.index < 0)
178 		sensor_hub_input_get_attribute_info(hsdev,
179 			HID_FEATURE_REPORT, usage_id,
180 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
181 			HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
182 			&st->common_attributes.sensitivity);
183 
184 	return ret;
185 }
186 
187 static struct hid_sensor_hub_callbacks temperature_callbacks = {
188 	.send_event = &temperature_proc_event,
189 	.capture_sample = &temperature_capture_sample,
190 };
191 
192 /* Function to initialize the processing for usage id */
193 static int hid_temperature_probe(struct platform_device *pdev)
194 {
195 	static const char *name = "temperature";
196 	struct iio_dev *indio_dev;
197 	struct temperature_state *temp_st;
198 	struct iio_chan_spec *temp_chans;
199 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
200 	int ret;
201 
202 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*temp_st));
203 	if (!indio_dev)
204 		return -ENOMEM;
205 
206 	temp_st = iio_priv(indio_dev);
207 	temp_st->common_attributes.hsdev = hsdev;
208 	temp_st->common_attributes.pdev = pdev;
209 
210 	ret = hid_sensor_parse_common_attributes(hsdev,
211 					HID_USAGE_SENSOR_TEMPERATURE,
212 					&temp_st->common_attributes);
213 	if (ret)
214 		return ret;
215 
216 	temp_chans = devm_kmemdup(&indio_dev->dev, temperature_channels,
217 				sizeof(temperature_channels), GFP_KERNEL);
218 	if (!temp_chans)
219 		return -ENOMEM;
220 
221 	ret = temperature_parse_report(pdev, hsdev, temp_chans,
222 				HID_USAGE_SENSOR_TEMPERATURE, temp_st);
223 	if (ret)
224 		return ret;
225 
226 	indio_dev->channels = temp_chans;
227 	indio_dev->num_channels = ARRAY_SIZE(temperature_channels);
228 	indio_dev->dev.parent = &pdev->dev;
229 	indio_dev->info = &temperature_info;
230 	indio_dev->name = name;
231 	indio_dev->modes = INDIO_DIRECT_MODE;
232 
233 	ret = devm_iio_triggered_buffer_setup(&pdev->dev, indio_dev,
234 					&iio_pollfunc_store_time, NULL, NULL);
235 	if (ret)
236 		return ret;
237 
238 	atomic_set(&temp_st->common_attributes.data_ready, 0);
239 	ret = hid_sensor_setup_trigger(indio_dev, name,
240 				&temp_st->common_attributes);
241 	if (ret)
242 		return ret;
243 
244 	platform_set_drvdata(pdev, indio_dev);
245 
246 	temperature_callbacks.pdev = pdev;
247 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE,
248 					&temperature_callbacks);
249 	if (ret)
250 		goto error_remove_trigger;
251 
252 	ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
253 	if (ret)
254 		goto error_remove_callback;
255 
256 	return ret;
257 
258 error_remove_callback:
259 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE);
260 error_remove_trigger:
261 	hid_sensor_remove_trigger(&temp_st->common_attributes);
262 	return ret;
263 }
264 
265 /* Function to deinitialize the processing for usage id */
266 static int hid_temperature_remove(struct platform_device *pdev)
267 {
268 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
269 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
270 	struct temperature_state *temp_st = iio_priv(indio_dev);
271 
272 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE);
273 	hid_sensor_remove_trigger(&temp_st->common_attributes);
274 
275 	return 0;
276 }
277 
278 static const struct platform_device_id hid_temperature_ids[] = {
279 	{
280 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
281 		.name = "HID-SENSOR-200033",
282 	},
283 	{ /* sentinel */ }
284 };
285 MODULE_DEVICE_TABLE(platform, hid_temperature_ids);
286 
287 static struct platform_driver hid_temperature_platform_driver = {
288 	.id_table = hid_temperature_ids,
289 	.driver = {
290 		.name	= "temperature-sensor",
291 		.pm	= &hid_sensor_pm_ops,
292 	},
293 	.probe		= hid_temperature_probe,
294 	.remove		= hid_temperature_remove,
295 };
296 module_platform_driver(hid_temperature_platform_driver);
297 
298 MODULE_DESCRIPTION("HID Environmental temperature sensor");
299 MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>");
300 MODULE_LICENSE("GPL v2");
301