xref: /openbmc/linux/drivers/iio/pressure/cros_ec_baro.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
137aa055cSEnric Balletbo i Serra // SPDX-License-Identifier: GPL-2.0
2d732248fSGwendal Grignou /*
3d732248fSGwendal Grignou  * cros_ec_baro - Driver for barometer sensor behind CrosEC.
4d732248fSGwendal Grignou  *
5d732248fSGwendal Grignou  * Copyright (C) 2017 Google, Inc
6d732248fSGwendal Grignou  */
7d732248fSGwendal Grignou 
8d732248fSGwendal Grignou #include <linux/device.h>
9d732248fSGwendal Grignou #include <linux/iio/buffer.h>
105a0b8cb4SGwendal Grignou #include <linux/iio/common/cros_ec_sensors_core.h>
11d732248fSGwendal Grignou #include <linux/iio/iio.h>
12d732248fSGwendal Grignou #include <linux/iio/kfifo_buf.h>
13d732248fSGwendal Grignou #include <linux/iio/trigger.h>
14d732248fSGwendal Grignou #include <linux/iio/triggered_buffer.h>
15d732248fSGwendal Grignou #include <linux/iio/trigger_consumer.h>
16d732248fSGwendal Grignou #include <linux/kernel.h>
17ca27395fSNuno Sá #include <linux/mod_devicetable.h>
18d732248fSGwendal Grignou #include <linux/module.h>
19d732248fSGwendal Grignou #include <linux/slab.h>
20840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_commands.h>
21840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_proto.h>
22d732248fSGwendal Grignou #include <linux/platform_device.h>
23d732248fSGwendal Grignou 
24d732248fSGwendal Grignou /*
25d732248fSGwendal Grignou  * One channel for pressure, the other for timestamp.
26d732248fSGwendal Grignou  */
27d732248fSGwendal Grignou #define CROS_EC_BARO_MAX_CHANNELS (1 + 1)
28d732248fSGwendal Grignou 
29d732248fSGwendal Grignou /* State data for ec_sensors iio driver. */
30d732248fSGwendal Grignou struct cros_ec_baro_state {
31d732248fSGwendal Grignou 	/* Shared by all sensors */
32d732248fSGwendal Grignou 	struct cros_ec_sensors_core_state core;
33d732248fSGwendal Grignou 
34d732248fSGwendal Grignou 	struct iio_chan_spec channels[CROS_EC_BARO_MAX_CHANNELS];
35d732248fSGwendal Grignou };
36d732248fSGwendal Grignou 
cros_ec_baro_read(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)37d732248fSGwendal Grignou static int cros_ec_baro_read(struct iio_dev *indio_dev,
38d732248fSGwendal Grignou 			     struct iio_chan_spec const *chan,
39d732248fSGwendal Grignou 			     int *val, int *val2, long mask)
40d732248fSGwendal Grignou {
41d732248fSGwendal Grignou 	struct cros_ec_baro_state *st = iio_priv(indio_dev);
42d732248fSGwendal Grignou 	u16 data = 0;
43f53199c0SGwendal Grignou 	int ret;
44d732248fSGwendal Grignou 	int idx = chan->scan_index;
45d732248fSGwendal Grignou 
46d732248fSGwendal Grignou 	mutex_lock(&st->core.cmd_lock);
47d732248fSGwendal Grignou 
48d732248fSGwendal Grignou 	switch (mask) {
49d732248fSGwendal Grignou 	case IIO_CHAN_INFO_RAW:
50f53199c0SGwendal Grignou 		ret = cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
51f53199c0SGwendal Grignou 					     (s16 *)&data);
52f53199c0SGwendal Grignou 		if (ret)
53f53199c0SGwendal Grignou 			break;
54f53199c0SGwendal Grignou 
55d732248fSGwendal Grignou 		*val = data;
56f53199c0SGwendal Grignou 		ret = IIO_VAL_INT;
57d732248fSGwendal Grignou 		break;
58d732248fSGwendal Grignou 	case IIO_CHAN_INFO_SCALE:
59d732248fSGwendal Grignou 		st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
60d732248fSGwendal Grignou 		st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE;
61d732248fSGwendal Grignou 
62f53199c0SGwendal Grignou 		ret = cros_ec_motion_send_host_cmd(&st->core, 0);
63f53199c0SGwendal Grignou 		if (ret)
64d732248fSGwendal Grignou 			break;
65f53199c0SGwendal Grignou 
66d732248fSGwendal Grignou 		*val = st->core.resp->sensor_range.ret;
67d732248fSGwendal Grignou 
68d732248fSGwendal Grignou 		/* scale * in_pressure_raw --> kPa */
69d732248fSGwendal Grignou 		*val2 = 10 << CROS_EC_SENSOR_BITS;
70d732248fSGwendal Grignou 		ret = IIO_VAL_FRACTIONAL;
71d732248fSGwendal Grignou 		break;
72d732248fSGwendal Grignou 	default:
73d732248fSGwendal Grignou 		ret = cros_ec_sensors_core_read(&st->core, chan, val, val2,
74d732248fSGwendal Grignou 						mask);
75d732248fSGwendal Grignou 		break;
76d732248fSGwendal Grignou 	}
77d732248fSGwendal Grignou 
78d732248fSGwendal Grignou 	mutex_unlock(&st->core.cmd_lock);
79d732248fSGwendal Grignou 
80d732248fSGwendal Grignou 	return ret;
81d732248fSGwendal Grignou }
82d732248fSGwendal Grignou 
cros_ec_baro_write(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)83d732248fSGwendal Grignou static int cros_ec_baro_write(struct iio_dev *indio_dev,
84d732248fSGwendal Grignou 			      struct iio_chan_spec const *chan,
85d732248fSGwendal Grignou 			      int val, int val2, long mask)
86d732248fSGwendal Grignou {
87d732248fSGwendal Grignou 	struct cros_ec_baro_state *st = iio_priv(indio_dev);
88d732248fSGwendal Grignou 	int ret = 0;
89d732248fSGwendal Grignou 
90d732248fSGwendal Grignou 	mutex_lock(&st->core.cmd_lock);
91d732248fSGwendal Grignou 
92d732248fSGwendal Grignou 	switch (mask) {
93d732248fSGwendal Grignou 	case IIO_CHAN_INFO_SCALE:
94d732248fSGwendal Grignou 		st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
95d732248fSGwendal Grignou 		st->core.param.sensor_range.data = val;
96d732248fSGwendal Grignou 
97d732248fSGwendal Grignou 		/* Always roundup, so caller gets at least what it asks for. */
98d732248fSGwendal Grignou 		st->core.param.sensor_range.roundup = 1;
99d732248fSGwendal Grignou 
100e7e3b9d2SGwendal Grignou 		ret = cros_ec_motion_send_host_cmd(&st->core, 0);
101e7e3b9d2SGwendal Grignou 		if (ret == 0) {
102e7e3b9d2SGwendal Grignou 			st->core.range_updated = true;
103e7e3b9d2SGwendal Grignou 			st->core.curr_range = val;
104e7e3b9d2SGwendal Grignou 		}
105d732248fSGwendal Grignou 		break;
106d732248fSGwendal Grignou 	default:
107d732248fSGwendal Grignou 		ret = cros_ec_sensors_core_write(&st->core, chan, val, val2,
108d732248fSGwendal Grignou 						 mask);
109d732248fSGwendal Grignou 		break;
110d732248fSGwendal Grignou 	}
111d732248fSGwendal Grignou 
112d732248fSGwendal Grignou 	mutex_unlock(&st->core.cmd_lock);
113d732248fSGwendal Grignou 
114d732248fSGwendal Grignou 	return ret;
115d732248fSGwendal Grignou }
116d732248fSGwendal Grignou 
117d732248fSGwendal Grignou static const struct iio_info cros_ec_baro_info = {
118d732248fSGwendal Grignou 	.read_raw = &cros_ec_baro_read,
119d732248fSGwendal Grignou 	.write_raw = &cros_ec_baro_write,
120e9a4cbcaSGwendal Grignou 	.read_avail = &cros_ec_sensors_core_read_avail,
121d732248fSGwendal Grignou };
122d732248fSGwendal Grignou 
cros_ec_baro_probe(struct platform_device * pdev)123d732248fSGwendal Grignou static int cros_ec_baro_probe(struct platform_device *pdev)
124d732248fSGwendal Grignou {
125d732248fSGwendal Grignou 	struct device *dev = &pdev->dev;
126d732248fSGwendal Grignou 	struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
127d732248fSGwendal Grignou 	struct iio_dev *indio_dev;
128d732248fSGwendal Grignou 	struct cros_ec_baro_state *state;
129d732248fSGwendal Grignou 	struct iio_chan_spec *channel;
130d732248fSGwendal Grignou 	int ret;
131d732248fSGwendal Grignou 
132d732248fSGwendal Grignou 	if (!ec_dev || !ec_dev->ec_dev) {
133d732248fSGwendal Grignou 		dev_warn(dev, "No CROS EC device found.\n");
134d732248fSGwendal Grignou 		return -EINVAL;
135d732248fSGwendal Grignou 	}
136d732248fSGwendal Grignou 
137d732248fSGwendal Grignou 	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
138d732248fSGwendal Grignou 	if (!indio_dev)
139d732248fSGwendal Grignou 		return -ENOMEM;
140d732248fSGwendal Grignou 
141aa984f1bSGwendal Grignou 	ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
142*0b4ae3f6SGwendal Grignou 					cros_ec_sensors_capture);
143d732248fSGwendal Grignou 	if (ret)
144d732248fSGwendal Grignou 		return ret;
145d732248fSGwendal Grignou 
146d732248fSGwendal Grignou 	indio_dev->info = &cros_ec_baro_info;
147d732248fSGwendal Grignou 	state = iio_priv(indio_dev);
148d732248fSGwendal Grignou 	channel = state->channels;
149d732248fSGwendal Grignou 	/* Common part */
150d732248fSGwendal Grignou 	channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
151d732248fSGwendal Grignou 	channel->info_mask_shared_by_all =
152d732248fSGwendal Grignou 		BIT(IIO_CHAN_INFO_SCALE) |
1536562793bSGwendal Grignou 		BIT(IIO_CHAN_INFO_SAMP_FREQ);
154e9a4cbcaSGwendal Grignou 	channel->info_mask_shared_by_all_available =
155e9a4cbcaSGwendal Grignou 		BIT(IIO_CHAN_INFO_SAMP_FREQ);
156d732248fSGwendal Grignou 	channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
157d732248fSGwendal Grignou 	channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
158d732248fSGwendal Grignou 	channel->scan_type.shift = 0;
159d732248fSGwendal Grignou 	channel->scan_index = 0;
160d732248fSGwendal Grignou 	channel->ext_info = cros_ec_sensors_ext_info;
161d732248fSGwendal Grignou 	channel->scan_type.sign = 'u';
162d732248fSGwendal Grignou 
163d732248fSGwendal Grignou 	/* Sensor specific */
164d732248fSGwendal Grignou 	switch (state->core.type) {
165d732248fSGwendal Grignou 	case MOTIONSENSE_TYPE_BARO:
166d732248fSGwendal Grignou 		channel->type = IIO_PRESSURE;
167d732248fSGwendal Grignou 		break;
168d732248fSGwendal Grignou 	default:
169d732248fSGwendal Grignou 		dev_warn(dev, "Unknown motion sensor\n");
170d732248fSGwendal Grignou 		return -EINVAL;
171d732248fSGwendal Grignou 	}
172d732248fSGwendal Grignou 
173d732248fSGwendal Grignou 	/* Timestamp */
174d732248fSGwendal Grignou 	channel++;
175d732248fSGwendal Grignou 	channel->type = IIO_TIMESTAMP;
176d732248fSGwendal Grignou 	channel->channel = -1;
177d732248fSGwendal Grignou 	channel->scan_index = 1;
178d732248fSGwendal Grignou 	channel->scan_type.sign = 's';
179d732248fSGwendal Grignou 	channel->scan_type.realbits = 64;
180d732248fSGwendal Grignou 	channel->scan_type.storagebits = 64;
181d732248fSGwendal Grignou 
182d732248fSGwendal Grignou 	indio_dev->channels = state->channels;
183d732248fSGwendal Grignou 	indio_dev->num_channels = CROS_EC_BARO_MAX_CHANNELS;
184d732248fSGwendal Grignou 
185d732248fSGwendal Grignou 	state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
186d732248fSGwendal Grignou 
187*0b4ae3f6SGwendal Grignou 	return cros_ec_sensors_core_register(dev, indio_dev,
188*0b4ae3f6SGwendal Grignou 					     cros_ec_sensors_push_data);
189d732248fSGwendal Grignou }
190d732248fSGwendal Grignou 
191d732248fSGwendal Grignou static const struct platform_device_id cros_ec_baro_ids[] = {
192d732248fSGwendal Grignou 	{
193d732248fSGwendal Grignou 		.name = "cros-ec-baro",
194d732248fSGwendal Grignou 	},
195d732248fSGwendal Grignou 	{ /* sentinel */ }
196d732248fSGwendal Grignou };
197d732248fSGwendal Grignou MODULE_DEVICE_TABLE(platform, cros_ec_baro_ids);
198d732248fSGwendal Grignou 
199d732248fSGwendal Grignou static struct platform_driver cros_ec_baro_platform_driver = {
200d732248fSGwendal Grignou 	.driver = {
201d732248fSGwendal Grignou 		.name	= "cros-ec-baro",
202e7e3b9d2SGwendal Grignou 		.pm	= &cros_ec_sensors_pm_ops,
203d732248fSGwendal Grignou 	},
204d732248fSGwendal Grignou 	.probe		= cros_ec_baro_probe,
205d732248fSGwendal Grignou 	.id_table	= cros_ec_baro_ids,
206d732248fSGwendal Grignou };
207d732248fSGwendal Grignou module_platform_driver(cros_ec_baro_platform_driver);
208d732248fSGwendal Grignou 
209d732248fSGwendal Grignou MODULE_DESCRIPTION("ChromeOS EC barometer sensor driver");
210d732248fSGwendal Grignou MODULE_LICENSE("GPL v2");
211