xref: /linux/drivers/i2c/busses/i2c-gpio.c (revision 1fc5a74b108fc90951890ec513ac81869f5eaff1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Bitbanging I2C bus driver using the GPIO API
4  *
5  * Copyright (C) 2007 Atmel Corporation
6  */
7 #include <linux/completion.h>
8 #include <linux/debugfs.h>
9 #include <linux/delay.h>
10 #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>
16 #include <linux/platform_data/i2c-gpio.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/slab.h>
20 
21 static LIST_HEAD(i2c_gpio_scl_list);
22 static DEFINE_MUTEX(i2c_gpio_scl_list_lock);
23 static struct lock_class_key i2c_gpio_scl_lock_key;
24 
25 struct i2c_gpio_scl_data {
26 	struct fwnode_reference_args args;
27 	struct gpio_desc *gpio;
28 	struct rt_mutex lock;
29 	struct list_head list;
30 	refcount_t ref;
31 };
32 
33 struct i2c_gpio_private_data {
34 	struct gpio_desc *sda;
35 	struct i2c_gpio_scl_data *scl;
36 	struct i2c_adapter adap;
37 	struct i2c_algo_bit_data bit_data;
38 	struct i2c_gpio_platform_data pdata;
39 #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
40 	/* these must be protected by bus lock */
41 	struct completion scl_irq_completion;
42 	u64 scl_irq_data;
43 #endif
44 };
45 
adap_to_priv(struct i2c_adapter * adap)46 static inline struct i2c_gpio_private_data *adap_to_priv(struct i2c_adapter *adap)
47 {
48 	return container_of(adap, struct i2c_gpio_private_data, adap);
49 }
50 
51 /*
52  * Toggle SDA by changing the output value of the pin. This is only
53  * valid for pins configured as open drain (i.e. setting the value
54  * high effectively turns off the output driver.)
55  */
i2c_gpio_setsda_val(void * data,int state)56 static void i2c_gpio_setsda_val(void *data, int state)
57 {
58 	struct i2c_gpio_private_data *priv = data;
59 
60 	gpiod_set_value_cansleep(priv->sda, state);
61 }
62 
63 /*
64  * Toggle SCL by changing the output value of the pin. This is used
65  * for pins that are configured as open drain and for output-only
66  * pins. The latter case will break the i2c protocol, but it will
67  * often work in practice.
68  */
i2c_gpio_setscl_val(void * data,int state)69 static void i2c_gpio_setscl_val(void *data, int state)
70 {
71 	struct i2c_gpio_private_data *priv = data;
72 
73 	gpiod_set_value_cansleep(priv->scl->gpio, state);
74 }
75 
i2c_gpio_getsda(void * data)76 static int i2c_gpio_getsda(void *data)
77 {
78 	struct i2c_gpio_private_data *priv = data;
79 
80 	return gpiod_get_value_cansleep(priv->sda);
81 }
82 
i2c_gpio_getscl(void * data)83 static int i2c_gpio_getscl(void *data)
84 {
85 	struct i2c_gpio_private_data *priv = data;
86 
87 	return gpiod_get_value_cansleep(priv->scl->gpio);
88 }
89 
i2c_gpio_lock_bus(struct i2c_adapter * adap,unsigned int flags)90 static void i2c_gpio_lock_bus(struct i2c_adapter *adap, unsigned int flags)
91 {
92 	/* Take care about adapter lock. See i2c_adapter_lock_bus() and others. */
93 	rt_mutex_lock_nested(&adap->bus_lock, i2c_adapter_depth(adap));
94 	rt_mutex_lock(&adap_to_priv(adap)->scl->lock);
95 }
96 
i2c_gpio_trylock_bus(struct i2c_adapter * adap,unsigned int flags)97 static int i2c_gpio_trylock_bus(struct i2c_adapter *adap, unsigned int flags)
98 {
99 	if (!rt_mutex_trylock(&adap->bus_lock))
100 		return 0;
101 
102 	if (!rt_mutex_trylock(&adap_to_priv(adap)->scl->lock)) {
103 		rt_mutex_unlock(&adap->bus_lock);
104 		return 0;
105 	}
106 
107 	return 1;
108 }
109 
i2c_gpio_unlock_bus(struct i2c_adapter * adap,unsigned int flags)110 static void i2c_gpio_unlock_bus(struct i2c_adapter *adap, unsigned int flags)
111 {
112 	rt_mutex_unlock(&adap_to_priv(adap)->scl->lock);
113 	rt_mutex_unlock(&adap->bus_lock);
114 }
115 
116 static const struct i2c_lock_operations i2c_gpio_lock_ops = {
117 	.lock_bus = i2c_gpio_lock_bus,
118 	.trylock_bus = i2c_gpio_trylock_bus,
119 	.unlock_bus = i2c_gpio_unlock_bus,
120 };
121 
122 #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
123 
124 #define setsda(bd, val)	((bd)->setsda((bd)->data, val))
125 #define setscl(bd, val)	((bd)->setscl((bd)->data, val))
126 #define getsda(bd)	((bd)->getsda((bd)->data))
127 #define getscl(bd)	((bd)->getscl((bd)->data))
128 
129 #define WIRE_ATTRIBUTE(wire) \
130 static int fops_##wire##_get(void *data, u64 *val)		\
131 {								\
132 	struct i2c_gpio_private_data *priv = data;		\
133 								\
134 	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
135 	*val = get##wire(&priv->bit_data);			\
136 	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
137 	return 0;						\
138 }								\
139 static int fops_##wire##_set(void *data, u64 val)		\
140 {								\
141 	struct i2c_gpio_private_data *priv = data;		\
142 								\
143 	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
144 	set##wire(&priv->bit_data, val);			\
145 	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);	\
146 	return 0;						\
147 }								\
148 DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
149 
150 WIRE_ATTRIBUTE(scl);
151 WIRE_ATTRIBUTE(sda);
152 
i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data * priv,u32 pattern,u8 pattern_size)153 static void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data *priv,
154 					u32 pattern, u8 pattern_size)
155 {
156 	struct i2c_algo_bit_data *bit_data = &priv->bit_data;
157 	int i;
158 
159 	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
160 
161 	/* START condition */
162 	setsda(bit_data, 0);
163 	udelay(bit_data->udelay);
164 
165 	/* Send pattern, request ACK, don't send STOP */
166 	for (i = pattern_size - 1; i >= 0; i--) {
167 		setscl(bit_data, 0);
168 		udelay(bit_data->udelay / 2);
169 		setsda(bit_data, (pattern >> i) & 1);
170 		udelay((bit_data->udelay + 1) / 2);
171 		setscl(bit_data, 1);
172 		udelay(bit_data->udelay);
173 	}
174 
175 	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
176 }
177 
fops_incomplete_addr_phase_set(void * data,u64 addr)178 static int fops_incomplete_addr_phase_set(void *data, u64 addr)
179 {
180 	struct i2c_gpio_private_data *priv = data;
181 	u32 pattern;
182 
183 	if (addr > 0x7f)
184 		return -EINVAL;
185 
186 	/* ADDR (7 bit) + RD (1 bit) + Client ACK, keep SDA hi (1 bit) */
187 	pattern = (addr << 2) | 3;
188 
189 	i2c_gpio_incomplete_transfer(priv, pattern, 9);
190 
191 	return 0;
192 }
193 DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase, NULL, fops_incomplete_addr_phase_set, "%llu\n");
194 
fops_incomplete_write_byte_set(void * data,u64 addr)195 static int fops_incomplete_write_byte_set(void *data, u64 addr)
196 {
197 	struct i2c_gpio_private_data *priv = data;
198 	u32 pattern;
199 
200 	if (addr > 0x7f)
201 		return -EINVAL;
202 
203 	/* ADDR (7 bit) + WR (1 bit) + Client ACK (1 bit) */
204 	pattern = (addr << 2) | 1;
205 	/* 0x00 (8 bit) + Client ACK, keep SDA hi (1 bit) */
206 	pattern = (pattern << 9) | 1;
207 
208 	i2c_gpio_incomplete_transfer(priv, pattern, 18);
209 
210 	return 0;
211 }
212 DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n");
213 
i2c_gpio_fi_act_on_scl_irq(struct i2c_gpio_private_data * priv,irqreturn_t handler (int,void *))214 static int i2c_gpio_fi_act_on_scl_irq(struct i2c_gpio_private_data *priv,
215 				       irqreturn_t handler(int, void*))
216 {
217 	int ret, irq = gpiod_to_irq(priv->scl->gpio);
218 
219 	if (irq < 0)
220 		return irq;
221 
222 	i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
223 
224 	ret = gpiod_direction_input(priv->scl->gpio);
225 	if (ret)
226 		goto unlock;
227 
228 	reinit_completion(&priv->scl_irq_completion);
229 
230 	ret = request_irq(irq, handler, IRQF_TRIGGER_FALLING,
231 			  "i2c_gpio_fault_injector_scl_irq", priv);
232 	if (ret)
233 		goto output;
234 
235 	wait_for_completion_interruptible(&priv->scl_irq_completion);
236 
237 	free_irq(irq, priv);
238  output:
239 	ret = gpiod_direction_output(priv->scl->gpio, 1) ?: ret;
240  unlock:
241 	i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
242 
243 	return ret;
244 }
245 
lose_arbitration_irq(int irq,void * dev_id)246 static irqreturn_t lose_arbitration_irq(int irq, void *dev_id)
247 {
248 	struct i2c_gpio_private_data *priv = dev_id;
249 
250 	setsda(&priv->bit_data, 0);
251 	udelay(priv->scl_irq_data);
252 	setsda(&priv->bit_data, 1);
253 
254 	complete(&priv->scl_irq_completion);
255 
256 	return IRQ_HANDLED;
257 }
258 
fops_lose_arbitration_set(void * data,u64 duration)259 static int fops_lose_arbitration_set(void *data, u64 duration)
260 {
261 	struct i2c_gpio_private_data *priv = data;
262 
263 	if (duration > 100 * 1000)
264 		return -EINVAL;
265 
266 	priv->scl_irq_data = duration;
267 	/*
268 	 * Interrupt on falling SCL. This ensures that the controller under test
269 	 * has really started the transfer. Interrupt on falling SDA did only
270 	 * exercise 'bus busy' detection on some HW but not 'arbitration lost'.
271 	 * Note that the interrupt latency may cause the first bits to be
272 	 * transmitted correctly.
273 	 */
274 	return i2c_gpio_fi_act_on_scl_irq(priv, lose_arbitration_irq);
275 }
276 DEFINE_DEBUGFS_ATTRIBUTE(fops_lose_arbitration, NULL, fops_lose_arbitration_set, "%llu\n");
277 
inject_panic_irq(int irq,void * dev_id)278 static irqreturn_t inject_panic_irq(int irq, void *dev_id)
279 {
280 	struct i2c_gpio_private_data *priv = dev_id;
281 
282 	udelay(priv->scl_irq_data);
283 	panic("I2C fault injector induced panic");
284 
285 	return IRQ_HANDLED;
286 }
287 
fops_inject_panic_set(void * data,u64 duration)288 static int fops_inject_panic_set(void *data, u64 duration)
289 {
290 	struct i2c_gpio_private_data *priv = data;
291 
292 	if (duration > 100 * 1000)
293 		return -EINVAL;
294 
295 	priv->scl_irq_data = duration;
296 	/*
297 	 * Interrupt on falling SCL. This ensures that the controller under test
298 	 * has really started the transfer.
299 	 */
300 	return i2c_gpio_fi_act_on_scl_irq(priv, inject_panic_irq);
301 }
302 DEFINE_DEBUGFS_ATTRIBUTE(fops_inject_panic, NULL, fops_inject_panic_set, "%llu\n");
303 
i2c_gpio_fault_injector_init(struct platform_device * pdev)304 static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
305 {
306 	struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
307 
308 	init_completion(&priv->scl_irq_completion);
309 
310 	debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->adap.debugfs,
311 				   priv, &fops_incomplete_addr_phase);
312 	debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->adap.debugfs,
313 				   priv, &fops_incomplete_write_byte);
314 	if (priv->bit_data.getscl) {
315 		debugfs_create_file_unsafe("inject_panic", 0200, priv->adap.debugfs,
316 					   priv, &fops_inject_panic);
317 		debugfs_create_file_unsafe("lose_arbitration", 0200, priv->adap.debugfs,
318 					   priv, &fops_lose_arbitration);
319 	}
320 	debugfs_create_file_unsafe("scl", 0600, priv->adap.debugfs, priv, &fops_scl);
321 	debugfs_create_file_unsafe("sda", 0600, priv->adap.debugfs, priv, &fops_sda);
322 }
323 #else
i2c_gpio_fault_injector_init(struct platform_device * pdev)324 static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
325 #endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
326 
327 /* Get i2c-gpio properties from DT or ACPI table */
i2c_gpio_get_properties(struct device * dev,struct i2c_gpio_platform_data * pdata)328 static void i2c_gpio_get_properties(struct device *dev,
329 				    struct i2c_gpio_platform_data *pdata)
330 {
331 	u32 reg;
332 
333 	device_property_read_u32(dev, "i2c-gpio,delay-us", &pdata->udelay);
334 
335 	if (!device_property_read_u32(dev, "i2c-gpio,timeout-ms", &reg))
336 		pdata->timeout = msecs_to_jiffies(reg);
337 
338 	pdata->sda_is_open_drain =
339 		device_property_read_bool(dev, "i2c-gpio,sda-open-drain");
340 	pdata->scl_is_open_drain =
341 		device_property_read_bool(dev, "i2c-gpio,scl-open-drain");
342 	pdata->scl_is_output_only =
343 		device_property_read_bool(dev, "i2c-gpio,scl-output-only");
344 	pdata->sda_is_output_only =
345 		device_property_read_bool(dev, "i2c-gpio,sda-output-only");
346 	pdata->sda_has_no_pullup =
347 		device_property_read_bool(dev, "i2c-gpio,sda-has-no-pullup");
348 	pdata->scl_has_no_pullup =
349 		device_property_read_bool(dev, "i2c-gpio,scl-has-no-pullup");
350 }
351 
i2c_gpio_get_desc(struct device * dev,const char * con_id,unsigned int index,enum gpiod_flags gflags)352 static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
353 					   const char *con_id,
354 					   unsigned int index,
355 					   enum gpiod_flags gflags)
356 {
357 	struct gpio_desc *retdesc;
358 	int ret;
359 
360 	/*
361 	 * Don't use resource-managed functions. SCL may be shared across adapters and has
362 	 * its own lifetime management. SDA uses the same path for consistency.
363 	 */
364 	retdesc = gpiod_get(dev, con_id, gflags);
365 	if (!IS_ERR(retdesc)) {
366 		dev_dbg(dev, "got GPIO from name %s\n", con_id);
367 		return retdesc;
368 	}
369 
370 	retdesc = gpiod_get_index(dev, NULL, index, gflags);
371 	if (!IS_ERR(retdesc)) {
372 		dev_dbg(dev, "got GPIO from index %u\n", index);
373 		return retdesc;
374 	}
375 
376 	ret = PTR_ERR(retdesc);
377 
378 	/* FIXME: hack in the old code, is this really necessary? */
379 	if (ret == -EINVAL)
380 		retdesc = ERR_PTR(-EPROBE_DEFER);
381 
382 	/* This happens if the GPIO driver is not yet probed, let's defer */
383 	if (ret == -ENOENT)
384 		retdesc = ERR_PTR(-EPROBE_DEFER);
385 
386 	if (PTR_ERR(retdesc) != -EPROBE_DEFER)
387 		dev_err(dev, "error trying to get descriptor: %d\n", ret);
388 
389 	return retdesc;
390 }
391 
i2c_gpio_create_scl(struct device * dev)392 static struct i2c_gpio_scl_data *i2c_gpio_create_scl(struct device *dev)
393 {
394 	struct fwnode_handle *fwnode = dev_fwnode(dev);
395 	struct fwnode_reference_args args;
396 	struct i2c_gpio_scl_data *scl;
397 	bool sharable = false;
398 	int ret;
399 
400 	/*
401 	 * SCL gpios can be shared if they are defined in the devicetree and have at least one
402 	 * cell in addition to the phandle. This is usually the pin. To support complex designs
403 	 * with arbitrary of_xlate() mappings handle the node cells opaque.
404 	 */
405 	if (fwnode) {
406 		ret = fwnode_property_get_reference_args(fwnode, "scl-gpios",
407 							 "#gpio-cells", 0, 0, &args);
408 		if (ret)
409 			/* try the ancient way */
410 			ret = fwnode_property_get_reference_args(fwnode, "gpios",
411 								 "#gpio-cells", 0, 1, &args);
412 
413 		if (!ret) {
414 			if (args.nargs >= 1)
415 				sharable = true;
416 			else
417 				fwnode_handle_put(args.fwnode);
418 		}
419 	}
420 
421 	scl = kzalloc_obj(*scl);
422 	if (!scl) {
423 		if (sharable)
424 			fwnode_handle_put(args.fwnode);
425 		return ERR_PTR(-ENOMEM);
426 	}
427 
428 	if (sharable) {
429 		scl->args.fwnode = args.fwnode;
430 		scl->args.nargs = args.nargs;
431 		memcpy(scl->args.args, args.args, sizeof(args.args[0]) * args.nargs);
432 	}
433 
434 	rt_mutex_init(&scl->lock);
435 	lockdep_set_class(&scl->lock, &i2c_gpio_scl_lock_key);
436 	refcount_set(&scl->ref, 1);
437 
438 	return scl;
439 }
440 
i2c_gpio_free_scl(struct i2c_gpio_scl_data * scl)441 static void i2c_gpio_free_scl(struct i2c_gpio_scl_data *scl)
442 {
443 	if (scl->args.fwnode)
444 		fwnode_handle_put(scl->args.fwnode);
445 	kfree(scl);
446 }
447 
i2c_gpio_scl_matches(struct i2c_gpio_scl_data * a,struct i2c_gpio_scl_data * b)448 static bool i2c_gpio_scl_matches(struct i2c_gpio_scl_data *a, struct i2c_gpio_scl_data *b)
449 {
450 	if (!a->args.fwnode || !b->args.fwnode ||
451 	    a->args.fwnode != b->args.fwnode || a->args.nargs != b->args.nargs)
452 		return false;
453 
454 	return memcmp(a->args.args, b->args.args, sizeof(a->args.args[0]) * a->args.nargs) == 0;
455 }
456 
457 /*
458  * Look up an existing or create a new shared SCL structure. Optimistic setup sequence always
459  * creates and tries to add a new entry to the list. This uses minimum locking and afterwards
460  * requests the GPIO without a lock held. Concurrent probes for the same SCL pin see the entry
461  * and do not race into a second gpiod_get(). Until everything is setup they terminate with
462  * -EPROBE_DEFER.
463  */
i2c_gpio_lookup_scl(struct device * dev,enum gpiod_flags gflags)464 static struct i2c_gpio_scl_data *i2c_gpio_lookup_scl(struct device *dev, enum gpiod_flags gflags)
465 {
466 	struct i2c_gpio_scl_data *scl, *new_scl;
467 	struct gpio_desc *gpio;
468 
469 	new_scl = i2c_gpio_create_scl(dev);
470 	if (IS_ERR(new_scl))
471 		return new_scl;
472 
473 	scoped_guard(mutex, &i2c_gpio_scl_list_lock) {
474 		list_for_each_entry(scl, &i2c_gpio_scl_list, list) {
475 			if (!i2c_gpio_scl_matches(scl, new_scl))
476 				continue;
477 
478 			i2c_gpio_free_scl(new_scl);
479 			if (!scl->gpio)
480 				return ERR_PTR(-EPROBE_DEFER);
481 
482 			refcount_inc(&scl->ref);
483 			if (scl->args.fwnode)
484 				dev_dbg(dev, "reusing shared SCL (%pfwP)\n", scl->args.fwnode);
485 
486 			return scl;
487 		}
488 		list_add(&new_scl->list, &i2c_gpio_scl_list);
489 	}
490 
491 	gpio = i2c_gpio_get_desc(dev, "scl", 1, gflags);
492 	if (IS_ERR(gpio)) {
493 		scoped_guard(mutex, &i2c_gpio_scl_list_lock)
494 			list_del(&new_scl->list);
495 		i2c_gpio_free_scl(new_scl);
496 
497 		return ERR_CAST(gpio);
498 	}
499 
500 	scoped_guard(mutex, &i2c_gpio_scl_list_lock)
501 		new_scl->gpio = gpio;
502 
503 	if (new_scl->args.fwnode)
504 		dev_dbg(dev, "registered shared SCL (%pfwP)\n", new_scl->args.fwnode);
505 
506 	return new_scl;
507 }
508 
i2c_gpio_cleanup_scl(struct i2c_gpio_scl_data * scl)509 static void i2c_gpio_cleanup_scl(struct i2c_gpio_scl_data *scl)
510 {
511 	if (!refcount_dec_and_mutex_lock(&scl->ref, &i2c_gpio_scl_list_lock))
512 		return;
513 
514 	list_del(&scl->list);
515 	mutex_unlock(&i2c_gpio_scl_list_lock);
516 	gpiod_put(scl->gpio);
517 	i2c_gpio_free_scl(scl);
518 }
519 
i2c_gpio_probe(struct platform_device * pdev)520 static int i2c_gpio_probe(struct platform_device *pdev)
521 {
522 	struct i2c_gpio_private_data *priv;
523 	struct i2c_gpio_platform_data *pdata;
524 	struct i2c_algo_bit_data *bit_data;
525 	struct i2c_adapter *adap;
526 	struct device *dev = &pdev->dev;
527 	struct fwnode_handle *fwnode = dev_fwnode(dev);
528 	enum gpiod_flags gflags;
529 	int ret;
530 
531 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
532 	if (!priv)
533 		return -ENOMEM;
534 
535 	adap = &priv->adap;
536 	bit_data = &priv->bit_data;
537 	pdata = &priv->pdata;
538 
539 	if (fwnode) {
540 		i2c_gpio_get_properties(dev, pdata);
541 	} else {
542 		/*
543 		 * If all platform data settings are zero it is OK
544 		 * to not provide any platform data from the board.
545 		 */
546 		if (dev_get_platdata(dev))
547 			memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
548 	}
549 
550 	/*
551 	 * First get the GPIO pins; if it fails, we'll defer the probe.
552 	 * If the SCL/SDA lines are marked "open drain" by platform data or
553 	 * device tree then this means that something outside of our control is
554 	 * marking these lines to be handled as open drain, and we should just
555 	 * handle them as we handle any other output. Else we enforce open
556 	 * drain as this is required for an I2C bus.
557 	 */
558 	if (pdata->sda_is_open_drain || pdata->sda_has_no_pullup)
559 		gflags = GPIOD_OUT_HIGH;
560 	else
561 		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
562 	priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
563 	if (IS_ERR(priv->sda))
564 		return PTR_ERR(priv->sda);
565 
566 	if (pdata->scl_is_open_drain || pdata->scl_has_no_pullup)
567 		gflags = GPIOD_OUT_HIGH;
568 	else
569 		gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
570 	priv->scl = i2c_gpio_lookup_scl(dev, gflags);
571 	if (IS_ERR(priv->scl)) {
572 		ret = PTR_ERR(priv->scl);
573 		goto err_cleanup_sda;
574 	}
575 
576 	if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl->gpio))
577 		dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
578 	else
579 		bit_data->can_do_atomic = true;
580 
581 	bit_data->skip_bit_test = !!priv->scl->args.fwnode;
582 	bit_data->setsda = i2c_gpio_setsda_val;
583 	bit_data->setscl = i2c_gpio_setscl_val;
584 
585 	if (!pdata->scl_is_output_only)
586 		bit_data->getscl = i2c_gpio_getscl;
587 	if (!pdata->sda_is_output_only)
588 		bit_data->getsda = i2c_gpio_getsda;
589 
590 	if (pdata->udelay)
591 		bit_data->udelay = pdata->udelay;
592 	else if (pdata->scl_is_output_only)
593 		bit_data->udelay = 50;			/* 10 kHz */
594 	else
595 		bit_data->udelay = 5;			/* 100 kHz */
596 
597 	if (pdata->timeout)
598 		bit_data->timeout = pdata->timeout;
599 	else
600 		bit_data->timeout = HZ / 10;		/* 100 ms */
601 
602 	bit_data->data = priv;
603 
604 	adap->owner = THIS_MODULE;
605 	if (fwnode)
606 		strscpy(adap->name, dev_name(dev), sizeof(adap->name));
607 	else
608 		snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
609 
610 	/* Always use shared SCL aware locking */
611 	adap->lock_ops = &i2c_gpio_lock_ops;
612 	adap->algo_data = bit_data;
613 	adap->class = I2C_CLASS_HWMON;
614 	adap->dev.parent = dev;
615 	device_set_node(&adap->dev, fwnode);
616 
617 	adap->nr = pdev->id;
618 	ret = i2c_bit_add_numbered_bus(adap);
619 	if (ret)
620 		goto err_cleanup_scl;
621 
622 	platform_set_drvdata(pdev, priv);
623 
624 	/*
625 	 * FIXME: using global GPIO numbers is not helpful. If/when we
626 	 * get accessors to get the actual name of the GPIO line,
627 	 * from the descriptor, then provide that instead.
628 	 */
629 	dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
630 		 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl->gpio),
631 		 pdata->scl_is_output_only
632 		 ? ", no clock stretching" : "");
633 
634 	i2c_gpio_fault_injector_init(pdev);
635 
636 	return 0;
637 
638 err_cleanup_scl:
639 	i2c_gpio_cleanup_scl(priv->scl);
640 err_cleanup_sda:
641 	gpiod_put(priv->sda);
642 
643 	return ret;
644 }
645 
i2c_gpio_remove(struct platform_device * pdev)646 static void i2c_gpio_remove(struct platform_device *pdev)
647 {
648 	struct i2c_gpio_private_data *priv;
649 	struct i2c_adapter *adap;
650 
651 	priv = platform_get_drvdata(pdev);
652 	adap = &priv->adap;
653 
654 	i2c_del_adapter(adap);
655 	i2c_gpio_cleanup_scl(priv->scl);
656 	gpiod_put(priv->sda);
657 }
658 
659 static const struct of_device_id i2c_gpio_dt_ids[] = {
660 	{ .compatible = "i2c-gpio", },
661 	{ /* sentinel */ }
662 };
663 
664 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
665 
666 static const struct acpi_device_id i2c_gpio_acpi_match[] = {
667 	{ "LOON0005" }, /* LoongArch */
668 	{ }
669 };
670 MODULE_DEVICE_TABLE(acpi, i2c_gpio_acpi_match);
671 
672 static struct platform_driver i2c_gpio_driver = {
673 	.driver		= {
674 		.name	= "i2c-gpio",
675 		.of_match_table	= i2c_gpio_dt_ids,
676 		.acpi_match_table = i2c_gpio_acpi_match,
677 	},
678 	.probe		= i2c_gpio_probe,
679 	.remove		= i2c_gpio_remove,
680 };
681 
i2c_gpio_init(void)682 static int __init i2c_gpio_init(void)
683 {
684 	int ret;
685 
686 	ret = platform_driver_register(&i2c_gpio_driver);
687 	if (ret)
688 		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
689 
690 	return ret;
691 }
692 subsys_initcall(i2c_gpio_init);
693 
i2c_gpio_exit(void)694 static void __exit i2c_gpio_exit(void)
695 {
696 	platform_driver_unregister(&i2c_gpio_driver);
697 }
698 module_exit(i2c_gpio_exit);
699 
700 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
701 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
702 MODULE_LICENSE("GPL v2");
703 MODULE_ALIAS("platform:i2c-gpio");
704