linux-doc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Zimmermann <tzimmermann@suse.de>
To: daniel@ffwll.ch, airlied@linux.ie,
	maarten.lankhorst@linux.intel.com, mripard@kernel.org,
	kraxel@redhat.com, corbet@lwn.net, lgirdwood@gmail.com,
	broonie@kernel.org, sam@ravnborg.org, robh@kernel.org,
	emil.l.velikov@gmail.com, geert+renesas@glider.be,
	hdegoede@redhat.com, bluescreen_avenger@verizon.net
Cc: dri-devel@lists.freedesktop.org, linux-doc@vger.kernel.org,
	virtualization@lists.linux-foundation.org,
	Thomas Zimmermann <tzimmermann@suse.de>
Subject: [PATCH v2 08/10] drm/simpledrm: Acquire clocks from DT device node
Date: Thu, 18 Mar 2021 11:29:19 +0100	[thread overview]
Message-ID: <20210318102921.21536-9-tzimmermann@suse.de> (raw)
In-Reply-To: <20210318102921.21536-1-tzimmermann@suse.de>

Make sure required hardware clocks are enabled while the firmware
framebuffer is in use.

The basic code has been taken from the simplefb driver and adapted
to DRM. Clocks are released automatically via devres helpers.

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
Tested-by: nerdopolis <bluescreen_avenger@verizon.net>
---
 drivers/gpu/drm/tiny/simpledrm.c | 108 +++++++++++++++++++++++++++++++
 1 file changed, 108 insertions(+)

diff --git a/drivers/gpu/drm/tiny/simpledrm.c b/drivers/gpu/drm/tiny/simpledrm.c
index c9cef2b50de6..10ca3373b61f 100644
--- a/drivers/gpu/drm/tiny/simpledrm.c
+++ b/drivers/gpu/drm/tiny/simpledrm.c
@@ -1,5 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
+#include <linux/clk.h>
+#include <linux/of_clk.h>
 #include <linux/platform_data/simplefb.h>
 #include <linux/platform_device.h>
 
@@ -190,6 +192,12 @@ struct simpledrm_device {
 	struct drm_device dev;
 	struct platform_device *pdev;
 
+	/* clocks */
+#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
+	unsigned int clk_count;
+	struct clk **clks;
+#endif
+
 	/* simplefb settings */
 	struct drm_display_mode mode;
 	const struct drm_format_info *format;
@@ -211,6 +219,103 @@ static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
 	return container_of(dev, struct simpledrm_device, dev);
 }
 
+/*
+ * Hardware
+ */
+
+#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
+/*
+ * Clock handling code.
+ *
+ * Here we handle the clocks property of our "simple-framebuffer" dt node.
+ * This is necessary so that we can make sure that any clocks needed by
+ * the display engine that the bootloader set up for us (and for which it
+ * provided a simplefb dt node), stay up, for the life of the simplefb
+ * driver.
+ *
+ * When the driver unloads, we cleanly disable, and then release the clocks.
+ *
+ * We only complain about errors here, no action is taken as the most likely
+ * error can only happen due to a mismatch between the bootloader which set
+ * up simplefb, and the clock definitions in the device tree. Chances are
+ * that there are no adverse effects, and if there are, a clean teardown of
+ * the fb probe will not help us much either. So just complain and carry on,
+ * and hope that the user actually gets a working fb at the end of things.
+ */
+
+static void simpledrm_device_release_clocks(void *res)
+{
+	struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
+	unsigned int i;
+
+	for (i = 0; i < sdev->clk_count; ++i) {
+		if (sdev->clks[i]) {
+			clk_disable_unprepare(sdev->clks[i]);
+			clk_put(sdev->clks[i]);
+		}
+	}
+}
+
+static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
+{
+	struct drm_device *dev = &sdev->dev;
+	struct platform_device *pdev = sdev->pdev;
+	struct device_node *of_node = pdev->dev.of_node;
+	struct clk *clock;
+	unsigned int i;
+	int ret;
+
+	if (dev_get_platdata(&pdev->dev) || !of_node)
+		return 0;
+
+	sdev->clk_count = of_clk_get_parent_count(of_node);
+	if (!sdev->clk_count)
+		return 0;
+
+	sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
+				  GFP_KERNEL);
+	if (!sdev->clks)
+		return -ENOMEM;
+
+	for (i = 0; i < sdev->clk_count; ++i) {
+		clock = of_clk_get(of_node, i);
+		if (IS_ERR(clock)) {
+			ret = PTR_ERR(clock);
+			if (ret == -EPROBE_DEFER)
+				goto err;
+			drm_err(dev, "clock %u not found: %d\n", i, ret);
+			continue;
+		}
+		ret = clk_prepare_enable(clock);
+		if (ret) {
+			drm_err(dev, "failed to enable clock %u: %d\n",
+				i, ret);
+			clk_put(clock);
+		}
+		sdev->clks[i] = clock;
+	}
+
+	return devm_add_action_or_reset(&pdev->dev,
+					simpledrm_device_release_clocks,
+					sdev);
+
+err:
+	while (i) {
+		--i;
+		if (sdev->clks[i]) {
+			clk_disable_unprepare(sdev->clks[i]);
+			clk_put(sdev->clks[i]);
+		}
+	}
+	return ret;
+}
+#else
+static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
+{
+	return 0;
+}
+#endif
+
 /*
  *  Simplefb settings
  */
@@ -534,6 +639,9 @@ simpledrm_device_create(struct drm_driver *drv, struct platform_device *pdev)
 		return ERR_CAST(sdev);
 	sdev->pdev = pdev;
 
+	ret = simpledrm_device_init_clocks(sdev);
+	if (ret)
+		return ERR_PTR(ret);
 	ret = simpledrm_device_init_fb(sdev);
 	if (ret)
 		return ERR_PTR(ret);
-- 
2.30.1


  parent reply	other threads:[~2021-03-18 10:30 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-18 10:29 [PATCH v2 00/10] drm: Support simple-framebuffer devices and firmware fbs Thomas Zimmermann
2021-03-18 10:29 ` [PATCH v2 01/10] drm/format-helper: Pass destination pitch to drm_fb_memcpy_dstclip() Thomas Zimmermann
2021-03-18 10:29 ` [PATCH v2 02/10] drm/format-helper: Add blitter functions Thomas Zimmermann
2021-03-18 10:29 ` [PATCH v2 03/10] drm/aperture: Move fbdev conflict helpers into drm_aperture.h Thomas Zimmermann
2021-04-08  9:50   ` Daniel Vetter
2021-04-09  7:09     ` Thomas Zimmermann
2021-03-18 10:29 ` [PATCH v2 04/10] drm/aperture: Add infrastructure for aperture ownership Thomas Zimmermann
2021-04-08  9:48   ` Daniel Vetter
2021-04-09  7:06     ` Thomas Zimmermann
2021-04-09  9:22       ` Daniel Vetter
2021-04-15  6:56         ` Thomas Zimmermann
2021-04-15 12:57           ` Daniel Vetter
2021-04-15 19:12             ` Thomas Zimmermann
2021-04-16  8:42               ` Daniel Vetter
2021-04-09  7:54     ` Thomas Zimmermann
2021-04-09  9:29       ` Daniel Vetter
2021-03-18 10:29 ` [PATCH v2 05/10] drm: Add simpledrm driver Thomas Zimmermann
2021-03-18 10:29 ` [PATCH v2 06/10] drm/simpledrm: Add fbdev emulation Thomas Zimmermann
2021-03-18 10:29 ` [PATCH v2 07/10] drm/simpledrm: Initialize framebuffer data from device-tree node Thomas Zimmermann
2021-03-18 10:29 ` Thomas Zimmermann [this message]
2021-03-18 10:39   ` [PATCH v2 08/10] drm/simpledrm: Acquire clocks from DT device node Geert Uytterhoeven
2021-03-18 11:00     ` Thomas Zimmermann
2021-04-08  8:13   ` Maxime Ripard
2021-04-15  7:31     ` Thomas Zimmermann
2021-04-15  9:21       ` Maxime Ripard
2021-04-15 11:02         ` Thomas Zimmermann
2021-04-15 12:11           ` maxime
2021-03-18 10:29 ` [PATCH v2 09/10] drm/simpledrm: Acquire regulators " Thomas Zimmermann
2021-03-18 10:29 ` [PATCH v2 10/10] drm/simpledrm: Acquire memory aperture for framebuffer Thomas Zimmermann
2021-03-25 11:29 ` [PATCH v2 00/10] drm: Support simple-framebuffer devices and firmware fbs Hans de Goede
2021-03-29 12:31   ` Thomas Zimmermann
2021-03-29 14:50     ` Hans de Goede
2021-03-30  7:09       ` Thomas Zimmermann
2021-03-30  8:34         ` Hans de Goede
2021-04-08 10:14           ` Daniel Vetter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210318102921.21536-9-tzimmermann@suse.de \
    --to=tzimmermann@suse.de \
    --cc=airlied@linux.ie \
    --cc=bluescreen_avenger@verizon.net \
    --cc=broonie@kernel.org \
    --cc=corbet@lwn.net \
    --cc=daniel@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=emil.l.velikov@gmail.com \
    --cc=geert+renesas@glider.be \
    --cc=hdegoede@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=robh@kernel.org \
    --cc=sam@ravnborg.org \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).