// SPDX-License-Identifier: GPL-2.0-only
#include <linux/clk.h>
#include <linux/of_clk.h>
#include <linux/minmax.h>
#include <linux/of_address.h>
#include <linux/platform_data/simplefb.h>
#include <linux/platform_device.h>
#include <linux/pm_domain.h>
#include <linux/regulator/consumer.h>
#include <drm/drm_aperture.h>
#include <drm/drm_atomic.h>
#include <drm/drm_atomic_state_helper.h>
#include <drm/drm_connector.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_damage_helper.h>
#include <drm/drm_device.h>
#include <drm/drm_drv.h>
#include <drm/drm_fbdev_shmem.h>
#include <drm/drm_format_helper.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_gem_atomic_helper.h>
#include <drm/drm_gem_framebuffer_helper.h>
#include <drm/drm_gem_shmem_helper.h>
#include <drm/drm_managed.h>
#include <drm/drm_modeset_helper_vtables.h>
#include <drm/drm_panic.h>
#include <drm/drm_probe_helper.h>
#define DRIVER_NAME "simpledrm"
#define DRIVER_DESC "DRM driver for simple-framebuffer platform devices"
#define DRIVER_DATE "20200625"
#define DRIVER_MAJOR 1
#define DRIVER_MINOR 0
/*
* Helpers for simplefb
*/
static int
simplefb_get_validated_int(struct drm_device *dev, const char *name,
uint32_t value)
{
if (value > INT_MAX) {
drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
name, value);
return -EINVAL;
}
return (int)value;
}
static int
simplefb_get_validated_int0(struct drm_device *dev, const char *name,
uint32_t value)
{
if (!value) {
drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
name, value);
return -EINVAL;
}
return simplefb_get_validated_int(dev, name, value);
}
static const struct drm_format_info *
simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
{
static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
const struct simplefb_format *fmt = formats;
const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
const struct drm_format_info *info;
if (!format_name) {
drm_err(dev, "simplefb: missing framebuffer format\n");
return ERR_PTR(-EINVAL);
}
while (fmt < end) {
if (!strcmp(format_name, fmt->name)) {
info = drm_format_info(fmt->fourcc);
if (!info)
return ERR_PTR(-EINVAL);
return info;
}
++fmt;
}
drm_err(dev, "simplefb: unknown framebuffer format %s\n",
format_name);
return ERR_PTR(-EINVAL);
}
static int
simplefb_get_width_pd(struct drm_device *dev,
const struct simplefb_platform_data *pd)
{
return simplefb_get_validated_int0(dev, "width", pd->width);
}
static int
simplefb_get_height_pd(struct drm_device *dev,
const struct simplefb_platform_data *pd)
{
return simplefb_get_validated_int0(dev, "height", pd->height);
}
static int
simplefb_get_stride_pd(struct drm_device *dev,
const struct simplefb_platform_data *pd)
{
return simplefb_get_validated_int(dev, "stride", pd->stride);
}
static const struct drm_format_info *
simplefb_get_format_pd(struct drm_device *dev,
const struct simplefb_platform_data *pd)
{
return simplefb_get_validated_format(dev, pd->format);
}
static int
simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
const char *name, u32 *value)
{
int ret = of_property_read_u32(of_node, name, value);
if (ret)
drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
name, ret);
return ret;
}
static int
simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
const char *name, const char **value)
{
int ret = of_property_read_string(of_node, name, value);
if (ret)
drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
name, ret);
return ret;
}
static int
simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
{
u32 width;
int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
if (ret)
return ret;
return simplefb_get_validated_int0(dev, "width", width);
}
static int
simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
{
u32 height;
int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
if (ret)
return ret;
return simplefb_get_validated_int0(dev, "height", height);
}
static int
simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
{
u32 stride;
int ret = simplefb_read_u32_of(dev, of_node, "stride", &st