Back to home page

OSCL-LXR

 
 

    


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