brintos

brintos / linux-shallow public Read only

0
0
Text · 14.7 KiB · 3da5b8b Raw
490 lines · c
1// SPDX-License-Identifier: GPL-2.0+2/*3 * Copyright (C) 2019 Laurent Pinchart <laurent.pinchart@ideasonboard.com>4 */5 6#include <linux/kernel.h>7#include <linux/module.h>8#include <linux/of.h>9#include <linux/property.h>10#include <linux/slab.h>11 12#include <drm/drm_atomic_state_helper.h>13#include <drm/drm_bridge.h>14#include <drm/drm_bridge_connector.h>15#include <drm/drm_connector.h>16#include <drm/drm_device.h>17#include <drm/drm_edid.h>18#include <drm/drm_managed.h>19#include <drm/drm_modeset_helper_vtables.h>20#include <drm/drm_probe_helper.h>21#include <drm/display/drm_hdmi_state_helper.h>22 23/**24 * DOC: overview25 *26 * The DRM bridge connector helper object provides a DRM connector27 * implementation that wraps a chain of &struct drm_bridge. The connector28 * operations are fully implemented based on the operations of the bridges in29 * the chain, and don't require any intervention from the display controller30 * driver at runtime.31 *32 * To use the helper, display controller drivers create a bridge connector with33 * a call to drm_bridge_connector_init(). This associates the newly created34 * connector with the chain of bridges passed to the function and registers it35 * with the DRM device. At that point the connector becomes fully usable, no36 * further operation is needed.37 *38 * The DRM bridge connector operations are implemented based on the operations39 * provided by the bridges in the chain. Each connector operation is delegated40 * to the bridge closest to the connector (at the end of the chain) that41 * provides the relevant functionality.42 *43 * To make use of this helper, all bridges in the chain shall report bridge44 * operation flags (&drm_bridge->ops) and bridge output type45 * (&drm_bridge->type), as well as the DRM_BRIDGE_ATTACH_NO_CONNECTOR attach46 * flag (none of the bridges shall create a DRM connector directly).47 */48 49/**50 * struct drm_bridge_connector - A connector backed by a chain of bridges51 */52struct drm_bridge_connector {53	/**54	 * @base: The base DRM connector55	 */56	struct drm_connector base;57	/**58	 * @encoder:59	 *60	 * The encoder at the start of the bridges chain.61	 */62	struct drm_encoder *encoder;63	/**64	 * @bridge_edid:65	 *66	 * The last bridge in the chain (closest to the connector) that provides67	 * EDID read support, if any (see &DRM_BRIDGE_OP_EDID).68	 */69	struct drm_bridge *bridge_edid;70	/**71	 * @bridge_hpd:72	 *73	 * The last bridge in the chain (closest to the connector) that provides74	 * hot-plug detection notification, if any (see &DRM_BRIDGE_OP_HPD).75	 */76	struct drm_bridge *bridge_hpd;77	/**78	 * @bridge_detect:79	 *80	 * The last bridge in the chain (closest to the connector) that provides81	 * connector detection, if any (see &DRM_BRIDGE_OP_DETECT).82	 */83	struct drm_bridge *bridge_detect;84	/**85	 * @bridge_modes:86	 *87	 * The last bridge in the chain (closest to the connector) that provides88	 * connector modes detection, if any (see &DRM_BRIDGE_OP_MODES).89	 */90	struct drm_bridge *bridge_modes;91	/**92	 * @bridge_hdmi:93	 *94	 * The bridge in the chain that implements necessary support for the95	 * HDMI connector infrastructure, if any (see &DRM_BRIDGE_OP_HDMI).96	 */97	struct drm_bridge *bridge_hdmi;98};99 100#define to_drm_bridge_connector(x) \101	container_of(x, struct drm_bridge_connector, base)102 103/* -----------------------------------------------------------------------------104 * Bridge Connector Hot-Plug Handling105 */106 107static void drm_bridge_connector_hpd_notify(struct drm_connector *connector,108					    enum drm_connector_status status)109{110	struct drm_bridge_connector *bridge_connector =111		to_drm_bridge_connector(connector);112	struct drm_bridge *bridge;113 114	/* Notify all bridges in the pipeline of hotplug events. */115	drm_for_each_bridge_in_chain(bridge_connector->encoder, bridge) {116		if (bridge->funcs->hpd_notify)117			bridge->funcs->hpd_notify(bridge, status);118	}119}120 121static void drm_bridge_connector_handle_hpd(struct drm_bridge_connector *drm_bridge_connector,122					    enum drm_connector_status status)123{124	struct drm_connector *connector = &drm_bridge_connector->base;125	struct drm_device *dev = connector->dev;126 127	mutex_lock(&dev->mode_config.mutex);128	connector->status = status;129	mutex_unlock(&dev->mode_config.mutex);130 131	drm_bridge_connector_hpd_notify(connector, status);132 133	drm_kms_helper_connector_hotplug_event(connector);134}135 136static void drm_bridge_connector_hpd_cb(void *cb_data,137					enum drm_connector_status status)138{139	drm_bridge_connector_handle_hpd(cb_data, status);140}141 142static void drm_bridge_connector_oob_hotplug_event(struct drm_connector *connector,143						   enum drm_connector_status status)144{145	struct drm_bridge_connector *bridge_connector =146		to_drm_bridge_connector(connector);147 148	drm_bridge_connector_handle_hpd(bridge_connector, status);149}150 151static void drm_bridge_connector_enable_hpd(struct drm_connector *connector)152{153	struct drm_bridge_connector *bridge_connector =154		to_drm_bridge_connector(connector);155	struct drm_bridge *hpd = bridge_connector->bridge_hpd;156 157	if (hpd)158		drm_bridge_hpd_enable(hpd, drm_bridge_connector_hpd_cb,159				      bridge_connector);160}161 162static void drm_bridge_connector_disable_hpd(struct drm_connector *connector)163{164	struct drm_bridge_connector *bridge_connector =165		to_drm_bridge_connector(connector);166	struct drm_bridge *hpd = bridge_connector->bridge_hpd;167 168	if (hpd)169		drm_bridge_hpd_disable(hpd);170}171 172/* -----------------------------------------------------------------------------173 * Bridge Connector Functions174 */175 176static enum drm_connector_status177drm_bridge_connector_detect(struct drm_connector *connector, bool force)178{179	struct drm_bridge_connector *bridge_connector =180		to_drm_bridge_connector(connector);181	struct drm_bridge *detect = bridge_connector->bridge_detect;182	enum drm_connector_status status;183 184	if (detect) {185		status = detect->funcs->detect(detect);186 187		drm_bridge_connector_hpd_notify(connector, status);188	} else {189		switch (connector->connector_type) {190		case DRM_MODE_CONNECTOR_DPI:191		case DRM_MODE_CONNECTOR_LVDS:192		case DRM_MODE_CONNECTOR_DSI:193		case DRM_MODE_CONNECTOR_eDP:194			status = connector_status_connected;195			break;196		default:197			status = connector_status_unknown;198			break;199		}200	}201 202	return status;203}204 205static void drm_bridge_connector_debugfs_init(struct drm_connector *connector,206					      struct dentry *root)207{208	struct drm_bridge_connector *bridge_connector =209		to_drm_bridge_connector(connector);210	struct drm_encoder *encoder = bridge_connector->encoder;211	struct drm_bridge *bridge;212 213	list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {214		if (bridge->funcs->debugfs_init)215			bridge->funcs->debugfs_init(bridge, root);216	}217}218 219static void drm_bridge_connector_reset(struct drm_connector *connector)220{221	struct drm_bridge_connector *bridge_connector =222		to_drm_bridge_connector(connector);223 224	drm_atomic_helper_connector_reset(connector);225	if (bridge_connector->bridge_hdmi)226		__drm_atomic_helper_connector_hdmi_reset(connector,227							 connector->state);228}229 230static const struct drm_connector_funcs drm_bridge_connector_funcs = {231	.reset = drm_bridge_connector_reset,232	.detect = drm_bridge_connector_detect,233	.fill_modes = drm_helper_probe_single_connector_modes,234	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,235	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,236	.debugfs_init = drm_bridge_connector_debugfs_init,237	.oob_hotplug_event = drm_bridge_connector_oob_hotplug_event,238};239 240/* -----------------------------------------------------------------------------241 * Bridge Connector Helper Functions242 */243 244static int drm_bridge_connector_get_modes_edid(struct drm_connector *connector,245					       struct drm_bridge *bridge)246{247	enum drm_connector_status status;248	const struct drm_edid *drm_edid;249	int n;250 251	status = drm_bridge_connector_detect(connector, false);252	if (status != connector_status_connected)253		goto no_edid;254 255	drm_edid = drm_bridge_edid_read(bridge, connector);256	if (!drm_edid_valid(drm_edid)) {257		drm_edid_free(drm_edid);258		goto no_edid;259	}260 261	drm_edid_connector_update(connector, drm_edid);262	n = drm_edid_connector_add_modes(connector);263 264	drm_edid_free(drm_edid);265	return n;266 267no_edid:268	drm_edid_connector_update(connector, NULL);269	return 0;270}271 272static int drm_bridge_connector_get_modes(struct drm_connector *connector)273{274	struct drm_bridge_connector *bridge_connector =275		to_drm_bridge_connector(connector);276	struct drm_bridge *bridge;277 278	/*279	 * If display exposes EDID, then we parse that in the normal way to280	 * build table of supported modes.281	 */282	bridge = bridge_connector->bridge_edid;283	if (bridge)284		return drm_bridge_connector_get_modes_edid(connector, bridge);285 286	/*287	 * Otherwise if the display pipeline reports modes (e.g. with a fixed288	 * resolution panel or an analog TV output), query it.289	 */290	bridge = bridge_connector->bridge_modes;291	if (bridge)292		return bridge->funcs->get_modes(bridge, connector);293 294	/*295	 * We can't retrieve modes, which can happen for instance for a DVI or296	 * VGA output with the DDC bus unconnected. The KMS core will add the297	 * default modes.298	 */299	return 0;300}301 302static const struct drm_connector_helper_funcs drm_bridge_connector_helper_funcs = {303	.get_modes = drm_bridge_connector_get_modes,304	/* No need for .mode_valid(), the bridges are checked by the core. */305	.enable_hpd = drm_bridge_connector_enable_hpd,306	.disable_hpd = drm_bridge_connector_disable_hpd,307};308 309static enum drm_mode_status310drm_bridge_connector_tmds_char_rate_valid(const struct drm_connector *connector,311					  const struct drm_display_mode *mode,312					  unsigned long long tmds_rate)313{314	struct drm_bridge_connector *bridge_connector =315		to_drm_bridge_connector(connector);316	struct drm_bridge *bridge;317 318	bridge = bridge_connector->bridge_hdmi;319	if (!bridge)320		return MODE_ERROR;321 322	if (bridge->funcs->hdmi_tmds_char_rate_valid)323		return bridge->funcs->hdmi_tmds_char_rate_valid(bridge, mode, tmds_rate);324	else325		return MODE_OK;326}327 328static int drm_bridge_connector_clear_infoframe(struct drm_connector *connector,329						enum hdmi_infoframe_type type)330{331	struct drm_bridge_connector *bridge_connector =332		to_drm_bridge_connector(connector);333	struct drm_bridge *bridge;334 335	bridge = bridge_connector->bridge_hdmi;336	if (!bridge)337		return -EINVAL;338 339	return bridge->funcs->hdmi_clear_infoframe(bridge, type);340}341 342static int drm_bridge_connector_write_infoframe(struct drm_connector *connector,343						enum hdmi_infoframe_type type,344						const u8 *buffer, size_t len)345{346	struct drm_bridge_connector *bridge_connector =347		to_drm_bridge_connector(connector);348	struct drm_bridge *bridge;349 350	bridge = bridge_connector->bridge_hdmi;351	if (!bridge)352		return -EINVAL;353 354	return bridge->funcs->hdmi_write_infoframe(bridge, type, buffer, len);355}356 357static const struct drm_connector_hdmi_funcs drm_bridge_connector_hdmi_funcs = {358	.tmds_char_rate_valid = drm_bridge_connector_tmds_char_rate_valid,359	.clear_infoframe = drm_bridge_connector_clear_infoframe,360	.write_infoframe = drm_bridge_connector_write_infoframe,361};362 363/* -----------------------------------------------------------------------------364 * Bridge Connector Initialisation365 */366 367/**368 * drm_bridge_connector_init - Initialise a connector for a chain of bridges369 * @drm: the DRM device370 * @encoder: the encoder where the bridge chain starts371 *372 * Allocate, initialise and register a &drm_bridge_connector with the @drm373 * device. The connector is associated with a chain of bridges that starts at374 * the @encoder. All bridges in the chain shall report bridge operation flags375 * (&drm_bridge->ops) and bridge output type (&drm_bridge->type), and none of376 * them may create a DRM connector directly.377 *378 * Returns a pointer to the new connector on success, or a negative error379 * pointer otherwise.380 */381struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,382						struct drm_encoder *encoder)383{384	struct drm_bridge_connector *bridge_connector;385	struct drm_connector *connector;386	struct i2c_adapter *ddc = NULL;387	struct drm_bridge *bridge, *panel_bridge = NULL;388	unsigned int supported_formats = BIT(HDMI_COLORSPACE_RGB);389	unsigned int max_bpc = 8;390	int connector_type;391	int ret;392 393	bridge_connector = drmm_kzalloc(drm, sizeof(*bridge_connector), GFP_KERNEL);394	if (!bridge_connector)395		return ERR_PTR(-ENOMEM);396 397	bridge_connector->encoder = encoder;398 399	/*400	 * TODO: Handle doublescan_allowed, stereo_allowed and401	 * ycbcr_420_allowed.402	 */403	connector = &bridge_connector->base;404	connector->interlace_allowed = true;405 406	/*407	 * Initialise connector status handling. First locate the furthest408	 * bridges in the pipeline that support HPD and output detection. Then409	 * initialise the connector polling mode, using HPD if available and410	 * falling back to polling if supported. If neither HPD nor output411	 * detection are available, we don't support hotplug detection at all.412	 */413	connector_type = DRM_MODE_CONNECTOR_Unknown;414	drm_for_each_bridge_in_chain(encoder, bridge) {415		if (!bridge->interlace_allowed)416			connector->interlace_allowed = false;417 418		if (bridge->ops & DRM_BRIDGE_OP_EDID)419			bridge_connector->bridge_edid = bridge;420		if (bridge->ops & DRM_BRIDGE_OP_HPD)421			bridge_connector->bridge_hpd = bridge;422		if (bridge->ops & DRM_BRIDGE_OP_DETECT)423			bridge_connector->bridge_detect = bridge;424		if (bridge->ops & DRM_BRIDGE_OP_MODES)425			bridge_connector->bridge_modes = bridge;426		if (bridge->ops & DRM_BRIDGE_OP_HDMI) {427			if (bridge_connector->bridge_hdmi)428				return ERR_PTR(-EBUSY);429			if (!bridge->funcs->hdmi_write_infoframe ||430			    !bridge->funcs->hdmi_clear_infoframe)431				return ERR_PTR(-EINVAL);432 433			bridge_connector->bridge_hdmi = bridge;434 435			if (bridge->supported_formats)436				supported_formats = bridge->supported_formats;437			if (bridge->max_bpc)438				max_bpc = bridge->max_bpc;439		}440 441		if (!drm_bridge_get_next_bridge(bridge))442			connector_type = bridge->type;443 444#ifdef CONFIG_OF445		if (!drm_bridge_get_next_bridge(bridge) &&446		    bridge->of_node)447			connector->fwnode = fwnode_handle_get(of_fwnode_handle(bridge->of_node));448#endif449 450		if (bridge->ddc)451			ddc = bridge->ddc;452 453		if (drm_bridge_is_panel(bridge))454			panel_bridge = bridge;455	}456 457	if (connector_type == DRM_MODE_CONNECTOR_Unknown)458		return ERR_PTR(-EINVAL);459 460	if (bridge_connector->bridge_hdmi)461		ret = drmm_connector_hdmi_init(drm, connector,462					       bridge_connector->bridge_hdmi->vendor,463					       bridge_connector->bridge_hdmi->product,464					       &drm_bridge_connector_funcs,465					       &drm_bridge_connector_hdmi_funcs,466					       connector_type, ddc,467					       supported_formats,468					       max_bpc);469	else470		ret = drmm_connector_init(drm, connector,471					  &drm_bridge_connector_funcs,472					  connector_type, ddc);473	if (ret)474		return ERR_PTR(ret);475 476	drm_connector_helper_add(connector, &drm_bridge_connector_helper_funcs);477 478	if (bridge_connector->bridge_hpd)479		connector->polled = DRM_CONNECTOR_POLL_HPD;480	else if (bridge_connector->bridge_detect)481		connector->polled = DRM_CONNECTOR_POLL_CONNECT482				  | DRM_CONNECTOR_POLL_DISCONNECT;483 484	if (panel_bridge)485		drm_panel_bridge_set_orientation(connector, panel_bridge);486 487	return connector;488}489EXPORT_SYMBOL_GPL(drm_bridge_connector_init);490