hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/i2c/busses/i2c-gpio.c
....@@ -1,23 +1,22 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Bitbanging I2C bus driver using the GPIO API
34 *
45 * Copyright (C) 2007 Atmel Corporation
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License version 2 as
8
- * published by the Free Software Foundation.
96 */
7
+#include <linux/completion.h>
108 #include <linux/debugfs.h>
119 #include <linux/delay.h>
12
-#include <linux/i2c.h>
13
-#include <linux/i2c-algo-bit.h>
14
-#include <linux/platform_data/i2c-gpio.h>
15
-#include <linux/init.h>
16
-#include <linux/module.h>
17
-#include <linux/slab.h>
18
-#include <linux/platform_device.h>
1910 #include <linux/gpio/consumer.h>
11
+#include <linux/i2c-algo-bit.h>
12
+#include <linux/i2c.h>
13
+#include <linux/init.h>
14
+#include <linux/interrupt.h>
15
+#include <linux/module.h>
2016 #include <linux/of.h>
17
+#include <linux/platform_data/i2c-gpio.h>
18
+#include <linux/platform_device.h>
19
+#include <linux/slab.h>
2120
2221 struct i2c_gpio_private_data {
2322 struct gpio_desc *sda;
....@@ -27,6 +26,9 @@
2726 struct i2c_gpio_platform_data pdata;
2827 #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
2928 struct dentry *debug_dir;
29
+ /* these must be protected by bus lock */
30
+ struct completion scl_irq_completion;
31
+ u64 scl_irq_data;
3032 #endif
3133 };
3234
....@@ -162,6 +164,96 @@
162164 }
163165 DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n");
164166
167
+static int i2c_gpio_fi_act_on_scl_irq(struct i2c_gpio_private_data *priv,
168
+ irqreturn_t handler(int, void*))
169
+{
170
+ int ret, irq = gpiod_to_irq(priv->scl);
171
+
172
+ if (irq < 0)
173
+ return irq;
174
+
175
+ i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
176
+
177
+ ret = gpiod_direction_input(priv->scl);
178
+ if (ret)
179
+ goto unlock;
180
+
181
+ reinit_completion(&priv->scl_irq_completion);
182
+
183
+ ret = request_irq(irq, handler, IRQF_TRIGGER_FALLING,
184
+ "i2c_gpio_fault_injector_scl_irq", priv);
185
+ if (ret)
186
+ goto output;
187
+
188
+ wait_for_completion_interruptible(&priv->scl_irq_completion);
189
+
190
+ free_irq(irq, priv);
191
+ output:
192
+ ret = gpiod_direction_output(priv->scl, 1) ?: ret;
193
+ unlock:
194
+ i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
195
+
196
+ return ret;
197
+}
198
+
199
+static irqreturn_t lose_arbitration_irq(int irq, void *dev_id)
200
+{
201
+ struct i2c_gpio_private_data *priv = dev_id;
202
+
203
+ setsda(&priv->bit_data, 0);
204
+ udelay(priv->scl_irq_data);
205
+ setsda(&priv->bit_data, 1);
206
+
207
+ complete(&priv->scl_irq_completion);
208
+
209
+ return IRQ_HANDLED;
210
+}
211
+
212
+static int fops_lose_arbitration_set(void *data, u64 duration)
213
+{
214
+ struct i2c_gpio_private_data *priv = data;
215
+
216
+ if (duration > 100 * 1000)
217
+ return -EINVAL;
218
+
219
+ priv->scl_irq_data = duration;
220
+ /*
221
+ * Interrupt on falling SCL. This ensures that the master under test has
222
+ * really started the transfer. Interrupt on falling SDA did only
223
+ * exercise 'bus busy' detection on some HW but not 'arbitration lost'.
224
+ * Note that the interrupt latency may cause the first bits to be
225
+ * transmitted correctly.
226
+ */
227
+ return i2c_gpio_fi_act_on_scl_irq(priv, lose_arbitration_irq);
228
+}
229
+DEFINE_DEBUGFS_ATTRIBUTE(fops_lose_arbitration, NULL, fops_lose_arbitration_set, "%llu\n");
230
+
231
+static irqreturn_t inject_panic_irq(int irq, void *dev_id)
232
+{
233
+ struct i2c_gpio_private_data *priv = dev_id;
234
+
235
+ udelay(priv->scl_irq_data);
236
+ panic("I2C fault injector induced panic");
237
+
238
+ return IRQ_HANDLED;
239
+}
240
+
241
+static int fops_inject_panic_set(void *data, u64 duration)
242
+{
243
+ struct i2c_gpio_private_data *priv = data;
244
+
245
+ if (duration > 100 * 1000)
246
+ return -EINVAL;
247
+
248
+ priv->scl_irq_data = duration;
249
+ /*
250
+ * Interrupt on falling SCL. This ensures that the master under test has
251
+ * really started the transfer.
252
+ */
253
+ return i2c_gpio_fi_act_on_scl_irq(priv, inject_panic_irq);
254
+}
255
+DEFINE_DEBUGFS_ATTRIBUTE(fops_inject_panic, NULL, fops_inject_panic_set, "%llu\n");
256
+
165257 static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
166258 {
167259 struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
....@@ -181,12 +273,20 @@
181273 if (!priv->debug_dir)
182274 return;
183275
184
- debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
185
- debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
276
+ init_completion(&priv->scl_irq_completion);
277
+
186278 debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->debug_dir,
187279 priv, &fops_incomplete_addr_phase);
188280 debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->debug_dir,
189281 priv, &fops_incomplete_write_byte);
282
+ if (priv->bit_data.getscl) {
283
+ debugfs_create_file_unsafe("inject_panic", 0200, priv->debug_dir,
284
+ priv, &fops_inject_panic);
285
+ debugfs_create_file_unsafe("lose_arbitration", 0200, priv->debug_dir,
286
+ priv, &fops_lose_arbitration);
287
+ }
288
+ debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
289
+ debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
190290 }
191291
192292 static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
....@@ -286,11 +386,11 @@
286386
287387 /*
288388 * First get the GPIO pins; if it fails, we'll defer the probe.
289
- * If the SDA line is marked from platform data or device tree as
290
- * "open drain" it means something outside of our control is making
291
- * this line being handled as open drain, and we should just handle
292
- * it as any other output. Else we enforce open drain as this is
293
- * required for an I2C bus.
389
+ * If the SCL/SDA lines are marked "open drain" by platform data or
390
+ * device tree then this means that something outside of our control is
391
+ * marking these lines to be handled as open drain, and we should just
392
+ * handle them as we handle any other output. Else we enforce open
393
+ * drain as this is required for an I2C bus.
294394 */
295395 if (pdata->sda_is_open_drain)
296396 gflags = GPIOD_OUT_HIGH;
....@@ -300,13 +400,6 @@
300400 if (IS_ERR(priv->sda))
301401 return PTR_ERR(priv->sda);
302402
303
- /*
304
- * If the SCL line is marked from platform data or device tree as
305
- * "open drain" it means something outside of our control is making
306
- * this line being handled as open drain, and we should just handle
307
- * it as any other output. Else we enforce open drain as this is
308
- * required for an I2C bus.
309
- */
310403 if (pdata->scl_is_open_drain)
311404 gflags = GPIOD_OUT_HIGH;
312405 else
....@@ -317,6 +410,8 @@
317410
318411 if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
319412 dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
413
+ else
414
+ bit_data->can_do_atomic = true;
320415
321416 bit_data->setsda = i2c_gpio_setsda_val;
322417 bit_data->setscl = i2c_gpio_setscl_val;