1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Invensense, Inc.
4 */
5 
6 #include <linux/pm_runtime.h>
7 
8 #include <linux/iio/common/inv_sensors_timestamp.h>
9 
10 #include "inv_mpu_iio.h"
11 
12 static unsigned int inv_scan_query_mpu6050(struct iio_dev *indio_dev)
13 {
14 	struct inv_mpu6050_state  *st = iio_priv(indio_dev);
15 	unsigned int mask;
16 
17 	/*
18 	 * If the MPU6050 is just used as a trigger, then the scan mask
19 	 * is not allocated so we simply enable the temperature channel
20 	 * as a dummy and bail out.
21 	 */
22 	if (!indio_dev->active_scan_mask) {
23 		st->chip_config.temp_fifo_enable = true;
24 		return INV_MPU6050_SENSOR_TEMP;
25 	}
26 
27 	st->chip_config.gyro_fifo_enable =
28 		test_bit(INV_MPU6050_SCAN_GYRO_X,
29 			 indio_dev->active_scan_mask) ||
30 		test_bit(INV_MPU6050_SCAN_GYRO_Y,
31 			 indio_dev->active_scan_mask) ||
32 		test_bit(INV_MPU6050_SCAN_GYRO_Z,
33 			 indio_dev->active_scan_mask);
34 
35 	st->chip_config.accl_fifo_enable =
36 		test_bit(INV_MPU6050_SCAN_ACCL_X,
37 			 indio_dev->active_scan_mask) ||
38 		test_bit(INV_MPU6050_SCAN_ACCL_Y,
39 			 indio_dev->active_scan_mask) ||
40 		test_bit(INV_MPU6050_SCAN_ACCL_Z,
41 			 indio_dev->active_scan_mask);
42 
43 	st->chip_config.temp_fifo_enable =
44 		test_bit(INV_MPU6050_SCAN_TEMP, indio_dev->active_scan_mask);
45 
46 	mask = 0;
47 	if (st->chip_config.gyro_fifo_enable)
48 		mask |= INV_MPU6050_SENSOR_GYRO;
49 	if (st->chip_config.accl_fifo_enable)
50 		mask |= INV_MPU6050_SENSOR_ACCL;
51 	if (st->chip_config.temp_fifo_enable)
52 		mask |= INV_MPU6050_SENSOR_TEMP;
53 
54 	return mask;
55 }
56 
57 static unsigned int inv_scan_query_mpu9x50(struct iio_dev *indio_dev)
58 {
59 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
60 	unsigned int mask;
61 
62 	mask = inv_scan_query_mpu6050(indio_dev);
63 
64 	/* no magnetometer if i2c auxiliary bus is used */
65 	if (st->magn_disabled)
66 		return mask;
67 
68 	st->chip_config.magn_fifo_enable =
69 		test_bit(INV_MPU9X50_SCAN_MAGN_X,
70 			 indio_dev->active_scan_mask) ||
71 		test_bit(INV_MPU9X50_SCAN_MAGN_Y,
72 			 indio_dev->active_scan_mask) ||
73 		test_bit(INV_MPU9X50_SCAN_MAGN_Z,
74 			 indio_dev->active_scan_mask);
75 	if (st->chip_config.magn_fifo_enable)
76 		mask |= INV_MPU6050_SENSOR_MAGN;
77 
78 	return mask;
79 }
80 
81 static unsigned int inv_scan_query(struct iio_dev *indio_dev)
82 {
83 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
84 
85 	switch (st->chip_type) {
86 	case INV_MPU9150:
87 	case INV_MPU9250:
88 	case INV_MPU9255:
89 		return inv_scan_query_mpu9x50(indio_dev);
90 	default:
91 		return inv_scan_query_mpu6050(indio_dev);
92 	}
93 }
94 
95 static unsigned int inv_compute_skip_samples(const struct inv_mpu6050_state *st)
96 {
97 	unsigned int skip_samples = 0;
98 
99 	/* mag first sample is always not ready, skip it */
100 	if (st->chip_config.magn_fifo_enable)
101 		skip_samples = 1;
102 
103 	return skip_samples;
104 }
105 
106 int inv_mpu6050_prepare_fifo(struct inv_mpu6050_state *st, bool enable)
107 {
108 	uint8_t d;
109 	int ret;
110 
111 	if (enable) {
112 		/* reset timestamping */
113 		inv_sensors_timestamp_reset(&st->timestamp);
114 		/* reset FIFO */
115 		d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_RST;
116 		ret = regmap_write(st->map, st->reg->user_ctrl, d);
117 		if (ret)
118 			return ret;
119 		/* enable sensor output to FIFO */
120 		d = 0;
121 		if (st->chip_config.gyro_fifo_enable)
122 			d |= INV_MPU6050_BITS_GYRO_OUT;
123 		if (st->chip_config.accl_fifo_enable)
124 			d |= INV_MPU6050_BIT_ACCEL_OUT;
125 		if (st->chip_config.temp_fifo_enable)
126 			d |= INV_MPU6050_BIT_TEMP_OUT;
127 		if (st->chip_config.magn_fifo_enable)
128 			d |= INV_MPU6050_BIT_SLAVE_0;
129 		ret = regmap_write(st->map, st->reg->fifo_en, d);
130 		if (ret)
131 			return ret;
132 		/* enable FIFO reading */
133 		d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_EN;
134 		ret = regmap_write(st->map, st->reg->user_ctrl, d);
135 		if (ret)
136 			return ret;
137 		/* enable interrupt */
138 		ret = regmap_write(st->map, st->reg->int_enable,
139 				   INV_MPU6050_BIT_DATA_RDY_EN);
140 	} else {
141 		ret = regmap_write(st->map, st->reg->int_enable, 0);
142 		if (ret)
143 			return ret;
144 		ret = regmap_write(st->map, st->reg->fifo_en, 0);
145 		if (ret)
146 			return ret;
147 		/* restore user_ctrl for disabling FIFO reading */
148 		ret = regmap_write(st->map, st->reg->user_ctrl,
149 				   st->chip_config.user_ctrl);
150 	}
151 
152 	return ret;
153 }
154 
155 /**
156  *  inv_mpu6050_set_enable() - enable chip functions.
157  *  @indio_dev:	Device driver instance.
158  *  @enable: enable/disable
159  */
160 static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable)
161 {
162 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
163 	struct device *pdev = regmap_get_device(st->map);
164 	unsigned int scan;
165 	int result;
166 
167 	if (enable) {
168 		scan = inv_scan_query(indio_dev);
169 		result = pm_runtime_resume_and_get(pdev);
170 		if (result)
171 			return result;
172 		/*
173 		 * In case autosuspend didn't trigger, turn off first not
174 		 * required sensors.
175 		 */
176 		result = inv_mpu6050_switch_engine(st, false, ~scan);
177 		if (result)
178 			goto error_power_off;
179 		result = inv_mpu6050_switch_engine(st, true, scan);
180 		if (result)
181 			goto error_power_off;
182 		st->skip_samples = inv_compute_skip_samples(st);
183 		result = inv_mpu6050_prepare_fifo(st, true);
184 		if (result)
185 			goto error_power_off;
186 	} else {
187 		result = inv_mpu6050_prepare_fifo(st, false);
188 		if (result)
189 			goto error_power_off;
190 		pm_runtime_mark_last_busy(pdev);
191 		pm_runtime_put_autosuspend(pdev);
192 	}
193 
194 	return 0;
195 
196 error_power_off:
197 	pm_runtime_put_autosuspend(pdev);
198 	return result;
199 }
200 
201 /**
202  * inv_mpu_data_rdy_trigger_set_state() - set data ready interrupt state
203  * @trig: Trigger instance
204  * @state: Desired trigger state
205  */
206 static int inv_mpu_data_rdy_trigger_set_state(struct iio_trigger *trig,
207 					      bool state)
208 {
209 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
210 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
211 	int result;
212 
213 	mutex_lock(&st->lock);
214 	result = inv_mpu6050_set_enable(indio_dev, state);
215 	mutex_unlock(&st->lock);
216 
217 	return result;
218 }
219 
220 static const struct iio_trigger_ops inv_mpu_trigger_ops = {
221 	.set_trigger_state = &inv_mpu_data_rdy_trigger_set_state,
222 };
223 
224 int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev, int irq_type)
225 {
226 	int ret;
227 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
228 
229 	st->trig = devm_iio_trigger_alloc(&indio_dev->dev,
230 					  "%s-dev%d",
231 					  indio_dev->name,
232 					  iio_device_id(indio_dev));
233 	if (!st->trig)
234 		return -ENOMEM;
235 
236 	ret = devm_request_irq(&indio_dev->dev, st->irq,
237 			       &iio_trigger_generic_data_rdy_poll,
238 			       irq_type,
239 			       "inv_mpu",
240 			       st->trig);
241 	if (ret)
242 		return ret;
243 
244 	st->trig->dev.parent = regmap_get_device(st->map);
245 	st->trig->ops = &inv_mpu_trigger_ops;
246 	iio_trigger_set_drvdata(st->trig, indio_dev);
247 
248 	ret = devm_iio_trigger_register(&indio_dev->dev, st->trig);
249 	if (ret)
250 		return ret;
251 
252 	indio_dev->trig = iio_trigger_get(st->trig);
253 
254 	return 0;
255 }
256