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