Linux Audio

Check our new training course

Linux BSP development engineering services

Need help to port Linux and bootloaders to your hardware?
Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-only
   2
   3#include <linux/clk.h>
   4#include <linux/of_clk.h>
   5#include <linux/minmax.h>
   6#include <linux/of_address.h>
   7#include <linux/platform_data/simplefb.h>
   8#include <linux/platform_device.h>
   9#include <linux/pm_domain.h>
  10#include <linux/regulator/consumer.h>
  11
  12#include <drm/drm_aperture.h>
  13#include <drm/drm_atomic.h>
  14#include <drm/drm_atomic_state_helper.h>
  15#include <drm/drm_connector.h>
  16#include <drm/drm_crtc_helper.h>
  17#include <drm/drm_damage_helper.h>
  18#include <drm/drm_device.h>
  19#include <drm/drm_drv.h>
  20#include <drm/drm_fbdev_generic.h>
  21#include <drm/drm_format_helper.h>
  22#include <drm/drm_framebuffer.h>
  23#include <drm/drm_gem_atomic_helper.h>
  24#include <drm/drm_gem_framebuffer_helper.h>
  25#include <drm/drm_gem_shmem_helper.h>
  26#include <drm/drm_managed.h>
  27#include <drm/drm_modeset_helper_vtables.h>
  28#include <drm/drm_probe_helper.h>
 
  29
  30#define DRIVER_NAME	"simpledrm"
  31#define DRIVER_DESC	"DRM driver for simple-framebuffer platform devices"
  32#define DRIVER_DATE	"20200625"
  33#define DRIVER_MAJOR	1
  34#define DRIVER_MINOR	0
  35
  36/*
 
 
 
 
 
 
 
 
 
 
  37 * Helpers for simplefb
  38 */
  39
  40static int
  41simplefb_get_validated_int(struct drm_device *dev, const char *name,
  42			   uint32_t value)
  43{
  44	if (value > INT_MAX) {
  45		drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
  46			name, value);
  47		return -EINVAL;
  48	}
  49	return (int)value;
  50}
  51
  52static int
  53simplefb_get_validated_int0(struct drm_device *dev, const char *name,
  54			    uint32_t value)
  55{
  56	if (!value) {
  57		drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
  58			name, value);
  59		return -EINVAL;
  60	}
  61	return simplefb_get_validated_int(dev, name, value);
  62}
  63
  64static const struct drm_format_info *
  65simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
  66{
  67	static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
  68	const struct simplefb_format *fmt = formats;
  69	const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
  70	const struct drm_format_info *info;
  71
  72	if (!format_name) {
  73		drm_err(dev, "simplefb: missing framebuffer format\n");
  74		return ERR_PTR(-EINVAL);
  75	}
  76
  77	while (fmt < end) {
  78		if (!strcmp(format_name, fmt->name)) {
  79			info = drm_format_info(fmt->fourcc);
  80			if (!info)
  81				return ERR_PTR(-EINVAL);
  82			return info;
  83		}
  84		++fmt;
  85	}
  86
  87	drm_err(dev, "simplefb: unknown framebuffer format %s\n",
  88		format_name);
  89
  90	return ERR_PTR(-EINVAL);
  91}
  92
  93static int
  94simplefb_get_width_pd(struct drm_device *dev,
  95		      const struct simplefb_platform_data *pd)
  96{
  97	return simplefb_get_validated_int0(dev, "width", pd->width);
  98}
  99
 100static int
 101simplefb_get_height_pd(struct drm_device *dev,
 102		       const struct simplefb_platform_data *pd)
 103{
 104	return simplefb_get_validated_int0(dev, "height", pd->height);
 105}
 106
 107static int
 108simplefb_get_stride_pd(struct drm_device *dev,
 109		       const struct simplefb_platform_data *pd)
 110{
 111	return simplefb_get_validated_int(dev, "stride", pd->stride);
 112}
 113
 114static const struct drm_format_info *
 115simplefb_get_format_pd(struct drm_device *dev,
 116		       const struct simplefb_platform_data *pd)
 117{
 118	return simplefb_get_validated_format(dev, pd->format);
 119}
 120
 121static int
 122simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
 123		     const char *name, u32 *value)
 124{
 125	int ret = of_property_read_u32(of_node, name, value);
 126
 127	if (ret)
 128		drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
 129			name, ret);
 130	return ret;
 131}
 132
 133static int
 134simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
 135			const char *name, const char **value)
 136{
 137	int ret = of_property_read_string(of_node, name, value);
 138
 139	if (ret)
 140		drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
 141			name, ret);
 142	return ret;
 143}
 144
 145static int
 146simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
 147{
 148	u32 width;
 149	int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
 150
 151	if (ret)
 152		return ret;
 153	return simplefb_get_validated_int0(dev, "width", width);
 154}
 155
 156static int
 157simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
 158{
 159	u32 height;
 160	int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
 161
 162	if (ret)
 163		return ret;
 164	return simplefb_get_validated_int0(dev, "height", height);
 165}
 166
 167static int
 168simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
 169{
 170	u32 stride;
 171	int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
 172
 173	if (ret)
 174		return ret;
 175	return simplefb_get_validated_int(dev, "stride", stride);
 176}
 177
 178static const struct drm_format_info *
 179simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
 180{
 181	const char *format;
 182	int ret = simplefb_read_string_of(dev, of_node, "format", &format);
 183
 184	if (ret)
 185		return ERR_PTR(ret);
 186	return simplefb_get_validated_format(dev, format);
 187}
 188
 189static struct resource *
 190simplefb_get_memory_of(struct drm_device *dev, struct device_node *of_node)
 191{
 192	struct device_node *np;
 193	struct resource *res;
 194	int err;
 195
 196	np = of_parse_phandle(of_node, "memory-region", 0);
 197	if (!np)
 198		return NULL;
 199
 200	res = devm_kzalloc(dev->dev, sizeof(*res), GFP_KERNEL);
 201	if (!res)
 202		return ERR_PTR(-ENOMEM);
 203
 204	err = of_address_to_resource(np, 0, res);
 205	if (err)
 206		return ERR_PTR(err);
 207
 208	if (of_property_present(of_node, "reg"))
 209		drm_warn(dev, "preferring \"memory-region\" over \"reg\" property\n");
 210
 211	return res;
 212}
 213
 214/*
 215 * Simple Framebuffer device
 216 */
 217
 218struct simpledrm_device {
 219	struct drm_device dev;
 
 220
 221	/* clocks */
 222#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
 223	unsigned int clk_count;
 224	struct clk **clks;
 225#endif
 226	/* regulators */
 227#if defined CONFIG_OF && defined CONFIG_REGULATOR
 228	unsigned int regulator_count;
 229	struct regulator **regulators;
 230#endif
 231	/* power-domains */
 232#if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
 233	int pwr_dom_count;
 234	struct device **pwr_dom_devs;
 235	struct device_link **pwr_dom_links;
 236#endif
 237
 238	/* simplefb settings */
 239	struct drm_display_mode mode;
 240	const struct drm_format_info *format;
 241	unsigned int pitch;
 242
 243	/* memory management */
 244	struct iosys_map screen_base;
 
 245
 246	/* modesetting */
 247	uint32_t formats[8];
 248	size_t nformats;
 249	struct drm_plane primary_plane;
 250	struct drm_crtc crtc;
 251	struct drm_encoder encoder;
 252	struct drm_connector connector;
 
 253};
 254
 255static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
 256{
 257	return container_of(dev, struct simpledrm_device, dev);
 258}
 259
 260/*
 261 * Hardware
 262 */
 263
 264#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
 265/*
 266 * Clock handling code.
 267 *
 268 * Here we handle the clocks property of our "simple-framebuffer" dt node.
 269 * This is necessary so that we can make sure that any clocks needed by
 270 * the display engine that the bootloader set up for us (and for which it
 271 * provided a simplefb dt node), stay up, for the life of the simplefb
 272 * driver.
 273 *
 274 * When the driver unloads, we cleanly disable, and then release the clocks.
 275 *
 276 * We only complain about errors here, no action is taken as the most likely
 277 * error can only happen due to a mismatch between the bootloader which set
 278 * up simplefb, and the clock definitions in the device tree. Chances are
 279 * that there are no adverse effects, and if there are, a clean teardown of
 280 * the fb probe will not help us much either. So just complain and carry on,
 281 * and hope that the user actually gets a working fb at the end of things.
 282 */
 283
 284static void simpledrm_device_release_clocks(void *res)
 285{
 286	struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
 287	unsigned int i;
 288
 289	for (i = 0; i < sdev->clk_count; ++i) {
 290		if (sdev->clks[i]) {
 291			clk_disable_unprepare(sdev->clks[i]);
 292			clk_put(sdev->clks[i]);
 293		}
 294	}
 295}
 296
 297static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
 298{
 299	struct drm_device *dev = &sdev->dev;
 300	struct platform_device *pdev = to_platform_device(dev->dev);
 301	struct device_node *of_node = pdev->dev.of_node;
 302	struct clk *clock;
 303	unsigned int i;
 304	int ret;
 305
 306	if (dev_get_platdata(&pdev->dev) || !of_node)
 307		return 0;
 308
 309	sdev->clk_count = of_clk_get_parent_count(of_node);
 310	if (!sdev->clk_count)
 311		return 0;
 312
 313	sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
 314				  GFP_KERNEL);
 315	if (!sdev->clks)
 316		return -ENOMEM;
 317
 318	for (i = 0; i < sdev->clk_count; ++i) {
 319		clock = of_clk_get(of_node, i);
 320		if (IS_ERR(clock)) {
 321			ret = PTR_ERR(clock);
 322			if (ret == -EPROBE_DEFER)
 323				goto err;
 324			drm_err(dev, "clock %u not found: %d\n", i, ret);
 325			continue;
 326		}
 327		ret = clk_prepare_enable(clock);
 328		if (ret) {
 329			drm_err(dev, "failed to enable clock %u: %d\n",
 330				i, ret);
 331			clk_put(clock);
 332			continue;
 333		}
 334		sdev->clks[i] = clock;
 335	}
 336
 337	return devm_add_action_or_reset(&pdev->dev,
 338					simpledrm_device_release_clocks,
 339					sdev);
 340
 341err:
 342	while (i) {
 343		--i;
 344		if (sdev->clks[i]) {
 345			clk_disable_unprepare(sdev->clks[i]);
 346			clk_put(sdev->clks[i]);
 347		}
 348	}
 349	return ret;
 350}
 351#else
 352static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
 353{
 354	return 0;
 355}
 356#endif
 357
 358#if defined CONFIG_OF && defined CONFIG_REGULATOR
 359
 360#define SUPPLY_SUFFIX "-supply"
 361
 362/*
 363 * Regulator handling code.
 364 *
 365 * Here we handle the num-supplies and vin*-supply properties of our
 366 * "simple-framebuffer" dt node. This is necessary so that we can make sure
 367 * that any regulators needed by the display hardware that the bootloader
 368 * set up for us (and for which it provided a simplefb dt node), stay up,
 369 * for the life of the simplefb driver.
 370 *
 371 * When the driver unloads, we cleanly disable, and then release the
 372 * regulators.
 373 *
 374 * We only complain about errors here, no action is taken as the most likely
 375 * error can only happen due to a mismatch between the bootloader which set
 376 * up simplefb, and the regulator definitions in the device tree. Chances are
 377 * that there are no adverse effects, and if there are, a clean teardown of
 378 * the fb probe will not help us much either. So just complain and carry on,
 379 * and hope that the user actually gets a working fb at the end of things.
 380 */
 381
 382static void simpledrm_device_release_regulators(void *res)
 383{
 384	struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
 385	unsigned int i;
 386
 387	for (i = 0; i < sdev->regulator_count; ++i) {
 388		if (sdev->regulators[i]) {
 389			regulator_disable(sdev->regulators[i]);
 390			regulator_put(sdev->regulators[i]);
 391		}
 392	}
 393}
 394
 395static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
 396{
 397	struct drm_device *dev = &sdev->dev;
 398	struct platform_device *pdev = to_platform_device(dev->dev);
 399	struct device_node *of_node = pdev->dev.of_node;
 400	struct property *prop;
 401	struct regulator *regulator;
 402	const char *p;
 403	unsigned int count = 0, i = 0;
 404	int ret;
 405
 406	if (dev_get_platdata(&pdev->dev) || !of_node)
 407		return 0;
 408
 409	/* Count the number of regulator supplies */
 410	for_each_property_of_node(of_node, prop) {
 411		p = strstr(prop->name, SUPPLY_SUFFIX);
 412		if (p && p != prop->name)
 413			++count;
 414	}
 415
 416	if (!count)
 417		return 0;
 418
 419	sdev->regulators = drmm_kzalloc(dev,
 420					count * sizeof(sdev->regulators[0]),
 421					GFP_KERNEL);
 422	if (!sdev->regulators)
 423		return -ENOMEM;
 424
 425	for_each_property_of_node(of_node, prop) {
 426		char name[32]; /* 32 is max size of property name */
 427		size_t len;
 428
 429		p = strstr(prop->name, SUPPLY_SUFFIX);
 430		if (!p || p == prop->name)
 431			continue;
 432		len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
 433		strscpy(name, prop->name, min(sizeof(name), len));
 434
 435		regulator = regulator_get_optional(&pdev->dev, name);
 436		if (IS_ERR(regulator)) {
 437			ret = PTR_ERR(regulator);
 438			if (ret == -EPROBE_DEFER)
 439				goto err;
 440			drm_err(dev, "regulator %s not found: %d\n",
 441				name, ret);
 442			continue;
 443		}
 444
 445		ret = regulator_enable(regulator);
 446		if (ret) {
 447			drm_err(dev, "failed to enable regulator %u: %d\n",
 448				i, ret);
 449			regulator_put(regulator);
 450			continue;
 451		}
 452
 453		sdev->regulators[i++] = regulator;
 454	}
 455	sdev->regulator_count = i;
 456
 457	return devm_add_action_or_reset(&pdev->dev,
 458					simpledrm_device_release_regulators,
 459					sdev);
 460
 461err:
 462	while (i) {
 463		--i;
 464		if (sdev->regulators[i]) {
 465			regulator_disable(sdev->regulators[i]);
 466			regulator_put(sdev->regulators[i]);
 467		}
 468	}
 469	return ret;
 470}
 471#else
 472static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
 473{
 474	return 0;
 475}
 476#endif
 477
 478#if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
 479/*
 480 * Generic power domain handling code.
 481 *
 482 * Here we handle the power-domains properties of our "simple-framebuffer"
 483 * dt node. This is only necessary if there is more than one power-domain.
 484 * A single power-domains is handled automatically by the driver core. Multiple
 485 * power-domains have to be handled by drivers since the driver core can't know
 486 * the correct power sequencing. Power sequencing is not an issue for simpledrm
 487 * since the bootloader has put the power domains already in the correct state.
 488 * simpledrm has only to ensure they remain active for its lifetime.
 489 *
 490 * When the driver unloads, we detach from the power-domains.
 491 *
 492 * We only complain about errors here, no action is taken as the most likely
 493 * error can only happen due to a mismatch between the bootloader which set
 494 * up the "simple-framebuffer" dt node, and the PM domain providers in the
 495 * device tree. Chances are that there are no adverse effects, and if there are,
 496 * a clean teardown of the fb probe will not help us much either. So just
 497 * complain and carry on, and hope that the user actually gets a working fb at
 498 * the end of things.
 499 */
 500static void simpledrm_device_detach_genpd(void *res)
 
 
 501{
 502	int i;
 503	struct simpledrm_device *sdev = res;
 504
 505	if (sdev->pwr_dom_count <= 1)
 506		return;
 507
 508	for (i = sdev->pwr_dom_count - 1; i >= 0; i--) {
 509		if (sdev->pwr_dom_links[i])
 510			device_link_del(sdev->pwr_dom_links[i]);
 511		if (!IS_ERR_OR_NULL(sdev->pwr_dom_devs[i]))
 512			dev_pm_domain_detach(sdev->pwr_dom_devs[i], true);
 513	}
 514}
 515
 516static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
 517{
 518	struct device *dev = sdev->dev.dev;
 519	int i;
 
 
 
 
 520
 521	sdev->pwr_dom_count = of_count_phandle_with_args(dev->of_node, "power-domains",
 522							 "#power-domain-cells");
 523	/*
 524	 * Single power-domain devices are handled by driver core nothing to do
 525	 * here. The same for device nodes without "power-domains" property.
 526	 */
 527	if (sdev->pwr_dom_count <= 1)
 528		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 529
 530	sdev->pwr_dom_devs = devm_kcalloc(dev, sdev->pwr_dom_count,
 531					       sizeof(*sdev->pwr_dom_devs),
 532					       GFP_KERNEL);
 533	if (!sdev->pwr_dom_devs)
 534		return -ENOMEM;
 535
 536	sdev->pwr_dom_links = devm_kcalloc(dev, sdev->pwr_dom_count,
 537						sizeof(*sdev->pwr_dom_links),
 538						GFP_KERNEL);
 539	if (!sdev->pwr_dom_links)
 540		return -ENOMEM;
 541
 542	for (i = 0; i < sdev->pwr_dom_count; i++) {
 543		sdev->pwr_dom_devs[i] = dev_pm_domain_attach_by_id(dev, i);
 544		if (IS_ERR(sdev->pwr_dom_devs[i])) {
 545			int ret = PTR_ERR(sdev->pwr_dom_devs[i]);
 546			if (ret == -EPROBE_DEFER) {
 547				simpledrm_device_detach_genpd(sdev);
 548				return ret;
 549			}
 550			drm_warn(&sdev->dev,
 551				 "pm_domain_attach_by_id(%u) failed: %d\n", i, ret);
 552			continue;
 553		}
 554
 555		sdev->pwr_dom_links[i] = device_link_add(dev,
 556							 sdev->pwr_dom_devs[i],
 557							 DL_FLAG_STATELESS |
 558							 DL_FLAG_PM_RUNTIME |
 559							 DL_FLAG_RPM_ACTIVE);
 560		if (!sdev->pwr_dom_links[i])
 561			drm_warn(&sdev->dev, "failed to link power-domain %d\n", i);
 562	}
 563
 564	return devm_add_action_or_reset(dev, simpledrm_device_detach_genpd, sdev);
 565}
 566#else
 567static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
 568{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 569	return 0;
 570}
 571#endif
 572
 573/*
 574 * Modesetting
 575 */
 576
 577static const uint64_t simpledrm_primary_plane_format_modifiers[] = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 578	DRM_FORMAT_MOD_LINEAR,
 579	DRM_FORMAT_MOD_INVALID
 580};
 581
 582static int simpledrm_primary_plane_helper_atomic_check(struct drm_plane *plane,
 583						       struct drm_atomic_state *state)
 584{
 585	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
 586	struct drm_shadow_plane_state *new_shadow_plane_state =
 587		to_drm_shadow_plane_state(new_plane_state);
 588	struct drm_framebuffer *new_fb = new_plane_state->fb;
 589	struct drm_crtc *new_crtc = new_plane_state->crtc;
 590	struct drm_crtc_state *new_crtc_state = NULL;
 591	struct drm_device *dev = plane->dev;
 592	struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
 593	int ret;
 594
 595	if (new_crtc)
 596		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
 597
 598	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
 599						  DRM_PLANE_NO_SCALING,
 600						  DRM_PLANE_NO_SCALING,
 601						  false, false);
 602	if (ret)
 603		return ret;
 604	else if (!new_plane_state->visible)
 605		return 0;
 606
 607	if (new_fb->format != sdev->format) {
 608		void *buf;
 609
 610		/* format conversion necessary; reserve buffer */
 611		buf = drm_format_conv_state_reserve(&new_shadow_plane_state->fmtcnv_state,
 612						    sdev->pitch, GFP_KERNEL);
 613		if (!buf)
 614			return -ENOMEM;
 615	}
 616
 617	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 618}
 619
 620static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
 621							 struct drm_atomic_state *state)
 
 
 622{
 623	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
 624	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
 625	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
 626	struct drm_framebuffer *fb = plane_state->fb;
 627	struct drm_device *dev = plane->dev;
 628	struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
 629	struct drm_atomic_helper_damage_iter iter;
 630	struct drm_rect damage;
 631	int ret, idx;
 632
 633	ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
 634	if (ret)
 635		return;
 636
 637	if (!drm_dev_enter(dev, &idx))
 638		goto out_drm_gem_fb_end_cpu_access;
 639
 640	drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
 641	drm_atomic_for_each_plane_damage(&iter, &damage) {
 642		struct drm_rect dst_clip = plane_state->dst;
 643		struct iosys_map dst = sdev->screen_base;
 644
 645		if (!drm_rect_intersect(&dst_clip, &damage))
 646			continue;
 647
 648		iosys_map_incr(&dst, drm_fb_clip_offset(sdev->pitch, sdev->format, &dst_clip));
 649		drm_fb_blit(&dst, &sdev->pitch, sdev->format->format, shadow_plane_state->data,
 650			    fb, &damage, &shadow_plane_state->fmtcnv_state);
 651	}
 652
 
 
 653	drm_dev_exit(idx);
 654out_drm_gem_fb_end_cpu_access:
 655	drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
 656}
 657
 658static void simpledrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
 659							  struct drm_atomic_state *state)
 660{
 661	struct drm_device *dev = plane->dev;
 662	struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
 663	int idx;
 664
 665	if (!drm_dev_enter(dev, &idx))
 666		return;
 667
 668	/* Clear screen to black if disabled */
 669	iosys_map_memset(&sdev->screen_base, 0, 0, sdev->pitch * sdev->mode.vdisplay);
 670
 671	drm_dev_exit(idx);
 672}
 673
 674static const struct drm_plane_helper_funcs simpledrm_primary_plane_helper_funcs = {
 675	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
 676	.atomic_check = simpledrm_primary_plane_helper_atomic_check,
 677	.atomic_update = simpledrm_primary_plane_helper_atomic_update,
 678	.atomic_disable = simpledrm_primary_plane_helper_atomic_disable,
 679};
 680
 681static const struct drm_plane_funcs simpledrm_primary_plane_funcs = {
 682	.update_plane = drm_atomic_helper_update_plane,
 683	.disable_plane = drm_atomic_helper_disable_plane,
 684	.destroy = drm_plane_cleanup,
 685	DRM_GEM_SHADOW_PLANE_FUNCS,
 686};
 687
 688static enum drm_mode_status simpledrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
 689							     const struct drm_display_mode *mode)
 690{
 691	struct simpledrm_device *sdev = simpledrm_device_of_dev(crtc->dev);
 
 
 
 
 
 
 
 692
 693	return drm_crtc_helper_mode_valid_fixed(crtc, mode, &sdev->mode);
 694}
 695
 696/*
 697 * The CRTC is always enabled. Screen updates are performed by
 698 * the primary plane's atomic_update function. Disabling clears
 699 * the screen in the primary plane's atomic_disable function.
 700 */
 701static const struct drm_crtc_helper_funcs simpledrm_crtc_helper_funcs = {
 702	.mode_valid = simpledrm_crtc_helper_mode_valid,
 703	.atomic_check = drm_crtc_helper_atomic_check,
 704};
 705
 706static const struct drm_crtc_funcs simpledrm_crtc_funcs = {
 707	.reset = drm_atomic_helper_crtc_reset,
 708	.destroy = drm_crtc_cleanup,
 709	.set_config = drm_atomic_helper_set_config,
 710	.page_flip = drm_atomic_helper_page_flip,
 711	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
 712	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
 713};
 714
 715static const struct drm_encoder_funcs simpledrm_encoder_funcs = {
 716	.destroy = drm_encoder_cleanup,
 717};
 718
 719static int simpledrm_connector_helper_get_modes(struct drm_connector *connector)
 720{
 721	struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev);
 722
 723	return drm_connector_helper_get_modes_fixed(connector, &sdev->mode);
 724}
 725
 726static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
 727	.get_modes = simpledrm_connector_helper_get_modes,
 728};
 729
 730static const struct drm_connector_funcs simpledrm_connector_funcs = {
 731	.reset = drm_atomic_helper_connector_reset,
 732	.fill_modes = drm_helper_probe_single_connector_modes,
 733	.destroy = drm_connector_cleanup,
 734	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
 735	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
 736};
 737
 738static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
 739	.fb_create = drm_gem_fb_create_with_dirty,
 740	.atomic_check = drm_atomic_helper_check,
 741	.atomic_commit = drm_atomic_helper_commit,
 742};
 743
 744/*
 745 * Init / Cleanup
 746 */
 747
 748static struct drm_display_mode simpledrm_mode(unsigned int width,
 749					      unsigned int height,
 750					      unsigned int width_mm,
 751					      unsigned int height_mm)
 752{
 753	const struct drm_display_mode mode = {
 754		DRM_MODE_INIT(60, width, height, width_mm, height_mm)
 755	};
 756
 757	return mode;
 758}
 759
 760static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv,
 761							struct platform_device *pdev)
 762{
 763	const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
 764	struct device_node *of_node = pdev->dev.of_node;
 765	struct simpledrm_device *sdev;
 766	struct drm_device *dev;
 767	int width, height, stride;
 768	int width_mm = 0, height_mm = 0;
 769	struct device_node *panel_node;
 770	const struct drm_format_info *format;
 771	struct resource *res, *mem = NULL;
 772	struct drm_plane *primary_plane;
 773	struct drm_crtc *crtc;
 774	struct drm_encoder *encoder;
 775	struct drm_connector *connector;
 776	unsigned long max_width, max_height;
 777	size_t nformats;
 778	int ret;
 779
 780	sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device, dev);
 781	if (IS_ERR(sdev))
 782		return ERR_CAST(sdev);
 783	dev = &sdev->dev;
 784	platform_set_drvdata(pdev, sdev);
 785
 786	/*
 787	 * Hardware settings
 788	 */
 789
 790	ret = simpledrm_device_init_clocks(sdev);
 791	if (ret)
 792		return ERR_PTR(ret);
 793	ret = simpledrm_device_init_regulators(sdev);
 794	if (ret)
 795		return ERR_PTR(ret);
 796	ret = simpledrm_device_attach_genpd(sdev);
 797	if (ret)
 798		return ERR_PTR(ret);
 799
 800	if (pd) {
 801		width = simplefb_get_width_pd(dev, pd);
 802		if (width < 0)
 803			return ERR_PTR(width);
 804		height = simplefb_get_height_pd(dev, pd);
 805		if (height < 0)
 806			return ERR_PTR(height);
 807		stride = simplefb_get_stride_pd(dev, pd);
 808		if (stride < 0)
 809			return ERR_PTR(stride);
 810		format = simplefb_get_format_pd(dev, pd);
 811		if (IS_ERR(format))
 812			return ERR_CAST(format);
 813	} else if (of_node) {
 814		width = simplefb_get_width_of(dev, of_node);
 815		if (width < 0)
 816			return ERR_PTR(width);
 817		height = simplefb_get_height_of(dev, of_node);
 818		if (height < 0)
 819			return ERR_PTR(height);
 820		stride = simplefb_get_stride_of(dev, of_node);
 821		if (stride < 0)
 822			return ERR_PTR(stride);
 823		format = simplefb_get_format_of(dev, of_node);
 824		if (IS_ERR(format))
 825			return ERR_CAST(format);
 826		mem = simplefb_get_memory_of(dev, of_node);
 827		if (IS_ERR(mem))
 828			return ERR_CAST(mem);
 829		panel_node = of_parse_phandle(of_node, "panel", 0);
 830		if (panel_node) {
 831			simplefb_read_u32_of(dev, panel_node, "width-mm", &width_mm);
 832			simplefb_read_u32_of(dev, panel_node, "height-mm", &height_mm);
 833			of_node_put(panel_node);
 834		}
 835	} else {
 836		drm_err(dev, "no simplefb configuration found\n");
 837		return ERR_PTR(-ENODEV);
 838	}
 839	if (!stride) {
 840		stride = drm_format_info_min_pitch(format, 0, width);
 841		if (drm_WARN_ON(dev, !stride))
 842			return ERR_PTR(-EINVAL);
 843	}
 844
 845	/*
 846	 * Assume a monitor resolution of 96 dpi if physical dimensions
 847	 * are not specified to get a somewhat reasonable screen size.
 
 
 
 848	 */
 849	if (!width_mm)
 850		width_mm = DRM_MODE_RES_MM(width, 96ul);
 851	if (!height_mm)
 852		height_mm = DRM_MODE_RES_MM(height, 96ul);
 853
 854	sdev->mode = simpledrm_mode(width, height, width_mm, height_mm);
 855	sdev->format = format;
 856	sdev->pitch = stride;
 857
 858	drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sdev->mode));
 859	drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
 860		&format->format, width, height, stride);
 861
 862	/*
 863	 * Memory management
 864	 */
 865
 866	if (mem) {
 867		void *screen_base;
 868
 869		ret = devm_aperture_acquire_from_firmware(dev, mem->start, resource_size(mem));
 870		if (ret) {
 871			drm_err(dev, "could not acquire memory range %pr: %d\n", mem, ret);
 872			return ERR_PTR(ret);
 873		}
 874
 875		drm_dbg(dev, "using system memory framebuffer at %pr\n", mem);
 876
 877		screen_base = devm_memremap(dev->dev, mem->start, resource_size(mem), MEMREMAP_WC);
 878		if (IS_ERR(screen_base))
 879			return screen_base;
 880
 881		iosys_map_set_vaddr(&sdev->screen_base, screen_base);
 882	} else {
 883		void __iomem *screen_base;
 884
 885		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 886		if (!res)
 887			return ERR_PTR(-EINVAL);
 888
 889		ret = devm_aperture_acquire_from_firmware(dev, res->start, resource_size(res));
 890		if (ret) {
 891			drm_err(dev, "could not acquire memory range %pr: %d\n", res, ret);
 892			return ERR_PTR(ret);
 893		}
 894
 895		drm_dbg(dev, "using I/O memory framebuffer at %pr\n", res);
 
 
 
 
 
 
 
 
 896
 897		mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
 898					      drv->name);
 899		if (!mem) {
 900			/*
 901			 * We cannot make this fatal. Sometimes this comes from magic
 902			 * spaces our resource handlers simply don't know about. Use
 903			 * the I/O-memory resource as-is and try to map that instead.
 904			 */
 905			drm_warn(dev, "could not acquire memory region %pr\n", res);
 906			mem = res;
 907		}
 908
 909		screen_base = devm_ioremap_wc(&pdev->dev, mem->start, resource_size(mem));
 910		if (!screen_base)
 911			return ERR_PTR(-ENOMEM);
 
 
 
 
 912
 913		iosys_map_set_vaddr_iomem(&sdev->screen_base, screen_base);
 914	}
 
 
 
 915
 916	/*
 917	 * Modesetting
 918	 */
 919
 920	ret = drmm_mode_config_init(dev);
 
 
 921	if (ret)
 922		return ERR_PTR(ret);
 923
 924	max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
 925	max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
 926
 927	dev->mode_config.min_width = width;
 928	dev->mode_config.max_width = max_width;
 929	dev->mode_config.min_height = height;
 930	dev->mode_config.max_height = max_height;
 931	dev->mode_config.preferred_depth = format->depth;
 932	dev->mode_config.funcs = &simpledrm_mode_config_funcs;
 933
 934	/* Primary plane */
 
 
 
 
 
 
 
 
 935
 936	nformats = drm_fb_build_fourcc_list(dev, &format->format, 1,
 937					    sdev->formats, ARRAY_SIZE(sdev->formats));
 
 
 
 
 938
 939	primary_plane = &sdev->primary_plane;
 940	ret = drm_universal_plane_init(dev, primary_plane, 0, &simpledrm_primary_plane_funcs,
 941				       sdev->formats, nformats,
 942				       simpledrm_primary_plane_format_modifiers,
 943				       DRM_PLANE_TYPE_PRIMARY, NULL);
 944	if (ret)
 945		return ERR_PTR(ret);
 946	drm_plane_helper_add(primary_plane, &simpledrm_primary_plane_helper_funcs);
 947	drm_plane_enable_fb_damage_clips(primary_plane);
 948
 949	/* CRTC */
 950
 951	crtc = &sdev->crtc;
 952	ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
 953					&simpledrm_crtc_funcs, NULL);
 954	if (ret)
 955		return ERR_PTR(ret);
 956	drm_crtc_helper_add(crtc, &simpledrm_crtc_helper_funcs);
 957
 958	/* Encoder */
 959
 960	encoder = &sdev->encoder;
 961	ret = drm_encoder_init(dev, encoder, &simpledrm_encoder_funcs,
 962			       DRM_MODE_ENCODER_NONE, NULL);
 963	if (ret)
 964		return ERR_PTR(ret);
 965	encoder->possible_crtcs = drm_crtc_mask(crtc);
 966
 967	/* Connector */
 968
 969	connector = &sdev->connector;
 970	ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
 971				 DRM_MODE_CONNECTOR_Unknown);
 972	if (ret)
 973		return ERR_PTR(ret);
 974	drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
 975	drm_connector_set_panel_orientation_with_quirk(connector,
 976						       DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
 977						       width, height);
 978
 979	ret = drm_connector_attach_encoder(connector, encoder);
 980	if (ret)
 981		return ERR_PTR(ret);
 982
 983	drm_mode_config_reset(dev);
 984
 985	return sdev;
 986}
 987
 988/*
 989 * DRM driver
 990 */
 991
 992DEFINE_DRM_GEM_FOPS(simpledrm_fops);
 993
 994static struct drm_driver simpledrm_driver = {
 995	DRM_GEM_SHMEM_DRIVER_OPS,
 996	.name			= DRIVER_NAME,
 997	.desc			= DRIVER_DESC,
 998	.date			= DRIVER_DATE,
 999	.major			= DRIVER_MAJOR,
1000	.minor			= DRIVER_MINOR,
1001	.driver_features	= DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
1002	.fops			= &simpledrm_fops,
1003};
1004
1005/*
1006 * Platform driver
1007 */
1008
1009static int simpledrm_probe(struct platform_device *pdev)
1010{
1011	struct simpledrm_device *sdev;
1012	struct drm_device *dev;
1013	unsigned int color_mode;
1014	int ret;
1015
1016	sdev = simpledrm_device_create(&simpledrm_driver, pdev);
1017	if (IS_ERR(sdev))
1018		return PTR_ERR(sdev);
1019	dev = &sdev->dev;
1020
1021	ret = drm_dev_register(dev, 0);
1022	if (ret)
1023		return ret;
1024
1025	color_mode = drm_format_info_bpp(sdev->format, 0);
1026	if (color_mode == 16)
1027		color_mode = sdev->format->depth; // can be 15 or 16
1028
1029	drm_fbdev_generic_setup(dev, color_mode);
1030
1031	return 0;
1032}
1033
1034static void simpledrm_remove(struct platform_device *pdev)
1035{
1036	struct simpledrm_device *sdev = platform_get_drvdata(pdev);
1037	struct drm_device *dev = &sdev->dev;
1038
1039	drm_dev_unplug(dev);
 
 
1040}
1041
1042static const struct of_device_id simpledrm_of_match_table[] = {
1043	{ .compatible = "simple-framebuffer", },
1044	{ },
1045};
1046MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
1047
1048static struct platform_driver simpledrm_platform_driver = {
1049	.driver = {
1050		.name = "simple-framebuffer", /* connect to sysfb */
1051		.of_match_table = simpledrm_of_match_table,
1052	},
1053	.probe = simpledrm_probe,
1054	.remove_new = simpledrm_remove,
1055};
1056
1057module_platform_driver(simpledrm_platform_driver);
1058
1059MODULE_DESCRIPTION(DRIVER_DESC);
1060MODULE_LICENSE("GPL v2");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2
  3#include <linux/clk.h>
  4#include <linux/of_clk.h>
 
 
  5#include <linux/platform_data/simplefb.h>
  6#include <linux/platform_device.h>
 
  7#include <linux/regulator/consumer.h>
  8
  9#include <drm/drm_aperture.h>
 
 10#include <drm/drm_atomic_state_helper.h>
 11#include <drm/drm_connector.h>
 
 12#include <drm/drm_damage_helper.h>
 13#include <drm/drm_device.h>
 14#include <drm/drm_drv.h>
 15#include <drm/drm_fb_helper.h>
 16#include <drm/drm_format_helper.h>
 
 17#include <drm/drm_gem_atomic_helper.h>
 18#include <drm/drm_gem_framebuffer_helper.h>
 19#include <drm/drm_gem_shmem_helper.h>
 20#include <drm/drm_managed.h>
 21#include <drm/drm_modeset_helper_vtables.h>
 22#include <drm/drm_probe_helper.h>
 23#include <drm/drm_simple_kms_helper.h>
 24
 25#define DRIVER_NAME	"simpledrm"
 26#define DRIVER_DESC	"DRM driver for simple-framebuffer platform devices"
 27#define DRIVER_DATE	"20200625"
 28#define DRIVER_MAJOR	1
 29#define DRIVER_MINOR	0
 30
 31/*
 32 * Assume a monitor resolution of 96 dpi to
 33 * get a somewhat reasonable screen size.
 34 */
 35#define RES_MM(d)	\
 36	(((d) * 254ul) / (96ul * 10ul))
 37
 38#define SIMPLEDRM_MODE(hd, vd)	\
 39	DRM_SIMPLE_MODE(hd, vd, RES_MM(hd), RES_MM(vd))
 40
 41/*
 42 * Helpers for simplefb
 43 */
 44
 45static int
 46simplefb_get_validated_int(struct drm_device *dev, const char *name,
 47			   uint32_t value)
 48{
 49	if (value > INT_MAX) {
 50		drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
 51			name, value);
 52		return -EINVAL;
 53	}
 54	return (int)value;
 55}
 56
 57static int
 58simplefb_get_validated_int0(struct drm_device *dev, const char *name,
 59			    uint32_t value)
 60{
 61	if (!value) {
 62		drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
 63			name, value);
 64		return -EINVAL;
 65	}
 66	return simplefb_get_validated_int(dev, name, value);
 67}
 68
 69static const struct drm_format_info *
 70simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
 71{
 72	static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
 73	const struct simplefb_format *fmt = formats;
 74	const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
 75	const struct drm_format_info *info;
 76
 77	if (!format_name) {
 78		drm_err(dev, "simplefb: missing framebuffer format\n");
 79		return ERR_PTR(-EINVAL);
 80	}
 81
 82	while (fmt < end) {
 83		if (!strcmp(format_name, fmt->name)) {
 84			info = drm_format_info(fmt->fourcc);
 85			if (!info)
 86				return ERR_PTR(-EINVAL);
 87			return info;
 88		}
 89		++fmt;
 90	}
 91
 92	drm_err(dev, "simplefb: unknown framebuffer format %s\n",
 93		format_name);
 94
 95	return ERR_PTR(-EINVAL);
 96}
 97
 98static int
 99simplefb_get_width_pd(struct drm_device *dev,
100		      const struct simplefb_platform_data *pd)
101{
102	return simplefb_get_validated_int0(dev, "width", pd->width);
103}
104
105static int
106simplefb_get_height_pd(struct drm_device *dev,
107		       const struct simplefb_platform_data *pd)
108{
109	return simplefb_get_validated_int0(dev, "height", pd->height);
110}
111
112static int
113simplefb_get_stride_pd(struct drm_device *dev,
114		       const struct simplefb_platform_data *pd)
115{
116	return simplefb_get_validated_int(dev, "stride", pd->stride);
117}
118
119static const struct drm_format_info *
120simplefb_get_format_pd(struct drm_device *dev,
121		       const struct simplefb_platform_data *pd)
122{
123	return simplefb_get_validated_format(dev, pd->format);
124}
125
126static int
127simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
128		     const char *name, u32 *value)
129{
130	int ret = of_property_read_u32(of_node, name, value);
131
132	if (ret)
133		drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
134			name, ret);
135	return ret;
136}
137
138static int
139simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
140			const char *name, const char **value)
141{
142	int ret = of_property_read_string(of_node, name, value);
143
144	if (ret)
145		drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
146			name, ret);
147	return ret;
148}
149
150static int
151simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
152{
153	u32 width;
154	int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
155
156	if (ret)
157		return ret;
158	return simplefb_get_validated_int0(dev, "width", width);
159}
160
161static int
162simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
163{
164	u32 height;
165	int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
166
167	if (ret)
168		return ret;
169	return simplefb_get_validated_int0(dev, "height", height);
170}
171
172static int
173simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
174{
175	u32 stride;
176	int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
177
178	if (ret)
179		return ret;
180	return simplefb_get_validated_int(dev, "stride", stride);
181}
182
183static const struct drm_format_info *
184simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
185{
186	const char *format;
187	int ret = simplefb_read_string_of(dev, of_node, "format", &format);
188
189	if (ret)
190		return ERR_PTR(ret);
191	return simplefb_get_validated_format(dev, format);
192}
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194/*
195 * Simple Framebuffer device
196 */
197
198struct simpledrm_device {
199	struct drm_device dev;
200	struct platform_device *pdev;
201
202	/* clocks */
203#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
204	unsigned int clk_count;
205	struct clk **clks;
206#endif
207	/* regulators */
208#if defined CONFIG_OF && defined CONFIG_REGULATOR
209	unsigned int regulator_count;
210	struct regulator **regulators;
211#endif
 
 
 
 
 
 
212
213	/* simplefb settings */
214	struct drm_display_mode mode;
215	const struct drm_format_info *format;
216	unsigned int pitch;
217
218	/* memory management */
219	struct resource *mem;
220	void __iomem *screen_base;
221
222	/* modesetting */
223	uint32_t formats[8];
224	size_t nformats;
 
 
 
225	struct drm_connector connector;
226	struct drm_simple_display_pipe pipe;
227};
228
229static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
230{
231	return container_of(dev, struct simpledrm_device, dev);
232}
233
234/*
235 * Hardware
236 */
237
238#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
239/*
240 * Clock handling code.
241 *
242 * Here we handle the clocks property of our "simple-framebuffer" dt node.
243 * This is necessary so that we can make sure that any clocks needed by
244 * the display engine that the bootloader set up for us (and for which it
245 * provided a simplefb dt node), stay up, for the life of the simplefb
246 * driver.
247 *
248 * When the driver unloads, we cleanly disable, and then release the clocks.
249 *
250 * We only complain about errors here, no action is taken as the most likely
251 * error can only happen due to a mismatch between the bootloader which set
252 * up simplefb, and the clock definitions in the device tree. Chances are
253 * that there are no adverse effects, and if there are, a clean teardown of
254 * the fb probe will not help us much either. So just complain and carry on,
255 * and hope that the user actually gets a working fb at the end of things.
256 */
257
258static void simpledrm_device_release_clocks(void *res)
259{
260	struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
261	unsigned int i;
262
263	for (i = 0; i < sdev->clk_count; ++i) {
264		if (sdev->clks[i]) {
265			clk_disable_unprepare(sdev->clks[i]);
266			clk_put(sdev->clks[i]);
267		}
268	}
269}
270
271static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
272{
273	struct drm_device *dev = &sdev->dev;
274	struct platform_device *pdev = sdev->pdev;
275	struct device_node *of_node = pdev->dev.of_node;
276	struct clk *clock;
277	unsigned int i;
278	int ret;
279
280	if (dev_get_platdata(&pdev->dev) || !of_node)
281		return 0;
282
283	sdev->clk_count = of_clk_get_parent_count(of_node);
284	if (!sdev->clk_count)
285		return 0;
286
287	sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
288				  GFP_KERNEL);
289	if (!sdev->clks)
290		return -ENOMEM;
291
292	for (i = 0; i < sdev->clk_count; ++i) {
293		clock = of_clk_get(of_node, i);
294		if (IS_ERR(clock)) {
295			ret = PTR_ERR(clock);
296			if (ret == -EPROBE_DEFER)
297				goto err;
298			drm_err(dev, "clock %u not found: %d\n", i, ret);
299			continue;
300		}
301		ret = clk_prepare_enable(clock);
302		if (ret) {
303			drm_err(dev, "failed to enable clock %u: %d\n",
304				i, ret);
305			clk_put(clock);
306			continue;
307		}
308		sdev->clks[i] = clock;
309	}
310
311	return devm_add_action_or_reset(&pdev->dev,
312					simpledrm_device_release_clocks,
313					sdev);
314
315err:
316	while (i) {
317		--i;
318		if (sdev->clks[i]) {
319			clk_disable_unprepare(sdev->clks[i]);
320			clk_put(sdev->clks[i]);
321		}
322	}
323	return ret;
324}
325#else
326static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
327{
328	return 0;
329}
330#endif
331
332#if defined CONFIG_OF && defined CONFIG_REGULATOR
333
334#define SUPPLY_SUFFIX "-supply"
335
336/*
337 * Regulator handling code.
338 *
339 * Here we handle the num-supplies and vin*-supply properties of our
340 * "simple-framebuffer" dt node. This is necessary so that we can make sure
341 * that any regulators needed by the display hardware that the bootloader
342 * set up for us (and for which it provided a simplefb dt node), stay up,
343 * for the life of the simplefb driver.
344 *
345 * When the driver unloads, we cleanly disable, and then release the
346 * regulators.
347 *
348 * We only complain about errors here, no action is taken as the most likely
349 * error can only happen due to a mismatch between the bootloader which set
350 * up simplefb, and the regulator definitions in the device tree. Chances are
351 * that there are no adverse effects, and if there are, a clean teardown of
352 * the fb probe will not help us much either. So just complain and carry on,
353 * and hope that the user actually gets a working fb at the end of things.
354 */
355
356static void simpledrm_device_release_regulators(void *res)
357{
358	struct simpledrm_device *sdev = simpledrm_device_of_dev(res);
359	unsigned int i;
360
361	for (i = 0; i < sdev->regulator_count; ++i) {
362		if (sdev->regulators[i]) {
363			regulator_disable(sdev->regulators[i]);
364			regulator_put(sdev->regulators[i]);
365		}
366	}
367}
368
369static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
370{
371	struct drm_device *dev = &sdev->dev;
372	struct platform_device *pdev = sdev->pdev;
373	struct device_node *of_node = pdev->dev.of_node;
374	struct property *prop;
375	struct regulator *regulator;
376	const char *p;
377	unsigned int count = 0, i = 0;
378	int ret;
379
380	if (dev_get_platdata(&pdev->dev) || !of_node)
381		return 0;
382
383	/* Count the number of regulator supplies */
384	for_each_property_of_node(of_node, prop) {
385		p = strstr(prop->name, SUPPLY_SUFFIX);
386		if (p && p != prop->name)
387			++count;
388	}
389
390	if (!count)
391		return 0;
392
393	sdev->regulators = drmm_kzalloc(dev,
394					count * sizeof(sdev->regulators[0]),
395					GFP_KERNEL);
396	if (!sdev->regulators)
397		return -ENOMEM;
398
399	for_each_property_of_node(of_node, prop) {
400		char name[32]; /* 32 is max size of property name */
401		size_t len;
402
403		p = strstr(prop->name, SUPPLY_SUFFIX);
404		if (!p || p == prop->name)
405			continue;
406		len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
407		strscpy(name, prop->name, min(sizeof(name), len));
408
409		regulator = regulator_get_optional(&pdev->dev, name);
410		if (IS_ERR(regulator)) {
411			ret = PTR_ERR(regulator);
412			if (ret == -EPROBE_DEFER)
413				goto err;
414			drm_err(dev, "regulator %s not found: %d\n",
415				name, ret);
416			continue;
417		}
418
419		ret = regulator_enable(regulator);
420		if (ret) {
421			drm_err(dev, "failed to enable regulator %u: %d\n",
422				i, ret);
423			regulator_put(regulator);
424			continue;
425		}
426
427		sdev->regulators[i++] = regulator;
428	}
429	sdev->regulator_count = i;
430
431	return devm_add_action_or_reset(&pdev->dev,
432					simpledrm_device_release_regulators,
433					sdev);
434
435err:
436	while (i) {
437		--i;
438		if (sdev->regulators[i]) {
439			regulator_disable(sdev->regulators[i]);
440			regulator_put(sdev->regulators[i]);
441		}
442	}
443	return ret;
444}
445#else
446static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
447{
448	return 0;
449}
450#endif
451
 
452/*
453 *  Simplefb settings
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454 */
455
456static struct drm_display_mode simpledrm_mode(unsigned int width,
457					      unsigned int height)
458{
459	struct drm_display_mode mode = { SIMPLEDRM_MODE(width, height) };
 
460
461	mode.clock = 60 /* Hz */ * mode.hdisplay * mode.vdisplay;
462	drm_mode_set_name(&mode);
463
464	return mode;
 
 
 
 
 
465}
466
467static int simpledrm_device_init_fb(struct simpledrm_device *sdev)
468{
469	int width, height, stride;
470	const struct drm_format_info *format;
471	struct drm_device *dev = &sdev->dev;
472	struct platform_device *pdev = sdev->pdev;
473	const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
474	struct device_node *of_node = pdev->dev.of_node;
475
476	if (pd) {
477		width = simplefb_get_width_pd(dev, pd);
478		if (width < 0)
479			return width;
480		height = simplefb_get_height_pd(dev, pd);
481		if (height < 0)
482			return height;
483		stride = simplefb_get_stride_pd(dev, pd);
484		if (stride < 0)
485			return stride;
486		format = simplefb_get_format_pd(dev, pd);
487		if (IS_ERR(format))
488			return PTR_ERR(format);
489	} else if (of_node) {
490		width = simplefb_get_width_of(dev, of_node);
491		if (width < 0)
492			return width;
493		height = simplefb_get_height_of(dev, of_node);
494		if (height < 0)
495			return height;
496		stride = simplefb_get_stride_of(dev, of_node);
497		if (stride < 0)
498			return stride;
499		format = simplefb_get_format_of(dev, of_node);
500		if (IS_ERR(format))
501			return PTR_ERR(format);
502	} else {
503		drm_err(dev, "no simplefb configuration found\n");
504		return -ENODEV;
505	}
506
507	sdev->mode = simpledrm_mode(width, height);
508	sdev->format = format;
509	sdev->pitch = stride;
 
 
510
511	drm_dbg_kms(dev, "display mode={" DRM_MODE_FMT "}\n",
512		    DRM_MODE_ARG(&sdev->mode));
513	drm_dbg_kms(dev,
514		    "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
515		    &format->format, width, height, stride);
516
517	return 0;
518}
 
 
 
 
 
 
 
 
 
 
519
520/*
521 * Memory management
522 */
 
 
 
 
 
523
524static int simpledrm_device_init_mm(struct simpledrm_device *sdev)
 
 
 
525{
526	struct drm_device *dev = &sdev->dev;
527	struct platform_device *pdev = sdev->pdev;
528	struct resource *mem;
529	void __iomem *screen_base;
530	int ret;
531
532	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
533	if (!mem)
534		return -EINVAL;
535
536	ret = devm_aperture_acquire_from_firmware(dev, mem->start, resource_size(mem));
537	if (ret) {
538		drm_err(dev, "could not acquire memory range %pr: error %d\n",
539			mem, ret);
540		return ret;
541	}
542
543	screen_base = devm_ioremap_wc(&pdev->dev, mem->start,
544				      resource_size(mem));
545	if (!screen_base)
546		return -ENOMEM;
547
548	sdev->mem = mem;
549	sdev->screen_base = screen_base;
550
551	return 0;
552}
 
553
554/*
555 * Modesetting
556 */
557
558/*
559 * Support all formats of simplefb and maybe more; in order
560 * of preference. The display's update function will do any
561 * conversion necessary.
562 *
563 * TODO: Add blit helpers for remaining formats and uncomment
564 *       constants.
565 */
566static const uint32_t simpledrm_default_formats[] = {
567	DRM_FORMAT_XRGB8888,
568	DRM_FORMAT_ARGB8888,
569	DRM_FORMAT_RGB565,
570	//DRM_FORMAT_XRGB1555,
571	//DRM_FORMAT_ARGB1555,
572	DRM_FORMAT_RGB888,
573	//DRM_FORMAT_XRGB2101010,
574	//DRM_FORMAT_ARGB2101010,
575};
576
577static const uint64_t simpledrm_format_modifiers[] = {
578	DRM_FORMAT_MOD_LINEAR,
579	DRM_FORMAT_MOD_INVALID
580};
581
582static int simpledrm_connector_helper_get_modes(struct drm_connector *connector)
 
583{
584	struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev);
585	struct drm_display_mode *mode;
 
 
 
 
 
 
 
 
 
 
586
587	mode = drm_mode_duplicate(connector->dev, &sdev->mode);
588	if (!mode)
 
 
 
 
 
589		return 0;
590
591	if (mode->name[0] == '\0')
592		drm_mode_set_name(mode);
593
594	mode->type |= DRM_MODE_TYPE_PREFERRED;
595	drm_mode_probed_add(connector, mode);
 
 
 
 
596
597	if (mode->width_mm)
598		connector->display_info.width_mm = mode->width_mm;
599	if (mode->height_mm)
600		connector->display_info.height_mm = mode->height_mm;
601
602	return 1;
603}
604
605static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
606	.get_modes = simpledrm_connector_helper_get_modes,
607};
608
609static const struct drm_connector_funcs simpledrm_connector_funcs = {
610	.reset = drm_atomic_helper_connector_reset,
611	.fill_modes = drm_helper_probe_single_connector_modes,
612	.destroy = drm_connector_cleanup,
613	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
614	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
615};
616
617static int
618simpledrm_simple_display_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
619				    const struct drm_display_mode *mode)
620{
621	struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
622
623	if (mode->hdisplay != sdev->mode.hdisplay &&
624	    mode->vdisplay != sdev->mode.vdisplay)
625		return MODE_ONE_SIZE;
626	else if (mode->hdisplay != sdev->mode.hdisplay)
627		return MODE_ONE_WIDTH;
628	else if (mode->vdisplay != sdev->mode.vdisplay)
629		return MODE_ONE_HEIGHT;
630
631	return MODE_OK;
632}
633
634static void
635simpledrm_simple_display_pipe_enable(struct drm_simple_display_pipe *pipe,
636				     struct drm_crtc_state *crtc_state,
637				     struct drm_plane_state *plane_state)
638{
639	struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
 
640	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
641	struct drm_framebuffer *fb = plane_state->fb;
642	void *vmap = shadow_plane_state->map[0].vaddr; /* TODO: Use mapping abstraction properly */
643	struct drm_device *dev = &sdev->dev;
644	int idx;
 
 
645
646	if (!fb)
 
647		return;
648
649	if (!drm_dev_enter(dev, &idx))
650		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
651
652	drm_fb_blit_dstclip(sdev->screen_base, sdev->pitch,
653			    sdev->format->format, vmap, fb);
654	drm_dev_exit(idx);
 
 
655}
656
657static void
658simpledrm_simple_display_pipe_disable(struct drm_simple_display_pipe *pipe)
659{
660	struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
661	struct drm_device *dev = &sdev->dev;
662	int idx;
663
664	if (!drm_dev_enter(dev, &idx))
665		return;
666
667	/* Clear screen to black if disabled */
668	memset_io(sdev->screen_base, 0, sdev->pitch * sdev->mode.vdisplay);
669
670	drm_dev_exit(idx);
671}
672
673static void
674simpledrm_simple_display_pipe_update(struct drm_simple_display_pipe *pipe,
675				     struct drm_plane_state *old_plane_state)
 
 
 
 
 
 
 
 
 
 
 
 
 
676{
677	struct simpledrm_device *sdev = simpledrm_device_of_dev(pipe->crtc.dev);
678	struct drm_plane_state *plane_state = pipe->plane.state;
679	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
680	void *vmap = shadow_plane_state->map[0].vaddr; /* TODO: Use mapping abstraction properly */
681	struct drm_framebuffer *fb = plane_state->fb;
682	struct drm_device *dev = &sdev->dev;
683	struct drm_rect clip;
684	int idx;
685
686	if (!fb)
687		return;
688
689	if (!drm_atomic_helper_damage_merged(old_plane_state, plane_state, &clip))
690		return;
 
 
 
 
 
 
 
691
692	if (!drm_dev_enter(dev, &idx))
693		return;
 
 
 
 
 
 
694
695	drm_fb_blit_rect_dstclip(sdev->screen_base, sdev->pitch,
696				 sdev->format->format, vmap, fb, &clip);
 
697
698	drm_dev_exit(idx);
 
 
 
 
699}
700
701static const struct drm_simple_display_pipe_funcs
702simpledrm_simple_display_pipe_funcs = {
703	.mode_valid = simpledrm_simple_display_pipe_mode_valid,
704	.enable = simpledrm_simple_display_pipe_enable,
705	.disable = simpledrm_simple_display_pipe_disable,
706	.update = simpledrm_simple_display_pipe_update,
707	DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
 
 
 
708};
709
710static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
711	.fb_create = drm_gem_fb_create_with_dirty,
712	.atomic_check = drm_atomic_helper_check,
713	.atomic_commit = drm_atomic_helper_commit,
714};
715
716static const uint32_t *simpledrm_device_formats(struct simpledrm_device *sdev,
717						size_t *nformats_out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
718{
719	struct drm_device *dev = &sdev->dev;
720	size_t i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
721
722	if (sdev->nformats)
723		goto out; /* don't rebuild list on recurring calls */
 
 
 
 
 
 
 
724
725	/* native format goes first */
726	sdev->formats[0] = sdev->format->format;
727	sdev->nformats = 1;
728
729	/* default formats go second */
730	for (i = 0; i < ARRAY_SIZE(simpledrm_default_formats); ++i) {
731		if (simpledrm_default_formats[i] == sdev->format->format)
732			continue; /* native format already went first */
733		sdev->formats[sdev->nformats] = simpledrm_default_formats[i];
734		sdev->nformats++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
735	}
736
737	/*
738	 * TODO: The simpledrm driver converts framebuffers to the native
739	 * format when copying them to device memory. If there are more
740	 * formats listed than supported by the driver, the native format
741	 * is not supported by the conversion helpers. Therefore *only*
742	 * support the native format and add a conversion helper ASAP.
743	 */
744	if (drm_WARN_ONCE(dev, i != sdev->nformats,
745			  "format conversion helpers required for %p4cc",
746			  &sdev->format->format)) {
747		sdev->nformats = 1;
748	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
749
750out:
751	*nformats_out = sdev->nformats;
752	return sdev->formats;
753}
 
754
755static int simpledrm_device_init_modeset(struct simpledrm_device *sdev)
756{
757	struct drm_device *dev = &sdev->dev;
758	struct drm_display_mode *mode = &sdev->mode;
759	struct drm_connector *connector = &sdev->connector;
760	struct drm_simple_display_pipe *pipe = &sdev->pipe;
761	const uint32_t *formats;
762	size_t nformats;
763	int ret;
764
765	ret = drmm_mode_config_init(dev);
766	if (ret)
767		return ret;
 
 
 
 
 
 
 
 
768
769	dev->mode_config.min_width = mode->hdisplay;
770	dev->mode_config.max_width = mode->hdisplay;
771	dev->mode_config.min_height = mode->vdisplay;
772	dev->mode_config.max_height = mode->vdisplay;
773	dev->mode_config.prefer_shadow_fbdev = true;
774	dev->mode_config.preferred_depth = sdev->format->cpp[0] * 8;
775	dev->mode_config.funcs = &simpledrm_mode_config_funcs;
776
777	ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
778				 DRM_MODE_CONNECTOR_Unknown);
779	if (ret)
780		return ret;
781	drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
782
783	formats = simpledrm_device_formats(sdev, &nformats);
 
 
784
785	ret = drm_simple_display_pipe_init(dev, pipe, &simpledrm_simple_display_pipe_funcs,
786					   formats, nformats, simpledrm_format_modifiers,
787					   connector);
788	if (ret)
789		return ret;
790
791	drm_mode_config_reset(dev);
 
792
793	return 0;
794}
 
 
 
 
795
796/*
797 * Init / Cleanup
798 */
799
800static struct simpledrm_device *
801simpledrm_device_create(struct drm_driver *drv, struct platform_device *pdev)
802{
803	struct simpledrm_device *sdev;
804	int ret;
805
806	sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device,
807				  dev);
808	if (IS_ERR(sdev))
809		return ERR_CAST(sdev);
810	sdev->pdev = pdev;
811	platform_set_drvdata(pdev, sdev);
812
813	ret = simpledrm_device_init_clocks(sdev);
 
 
 
 
814	if (ret)
815		return ERR_PTR(ret);
816	ret = simpledrm_device_init_regulators(sdev);
 
 
 
 
 
 
 
817	if (ret)
818		return ERR_PTR(ret);
819	ret = simpledrm_device_init_fb(sdev);
 
 
 
 
 
 
820	if (ret)
821		return ERR_PTR(ret);
822	ret = simpledrm_device_init_mm(sdev);
 
 
 
 
 
 
823	if (ret)
824		return ERR_PTR(ret);
825	ret = simpledrm_device_init_modeset(sdev);
 
 
 
 
 
826	if (ret)
827		return ERR_PTR(ret);
828
 
 
829	return sdev;
830}
831
832/*
833 * DRM driver
834 */
835
836DEFINE_DRM_GEM_FOPS(simpledrm_fops);
837
838static struct drm_driver simpledrm_driver = {
839	DRM_GEM_SHMEM_DRIVER_OPS,
840	.name			= DRIVER_NAME,
841	.desc			= DRIVER_DESC,
842	.date			= DRIVER_DATE,
843	.major			= DRIVER_MAJOR,
844	.minor			= DRIVER_MINOR,
845	.driver_features	= DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
846	.fops			= &simpledrm_fops,
847};
848
849/*
850 * Platform driver
851 */
852
853static int simpledrm_probe(struct platform_device *pdev)
854{
855	struct simpledrm_device *sdev;
856	struct drm_device *dev;
 
857	int ret;
858
859	sdev = simpledrm_device_create(&simpledrm_driver, pdev);
860	if (IS_ERR(sdev))
861		return PTR_ERR(sdev);
862	dev = &sdev->dev;
863
864	ret = drm_dev_register(dev, 0);
865	if (ret)
866		return ret;
867
868	drm_fbdev_generic_setup(dev, 0);
 
 
 
 
869
870	return 0;
871}
872
873static int simpledrm_remove(struct platform_device *pdev)
874{
875	struct simpledrm_device *sdev = platform_get_drvdata(pdev);
876	struct drm_device *dev = &sdev->dev;
877
878	drm_dev_unplug(dev);
879
880	return 0;
881}
882
883static const struct of_device_id simpledrm_of_match_table[] = {
884	{ .compatible = "simple-framebuffer", },
885	{ },
886};
887MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
888
889static struct platform_driver simpledrm_platform_driver = {
890	.driver = {
891		.name = "simple-framebuffer", /* connect to sysfb */
892		.of_match_table = simpledrm_of_match_table,
893	},
894	.probe = simpledrm_probe,
895	.remove = simpledrm_remove,
896};
897
898module_platform_driver(simpledrm_platform_driver);
899
900MODULE_DESCRIPTION(DRIVER_DESC);
901MODULE_LICENSE("GPL v2");