18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2016, The Linux Foundation. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/of_graph.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "adv7511.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic const struct reg_sequence adv7533_fixed_registers[] = {
118c2ecf20Sopenharmony_ci	{ 0x16, 0x20 },
128c2ecf20Sopenharmony_ci	{ 0x9a, 0xe0 },
138c2ecf20Sopenharmony_ci	{ 0xba, 0x70 },
148c2ecf20Sopenharmony_ci	{ 0xde, 0x82 },
158c2ecf20Sopenharmony_ci	{ 0xe4, 0x40 },
168c2ecf20Sopenharmony_ci	{ 0xe5, 0x80 },
178c2ecf20Sopenharmony_ci};
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic const struct reg_sequence adv7533_cec_fixed_registers[] = {
208c2ecf20Sopenharmony_ci	{ 0x15, 0xd0 },
218c2ecf20Sopenharmony_ci	{ 0x17, 0xd0 },
228c2ecf20Sopenharmony_ci	{ 0x24, 0x20 },
238c2ecf20Sopenharmony_ci	{ 0x57, 0x11 },
248c2ecf20Sopenharmony_ci	{ 0x05, 0xc8 },
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic void adv7511_dsi_config_timing_gen(struct adv7511 *adv)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = adv->dsi;
308c2ecf20Sopenharmony_ci	struct drm_display_mode *mode = &adv->curr_mode;
318c2ecf20Sopenharmony_ci	unsigned int hsw, hfp, hbp, vsw, vfp, vbp;
328c2ecf20Sopenharmony_ci	u8 clock_div_by_lanes[] = { 6, 4, 3 };	/* 2, 3, 4 lanes */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	hsw = mode->hsync_end - mode->hsync_start;
358c2ecf20Sopenharmony_ci	hfp = mode->hsync_start - mode->hdisplay;
368c2ecf20Sopenharmony_ci	hbp = mode->htotal - mode->hsync_end;
378c2ecf20Sopenharmony_ci	vsw = mode->vsync_end - mode->vsync_start;
388c2ecf20Sopenharmony_ci	vfp = mode->vsync_start - mode->vdisplay;
398c2ecf20Sopenharmony_ci	vbp = mode->vtotal - mode->vsync_end;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	/* set pixel clock divider mode */
428c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x16,
438c2ecf20Sopenharmony_ci		     clock_div_by_lanes[dsi->lanes - 2] << 3);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/* horizontal porch params */
468c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x28, mode->htotal >> 4);
478c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x29, (mode->htotal << 4) & 0xff);
488c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x2a, hsw >> 4);
498c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x2b, (hsw << 4) & 0xff);
508c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x2c, hfp >> 4);
518c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x2d, (hfp << 4) & 0xff);
528c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x2e, hbp >> 4);
538c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x2f, (hbp << 4) & 0xff);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* vertical porch params */
568c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x30, mode->vtotal >> 4);
578c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x31, (mode->vtotal << 4) & 0xff);
588c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x32, vsw >> 4);
598c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x33, (vsw << 4) & 0xff);
608c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x34, vfp >> 4);
618c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x35, (vfp << 4) & 0xff);
628c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x36, vbp >> 4);
638c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x37, (vbp << 4) & 0xff);
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_civoid adv7533_dsi_power_on(struct adv7511 *adv)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = adv->dsi;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	if (adv->use_timing_gen)
718c2ecf20Sopenharmony_ci		adv7511_dsi_config_timing_gen(adv);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/* set number of dsi lanes */
748c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x1c, dsi->lanes << 4);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	if (adv->use_timing_gen) {
778c2ecf20Sopenharmony_ci		/* reset internal timing generator */
788c2ecf20Sopenharmony_ci		regmap_write(adv->regmap_cec, 0x27, 0xcb);
798c2ecf20Sopenharmony_ci		regmap_write(adv->regmap_cec, 0x27, 0x8b);
808c2ecf20Sopenharmony_ci		regmap_write(adv->regmap_cec, 0x27, 0xcb);
818c2ecf20Sopenharmony_ci	} else {
828c2ecf20Sopenharmony_ci		/* disable internal timing generator */
838c2ecf20Sopenharmony_ci		regmap_write(adv->regmap_cec, 0x27, 0x0b);
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	/* enable hdmi */
878c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x03, 0x89);
888c2ecf20Sopenharmony_ci	/* disable test mode */
898c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x55, 0x00);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	regmap_register_patch(adv->regmap_cec, adv7533_cec_fixed_registers,
928c2ecf20Sopenharmony_ci			      ARRAY_SIZE(adv7533_cec_fixed_registers));
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_civoid adv7533_dsi_power_off(struct adv7511 *adv)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	/* disable hdmi */
988c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x03, 0x0b);
998c2ecf20Sopenharmony_ci	/* disable internal timing generator */
1008c2ecf20Sopenharmony_ci	regmap_write(adv->regmap_cec, 0x27, 0x0b);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cienum drm_mode_status adv7533_mode_valid(struct adv7511 *adv,
1048c2ecf20Sopenharmony_ci					const struct drm_display_mode *mode)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	unsigned long max_lane_freq;
1078c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi = adv->dsi;
1088c2ecf20Sopenharmony_ci	u8 bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* Check max clock for either 7533 or 7535 */
1118c2ecf20Sopenharmony_ci	if (mode->clock > (adv->type == ADV7533 ? 80000 : 148500))
1128c2ecf20Sopenharmony_ci		return MODE_CLOCK_HIGH;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	/* Check max clock for each lane */
1158c2ecf20Sopenharmony_ci	max_lane_freq = (adv->type == ADV7533 ? 800000 : 891000);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (mode->clock * bpp > max_lane_freq * adv->num_dsi_lanes)
1188c2ecf20Sopenharmony_ci		return MODE_CLOCK_HIGH;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return MODE_OK;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciint adv7533_patch_registers(struct adv7511 *adv)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	return regmap_register_patch(adv->regmap,
1268c2ecf20Sopenharmony_ci				     adv7533_fixed_registers,
1278c2ecf20Sopenharmony_ci				     ARRAY_SIZE(adv7533_fixed_registers));
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ciint adv7533_patch_cec_registers(struct adv7511 *adv)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	return regmap_register_patch(adv->regmap_cec,
1338c2ecf20Sopenharmony_ci				    adv7533_cec_fixed_registers,
1348c2ecf20Sopenharmony_ci				    ARRAY_SIZE(adv7533_cec_fixed_registers));
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciint adv7533_attach_dsi(struct adv7511 *adv)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	struct device *dev = &adv->i2c_main->dev;
1408c2ecf20Sopenharmony_ci	struct mipi_dsi_host *host;
1418c2ecf20Sopenharmony_ci	struct mipi_dsi_device *dsi;
1428c2ecf20Sopenharmony_ci	int ret = 0;
1438c2ecf20Sopenharmony_ci	const struct mipi_dsi_device_info info = { .type = "adv7533",
1448c2ecf20Sopenharmony_ci						   .channel = 0,
1458c2ecf20Sopenharmony_ci						   .node = NULL,
1468c2ecf20Sopenharmony_ci						 };
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	host = of_find_mipi_dsi_host_by_node(adv->host_node);
1498c2ecf20Sopenharmony_ci	if (!host) {
1508c2ecf20Sopenharmony_ci		dev_err(dev, "failed to find dsi host\n");
1518c2ecf20Sopenharmony_ci		return -EPROBE_DEFER;
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	dsi = mipi_dsi_device_register_full(host, &info);
1558c2ecf20Sopenharmony_ci	if (IS_ERR(dsi)) {
1568c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create dsi device\n");
1578c2ecf20Sopenharmony_ci		ret = PTR_ERR(dsi);
1588c2ecf20Sopenharmony_ci		goto err_dsi_device;
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	adv->dsi = dsi;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	dsi->lanes = adv->num_dsi_lanes;
1648c2ecf20Sopenharmony_ci	dsi->format = MIPI_DSI_FMT_RGB888;
1658c2ecf20Sopenharmony_ci	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
1668c2ecf20Sopenharmony_ci			  MIPI_DSI_MODE_EOT_PACKET | MIPI_DSI_MODE_VIDEO_HSE;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	ret = mipi_dsi_attach(dsi);
1698c2ecf20Sopenharmony_ci	if (ret < 0) {
1708c2ecf20Sopenharmony_ci		dev_err(dev, "failed to attach dsi to host\n");
1718c2ecf20Sopenharmony_ci		goto err_dsi_attach;
1728c2ecf20Sopenharmony_ci	}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	return 0;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cierr_dsi_attach:
1778c2ecf20Sopenharmony_ci	mipi_dsi_device_unregister(dsi);
1788c2ecf20Sopenharmony_cierr_dsi_device:
1798c2ecf20Sopenharmony_ci	return ret;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_civoid adv7533_detach_dsi(struct adv7511 *adv)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	mipi_dsi_detach(adv->dsi);
1858c2ecf20Sopenharmony_ci	mipi_dsi_device_unregister(adv->dsi);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ciint adv7533_parse_dt(struct device_node *np, struct adv7511 *adv)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	u32 num_lanes;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	of_property_read_u32(np, "adi,dsi-lanes", &num_lanes);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	if (num_lanes < 1 || num_lanes > 4)
1958c2ecf20Sopenharmony_ci		return -EINVAL;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	adv->num_dsi_lanes = num_lanes;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	adv->host_node = of_graph_get_remote_node(np, 0, 0);
2008c2ecf20Sopenharmony_ci	if (!adv->host_node)
2018c2ecf20Sopenharmony_ci		return -ENODEV;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	of_node_put(adv->host_node);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	adv->use_timing_gen = !of_property_read_bool(np,
2068c2ecf20Sopenharmony_ci						"adi,disable-timing-generator");
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	/* TODO: Check if these need to be parsed by DT or not */
2098c2ecf20Sopenharmony_ci	adv->rgb = true;
2108c2ecf20Sopenharmony_ci	adv->embedded_sync = false;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return 0;
2138c2ecf20Sopenharmony_ci}
214