Re: [PATCH 09/10] i2c: pasemi: Add Apple platform driver

2021-10-03 Thread Sven Peter
On Wed, Sep 29, 2021, at 22:33, Wolfram Sang wrote:
>>  drivers/i2c/busses/i2c-pasemi-apple.c | 122 ++
>
> Can't we name it 'i2c-pasemi-platform.c' instead? Makes more sense to me
> because the other instance is named -pci.

Sure, that's more consistent. I'll change the filename for v2.


Thanks,


Sven


Re: [PATCH 09/10] i2c: pasemi: Add Apple platform driver

2021-09-29 Thread Wolfram Sang

>  drivers/i2c/busses/i2c-pasemi-apple.c | 122 ++

Can't we name it 'i2c-pasemi-platform.c' instead? Makes more sense to me
because the other instance is named -pci.



signature.asc
Description: PGP signature


[PATCH 09/10] i2c: pasemi: Add Apple platform driver

2021-09-26 Thread Sven Peter
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.

Signed-off-by: Sven Peter 
---
 MAINTAINERS   |   1 +
 drivers/i2c/busses/Kconfig|  11 +++
 drivers/i2c/busses/Makefile   |   2 +
 drivers/i2c/busses/i2c-pasemi-apple.c | 122 ++
 4 files changed, 136 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-pasemi-apple.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 380a680db92f..6e952158b6e1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1738,6 +1738,7 @@ F:
Documentation/devicetree/bindings/i2c/apple,i2c.yaml
 F: Documentation/devicetree/bindings/interrupt-controller/apple,aic.yaml
 F: Documentation/devicetree/bindings/pinctrl/apple,pinctrl.yaml
 F: arch/arm64/boot/dts/apple/
+F: drivers/i2c/busses/i2c-pasemi-apple.c
 F: drivers/irqchip/irq-apple-aic.c
 F: include/dt-bindings/interrupt-controller/apple-aic.h
 F: include/dt-bindings/pinctrl/apple.h
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index e17790fe35a7..cf4dae07e319 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -856,6 +856,17 @@ config I2C_PASEMI
help
  Supports the PA Semi PWRficient on-chip SMBus interfaces.
 
+config I2C_APPLE
+   tristate "Apple SMBus platform driver"
+   depends on ARCH_APPLE || COMPILE_TEST
+   default ARCH_APPLE
+   help
+ Say Y here if you want to use the I2C controller present on Apple
+ Silicon chips such as the M1.
+
+ This driver can also be built as a module. If so, the module
+ will be called i2c-apple.
+
 config I2C_PCA_PLATFORM
tristate "PCA9564/PCA9665 as platform device"
select I2C_ALGOPCA
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index 0ab1b4cb2228..474fe2c520d0 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -86,6 +86,8 @@ obj-$(CONFIG_I2C_OMAP)+= i2c-omap.o
 obj-$(CONFIG_I2C_OWL)  += i2c-owl.o
 i2c-pasemi-objs := i2c-pasemi-core.o i2c-pasemi-pci.o
 obj-$(CONFIG_I2C_PASEMI)   += i2c-pasemi.o
+i2c-apple-objs := i2c-pasemi-core.o i2c-pasemi-apple.o
+obj-$(CONFIG_I2C_APPLE)+= i2c-apple.o
 obj-$(CONFIG_I2C_PCA_PLATFORM) += i2c-pca-platform.o
 obj-$(CONFIG_I2C_PNX)  += i2c-pnx.o
 obj-$(CONFIG_I2C_PXA)  += i2c-pxa.o
diff --git a/drivers/i2c/busses/i2c-pasemi-apple.c 
b/drivers/i2c/busses/i2c-pasemi-apple.c
new file mode 100644
index ..c87f8e516eff
--- /dev/null
+++ b/drivers/i2c/busses/i2c-pasemi-apple.c
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021 The Asahi Linux Contributors
+ *
+ * PA Semi PWRficient SMBus host driver for Apple SoCs
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "i2c-pasemi-core.h"
+
+struct pasemi_apple_i2c_data {
+   struct pasemi_smbus smbus;
+   struct clk *clk_ref;
+   struct pinctrl *pctrl;
+};
+
+static int pasemi_apple_i2c_calc_clk_div(struct pasemi_apple_i2c_data *data,
+u32 frequency)
+{
+   unsigned long clk_rate = clk_get_rate(data->clk_ref);
+
+   if (!clk_rate)
+   return -EINVAL;
+
+   data->smbus.clk_div = DIV_ROUND_UP(clk_rate, 16 * frequency);
+   if (data->smbus.clk_div < 4)
+   return dev_err_probe(data->smbus.dev, -EINVAL,
+"Bus frequency %d is too fast.\n",
+frequency);
+   if (data->smbus.clk_div > 0xff)
+   return dev_err_probe(data->smbus.dev, -EINVAL,
+"Bus frequency %d is too slow.\n",
+frequency);
+
+   return 0;
+}
+
+static int pasemi_apple_i2c_probe(struct platform_device *pdev)
+{
+   struct device *dev = &pdev->dev;
+   struct pasemi_apple_i2c_data *data;
+   struct pasemi_smbus *smbus;
+   u32 frequency;
+   int error;
+
+   data = devm_kzalloc(dev, sizeof(struct pasemi_apple_i2c_data),
+   GFP_KERNEL);
+   if (!data)
+   return -ENOMEM;
+
+   smbus = &data->smbus;
+   smbus->dev = dev;
+
+   smbus->ioaddr = devm_platform_ioremap_resource(pdev, 0);
+   if (IS_ERR(smbus->ioaddr))
+   return PTR_ERR(smbus->ioaddr);
+
+   if (of_property_read_u32(dev->of_node, "clock-frequency", &frequency))
+   frequency = I2C_MAX_STANDARD_MODE_FREQ;
+
+   data->clk_ref = devm_clk_get(dev, NULL);
+   if (IS_ERR(data->clk_ref))
+   return PTR_ERR(data->clk_ref);
+
+   error = clk_prepare_enable(data->clk_ref);
+   if (error)
+   return error;
+
+   error = pasemi_apple_i2c_calc_cl