[PATCH v2] auxdisplay: cfag12864bfb: Fix init cleanup and simplify error paths

From: Haoxiang Li

Date: Wed Nov 18 2026 - 04:26:04 EST


If initialization fails after cfag12864b_enable() succeeds, LCD
refreshing remains enabled. A subsequent attempt to load the
framebuffer module can fail because refreshing is still marked
as in use.

Use platform_device_register_full() to simplify device registration
and its error handling. Unregister the platform driver if device
registration fails, and call cfag12864b_disable() on all failure
paths after refreshing has been enabled.

Fixes: 70e840499aae ("[PATCH] drivers: add LCD support")
Signed-off-by: Haoxiang Li <haoxiang_li2024@xxxxxxx>
---
Changes in v2:
- Use platform_device_register_full() to simplify code. Thanks, Andy!
- Modify the commit message.
---
drivers/auxdisplay/cfag12864bfb.c | 34 +++++++++++++++++--------------
1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/drivers/auxdisplay/cfag12864bfb.c b/drivers/auxdisplay/cfag12864bfb.c
index 24baf6b2c587..b1fe462715d6 100644
--- a/drivers/auxdisplay/cfag12864bfb.c
+++ b/drivers/auxdisplay/cfag12864bfb.c
@@ -13,6 +13,7 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
+#include <linux/err.h>
#include <linux/fb.h>
#include <linux/mm.h>
#include <linux/platform_device.h>
@@ -118,13 +119,17 @@ static struct platform_device *cfag12864bfb_device;

static int __init cfag12864bfb_init(void)
{
- int ret = -EINVAL;
+ const struct platform_device_info pdevinfo = {
+ .name = CFAG12864BFB_NAME,
+ .id = 0,
+ };
+ int ret;

/* cfag12864b_init() must be called first */
if (!cfag12864b_isinited()) {
printk(KERN_ERR CFAG12864BFB_NAME ": ERROR: "
"cfag12864b is not initialized\n");
- goto none;
+ return -EINVAL;
}

if (cfag12864b_enable()) {
@@ -134,23 +139,22 @@ static int __init cfag12864bfb_init(void)
}

ret = platform_driver_register(&cfag12864bfb_driver);
+ if (ret)
+ goto err_disable;

- if (!ret) {
- cfag12864bfb_device =
- platform_device_alloc(CFAG12864BFB_NAME, 0);
+ cfag12864bfb_device = platform_device_register_full(&pdevinfo);
+ if (IS_ERR(cfag12864bfb_device)) {
+ ret = PTR_ERR(cfag12864bfb_device);
+ goto err_unregister_driver;
+ }

- if (cfag12864bfb_device)
- ret = platform_device_add(cfag12864bfb_device);
- else
- ret = -ENOMEM;
+ return 0;

- if (ret) {
- platform_device_put(cfag12864bfb_device);
- platform_driver_unregister(&cfag12864bfb_driver);
- }
- }
+err_unregister_driver:
+ platform_driver_unregister(&cfag12864bfb_driver);

-none:
+err_disable:
+ cfag12864b_disable();
return ret;
}

--
2.25.1