blob: c079b896008241e432cc9e70611a8d949722902b [file] [log] [blame]
Enric Balletbo i Serra37aa0552019-03-13 12:41:20 +01001// SPDX-License-Identifier: GPL-2.0
Gwendal Grignoud7322482017-01-24 14:41:41 +01002/*
3 * cros_ec_baro - Driver for barometer sensor behind CrosEC.
4 *
5 * Copyright (C) 2017 Google, Inc
Gwendal Grignoud7322482017-01-24 14:41:41 +01006 */
7
Gwendal Grignoud7322482017-01-24 14:41:41 +01008#include <linux/device.h>
9#include <linux/iio/buffer.h>
Gwendal Grignou5a0b8cb2018-03-13 14:23:28 -070010#include <linux/iio/common/cros_ec_sensors_core.h>
Gwendal Grignoud7322482017-01-24 14:41:41 +010011#include <linux/iio/iio.h>
12#include <linux/iio/kfifo_buf.h>
13#include <linux/iio/trigger.h>
14#include <linux/iio/triggered_buffer.h>
15#include <linux/iio/trigger_consumer.h>
16#include <linux/kernel.h>
Gwendal Grignoud7322482017-01-24 14:41:41 +010017#include <linux/module.h>
18#include <linux/slab.h>
Enric Balletbo i Serra840d9f12019-09-02 11:53:05 +020019#include <linux/platform_data/cros_ec_commands.h>
20#include <linux/platform_data/cros_ec_proto.h>
Gwendal Grignoud7322482017-01-24 14:41:41 +010021#include <linux/platform_device.h>
22
Gwendal Grignoud7322482017-01-24 14:41:41 +010023/*
24 * One channel for pressure, the other for timestamp.
25 */
26#define CROS_EC_BARO_MAX_CHANNELS (1 + 1)
27
28/* State data for ec_sensors iio driver. */
29struct cros_ec_baro_state {
30 /* Shared by all sensors */
31 struct cros_ec_sensors_core_state core;
32
33 struct iio_chan_spec channels[CROS_EC_BARO_MAX_CHANNELS];
34};
35
36static int cros_ec_baro_read(struct iio_dev *indio_dev,
37 struct iio_chan_spec const *chan,
38 int *val, int *val2, long mask)
39{
40 struct cros_ec_baro_state *st = iio_priv(indio_dev);
41 u16 data = 0;
Gwendal Grignouf53199c2019-07-18 15:22:37 -070042 int ret;
Gwendal Grignoud7322482017-01-24 14:41:41 +010043 int idx = chan->scan_index;
44
45 mutex_lock(&st->core.cmd_lock);
46
47 switch (mask) {
48 case IIO_CHAN_INFO_RAW:
Gwendal Grignouf53199c2019-07-18 15:22:37 -070049 ret = cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
50 (s16 *)&data);
51 if (ret)
52 break;
53
Gwendal Grignoud7322482017-01-24 14:41:41 +010054 *val = data;
Gwendal Grignouf53199c2019-07-18 15:22:37 -070055 ret = IIO_VAL_INT;
Gwendal Grignoud7322482017-01-24 14:41:41 +010056 break;
57 case IIO_CHAN_INFO_SCALE:
58 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
59 st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE;
60
Gwendal Grignouf53199c2019-07-18 15:22:37 -070061 ret = cros_ec_motion_send_host_cmd(&st->core, 0);
62 if (ret)
Gwendal Grignoud7322482017-01-24 14:41:41 +010063 break;
Gwendal Grignouf53199c2019-07-18 15:22:37 -070064
Gwendal Grignoud7322482017-01-24 14:41:41 +010065 *val = st->core.resp->sensor_range.ret;
66
67 /* scale * in_pressure_raw --> kPa */
68 *val2 = 10 << CROS_EC_SENSOR_BITS;
69 ret = IIO_VAL_FRACTIONAL;
70 break;
71 default:
72 ret = cros_ec_sensors_core_read(&st->core, chan, val, val2,
73 mask);
74 break;
75 }
76
77 mutex_unlock(&st->core.cmd_lock);
78
79 return ret;
80}
81
82static int cros_ec_baro_write(struct iio_dev *indio_dev,
83 struct iio_chan_spec const *chan,
84 int val, int val2, long mask)
85{
86 struct cros_ec_baro_state *st = iio_priv(indio_dev);
87 int ret = 0;
88
89 mutex_lock(&st->core.cmd_lock);
90
91 switch (mask) {
92 case IIO_CHAN_INFO_SCALE:
93 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
94 st->core.param.sensor_range.data = val;
95
96 /* Always roundup, so caller gets at least what it asks for. */
97 st->core.param.sensor_range.roundup = 1;
98
99 if (cros_ec_motion_send_host_cmd(&st->core, 0))
100 ret = -EIO;
101 break;
102 default:
103 ret = cros_ec_sensors_core_write(&st->core, chan, val, val2,
104 mask);
105 break;
106 }
107
108 mutex_unlock(&st->core.cmd_lock);
109
110 return ret;
111}
112
113static const struct iio_info cros_ec_baro_info = {
114 .read_raw = &cros_ec_baro_read,
115 .write_raw = &cros_ec_baro_write,
Gwendal Grignoue9a4cbc2019-11-06 09:55:33 -0800116 .read_avail = &cros_ec_sensors_core_read_avail,
Gwendal Grignoud7322482017-01-24 14:41:41 +0100117};
118
119static int cros_ec_baro_probe(struct platform_device *pdev)
120{
121 struct device *dev = &pdev->dev;
122 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
Gwendal Grignoud7322482017-01-24 14:41:41 +0100123 struct iio_dev *indio_dev;
124 struct cros_ec_baro_state *state;
125 struct iio_chan_spec *channel;
126 int ret;
127
128 if (!ec_dev || !ec_dev->ec_dev) {
129 dev_warn(dev, "No CROS EC device found.\n");
130 return -EINVAL;
131 }
Gwendal Grignoud7322482017-01-24 14:41:41 +0100132
133 indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
134 if (!indio_dev)
135 return -ENOMEM;
136
Gwendal Grignouaa984f12020-03-27 15:34:38 -0700137 ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
138 cros_ec_sensors_capture,
139 cros_ec_sensors_push_data);
Gwendal Grignoud7322482017-01-24 14:41:41 +0100140 if (ret)
141 return ret;
142
Gwendal Grignou65627932020-03-27 15:34:40 -0700143 iio_buffer_set_attrs(indio_dev->buffer, cros_ec_sensor_fifo_attributes);
144
Gwendal Grignoud7322482017-01-24 14:41:41 +0100145 indio_dev->info = &cros_ec_baro_info;
146 state = iio_priv(indio_dev);
147 state->core.type = state->core.resp->info.type;
148 state->core.loc = state->core.resp->info.location;
149 channel = state->channels;
150 /* Common part */
151 channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
152 channel->info_mask_shared_by_all =
153 BIT(IIO_CHAN_INFO_SCALE) |
Gwendal Grignou65627932020-03-27 15:34:40 -0700154 BIT(IIO_CHAN_INFO_SAMP_FREQ);
Gwendal Grignoue9a4cbc2019-11-06 09:55:33 -0800155 channel->info_mask_shared_by_all_available =
156 BIT(IIO_CHAN_INFO_SAMP_FREQ);
Gwendal Grignoud7322482017-01-24 14:41:41 +0100157 channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
158 channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
159 channel->scan_type.shift = 0;
160 channel->scan_index = 0;
161 channel->ext_info = cros_ec_sensors_ext_info;
162 channel->scan_type.sign = 'u';
163
Gwendal Grignoud7322482017-01-24 14:41:41 +0100164 /* Sensor specific */
165 switch (state->core.type) {
166 case MOTIONSENSE_TYPE_BARO:
167 channel->type = IIO_PRESSURE;
168 break;
169 default:
170 dev_warn(dev, "Unknown motion sensor\n");
171 return -EINVAL;
172 }
173
174 /* Timestamp */
175 channel++;
176 channel->type = IIO_TIMESTAMP;
177 channel->channel = -1;
178 channel->scan_index = 1;
179 channel->scan_type.sign = 's';
180 channel->scan_type.realbits = 64;
181 channel->scan_type.storagebits = 64;
182
183 indio_dev->channels = state->channels;
184 indio_dev->num_channels = CROS_EC_BARO_MAX_CHANNELS;
185
186 state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
187
Gwendal Grignoud7322482017-01-24 14:41:41 +0100188 return devm_iio_device_register(dev, indio_dev);
189}
190
191static const struct platform_device_id cros_ec_baro_ids[] = {
192 {
193 .name = "cros-ec-baro",
194 },
195 { /* sentinel */ }
196};
197MODULE_DEVICE_TABLE(platform, cros_ec_baro_ids);
198
199static struct platform_driver cros_ec_baro_platform_driver = {
200 .driver = {
201 .name = "cros-ec-baro",
202 },
203 .probe = cros_ec_baro_probe,
204 .id_table = cros_ec_baro_ids,
205};
206module_platform_driver(cros_ec_baro_platform_driver);
207
208MODULE_DESCRIPTION("ChromeOS EC barometer sensor driver");
209MODULE_LICENSE("GPL v2");