Skip to contentSkip to conversation

Use devm_i2c_add_adapter() for amdgpu

PATCH Applied Rodrigo Siqueira 2 participants 12 messages Sep 10, 2025 – Sep 15, 2025

Subsystems (4)
lore
Share & tools
lore

Rodrigo Siqueira

author reviewer
#0 lore raw

cover Use devm_i2c_add_adapter() for amdgpu

When discussing the load/unload issue in amdgpu and its fix [1], Robert Beckett suggested the use of devm_i2c_add_adapter(). This function is part of Devres [2], and it simplifies resource management when the driver is detached. Aside from reducing boilerplate, it also reduces maintenance effort and minimizes the likelihood of future load/unload issues associated with i2c.

With this idea in mind, this series replaces all the i2c_add_adapter() and i2c_del_adapter() with devm_i2c_add_adapter() in amdgpu. The first patch is focused on DM, the second patch focuses on the generic amdgpu_i2c, and all the other patches are part of the SMU (they share a similar code).

[1] - https://gitlab.freedesktop.org/agd5f/linux/-/commit/89923fb7ead4fdd37b78dd49962d9bb5892403e6
[2] - https://docs.kernel.org/driver-api/driver-model/devres.html

Thanks

Rodrigo Siqueira (10):
drm/amd/display: Use devm_i2c_add_adapter to simplify i2c cleanup
logic
drm/amdgpu/amdgpu_i2c: Use devm_i2c_add_adapter instead of
i2c_add_adapter
drm/amdgpu: Use devm_i2c_add_adapter() in SMU V11 drm/amd/pm: Use devm_i2c_add_adapter() in the i2c init drm/amd/pm: Use devm_i2c_add_adapter() in the Arcturus smu drm/amd/pm: Use devm_i2c_add_adapter() in the Navi10 smu drm/amd/pm: Use devm_i2c_add_adapter() in the Sienna smu drm/amd/pm: Use devm_i2c_add_adapter() in the V13 smu drm/amd/pm: Use devm_i2c_add_adapter() in the V13_0_6 smu drm/amd/pm: Use devm_i2c_add_adapter() in the V14_0_2 smu

 drivers/gpu/drm/amd/amdgpu/amdgpu_i2c.c       | 18 +++-------------
 drivers/gpu/drm/amd/amdgpu/smu_v11_0_i2c.c    |  5 +----
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 21 ++-----------------
 .../gpu/drm/amd/pm/swsmu/smu11/arcturus_ppt.c | 19 ++---------------
 .../gpu/drm/amd/pm/swsmu/smu11/navi10_ppt.c   | 19 ++---------------
 .../amd/pm/swsmu/smu11/sienna_cichlid_ppt.c   | 19 ++---------------
 .../drm/amd/pm/swsmu/smu13/aldebaran_ppt.c    | 15 ++-----------
 .../drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c  | 19 ++---------------
 .../drm/amd/pm/swsmu/smu13/smu_v13_0_6_ppt.c  | 19 ++---------------
 .../drm/amd/pm/swsmu/smu14/smu_v14_0_2_ppt.c  | 19 ++---------------
 10 files changed, 20 insertions(+), 153 deletions(-)
Signature · 1 line
2.50.1
author reviewer
#1 lore raw

PATCH 1/10 drm/amd/display: Use devm_i2c_add_adapter to simplify i2c cleanup logic

This commit replaces the utilization of i2c_add/del_adapter() with devm_i2c_add_adapter() to reduce the amount of boilerplate. Using devm_i2c_add_adapter() has the advantage of removing the manual manipulation of the I2C adapter.

---
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 21 ++-----------------
 1 file changed, 2 insertions(+), 19 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +2 -19
index fadc6098eaee..5a06a82de6d9 100644
@@ -2945,7 +2945,7 @@ static int dm_oem_i2c_hw_init(struct amdgpu_device *adev)
 			return -ENOMEM;
 		}
 
-		r = i2c_add_adapter(&oem_i2c->base);
+		r = devm_i2c_add_adapter(adev->dev, &oem_i2c->base);
 		if (r) {
 			drm_info(adev_to_drm(adev), "Failed to register oem i2c\n");
 			kfree(oem_i2c);
@@ -2957,17 +2957,6 @@ static int dm_oem_i2c_hw_init(struct amdgpu_device *adev)
 	return 0;
 }
 
-static void dm_oem_i2c_hw_fini(struct amdgpu_device *adev)
-{
-	struct amdgpu_display_manager *dm = &adev->dm;
-
-	if (dm->oem_i2c) {
-		i2c_del_adapter(&dm->oem_i2c->base);
-		kfree(dm->oem_i2c);
-		dm->oem_i2c = NULL;
-	}
-}
-
 /**
  * dm_hw_init() - Initialize DC device
  * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
@@ -3018,8 +3007,6 @@ static int dm_hw_fini(struct amdgpu_ip_block *ip_block)
 {
 	struct amdgpu_device *adev = ip_block->adev;
 
-	dm_oem_i2c_hw_fini(adev);
-
 	amdgpu_dm_hpd_fini(adev);
 
 	amdgpu_dm_irq_fini(adev);
@@ -7363,10 +7350,6 @@ static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
 	drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
 	drm_connector_unregister(connector);
 	drm_connector_cleanup(connector);
-	if (aconnector->i2c) {
-		i2c_del_adapter(&aconnector->i2c->base);
-		kfree(aconnector->i2c);
-	}
 	kfree(aconnector->dm_dp_aux.aux.name);
 
 	kfree(connector);
@@ -8698,7 +8681,7 @@ static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
 	}
 
 	aconnector->i2c = i2c;
-	res = i2c_add_adapter(&i2c->base);
+	res = devm_i2c_add_adapter(dm->adev->dev, &i2c->base);
 
 	if (res) {
 		drm_err(adev_to_drm(dm->adev), "Failed to register hw i2c %d\n", link->link_index);
Signature · 1 line
2.50.1
author reviewer
#2 lore raw

PATCH 2/10 drm/amdgpu/amdgpu_i2c: Use devm_i2c_add_adapter instead of i2c_add_adapter

In reply to Rodrigo Siqueira

This commit replaces i2c_add_adapter() with devm_i2c_add_adapter() and removes part of the cleanup logic since the new function handles the i2c removal.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_i2c.c | 18 +++---------------
 1 file changed, 3 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_i2c.c +3 -15
index 57101d24422f..9cb72f0c5277 100644
@@ -184,7 +184,7 @@ struct amdgpu_i2c_chan *amdgpu_i2c_create(struct drm_device *dev,
 		snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
 			 "AMDGPU i2c hw bus %s", name);
 		i2c->adapter.algo = &amdgpu_atombios_i2c_algo;
-		ret = i2c_add_adapter(&i2c->adapter);
+		ret = devm_i2c_add_adapter(dev->dev, &i2c->adapter);
 		if (ret)
 			goto out_free;
 	} else {
@@ -215,15 +215,6 @@ struct amdgpu_i2c_chan *amdgpu_i2c_create(struct drm_device *dev,
 
 }
 
-void amdgpu_i2c_destroy(struct amdgpu_i2c_chan *i2c)
-{
-	if (!i2c)
-		return;
-	WARN_ON(i2c->has_aux);
-	i2c_del_adapter(&i2c->adapter);
-	kfree(i2c);
-}
-
 void amdgpu_i2c_init(struct amdgpu_device *adev)
 {
 	if (!adev->is_atom_fw) {
@@ -248,12 +239,9 @@ void amdgpu_i2c_fini(struct amdgpu_device *adev)
 {
 	int i;
 
-	for (i = 0; i < AMDGPU_MAX_I2C_BUS; i++) {
-		if (adev->i2c_bus[i]) {
-			amdgpu_i2c_destroy(adev->i2c_bus[i]);
+	for (i = 0; i < AMDGPU_MAX_I2C_BUS; i++)
+		if (adev->i2c_bus[i])
 			adev->i2c_bus[i] = NULL;
-		}
-	}
 }
 
 /* looks up bus based on id */
Signature · 1 line
2.50.1
author reviewer
#3 lore raw

PATCH 3/10 drm/amdgpu: Use devm_i2c_add_adapter() in SMU V11

In reply to Rodrigo Siqueira

Instead of using i2c_add_adapter() and i2c_del_adapter() in the SMU V11, use devm_i2c_add_adapter() to simplify the code path.

---
 drivers/gpu/drm/amd/amdgpu/smu_v11_0_i2c.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/smu_v11_0_i2c.c +1 -4
index dd2d66090d23..68aef47254a9 100644
@@ -743,7 +743,7 @@ int smu_v11_0_i2c_control_init(struct amdgpu_device *adev)
 	adev->pm.ras_eeprom_i2c_bus = &adev->pm.smu_i2c[0].adapter;
 	adev->pm.fru_eeprom_i2c_bus = &adev->pm.smu_i2c[0].adapter;
 
-	res = i2c_add_adapter(control);
+	res = devm_i2c_add_adapter(adev->dev, control);
 	if (res)
 		DRM_ERROR("Failed to register hw i2c, err: %d\n", res);
 
@@ -752,9 +752,6 @@ int smu_v11_0_i2c_control_init(struct amdgpu_device *adev)
 
 void smu_v11_0_i2c_control_fini(struct amdgpu_device *adev)
 {
-	struct i2c_adapter *control = adev->pm.ras_eeprom_i2c_bus;
-
-	i2c_del_adapter(control);
 	adev->pm.ras_eeprom_i2c_bus = NULL;
 	adev->pm.fru_eeprom_i2c_bus = NULL;
 }
Signature · 1 line
2.50.1
author reviewer
#4 lore raw

PATCH 4/10 drm/amd/pm: Use devm_i2c_add_adapter() in the i2c init

In reply to Rodrigo Siqueira

Instead of using i2c_add_adapter() and i2c_del_adapter(), replace them with devm_i2c_add_adapter() to simplify the i2c logic.

---
 .../gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c    | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu13/aldebaran_ppt.c +2 -13
index b067147b7c41..18d5d0704509 100644
@@ -1641,33 +1641,22 @@ static int aldebaran_i2c_control_init(struct smu_context *smu)
 	control->quirks = &aldebaran_i2c_control_quirks;
 	i2c_set_adapdata(control, smu_i2c);
 
-	res = i2c_add_adapter(control);
+	res = devm_i2c_add_adapter(adev->dev, control);
 	if (res) {
 		DRM_ERROR("Failed to register hw i2c, err: %d\n", res);
-		goto Out_err;
+		return res;
 	}
 
 	adev->pm.ras_eeprom_i2c_bus = &adev->pm.smu_i2c[0].adapter;
 	adev->pm.fru_eeprom_i2c_bus = &adev->pm.smu_i2c[0].adapter;
 
 	return 0;
-Out_err:
-	i2c_del_adapter(control);
-
-	return res;
 }
 
 static void aldebaran_i2c_control_fini(struct smu_context *smu)
 {
 	struct amdgpu_device *adev = smu->adev;
-	int i;
-
-	for (i = 0; i < MAX_SMU_I2C_BUSES; i++) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
 
-		i2c_del_adapter(control);
-	}
 	adev->pm.ras_eeprom_i2c_bus = NULL;
 	adev->pm.fru_eeprom_i2c_bus = NULL;
 }
Signature · 1 line
2.50.1
author reviewer
#5 lore raw

PATCH 5/10 drm/amd/pm: Use devm_i2c_add_adapter() in the Arcturus smu

In reply to Rodrigo Siqueira

The I2C init for Arcturus uses i2c_add_adapter() and i2c_del_adapter(), this commit replaces the use of these two functions with devm_i2c_add_adapter(). Notice that Arcturus init initializes multiple I2C buses in a loop; if something goes wrong, the previous adapters are removed, and the amdgpu load is interrupted. Since I2C init is required for the correct load of amdgpu, it is safe to rely on devm_i2c_add_adapter() to handle any previously initialized I2C adapter.

---
 .../gpu/drm/amd/pm/swsmu/smu11/arcturus_ppt.c | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu11/arcturus_ppt.c +2 -17
index 599eddb5a67d..4fff78da81ff 100644
@@ -1745,10 +1745,10 @@ static int arcturus_i2c_control_init(struct smu_context *smu)
 		snprintf(control->name, sizeof(control->name), "AMDGPU SMU %d", i);
 		i2c_set_adapdata(control, smu_i2c);
 
-		res = i2c_add_adapter(control);
+		res = devm_i2c_add_adapter(adev->dev, control);
 		if (res) {
 			DRM_ERROR("Failed to register hw i2c, err: %d\n", res);
-			goto Out_err;
+			return res;
 		}
 	}
 
@@ -1756,27 +1756,12 @@ static int arcturus_i2c_control_init(struct smu_context *smu)
 	adev->pm.fru_eeprom_i2c_bus = &adev->pm.smu_i2c[1].adapter;
 
 	return 0;
-Out_err:
-	for ( ; i >= 0; i--) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
-	return res;
 }
 
 static void arcturus_i2c_control_fini(struct smu_context *smu)
 {
 	struct amdgpu_device *adev = smu->adev;
-	int i;
 
-	for (i = 0; i < MAX_SMU_I2C_BUSES; i++) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
 	adev->pm.ras_eeprom_i2c_bus = NULL;
 	adev->pm.fru_eeprom_i2c_bus = NULL;
 }
Signature · 1 line
2.50.1
author reviewer
#6 lore raw

PATCH 6/10 drm/amd/pm: Use devm_i2c_add_adapter() in the Navi10 smu

In reply to Rodrigo Siqueira

The I2C init for Navi10 uses i2c_add_adapter() and i2c_del_adapter(), this commit replaces the use of these two functions with devm_i2c_add_adapter(). Notice that Navi10 init initializes multiple I2C buses in a loop; if something goes wrong, the previous adapters are removed, and the amdgpu load is interrupted. Since I2C init is required for the correct load of amdgpu, it is safe to rely on devm_i2c_add_adapter() to handle any previously initialized I2C adapter.

---
 .../gpu/drm/amd/pm/swsmu/smu11/navi10_ppt.c   | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu11/navi10_ppt.c +2 -17
index aac202d0c30e..0028f10ead42 100644
@@ -3145,10 +3145,10 @@ static int navi10_i2c_control_init(struct smu_context *smu)
 		control->quirks = &navi10_i2c_control_quirks;
 		i2c_set_adapdata(control, smu_i2c);
 
-		res = i2c_add_adapter(control);
+		res = devm_i2c_add_adapter(adev->dev, control);
 		if (res) {
 			DRM_ERROR("Failed to register hw i2c, err: %d\n", res);
-			goto Out_err;
+			return res;
 		}
 	}
 
@@ -3156,27 +3156,12 @@ static int navi10_i2c_control_init(struct smu_context *smu)
 	adev->pm.fru_eeprom_i2c_bus = &adev->pm.smu_i2c[1].adapter;
 
 	return 0;
-Out_err:
-	for ( ; i >= 0; i--) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
-	return res;
 }
 
 static void navi10_i2c_control_fini(struct smu_context *smu)
 {
 	struct amdgpu_device *adev = smu->adev;
-	int i;
 
-	for (i = 0; i < MAX_SMU_I2C_BUSES; i++) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
 	adev->pm.ras_eeprom_i2c_bus = NULL;
 	adev->pm.fru_eeprom_i2c_bus = NULL;
 }
Signature · 1 line
2.50.1
author reviewer
#7 lore raw

PATCH 7/10 drm/amd/pm: Use devm_i2c_add_adapter() in the Sienna smu

In reply to Rodrigo Siqueira

The I2C init for Sienna Cichlid uses i2c_add_adapter() and i2c_del_adapter(), this commit replaces the use of these two functions with devm_i2c_add_adapter(). Notice that Sienna Cichlid init initializes multiple I2C buses in a loop; if something goes wrong, the previous adapters are removed, and the amdgpu load is interrupted. Since I2C init is required for the correct load of amdgpu, it is safe to rely on devm_i2c_add_adapter() to handle any previously initialized I2C adapter.

---
 .../amd/pm/swsmu/smu11/sienna_cichlid_ppt.c   | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu11/sienna_cichlid_ppt.c +2 -17
index d57591509aed..31c2c0386b1f 100644
@@ -2648,10 +2648,10 @@ static int sienna_cichlid_i2c_control_init(struct smu_context *smu)
 		control->quirks = &sienna_cichlid_i2c_control_quirks;
 		i2c_set_adapdata(control, smu_i2c);
 
-		res = i2c_add_adapter(control);
+		res = devm_i2c_add_adapter(adev->dev, control);
 		if (res) {
 			DRM_ERROR("Failed to register hw i2c, err: %d\n", res);
-			goto Out_err;
+			return res;
 		}
 	}
 	/* assign the buses used for the FRU EEPROM and RAS EEPROM */
@@ -2660,27 +2660,12 @@ static int sienna_cichlid_i2c_control_init(struct smu_context *smu)
 	adev->pm.fru_eeprom_i2c_bus = &adev->pm.smu_i2c[0].adapter;
 
 	return 0;
-Out_err:
-	for ( ; i >= 0; i--) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
-	return res;
 }
 
 static void sienna_cichlid_i2c_control_fini(struct smu_context *smu)
 {
 	struct amdgpu_device *adev = smu->adev;
-	int i;
 
-	for (i = 0; i < MAX_SMU_I2C_BUSES; i++) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
 	adev->pm.ras_eeprom_i2c_bus = NULL;
 	adev->pm.fru_eeprom_i2c_bus = NULL;
 }
Signature · 1 line
2.50.1
author reviewer
#8 lore raw

PATCH 8/10 drm/amd/pm: Use devm_i2c_add_adapter() in the V13 smu

In reply to Rodrigo Siqueira

The I2C init for SMU_V13 uses i2c_add_adapter() and i2c_del_adapter(), this commit replaces the use of these two functions with devm_i2c_add_adapter(). Notice that SMU_V13 init initializes multiple I2C buses in a loop; if something goes wrong, the previous adapters are removed, and the amdgpu load is interrupted. Since I2C init is required for the correct load of amdgpu, it is safe to rely on devm_i2c_add_adapter() to handle any previously initialized I2C adapter.

---
 .../drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c  | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c +2 -17
index e084ed99ec0e..c1062e5f0393 100644
@@ -2825,10 +2825,10 @@ static int smu_v13_0_0_i2c_control_init(struct smu_context *smu)
 		control->quirks = &smu_v13_0_0_i2c_control_quirks;
 		i2c_set_adapdata(control, smu_i2c);
 
-		res = i2c_add_adapter(control);
+		res = devm_i2c_add_adapter(adev->dev, control);
 		if (res) {
 			DRM_ERROR("Failed to register hw i2c, err: %d\n", res);
-			goto Out_err;
+			return res;
 		}
 	}
 
@@ -2838,27 +2838,12 @@ static int smu_v13_0_0_i2c_control_init(struct smu_context *smu)
 	adev->pm.fru_eeprom_i2c_bus = &adev->pm.smu_i2c[0].adapter;
 
 	return 0;
-Out_err:
-	for ( ; i >= 0; i--) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
-	return res;
 }
 
 static void smu_v13_0_0_i2c_control_fini(struct smu_context *smu)
 {
 	struct amdgpu_device *adev = smu->adev;
-	int i;
 
-	for (i = 0; i < MAX_SMU_I2C_BUSES; i++) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
 	adev->pm.ras_eeprom_i2c_bus = NULL;
 	adev->pm.fru_eeprom_i2c_bus = NULL;
 }
Signature · 1 line
2.50.1
author reviewer
#9 lore raw

PATCH 9/10 drm/amd/pm: Use devm_i2c_add_adapter() in the V13_0_6 smu

In reply to Rodrigo Siqueira

The I2C init for V13_0_6 uses i2c_add_adapter() and i2c_del_adapter(), this commit replaces the use of these two functions with devm_i2c_add_adapter(). Notice that V13_0_6 init initializes multiple I2C buses in a loop; if something goes wrong, the previous adapters are removed, and the amdgpu load is interrupted. Since I2C init is required for the correct load of amdgpu, it is safe to rely on devm_i2c_add_adapter() to handle any previously initialized I2C adapter.

---
 .../drm/amd/pm/swsmu/smu13/smu_v13_0_6_ppt.c  | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_6_ppt.c +2 -17
index ebee659f8a1c..7bc18b3350dc 100644
@@ -2490,10 +2490,10 @@ static int smu_v13_0_6_i2c_control_init(struct smu_context *smu)
 		control->quirks = &smu_v13_0_6_i2c_control_quirks;
 		i2c_set_adapdata(control, smu_i2c);
 
-		res = i2c_add_adapter(control);
+		res = devm_i2c_add_adapter(adev->dev, control);
 		if (res) {
 			DRM_ERROR("Failed to register hw i2c, err: %d\n", res);
-			goto Out_err;
+			return res;
 		}
 	}
 
@@ -2501,27 +2501,12 @@ static int smu_v13_0_6_i2c_control_init(struct smu_context *smu)
 	adev->pm.fru_eeprom_i2c_bus = &adev->pm.smu_i2c[0].adapter;
 
 	return 0;
-Out_err:
-	for ( ; i >= 0; i--) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
-	return res;
 }
 
 static void smu_v13_0_6_i2c_control_fini(struct smu_context *smu)
 {
 	struct amdgpu_device *adev = smu->adev;
-	int i;
 
-	for (i = 0; i < MAX_SMU_I2C_BUSES; i++) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
 	adev->pm.ras_eeprom_i2c_bus = NULL;
 	adev->pm.fru_eeprom_i2c_bus = NULL;
 }
Signature · 1 line
2.50.1
author reviewer
#10 lore raw

PATCH 10/10 drm/amd/pm: Use devm_i2c_add_adapter() in the V14_0_2 smu

In reply to Rodrigo Siqueira

The I2C init for V14_0_2 uses i2c_add_adapter() and i2c_del_adapter(), this commit replaces the use of these two functions with devm_i2c_add_adapter(). Notice that V14_0_2 init initializes multiple I2C buses in a loop; if something goes wrong, the previous adapters are removed, and the amdgpu load is interrupted. Since I2C init is required for the correct load of amdgpu, it is safe to rely on devm_i2c_add_adapter() to handle any previously initialized I2C adapter.

---
 .../drm/amd/pm/swsmu/smu14/smu_v14_0_2_ppt.c  | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/pm/swsmu/smu14/smu_v14_0_2_ppt.c +2 -17
index f32474af90b3..086501cc5213 100644
@@ -2087,10 +2087,10 @@ static int smu_v14_0_2_i2c_control_init(struct smu_context *smu)
 		control->quirks = &smu_v14_0_2_i2c_control_quirks;
 		i2c_set_adapdata(control, smu_i2c);
 
-		res = i2c_add_adapter(control);
+		res = devm_i2c_add_adapter(adev->dev, control);
 		if (res) {
 			DRM_ERROR("Failed to register hw i2c, err: %d\n", res);
-			goto Out_err;
+			return res;
 		}
 	}
 
@@ -2100,27 +2100,12 @@ static int smu_v14_0_2_i2c_control_init(struct smu_context *smu)
 	adev->pm.fru_eeprom_i2c_bus = &adev->pm.smu_i2c[0].adapter;
 
 	return 0;
-Out_err:
-	for ( ; i >= 0; i--) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
-	return res;
 }
 
 static void smu_v14_0_2_i2c_control_fini(struct smu_context *smu)
 {
 	struct amdgpu_device *adev = smu->adev;
-	int i;
 
-	for (i = 0; i < MAX_SMU_I2C_BUSES; i++) {
-		struct amdgpu_smu_i2c_bus *smu_i2c = &adev->pm.smu_i2c[i];
-		struct i2c_adapter *control = &smu_i2c->adapter;
-
-		i2c_del_adapter(control);
-	}
 	adev->pm.ras_eeprom_i2c_bus = NULL;
 	adev->pm.fru_eeprom_i2c_bus = NULL;
 }
Signature · 1 line
2.50.1
Applied
#11 lore raw
In reply to Rodrigo Siqueira

Applied the series. Thanks!

Alex

Quoted 48 lines · Rodrigo Siqueira

When discussing the load/unload issue in amdgpu and its fix [1], Robert Beckett suggested the use of devm_i2c_add_adapter(). This function is part of Devres [2], and it simplifies resource management when the driver is detached. Aside from reducing boilerplate, it also reduces maintenance effort and minimizes the likelihood of future load/unload issues associated with i2c.

With this idea in mind, this series replaces all the i2c_add_adapter() and i2c_del_adapter() with devm_i2c_add_adapter() in amdgpu. The first patch is focused on DM, the second patch focuses on the generic amdgpu_i2c, and all the other patches are part of the SMU (they share a similar code).

[1] - https://gitlab.freedesktop.org/agd5f/linux/-/commit/89923fb7ead4fdd37b78dd49962d9bb5892403e6
[2] - https://docs.kernel.org/driver-api/driver-model/devres.html

Thanks

Rodrigo Siqueira (10):
drm/amd/display: Use devm_i2c_add_adapter to simplify i2c cleanup
logic
drm/amdgpu/amdgpu_i2c: Use devm_i2c_add_adapter instead of
i2c_add_adapter
drm/amdgpu: Use devm_i2c_add_adapter() in SMU V11 drm/amd/pm: Use devm_i2c_add_adapter() in the i2c init drm/amd/pm: Use devm_i2c_add_adapter() in the Arcturus smu drm/amd/pm: Use devm_i2c_add_adapter() in the Navi10 smu drm/amd/pm: Use devm_i2c_add_adapter() in the Sienna smu drm/amd/pm: Use devm_i2c_add_adapter() in the V13 smu drm/amd/pm: Use devm_i2c_add_adapter() in the V13_0_6 smu drm/amd/pm: Use devm_i2c_add_adapter() in the V14_0_2 smu

 drivers/gpu/drm/amd/amdgpu/amdgpu_i2c.c       | 18 +++-------------
 drivers/gpu/drm/amd/amdgpu/smu_v11_0_i2c.c    |  5 +----
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 21 ++-----------------
 .../gpu/drm/amd/pm/swsmu/smu11/arcturus_ppt.c | 19 ++---------------
 .../gpu/drm/amd/pm/swsmu/smu11/navi10_ppt.c   | 19 ++---------------
 .../amd/pm/swsmu/smu11/sienna_cichlid_ppt.c   | 19 ++---------------
 .../drm/amd/pm/swsmu/smu13/aldebaran_ppt.c    | 15 ++-----------
 .../drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c  | 19 ++---------------
 .../drm/amd/pm/swsmu/smu13/smu_v13_0_6_ppt.c  | 19 ++---------------
 .../drm/amd/pm/swsmu/smu14/smu_v14_0_2_ppt.c  | 19 ++---------------
 10 files changed, 20 insertions(+), 153 deletions(-)

--
2.50.1