1 // SPDX-License-Identifier: GPL-2.0
3 * cros_ec_baro - Driver for barometer sensor behind CrosEC.
5 * Copyright (C) 2017 Google, Inc
8 #include <linux/device.h>
9 #include <linux/iio/buffer.h>
10 #include <linux/iio/common/cros_ec_sensors_core.h>
11 #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>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/platform_data/cros_ec_commands.h>
21 #include <linux/platform_data/cros_ec_proto.h>
22 #include <linux/platform_device.h>
25 * One channel for pressure, the other for timestamp.
27 #define CROS_EC_BARO_MAX_CHANNELS (1 + 1)
29 /* State data for ec_sensors iio driver. */
30 struct cros_ec_baro_state
{
31 /* Shared by all sensors */
32 struct cros_ec_sensors_core_state core
;
34 struct iio_chan_spec channels
[CROS_EC_BARO_MAX_CHANNELS
];
37 static int cros_ec_baro_read(struct iio_dev
*indio_dev
,
38 struct iio_chan_spec
const *chan
,
39 int *val
, int *val2
, long mask
)
41 struct cros_ec_baro_state
*st
= iio_priv(indio_dev
);
44 int idx
= chan
->scan_index
;
46 mutex_lock(&st
->core
.cmd_lock
);
49 case IIO_CHAN_INFO_RAW
:
50 ret
= cros_ec_sensors_read_cmd(indio_dev
, 1 << idx
,
58 case IIO_CHAN_INFO_SCALE
:
59 st
->core
.param
.cmd
= MOTIONSENSE_CMD_SENSOR_RANGE
;
60 st
->core
.param
.sensor_range
.data
= EC_MOTION_SENSE_NO_VALUE
;
62 ret
= cros_ec_motion_send_host_cmd(&st
->core
, 0);
66 *val
= st
->core
.resp
->sensor_range
.ret
;
68 /* scale * in_pressure_raw --> kPa */
69 *val2
= 10 << CROS_EC_SENSOR_BITS
;
70 ret
= IIO_VAL_FRACTIONAL
;
73 ret
= cros_ec_sensors_core_read(&st
->core
, chan
, val
, val2
,
78 mutex_unlock(&st
->core
.cmd_lock
);
83 static int cros_ec_baro_write(struct iio_dev
*indio_dev
,
84 struct iio_chan_spec
const *chan
,
85 int val
, int val2
, long mask
)
87 struct cros_ec_baro_state
*st
= iio_priv(indio_dev
);
90 mutex_lock(&st
->core
.cmd_lock
);
93 case IIO_CHAN_INFO_SCALE
:
94 st
->core
.param
.cmd
= MOTIONSENSE_CMD_SENSOR_RANGE
;
95 st
->core
.param
.sensor_range
.data
= val
;
97 /* Always roundup, so caller gets at least what it asks for. */
98 st
->core
.param
.sensor_range
.roundup
= 1;
100 ret
= cros_ec_motion_send_host_cmd(&st
->core
, 0);
102 st
->core
.range_updated
= true;
103 st
->core
.curr_range
= val
;
107 ret
= cros_ec_sensors_core_write(&st
->core
, chan
, val
, val2
,
112 mutex_unlock(&st
->core
.cmd_lock
);
117 static const struct iio_info cros_ec_baro_info
= {
118 .read_raw
= &cros_ec_baro_read
,
119 .write_raw
= &cros_ec_baro_write
,
120 .read_avail
= &cros_ec_sensors_core_read_avail
,
123 static int cros_ec_baro_probe(struct platform_device
*pdev
)
125 struct device
*dev
= &pdev
->dev
;
126 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(dev
->parent
);
127 struct iio_dev
*indio_dev
;
128 struct cros_ec_baro_state
*state
;
129 struct iio_chan_spec
*channel
;
132 if (!ec_dev
|| !ec_dev
->ec_dev
) {
133 dev_warn(dev
, "No CROS EC device found.\n");
137 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*state
));
141 ret
= cros_ec_sensors_core_init(pdev
, indio_dev
, true,
142 cros_ec_sensors_capture
);
146 indio_dev
->info
= &cros_ec_baro_info
;
147 state
= iio_priv(indio_dev
);
148 channel
= state
->channels
;
150 channel
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
151 channel
->info_mask_shared_by_all
=
152 BIT(IIO_CHAN_INFO_SCALE
) |
153 BIT(IIO_CHAN_INFO_SAMP_FREQ
);
154 channel
->info_mask_shared_by_all_available
=
155 BIT(IIO_CHAN_INFO_SAMP_FREQ
);
156 channel
->scan_type
.realbits
= CROS_EC_SENSOR_BITS
;
157 channel
->scan_type
.storagebits
= CROS_EC_SENSOR_BITS
;
158 channel
->scan_type
.shift
= 0;
159 channel
->scan_index
= 0;
160 channel
->ext_info
= cros_ec_sensors_ext_info
;
161 channel
->scan_type
.sign
= 'u';
163 /* Sensor specific */
164 switch (state
->core
.type
) {
165 case MOTIONSENSE_TYPE_BARO
:
166 channel
->type
= IIO_PRESSURE
;
169 dev_warn(dev
, "Unknown motion sensor\n");
175 channel
->type
= IIO_TIMESTAMP
;
176 channel
->channel
= -1;
177 channel
->scan_index
= 1;
178 channel
->scan_type
.sign
= 's';
179 channel
->scan_type
.realbits
= 64;
180 channel
->scan_type
.storagebits
= 64;
182 indio_dev
->channels
= state
->channels
;
183 indio_dev
->num_channels
= CROS_EC_BARO_MAX_CHANNELS
;
185 state
->core
.read_ec_sensors_data
= cros_ec_sensors_read_cmd
;
187 return cros_ec_sensors_core_register(dev
, indio_dev
,
188 cros_ec_sensors_push_data
);
191 static const struct platform_device_id cros_ec_baro_ids
[] = {
193 .name
= "cros-ec-baro",
197 MODULE_DEVICE_TABLE(platform
, cros_ec_baro_ids
);
199 static struct platform_driver cros_ec_baro_platform_driver
= {
201 .name
= "cros-ec-baro",
202 .pm
= &cros_ec_sensors_pm_ops
,
204 .probe
= cros_ec_baro_probe
,
205 .id_table
= cros_ec_baro_ids
,
207 module_platform_driver(cros_ec_baro_platform_driver
);
209 MODULE_DESCRIPTION("ChromeOS EC barometer sensor driver");
210 MODULE_LICENSE("GPL v2");