Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /*
0004  * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor.
0005  *
0006  * Copyright 2018 Google, Inc
0007  *
0008  * This driver uses the cros-ec interface to communicate with the Chrome OS
0009  * EC about counter sensors. Counters are presented through
0010  * iio sysfs.
0011  */
0012 
0013 #include <linux/delay.h>
0014 #include <linux/device.h>
0015 #include <linux/iio/buffer.h>
0016 #include <linux/iio/common/cros_ec_sensors_core.h>
0017 #include <linux/iio/iio.h>
0018 #include <linux/iio/kfifo_buf.h>
0019 #include <linux/iio/trigger.h>
0020 #include <linux/iio/triggered_buffer.h>
0021 #include <linux/iio/trigger_consumer.h>
0022 #include <linux/kernel.h>
0023 #include <linux/mod_devicetable.h>
0024 #include <linux/module.h>
0025 #include <linux/platform_data/cros_ec_commands.h>
0026 #include <linux/platform_device.h>
0027 #include <linux/slab.h>
0028 
0029 #define DRV_NAME "cros-ec-lid-angle"
0030 
0031 /*
0032  * One channel for the lid angle, the other for timestamp.
0033  */
0034 static const struct iio_chan_spec cros_ec_lid_angle_channels[] = {
0035     {
0036         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
0037         .scan_type.realbits = CROS_EC_SENSOR_BITS,
0038         .scan_type.storagebits = CROS_EC_SENSOR_BITS,
0039         .scan_type.sign = 'u',
0040         .type = IIO_ANGL
0041     },
0042     IIO_CHAN_SOFT_TIMESTAMP(1)
0043 };
0044 
0045 /* State data for ec_sensors iio driver. */
0046 struct cros_ec_lid_angle_state {
0047     /* Shared by all sensors */
0048     struct cros_ec_sensors_core_state core;
0049 };
0050 
0051 static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev,
0052                       unsigned long scan_mask, s16 *data)
0053 {
0054     struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
0055     int ret;
0056 
0057     st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE;
0058     ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle));
0059     if (ret) {
0060         dev_warn(&indio_dev->dev, "Unable to read lid angle\n");
0061         return ret;
0062     }
0063 
0064     *data = st->resp->lid_angle.value;
0065     return 0;
0066 }
0067 
0068 static int cros_ec_lid_angle_read(struct iio_dev *indio_dev,
0069                     struct iio_chan_spec const *chan,
0070                     int *val, int *val2, long mask)
0071 {
0072     struct cros_ec_lid_angle_state *st = iio_priv(indio_dev);
0073     s16 data;
0074     int ret;
0075 
0076     mutex_lock(&st->core.cmd_lock);
0077     ret = cros_ec_sensors_read_lid_angle(indio_dev, 1, &data);
0078     if (ret == 0) {
0079         *val = data;
0080         ret = IIO_VAL_INT;
0081     }
0082     mutex_unlock(&st->core.cmd_lock);
0083     return ret;
0084 }
0085 
0086 static const struct iio_info cros_ec_lid_angle_info = {
0087     .read_raw = &cros_ec_lid_angle_read,
0088 };
0089 
0090 static int cros_ec_lid_angle_probe(struct platform_device *pdev)
0091 {
0092     struct device *dev = &pdev->dev;
0093     struct iio_dev *indio_dev;
0094     struct cros_ec_lid_angle_state *state;
0095     int ret;
0096 
0097     indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
0098     if (!indio_dev)
0099         return -ENOMEM;
0100 
0101     ret = cros_ec_sensors_core_init(pdev, indio_dev, false, NULL);
0102     if (ret)
0103         return ret;
0104 
0105     indio_dev->info = &cros_ec_lid_angle_info;
0106     state = iio_priv(indio_dev);
0107     indio_dev->channels = cros_ec_lid_angle_channels;
0108     indio_dev->num_channels = ARRAY_SIZE(cros_ec_lid_angle_channels);
0109 
0110     state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle;
0111 
0112     ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
0113             cros_ec_sensors_capture, NULL);
0114     if (ret)
0115         return ret;
0116 
0117     return cros_ec_sensors_core_register(dev, indio_dev, NULL);
0118 }
0119 
0120 static const struct platform_device_id cros_ec_lid_angle_ids[] = {
0121     {
0122         .name = DRV_NAME,
0123     },
0124     { /* sentinel */ }
0125 };
0126 MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids);
0127 
0128 static struct platform_driver cros_ec_lid_angle_platform_driver = {
0129     .driver = {
0130         .name   = DRV_NAME,
0131     },
0132     .probe      = cros_ec_lid_angle_probe,
0133     .id_table   = cros_ec_lid_angle_ids,
0134 };
0135 module_platform_driver(cros_ec_lid_angle_platform_driver);
0136 
0137 MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle.");
0138 MODULE_LICENSE("GPL v2");