Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  i2c-versatile.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 2006 ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  written by Russell King, Deep Blue Solutions Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/i2c-algo-bit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define I2C_CONTROL	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define I2C_CONTROLS	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define I2C_CONTROLC	0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define SCL		(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define SDA		(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) struct i2c_versatile {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct i2c_adapter	 adap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct i2c_algo_bit_data algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	void __iomem		 *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static void i2c_versatile_setsda(void *data, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct i2c_versatile *i2c = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	writel(SDA, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static void i2c_versatile_setscl(void *data, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct i2c_versatile *i2c = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	writel(SCL, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int i2c_versatile_getsda(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct i2c_versatile *i2c = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return !!(readl(i2c->base + I2C_CONTROL) & SDA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static int i2c_versatile_getscl(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct i2c_versatile *i2c = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	return !!(readl(i2c->base + I2C_CONTROL) & SCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static const struct i2c_algo_bit_data i2c_versatile_algo = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	.setsda	= i2c_versatile_setsda,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	.setscl = i2c_versatile_setscl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	.getsda	= i2c_versatile_getsda,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	.getscl = i2c_versatile_getscl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	.udelay	= 30,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	.timeout = HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static int i2c_versatile_probe(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct i2c_versatile *i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct resource *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	i2c = devm_kzalloc(&dev->dev, sizeof(struct i2c_versatile), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (!i2c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	r = platform_get_resource(dev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	i2c->base = devm_ioremap_resource(&dev->dev, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (IS_ERR(i2c->base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		return PTR_ERR(i2c->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	writel(SCL | SDA, i2c->base + I2C_CONTROLS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	i2c->adap.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	strlcpy(i2c->adap.name, "Versatile I2C adapter", sizeof(i2c->adap.name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	i2c->adap.algo_data = &i2c->algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	i2c->adap.dev.parent = &dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	i2c->adap.dev.of_node = dev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	i2c->algo = i2c_versatile_algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	i2c->algo.data = i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	i2c->adap.nr = dev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	ret = i2c_bit_add_numbered_bus(&i2c->adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	platform_set_drvdata(dev, i2c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static int i2c_versatile_remove(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct i2c_versatile *i2c = platform_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	i2c_del_adapter(&i2c->adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static const struct of_device_id i2c_versatile_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	{ .compatible = "arm,versatile-i2c", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) MODULE_DEVICE_TABLE(of, i2c_versatile_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static struct platform_driver i2c_versatile_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	.probe		= i2c_versatile_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	.remove		= i2c_versatile_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		.name	= "versatile-i2c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		.of_match_table = i2c_versatile_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int __init i2c_versatile_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return platform_driver_register(&i2c_versatile_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static void __exit i2c_versatile_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	platform_driver_unregister(&i2c_versatile_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) subsys_initcall(i2c_versatile_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) module_exit(i2c_versatile_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) MODULE_DESCRIPTION("ARM Versatile I2C bus driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) MODULE_ALIAS("platform:versatile-i2c");