Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * HID Sensors Driver
0004  * Copyright (c) 2014, Intel Corporation.
0005  */
0006 
0007 #include <linux/device.h>
0008 #include <linux/platform_device.h>
0009 #include <linux/module.h>
0010 #include <linux/mod_devicetable.h>
0011 #include <linux/hid-sensor-hub.h>
0012 #include <linux/iio/iio.h>
0013 #include <linux/iio/sysfs.h>
0014 #include <linux/iio/buffer.h>
0015 #include "../common/hid-sensors/hid-sensor-trigger.h"
0016 
0017 struct dev_rot_state {
0018     struct hid_sensor_hub_callbacks callbacks;
0019     struct hid_sensor_common common_attributes;
0020     struct hid_sensor_hub_attribute_info quaternion;
0021     struct {
0022         s32 sampled_vals[4] __aligned(16);
0023         u64 timestamp __aligned(8);
0024     } scan;
0025     int scale_pre_decml;
0026     int scale_post_decml;
0027     int scale_precision;
0028     int value_offset;
0029     s64 timestamp;
0030 };
0031 
0032 static const u32 rotation_sensitivity_addresses[] = {
0033     HID_USAGE_SENSOR_DATA_ORIENTATION,
0034     HID_USAGE_SENSOR_ORIENT_QUATERNION,
0035 };
0036 
0037 /* Channel definitions */
0038 static const struct iio_chan_spec dev_rot_channels[] = {
0039     {
0040         .type = IIO_ROT,
0041         .modified = 1,
0042         .channel2 = IIO_MOD_QUATERNION,
0043         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
0044         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
0045                     BIT(IIO_CHAN_INFO_OFFSET) |
0046                     BIT(IIO_CHAN_INFO_SCALE) |
0047                     BIT(IIO_CHAN_INFO_HYSTERESIS),
0048         .scan_index = 0
0049     },
0050     IIO_CHAN_SOFT_TIMESTAMP(1)
0051 };
0052 
0053 /* Adjust channel real bits based on report descriptor */
0054 static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan,
0055                         int size)
0056 {
0057     chan->scan_type.sign = 's';
0058     /* Real storage bits will change based on the report desc. */
0059     chan->scan_type.realbits = size * 8;
0060     /* Maximum size of a sample to capture is u32 */
0061     chan->scan_type.storagebits = sizeof(u32) * 8;
0062     chan->scan_type.repeat = 4;
0063 }
0064 
0065 /* Channel read_raw handler */
0066 static int dev_rot_read_raw(struct iio_dev *indio_dev,
0067                 struct iio_chan_spec const *chan,
0068                 int size, int *vals, int *val_len,
0069                 long mask)
0070 {
0071     struct dev_rot_state *rot_state = iio_priv(indio_dev);
0072     int ret_type;
0073     int i;
0074 
0075     vals[0] = 0;
0076     vals[1] = 0;
0077 
0078     switch (mask) {
0079     case IIO_CHAN_INFO_RAW:
0080         if (size >= 4) {
0081             for (i = 0; i < 4; ++i)
0082                 vals[i] = rot_state->scan.sampled_vals[i];
0083             ret_type = IIO_VAL_INT_MULTIPLE;
0084             *val_len =  4;
0085         } else
0086             ret_type = -EINVAL;
0087         break;
0088     case IIO_CHAN_INFO_SCALE:
0089         vals[0] = rot_state->scale_pre_decml;
0090         vals[1] = rot_state->scale_post_decml;
0091         return rot_state->scale_precision;
0092 
0093     case IIO_CHAN_INFO_OFFSET:
0094         *vals = rot_state->value_offset;
0095         return IIO_VAL_INT;
0096 
0097     case IIO_CHAN_INFO_SAMP_FREQ:
0098         ret_type = hid_sensor_read_samp_freq_value(
0099             &rot_state->common_attributes, &vals[0], &vals[1]);
0100         break;
0101     case IIO_CHAN_INFO_HYSTERESIS:
0102         ret_type = hid_sensor_read_raw_hyst_value(
0103             &rot_state->common_attributes, &vals[0], &vals[1]);
0104         break;
0105     default:
0106         ret_type = -EINVAL;
0107         break;
0108     }
0109 
0110     return ret_type;
0111 }
0112 
0113 /* Channel write_raw handler */
0114 static int dev_rot_write_raw(struct iio_dev *indio_dev,
0115                    struct iio_chan_spec const *chan,
0116                    int val,
0117                    int val2,
0118                    long mask)
0119 {
0120     struct dev_rot_state *rot_state = iio_priv(indio_dev);
0121     int ret;
0122 
0123     switch (mask) {
0124     case IIO_CHAN_INFO_SAMP_FREQ:
0125         ret = hid_sensor_write_samp_freq_value(
0126                 &rot_state->common_attributes, val, val2);
0127         break;
0128     case IIO_CHAN_INFO_HYSTERESIS:
0129         ret = hid_sensor_write_raw_hyst_value(
0130                 &rot_state->common_attributes, val, val2);
0131         break;
0132     default:
0133         ret = -EINVAL;
0134     }
0135 
0136     return ret;
0137 }
0138 
0139 static const struct iio_info dev_rot_info = {
0140     .read_raw_multi = &dev_rot_read_raw,
0141     .write_raw = &dev_rot_write_raw,
0142 };
0143 
0144 /* Callback handler to send event after all samples are received and captured */
0145 static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
0146                 unsigned usage_id,
0147                 void *priv)
0148 {
0149     struct iio_dev *indio_dev = platform_get_drvdata(priv);
0150     struct dev_rot_state *rot_state = iio_priv(indio_dev);
0151 
0152     dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
0153     if (atomic_read(&rot_state->common_attributes.data_ready)) {
0154         if (!rot_state->timestamp)
0155             rot_state->timestamp = iio_get_time_ns(indio_dev);
0156 
0157         iio_push_to_buffers_with_timestamp(indio_dev, &rot_state->scan,
0158                            rot_state->timestamp);
0159 
0160         rot_state->timestamp = 0;
0161     }
0162 
0163     return 0;
0164 }
0165 
0166 /* Capture samples in local storage */
0167 static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
0168                 unsigned usage_id,
0169                 size_t raw_len, char *raw_data,
0170                 void *priv)
0171 {
0172     struct iio_dev *indio_dev = platform_get_drvdata(priv);
0173     struct dev_rot_state *rot_state = iio_priv(indio_dev);
0174 
0175     if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
0176         if (raw_len / 4 == sizeof(s16)) {
0177             rot_state->scan.sampled_vals[0] = ((s16 *)raw_data)[0];
0178             rot_state->scan.sampled_vals[1] = ((s16 *)raw_data)[1];
0179             rot_state->scan.sampled_vals[2] = ((s16 *)raw_data)[2];
0180             rot_state->scan.sampled_vals[3] = ((s16 *)raw_data)[3];
0181         } else {
0182             memcpy(&rot_state->scan.sampled_vals, raw_data,
0183                    sizeof(rot_state->scan.sampled_vals));
0184         }
0185 
0186         dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
0187             sizeof(rot_state->scan.sampled_vals));
0188     } else if (usage_id == HID_USAGE_SENSOR_TIME_TIMESTAMP) {
0189         rot_state->timestamp = hid_sensor_convert_timestamp(&rot_state->common_attributes,
0190                                     *(s64 *)raw_data);
0191     }
0192 
0193     return 0;
0194 }
0195 
0196 /* Parse report which is specific to an usage id*/
0197 static int dev_rot_parse_report(struct platform_device *pdev,
0198                 struct hid_sensor_hub_device *hsdev,
0199                 struct iio_chan_spec *channels,
0200                 unsigned usage_id,
0201                 struct dev_rot_state *st)
0202 {
0203     int ret;
0204 
0205     ret = sensor_hub_input_get_attribute_info(hsdev,
0206                 HID_INPUT_REPORT,
0207                 usage_id,
0208                 HID_USAGE_SENSOR_ORIENT_QUATERNION,
0209                 &st->quaternion);
0210     if (ret)
0211         return ret;
0212 
0213     dev_rot_adjust_channel_bit_mask(&channels[0],
0214         st->quaternion.size / 4);
0215 
0216     dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
0217         st->quaternion.report_id);
0218 
0219     dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
0220                 st->quaternion.size);
0221 
0222     st->scale_precision = hid_sensor_format_scale(
0223                 hsdev->usage,
0224                 &st->quaternion,
0225                 &st->scale_pre_decml, &st->scale_post_decml);
0226 
0227     return 0;
0228 }
0229 
0230 /* Function to initialize the processing for usage id */
0231 static int hid_dev_rot_probe(struct platform_device *pdev)
0232 {
0233     int ret;
0234     char *name;
0235     struct iio_dev *indio_dev;
0236     struct dev_rot_state *rot_state;
0237     struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
0238 
0239     indio_dev = devm_iio_device_alloc(&pdev->dev,
0240                       sizeof(struct dev_rot_state));
0241     if (indio_dev == NULL)
0242         return -ENOMEM;
0243 
0244     platform_set_drvdata(pdev, indio_dev);
0245 
0246     rot_state = iio_priv(indio_dev);
0247     rot_state->common_attributes.hsdev = hsdev;
0248     rot_state->common_attributes.pdev = pdev;
0249 
0250     switch (hsdev->usage) {
0251     case HID_USAGE_SENSOR_DEVICE_ORIENTATION:
0252         name = "dev_rotation";
0253         break;
0254     case HID_USAGE_SENSOR_RELATIVE_ORIENTATION:
0255         name = "relative_orientation";
0256         break;
0257     case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION:
0258         name = "geomagnetic_orientation";
0259         break;
0260     default:
0261         return -EINVAL;
0262     }
0263 
0264     ret = hid_sensor_parse_common_attributes(hsdev,
0265                          hsdev->usage,
0266                          &rot_state->common_attributes,
0267                          rotation_sensitivity_addresses,
0268                          ARRAY_SIZE(rotation_sensitivity_addresses));
0269     if (ret) {
0270         dev_err(&pdev->dev, "failed to setup common attributes\n");
0271         return ret;
0272     }
0273 
0274     indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels,
0275                        sizeof(dev_rot_channels),
0276                        GFP_KERNEL);
0277     if (!indio_dev->channels) {
0278         dev_err(&pdev->dev, "failed to duplicate channels\n");
0279         return -ENOMEM;
0280     }
0281 
0282     ret = dev_rot_parse_report(pdev, hsdev,
0283                    (struct iio_chan_spec *)indio_dev->channels,
0284                     hsdev->usage, rot_state);
0285     if (ret) {
0286         dev_err(&pdev->dev, "failed to setup attributes\n");
0287         return ret;
0288     }
0289 
0290     indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
0291     indio_dev->info = &dev_rot_info;
0292     indio_dev->name = name;
0293     indio_dev->modes = INDIO_DIRECT_MODE;
0294 
0295     atomic_set(&rot_state->common_attributes.data_ready, 0);
0296 
0297     ret = hid_sensor_setup_trigger(indio_dev, name,
0298                     &rot_state->common_attributes);
0299     if (ret) {
0300         dev_err(&pdev->dev, "trigger setup failed\n");
0301         return ret;
0302     }
0303 
0304     ret = iio_device_register(indio_dev);
0305     if (ret) {
0306         dev_err(&pdev->dev, "device register failed\n");
0307         goto error_remove_trigger;
0308     }
0309 
0310     rot_state->callbacks.send_event = dev_rot_proc_event;
0311     rot_state->callbacks.capture_sample = dev_rot_capture_sample;
0312     rot_state->callbacks.pdev = pdev;
0313     ret = sensor_hub_register_callback(hsdev, hsdev->usage,
0314                     &rot_state->callbacks);
0315     if (ret) {
0316         dev_err(&pdev->dev, "callback reg failed\n");
0317         goto error_iio_unreg;
0318     }
0319 
0320     return 0;
0321 
0322 error_iio_unreg:
0323     iio_device_unregister(indio_dev);
0324 error_remove_trigger:
0325     hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
0326     return ret;
0327 }
0328 
0329 /* Function to deinitialize the processing for usage id */
0330 static int hid_dev_rot_remove(struct platform_device *pdev)
0331 {
0332     struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
0333     struct iio_dev *indio_dev = platform_get_drvdata(pdev);
0334     struct dev_rot_state *rot_state = iio_priv(indio_dev);
0335 
0336     sensor_hub_remove_callback(hsdev, hsdev->usage);
0337     iio_device_unregister(indio_dev);
0338     hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
0339 
0340     return 0;
0341 }
0342 
0343 static const struct platform_device_id hid_dev_rot_ids[] = {
0344     {
0345         /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
0346         .name = "HID-SENSOR-20008a",
0347     },
0348     {
0349         /* Relative orientation(AG) sensor */
0350         .name = "HID-SENSOR-20008e",
0351     },
0352     {
0353         /* Geomagnetic orientation(AM) sensor */
0354         .name = "HID-SENSOR-2000c1",
0355     },
0356     { /* sentinel */ }
0357 };
0358 MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
0359 
0360 static struct platform_driver hid_dev_rot_platform_driver = {
0361     .id_table = hid_dev_rot_ids,
0362     .driver = {
0363         .name   = KBUILD_MODNAME,
0364         .pm     = &hid_sensor_pm_ops,
0365     },
0366     .probe      = hid_dev_rot_probe,
0367     .remove     = hid_dev_rot_remove,
0368 };
0369 module_platform_driver(hid_dev_rot_platform_driver);
0370 
0371 MODULE_DESCRIPTION("HID Sensor Device Rotation");
0372 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
0373 MODULE_LICENSE("GPL");
0374 MODULE_IMPORT_NS(IIO_HID);