Skip to contentSkip to conversation

Add Apple M1 support to PASemi i2c driver

PATCH Superseded Sven Peter 4 participants 18 messages Sep 26, 2021 – Oct 4, 2021

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Sven Peter

author
#0 lore raw

cover Add Apple M1 support to PASemi i2c driver

Hi,

This series adds support for the I2C controller found on Apple Silicon Macs which has quite a bit of history:

Apple bought P.A. Semi in 2008 and it looks like a part of its legacy continues to live on in the M1. This controller has actually been used since at least the iPhone 4S and hasn't changed much since then.
Essentially, there are only a few differences that matter:

- The controller no longer is a PCI device
- Starting at some iPhone an additional bit in one register must be set in order to start transmissions.
- The reference clock and hence the clock dividers are different

In order to add support for a platform device I first replaced PCI-specific bits and split out the PCI driver to its own file. Then I added support to make the clock divider configurable and converted the driver to use managed device resources to make it a bit simpler.

The Apple and PASemi driver will never be compiled in the same kernel since the Apple one will run on arm64 while the original PASemi driver will only be useful on powerpc.
I've thus followed the octeon (mips)/thunderx(arm64) approach to do the
split: I created a -core.c file which contains the shared logic and just compile that one for both the PASemi and the new Apple driver.

Now unfortunately I don't have access to any old PASemi hardware and cannot confirm that my changes haven't broken anything for those. I believe Hector was in contact with Olof a few months ago who said that he might still have an old machine on which he could test this.
I'd very much appreciate if he (or anyone else for that matter :-)) could give this series a quick test on the old PASemi machines.

Best,

Sven

Sven Peter (10):
dt-bindings: i2c: Add Apple I2C controller bindings
i2c: pasemi: Use io{read,write}32
i2c: pasemi: Remove usage of pci_dev
i2c: pasemi: Split off common probing code
i2c: pasemi: Split pci driver to its own file
i2c: pasemi: Move common reset code to own function
i2c: pasemi: Allow to configure bus frequency
i2c: pasemi: Refactor _probe to use devm_*
i2c: pasemi: Add Apple platform driver
i2c: pasemi: Set enable bit for Apple variant

 .../devicetree/bindings/i2c/apple,i2c.yaml    |  61 +++++++++
 MAINTAINERS                                   |   2 +
 drivers/i2c/busses/Kconfig                    |  11 ++
 drivers/i2c/busses/Makefile                   |   3 +
 drivers/i2c/busses/i2c-pasemi-apple.c         | 122 ++++++++++++++++++
 .../{i2c-pasemi.c => i2c-pasemi-core.c}       | 114 +++++-----------
 drivers/i2c/busses/i2c-pasemi-core.h          |  21 +++
 drivers/i2c/busses/i2c-pasemi-pci.c           |  85 ++++++++++++
 8 files changed, 334 insertions(+), 85 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/i2c/apple,i2c.yaml
 create mode 100644 drivers/i2c/busses/i2c-pasemi-apple.c
 rename drivers/i2c/busses/{i2c-pasemi.c => i2c-pasemi-core.c} (77%)
 create mode 100644 drivers/i2c/busses/i2c-pasemi-core.h
 create mode 100644 drivers/i2c/busses/i2c-pasemi-pci.c
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/10 dt-bindings: i2c: Add Apple I2C controller bindings

The Apple I2C controller is based on the PASemi I2C controller. It is present on Apple SoCs such as the M1.

---
 .../devicetree/bindings/i2c/apple,i2c.yaml    | 61 +++++++++++++++++++
 MAINTAINERS                                   |  1 +
 2 files changed, 62 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/apple,i2c.yaml
Documentation/devicetree/bindings/i2c/apple,i2c.yaml new file +61 -0
new file mode 100644
index 000000000000..22fc8483256f
@@ -0,0 +1,61 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/i2c/apple,i2c.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: Apple/PASemi I2C controller
+
+maintainers:
+  - Sven Peter <[email protected]>
+
+description: |
+  Apple SoCs such as the M1 come with a I2C controller based on the one found
+  in machines with P. A. Semi's PWRficient processors.
+  The bus is used to communicate with e.g. USB PD chips or the speaker
+  amp.
+
+allOf:
+  - $ref: /schemas/i2c/i2c-controller.yaml#
+
+properties:
+  compatible:
+    enum:
+      - apple,t8103-i2c
+      - apple,i2c
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: I2C bus reference clock
+
+  interrupts:
+    maxItems: 1
+
+  clock-frequency:
+    description: |
+      Desired I2C bus clock frequency in Hz. If not specified, 100 kHz will be
+      used. This frequency is generated by dividing the reference clock.
+      Allowed values are between ref_clk/(16*4) and ref_clk/(16*255).
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - interrupts
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    i2c@35010000 {
+      compatible = "apple,t8103-i2c";
+      reg = <0x35010000 0x4000>;
+      interrupt-parent = <&aic>;
+      interrupts = <0 627 4>;
+      clocks = <&ref_clk>;
+      #address-cells = <1>;
+      #size-cells = <0>;
+    };
MAINTAINERS +1 -0
index 329d3a0a9fdb..380a680db92f 100644
@@ -1734,6 +1734,7 @@ B:	https://github.com/AsahiLinux/linux/issues
 C:	irc://irc.oftc.net/asahi-dev
 T:	git https://github.com/AsahiLinux/linux.git
 F:	Documentation/devicetree/bindings/arm/apple.yaml
+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/
Signature · 1 line
2.25.1
author
#3 lore raw

PATCH 2/10 i2c: pasemi: Use io{read,write}32

In reply to Sven Peter

In preparation for splitting this driver up into a platform_driver and a pci_driver, replace outl/inl usage with ioport_map and ioread32/iowrite32.

---
 drivers/i2c/busses/i2c-pasemi.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-pasemi.c +13 -3
index 20f2772c0e79..dd31d902a621 100644
@@ -20,6 +20,7 @@ static struct pci_driver pasemi_smb_driver;
 struct pasemi_smbus {
 	struct pci_dev		*dev;
 	struct i2c_adapter	 adapter;
+	void __iomem		*ioaddr;
 	unsigned long		 base;
 	int			 size;
 };
@@ -53,13 +54,13 @@ static inline void reg_write(struct pasemi_smbus *smbus, int reg, int val)
 {
 	dev_dbg(&smbus->dev->dev, "smbus write reg %lx val %08x\n",
 		smbus->base + reg, val);
-	outl(val, smbus->base + reg);
+	iowrite32(val, smbus->ioaddr + reg);
 }
 
 static inline int reg_read(struct pasemi_smbus *smbus, int reg)
 {
 	int ret;
-	ret = inl(smbus->base + reg);
+	ret = ioread32(smbus->ioaddr + reg);
 	dev_dbg(&smbus->dev->dev, "smbus read reg %lx val %08x\n",
 		smbus->base + reg, ret);
 	return ret;
@@ -351,6 +352,12 @@ static int pasemi_smb_probe(struct pci_dev *dev,
 		goto out_kfree;
 	}
 
+	smbus->ioaddr = ioport_map(smbus->base, smbus->size);
+	if (!smbus->ioaddr) {
+		error = -EBUSY;
+		goto out_release_region;
+	}
+
 	smbus->adapter.owner = THIS_MODULE;
 	snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
 		 "PA Semi SMBus adapter at 0x%lx", smbus->base);
@@ -366,12 +373,14 @@ static int pasemi_smb_probe(struct pci_dev *dev,
 
 	error = i2c_add_adapter(&smbus->adapter);
 	if (error)
-		goto out_release_region;
+		goto out_ioport_unmap;
 
 	pci_set_drvdata(dev, smbus);
 
 	return 0;
 
+ out_ioport_unmap:
+	ioport_unmap(smbus->ioaddr);
  out_release_region:
 	release_region(smbus->base, smbus->size);
  out_kfree:
@@ -384,6 +393,7 @@ static void pasemi_smb_remove(struct pci_dev *dev)
 	struct pasemi_smbus *smbus = pci_get_drvdata(dev);
 
 	i2c_del_adapter(&smbus->adapter);
+	ioport_unmap(smbus->ioaddr);
 	release_region(smbus->base, smbus->size);
 	kfree(smbus);
 }
Signature · 1 line
2.25.1
author
#6 lore raw

PATCH 3/10 i2c: pasemi: Remove usage of pci_dev

In reply to Sven Peter

Prepare to create a platform driver by removing all usages of pci_dev we can.

---
 drivers/i2c/busses/i2c-pasemi.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/i2c/busses/i2c-pasemi.c +6 -6
index dd31d902a621..5a25c2e54b9e 100644
@@ -18,7 +18,7 @@
 static struct pci_driver pasemi_smb_driver;
 
 struct pasemi_smbus {
-	struct pci_dev		*dev;
+	struct device           *dev;
 	struct i2c_adapter	 adapter;
 	void __iomem		*ioaddr;
 	unsigned long		 base;
@@ -52,7 +52,7 @@ struct pasemi_smbus {
 
 static inline void reg_write(struct pasemi_smbus *smbus, int reg, int val)
 {
-	dev_dbg(&smbus->dev->dev, "smbus write reg %lx val %08x\n",
+	dev_dbg(smbus->dev, "smbus write reg %lx val %08x\n",
 		smbus->base + reg, val);
 	iowrite32(val, smbus->ioaddr + reg);
 }
@@ -61,7 +61,7 @@ static inline int reg_read(struct pasemi_smbus *smbus, int reg)
 {
 	int ret;
 	ret = ioread32(smbus->ioaddr + reg);
-	dev_dbg(&smbus->dev->dev, "smbus read reg %lx val %08x\n",
+	dev_dbg(smbus->dev, "smbus read reg %lx val %08x\n",
 		smbus->base + reg, ret);
 	return ret;
 }
@@ -94,7 +94,7 @@ static int pasemi_smb_waitready(struct pasemi_smbus *smbus)
 		return -ENXIO;
 
 	if (timeout < 0) {
-		dev_warn(&smbus->dev->dev, "Timeout, status 0x%08x\n", status);
+		dev_warn(smbus->dev, "Timeout, status 0x%08x\n", status);
 		reg_write(smbus, REG_SMSTA, status);
 		return -ETIME;
 	}
@@ -342,7 +342,7 @@ static int pasemi_smb_probe(struct pci_dev *dev,
 	if (!smbus)
 		return -ENOMEM;
 
-	smbus->dev = dev;
+	smbus->dev = &dev->dev;
 	smbus->base = pci_resource_start(dev, 0);
 	smbus->size = pci_resource_len(dev, 0);
 
@@ -366,7 +366,7 @@ static int pasemi_smb_probe(struct pci_dev *dev,
 	smbus->adapter.algo_data = smbus;
 
 	/* set up the sysfs linkage to our parent device */
-	smbus->adapter.dev.parent = &dev->dev;
+	smbus->adapter.dev.parent = smbus->dev;
 
 	reg_write(smbus, REG_CTL, (CTL_MTR | CTL_MRR |
 		  (CLK_100K_DIV & CTL_CLK_M)));
Signature · 1 line
2.25.1
author
#7 lore raw

PATCH 4/10 i2c: pasemi: Split off common probing code

In reply to Sven Peter

Split off common probing code that will be used by both the PCI and the platform device.

---
 drivers/i2c/busses/i2c-pasemi.c | 39 +++++++++++++++++++++------------
 1 file changed, 25 insertions(+), 14 deletions(-)
drivers/i2c/busses/i2c-pasemi.c +25 -14
index 5a25c2e54b9e..9d69ff63f674 100644
@@ -329,6 +329,30 @@ static const struct i2c_algorithm smbus_algorithm = {
 	.functionality	= pasemi_smb_func,
 };
 
+static int pasemi_i2c_common_probe(struct pasemi_smbus *smbus)
+{
+	int error;
+
+	smbus->adapter.owner = THIS_MODULE;
+	snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
+		 "PA Semi SMBus adapter at 0x%lx", smbus->base);
+	smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
+	smbus->adapter.algo = &smbus_algorithm;
+	smbus->adapter.algo_data = smbus;
+
+	/* set up the sysfs linkage to our parent device */
+	smbus->adapter.dev.parent = smbus->dev;
+
+	reg_write(smbus, REG_CTL, (CTL_MTR | CTL_MRR |
+		  (CLK_100K_DIV & CTL_CLK_M)));
+
+	error = i2c_add_adapter(&smbus->adapter);
+	if (error)
+		return error;
+
+	return 0;
+}
+
 static int pasemi_smb_probe(struct pci_dev *dev,
 				      const struct pci_device_id *id)
 {
@@ -358,20 +382,7 @@ static int pasemi_smb_probe(struct pci_dev *dev,
 		goto out_release_region;
 	}
 
-	smbus->adapter.owner = THIS_MODULE;
-	snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
-		 "PA Semi SMBus adapter at 0x%lx", smbus->base);
-	smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
-	smbus->adapter.algo = &smbus_algorithm;
-	smbus->adapter.algo_data = smbus;
-
-	/* set up the sysfs linkage to our parent device */
-	smbus->adapter.dev.parent = smbus->dev;
-
-	reg_write(smbus, REG_CTL, (CTL_MTR | CTL_MRR |
-		  (CLK_100K_DIV & CTL_CLK_M)));
-
-	error = i2c_add_adapter(&smbus->adapter);
+	int error = pasemi_i2c_common_probe(smbus);
 	if (error)
 		goto out_ioport_unmap;
 
Signature · 1 line
2.25.1
author
#8 lore raw

PATCH 5/10 i2c: pasemi: Split pci driver to its own file

In reply to Sven Peter

Split off the PCI driver so that we can reuse common code for the platform driver.

---
 drivers/i2c/busses/Makefile                   |  1 +
 .../{i2c-pasemi.c => i2c-pasemi-core.c}       | 88 +----------------
 drivers/i2c/busses/i2c-pasemi-core.h          | 19 ++++
 drivers/i2c/busses/i2c-pasemi-pci.c           | 96 +++++++++++++++++++
 4 files changed, 118 insertions(+), 86 deletions(-)
 rename drivers/i2c/busses/{i2c-pasemi.c => i2c-pasemi-core.c} (81%)
 create mode 100644 drivers/i2c/busses/i2c-pasemi-core.h
 create mode 100644 drivers/i2c/busses/i2c-pasemi-pci.c
drivers/i2c/busses/Makefile +1 -0
index 1336b04f40e2..0ab1b4cb2228 100644
@@ -84,6 +84,7 @@ obj-$(CONFIG_I2C_NPCM7XX)	+= i2c-npcm7xx.o
 obj-$(CONFIG_I2C_OCORES)	+= i2c-ocores.o
 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
 obj-$(CONFIG_I2C_PCA_PLATFORM)	+= i2c-pca-platform.o
 obj-$(CONFIG_I2C_PNX)		+= i2c-pnx.o
drivers/i2c/busses/i2c-pasemi-core.c renamed from drivers/i2c/busses/i2c-pasemi.c +2 -86

103 lines not on this page · raw message

drivers/i2c/busses/i2c-pasemi-core.h new file +19 -0
new file mode 100644
index 000000000000..7acc33de6ce1
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <linux/atomic.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/i2c.h>
+#include <linux/i2c-smbus.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+
+struct pasemi_smbus {
+	struct device		*dev;
+	struct i2c_adapter	 adapter;
+	void __iomem		*ioaddr;
+	unsigned long		 base;
+	int			 size;
+};
+
+int pasemi_i2c_common_probe(struct pasemi_smbus *smbus);
drivers/i2c/busses/i2c-pasemi-pci.c new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#9 lore raw

PATCH 6/10 i2c: pasemi: Move common reset code to own function

In reply to Sven Peter

Split out common reset call to its own function so that we can later add support for selecting the clock frequency and an additional enable bit found in newer revisions.

---
 drivers/i2c/busses/i2c-pasemi-core.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/i2c/busses/i2c-pasemi-core.c +9 -6
index 3eda5e375fa2..7c6715f5dbb8 100644
@@ -61,6 +61,12 @@ static inline int reg_read(struct pasemi_smbus *smbus, int reg)
 #define TXFIFO_WR(smbus, reg)	reg_write((smbus), REG_MTXFIFO, (reg))
 #define RXFIFO_RD(smbus)	reg_read((smbus), REG_MRXFIFO)
 
+static void pasemi_reset(struct pasemi_smbus *smbus)
+{
+	reg_write(smbus, REG_CTL, (CTL_MTR | CTL_MRR |
+		  (CLK_100K_DIV & CTL_CLK_M)));
+}
+
 static void pasemi_smb_clear(struct pasemi_smbus *smbus)
 {
 	unsigned int status;
@@ -135,8 +141,7 @@ static int pasemi_i2c_xfer_msg(struct i2c_adapter *adapter,
 	return 0;
 
  reset_out:
-	reg_write(smbus, REG_CTL, (CTL_MTR | CTL_MRR |
-		  (CLK_100K_DIV & CTL_CLK_M)));
+	pasemi_reset(smbus);
 	return err;
 }
 
@@ -302,8 +307,7 @@ static int pasemi_smb_xfer(struct i2c_adapter *adapter,
 	return 0;
 
  reset_out:
-	reg_write(smbus, REG_CTL, (CTL_MTR | CTL_MRR |
-		  (CLK_100K_DIV & CTL_CLK_M)));
+	pasemi_reset(smbus);
 	return err;
 }
 
@@ -335,8 +339,7 @@ int pasemi_i2c_common_probe(struct pasemi_smbus *smbus)
 	/* set up the sysfs linkage to our parent device */
 	smbus->adapter.dev.parent = smbus->dev;
 
-	reg_write(smbus, REG_CTL, (CTL_MTR | CTL_MRR |
-		  (CLK_100K_DIV & CTL_CLK_M)));
+	pasemi_reset(smbus);
 
 	error = i2c_add_adapter(&smbus->adapter);
 	if (error)
Signature · 1 line
2.25.1
author
#10 lore raw

PATCH 7/10 i2c: pasemi: Allow to configure bus frequency

In reply to Sven Peter

Right now the bus frequency has always been hardcoded as 100 KHz with the specific reference clock used in the PASemi PCI controllers. Make this configurable to prepare for the platform driver.

---
 drivers/i2c/busses/i2c-pasemi-core.c | 8 +++-----
 drivers/i2c/busses/i2c-pasemi-core.h | 1 +
 drivers/i2c/busses/i2c-pasemi-pci.c  | 4 ++++
 3 files changed, 8 insertions(+), 5 deletions(-)
drivers/i2c/busses/i2c-pasemi-core.c +3 -5
index 7c6715f5dbb8..a39e3258b162 100644
@@ -39,9 +39,6 @@
 #define CTL_MTR		0x00000200
 #define CTL_CLK_M	0x000000ff
 
-#define CLK_100K_DIV	84
-#define CLK_400K_DIV	21
-
 static inline void reg_write(struct pasemi_smbus *smbus, int reg, int val)
 {
 	dev_dbg(smbus->dev, "smbus write reg %lx val %08x\n",
@@ -63,8 +60,9 @@ static inline int reg_read(struct pasemi_smbus *smbus, int reg)
 
 static void pasemi_reset(struct pasemi_smbus *smbus)
 {
-	reg_write(smbus, REG_CTL, (CTL_MTR | CTL_MRR |
-		  (CLK_100K_DIV & CTL_CLK_M)));
+	u32 val = (CTL_MTR | CTL_MRR | (smbus->clk_div & CTL_CLK_M));
+
+	reg_write(smbus, REG_CTL, val);
 }
 
 static void pasemi_smb_clear(struct pasemi_smbus *smbus)
drivers/i2c/busses/i2c-pasemi-core.h +1 -0
index 7acc33de6ce1..30a7990825ef 100644
@@ -14,6 +14,7 @@ struct pasemi_smbus {
 	void __iomem		*ioaddr;
 	unsigned long		 base;
 	int			 size;
+	unsigned int		 clk_div;
 };
 
 int pasemi_i2c_common_probe(struct pasemi_smbus *smbus);
drivers/i2c/busses/i2c-pasemi-pci.c +4 -0
index 9a19df31866b..7405e0b48514 100644
@@ -17,6 +17,9 @@
 
 #include "i2c-pasemi-core.h"
 
+#define CLK_100K_DIV	84
+#define CLK_400K_DIV	21
+
 static struct pci_driver pasemi_smb_pci_driver;
 
 static int pasemi_smb_pci_probe(struct pci_dev *dev,
@@ -35,6 +38,7 @@ static int pasemi_smb_pci_probe(struct pci_dev *dev,
 	smbus->dev = &dev->dev;
 	smbus->base = pci_resource_start(dev, 0);
 	smbus->size = pci_resource_len(dev, 0);
+	smbus->clk_div = CLK_100K_DIV;
 
 	if (!request_region(smbus->base, smbus->size,
 			    pasemi_smb_pci_driver.name)) {
Signature · 1 line
2.25.1
author
#11 lore raw

PATCH 8/10 i2c: pasemi: Refactor _probe to use devm_*

In reply to Sven Peter

Using managed device resources means there's nothing left to be done in pasemi_smb_pci_remove and also allows to remove base and size from struct pasemi_smbus.

---
 drivers/i2c/busses/i2c-pasemi-core.c | 10 +++----
 drivers/i2c/busses/i2c-pasemi-core.h |  2 --
 drivers/i2c/busses/i2c-pasemi-pci.c  | 45 ++++++++--------------------
 3 files changed, 16 insertions(+), 41 deletions(-)
drivers/i2c/busses/i2c-pasemi-core.c +4 -6
index a39e3258b162..0ec65263fd08 100644
@@ -41,8 +41,7 @@
 
 static inline void reg_write(struct pasemi_smbus *smbus, int reg, int val)
 {
-	dev_dbg(smbus->dev, "smbus write reg %lx val %08x\n",
-		smbus->base + reg, val);
+	dev_dbg(smbus->dev, "smbus write reg %x val %08x\n", reg, val);
 	iowrite32(val, smbus->ioaddr + reg);
 }
 
@@ -50,8 +49,7 @@ static inline int reg_read(struct pasemi_smbus *smbus, int reg)
 {
 	int ret;
 	ret = ioread32(smbus->ioaddr + reg);
-	dev_dbg(smbus->dev, "smbus read reg %lx val %08x\n",
-		smbus->base + reg, ret);
+	dev_dbg(smbus->dev, "smbus read reg %x val %08x\n", reg, ret);
 	return ret;
 }
 
@@ -329,7 +327,7 @@ int pasemi_i2c_common_probe(struct pasemi_smbus *smbus)
 
 	smbus->adapter.owner = THIS_MODULE;
 	snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
-		 "PA Semi SMBus adapter at 0x%lx", smbus->base);
+		 "PA Semi SMBus adapter at 0x%p", smbus->ioaddr);
 	smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
 	smbus->adapter.algo = &smbus_algorithm;
 	smbus->adapter.algo_data = smbus;
@@ -339,7 +337,7 @@ int pasemi_i2c_common_probe(struct pasemi_smbus *smbus)
 
 	pasemi_reset(smbus);
 
-	error = i2c_add_adapter(&smbus->adapter);
+	error = devm_i2c_add_adapter(smbus->dev, &smbus->adapter);
 	if (error)
 		return error;
 
drivers/i2c/busses/i2c-pasemi-core.h +0 -2
index 30a7990825ef..aca4e2da9089 100644
@@ -12,8 +12,6 @@ struct pasemi_smbus {
 	struct device		*dev;
 	struct i2c_adapter	 adapter;
 	void __iomem		*ioaddr;
-	unsigned long		 base;
-	int			 size;
 	unsigned int		 clk_div;
 };
 
drivers/i2c/busses/i2c-pasemi-pci.c +12 -33

76 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#12 lore raw

PATCH 9/10 i2c: pasemi: Add Apple platform driver

In reply to 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.

---
 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
MAINTAINERS +1 -0
index 380a680db92f..6e952158b6e1 100644
@@ -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
drivers/i2c/busses/Kconfig +11 -0
index e17790fe35a7..cf4dae07e319 100644
@@ -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
drivers/i2c/busses/Makefile +2 -0
index 0ab1b4cb2228..474fe2c520d0 100644
@@ -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
drivers/i2c/busses/i2c-pasemi-apple.c new file +122 -0

122 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#15 lore raw

PATCH 10/10 i2c: pasemi: Set enable bit for Apple variant

In reply to Sven Peter

Some later revisions after the original PASemi I2C controller introduce what likely is an enable bit to the CTL register. Without setting it the actual i2c transmission is never started.

---
 drivers/i2c/busses/i2c-pasemi-core.c | 8 ++++++++
 drivers/i2c/busses/i2c-pasemi-core.h | 3 +++
 drivers/i2c/busses/i2c-pasemi-pci.c  | 6 ++++++
 3 files changed, 17 insertions(+)
drivers/i2c/busses/i2c-pasemi-core.c +8 -0
index 0ec65263fd08..b52a65beda99 100644
@@ -22,6 +22,7 @@
 #define REG_MRXFIFO	0x04
 #define REG_SMSTA	0x14
 #define REG_CTL		0x1c
+#define REG_REV		0x28
 
 /* Register defs */
 #define MTXFIFO_READ	0x00000400
@@ -37,6 +38,7 @@
 
 #define CTL_MRR		0x00000400
 #define CTL_MTR		0x00000200
+#define CTL_EN		0x00000800
 #define CTL_CLK_M	0x000000ff
 
 static inline void reg_write(struct pasemi_smbus *smbus, int reg, int val)
@@ -60,6 +62,9 @@ static void pasemi_reset(struct pasemi_smbus *smbus)
 {
 	u32 val = (CTL_MTR | CTL_MRR | (smbus->clk_div & CTL_CLK_M));
 
+	if (smbus->hw_rev >= 6)
+		val |= CTL_EN;
+
 	reg_write(smbus, REG_CTL, val);
 }
 
@@ -335,6 +340,9 @@ int pasemi_i2c_common_probe(struct pasemi_smbus *smbus)
 	/* set up the sysfs linkage to our parent device */
 	smbus->adapter.dev.parent = smbus->dev;
 
+	if (smbus->hw_rev != PASEMI_HW_REV_PCI)
+		smbus->hw_rev = reg_read(smbus, REG_REV);
+
 	pasemi_reset(smbus);
 
 	error = devm_i2c_add_adapter(smbus->dev, &smbus->adapter);
drivers/i2c/busses/i2c-pasemi-core.h +3 -0
index aca4e2da9089..4655124a37f3 100644
@@ -8,11 +8,14 @@
 #include <linux/io.h>
 #include <linux/kernel.h>
 
+#define PASEMI_HW_REV_PCI -1
+
 struct pasemi_smbus {
 	struct device		*dev;
 	struct i2c_adapter	 adapter;
 	void __iomem		*ioaddr;
 	unsigned int		 clk_div;
+	int			 hw_rev;
 };
 
 int pasemi_i2c_common_probe(struct pasemi_smbus *smbus);
drivers/i2c/busses/i2c-pasemi-pci.c +6 -0
index c1b8901110c0..2b7be35421bc 100644
@@ -42,6 +42,12 @@ static int pasemi_smb_pci_probe(struct pci_dev *dev,
 	size = pci_resource_len(dev, 0);
 	smbus->clk_div = CLK_100K_DIV;
 
+	/*
+	 * The original PASemi PCI controllers don't have a register for
+	 * their HW revision.
+	 */
+	smbus->hw_rev = PASEMI_HW_REV_PCI;
+
 	if (!devm_request_region(&dev->dev, base, size,
 			    pasemi_smb_pci_driver.name))
 		return -EBUSY;
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH 02/10] i2c: pasemi: Use io{read,write}32

In reply to Sven Peter

drivers/i2c/busses/i2c-pasemi.c static int pasemi_smb_probe(struct pci_dev *dev, · line 359

Quoted 12 lines · Sven Peter

In preparation for splitting this driver up into a platform_driver and a pci_driver, replace outl/inl usage with ioport_map and ioread32/iowrite32.

drivers/i2c/busses/i2c-pasemi.c +5 -0
+       smbus->ioaddr = ioport_map(smbus->base, smbus->size);
+       if (!smbus->ioaddr) {
+               error = -EBUSY;
+               goto out_release_region;
+       }

While this works, I would suggest using the more regular pci_iomap() or pcim_iomap() helper to turn the port number into an __iomem token.

Arnd

Reviewed-by on the v1 series
#16 lore raw
In reply to Sven Peter
Quoted 19 lines · Sven Peter

This series adds support for the I2C controller found on Apple Silicon Macs which has quite a bit of history:

Apple bought P.A. Semi in 2008 and it looks like a part of its legacy continues to live on in the M1. This controller has actually been used since at least the iPhone 4S and hasn't changed much since then.
Essentially, there are only a few differences that matter:

- The controller no longer is a PCI device
- Starting at some iPhone an additional bit in one register must be set in order to start transmissions.
- The reference clock and hence the clock dividers are different

In order to add support for a platform device I first replaced PCI-specific bits and split out the PCI driver to its own file. Then I added support to make the clock divider configurable and converted the driver to use managed device resources to make it a bit simpler.

Quoted 6 lines

The Apple and PASemi driver will never be compiled in the same kernel since the Apple one will run on arm64 while the original PASemi driver will only be useful on powerpc.
I've thus followed the octeon (mips)/thunderx(arm64) approach to do the
split: I created a -core.c file which contains the shared logic and just compile that one for both the PASemi and the new Apple driver.

This looks all very good to me, I had one very minor comment.

Whole series

author
#5 lore raw

Re: [PATCH 02/10] i2c: pasemi: Use io{read,write}32

In reply to Arnd Bergmann
Quoted 8 lines · Arnd Bergmann
Quoted 7 lines · Sven Peter

In preparation for splitting this driver up into a platform_driver and a pci_driver, replace outl/inl usage with ioport_map and ioread32/iowrite32.

Quoted 8 lines

drivers/i2c/busses/i2c-pasemi.c static int pasemi_smb_probe(struct pci_dev *dev, · line 359

Quoted 5 lines
drivers/i2c/busses/i2c-pasemi.c +5 -0
+       smbus->ioaddr = ioport_map(smbus->base, smbus->size);
+       if (!smbus->ioaddr) {
+               error = -EBUSY;
+               goto out_release_region;
+       }

While this works, I would suggest using the more regular pci_iomap() or pcim_iomap() helper to turn the port number into an __iomem token.

Thanks a lot for the review!

I'll replace it with pci_iomap here and then later in this series with pcim_iomap when also switching the rest to devres.

Thanks,

Sven

#13 lore raw

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

In reply to Sven Peter
Quoted 2 lines
 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.

author
#14 lore raw

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

In reply to Wolfram Sang
Quoted 4 lines · Wolfram Sang
Quoted 1 line
 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

maintainer Reviewed-by on v1 1/10
#2 lore raw

Re: [PATCH 01/10] dt-bindings: i2c: Add Apple I2C controller bindings

In reply to Sven Peter
Quoted 10 lines · Sven Peter

The Apple I2C controller is based on the PASemi I2C controller. It is present on Apple SoCs such as the M1.

---
 .../devicetree/bindings/i2c/apple,i2c.yaml    | 61 +++++++++++++++++++
 MAINTAINERS                                   |  1 +
 2 files changed, 62 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/apple,i2c.yaml