1 /*
2 * cros_ec_baro - Driver for barometer sensor behind CrosEC.
3 *
4 * Copyright (C) 2017 Google, Inc
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/common/cros_ec_sensors_core.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/kfifo_buf.h>
22 #include <linux/iio/trigger.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/kernel.h>
26 #include <linux/mfd/cros_ec.h>
27 #include <linux/mfd/cros_ec_commands.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/platform_device.h>
31
32 /*
33 * One channel for pressure, the other for timestamp.
34 */
35 #define CROS_EC_BARO_MAX_CHANNELS (1 + 1)
36
37 /* State data for ec_sensors iio driver. */
38 struct cros_ec_baro_state {
39 /* Shared by all sensors */
40 struct cros_ec_sensors_core_state core;
41
42 struct iio_chan_spec channels[CROS_EC_BARO_MAX_CHANNELS];
43 };
44
cros_ec_baro_read(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)45 static int cros_ec_baro_read(struct iio_dev *indio_dev,
46 struct iio_chan_spec const *chan,
47 int *val, int *val2, long mask)
48 {
49 struct cros_ec_baro_state *st = iio_priv(indio_dev);
50 u16 data = 0;
51 int ret = IIO_VAL_INT;
52 int idx = chan->scan_index;
53
54 mutex_lock(&st->core.cmd_lock);
55
56 switch (mask) {
57 case IIO_CHAN_INFO_RAW:
58 if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
59 (s16 *)&data) < 0)
60 ret = -EIO;
61 *val = data;
62 break;
63 case IIO_CHAN_INFO_SCALE:
64 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
65 st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE;
66
67 if (cros_ec_motion_send_host_cmd(&st->core, 0)) {
68 ret = -EIO;
69 break;
70 }
71 *val = st->core.resp->sensor_range.ret;
72
73 /* scale * in_pressure_raw --> kPa */
74 *val2 = 10 << CROS_EC_SENSOR_BITS;
75 ret = IIO_VAL_FRACTIONAL;
76 break;
77 default:
78 ret = cros_ec_sensors_core_read(&st->core, chan, val, val2,
79 mask);
80 break;
81 }
82
83 mutex_unlock(&st->core.cmd_lock);
84
85 return ret;
86 }
87
cros_ec_baro_write(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)88 static int cros_ec_baro_write(struct iio_dev *indio_dev,
89 struct iio_chan_spec const *chan,
90 int val, int val2, long mask)
91 {
92 struct cros_ec_baro_state *st = iio_priv(indio_dev);
93 int ret = 0;
94
95 mutex_lock(&st->core.cmd_lock);
96
97 switch (mask) {
98 case IIO_CHAN_INFO_SCALE:
99 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
100 st->core.param.sensor_range.data = val;
101
102 /* Always roundup, so caller gets at least what it asks for. */
103 st->core.param.sensor_range.roundup = 1;
104
105 if (cros_ec_motion_send_host_cmd(&st->core, 0))
106 ret = -EIO;
107 break;
108 default:
109 ret = cros_ec_sensors_core_write(&st->core, chan, val, val2,
110 mask);
111 break;
112 }
113
114 mutex_unlock(&st->core.cmd_lock);
115
116 return ret;
117 }
118
119 static const struct iio_info cros_ec_baro_info = {
120 .read_raw = &cros_ec_baro_read,
121 .write_raw = &cros_ec_baro_write,
122 };
123
cros_ec_baro_probe(struct platform_device * pdev)124 static int cros_ec_baro_probe(struct platform_device *pdev)
125 {
126 struct device *dev = &pdev->dev;
127 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
128 struct iio_dev *indio_dev;
129 struct cros_ec_baro_state *state;
130 struct iio_chan_spec *channel;
131 int ret;
132
133 if (!ec_dev || !ec_dev->ec_dev) {
134 dev_warn(dev, "No CROS EC device found.\n");
135 return -EINVAL;
136 }
137
138 indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
139 if (!indio_dev)
140 return -ENOMEM;
141
142 ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
143 if (ret)
144 return ret;
145
146 indio_dev->info = &cros_ec_baro_info;
147 state = iio_priv(indio_dev);
148 state->core.type = state->core.resp->info.type;
149 state->core.loc = state->core.resp->info.location;
150 channel = state->channels;
151 /* Common part */
152 channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
153 channel->info_mask_shared_by_all =
154 BIT(IIO_CHAN_INFO_SCALE) |
155 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
156 BIT(IIO_CHAN_INFO_FREQUENCY);
157 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
164 state->core.calib[0] = 0;
165
166 /* Sensor specific */
167 switch (state->core.type) {
168 case MOTIONSENSE_TYPE_BARO:
169 channel->type = IIO_PRESSURE;
170 break;
171 default:
172 dev_warn(dev, "Unknown motion sensor\n");
173 return -EINVAL;
174 }
175
176 /* Timestamp */
177 channel++;
178 channel->type = IIO_TIMESTAMP;
179 channel->channel = -1;
180 channel->scan_index = 1;
181 channel->scan_type.sign = 's';
182 channel->scan_type.realbits = 64;
183 channel->scan_type.storagebits = 64;
184
185 indio_dev->channels = state->channels;
186 indio_dev->num_channels = CROS_EC_BARO_MAX_CHANNELS;
187
188 state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
189
190 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
191 cros_ec_sensors_capture, NULL);
192 if (ret)
193 return ret;
194
195 return devm_iio_device_register(dev, indio_dev);
196 }
197
198 static const struct platform_device_id cros_ec_baro_ids[] = {
199 {
200 .name = "cros-ec-baro",
201 },
202 { /* sentinel */ }
203 };
204 MODULE_DEVICE_TABLE(platform, cros_ec_baro_ids);
205
206 static struct platform_driver cros_ec_baro_platform_driver = {
207 .driver = {
208 .name = "cros-ec-baro",
209 },
210 .probe = cros_ec_baro_probe,
211 .id_table = cros_ec_baro_ids,
212 };
213 module_platform_driver(cros_ec_baro_platform_driver);
214
215 MODULE_DESCRIPTION("ChromeOS EC barometer sensor driver");
216 MODULE_LICENSE("GPL v2");
217