xref: /linux/drivers/iio/temperature/hid-sensor-temperature.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1f6cc69f1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
259d0f2daSSong Hongyan /*
359d0f2daSSong Hongyan  * HID Sensors Driver
459d0f2daSSong Hongyan  * Copyright (c) 2017, Intel Corporation.
559d0f2daSSong Hongyan  */
659d0f2daSSong Hongyan #include <linux/device.h>
759d0f2daSSong Hongyan #include <linux/hid-sensor-hub.h>
859d0f2daSSong Hongyan #include <linux/iio/buffer.h>
959d0f2daSSong Hongyan #include <linux/iio/iio.h>
1059d0f2daSSong Hongyan #include <linux/module.h>
11fb226ae7SJonathan Cameron #include <linux/mod_devicetable.h>
1259d0f2daSSong Hongyan #include <linux/platform_device.h>
1359d0f2daSSong Hongyan 
1459d0f2daSSong Hongyan #include "../common/hid-sensors/hid-sensor-trigger.h"
1559d0f2daSSong Hongyan 
1659d0f2daSSong Hongyan struct temperature_state {
1759d0f2daSSong Hongyan 	struct hid_sensor_common common_attributes;
1859d0f2daSSong Hongyan 	struct hid_sensor_hub_attribute_info temperature_attr;
19141e7633SYe Xiang 	struct {
2059d0f2daSSong Hongyan 		s32 temperature_data;
21141e7633SYe Xiang 		u64 timestamp __aligned(8);
22141e7633SYe Xiang 	} scan;
2359d0f2daSSong Hongyan 	int scale_pre_decml;
2459d0f2daSSong Hongyan 	int scale_post_decml;
2559d0f2daSSong Hongyan 	int scale_precision;
2659d0f2daSSong Hongyan 	int value_offset;
2759d0f2daSSong Hongyan };
2859d0f2daSSong Hongyan 
290e41fd51SYe Xiang static const u32 temperature_sensitivity_addresses[] = {
300e41fd51SYe Xiang 	HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
310e41fd51SYe Xiang };
320e41fd51SYe Xiang 
3359d0f2daSSong Hongyan /* Channel definitions */
3459d0f2daSSong Hongyan static const struct iio_chan_spec temperature_channels[] = {
3559d0f2daSSong Hongyan 	{
3659d0f2daSSong Hongyan 		.type = IIO_TEMP,
3759d0f2daSSong Hongyan 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
3859d0f2daSSong Hongyan 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
3959d0f2daSSong Hongyan 			BIT(IIO_CHAN_INFO_SCALE) |
4059d0f2daSSong Hongyan 			BIT(IIO_CHAN_INFO_SAMP_FREQ) |
4159d0f2daSSong Hongyan 			BIT(IIO_CHAN_INFO_HYSTERESIS),
4259d0f2daSSong Hongyan 	},
43141e7633SYe Xiang 	IIO_CHAN_SOFT_TIMESTAMP(1),
4459d0f2daSSong Hongyan };
4559d0f2daSSong Hongyan 
4659d0f2daSSong Hongyan /* Adjust channel real bits based on report descriptor */
temperature_adjust_channel_bit_mask(struct iio_chan_spec * channels,int channel,int size)4759d0f2daSSong Hongyan static void temperature_adjust_channel_bit_mask(struct iio_chan_spec *channels,
4859d0f2daSSong Hongyan 					int channel, int size)
4959d0f2daSSong Hongyan {
5059d0f2daSSong Hongyan 	channels[channel].scan_type.sign = 's';
5159d0f2daSSong Hongyan 	/* Real storage bits will change based on the report desc. */
5259d0f2daSSong Hongyan 	channels[channel].scan_type.realbits = size * 8;
5359d0f2daSSong Hongyan 	/* Maximum size of a sample to capture is s32 */
5459d0f2daSSong Hongyan 	channels[channel].scan_type.storagebits = sizeof(s32) * 8;
5559d0f2daSSong Hongyan }
5659d0f2daSSong Hongyan 
temperature_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)5759d0f2daSSong Hongyan static int temperature_read_raw(struct iio_dev *indio_dev,
5859d0f2daSSong Hongyan 				struct iio_chan_spec const *chan,
5959d0f2daSSong Hongyan 				int *val, int *val2, long mask)
6059d0f2daSSong Hongyan {
6159d0f2daSSong Hongyan 	struct temperature_state *temp_st = iio_priv(indio_dev);
6259d0f2daSSong Hongyan 
6359d0f2daSSong Hongyan 	switch (mask) {
6459d0f2daSSong Hongyan 	case IIO_CHAN_INFO_RAW:
6559d0f2daSSong Hongyan 		if (chan->type != IIO_TEMP)
6659d0f2daSSong Hongyan 			return -EINVAL;
6759d0f2daSSong Hongyan 		hid_sensor_power_state(
6859d0f2daSSong Hongyan 			&temp_st->common_attributes, true);
6959d0f2daSSong Hongyan 		*val = sensor_hub_input_attr_get_raw_value(
7059d0f2daSSong Hongyan 			temp_st->common_attributes.hsdev,
7159d0f2daSSong Hongyan 			HID_USAGE_SENSOR_TEMPERATURE,
7259d0f2daSSong Hongyan 			HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
7359d0f2daSSong Hongyan 			temp_st->temperature_attr.report_id,
740145b505SHans de Goede 			SENSOR_HUB_SYNC,
750145b505SHans de Goede 			temp_st->temperature_attr.logical_minimum < 0);
7659d0f2daSSong Hongyan 		hid_sensor_power_state(
7759d0f2daSSong Hongyan 				&temp_st->common_attributes,
7859d0f2daSSong Hongyan 				false);
7959d0f2daSSong Hongyan 
8059d0f2daSSong Hongyan 		return IIO_VAL_INT;
8159d0f2daSSong Hongyan 
8259d0f2daSSong Hongyan 	case IIO_CHAN_INFO_SCALE:
8359d0f2daSSong Hongyan 		*val = temp_st->scale_pre_decml;
8459d0f2daSSong Hongyan 		*val2 = temp_st->scale_post_decml;
8559d0f2daSSong Hongyan 		return temp_st->scale_precision;
8659d0f2daSSong Hongyan 
8759d0f2daSSong Hongyan 	case IIO_CHAN_INFO_OFFSET:
8859d0f2daSSong Hongyan 		*val = temp_st->value_offset;
8959d0f2daSSong Hongyan 		return IIO_VAL_INT;
9059d0f2daSSong Hongyan 
9159d0f2daSSong Hongyan 	case IIO_CHAN_INFO_SAMP_FREQ:
9259d0f2daSSong Hongyan 		return hid_sensor_read_samp_freq_value(
9359d0f2daSSong Hongyan 				&temp_st->common_attributes, val, val2);
9459d0f2daSSong Hongyan 
9559d0f2daSSong Hongyan 	case IIO_CHAN_INFO_HYSTERESIS:
9659d0f2daSSong Hongyan 		return hid_sensor_read_raw_hyst_value(
9759d0f2daSSong Hongyan 				&temp_st->common_attributes, val, val2);
9859d0f2daSSong Hongyan 	default:
9959d0f2daSSong Hongyan 		return -EINVAL;
10059d0f2daSSong Hongyan 	}
10159d0f2daSSong Hongyan }
10259d0f2daSSong Hongyan 
temperature_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)10359d0f2daSSong Hongyan static int temperature_write_raw(struct iio_dev *indio_dev,
10459d0f2daSSong Hongyan 				struct iio_chan_spec const *chan,
10559d0f2daSSong Hongyan 				int val, int val2, long mask)
10659d0f2daSSong Hongyan {
10759d0f2daSSong Hongyan 	struct temperature_state *temp_st = iio_priv(indio_dev);
10859d0f2daSSong Hongyan 
10959d0f2daSSong Hongyan 	switch (mask) {
11059d0f2daSSong Hongyan 	case IIO_CHAN_INFO_SAMP_FREQ:
11159d0f2daSSong Hongyan 		return hid_sensor_write_samp_freq_value(
11259d0f2daSSong Hongyan 				&temp_st->common_attributes, val, val2);
11359d0f2daSSong Hongyan 	case IIO_CHAN_INFO_HYSTERESIS:
11459d0f2daSSong Hongyan 		return hid_sensor_write_raw_hyst_value(
11559d0f2daSSong Hongyan 				&temp_st->common_attributes, val, val2);
11659d0f2daSSong Hongyan 	default:
11759d0f2daSSong Hongyan 		return -EINVAL;
11859d0f2daSSong Hongyan 	}
11959d0f2daSSong Hongyan }
12059d0f2daSSong Hongyan 
12159d0f2daSSong Hongyan static const struct iio_info temperature_info = {
12259d0f2daSSong Hongyan 	.read_raw = &temperature_read_raw,
12359d0f2daSSong Hongyan 	.write_raw = &temperature_write_raw,
12459d0f2daSSong Hongyan };
12559d0f2daSSong Hongyan 
12659d0f2daSSong Hongyan /* Callback handler to send event after all samples are received and captured */
temperature_proc_event(struct hid_sensor_hub_device * hsdev,unsigned int usage_id,void * pdev)12759d0f2daSSong Hongyan static int temperature_proc_event(struct hid_sensor_hub_device *hsdev,
12859d0f2daSSong Hongyan 				unsigned int usage_id, void *pdev)
12959d0f2daSSong Hongyan {
13059d0f2daSSong Hongyan 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
13159d0f2daSSong Hongyan 	struct temperature_state *temp_st = iio_priv(indio_dev);
13259d0f2daSSong Hongyan 
13359d0f2daSSong Hongyan 	if (atomic_read(&temp_st->common_attributes.data_ready))
134141e7633SYe Xiang 		iio_push_to_buffers_with_timestamp(indio_dev, &temp_st->scan,
13559d0f2daSSong Hongyan 						   iio_get_time_ns(indio_dev));
13659d0f2daSSong Hongyan 
13759d0f2daSSong Hongyan 	return 0;
13859d0f2daSSong Hongyan }
13959d0f2daSSong Hongyan 
14059d0f2daSSong Hongyan /* Capture samples in local storage */
temperature_capture_sample(struct hid_sensor_hub_device * hsdev,unsigned int usage_id,size_t raw_len,char * raw_data,void * pdev)14159d0f2daSSong Hongyan static int temperature_capture_sample(struct hid_sensor_hub_device *hsdev,
14259d0f2daSSong Hongyan 				unsigned int usage_id, size_t raw_len,
14359d0f2daSSong Hongyan 				char *raw_data, void *pdev)
14459d0f2daSSong Hongyan {
14559d0f2daSSong Hongyan 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
14659d0f2daSSong Hongyan 	struct temperature_state *temp_st = iio_priv(indio_dev);
14759d0f2daSSong Hongyan 
14859d0f2daSSong Hongyan 	switch (usage_id) {
14959d0f2daSSong Hongyan 	case HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE:
150141e7633SYe Xiang 		temp_st->scan.temperature_data = *(s32 *)raw_data;
15159d0f2daSSong Hongyan 		return 0;
15259d0f2daSSong Hongyan 	default:
15359d0f2daSSong Hongyan 		return -EINVAL;
15459d0f2daSSong Hongyan 	}
15559d0f2daSSong Hongyan }
15659d0f2daSSong Hongyan 
15759d0f2daSSong Hongyan /* Parse report which is specific to an usage id*/
temperature_parse_report(struct platform_device * pdev,struct hid_sensor_hub_device * hsdev,struct iio_chan_spec * channels,unsigned int usage_id,struct temperature_state * st)15859d0f2daSSong Hongyan static int temperature_parse_report(struct platform_device *pdev,
15959d0f2daSSong Hongyan 				struct hid_sensor_hub_device *hsdev,
16059d0f2daSSong Hongyan 				struct iio_chan_spec *channels,
16159d0f2daSSong Hongyan 				unsigned int usage_id,
16259d0f2daSSong Hongyan 				struct temperature_state *st)
16359d0f2daSSong Hongyan {
16459d0f2daSSong Hongyan 	int ret;
16559d0f2daSSong Hongyan 
16659d0f2daSSong Hongyan 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
16759d0f2daSSong Hongyan 			usage_id,
16859d0f2daSSong Hongyan 			HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
16959d0f2daSSong Hongyan 			&st->temperature_attr);
17059d0f2daSSong Hongyan 	if (ret < 0)
17159d0f2daSSong Hongyan 		return ret;
17259d0f2daSSong Hongyan 
17359d0f2daSSong Hongyan 	temperature_adjust_channel_bit_mask(channels, 0,
17459d0f2daSSong Hongyan 					st->temperature_attr.size);
17559d0f2daSSong Hongyan 
17659d0f2daSSong Hongyan 	st->scale_precision = hid_sensor_format_scale(
17759d0f2daSSong Hongyan 				HID_USAGE_SENSOR_TEMPERATURE,
17859d0f2daSSong Hongyan 				&st->temperature_attr,
17959d0f2daSSong Hongyan 				&st->scale_pre_decml, &st->scale_post_decml);
18059d0f2daSSong Hongyan 
18159d0f2daSSong Hongyan 	return ret;
18259d0f2daSSong Hongyan }
18359d0f2daSSong Hongyan 
18459d0f2daSSong Hongyan static struct hid_sensor_hub_callbacks temperature_callbacks = {
18559d0f2daSSong Hongyan 	.send_event = &temperature_proc_event,
18659d0f2daSSong Hongyan 	.capture_sample = &temperature_capture_sample,
18759d0f2daSSong Hongyan };
18859d0f2daSSong Hongyan 
18959d0f2daSSong Hongyan /* Function to initialize the processing for usage id */
hid_temperature_probe(struct platform_device * pdev)19059d0f2daSSong Hongyan static int hid_temperature_probe(struct platform_device *pdev)
19159d0f2daSSong Hongyan {
19259d0f2daSSong Hongyan 	static const char *name = "temperature";
19359d0f2daSSong Hongyan 	struct iio_dev *indio_dev;
19459d0f2daSSong Hongyan 	struct temperature_state *temp_st;
19559d0f2daSSong Hongyan 	struct iio_chan_spec *temp_chans;
19659d0f2daSSong Hongyan 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
19759d0f2daSSong Hongyan 	int ret;
19859d0f2daSSong Hongyan 
19959d0f2daSSong Hongyan 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*temp_st));
20059d0f2daSSong Hongyan 	if (!indio_dev)
20159d0f2daSSong Hongyan 		return -ENOMEM;
20259d0f2daSSong Hongyan 
20359d0f2daSSong Hongyan 	temp_st = iio_priv(indio_dev);
20459d0f2daSSong Hongyan 	temp_st->common_attributes.hsdev = hsdev;
20559d0f2daSSong Hongyan 	temp_st->common_attributes.pdev = pdev;
20659d0f2daSSong Hongyan 
20759d0f2daSSong Hongyan 	ret = hid_sensor_parse_common_attributes(hsdev,
20859d0f2daSSong Hongyan 					HID_USAGE_SENSOR_TEMPERATURE,
2090e41fd51SYe Xiang 					&temp_st->common_attributes,
2100e41fd51SYe Xiang 					temperature_sensitivity_addresses,
2110e41fd51SYe Xiang 					ARRAY_SIZE(temperature_sensitivity_addresses));
21259d0f2daSSong Hongyan 	if (ret)
21359d0f2daSSong Hongyan 		return ret;
21459d0f2daSSong Hongyan 
21559d0f2daSSong Hongyan 	temp_chans = devm_kmemdup(&indio_dev->dev, temperature_channels,
21659d0f2daSSong Hongyan 				sizeof(temperature_channels), GFP_KERNEL);
21759d0f2daSSong Hongyan 	if (!temp_chans)
21859d0f2daSSong Hongyan 		return -ENOMEM;
21959d0f2daSSong Hongyan 
22059d0f2daSSong Hongyan 	ret = temperature_parse_report(pdev, hsdev, temp_chans,
22159d0f2daSSong Hongyan 				HID_USAGE_SENSOR_TEMPERATURE, temp_st);
22259d0f2daSSong Hongyan 	if (ret)
22359d0f2daSSong Hongyan 		return ret;
22459d0f2daSSong Hongyan 
22559d0f2daSSong Hongyan 	indio_dev->channels = temp_chans;
22659d0f2daSSong Hongyan 	indio_dev->num_channels = ARRAY_SIZE(temperature_channels);
22759d0f2daSSong Hongyan 	indio_dev->info = &temperature_info;
22859d0f2daSSong Hongyan 	indio_dev->name = name;
22959d0f2daSSong Hongyan 	indio_dev->modes = INDIO_DIRECT_MODE;
23059d0f2daSSong Hongyan 
23159d0f2daSSong Hongyan 	atomic_set(&temp_st->common_attributes.data_ready, 0);
232067fda1cSAlexandru Ardelean 
23359d0f2daSSong Hongyan 	ret = hid_sensor_setup_trigger(indio_dev, name,
23459d0f2daSSong Hongyan 				&temp_st->common_attributes);
23559d0f2daSSong Hongyan 	if (ret)
23659d0f2daSSong Hongyan 		return ret;
23759d0f2daSSong Hongyan 
23859d0f2daSSong Hongyan 	platform_set_drvdata(pdev, indio_dev);
23959d0f2daSSong Hongyan 
24059d0f2daSSong Hongyan 	temperature_callbacks.pdev = pdev;
24159d0f2daSSong Hongyan 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE,
24259d0f2daSSong Hongyan 					&temperature_callbacks);
24359d0f2daSSong Hongyan 	if (ret)
24459d0f2daSSong Hongyan 		goto error_remove_trigger;
24559d0f2daSSong Hongyan 
24659d0f2daSSong Hongyan 	ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
24759d0f2daSSong Hongyan 	if (ret)
24859d0f2daSSong Hongyan 		goto error_remove_callback;
24959d0f2daSSong Hongyan 
25059d0f2daSSong Hongyan 	return ret;
25159d0f2daSSong Hongyan 
25259d0f2daSSong Hongyan error_remove_callback:
25359d0f2daSSong Hongyan 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE);
25459d0f2daSSong Hongyan error_remove_trigger:
255067fda1cSAlexandru Ardelean 	hid_sensor_remove_trigger(indio_dev, &temp_st->common_attributes);
25659d0f2daSSong Hongyan 	return ret;
25759d0f2daSSong Hongyan }
25859d0f2daSSong Hongyan 
25959d0f2daSSong Hongyan /* Function to deinitialize the processing for usage id */
hid_temperature_remove(struct platform_device * pdev)260*f5589173SUwe Kleine-König static void hid_temperature_remove(struct platform_device *pdev)
26159d0f2daSSong Hongyan {
26259d0f2daSSong Hongyan 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
26359d0f2daSSong Hongyan 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
26459d0f2daSSong Hongyan 	struct temperature_state *temp_st = iio_priv(indio_dev);
26559d0f2daSSong Hongyan 
26659d0f2daSSong Hongyan 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE);
267067fda1cSAlexandru Ardelean 	hid_sensor_remove_trigger(indio_dev, &temp_st->common_attributes);
26859d0f2daSSong Hongyan }
26959d0f2daSSong Hongyan 
27059d0f2daSSong Hongyan static const struct platform_device_id hid_temperature_ids[] = {
27159d0f2daSSong Hongyan 	{
27259d0f2daSSong Hongyan 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
27359d0f2daSSong Hongyan 		.name = "HID-SENSOR-200033",
27459d0f2daSSong Hongyan 	},
27559d0f2daSSong Hongyan 	{ /* sentinel */ }
27659d0f2daSSong Hongyan };
27759d0f2daSSong Hongyan MODULE_DEVICE_TABLE(platform, hid_temperature_ids);
27859d0f2daSSong Hongyan 
27959d0f2daSSong Hongyan static struct platform_driver hid_temperature_platform_driver = {
28059d0f2daSSong Hongyan 	.id_table = hid_temperature_ids,
28159d0f2daSSong Hongyan 	.driver = {
28259d0f2daSSong Hongyan 		.name	= "temperature-sensor",
28359d0f2daSSong Hongyan 		.pm	= &hid_sensor_pm_ops,
28459d0f2daSSong Hongyan 	},
28559d0f2daSSong Hongyan 	.probe		= hid_temperature_probe,
286*f5589173SUwe Kleine-König 	.remove_new	= hid_temperature_remove,
28759d0f2daSSong Hongyan };
28859d0f2daSSong Hongyan module_platform_driver(hid_temperature_platform_driver);
28959d0f2daSSong Hongyan 
29059d0f2daSSong Hongyan MODULE_DESCRIPTION("HID Environmental temperature sensor");
29159d0f2daSSong Hongyan MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>");
29259d0f2daSSong Hongyan MODULE_LICENSE("GPL v2");
29312f13d1fSAndy Shevchenko MODULE_IMPORT_NS(IIO_HID);
294