rcar_du_hdmienc.c 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * R-Car Display Unit HDMI Encoder
 *
 * Copyright (C) 2014 Renesas Electronics Corporation
 *
 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/slab.h>

#include <drm/drmP.h>
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_encoder_slave.h>

#include "rcar_du_drv.h"
#include "rcar_du_encoder.h"
#include "rcar_du_hdmienc.h"
24
#include "rcar_du_lvdsenc.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

struct rcar_du_hdmienc {
	struct rcar_du_encoder *renc;
	struct device *dev;
	int dpms;
};

#define to_rcar_hdmienc(e)	(to_rcar_encoder(e)->hdmi)
#define to_slave_funcs(e)	(to_rcar_encoder(e)->slave.slave_funcs)

static void rcar_du_hdmienc_dpms(struct drm_encoder *encoder, int mode)
{
	struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
	struct drm_encoder_slave_funcs *sfuncs = to_slave_funcs(encoder);

40 41 42
	if (mode != DRM_MODE_DPMS_ON)
		mode = DRM_MODE_DPMS_OFF;

43 44 45
	if (hdmienc->dpms == mode)
		return;

46
	if (mode == DRM_MODE_DPMS_ON && hdmienc->renc->lvds)
47 48
		rcar_du_lvdsenc_enable(hdmienc->renc->lvds, encoder->crtc,
				       true);
49

50 51 52
	if (sfuncs->dpms)
		sfuncs->dpms(encoder, mode);

53
	if (mode != DRM_MODE_DPMS_ON && hdmienc->renc->lvds)
54 55
		rcar_du_lvdsenc_enable(hdmienc->renc->lvds, encoder->crtc,
				       false);
56

57 58 59 60 61 62 63
	hdmienc->dpms = mode;
}

static bool rcar_du_hdmienc_mode_fixup(struct drm_encoder *encoder,
				       const struct drm_display_mode *mode,
				       struct drm_display_mode *adjusted_mode)
{
64
	struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
65 66
	struct drm_encoder_slave_funcs *sfuncs = to_slave_funcs(encoder);

67 68 69 70 71 72 73
	/* The internal LVDS encoder has a clock frequency operating range of
	 * 30MHz to 150MHz. Clamp the clock accordingly.
	 */
	if (hdmienc->renc->lvds)
		adjusted_mode->clock = clamp(adjusted_mode->clock,
					     30000, 150000);

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	if (sfuncs->mode_fixup == NULL)
		return true;

	return sfuncs->mode_fixup(encoder, mode, adjusted_mode);
}

static void rcar_du_hdmienc_mode_prepare(struct drm_encoder *encoder)
{
	rcar_du_hdmienc_dpms(encoder, DRM_MODE_DPMS_OFF);
}

static void rcar_du_hdmienc_mode_commit(struct drm_encoder *encoder)
{
	rcar_du_hdmienc_dpms(encoder, DRM_MODE_DPMS_ON);
}

static void rcar_du_hdmienc_mode_set(struct drm_encoder *encoder,
				     struct drm_display_mode *mode,
				     struct drm_display_mode *adjusted_mode)
{
	struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
	struct drm_encoder_slave_funcs *sfuncs = to_slave_funcs(encoder);

	if (sfuncs->mode_set)
		sfuncs->mode_set(encoder, mode, adjusted_mode);

	rcar_du_crtc_route_output(encoder->crtc, hdmienc->renc->output);
}

static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
	.dpms = rcar_du_hdmienc_dpms,
	.mode_fixup = rcar_du_hdmienc_mode_fixup,
	.prepare = rcar_du_hdmienc_mode_prepare,
	.commit = rcar_du_hdmienc_mode_commit,
	.mode_set = rcar_du_hdmienc_mode_set,
};

static void rcar_du_hdmienc_cleanup(struct drm_encoder *encoder)
{
	struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);

	rcar_du_hdmienc_dpms(encoder, DRM_MODE_DPMS_OFF);

	drm_encoder_cleanup(encoder);
	put_device(hdmienc->dev);
}

static const struct drm_encoder_funcs encoder_funcs = {
	.destroy = rcar_du_hdmienc_cleanup,
};

int rcar_du_hdmienc_init(struct rcar_du_device *rcdu,
			 struct rcar_du_encoder *renc, struct device_node *np)
{
	struct drm_encoder *encoder = rcar_encoder_to_drm_encoder(renc);
	struct drm_i2c_encoder_driver *driver;
	struct i2c_client *i2c_slave;
	struct rcar_du_hdmienc *hdmienc;
	int ret;

	hdmienc = devm_kzalloc(rcdu->dev, sizeof(*hdmienc), GFP_KERNEL);
	if (hdmienc == NULL)
		return -ENOMEM;

	/* Locate the slave I2C device and driver. */
	i2c_slave = of_find_i2c_device_by_node(np);
	if (!i2c_slave || !i2c_get_clientdata(i2c_slave))
		return -EPROBE_DEFER;

	hdmienc->dev = &i2c_slave->dev;

	if (hdmienc->dev->driver == NULL) {
		ret = -EPROBE_DEFER;
		goto error;
	}

	/* Initialize the slave encoder. */
	driver = to_drm_i2c_encoder_driver(to_i2c_driver(hdmienc->dev->driver));
	ret = driver->encoder_init(i2c_slave, rcdu->ddev, &renc->slave);
	if (ret < 0)
		goto error;

	ret = drm_encoder_init(rcdu->ddev, encoder, &encoder_funcs,
			       DRM_MODE_ENCODER_TMDS);
	if (ret < 0)
		goto error;

	drm_encoder_helper_add(encoder, &encoder_helper_funcs);

	renc->hdmi = hdmienc;
	hdmienc->renc = renc;

	return 0;

error:
	put_device(hdmienc->dev);
	return ret;
}