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 #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 #define CHANNEL_SCAN_INDEX_PRESENCE 0
0017 
0018 struct prox_state {
0019     struct hid_sensor_hub_callbacks callbacks;
0020     struct hid_sensor_common common_attributes;
0021     struct hid_sensor_hub_attribute_info prox_attr;
0022     u32 human_presence;
0023     int scale_pre_decml;
0024     int scale_post_decml;
0025     int scale_precision;
0026 };
0027 
0028 static const u32 prox_sensitivity_addresses[] = {
0029     HID_USAGE_SENSOR_HUMAN_PRESENCE,
0030     HID_USAGE_SENSOR_DATA_PRESENCE,
0031 };
0032 
0033 /* Channel definitions */
0034 static const struct iio_chan_spec prox_channels[] = {
0035     {
0036         .type = IIO_PROXIMITY,
0037         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
0038         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
0039         BIT(IIO_CHAN_INFO_SCALE) |
0040         BIT(IIO_CHAN_INFO_SAMP_FREQ) |
0041         BIT(IIO_CHAN_INFO_HYSTERESIS),
0042         .scan_index = CHANNEL_SCAN_INDEX_PRESENCE,
0043     }
0044 };
0045 
0046 /* Adjust channel real bits based on report descriptor */
0047 static void prox_adjust_channel_bit_mask(struct iio_chan_spec *channels,
0048                     int channel, int size)
0049 {
0050     channels[channel].scan_type.sign = 's';
0051     /* Real storage bits will change based on the report desc. */
0052     channels[channel].scan_type.realbits = size * 8;
0053     /* Maximum size of a sample to capture is u32 */
0054     channels[channel].scan_type.storagebits = sizeof(u32) * 8;
0055 }
0056 
0057 /* Channel read_raw handler */
0058 static int prox_read_raw(struct iio_dev *indio_dev,
0059                   struct iio_chan_spec const *chan,
0060                   int *val, int *val2,
0061                   long mask)
0062 {
0063     struct prox_state *prox_state = iio_priv(indio_dev);
0064     int report_id = -1;
0065     u32 address;
0066     int ret_type;
0067     s32 min;
0068 
0069     *val = 0;
0070     *val2 = 0;
0071     switch (mask) {
0072     case IIO_CHAN_INFO_RAW:
0073         switch (chan->scan_index) {
0074         case  CHANNEL_SCAN_INDEX_PRESENCE:
0075             report_id = prox_state->prox_attr.report_id;
0076             min = prox_state->prox_attr.logical_minimum;
0077             address = HID_USAGE_SENSOR_HUMAN_PRESENCE;
0078             break;
0079         default:
0080             report_id = -1;
0081             break;
0082         }
0083         if (report_id >= 0) {
0084             hid_sensor_power_state(&prox_state->common_attributes,
0085                         true);
0086             *val = sensor_hub_input_attr_get_raw_value(
0087                 prox_state->common_attributes.hsdev,
0088                 HID_USAGE_SENSOR_PROX, address,
0089                 report_id,
0090                 SENSOR_HUB_SYNC,
0091                 min < 0);
0092             hid_sensor_power_state(&prox_state->common_attributes,
0093                         false);
0094         } else {
0095             *val = 0;
0096             return -EINVAL;
0097         }
0098         ret_type = IIO_VAL_INT;
0099         break;
0100     case IIO_CHAN_INFO_SCALE:
0101         *val = prox_state->scale_pre_decml;
0102         *val2 = prox_state->scale_post_decml;
0103         ret_type = prox_state->scale_precision;
0104         break;
0105     case IIO_CHAN_INFO_OFFSET:
0106         *val = hid_sensor_convert_exponent(
0107                 prox_state->prox_attr.unit_expo);
0108         ret_type = IIO_VAL_INT;
0109         break;
0110     case IIO_CHAN_INFO_SAMP_FREQ:
0111         ret_type = hid_sensor_read_samp_freq_value(
0112                 &prox_state->common_attributes, val, val2);
0113         break;
0114     case IIO_CHAN_INFO_HYSTERESIS:
0115         ret_type = hid_sensor_read_raw_hyst_value(
0116                 &prox_state->common_attributes, val, val2);
0117         break;
0118     default:
0119         ret_type = -EINVAL;
0120         break;
0121     }
0122 
0123     return ret_type;
0124 }
0125 
0126 /* Channel write_raw handler */
0127 static int prox_write_raw(struct iio_dev *indio_dev,
0128                    struct iio_chan_spec const *chan,
0129                    int val,
0130                    int val2,
0131                    long mask)
0132 {
0133     struct prox_state *prox_state = iio_priv(indio_dev);
0134     int ret = 0;
0135 
0136     switch (mask) {
0137     case IIO_CHAN_INFO_SAMP_FREQ:
0138         ret = hid_sensor_write_samp_freq_value(
0139                 &prox_state->common_attributes, val, val2);
0140         break;
0141     case IIO_CHAN_INFO_HYSTERESIS:
0142         ret = hid_sensor_write_raw_hyst_value(
0143                 &prox_state->common_attributes, val, val2);
0144         break;
0145     default:
0146         ret = -EINVAL;
0147     }
0148 
0149     return ret;
0150 }
0151 
0152 static const struct iio_info prox_info = {
0153     .read_raw = &prox_read_raw,
0154     .write_raw = &prox_write_raw,
0155 };
0156 
0157 /* Function to push data to buffer */
0158 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
0159                     int len)
0160 {
0161     dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
0162     iio_push_to_buffers(indio_dev, data);
0163 }
0164 
0165 /* Callback handler to send event after all samples are received and captured */
0166 static int prox_proc_event(struct hid_sensor_hub_device *hsdev,
0167                 unsigned usage_id,
0168                 void *priv)
0169 {
0170     struct iio_dev *indio_dev = platform_get_drvdata(priv);
0171     struct prox_state *prox_state = iio_priv(indio_dev);
0172 
0173     dev_dbg(&indio_dev->dev, "prox_proc_event\n");
0174     if (atomic_read(&prox_state->common_attributes.data_ready))
0175         hid_sensor_push_data(indio_dev,
0176                 &prox_state->human_presence,
0177                 sizeof(prox_state->human_presence));
0178 
0179     return 0;
0180 }
0181 
0182 /* Capture samples in local storage */
0183 static int prox_capture_sample(struct hid_sensor_hub_device *hsdev,
0184                 unsigned usage_id,
0185                 size_t raw_len, char *raw_data,
0186                 void *priv)
0187 {
0188     struct iio_dev *indio_dev = platform_get_drvdata(priv);
0189     struct prox_state *prox_state = iio_priv(indio_dev);
0190     int ret = -EINVAL;
0191 
0192     switch (usage_id) {
0193     case HID_USAGE_SENSOR_HUMAN_PRESENCE:
0194         prox_state->human_presence = *(u32 *)raw_data;
0195         ret = 0;
0196         break;
0197     default:
0198         break;
0199     }
0200 
0201     return ret;
0202 }
0203 
0204 /* Parse report which is specific to an usage id*/
0205 static int prox_parse_report(struct platform_device *pdev,
0206                 struct hid_sensor_hub_device *hsdev,
0207                 struct iio_chan_spec *channels,
0208                 unsigned usage_id,
0209                 struct prox_state *st)
0210 {
0211     int ret;
0212 
0213     ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
0214             usage_id,
0215             HID_USAGE_SENSOR_HUMAN_PRESENCE,
0216             &st->prox_attr);
0217     if (ret < 0)
0218         return ret;
0219     prox_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESENCE,
0220                     st->prox_attr.size);
0221 
0222     dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr.index,
0223             st->prox_attr.report_id);
0224 
0225     return ret;
0226 }
0227 
0228 /* Function to initialize the processing for usage id */
0229 static int hid_prox_probe(struct platform_device *pdev)
0230 {
0231     int ret = 0;
0232     static const char *name = "prox";
0233     struct iio_dev *indio_dev;
0234     struct prox_state *prox_state;
0235     struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
0236 
0237     indio_dev = devm_iio_device_alloc(&pdev->dev,
0238                 sizeof(struct prox_state));
0239     if (!indio_dev)
0240         return -ENOMEM;
0241     platform_set_drvdata(pdev, indio_dev);
0242 
0243     prox_state = iio_priv(indio_dev);
0244     prox_state->common_attributes.hsdev = hsdev;
0245     prox_state->common_attributes.pdev = pdev;
0246 
0247     ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_PROX,
0248                     &prox_state->common_attributes,
0249                     prox_sensitivity_addresses,
0250                     ARRAY_SIZE(prox_sensitivity_addresses));
0251     if (ret) {
0252         dev_err(&pdev->dev, "failed to setup common attributes\n");
0253         return ret;
0254     }
0255 
0256     indio_dev->channels = devm_kmemdup(&pdev->dev, prox_channels,
0257                        sizeof(prox_channels), GFP_KERNEL);
0258     if (!indio_dev->channels) {
0259         dev_err(&pdev->dev, "failed to duplicate channels\n");
0260         return -ENOMEM;
0261     }
0262 
0263     ret = prox_parse_report(pdev, hsdev,
0264                 (struct iio_chan_spec *)indio_dev->channels,
0265                 HID_USAGE_SENSOR_PROX, prox_state);
0266     if (ret) {
0267         dev_err(&pdev->dev, "failed to setup attributes\n");
0268         return ret;
0269     }
0270 
0271     indio_dev->num_channels = ARRAY_SIZE(prox_channels);
0272     indio_dev->info = &prox_info;
0273     indio_dev->name = name;
0274     indio_dev->modes = INDIO_DIRECT_MODE;
0275 
0276     atomic_set(&prox_state->common_attributes.data_ready, 0);
0277 
0278     ret = hid_sensor_setup_trigger(indio_dev, name,
0279                 &prox_state->common_attributes);
0280     if (ret) {
0281         dev_err(&pdev->dev, "trigger setup failed\n");
0282         return ret;
0283     }
0284 
0285     ret = iio_device_register(indio_dev);
0286     if (ret) {
0287         dev_err(&pdev->dev, "device register failed\n");
0288         goto error_remove_trigger;
0289     }
0290 
0291     prox_state->callbacks.send_event = prox_proc_event;
0292     prox_state->callbacks.capture_sample = prox_capture_sample;
0293     prox_state->callbacks.pdev = pdev;
0294     ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PROX,
0295                     &prox_state->callbacks);
0296     if (ret < 0) {
0297         dev_err(&pdev->dev, "callback reg failed\n");
0298         goto error_iio_unreg;
0299     }
0300 
0301     return ret;
0302 
0303 error_iio_unreg:
0304     iio_device_unregister(indio_dev);
0305 error_remove_trigger:
0306     hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
0307     return ret;
0308 }
0309 
0310 /* Function to deinitialize the processing for usage id */
0311 static int hid_prox_remove(struct platform_device *pdev)
0312 {
0313     struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
0314     struct iio_dev *indio_dev = platform_get_drvdata(pdev);
0315     struct prox_state *prox_state = iio_priv(indio_dev);
0316 
0317     sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PROX);
0318     iio_device_unregister(indio_dev);
0319     hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
0320 
0321     return 0;
0322 }
0323 
0324 static const struct platform_device_id hid_prox_ids[] = {
0325     {
0326         /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
0327         .name = "HID-SENSOR-200011",
0328     },
0329     { /* sentinel */ }
0330 };
0331 MODULE_DEVICE_TABLE(platform, hid_prox_ids);
0332 
0333 static struct platform_driver hid_prox_platform_driver = {
0334     .id_table = hid_prox_ids,
0335     .driver = {
0336         .name   = KBUILD_MODNAME,
0337         .pm = &hid_sensor_pm_ops,
0338     },
0339     .probe      = hid_prox_probe,
0340     .remove     = hid_prox_remove,
0341 };
0342 module_platform_driver(hid_prox_platform_driver);
0343 
0344 MODULE_DESCRIPTION("HID Sensor Proximity");
0345 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
0346 MODULE_LICENSE("GPL");
0347 MODULE_IMPORT_NS(IIO_HID);