xref: /openbmc/linux/drivers/iio/temperature/hid-sensor-temperature.c (revision f8a11425075ff11b4b5784f077cb84f3d2dfb3f0)
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 	struct {
19 		s32 temperature_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 temperature_sensitivity_addresses[] = {
29 	HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
30 };
31 
32 /* Channel definitions */
33 static const struct iio_chan_spec temperature_channels[] = {
34 	{
35 		.type = IIO_TEMP,
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 temperature_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 temperature_read_raw(struct iio_dev *indio_dev,
57 				struct iio_chan_spec const *chan,
58 				int *val, int *val2, long mask)
59 {
60 	struct temperature_state *temp_st = iio_priv(indio_dev);
61 
62 	switch (mask) {
63 	case IIO_CHAN_INFO_RAW:
64 		if (chan->type != IIO_TEMP)
65 			return -EINVAL;
66 		hid_sensor_power_state(
67 			&temp_st->common_attributes, true);
68 		*val = sensor_hub_input_attr_get_raw_value(
69 			temp_st->common_attributes.hsdev,
70 			HID_USAGE_SENSOR_TEMPERATURE,
71 			HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
72 			temp_st->temperature_attr.report_id,
73 			SENSOR_HUB_SYNC,
74 			temp_st->temperature_attr.logical_minimum < 0);
75 		hid_sensor_power_state(
76 				&temp_st->common_attributes,
77 				false);
78 
79 		return IIO_VAL_INT;
80 
81 	case IIO_CHAN_INFO_SCALE:
82 		*val = temp_st->scale_pre_decml;
83 		*val2 = temp_st->scale_post_decml;
84 		return temp_st->scale_precision;
85 
86 	case IIO_CHAN_INFO_OFFSET:
87 		*val = temp_st->value_offset;
88 		return IIO_VAL_INT;
89 
90 	case IIO_CHAN_INFO_SAMP_FREQ:
91 		return hid_sensor_read_samp_freq_value(
92 				&temp_st->common_attributes, val, val2);
93 
94 	case IIO_CHAN_INFO_HYSTERESIS:
95 		return hid_sensor_read_raw_hyst_value(
96 				&temp_st->common_attributes, val, val2);
97 	default:
98 		return -EINVAL;
99 	}
100 }
101 
102 static int temperature_write_raw(struct iio_dev *indio_dev,
103 				struct iio_chan_spec const *chan,
104 				int val, int val2, long mask)
105 {
106 	struct temperature_state *temp_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 				&temp_st->common_attributes, val, val2);
112 	case IIO_CHAN_INFO_HYSTERESIS:
113 		return hid_sensor_write_raw_hyst_value(
114 				&temp_st->common_attributes, val, val2);
115 	default:
116 		return -EINVAL;
117 	}
118 }
119 
120 static const struct iio_info temperature_info = {
121 	.read_raw = &temperature_read_raw,
122 	.write_raw = &temperature_write_raw,
123 };
124 
125 /* Callback handler to send event after all samples are received and captured */
126 static int temperature_proc_event(struct hid_sensor_hub_device *hsdev,
127 				unsigned int usage_id, void *pdev)
128 {
129 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
130 	struct temperature_state *temp_st = iio_priv(indio_dev);
131 
132 	if (atomic_read(&temp_st->common_attributes.data_ready))
133 		iio_push_to_buffers_with_timestamp(indio_dev, &temp_st->scan,
134 						   iio_get_time_ns(indio_dev));
135 
136 	return 0;
137 }
138 
139 /* Capture samples in local storage */
140 static int temperature_capture_sample(struct hid_sensor_hub_device *hsdev,
141 				unsigned int usage_id, size_t raw_len,
142 				char *raw_data, void *pdev)
143 {
144 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
145 	struct temperature_state *temp_st = iio_priv(indio_dev);
146 
147 	switch (usage_id) {
148 	case HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE:
149 		temp_st->scan.temperature_data = *(s32 *)raw_data;
150 		return 0;
151 	default:
152 		return -EINVAL;
153 	}
154 }
155 
156 /* Parse report which is specific to an usage id*/
157 static int temperature_parse_report(struct platform_device *pdev,
158 				struct hid_sensor_hub_device *hsdev,
159 				struct iio_chan_spec *channels,
160 				unsigned int usage_id,
161 				struct temperature_state *st)
162 {
163 	int ret;
164 
165 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
166 			usage_id,
167 			HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
168 			&st->temperature_attr);
169 	if (ret < 0)
170 		return ret;
171 
172 	temperature_adjust_channel_bit_mask(channels, 0,
173 					st->temperature_attr.size);
174 
175 	st->scale_precision = hid_sensor_format_scale(
176 				HID_USAGE_SENSOR_TEMPERATURE,
177 				&st->temperature_attr,
178 				&st->scale_pre_decml, &st->scale_post_decml);
179 
180 	return ret;
181 }
182 
183 static struct hid_sensor_hub_callbacks temperature_callbacks = {
184 	.send_event = &temperature_proc_event,
185 	.capture_sample = &temperature_capture_sample,
186 };
187 
188 /* Function to initialize the processing for usage id */
189 static int hid_temperature_probe(struct platform_device *pdev)
190 {
191 	static const char *name = "temperature";
192 	struct iio_dev *indio_dev;
193 	struct temperature_state *temp_st;
194 	struct iio_chan_spec *temp_chans;
195 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
196 	int ret;
197 
198 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*temp_st));
199 	if (!indio_dev)
200 		return -ENOMEM;
201 
202 	temp_st = iio_priv(indio_dev);
203 	temp_st->common_attributes.hsdev = hsdev;
204 	temp_st->common_attributes.pdev = pdev;
205 
206 	ret = hid_sensor_parse_common_attributes(hsdev,
207 					HID_USAGE_SENSOR_TEMPERATURE,
208 					&temp_st->common_attributes,
209 					temperature_sensitivity_addresses,
210 					ARRAY_SIZE(temperature_sensitivity_addresses));
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