18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * HID Sensors Driver
48c2ecf20Sopenharmony_ci * Copyright (c) 2017, Intel Corporation.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#include <linux/device.h>
78c2ecf20Sopenharmony_ci#include <linux/hid-sensor-hub.h>
88c2ecf20Sopenharmony_ci#include <linux/iio/buffer.h>
98c2ecf20Sopenharmony_ci#include <linux/iio/iio.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "../common/hid-sensors/hid-sensor-trigger.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistruct temperature_state {
168c2ecf20Sopenharmony_ci	struct hid_sensor_common common_attributes;
178c2ecf20Sopenharmony_ci	struct hid_sensor_hub_attribute_info temperature_attr;
188c2ecf20Sopenharmony_ci	struct {
198c2ecf20Sopenharmony_ci		s32 temperature_data;
208c2ecf20Sopenharmony_ci		u64 timestamp __aligned(8);
218c2ecf20Sopenharmony_ci	} scan;
228c2ecf20Sopenharmony_ci	int scale_pre_decml;
238c2ecf20Sopenharmony_ci	int scale_post_decml;
248c2ecf20Sopenharmony_ci	int scale_precision;
258c2ecf20Sopenharmony_ci	int value_offset;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* Channel definitions */
298c2ecf20Sopenharmony_cistatic const struct iio_chan_spec temperature_channels[] = {
308c2ecf20Sopenharmony_ci	{
318c2ecf20Sopenharmony_ci		.type = IIO_TEMP,
328c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
338c2ecf20Sopenharmony_ci		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
348c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_SCALE) |
358c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_SAMP_FREQ) |
368c2ecf20Sopenharmony_ci			BIT(IIO_CHAN_INFO_HYSTERESIS),
378c2ecf20Sopenharmony_ci	},
388c2ecf20Sopenharmony_ci	IIO_CHAN_SOFT_TIMESTAMP(1),
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Adjust channel real bits based on report descriptor */
428c2ecf20Sopenharmony_cistatic void temperature_adjust_channel_bit_mask(struct iio_chan_spec *channels,
438c2ecf20Sopenharmony_ci					int channel, int size)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	channels[channel].scan_type.sign = 's';
468c2ecf20Sopenharmony_ci	/* Real storage bits will change based on the report desc. */
478c2ecf20Sopenharmony_ci	channels[channel].scan_type.realbits = size * 8;
488c2ecf20Sopenharmony_ci	/* Maximum size of a sample to capture is s32 */
498c2ecf20Sopenharmony_ci	channels[channel].scan_type.storagebits = sizeof(s32) * 8;
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int temperature_read_raw(struct iio_dev *indio_dev,
538c2ecf20Sopenharmony_ci				struct iio_chan_spec const *chan,
548c2ecf20Sopenharmony_ci				int *val, int *val2, long mask)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct temperature_state *temp_st = iio_priv(indio_dev);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	switch (mask) {
598c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_RAW:
608c2ecf20Sopenharmony_ci		if (chan->type != IIO_TEMP)
618c2ecf20Sopenharmony_ci			return -EINVAL;
628c2ecf20Sopenharmony_ci		hid_sensor_power_state(
638c2ecf20Sopenharmony_ci			&temp_st->common_attributes, true);
648c2ecf20Sopenharmony_ci		*val = sensor_hub_input_attr_get_raw_value(
658c2ecf20Sopenharmony_ci			temp_st->common_attributes.hsdev,
668c2ecf20Sopenharmony_ci			HID_USAGE_SENSOR_TEMPERATURE,
678c2ecf20Sopenharmony_ci			HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
688c2ecf20Sopenharmony_ci			temp_st->temperature_attr.report_id,
698c2ecf20Sopenharmony_ci			SENSOR_HUB_SYNC,
708c2ecf20Sopenharmony_ci			temp_st->temperature_attr.logical_minimum < 0);
718c2ecf20Sopenharmony_ci		hid_sensor_power_state(
728c2ecf20Sopenharmony_ci				&temp_st->common_attributes,
738c2ecf20Sopenharmony_ci				false);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SCALE:
788c2ecf20Sopenharmony_ci		*val = temp_st->scale_pre_decml;
798c2ecf20Sopenharmony_ci		*val2 = temp_st->scale_post_decml;
808c2ecf20Sopenharmony_ci		return temp_st->scale_precision;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_OFFSET:
838c2ecf20Sopenharmony_ci		*val = temp_st->value_offset;
848c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SAMP_FREQ:
878c2ecf20Sopenharmony_ci		return hid_sensor_read_samp_freq_value(
888c2ecf20Sopenharmony_ci				&temp_st->common_attributes, val, val2);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_HYSTERESIS:
918c2ecf20Sopenharmony_ci		return hid_sensor_read_raw_hyst_value(
928c2ecf20Sopenharmony_ci				&temp_st->common_attributes, val, val2);
938c2ecf20Sopenharmony_ci	default:
948c2ecf20Sopenharmony_ci		return -EINVAL;
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic int temperature_write_raw(struct iio_dev *indio_dev,
998c2ecf20Sopenharmony_ci				struct iio_chan_spec const *chan,
1008c2ecf20Sopenharmony_ci				int val, int val2, long mask)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	struct temperature_state *temp_st = iio_priv(indio_dev);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	switch (mask) {
1058c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SAMP_FREQ:
1068c2ecf20Sopenharmony_ci		return hid_sensor_write_samp_freq_value(
1078c2ecf20Sopenharmony_ci				&temp_st->common_attributes, val, val2);
1088c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_HYSTERESIS:
1098c2ecf20Sopenharmony_ci		return hid_sensor_write_raw_hyst_value(
1108c2ecf20Sopenharmony_ci				&temp_st->common_attributes, val, val2);
1118c2ecf20Sopenharmony_ci	default:
1128c2ecf20Sopenharmony_ci		return -EINVAL;
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic const struct iio_info temperature_info = {
1178c2ecf20Sopenharmony_ci	.read_raw = &temperature_read_raw,
1188c2ecf20Sopenharmony_ci	.write_raw = &temperature_write_raw,
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/* Callback handler to send event after all samples are received and captured */
1228c2ecf20Sopenharmony_cistatic int temperature_proc_event(struct hid_sensor_hub_device *hsdev,
1238c2ecf20Sopenharmony_ci				unsigned int usage_id, void *pdev)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1268c2ecf20Sopenharmony_ci	struct temperature_state *temp_st = iio_priv(indio_dev);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (atomic_read(&temp_st->common_attributes.data_ready))
1298c2ecf20Sopenharmony_ci		iio_push_to_buffers_with_timestamp(indio_dev, &temp_st->scan,
1308c2ecf20Sopenharmony_ci						   iio_get_time_ns(indio_dev));
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	return 0;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/* Capture samples in local storage */
1368c2ecf20Sopenharmony_cistatic int temperature_capture_sample(struct hid_sensor_hub_device *hsdev,
1378c2ecf20Sopenharmony_ci				unsigned int usage_id, size_t raw_len,
1388c2ecf20Sopenharmony_ci				char *raw_data, void *pdev)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1418c2ecf20Sopenharmony_ci	struct temperature_state *temp_st = iio_priv(indio_dev);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	switch (usage_id) {
1448c2ecf20Sopenharmony_ci	case HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE:
1458c2ecf20Sopenharmony_ci		temp_st->scan.temperature_data = *(s32 *)raw_data;
1468c2ecf20Sopenharmony_ci		return 0;
1478c2ecf20Sopenharmony_ci	default:
1488c2ecf20Sopenharmony_ci		return -EINVAL;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/* Parse report which is specific to an usage id*/
1538c2ecf20Sopenharmony_cistatic int temperature_parse_report(struct platform_device *pdev,
1548c2ecf20Sopenharmony_ci				struct hid_sensor_hub_device *hsdev,
1558c2ecf20Sopenharmony_ci				struct iio_chan_spec *channels,
1568c2ecf20Sopenharmony_ci				unsigned int usage_id,
1578c2ecf20Sopenharmony_ci				struct temperature_state *st)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	int ret;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
1628c2ecf20Sopenharmony_ci			usage_id,
1638c2ecf20Sopenharmony_ci			HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
1648c2ecf20Sopenharmony_ci			&st->temperature_attr);
1658c2ecf20Sopenharmony_ci	if (ret < 0)
1668c2ecf20Sopenharmony_ci		return ret;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	temperature_adjust_channel_bit_mask(channels, 0,
1698c2ecf20Sopenharmony_ci					st->temperature_attr.size);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	st->scale_precision = hid_sensor_format_scale(
1728c2ecf20Sopenharmony_ci				HID_USAGE_SENSOR_TEMPERATURE,
1738c2ecf20Sopenharmony_ci				&st->temperature_attr,
1748c2ecf20Sopenharmony_ci				&st->scale_pre_decml, &st->scale_post_decml);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	/* Set Sensitivity field ids, when there is no individual modifier */
1778c2ecf20Sopenharmony_ci	if (st->common_attributes.sensitivity.index < 0)
1788c2ecf20Sopenharmony_ci		sensor_hub_input_get_attribute_info(hsdev,
1798c2ecf20Sopenharmony_ci			HID_FEATURE_REPORT, usage_id,
1808c2ecf20Sopenharmony_ci			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
1818c2ecf20Sopenharmony_ci			HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE,
1828c2ecf20Sopenharmony_ci			&st->common_attributes.sensitivity);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	return ret;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic struct hid_sensor_hub_callbacks temperature_callbacks = {
1888c2ecf20Sopenharmony_ci	.send_event = &temperature_proc_event,
1898c2ecf20Sopenharmony_ci	.capture_sample = &temperature_capture_sample,
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/* Function to initialize the processing for usage id */
1938c2ecf20Sopenharmony_cistatic int hid_temperature_probe(struct platform_device *pdev)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	static const char *name = "temperature";
1968c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev;
1978c2ecf20Sopenharmony_ci	struct temperature_state *temp_st;
1988c2ecf20Sopenharmony_ci	struct iio_chan_spec *temp_chans;
1998c2ecf20Sopenharmony_ci	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
2008c2ecf20Sopenharmony_ci	int ret;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*temp_st));
2038c2ecf20Sopenharmony_ci	if (!indio_dev)
2048c2ecf20Sopenharmony_ci		return -ENOMEM;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	temp_st = iio_priv(indio_dev);
2078c2ecf20Sopenharmony_ci	temp_st->common_attributes.hsdev = hsdev;
2088c2ecf20Sopenharmony_ci	temp_st->common_attributes.pdev = pdev;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	ret = hid_sensor_parse_common_attributes(hsdev,
2118c2ecf20Sopenharmony_ci					HID_USAGE_SENSOR_TEMPERATURE,
2128c2ecf20Sopenharmony_ci					&temp_st->common_attributes);
2138c2ecf20Sopenharmony_ci	if (ret)
2148c2ecf20Sopenharmony_ci		return ret;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	temp_chans = devm_kmemdup(&indio_dev->dev, temperature_channels,
2178c2ecf20Sopenharmony_ci				sizeof(temperature_channels), GFP_KERNEL);
2188c2ecf20Sopenharmony_ci	if (!temp_chans)
2198c2ecf20Sopenharmony_ci		return -ENOMEM;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	ret = temperature_parse_report(pdev, hsdev, temp_chans,
2228c2ecf20Sopenharmony_ci				HID_USAGE_SENSOR_TEMPERATURE, temp_st);
2238c2ecf20Sopenharmony_ci	if (ret)
2248c2ecf20Sopenharmony_ci		return ret;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	indio_dev->channels = temp_chans;
2278c2ecf20Sopenharmony_ci	indio_dev->num_channels = ARRAY_SIZE(temperature_channels);
2288c2ecf20Sopenharmony_ci	indio_dev->info = &temperature_info;
2298c2ecf20Sopenharmony_ci	indio_dev->name = name;
2308c2ecf20Sopenharmony_ci	indio_dev->modes = INDIO_DIRECT_MODE;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	atomic_set(&temp_st->common_attributes.data_ready, 0);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	ret = hid_sensor_setup_trigger(indio_dev, name,
2358c2ecf20Sopenharmony_ci				&temp_st->common_attributes);
2368c2ecf20Sopenharmony_ci	if (ret)
2378c2ecf20Sopenharmony_ci		return ret;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, indio_dev);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	temperature_callbacks.pdev = pdev;
2428c2ecf20Sopenharmony_ci	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE,
2438c2ecf20Sopenharmony_ci					&temperature_callbacks);
2448c2ecf20Sopenharmony_ci	if (ret)
2458c2ecf20Sopenharmony_ci		goto error_remove_trigger;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
2488c2ecf20Sopenharmony_ci	if (ret)
2498c2ecf20Sopenharmony_ci		goto error_remove_callback;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return ret;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cierror_remove_callback:
2548c2ecf20Sopenharmony_ci	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE);
2558c2ecf20Sopenharmony_cierror_remove_trigger:
2568c2ecf20Sopenharmony_ci	hid_sensor_remove_trigger(indio_dev, &temp_st->common_attributes);
2578c2ecf20Sopenharmony_ci	return ret;
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci/* Function to deinitialize the processing for usage id */
2618c2ecf20Sopenharmony_cistatic int hid_temperature_remove(struct platform_device *pdev)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
2648c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
2658c2ecf20Sopenharmony_ci	struct temperature_state *temp_st = iio_priv(indio_dev);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE);
2688c2ecf20Sopenharmony_ci	hid_sensor_remove_trigger(indio_dev, &temp_st->common_attributes);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	return 0;
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic const struct platform_device_id hid_temperature_ids[] = {
2748c2ecf20Sopenharmony_ci	{
2758c2ecf20Sopenharmony_ci		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
2768c2ecf20Sopenharmony_ci		.name = "HID-SENSOR-200033",
2778c2ecf20Sopenharmony_ci	},
2788c2ecf20Sopenharmony_ci	{ /* sentinel */ }
2798c2ecf20Sopenharmony_ci};
2808c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, hid_temperature_ids);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic struct platform_driver hid_temperature_platform_driver = {
2838c2ecf20Sopenharmony_ci	.id_table = hid_temperature_ids,
2848c2ecf20Sopenharmony_ci	.driver = {
2858c2ecf20Sopenharmony_ci		.name	= "temperature-sensor",
2868c2ecf20Sopenharmony_ci		.pm	= &hid_sensor_pm_ops,
2878c2ecf20Sopenharmony_ci	},
2888c2ecf20Sopenharmony_ci	.probe		= hid_temperature_probe,
2898c2ecf20Sopenharmony_ci	.remove		= hid_temperature_remove,
2908c2ecf20Sopenharmony_ci};
2918c2ecf20Sopenharmony_cimodule_platform_driver(hid_temperature_platform_driver);
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HID Environmental temperature sensor");
2948c2ecf20Sopenharmony_ciMODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>");
2958c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
296