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