Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  i2c-versatile.c
0004  *
0005  *  Copyright (C) 2006 ARM Ltd.
0006  *  written by Russell King, Deep Blue Solutions Ltd.
0007  */
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/i2c.h>
0011 #include <linux/i2c-algo-bit.h>
0012 #include <linux/init.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/slab.h>
0015 #include <linux/io.h>
0016 
0017 #define I2C_CONTROL 0x00
0018 #define I2C_CONTROLS    0x00
0019 #define I2C_CONTROLC    0x04
0020 #define SCL     (1 << 0)
0021 #define SDA     (1 << 1)
0022 
0023 struct i2c_versatile {
0024     struct i2c_adapter   adap;
0025     struct i2c_algo_bit_data algo;
0026     void __iomem         *base;
0027 };
0028 
0029 static void i2c_versatile_setsda(void *data, int state)
0030 {
0031     struct i2c_versatile *i2c = data;
0032 
0033     writel(SDA, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
0034 }
0035 
0036 static void i2c_versatile_setscl(void *data, int state)
0037 {
0038     struct i2c_versatile *i2c = data;
0039 
0040     writel(SCL, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
0041 }
0042 
0043 static int i2c_versatile_getsda(void *data)
0044 {
0045     struct i2c_versatile *i2c = data;
0046     return !!(readl(i2c->base + I2C_CONTROL) & SDA);
0047 }
0048 
0049 static int i2c_versatile_getscl(void *data)
0050 {
0051     struct i2c_versatile *i2c = data;
0052     return !!(readl(i2c->base + I2C_CONTROL) & SCL);
0053 }
0054 
0055 static const struct i2c_algo_bit_data i2c_versatile_algo = {
0056     .setsda = i2c_versatile_setsda,
0057     .setscl = i2c_versatile_setscl,
0058     .getsda = i2c_versatile_getsda,
0059     .getscl = i2c_versatile_getscl,
0060     .udelay = 30,
0061     .timeout = HZ,
0062 };
0063 
0064 static int i2c_versatile_probe(struct platform_device *dev)
0065 {
0066     struct i2c_versatile *i2c;
0067     struct resource *r;
0068     int ret;
0069 
0070     i2c = devm_kzalloc(&dev->dev, sizeof(struct i2c_versatile), GFP_KERNEL);
0071     if (!i2c)
0072         return -ENOMEM;
0073 
0074     r = platform_get_resource(dev, IORESOURCE_MEM, 0);
0075     i2c->base = devm_ioremap_resource(&dev->dev, r);
0076     if (IS_ERR(i2c->base))
0077         return PTR_ERR(i2c->base);
0078 
0079     writel(SCL | SDA, i2c->base + I2C_CONTROLS);
0080 
0081     i2c->adap.owner = THIS_MODULE;
0082     strscpy(i2c->adap.name, "Versatile I2C adapter", sizeof(i2c->adap.name));
0083     i2c->adap.algo_data = &i2c->algo;
0084     i2c->adap.dev.parent = &dev->dev;
0085     i2c->adap.dev.of_node = dev->dev.of_node;
0086     i2c->algo = i2c_versatile_algo;
0087     i2c->algo.data = i2c;
0088 
0089     i2c->adap.nr = dev->id;
0090     ret = i2c_bit_add_numbered_bus(&i2c->adap);
0091     if (ret < 0)
0092         return ret;
0093 
0094     platform_set_drvdata(dev, i2c);
0095 
0096     return 0;
0097 }
0098 
0099 static int i2c_versatile_remove(struct platform_device *dev)
0100 {
0101     struct i2c_versatile *i2c = platform_get_drvdata(dev);
0102 
0103     i2c_del_adapter(&i2c->adap);
0104     return 0;
0105 }
0106 
0107 static const struct of_device_id i2c_versatile_match[] = {
0108     { .compatible = "arm,versatile-i2c", },
0109     {},
0110 };
0111 MODULE_DEVICE_TABLE(of, i2c_versatile_match);
0112 
0113 static struct platform_driver i2c_versatile_driver = {
0114     .probe      = i2c_versatile_probe,
0115     .remove     = i2c_versatile_remove,
0116     .driver     = {
0117         .name   = "versatile-i2c",
0118         .of_match_table = i2c_versatile_match,
0119     },
0120 };
0121 
0122 static int __init i2c_versatile_init(void)
0123 {
0124     return platform_driver_register(&i2c_versatile_driver);
0125 }
0126 
0127 static void __exit i2c_versatile_exit(void)
0128 {
0129     platform_driver_unregister(&i2c_versatile_driver);
0130 }
0131 
0132 subsys_initcall(i2c_versatile_init);
0133 module_exit(i2c_versatile_exit);
0134 
0135 MODULE_DESCRIPTION("ARM Versatile I2C bus driver");
0136 MODULE_LICENSE("GPL");
0137 MODULE_ALIAS("platform:versatile-i2c");