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