Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Sensor HUB driver that discovers sensors behind a ChromeOS Embedded
0004  * Controller.
0005  *
0006  * Copyright 2019 Google LLC
0007  */
0008 
0009 #include <linux/init.h>
0010 #include <linux/device.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_data/cros_ec_commands.h>
0013 #include <linux/platform_data/cros_ec_proto.h>
0014 #include <linux/platform_data/cros_ec_sensorhub.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/slab.h>
0017 #include <linux/types.h>
0018 
0019 #define DRV_NAME        "cros-ec-sensorhub"
0020 
0021 static void cros_ec_sensorhub_free_sensor(void *arg)
0022 {
0023     struct platform_device *pdev = arg;
0024 
0025     platform_device_unregister(pdev);
0026 }
0027 
0028 static int cros_ec_sensorhub_allocate_sensor(struct device *parent,
0029                          char *sensor_name,
0030                          int sensor_num)
0031 {
0032     struct cros_ec_sensor_platform sensor_platforms = {
0033         .sensor_num = sensor_num,
0034     };
0035     struct platform_device *pdev;
0036 
0037     pdev = platform_device_register_data(parent, sensor_name,
0038                          PLATFORM_DEVID_AUTO,
0039                          &sensor_platforms,
0040                          sizeof(sensor_platforms));
0041     if (IS_ERR(pdev))
0042         return PTR_ERR(pdev);
0043 
0044     return devm_add_action_or_reset(parent,
0045                     cros_ec_sensorhub_free_sensor,
0046                     pdev);
0047 }
0048 
0049 static int cros_ec_sensorhub_register(struct device *dev,
0050                       struct cros_ec_sensorhub *sensorhub)
0051 {
0052     int sensor_type[MOTIONSENSE_TYPE_MAX] = { 0 };
0053     struct cros_ec_command *msg = sensorhub->msg;
0054     struct cros_ec_dev *ec = sensorhub->ec;
0055     int ret, i;
0056     char *name;
0057 
0058 
0059     msg->version = 1;
0060     msg->insize = sizeof(struct ec_response_motion_sense);
0061     msg->outsize = sizeof(struct ec_params_motion_sense);
0062 
0063     for (i = 0; i < sensorhub->sensor_num; i++) {
0064         sensorhub->params->cmd = MOTIONSENSE_CMD_INFO;
0065         sensorhub->params->info.sensor_num = i;
0066 
0067         ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
0068         if (ret < 0) {
0069             dev_warn(dev, "no info for EC sensor %d : %d/%d\n",
0070                  i, ret, msg->result);
0071             continue;
0072         }
0073 
0074         switch (sensorhub->resp->info.type) {
0075         case MOTIONSENSE_TYPE_ACCEL:
0076             name = "cros-ec-accel";
0077             break;
0078         case MOTIONSENSE_TYPE_BARO:
0079             name = "cros-ec-baro";
0080             break;
0081         case MOTIONSENSE_TYPE_GYRO:
0082             name = "cros-ec-gyro";
0083             break;
0084         case MOTIONSENSE_TYPE_MAG:
0085             name = "cros-ec-mag";
0086             break;
0087         case MOTIONSENSE_TYPE_PROX:
0088             name = "cros-ec-prox";
0089             break;
0090         case MOTIONSENSE_TYPE_LIGHT:
0091             name = "cros-ec-light";
0092             break;
0093         case MOTIONSENSE_TYPE_ACTIVITY:
0094             name = "cros-ec-activity";
0095             break;
0096         default:
0097             dev_warn(dev, "unknown type %d\n",
0098                  sensorhub->resp->info.type);
0099             continue;
0100         }
0101 
0102         ret = cros_ec_sensorhub_allocate_sensor(dev, name, i);
0103         if (ret)
0104             return ret;
0105 
0106         sensor_type[sensorhub->resp->info.type]++;
0107     }
0108 
0109     if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
0110         ec->has_kb_wake_angle = true;
0111 
0112     if (cros_ec_check_features(ec,
0113                    EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
0114         ret = cros_ec_sensorhub_allocate_sensor(dev,
0115                             "cros-ec-lid-angle",
0116                             0);
0117         if (ret)
0118             return ret;
0119     }
0120 
0121     return 0;
0122 }
0123 
0124 static int cros_ec_sensorhub_probe(struct platform_device *pdev)
0125 {
0126     struct device *dev = &pdev->dev;
0127     struct cros_ec_dev *ec = dev_get_drvdata(dev->parent);
0128     struct cros_ec_sensorhub *data;
0129     struct cros_ec_command *msg;
0130     int ret, i, sensor_num;
0131 
0132     msg = devm_kzalloc(dev, sizeof(struct cros_ec_command) +
0133                max((u16)sizeof(struct ec_params_motion_sense),
0134                    ec->ec_dev->max_response), GFP_KERNEL);
0135     if (!msg)
0136         return -ENOMEM;
0137 
0138     msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
0139 
0140     data = devm_kzalloc(dev, sizeof(struct cros_ec_sensorhub), GFP_KERNEL);
0141     if (!data)
0142         return -ENOMEM;
0143 
0144     mutex_init(&data->cmd_lock);
0145 
0146     data->dev = dev;
0147     data->ec = ec;
0148     data->msg = msg;
0149     data->params = (struct ec_params_motion_sense *)msg->data;
0150     data->resp = (struct ec_response_motion_sense *)msg->data;
0151 
0152     dev_set_drvdata(dev, data);
0153 
0154     /* Check whether this EC is a sensor hub. */
0155     if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE)) {
0156         sensor_num = cros_ec_get_sensor_count(ec);
0157         if (sensor_num < 0) {
0158             dev_err(dev,
0159                 "Unable to retrieve sensor information (err:%d)\n",
0160                 sensor_num);
0161             return sensor_num;
0162         }
0163         if (sensor_num == 0) {
0164             dev_err(dev, "Zero sensors reported.\n");
0165             return -EINVAL;
0166         }
0167         data->sensor_num = sensor_num;
0168 
0169         /*
0170          * Prepare the ring handler before enumering the
0171          * sensors.
0172          */
0173         if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
0174             ret = cros_ec_sensorhub_ring_allocate(data);
0175             if (ret)
0176                 return ret;
0177         }
0178 
0179         /* Enumerate the sensors.*/
0180         ret = cros_ec_sensorhub_register(dev, data);
0181         if (ret)
0182             return ret;
0183 
0184         /*
0185          * When the EC does not have a FIFO, the sensors will query
0186          * their data themselves via sysfs or a software trigger.
0187          */
0188         if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
0189             ret = cros_ec_sensorhub_ring_add(data);
0190             if (ret)
0191                 return ret;
0192             /*
0193              * The msg and its data is not under the control of the
0194              * ring handler.
0195              */
0196             return devm_add_action_or_reset(dev,
0197                     cros_ec_sensorhub_ring_remove,
0198                     data);
0199         }
0200 
0201     } else {
0202         /*
0203          * If the device has sensors but does not claim to
0204          * be a sensor hub, we are in legacy mode.
0205          */
0206         data->sensor_num = 2;
0207         for (i = 0; i < data->sensor_num; i++) {
0208             ret = cros_ec_sensorhub_allocate_sensor(dev,
0209                         "cros-ec-accel-legacy", i);
0210             if (ret)
0211                 return ret;
0212         }
0213     }
0214 
0215 
0216     return 0;
0217 }
0218 
0219 #ifdef CONFIG_PM_SLEEP
0220 /*
0221  * When the EC is suspending, we must stop sending interrupt,
0222  * we may use the same interrupt line for waking up the device.
0223  * Tell the EC to stop sending non-interrupt event on the iio ring.
0224  */
0225 static int cros_ec_sensorhub_suspend(struct device *dev)
0226 {
0227     struct cros_ec_sensorhub *sensorhub = dev_get_drvdata(dev);
0228     struct cros_ec_dev *ec = sensorhub->ec;
0229 
0230     if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO))
0231         return cros_ec_sensorhub_ring_fifo_enable(sensorhub, false);
0232     return 0;
0233 }
0234 
0235 static int cros_ec_sensorhub_resume(struct device *dev)
0236 {
0237     struct cros_ec_sensorhub *sensorhub = dev_get_drvdata(dev);
0238     struct cros_ec_dev *ec = sensorhub->ec;
0239 
0240     if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO))
0241         return cros_ec_sensorhub_ring_fifo_enable(sensorhub, true);
0242     return 0;
0243 }
0244 #endif
0245 
0246 static SIMPLE_DEV_PM_OPS(cros_ec_sensorhub_pm_ops,
0247         cros_ec_sensorhub_suspend,
0248         cros_ec_sensorhub_resume);
0249 
0250 static struct platform_driver cros_ec_sensorhub_driver = {
0251     .driver = {
0252         .name = DRV_NAME,
0253         .pm = &cros_ec_sensorhub_pm_ops,
0254     },
0255     .probe = cros_ec_sensorhub_probe,
0256 };
0257 
0258 module_platform_driver(cros_ec_sensorhub_driver);
0259 
0260 MODULE_ALIAS("platform:" DRV_NAME);
0261 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
0262 MODULE_DESCRIPTION("ChromeOS EC MEMS Sensor Hub Driver");
0263 MODULE_LICENSE("GPL");