i2c: Don't let i2c adapters declare I2C_CLASS_SPD support if they support I2C_CLASS_HWMON
[linux-block.git] / drivers / i2c / busses / i2c-gpio.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
1c23af90
HS
2/*
3 * Bitbanging I2C bus driver using the GPIO API
4 *
5 * Copyright (C) 2007 Atmel Corporation
1c23af90 6 */
63e57b6f 7#include <linux/completion.h>
14911c6f
WS
8#include <linux/debugfs.h>
9#include <linux/delay.h>
2f8aa465 10#include <linux/gpio/consumer.h>
1c23af90 11#include <linux/i2c-algo-bit.h>
2f8aa465 12#include <linux/i2c.h>
1c23af90 13#include <linux/init.h>
63e57b6f 14#include <linux/interrupt.h>
1c23af90 15#include <linux/module.h>
2f8aa465
WS
16#include <linux/platform_data/i2c-gpio.h>
17#include <linux/platform_device.h>
7b6e9dc7 18#include <linux/property.h>
2f8aa465 19#include <linux/slab.h>
8ffaa0f4
JCPV
20
21struct i2c_gpio_private_data {
b2e63555
LW
22 struct gpio_desc *sda;
23 struct gpio_desc *scl;
8ffaa0f4
JCPV
24 struct i2c_adapter adap;
25 struct i2c_algo_bit_data bit_data;
26 struct i2c_gpio_platform_data pdata;
14911c6f
WS
27#ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
28 struct dentry *debug_dir;
63e57b6f
WS
29 /* these must be protected by bus lock */
30 struct completion scl_irq_completion;
31 u64 scl_irq_data;
14911c6f 32#endif
8ffaa0f4 33};
1c23af90 34
1c23af90
HS
35/*
36 * Toggle SDA by changing the output value of the pin. This is only
37 * valid for pins configured as open drain (i.e. setting the value
38 * high effectively turns off the output driver.)
39 */
40static void i2c_gpio_setsda_val(void *data, int state)
41{
b2e63555 42 struct i2c_gpio_private_data *priv = data;
1c23af90 43
f11a0446 44 gpiod_set_value_cansleep(priv->sda, state);
1c23af90
HS
45}
46
1c23af90
HS
47/*
48 * Toggle SCL by changing the output value of the pin. This is used
49 * for pins that are configured as open drain and for output-only
50 * pins. The latter case will break the i2c protocol, but it will
51 * often work in practice.
52 */
53static void i2c_gpio_setscl_val(void *data, int state)
54{
b2e63555 55 struct i2c_gpio_private_data *priv = data;
1c23af90 56
f11a0446 57 gpiod_set_value_cansleep(priv->scl, state);
1c23af90
HS
58}
59
4d6ceed4 60static int i2c_gpio_getsda(void *data)
1c23af90 61{
b2e63555 62 struct i2c_gpio_private_data *priv = data;
1c23af90 63
f11a0446 64 return gpiod_get_value_cansleep(priv->sda);
1c23af90
HS
65}
66
4d6ceed4 67static int i2c_gpio_getscl(void *data)
1c23af90 68{
b2e63555 69 struct i2c_gpio_private_data *priv = data;
1c23af90 70
f11a0446 71 return gpiod_get_value_cansleep(priv->scl);
1b295c83
JD
72}
73
14911c6f
WS
74#ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
75static struct dentry *i2c_gpio_debug_dir;
76
77#define setsda(bd, val) ((bd)->setsda((bd)->data, val))
78#define setscl(bd, val) ((bd)->setscl((bd)->data, val))
79#define getsda(bd) ((bd)->getsda((bd)->data))
80#define getscl(bd) ((bd)->getscl((bd)->data))
81
82#define WIRE_ATTRIBUTE(wire) \
3f3a89e1
PR
83static int fops_##wire##_get(void *data, u64 *val) \
84{ \
85 struct i2c_gpio_private_data *priv = data; \
86 \
87 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
88 *val = get##wire(&priv->bit_data); \
89 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
90 return 0; \
91} \
92static int fops_##wire##_set(void *data, u64 val) \
93{ \
94 struct i2c_gpio_private_data *priv = data; \
95 \
96 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
97 set##wire(&priv->bit_data, val); \
98 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
99 return 0; \
100} \
14911c6f
WS
101DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
102
103WIRE_ATTRIBUTE(scl);
104WIRE_ATTRIBUTE(sda);
105
16d55daa
WS
106static void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data *priv,
107 u32 pattern, u8 pattern_size)
14911c6f 108{
14911c6f 109 struct i2c_algo_bit_data *bit_data = &priv->bit_data;
16d55daa 110 int i;
14911c6f 111
3f3a89e1 112 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
14911c6f
WS
113
114 /* START condition */
115 setsda(bit_data, 0);
116 udelay(bit_data->udelay);
117
16d55daa
WS
118 /* Send pattern, request ACK, don't send STOP */
119 for (i = pattern_size - 1; i >= 0; i--) {
14911c6f
WS
120 setscl(bit_data, 0);
121 udelay(bit_data->udelay / 2);
122 setsda(bit_data, (pattern >> i) & 1);
123 udelay((bit_data->udelay + 1) / 2);
124 setscl(bit_data, 1);
125 udelay(bit_data->udelay);
126 }
127
3f3a89e1 128 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER);
16d55daa
WS
129}
130
131static int fops_incomplete_addr_phase_set(void *data, u64 addr)
132{
133 struct i2c_gpio_private_data *priv = data;
134 u32 pattern;
135
136 if (addr > 0x7f)
137 return -EINVAL;
138
139 /* ADDR (7 bit) + RD (1 bit) + Client ACK, keep SDA hi (1 bit) */
140 pattern = (addr << 2) | 3;
141
142 i2c_gpio_incomplete_transfer(priv, pattern, 9);
14911c6f
WS
143
144 return 0;
145}
16d55daa 146DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase, NULL, fops_incomplete_addr_phase_set, "%llu\n");
14911c6f 147
bbe89970
WS
148static int fops_incomplete_write_byte_set(void *data, u64 addr)
149{
150 struct i2c_gpio_private_data *priv = data;
151 u32 pattern;
152
153 if (addr > 0x7f)
154 return -EINVAL;
155
156 /* ADDR (7 bit) + WR (1 bit) + Client ACK (1 bit) */
157 pattern = (addr << 2) | 1;
158 /* 0x00 (8 bit) + Client ACK, keep SDA hi (1 bit) */
159 pattern = (pattern << 9) | 1;
160
161 i2c_gpio_incomplete_transfer(priv, pattern, 18);
162
163 return 0;
164}
165DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte, NULL, fops_incomplete_write_byte_set, "%llu\n");
166
63e57b6f
WS
167static 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
199static 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
212static 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}
229DEFINE_DEBUGFS_ATTRIBUTE(fops_lose_arbitration, NULL, fops_lose_arbitration_set, "%llu\n");
230
bb6bdd51
WS
231static 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
241static 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}
255DEFINE_DEBUGFS_ATTRIBUTE(fops_inject_panic, NULL, fops_inject_panic_set, "%llu\n");
256
14911c6f
WS
257static void i2c_gpio_fault_injector_init(struct platform_device *pdev)
258{
259 struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
260
261 /*
262 * If there will be a debugfs-dir per i2c adapter somewhen, put the
263 * 'fault-injector' dir there. Until then, we have a global dir with
264 * all adapters as subdirs.
265 */
24051338 266 if (!i2c_gpio_debug_dir)
14911c6f 267 i2c_gpio_debug_dir = debugfs_create_dir("i2c-fault-injector", NULL);
14911c6f
WS
268
269 priv->debug_dir = debugfs_create_dir(pdev->name, i2c_gpio_debug_dir);
14911c6f 270
63e57b6f
WS
271 init_completion(&priv->scl_irq_completion);
272
16d55daa
WS
273 debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv->debug_dir,
274 priv, &fops_incomplete_addr_phase);
bbe89970
WS
275 debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv->debug_dir,
276 priv, &fops_incomplete_write_byte);
bb6bdd51
WS
277 if (priv->bit_data.getscl) {
278 debugfs_create_file_unsafe("inject_panic", 0200, priv->debug_dir,
279 priv, &fops_inject_panic);
63e57b6f
WS
280 debugfs_create_file_unsafe("lose_arbitration", 0200, priv->debug_dir,
281 priv, &fops_lose_arbitration);
bb6bdd51 282 }
c6324fad
WS
283 debugfs_create_file_unsafe("scl", 0600, priv->debug_dir, priv, &fops_scl);
284 debugfs_create_file_unsafe("sda", 0600, priv->debug_dir, priv, &fops_sda);
14911c6f
WS
285}
286
287static void i2c_gpio_fault_injector_exit(struct platform_device *pdev)
288{
289 struct i2c_gpio_private_data *priv = platform_get_drvdata(pdev);
290
291 debugfs_remove_recursive(priv->debug_dir);
292}
293#else
294static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
295static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
296#endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
297
7b6e9dc7
BZ
298/* Get i2c-gpio properties from DT or ACPI table */
299static void i2c_gpio_get_properties(struct device *dev,
300 struct i2c_gpio_platform_data *pdata)
1b295c83
JD
301{
302 u32 reg;
303
7b6e9dc7 304 device_property_read_u32(dev, "i2c-gpio,delay-us", &pdata->udelay);
8ffaa0f4 305
7b6e9dc7 306 if (!device_property_read_u32(dev, "i2c-gpio,timeout-ms", &reg))
8ffaa0f4
JCPV
307 pdata->timeout = msecs_to_jiffies(reg);
308
309 pdata->sda_is_open_drain =
7b6e9dc7 310 device_property_read_bool(dev, "i2c-gpio,sda-open-drain");
8ffaa0f4 311 pdata->scl_is_open_drain =
7b6e9dc7 312 device_property_read_bool(dev, "i2c-gpio,scl-open-drain");
8ffaa0f4 313 pdata->scl_is_output_only =
7b6e9dc7 314 device_property_read_bool(dev, "i2c-gpio,scl-output-only");
8786b095
HK
315 pdata->sda_is_output_only =
316 device_property_read_bool(dev, "i2c-gpio,sda-output-only");
317 pdata->sda_has_no_pullup =
318 device_property_read_bool(dev, "i2c-gpio,sda-has-no-pullup");
319 pdata->scl_has_no_pullup =
320 device_property_read_bool(dev, "i2c-gpio,scl-has-no-pullup");
8ffaa0f4
JCPV
321}
322
05c74778
LW
323static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
324 const char *con_id,
325 unsigned int index,
326 enum gpiod_flags gflags)
327{
328 struct gpio_desc *retdesc;
329 int ret;
330
331 retdesc = devm_gpiod_get(dev, con_id, gflags);
332 if (!IS_ERR(retdesc)) {
333 dev_dbg(dev, "got GPIO from name %s\n", con_id);
334 return retdesc;
335 }
336
337 retdesc = devm_gpiod_get_index(dev, NULL, index, gflags);
338 if (!IS_ERR(retdesc)) {
339 dev_dbg(dev, "got GPIO from index %u\n", index);
340 return retdesc;
341 }
342
343 ret = PTR_ERR(retdesc);
344
345 /* FIXME: hack in the old code, is this really necessary? */
346 if (ret == -EINVAL)
347 retdesc = ERR_PTR(-EPROBE_DEFER);
348
349 /* This happens if the GPIO driver is not yet probed, let's defer */
350 if (ret == -ENOENT)
351 retdesc = ERR_PTR(-EPROBE_DEFER);
352
3747cd2e 353 if (PTR_ERR(retdesc) != -EPROBE_DEFER)
05c74778
LW
354 dev_err(dev, "error trying to get descriptor: %d\n", ret);
355
356 return retdesc;
357}
358
0b255e92 359static int i2c_gpio_probe(struct platform_device *pdev)
1c23af90 360{
8ffaa0f4 361 struct i2c_gpio_private_data *priv;
1c23af90
HS
362 struct i2c_gpio_platform_data *pdata;
363 struct i2c_algo_bit_data *bit_data;
364 struct i2c_adapter *adap;
b9ab0517 365 struct device *dev = &pdev->dev;
7b6e9dc7 366 struct fwnode_handle *fwnode = dev_fwnode(dev);
7bb75029 367 enum gpiod_flags gflags;
1c23af90
HS
368 int ret;
369
b9ab0517 370 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
b2e63555
LW
371 if (!priv)
372 return -ENOMEM;
1c23af90 373
1b295c83
JD
374 adap = &priv->adap;
375 bit_data = &priv->bit_data;
376 pdata = &priv->pdata;
377
7b6e9dc7
BZ
378 if (fwnode) {
379 i2c_gpio_get_properties(dev, pdata);
1b295c83 380 } else {
b2e63555
LW
381 /*
382 * If all platform data settings are zero it is OK
383 * to not provide any platform data from the board.
384 */
b9ab0517
LW
385 if (dev_get_platdata(dev))
386 memcpy(pdata, dev_get_platdata(dev), sizeof(*pdata));
1b295c83 387 }
1c23af90 388
b2e63555 389 /*
7bb75029 390 * First get the GPIO pins; if it fails, we'll defer the probe.
60c1d560
WS
391 * If the SCL/SDA lines are marked "open drain" by platform data or
392 * device tree then this means that something outside of our control is
393 * marking these lines to be handled as open drain, and we should just
394 * handle them as we handle any other output. Else we enforce open
395 * drain as this is required for an I2C bus.
b2e63555 396 */
8786b095 397 if (pdata->sda_is_open_drain || pdata->sda_has_no_pullup)
7bb75029
LW
398 gflags = GPIOD_OUT_HIGH;
399 else
400 gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
05c74778
LW
401 priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
402 if (IS_ERR(priv->sda))
403 return PTR_ERR(priv->sda);
404
8786b095 405 if (pdata->scl_is_open_drain || pdata->scl_has_no_pullup)
12b731dd 406 gflags = GPIOD_OUT_HIGH;
7bb75029 407 else
12b731dd 408 gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
05c74778
LW
409 priv->scl = i2c_gpio_get_desc(dev, "scl", 1, gflags);
410 if (IS_ERR(priv->scl))
411 return PTR_ERR(priv->scl);
1c23af90 412
f11a0446
JK
413 if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl))
414 dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
e155e388
WS
415 else
416 bit_data->can_do_atomic = true;
f11a0446 417
7bb75029
LW
418 bit_data->setsda = i2c_gpio_setsda_val;
419 bit_data->setscl = i2c_gpio_setscl_val;
420
1c23af90
HS
421 if (!pdata->scl_is_output_only)
422 bit_data->getscl = i2c_gpio_getscl;
8786b095
HK
423 if (!pdata->sda_is_output_only)
424 bit_data->getsda = i2c_gpio_getsda;
1c23af90
HS
425
426 if (pdata->udelay)
427 bit_data->udelay = pdata->udelay;
428 else if (pdata->scl_is_output_only)
429 bit_data->udelay = 50; /* 10 kHz */
430 else
431 bit_data->udelay = 5; /* 100 kHz */
432
433 if (pdata->timeout)
434 bit_data->timeout = pdata->timeout;
435 else
436 bit_data->timeout = HZ / 10; /* 100 ms */
437
b2e63555 438 bit_data->data = priv;
1c23af90
HS
439
440 adap->owner = THIS_MODULE;
7b6e9dc7 441 if (fwnode)
ea1558ce 442 strscpy(adap->name, dev_name(dev), sizeof(adap->name));
58a7371a
BS
443 else
444 snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
445
1c23af90 446 adap->algo_data = bit_data;
9fd12f38 447 adap->class = I2C_CLASS_HWMON;
b9ab0517 448 adap->dev.parent = dev;
7b6e9dc7 449 device_set_node(&adap->dev, fwnode);
1c23af90 450
44454baa 451 adap->nr = pdev->id;
7e69c3ac 452 ret = i2c_bit_add_numbered_bus(adap);
1c23af90 453 if (ret)
a0682a31 454 return ret;
1c23af90 455
8ffaa0f4 456 platform_set_drvdata(pdev, priv);
1c23af90 457
b2e63555
LW
458 /*
459 * FIXME: using global GPIO numbers is not helpful. If/when we
460 * get accessors to get the actual name of the GPIO line,
461 * from the descriptor, then provide that instead.
462 */
b9ab0517 463 dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
b2e63555 464 desc_to_gpio(priv->sda), desc_to_gpio(priv->scl),
1c23af90
HS
465 pdata->scl_is_output_only
466 ? ", no clock stretching" : "");
467
14911c6f
WS
468 i2c_gpio_fault_injector_init(pdev);
469
1c23af90 470 return 0;
1c23af90
HS
471}
472
e190a0c3 473static void i2c_gpio_remove(struct platform_device *pdev)
1c23af90 474{
8ffaa0f4 475 struct i2c_gpio_private_data *priv;
1c23af90
HS
476 struct i2c_adapter *adap;
477
14911c6f
WS
478 i2c_gpio_fault_injector_exit(pdev);
479
8ffaa0f4
JCPV
480 priv = platform_get_drvdata(pdev);
481 adap = &priv->adap;
1c23af90
HS
482
483 i2c_del_adapter(adap);
1c23af90
HS
484}
485
8ffaa0f4
JCPV
486static const struct of_device_id i2c_gpio_dt_ids[] = {
487 { .compatible = "i2c-gpio", },
488 { /* sentinel */ }
489};
490
491MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
8ffaa0f4 492
7b6e9dc7
BZ
493static const struct acpi_device_id i2c_gpio_acpi_match[] = {
494 { "LOON0005" }, /* LoongArch */
495 { }
496};
497MODULE_DEVICE_TABLE(acpi, i2c_gpio_acpi_match);
498
1c23af90
HS
499static struct platform_driver i2c_gpio_driver = {
500 .driver = {
501 .name = "i2c-gpio",
73e9841b 502 .of_match_table = i2c_gpio_dt_ids,
7b6e9dc7 503 .acpi_match_table = i2c_gpio_acpi_match,
1c23af90 504 },
1efe7c55 505 .probe = i2c_gpio_probe,
e190a0c3 506 .remove_new = i2c_gpio_remove,
1c23af90
HS
507};
508
509static int __init i2c_gpio_init(void)
510{
511 int ret;
512
1efe7c55 513 ret = platform_driver_register(&i2c_gpio_driver);
1c23af90
HS
514 if (ret)
515 printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
516
517 return ret;
518}
b8680784 519subsys_initcall(i2c_gpio_init);
1c23af90
HS
520
521static void __exit i2c_gpio_exit(void)
522{
523 platform_driver_unregister(&i2c_gpio_driver);
524}
525module_exit(i2c_gpio_exit);
526
e05503ef 527MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1c23af90 528MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
61adf63a 529MODULE_LICENSE("GPL v2");
add8eda7 530MODULE_ALIAS("platform:i2c-gpio");