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/mfd/cros_ec.h>
18 #include <linux/mfd/cros_ec_commands.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/platform_device.h>
24 * One channel for pressure, the other for timestamp.
26 #define CROS_EC_BARO_MAX_CHANNELS (1 + 1)
28 /* State data for ec_sensors iio driver. */
29 struct cros_ec_baro_state
{
30 /* Shared by all sensors */
31 struct cros_ec_sensors_core_state core
;
33 struct iio_chan_spec channels
[CROS_EC_BARO_MAX_CHANNELS
];
36 static int cros_ec_baro_read(struct iio_dev
*indio_dev
,
37 struct iio_chan_spec
const *chan
,
38 int *val
, int *val2
, long mask
)
40 struct cros_ec_baro_state
*st
= iio_priv(indio_dev
);
42 int ret
= IIO_VAL_INT
;
43 int idx
= chan
->scan_index
;
45 mutex_lock(&st
->core
.cmd_lock
);
48 case IIO_CHAN_INFO_RAW
:
49 if (cros_ec_sensors_read_cmd(indio_dev
, 1 << idx
,
54 case IIO_CHAN_INFO_SCALE
:
55 st
->core
.param
.cmd
= MOTIONSENSE_CMD_SENSOR_RANGE
;
56 st
->core
.param
.sensor_range
.data
= EC_MOTION_SENSE_NO_VALUE
;
58 if (cros_ec_motion_send_host_cmd(&st
->core
, 0)) {
62 *val
= st
->core
.resp
->sensor_range
.ret
;
64 /* scale * in_pressure_raw --> kPa */
65 *val2
= 10 << CROS_EC_SENSOR_BITS
;
66 ret
= IIO_VAL_FRACTIONAL
;
69 ret
= cros_ec_sensors_core_read(&st
->core
, chan
, val
, val2
,
74 mutex_unlock(&st
->core
.cmd_lock
);
79 static int cros_ec_baro_write(struct iio_dev
*indio_dev
,
80 struct iio_chan_spec
const *chan
,
81 int val
, int val2
, long mask
)
83 struct cros_ec_baro_state
*st
= iio_priv(indio_dev
);
86 mutex_lock(&st
->core
.cmd_lock
);
89 case IIO_CHAN_INFO_SCALE
:
90 st
->core
.param
.cmd
= MOTIONSENSE_CMD_SENSOR_RANGE
;
91 st
->core
.param
.sensor_range
.data
= val
;
93 /* Always roundup, so caller gets at least what it asks for. */
94 st
->core
.param
.sensor_range
.roundup
= 1;
96 if (cros_ec_motion_send_host_cmd(&st
->core
, 0))
100 ret
= cros_ec_sensors_core_write(&st
->core
, chan
, val
, val2
,
105 mutex_unlock(&st
->core
.cmd_lock
);
110 static const struct iio_info cros_ec_baro_info
= {
111 .read_raw
= &cros_ec_baro_read
,
112 .write_raw
= &cros_ec_baro_write
,
115 static int cros_ec_baro_probe(struct platform_device
*pdev
)
117 struct device
*dev
= &pdev
->dev
;
118 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(dev
->parent
);
119 struct iio_dev
*indio_dev
;
120 struct cros_ec_baro_state
*state
;
121 struct iio_chan_spec
*channel
;
124 if (!ec_dev
|| !ec_dev
->ec_dev
) {
125 dev_warn(dev
, "No CROS EC device found.\n");
129 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*state
));
133 ret
= cros_ec_sensors_core_init(pdev
, indio_dev
, true);
137 indio_dev
->info
= &cros_ec_baro_info
;
138 state
= iio_priv(indio_dev
);
139 state
->core
.type
= state
->core
.resp
->info
.type
;
140 state
->core
.loc
= state
->core
.resp
->info
.location
;
141 channel
= state
->channels
;
143 channel
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
144 channel
->info_mask_shared_by_all
=
145 BIT(IIO_CHAN_INFO_SCALE
) |
146 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
147 BIT(IIO_CHAN_INFO_FREQUENCY
);
148 channel
->scan_type
.realbits
= CROS_EC_SENSOR_BITS
;
149 channel
->scan_type
.storagebits
= CROS_EC_SENSOR_BITS
;
150 channel
->scan_type
.shift
= 0;
151 channel
->scan_index
= 0;
152 channel
->ext_info
= cros_ec_sensors_ext_info
;
153 channel
->scan_type
.sign
= 'u';
155 state
->core
.calib
[0] = 0;
157 /* Sensor specific */
158 switch (state
->core
.type
) {
159 case MOTIONSENSE_TYPE_BARO
:
160 channel
->type
= IIO_PRESSURE
;
163 dev_warn(dev
, "Unknown motion sensor\n");
169 channel
->type
= IIO_TIMESTAMP
;
170 channel
->channel
= -1;
171 channel
->scan_index
= 1;
172 channel
->scan_type
.sign
= 's';
173 channel
->scan_type
.realbits
= 64;
174 channel
->scan_type
.storagebits
= 64;
176 indio_dev
->channels
= state
->channels
;
177 indio_dev
->num_channels
= CROS_EC_BARO_MAX_CHANNELS
;
179 state
->core
.read_ec_sensors_data
= cros_ec_sensors_read_cmd
;
181 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
, NULL
,
182 cros_ec_sensors_capture
, NULL
);
186 return devm_iio_device_register(dev
, indio_dev
);
189 static const struct platform_device_id cros_ec_baro_ids
[] = {
191 .name
= "cros-ec-baro",
195 MODULE_DEVICE_TABLE(platform
, cros_ec_baro_ids
);
197 static struct platform_driver cros_ec_baro_platform_driver
= {
199 .name
= "cros-ec-baro",
201 .probe
= cros_ec_baro_probe
,
202 .id_table
= cros_ec_baro_ids
,
204 module_platform_driver(cros_ec_baro_platform_driver
);
206 MODULE_DESCRIPTION("ChromeOS EC barometer sensor driver");
207 MODULE_LICENSE("GPL v2");