Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / i2c / busses / i2c-versatile.c
blob081d9578ce103ac90ddb1143fa8279082151ea1a
1 /*
2 * i2c-versatile.c
4 * Copyright (C) 2006 ARM Ltd.
5 * written by Russell King, Deep Blue Solutions Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/i2c-algo-bit.h>
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
18 #include <asm/io.h>
20 #define I2C_CONTROL 0x00
21 #define I2C_CONTROLS 0x00
22 #define I2C_CONTROLC 0x04
23 #define SCL (1 << 0)
24 #define SDA (1 << 1)
26 struct i2c_versatile {
27 struct i2c_adapter adap;
28 struct i2c_algo_bit_data algo;
29 void __iomem *base;
32 static void i2c_versatile_setsda(void *data, int state)
34 struct i2c_versatile *i2c = data;
36 writel(SDA, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
39 static void i2c_versatile_setscl(void *data, int state)
41 struct i2c_versatile *i2c = data;
43 writel(SCL, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
46 static int i2c_versatile_getsda(void *data)
48 struct i2c_versatile *i2c = data;
49 return !!(readl(i2c->base + I2C_CONTROL) & SDA);
52 static int i2c_versatile_getscl(void *data)
54 struct i2c_versatile *i2c = data;
55 return !!(readl(i2c->base + I2C_CONTROL) & SCL);
58 static struct i2c_algo_bit_data i2c_versatile_algo = {
59 .setsda = i2c_versatile_setsda,
60 .setscl = i2c_versatile_setscl,
61 .getsda = i2c_versatile_getsda,
62 .getscl = i2c_versatile_getscl,
63 .udelay = 30,
64 .timeout = HZ,
67 static int i2c_versatile_probe(struct platform_device *dev)
69 struct i2c_versatile *i2c;
70 struct resource *r;
71 int ret;
73 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
74 if (!r) {
75 ret = -EINVAL;
76 goto err_out;
79 if (!request_mem_region(r->start, r->end - r->start + 1, "versatile-i2c")) {
80 ret = -EBUSY;
81 goto err_out;
84 i2c = kzalloc(sizeof(struct i2c_versatile), GFP_KERNEL);
85 if (!i2c) {
86 ret = -ENOMEM;
87 goto err_release;
90 i2c->base = ioremap(r->start, r->end - r->start + 1);
91 if (!i2c->base) {
92 ret = -ENOMEM;
93 goto err_free;
96 writel(SCL | SDA, i2c->base + I2C_CONTROLS);
98 i2c->adap.owner = THIS_MODULE;
99 strlcpy(i2c->adap.name, "Versatile I2C adapter", sizeof(i2c->adap.name));
100 i2c->adap.algo_data = &i2c->algo;
101 i2c->adap.dev.parent = &dev->dev;
102 i2c->algo = i2c_versatile_algo;
103 i2c->algo.data = i2c;
105 ret = i2c_bit_add_bus(&i2c->adap);
106 if (ret >= 0) {
107 platform_set_drvdata(dev, i2c);
108 return 0;
111 iounmap(i2c->base);
112 err_free:
113 kfree(i2c);
114 err_release:
115 release_mem_region(r->start, r->end - r->start + 1);
116 err_out:
117 return ret;
120 static int i2c_versatile_remove(struct platform_device *dev)
122 struct i2c_versatile *i2c = platform_get_drvdata(dev);
124 platform_set_drvdata(dev, NULL);
126 i2c_del_adapter(&i2c->adap);
127 return 0;
130 static struct platform_driver i2c_versatile_driver = {
131 .probe = i2c_versatile_probe,
132 .remove = i2c_versatile_remove,
133 .driver = {
134 .name = "versatile-i2c",
135 .owner = THIS_MODULE,
139 static int __init i2c_versatile_init(void)
141 return platform_driver_register(&i2c_versatile_driver);
144 static void __exit i2c_versatile_exit(void)
146 platform_driver_unregister(&i2c_versatile_driver);
149 module_init(i2c_versatile_init);
150 module_exit(i2c_versatile_exit);
152 MODULE_DESCRIPTION("ARM Versatile I2C bus driver");
153 MODULE_LICENSE("GPL");