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 "hid-sensor-trigger.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistruct hid_humidity_state {
168c2ecf20Sopenharmony_ci	struct hid_sensor_common common_attributes;
178c2ecf20Sopenharmony_ci	struct hid_sensor_hub_attribute_info humidity_attr;
188c2ecf20Sopenharmony_ci	struct {
198c2ecf20Sopenharmony_ci		s32 humidity_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 humidity_channels[] = {
308c2ecf20Sopenharmony_ci	{
318c2ecf20Sopenharmony_ci		.type = IIO_HUMIDITYRELATIVE,
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 humidity_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 humidity_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 hid_humidity_state *humid_st = iio_priv(indio_dev);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	switch (mask) {
598c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_RAW:
608c2ecf20Sopenharmony_ci		if (chan->type != IIO_HUMIDITYRELATIVE)
618c2ecf20Sopenharmony_ci			return -EINVAL;
628c2ecf20Sopenharmony_ci		hid_sensor_power_state(&humid_st->common_attributes, true);
638c2ecf20Sopenharmony_ci		*val = sensor_hub_input_attr_get_raw_value(
648c2ecf20Sopenharmony_ci				humid_st->common_attributes.hsdev,
658c2ecf20Sopenharmony_ci				HID_USAGE_SENSOR_HUMIDITY,
668c2ecf20Sopenharmony_ci				HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY,
678c2ecf20Sopenharmony_ci				humid_st->humidity_attr.report_id,
688c2ecf20Sopenharmony_ci				SENSOR_HUB_SYNC,
698c2ecf20Sopenharmony_ci				humid_st->humidity_attr.logical_minimum < 0);
708c2ecf20Sopenharmony_ci		hid_sensor_power_state(&humid_st->common_attributes, false);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SCALE:
758c2ecf20Sopenharmony_ci		*val = humid_st->scale_pre_decml;
768c2ecf20Sopenharmony_ci		*val2 = humid_st->scale_post_decml;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci		return humid_st->scale_precision;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_OFFSET:
818c2ecf20Sopenharmony_ci		*val = humid_st->value_offset;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		return IIO_VAL_INT;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SAMP_FREQ:
868c2ecf20Sopenharmony_ci		return hid_sensor_read_samp_freq_value(
878c2ecf20Sopenharmony_ci				&humid_st->common_attributes, val, val2);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_HYSTERESIS:
908c2ecf20Sopenharmony_ci		return hid_sensor_read_raw_hyst_value(
918c2ecf20Sopenharmony_ci				&humid_st->common_attributes, val, val2);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	default:
948c2ecf20Sopenharmony_ci		return -EINVAL;
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic int humidity_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 hid_humidity_state *humid_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				&humid_st->common_attributes, val, val2);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_HYSTERESIS:
1108c2ecf20Sopenharmony_ci		return hid_sensor_write_raw_hyst_value(
1118c2ecf20Sopenharmony_ci				&humid_st->common_attributes, val, val2);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	default:
1148c2ecf20Sopenharmony_ci		return -EINVAL;
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic const struct iio_info humidity_info = {
1198c2ecf20Sopenharmony_ci	.read_raw = &humidity_read_raw,
1208c2ecf20Sopenharmony_ci	.write_raw = &humidity_write_raw,
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* Callback handler to send event after all samples are received and captured */
1248c2ecf20Sopenharmony_cistatic int humidity_proc_event(struct hid_sensor_hub_device *hsdev,
1258c2ecf20Sopenharmony_ci				unsigned int usage_id, void *pdev)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1288c2ecf20Sopenharmony_ci	struct hid_humidity_state *humid_st = iio_priv(indio_dev);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (atomic_read(&humid_st->common_attributes.data_ready))
1318c2ecf20Sopenharmony_ci		iio_push_to_buffers_with_timestamp(indio_dev, &humid_st->scan,
1328c2ecf20Sopenharmony_ci						   iio_get_time_ns(indio_dev));
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return 0;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci/* Capture samples in local storage */
1388c2ecf20Sopenharmony_cistatic int humidity_capture_sample(struct hid_sensor_hub_device *hsdev,
1398c2ecf20Sopenharmony_ci				unsigned int usage_id, size_t raw_len,
1408c2ecf20Sopenharmony_ci				char *raw_data, void *pdev)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1438c2ecf20Sopenharmony_ci	struct hid_humidity_state *humid_st = iio_priv(indio_dev);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	switch (usage_id) {
1468c2ecf20Sopenharmony_ci	case HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY:
1478c2ecf20Sopenharmony_ci		humid_st->scan.humidity_data = *(s32 *)raw_data;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci		return 0;
1508c2ecf20Sopenharmony_ci	default:
1518c2ecf20Sopenharmony_ci		return -EINVAL;
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/* Parse report which is specific to an usage id */
1568c2ecf20Sopenharmony_cistatic int humidity_parse_report(struct platform_device *pdev,
1578c2ecf20Sopenharmony_ci				struct hid_sensor_hub_device *hsdev,
1588c2ecf20Sopenharmony_ci				struct iio_chan_spec *channels,
1598c2ecf20Sopenharmony_ci				unsigned int usage_id,
1608c2ecf20Sopenharmony_ci				struct hid_humidity_state *st)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	int ret;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
1658c2ecf20Sopenharmony_ci					usage_id,
1668c2ecf20Sopenharmony_ci					HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY,
1678c2ecf20Sopenharmony_ci					&st->humidity_attr);
1688c2ecf20Sopenharmony_ci	if (ret < 0)
1698c2ecf20Sopenharmony_ci		return ret;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	humidity_adjust_channel_bit_mask(channels, 0, st->humidity_attr.size);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	st->scale_precision = hid_sensor_format_scale(
1748c2ecf20Sopenharmony_ci						HID_USAGE_SENSOR_HUMIDITY,
1758c2ecf20Sopenharmony_ci						&st->humidity_attr,
1768c2ecf20Sopenharmony_ci						&st->scale_pre_decml,
1778c2ecf20Sopenharmony_ci						&st->scale_post_decml);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	/* Set Sensitivity field ids, when there is no individual modifier */
1808c2ecf20Sopenharmony_ci	if (st->common_attributes.sensitivity.index < 0)
1818c2ecf20Sopenharmony_ci		sensor_hub_input_get_attribute_info(hsdev,
1828c2ecf20Sopenharmony_ci			HID_FEATURE_REPORT, usage_id,
1838c2ecf20Sopenharmony_ci			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
1848c2ecf20Sopenharmony_ci			HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY,
1858c2ecf20Sopenharmony_ci			&st->common_attributes.sensitivity);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	return ret;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic struct hid_sensor_hub_callbacks humidity_callbacks = {
1918c2ecf20Sopenharmony_ci	.send_event = &humidity_proc_event,
1928c2ecf20Sopenharmony_ci	.capture_sample = &humidity_capture_sample,
1938c2ecf20Sopenharmony_ci};
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci/* Function to initialize the processing for usage id */
1968c2ecf20Sopenharmony_cistatic int hid_humidity_probe(struct platform_device *pdev)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	static const char *name = "humidity";
1998c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev;
2008c2ecf20Sopenharmony_ci	struct hid_humidity_state *humid_st;
2018c2ecf20Sopenharmony_ci	struct iio_chan_spec *humid_chans;
2028c2ecf20Sopenharmony_ci	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
2038c2ecf20Sopenharmony_ci	int ret;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*humid_st));
2068c2ecf20Sopenharmony_ci	if (!indio_dev)
2078c2ecf20Sopenharmony_ci		return -ENOMEM;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	humid_st = iio_priv(indio_dev);
2108c2ecf20Sopenharmony_ci	humid_st->common_attributes.hsdev = hsdev;
2118c2ecf20Sopenharmony_ci	humid_st->common_attributes.pdev = pdev;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	ret = hid_sensor_parse_common_attributes(hsdev,
2148c2ecf20Sopenharmony_ci					HID_USAGE_SENSOR_HUMIDITY,
2158c2ecf20Sopenharmony_ci					&humid_st->common_attributes);
2168c2ecf20Sopenharmony_ci	if (ret)
2178c2ecf20Sopenharmony_ci		return ret;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	humid_chans = devm_kmemdup(&indio_dev->dev, humidity_channels,
2208c2ecf20Sopenharmony_ci					sizeof(humidity_channels), GFP_KERNEL);
2218c2ecf20Sopenharmony_ci	if (!humid_chans)
2228c2ecf20Sopenharmony_ci		return -ENOMEM;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	ret = humidity_parse_report(pdev, hsdev, humid_chans,
2258c2ecf20Sopenharmony_ci				HID_USAGE_SENSOR_HUMIDITY, humid_st);
2268c2ecf20Sopenharmony_ci	if (ret)
2278c2ecf20Sopenharmony_ci		return ret;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	indio_dev->channels = humid_chans;
2308c2ecf20Sopenharmony_ci	indio_dev->num_channels = ARRAY_SIZE(humidity_channels);
2318c2ecf20Sopenharmony_ci	indio_dev->info = &humidity_info;
2328c2ecf20Sopenharmony_ci	indio_dev->name = name;
2338c2ecf20Sopenharmony_ci	indio_dev->modes = INDIO_DIRECT_MODE;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	atomic_set(&humid_st->common_attributes.data_ready, 0);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	ret = hid_sensor_setup_trigger(indio_dev, name,
2388c2ecf20Sopenharmony_ci				&humid_st->common_attributes);
2398c2ecf20Sopenharmony_ci	if (ret)
2408c2ecf20Sopenharmony_ci		return ret;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, indio_dev);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	humidity_callbacks.pdev = pdev;
2458c2ecf20Sopenharmony_ci	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY,
2468c2ecf20Sopenharmony_ci					&humidity_callbacks);
2478c2ecf20Sopenharmony_ci	if (ret)
2488c2ecf20Sopenharmony_ci		goto error_remove_trigger;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	ret = iio_device_register(indio_dev);
2518c2ecf20Sopenharmony_ci	if (ret)
2528c2ecf20Sopenharmony_ci		goto error_remove_callback;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	return ret;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cierror_remove_callback:
2578c2ecf20Sopenharmony_ci	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY);
2588c2ecf20Sopenharmony_cierror_remove_trigger:
2598c2ecf20Sopenharmony_ci	hid_sensor_remove_trigger(indio_dev, &humid_st->common_attributes);
2608c2ecf20Sopenharmony_ci	return ret;
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci/* Function to deinitialize the processing for usage id */
2648c2ecf20Sopenharmony_cistatic int hid_humidity_remove(struct platform_device *pdev)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
2678c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
2688c2ecf20Sopenharmony_ci	struct hid_humidity_state *humid_st = iio_priv(indio_dev);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	iio_device_unregister(indio_dev);
2718c2ecf20Sopenharmony_ci	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY);
2728c2ecf20Sopenharmony_ci	hid_sensor_remove_trigger(indio_dev, &humid_st->common_attributes);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	return 0;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic const struct platform_device_id hid_humidity_ids[] = {
2788c2ecf20Sopenharmony_ci	{
2798c2ecf20Sopenharmony_ci		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
2808c2ecf20Sopenharmony_ci		.name = "HID-SENSOR-200032",
2818c2ecf20Sopenharmony_ci	},
2828c2ecf20Sopenharmony_ci	{ /* sentinel */ }
2838c2ecf20Sopenharmony_ci};
2848c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, hid_humidity_ids);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic struct platform_driver hid_humidity_platform_driver = {
2878c2ecf20Sopenharmony_ci	.id_table = hid_humidity_ids,
2888c2ecf20Sopenharmony_ci	.driver = {
2898c2ecf20Sopenharmony_ci		.name	= KBUILD_MODNAME,
2908c2ecf20Sopenharmony_ci		.pm	= &hid_sensor_pm_ops,
2918c2ecf20Sopenharmony_ci	},
2928c2ecf20Sopenharmony_ci	.probe		= hid_humidity_probe,
2938c2ecf20Sopenharmony_ci	.remove		= hid_humidity_remove,
2948c2ecf20Sopenharmony_ci};
2958c2ecf20Sopenharmony_cimodule_platform_driver(hid_humidity_platform_driver);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HID Environmental humidity sensor");
2988c2ecf20Sopenharmony_ciMODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>");
2998c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
300