1 // SPDX-License-Identifier: GPL-2.0 2 3 /* 4 * cros_ec_lid_angle - Driver for CrOS EC lid angle sensor. 5 * 6 * Copyright 2018 Google, Inc 7 * 8 * This driver uses the cros-ec interface to communicate with the Chrome OS 9 * EC about counter sensors. Counters are presented through 10 * iio sysfs. 11 */ 12 13 #include <linux/delay.h> 14 #include <linux/device.h> 15 #include <linux/iio/buffer.h> 16 #include <linux/iio/common/cros_ec_sensors_core.h> 17 #include <linux/iio/iio.h> 18 #include <linux/iio/kfifo_buf.h> 19 #include <linux/iio/trigger.h> 20 #include <linux/iio/triggered_buffer.h> 21 #include <linux/iio/trigger_consumer.h> 22 #include <linux/kernel.h> 23 #include <linux/mod_devicetable.h> 24 #include <linux/module.h> 25 #include <linux/platform_data/cros_ec_commands.h> 26 #include <linux/platform_device.h> 27 #include <linux/slab.h> 28 29 #define DRV_NAME "cros-ec-lid-angle" 30 31 /* 32 * One channel for the lid angle, the other for timestamp. 33 */ 34 static const struct iio_chan_spec cros_ec_lid_angle_channels[] = { 35 { 36 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 37 .scan_type.realbits = CROS_EC_SENSOR_BITS, 38 .scan_type.storagebits = CROS_EC_SENSOR_BITS, 39 .scan_type.sign = 'u', 40 .type = IIO_ANGL 41 }, 42 IIO_CHAN_SOFT_TIMESTAMP(1) 43 }; 44 45 /* State data for ec_sensors iio driver. */ 46 struct cros_ec_lid_angle_state { 47 /* Shared by all sensors */ 48 struct cros_ec_sensors_core_state core; 49 }; 50 51 static int cros_ec_sensors_read_lid_angle(struct iio_dev *indio_dev, 52 unsigned long scan_mask, s16 *data) 53 { 54 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 55 int ret; 56 57 st->param.cmd = MOTIONSENSE_CMD_LID_ANGLE; 58 ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->lid_angle)); 59 if (ret) { 60 dev_warn(&indio_dev->dev, "Unable to read lid angle\n"); 61 return ret; 62 } 63 64 *data = st->resp->lid_angle.value; 65 return 0; 66 } 67 68 static int cros_ec_lid_angle_read(struct iio_dev *indio_dev, 69 struct iio_chan_spec const *chan, 70 int *val, int *val2, long mask) 71 { 72 struct cros_ec_lid_angle_state *st = iio_priv(indio_dev); 73 s16 data; 74 int ret; 75 76 mutex_lock(&st->core.cmd_lock); 77 ret = cros_ec_sensors_read_lid_angle(indio_dev, 1, &data); 78 if (ret == 0) { 79 *val = data; 80 ret = IIO_VAL_INT; 81 } 82 mutex_unlock(&st->core.cmd_lock); 83 return ret; 84 } 85 86 static const struct iio_info cros_ec_lid_angle_info = { 87 .read_raw = &cros_ec_lid_angle_read, 88 }; 89 90 static int cros_ec_lid_angle_probe(struct platform_device *pdev) 91 { 92 struct device *dev = &pdev->dev; 93 struct iio_dev *indio_dev; 94 struct cros_ec_lid_angle_state *state; 95 int ret; 96 97 indio_dev = devm_iio_device_alloc(dev, sizeof(*state)); 98 if (!indio_dev) 99 return -ENOMEM; 100 101 ret = cros_ec_sensors_core_init(pdev, indio_dev, false, NULL); 102 if (ret) 103 return ret; 104 105 indio_dev->info = &cros_ec_lid_angle_info; 106 state = iio_priv(indio_dev); 107 indio_dev->channels = cros_ec_lid_angle_channels; 108 indio_dev->num_channels = ARRAY_SIZE(cros_ec_lid_angle_channels); 109 110 state->core.read_ec_sensors_data = cros_ec_sensors_read_lid_angle; 111 112 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, 113 cros_ec_sensors_capture, NULL); 114 if (ret) 115 return ret; 116 117 return cros_ec_sensors_core_register(dev, indio_dev, NULL); 118 } 119 120 static const struct platform_device_id cros_ec_lid_angle_ids[] = { 121 { 122 .name = DRV_NAME, 123 }, 124 { /* sentinel */ } 125 }; 126 MODULE_DEVICE_TABLE(platform, cros_ec_lid_angle_ids); 127 128 static struct platform_driver cros_ec_lid_angle_platform_driver = { 129 .driver = { 130 .name = DRV_NAME, 131 }, 132 .probe = cros_ec_lid_angle_probe, 133 .id_table = cros_ec_lid_angle_ids, 134 }; 135 module_platform_driver(cros_ec_lid_angle_platform_driver); 136 137 MODULE_DESCRIPTION("ChromeOS EC driver for reporting convertible lid angle."); 138 MODULE_LICENSE("GPL v2"); 139