Skip to content

Commit d88ae29

Browse files
svenpeter42wsakernel
authored andcommitted
i2c: pasemi: Add Apple platform driver
With all the previous preparations we can now finally add the platform driver to support the PASemi-based controllers in Apple SoCs. This does not work on the M1 yet but should work on the early iPhones already. Reviewed-by: Arnd Bergmann <[email protected]> Signed-off-by: Sven Peter <[email protected]> Acked-by: Olof Johansson <[email protected]> Tested-by: Christian Zigotzky <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent a2c34bf commit d88ae29

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

drivers/i2c/busses/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,17 @@ config I2C_PASEMI
859859
help
860860
Supports the PA Semi PWRficient on-chip SMBus interfaces.
861861

862+
config I2C_APPLE
863+
tristate "Apple SMBus platform driver"
864+
depends on ARCH_APPLE || COMPILE_TEST
865+
default ARCH_APPLE
866+
help
867+
Say Y here if you want to use the I2C controller present on Apple
868+
Silicon chips such as the M1.
869+
870+
This driver can also be built as a module. If so, the module
871+
will be called i2c-apple.
872+
862873
config I2C_PCA_PLATFORM
863874
tristate "PCA9564/PCA9665 as platform device"
864875
select I2C_ALGOPCA

drivers/i2c/busses/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ obj-$(CONFIG_I2C_OMAP) += i2c-omap.o
8686
obj-$(CONFIG_I2C_OWL) += i2c-owl.o
8787
i2c-pasemi-objs := i2c-pasemi-core.o i2c-pasemi-pci.o
8888
obj-$(CONFIG_I2C_PASEMI) += i2c-pasemi.o
89+
i2c-apple-objs := i2c-pasemi-core.o i2c-pasemi-platform.o
90+
obj-$(CONFIG_I2C_APPLE) += i2c-apple.o
8991
obj-$(CONFIG_I2C_PCA_PLATFORM) += i2c-pca-platform.o
9092
obj-$(CONFIG_I2C_PNX) += i2c-pnx.o
9193
obj-$(CONFIG_I2C_PXA) += i2c-pxa.o
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2021 The Asahi Linux Contributors
4+
*
5+
* PA Semi PWRficient SMBus host driver for Apple SoCs
6+
*/
7+
8+
#include <linux/clk.h>
9+
#include <linux/i2c.h>
10+
#include <linux/io.h>
11+
#include <linux/kernel.h>
12+
#include <linux/module.h>
13+
#include <linux/platform_device.h>
14+
#include <linux/types.h>
15+
16+
#include "i2c-pasemi-core.h"
17+
18+
struct pasemi_platform_i2c_data {
19+
struct pasemi_smbus smbus;
20+
struct clk *clk_ref;
21+
};
22+
23+
static int
24+
pasemi_platform_i2c_calc_clk_div(struct pasemi_platform_i2c_data *data,
25+
u32 frequency)
26+
{
27+
unsigned long clk_rate = clk_get_rate(data->clk_ref);
28+
29+
if (!clk_rate)
30+
return -EINVAL;
31+
32+
data->smbus.clk_div = DIV_ROUND_UP(clk_rate, 16 * frequency);
33+
if (data->smbus.clk_div < 4)
34+
return dev_err_probe(data->smbus.dev, -EINVAL,
35+
"Bus frequency %d is too fast.\n",
36+
frequency);
37+
if (data->smbus.clk_div > 0xff)
38+
return dev_err_probe(data->smbus.dev, -EINVAL,
39+
"Bus frequency %d is too slow.\n",
40+
frequency);
41+
42+
return 0;
43+
}
44+
45+
static int pasemi_platform_i2c_probe(struct platform_device *pdev)
46+
{
47+
struct device *dev = &pdev->dev;
48+
struct pasemi_platform_i2c_data *data;
49+
struct pasemi_smbus *smbus;
50+
u32 frequency;
51+
int error;
52+
53+
data = devm_kzalloc(dev, sizeof(struct pasemi_platform_i2c_data),
54+
GFP_KERNEL);
55+
if (!data)
56+
return -ENOMEM;
57+
58+
smbus = &data->smbus;
59+
smbus->dev = dev;
60+
61+
smbus->ioaddr = devm_platform_ioremap_resource(pdev, 0);
62+
if (IS_ERR(smbus->ioaddr))
63+
return PTR_ERR(smbus->ioaddr);
64+
65+
if (of_property_read_u32(dev->of_node, "clock-frequency", &frequency))
66+
frequency = I2C_MAX_STANDARD_MODE_FREQ;
67+
68+
data->clk_ref = devm_clk_get(dev, NULL);
69+
if (IS_ERR(data->clk_ref))
70+
return PTR_ERR(data->clk_ref);
71+
72+
error = clk_prepare_enable(data->clk_ref);
73+
if (error)
74+
return error;
75+
76+
error = pasemi_platform_i2c_calc_clk_div(data, frequency);
77+
if (error)
78+
goto out_clk_disable;
79+
80+
smbus->adapter.dev.of_node = pdev->dev.of_node;
81+
error = pasemi_i2c_common_probe(smbus);
82+
if (error)
83+
goto out_clk_disable;
84+
85+
platform_set_drvdata(pdev, data);
86+
87+
return 0;
88+
89+
out_clk_disable:
90+
clk_disable_unprepare(data->clk_ref);
91+
92+
return error;
93+
}
94+
95+
static int pasemi_platform_i2c_remove(struct platform_device *pdev)
96+
{
97+
struct pasemi_platform_i2c_data *data = platform_get_drvdata(pdev);
98+
99+
clk_disable_unprepare(data->clk_ref);
100+
return 0;
101+
}
102+
103+
static const struct of_device_id pasemi_platform_i2c_of_match[] = {
104+
{ .compatible = "apple,t8103-i2c" },
105+
{ .compatible = "apple,i2c" },
106+
{},
107+
};
108+
MODULE_DEVICE_TABLE(of, pasemi_platform_i2c_of_match);
109+
110+
static struct platform_driver pasemi_platform_i2c_driver = {
111+
.driver = {
112+
.name = "i2c-apple",
113+
.of_match_table = pasemi_platform_i2c_of_match,
114+
},
115+
.probe = pasemi_platform_i2c_probe,
116+
.remove = pasemi_platform_i2c_remove,
117+
};
118+
module_platform_driver(pasemi_platform_i2c_driver);
119+
120+
MODULE_LICENSE("GPL");
121+
MODULE_AUTHOR("Sven Peter <[email protected]>");
122+
MODULE_DESCRIPTION("Apple/PASemi SMBus platform driver");

0 commit comments

Comments
 (0)