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