| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (C) 2016 Maxime Ripard |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or |
|---|
| 7 | | - * modify it under the terms of the GNU General Public License as |
|---|
| 8 | | - * published by the Free Software Foundation; either version 2 of |
|---|
| 9 | | - * the License, or (at your option) any later version. |
|---|
| 10 | 6 | */ |
|---|
| 11 | | - |
|---|
| 12 | | -#include <drm/drmP.h> |
|---|
| 13 | | -#include <drm/drm_atomic_helper.h> |
|---|
| 14 | | -#include <drm/drm_crtc_helper.h> |
|---|
| 15 | | -#include <drm/drm_edid.h> |
|---|
| 16 | | -#include <drm/drm_encoder.h> |
|---|
| 17 | | -#include <drm/drm_of.h> |
|---|
| 18 | | -#include <drm/drm_panel.h> |
|---|
| 19 | 7 | |
|---|
| 20 | 8 | #include <linux/clk.h> |
|---|
| 21 | 9 | #include <linux/component.h> |
|---|
| 22 | 10 | #include <linux/iopoll.h> |
|---|
| 11 | +#include <linux/module.h> |
|---|
| 23 | 12 | #include <linux/of_device.h> |
|---|
| 24 | 13 | #include <linux/platform_device.h> |
|---|
| 25 | 14 | #include <linux/pm_runtime.h> |
|---|
| 26 | 15 | #include <linux/regmap.h> |
|---|
| 27 | 16 | #include <linux/reset.h> |
|---|
| 17 | + |
|---|
| 18 | +#include <drm/drm_atomic_helper.h> |
|---|
| 19 | +#include <drm/drm_edid.h> |
|---|
| 20 | +#include <drm/drm_encoder.h> |
|---|
| 21 | +#include <drm/drm_of.h> |
|---|
| 22 | +#include <drm/drm_panel.h> |
|---|
| 23 | +#include <drm/drm_print.h> |
|---|
| 24 | +#include <drm/drm_probe_helper.h> |
|---|
| 25 | +#include <drm/drm_simple_kms_helper.h> |
|---|
| 28 | 26 | |
|---|
| 29 | 27 | #include "sun4i_backend.h" |
|---|
| 30 | 28 | #include "sun4i_crtc.h" |
|---|
| .. | .. |
|---|
| 52 | 50 | u8 buffer[17]; |
|---|
| 53 | 51 | int i, ret; |
|---|
| 54 | 52 | |
|---|
| 55 | | - ret = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false); |
|---|
| 53 | + ret = drm_hdmi_avi_infoframe_from_display_mode(&frame, |
|---|
| 54 | + &hdmi->connector, mode); |
|---|
| 56 | 55 | if (ret < 0) { |
|---|
| 57 | 56 | DRM_ERROR("Failed to get infoframes from mode\n"); |
|---|
| 58 | 57 | return ret; |
|---|
| .. | .. |
|---|
| 206 | 205 | .mode_valid = sun4i_hdmi_mode_valid, |
|---|
| 207 | 206 | }; |
|---|
| 208 | 207 | |
|---|
| 209 | | -static const struct drm_encoder_funcs sun4i_hdmi_funcs = { |
|---|
| 210 | | - .destroy = drm_encoder_cleanup, |
|---|
| 211 | | -}; |
|---|
| 212 | | - |
|---|
| 213 | 208 | static int sun4i_hdmi_get_modes(struct drm_connector *connector) |
|---|
| 214 | 209 | { |
|---|
| 215 | 210 | struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector); |
|---|
| 216 | 211 | struct edid *edid; |
|---|
| 217 | 212 | int ret; |
|---|
| 218 | 213 | |
|---|
| 219 | | - edid = drm_get_edid(connector, hdmi->i2c); |
|---|
| 214 | + edid = drm_get_edid(connector, hdmi->ddc_i2c ?: hdmi->i2c); |
|---|
| 220 | 215 | if (!edid) |
|---|
| 221 | 216 | return 0; |
|---|
| 222 | 217 | |
|---|
| .. | .. |
|---|
| 230 | 225 | kfree(edid); |
|---|
| 231 | 226 | |
|---|
| 232 | 227 | return ret; |
|---|
| 228 | +} |
|---|
| 229 | + |
|---|
| 230 | +static struct i2c_adapter *sun4i_hdmi_get_ddc(struct device *dev) |
|---|
| 231 | +{ |
|---|
| 232 | + struct device_node *phandle, *remote; |
|---|
| 233 | + struct i2c_adapter *ddc; |
|---|
| 234 | + |
|---|
| 235 | + remote = of_graph_get_remote_node(dev->of_node, 1, -1); |
|---|
| 236 | + if (!remote) |
|---|
| 237 | + return ERR_PTR(-EINVAL); |
|---|
| 238 | + |
|---|
| 239 | + phandle = of_parse_phandle(remote, "ddc-i2c-bus", 0); |
|---|
| 240 | + of_node_put(remote); |
|---|
| 241 | + if (!phandle) |
|---|
| 242 | + return ERR_PTR(-ENODEV); |
|---|
| 243 | + |
|---|
| 244 | + ddc = of_get_i2c_adapter_by_node(phandle); |
|---|
| 245 | + of_node_put(phandle); |
|---|
| 246 | + if (!ddc) |
|---|
| 247 | + return ERR_PTR(-EPROBE_DEFER); |
|---|
| 248 | + |
|---|
| 249 | + return ddc; |
|---|
| 233 | 250 | } |
|---|
| 234 | 251 | |
|---|
| 235 | 252 | static const struct drm_connector_helper_funcs sun4i_hdmi_connector_helper_funcs = { |
|---|
| .. | .. |
|---|
| 261 | 278 | }; |
|---|
| 262 | 279 | |
|---|
| 263 | 280 | #ifdef CONFIG_DRM_SUN4I_HDMI_CEC |
|---|
| 264 | | -static bool sun4i_hdmi_cec_pin_read(struct cec_adapter *adap) |
|---|
| 281 | +static int sun4i_hdmi_cec_pin_read(struct cec_adapter *adap) |
|---|
| 265 | 282 | { |
|---|
| 266 | 283 | struct sun4i_hdmi *hdmi = cec_get_drvdata(adap); |
|---|
| 267 | 284 | |
|---|
| .. | .. |
|---|
| 469 | 486 | { |
|---|
| 470 | 487 | struct platform_device *pdev = to_platform_device(dev); |
|---|
| 471 | 488 | struct drm_device *drm = data; |
|---|
| 489 | + struct cec_connector_info conn_info; |
|---|
| 472 | 490 | struct sun4i_drv *drv = drm->dev_private; |
|---|
| 473 | 491 | struct sun4i_hdmi *hdmi; |
|---|
| 474 | 492 | struct resource *res; |
|---|
| .. | .. |
|---|
| 578 | 596 | goto err_disable_mod_clk; |
|---|
| 579 | 597 | } |
|---|
| 580 | 598 | |
|---|
| 599 | + hdmi->ddc_i2c = sun4i_hdmi_get_ddc(dev); |
|---|
| 600 | + if (IS_ERR(hdmi->ddc_i2c)) { |
|---|
| 601 | + ret = PTR_ERR(hdmi->ddc_i2c); |
|---|
| 602 | + if (ret == -ENODEV) |
|---|
| 603 | + hdmi->ddc_i2c = NULL; |
|---|
| 604 | + else |
|---|
| 605 | + goto err_del_i2c_adapter; |
|---|
| 606 | + } |
|---|
| 607 | + |
|---|
| 581 | 608 | drm_encoder_helper_add(&hdmi->encoder, |
|---|
| 582 | 609 | &sun4i_hdmi_helper_funcs); |
|---|
| 583 | | - ret = drm_encoder_init(drm, |
|---|
| 584 | | - &hdmi->encoder, |
|---|
| 585 | | - &sun4i_hdmi_funcs, |
|---|
| 586 | | - DRM_MODE_ENCODER_TMDS, |
|---|
| 587 | | - NULL); |
|---|
| 610 | + ret = drm_simple_encoder_init(drm, &hdmi->encoder, |
|---|
| 611 | + DRM_MODE_ENCODER_TMDS); |
|---|
| 588 | 612 | if (ret) { |
|---|
| 589 | 613 | dev_err(dev, "Couldn't initialise the HDMI encoder\n"); |
|---|
| 590 | | - goto err_del_i2c_adapter; |
|---|
| 614 | + goto err_put_ddc_i2c; |
|---|
| 591 | 615 | } |
|---|
| 592 | 616 | |
|---|
| 593 | 617 | hdmi->encoder.possible_crtcs = drm_of_find_possible_crtcs(drm, |
|---|
| 594 | 618 | dev->of_node); |
|---|
| 595 | 619 | if (!hdmi->encoder.possible_crtcs) { |
|---|
| 596 | 620 | ret = -EPROBE_DEFER; |
|---|
| 597 | | - goto err_del_i2c_adapter; |
|---|
| 621 | + goto err_put_ddc_i2c; |
|---|
| 598 | 622 | } |
|---|
| 599 | 623 | |
|---|
| 600 | 624 | #ifdef CONFIG_DRM_SUN4I_HDMI_CEC |
|---|
| 601 | 625 | hdmi->cec_adap = cec_pin_allocate_adapter(&sun4i_hdmi_cec_pin_ops, |
|---|
| 602 | | - hdmi, "sun4i", CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS | |
|---|
| 603 | | - CEC_CAP_PASSTHROUGH | CEC_CAP_RC); |
|---|
| 626 | + hdmi, "sun4i", CEC_CAP_DEFAULTS | CEC_CAP_CONNECTOR_INFO); |
|---|
| 604 | 627 | ret = PTR_ERR_OR_ZERO(hdmi->cec_adap); |
|---|
| 605 | 628 | if (ret < 0) |
|---|
| 606 | 629 | goto err_cleanup_connector; |
|---|
| .. | .. |
|---|
| 610 | 633 | |
|---|
| 611 | 634 | drm_connector_helper_add(&hdmi->connector, |
|---|
| 612 | 635 | &sun4i_hdmi_connector_helper_funcs); |
|---|
| 613 | | - ret = drm_connector_init(drm, &hdmi->connector, |
|---|
| 614 | | - &sun4i_hdmi_connector_funcs, |
|---|
| 615 | | - DRM_MODE_CONNECTOR_HDMIA); |
|---|
| 636 | + ret = drm_connector_init_with_ddc(drm, &hdmi->connector, |
|---|
| 637 | + &sun4i_hdmi_connector_funcs, |
|---|
| 638 | + DRM_MODE_CONNECTOR_HDMIA, |
|---|
| 639 | + hdmi->ddc_i2c); |
|---|
| 616 | 640 | if (ret) { |
|---|
| 617 | 641 | dev_err(dev, |
|---|
| 618 | 642 | "Couldn't initialise the HDMI connector\n"); |
|---|
| 619 | 643 | goto err_cleanup_connector; |
|---|
| 620 | 644 | } |
|---|
| 645 | + cec_fill_conn_info_from_drm(&conn_info, &hdmi->connector); |
|---|
| 646 | + cec_s_conn_info(hdmi->cec_adap, &conn_info); |
|---|
| 621 | 647 | |
|---|
| 622 | 648 | /* There is no HPD interrupt, so we need to poll the controller */ |
|---|
| 623 | 649 | hdmi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | |
|---|
| .. | .. |
|---|
| 633 | 659 | err_cleanup_connector: |
|---|
| 634 | 660 | cec_delete_adapter(hdmi->cec_adap); |
|---|
| 635 | 661 | drm_encoder_cleanup(&hdmi->encoder); |
|---|
| 662 | +err_put_ddc_i2c: |
|---|
| 663 | + i2c_put_adapter(hdmi->ddc_i2c); |
|---|
| 636 | 664 | err_del_i2c_adapter: |
|---|
| 637 | 665 | i2c_del_adapter(hdmi->i2c); |
|---|
| 638 | 666 | err_disable_mod_clk: |
|---|
| .. | .. |
|---|
| 651 | 679 | |
|---|
| 652 | 680 | cec_unregister_adapter(hdmi->cec_adap); |
|---|
| 653 | 681 | i2c_del_adapter(hdmi->i2c); |
|---|
| 682 | + i2c_put_adapter(hdmi->ddc_i2c); |
|---|
| 654 | 683 | clk_disable_unprepare(hdmi->mod_clk); |
|---|
| 655 | 684 | clk_disable_unprepare(hdmi->bus_clk); |
|---|
| 656 | 685 | } |
|---|