18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  i2c-versatile.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2006 ARM Ltd.
68c2ecf20Sopenharmony_ci *  written by Russell King, Deep Blue Solutions Ltd.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/i2c.h>
118c2ecf20Sopenharmony_ci#include <linux/i2c-algo-bit.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <linux/io.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define I2C_CONTROL	0x00
188c2ecf20Sopenharmony_ci#define I2C_CONTROLS	0x00
198c2ecf20Sopenharmony_ci#define I2C_CONTROLC	0x04
208c2ecf20Sopenharmony_ci#define SCL		(1 << 0)
218c2ecf20Sopenharmony_ci#define SDA		(1 << 1)
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistruct i2c_versatile {
248c2ecf20Sopenharmony_ci	struct i2c_adapter	 adap;
258c2ecf20Sopenharmony_ci	struct i2c_algo_bit_data algo;
268c2ecf20Sopenharmony_ci	void __iomem		 *base;
278c2ecf20Sopenharmony_ci};
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic void i2c_versatile_setsda(void *data, int state)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	struct i2c_versatile *i2c = data;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	writel(SDA, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic void i2c_versatile_setscl(void *data, int state)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct i2c_versatile *i2c = data;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	writel(SCL, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC));
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic int i2c_versatile_getsda(void *data)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct i2c_versatile *i2c = data;
468c2ecf20Sopenharmony_ci	return !!(readl(i2c->base + I2C_CONTROL) & SDA);
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic int i2c_versatile_getscl(void *data)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct i2c_versatile *i2c = data;
528c2ecf20Sopenharmony_ci	return !!(readl(i2c->base + I2C_CONTROL) & SCL);
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic const struct i2c_algo_bit_data i2c_versatile_algo = {
568c2ecf20Sopenharmony_ci	.setsda	= i2c_versatile_setsda,
578c2ecf20Sopenharmony_ci	.setscl = i2c_versatile_setscl,
588c2ecf20Sopenharmony_ci	.getsda	= i2c_versatile_getsda,
598c2ecf20Sopenharmony_ci	.getscl = i2c_versatile_getscl,
608c2ecf20Sopenharmony_ci	.udelay	= 30,
618c2ecf20Sopenharmony_ci	.timeout = HZ,
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic int i2c_versatile_probe(struct platform_device *dev)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	struct i2c_versatile *i2c;
678c2ecf20Sopenharmony_ci	struct resource *r;
688c2ecf20Sopenharmony_ci	int ret;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	i2c = devm_kzalloc(&dev->dev, sizeof(struct i2c_versatile), GFP_KERNEL);
718c2ecf20Sopenharmony_ci	if (!i2c)
728c2ecf20Sopenharmony_ci		return -ENOMEM;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	r = platform_get_resource(dev, IORESOURCE_MEM, 0);
758c2ecf20Sopenharmony_ci	i2c->base = devm_ioremap_resource(&dev->dev, r);
768c2ecf20Sopenharmony_ci	if (IS_ERR(i2c->base))
778c2ecf20Sopenharmony_ci		return PTR_ERR(i2c->base);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	writel(SCL | SDA, i2c->base + I2C_CONTROLS);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	i2c->adap.owner = THIS_MODULE;
828c2ecf20Sopenharmony_ci	strlcpy(i2c->adap.name, "Versatile I2C adapter", sizeof(i2c->adap.name));
838c2ecf20Sopenharmony_ci	i2c->adap.algo_data = &i2c->algo;
848c2ecf20Sopenharmony_ci	i2c->adap.dev.parent = &dev->dev;
858c2ecf20Sopenharmony_ci	i2c->adap.dev.of_node = dev->dev.of_node;
868c2ecf20Sopenharmony_ci	i2c->algo = i2c_versatile_algo;
878c2ecf20Sopenharmony_ci	i2c->algo.data = i2c;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	i2c->adap.nr = dev->id;
908c2ecf20Sopenharmony_ci	ret = i2c_bit_add_numbered_bus(&i2c->adap);
918c2ecf20Sopenharmony_ci	if (ret < 0)
928c2ecf20Sopenharmony_ci		return ret;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	platform_set_drvdata(dev, i2c);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	return 0;
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic int i2c_versatile_remove(struct platform_device *dev)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	struct i2c_versatile *i2c = platform_get_drvdata(dev);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	i2c_del_adapter(&i2c->adap);
1048c2ecf20Sopenharmony_ci	return 0;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic const struct of_device_id i2c_versatile_match[] = {
1088c2ecf20Sopenharmony_ci	{ .compatible = "arm,versatile-i2c", },
1098c2ecf20Sopenharmony_ci	{},
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, i2c_versatile_match);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic struct platform_driver i2c_versatile_driver = {
1148c2ecf20Sopenharmony_ci	.probe		= i2c_versatile_probe,
1158c2ecf20Sopenharmony_ci	.remove		= i2c_versatile_remove,
1168c2ecf20Sopenharmony_ci	.driver		= {
1178c2ecf20Sopenharmony_ci		.name	= "versatile-i2c",
1188c2ecf20Sopenharmony_ci		.of_match_table = i2c_versatile_match,
1198c2ecf20Sopenharmony_ci	},
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic int __init i2c_versatile_init(void)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	return platform_driver_register(&i2c_versatile_driver);
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic void __exit i2c_versatile_exit(void)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	platform_driver_unregister(&i2c_versatile_driver);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cisubsys_initcall(i2c_versatile_init);
1338c2ecf20Sopenharmony_cimodule_exit(i2c_versatile_exit);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ARM Versatile I2C bus driver");
1368c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1378c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:versatile-i2c");
138