Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / i2c / busses / i2c-versatile.c
bloba1ab6ef6f0716df70e47514bc661eb5b3b854728
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * i2c-versatile.c
5 * Copyright (C) 2006 ARM Ltd.
6 * written by Russell King, Deep Blue Solutions Ltd.
7 */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/i2c-algo-bit.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/io.h>
17 #define I2C_CONTROL 0x00
18 #define I2C_CONTROLS 0x00
19 #define I2C_CONTROLC 0x04
20 #define SCL (1 << 0)
21 #define SDA (1 << 1)
23 struct i2c_versatile {
24 struct i2c_adapter adap;
25 struct i2c_algo_bit_data algo;
26 void __iomem *base;
29 static void i2c_versatile_setsda(void *data, int state)
31 struct i2c_versatile *i2c = data;
33 writel(SDA, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
36 static void i2c_versatile_setscl(void *data, int state)
38 struct i2c_versatile *i2c = data;
40 writel(SCL, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
43 static int i2c_versatile_getsda(void *data)
45 struct i2c_versatile *i2c = data;
46 return !!(readl(i2c->base + I2C_CONTROL) & SDA);
49 static int i2c_versatile_getscl(void *data)
51 struct i2c_versatile *i2c = data;
52 return !!(readl(i2c->base + I2C_CONTROL) & SCL);
55 static const struct i2c_algo_bit_data i2c_versatile_algo = {
56 .setsda = i2c_versatile_setsda,
57 .setscl = i2c_versatile_setscl,
58 .getsda = i2c_versatile_getsda,
59 .getscl = i2c_versatile_getscl,
60 .udelay = 30,
61 .timeout = HZ,
64 static int i2c_versatile_probe(struct platform_device *dev)
66 struct i2c_versatile *i2c;
67 int ret;
69 i2c = devm_kzalloc(&dev->dev, sizeof(struct i2c_versatile), GFP_KERNEL);
70 if (!i2c)
71 return -ENOMEM;
73 i2c->base = devm_platform_get_and_ioremap_resource(dev, 0, NULL);
74 if (IS_ERR(i2c->base))
75 return PTR_ERR(i2c->base);
77 writel(SCL | SDA, i2c->base + I2C_CONTROLS);
79 i2c->adap.owner = THIS_MODULE;
80 strscpy(i2c->adap.name, "Versatile I2C adapter", sizeof(i2c->adap.name));
81 i2c->adap.algo_data = &i2c->algo;
82 i2c->adap.dev.parent = &dev->dev;
83 i2c->adap.dev.of_node = dev->dev.of_node;
84 i2c->algo = i2c_versatile_algo;
85 i2c->algo.data = i2c;
87 i2c->adap.nr = dev->id;
88 ret = i2c_bit_add_numbered_bus(&i2c->adap);
89 if (ret < 0)
90 return ret;
92 platform_set_drvdata(dev, i2c);
94 return 0;
97 static void i2c_versatile_remove(struct platform_device *dev)
99 struct i2c_versatile *i2c = platform_get_drvdata(dev);
101 i2c_del_adapter(&i2c->adap);
104 static const struct of_device_id i2c_versatile_match[] = {
105 { .compatible = "arm,versatile-i2c", },
108 MODULE_DEVICE_TABLE(of, i2c_versatile_match);
110 static struct platform_driver i2c_versatile_driver = {
111 .probe = i2c_versatile_probe,
112 .remove = i2c_versatile_remove,
113 .driver = {
114 .name = "versatile-i2c",
115 .of_match_table = i2c_versatile_match,
119 static int __init i2c_versatile_init(void)
121 return platform_driver_register(&i2c_versatile_driver);
124 static void __exit i2c_versatile_exit(void)
126 platform_driver_unregister(&i2c_versatile_driver);
129 subsys_initcall(i2c_versatile_init);
130 module_exit(i2c_versatile_exit);
132 MODULE_DESCRIPTION("ARM Versatile I2C bus driver");
133 MODULE_LICENSE("GPL");
134 MODULE_ALIAS("platform:versatile-i2c");