Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * i2c-versatile.c
4 *
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>
16
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)
22
23struct i2c_versatile {
24 struct i2c_adapter adap;
25 struct i2c_algo_bit_data algo;
26 void __iomem *base;
27};
28
29static void i2c_versatile_setsda(void *data, int state)
30{
31 struct i2c_versatile *i2c = data;
32
33 writel(SDA, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
34}
35
36static void i2c_versatile_setscl(void *data, int state)
37{
38 struct i2c_versatile *i2c = data;
39
40 writel(SCL, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
41}
42
43static int i2c_versatile_getsda(void *data)
44{
45 struct i2c_versatile *i2c = data;
46 return !!(readl(i2c->base + I2C_CONTROL) & SDA);
47}
48
49static int i2c_versatile_getscl(void *data)
50{
51 struct i2c_versatile *i2c = data;
52 return !!(readl(i2c->base + I2C_CONTROL) & SCL);
53}
54
55static 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,
62};
63
64static int i2c_versatile_probe(struct platform_device *dev)
65{
66 struct i2c_versatile *i2c;
67 int ret;
68
69 i2c = devm_kzalloc(&dev->dev, sizeof(struct i2c_versatile), GFP_KERNEL);
70 if (!i2c)
71 return -ENOMEM;
72
73 i2c->base = devm_platform_get_and_ioremap_resource(dev, 0, NULL);
74 if (IS_ERR(i2c->base))
75 return PTR_ERR(i2c->base);
76
77 writel(SCL | SDA, i2c->base + I2C_CONTROLS);
78
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;
86
87 i2c->adap.nr = dev->id;
88 ret = i2c_bit_add_numbered_bus(&i2c->adap);
89 if (ret < 0)
90 return ret;
91
92 platform_set_drvdata(dev, i2c);
93
94 return 0;
95}
96
97static void i2c_versatile_remove(struct platform_device *dev)
98{
99 struct i2c_versatile *i2c = platform_get_drvdata(dev);
100
101 i2c_del_adapter(&i2c->adap);
102}
103
104static const struct of_device_id i2c_versatile_match[] = {
105 { .compatible = "arm,versatile-i2c", },
106 {},
107};
108MODULE_DEVICE_TABLE(of, i2c_versatile_match);
109
110static 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,
116 },
117};
118
119static int __init i2c_versatile_init(void)
120{
121 return platform_driver_register(&i2c_versatile_driver);
122}
123
124static void __exit i2c_versatile_exit(void)
125{
126 platform_driver_unregister(&i2c_versatile_driver);
127}
128
129subsys_initcall(i2c_versatile_init);
130module_exit(i2c_versatile_exit);
131
132MODULE_DESCRIPTION("ARM Versatile I2C bus driver");
133MODULE_LICENSE("GPL");
134MODULE_ALIAS("platform:versatile-i2c");
1/*
2 * i2c-versatile.c
3 *
4 * Copyright (C) 2006 ARM Ltd.
5 * written by Russell King, Deep Blue Solutions Ltd.
6 *
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.
10 */
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>
17#include <linux/slab.h>
18#include <linux/io.h>
19
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)
25
26struct i2c_versatile {
27 struct i2c_adapter adap;
28 struct i2c_algo_bit_data algo;
29 void __iomem *base;
30};
31
32static void i2c_versatile_setsda(void *data, int state)
33{
34 struct i2c_versatile *i2c = data;
35
36 writel(SDA, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
37}
38
39static void i2c_versatile_setscl(void *data, int state)
40{
41 struct i2c_versatile *i2c = data;
42
43 writel(SCL, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
44}
45
46static int i2c_versatile_getsda(void *data)
47{
48 struct i2c_versatile *i2c = data;
49 return !!(readl(i2c->base + I2C_CONTROL) & SDA);
50}
51
52static int i2c_versatile_getscl(void *data)
53{
54 struct i2c_versatile *i2c = data;
55 return !!(readl(i2c->base + I2C_CONTROL) & SCL);
56}
57
58static 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,
65};
66
67static int i2c_versatile_probe(struct platform_device *dev)
68{
69 struct i2c_versatile *i2c;
70 struct resource *r;
71 int ret;
72
73 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
74 if (!r) {
75 ret = -EINVAL;
76 goto err_out;
77 }
78
79 if (!request_mem_region(r->start, resource_size(r), "versatile-i2c")) {
80 ret = -EBUSY;
81 goto err_out;
82 }
83
84 i2c = kzalloc(sizeof(struct i2c_versatile), GFP_KERNEL);
85 if (!i2c) {
86 ret = -ENOMEM;
87 goto err_release;
88 }
89
90 i2c->base = ioremap(r->start, resource_size(r));
91 if (!i2c->base) {
92 ret = -ENOMEM;
93 goto err_free;
94 }
95
96 writel(SCL | SDA, i2c->base + I2C_CONTROLS);
97
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->adap.dev.of_node = dev->dev.of_node;
103 i2c->algo = i2c_versatile_algo;
104 i2c->algo.data = i2c;
105
106 i2c->adap.nr = dev->id;
107 ret = i2c_bit_add_numbered_bus(&i2c->adap);
108 if (ret >= 0) {
109 platform_set_drvdata(dev, i2c);
110 return 0;
111 }
112
113 iounmap(i2c->base);
114 err_free:
115 kfree(i2c);
116 err_release:
117 release_mem_region(r->start, resource_size(r));
118 err_out:
119 return ret;
120}
121
122static int i2c_versatile_remove(struct platform_device *dev)
123{
124 struct i2c_versatile *i2c = platform_get_drvdata(dev);
125
126 i2c_del_adapter(&i2c->adap);
127 return 0;
128}
129
130static const struct of_device_id i2c_versatile_match[] = {
131 { .compatible = "arm,versatile-i2c", },
132 {},
133};
134MODULE_DEVICE_TABLE(of, i2c_versatile_match);
135
136static struct platform_driver i2c_versatile_driver = {
137 .probe = i2c_versatile_probe,
138 .remove = i2c_versatile_remove,
139 .driver = {
140 .name = "versatile-i2c",
141 .owner = THIS_MODULE,
142 .of_match_table = i2c_versatile_match,
143 },
144};
145
146static int __init i2c_versatile_init(void)
147{
148 return platform_driver_register(&i2c_versatile_driver);
149}
150
151static void __exit i2c_versatile_exit(void)
152{
153 platform_driver_unregister(&i2c_versatile_driver);
154}
155
156subsys_initcall(i2c_versatile_init);
157module_exit(i2c_versatile_exit);
158
159MODULE_DESCRIPTION("ARM Versatile I2C bus driver");
160MODULE_LICENSE("GPL");
161MODULE_ALIAS("platform:versatile-i2c");