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