zx_tvenc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright 2017 Linaro Ltd.
  3. * Copyright 2017 ZTE Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/component.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/regmap.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drmP.h>
  17. #include "zx_drm_drv.h"
  18. #include "zx_tvenc_regs.h"
  19. #include "zx_vou.h"
  20. struct zx_tvenc_pwrctrl {
  21. struct regmap *regmap;
  22. u32 reg;
  23. u32 mask;
  24. };
  25. struct zx_tvenc {
  26. struct drm_connector connector;
  27. struct drm_encoder encoder;
  28. struct device *dev;
  29. void __iomem *mmio;
  30. const struct vou_inf *inf;
  31. struct zx_tvenc_pwrctrl pwrctrl;
  32. };
  33. #define to_zx_tvenc(x) container_of(x, struct zx_tvenc, x)
  34. struct zx_tvenc_mode {
  35. struct drm_display_mode mode;
  36. u32 video_info;
  37. u32 video_res;
  38. u32 field1_param;
  39. u32 field2_param;
  40. u32 burst_line_odd1;
  41. u32 burst_line_even1;
  42. u32 burst_line_odd2;
  43. u32 burst_line_even2;
  44. u32 line_timing_param;
  45. u32 weight_value;
  46. u32 blank_black_level;
  47. u32 burst_level;
  48. u32 control_param;
  49. u32 sub_carrier_phase1;
  50. u32 phase_line_incr_cvbs;
  51. };
  52. /*
  53. * The CRM cannot directly provide a suitable frequency, and we have to
  54. * ask a multiplied rate from CRM and use the divider in VOU to get the
  55. * desired one.
  56. */
  57. #define TVENC_CLOCK_MULTIPLIER 4
  58. static const struct zx_tvenc_mode tvenc_mode_pal = {
  59. .mode = {
  60. .clock = 13500 * TVENC_CLOCK_MULTIPLIER,
  61. .hdisplay = 720,
  62. .hsync_start = 720 + 12,
  63. .hsync_end = 720 + 12 + 2,
  64. .htotal = 720 + 12 + 2 + 130,
  65. .vdisplay = 576,
  66. .vsync_start = 576 + 2,
  67. .vsync_end = 576 + 2 + 2,
  68. .vtotal = 576 + 2 + 2 + 20,
  69. .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
  70. DRM_MODE_FLAG_INTERLACE,
  71. },
  72. .video_info = 0x00040040,
  73. .video_res = 0x05a9c760,
  74. .field1_param = 0x0004d416,
  75. .field2_param = 0x0009b94f,
  76. .burst_line_odd1 = 0x0004d406,
  77. .burst_line_even1 = 0x0009b53e,
  78. .burst_line_odd2 = 0x0004d805,
  79. .burst_line_even2 = 0x0009b93f,
  80. .line_timing_param = 0x06a96fdf,
  81. .weight_value = 0x00c188a0,
  82. .blank_black_level = 0x0000fcfc,
  83. .burst_level = 0x00001595,
  84. .control_param = 0x00000001,
  85. .sub_carrier_phase1 = 0x1504c566,
  86. .phase_line_incr_cvbs = 0xc068db8c,
  87. };
  88. static const struct zx_tvenc_mode tvenc_mode_ntsc = {
  89. .mode = {
  90. .clock = 13500 * TVENC_CLOCK_MULTIPLIER,
  91. .hdisplay = 720,
  92. .hsync_start = 720 + 16,
  93. .hsync_end = 720 + 16 + 2,
  94. .htotal = 720 + 16 + 2 + 120,
  95. .vdisplay = 480,
  96. .vsync_start = 480 + 3,
  97. .vsync_end = 480 + 3 + 2,
  98. .vtotal = 480 + 3 + 2 + 17,
  99. .flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
  100. DRM_MODE_FLAG_INTERLACE,
  101. },
  102. .video_info = 0x00040080,
  103. .video_res = 0x05a8375a,
  104. .field1_param = 0x00041817,
  105. .field2_param = 0x0008351e,
  106. .burst_line_odd1 = 0x00041006,
  107. .burst_line_even1 = 0x0008290d,
  108. .burst_line_odd2 = 0x00000000,
  109. .burst_line_even2 = 0x00000000,
  110. .line_timing_param = 0x06a8ef9e,
  111. .weight_value = 0x00b68197,
  112. .blank_black_level = 0x0000f0f0,
  113. .burst_level = 0x0000009c,
  114. .control_param = 0x00000001,
  115. .sub_carrier_phase1 = 0x10f83e10,
  116. .phase_line_incr_cvbs = 0x80000000,
  117. };
  118. static const struct zx_tvenc_mode *tvenc_modes[] = {
  119. &tvenc_mode_pal,
  120. &tvenc_mode_ntsc,
  121. };
  122. static const struct zx_tvenc_mode *
  123. zx_tvenc_find_zmode(struct drm_display_mode *mode)
  124. {
  125. int i;
  126. for (i = 0; i < ARRAY_SIZE(tvenc_modes); i++) {
  127. const struct zx_tvenc_mode *zmode = tvenc_modes[i];
  128. if (drm_mode_equal(mode, &zmode->mode))
  129. return zmode;
  130. }
  131. return NULL;
  132. }
  133. static void zx_tvenc_encoder_mode_set(struct drm_encoder *encoder,
  134. struct drm_display_mode *mode,
  135. struct drm_display_mode *adj_mode)
  136. {
  137. struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
  138. const struct zx_tvenc_mode *zmode;
  139. struct vou_div_config configs[] = {
  140. { VOU_DIV_INF, VOU_DIV_4 },
  141. { VOU_DIV_TVENC, VOU_DIV_1 },
  142. { VOU_DIV_LAYER, VOU_DIV_2 },
  143. };
  144. zx_vou_config_dividers(encoder->crtc, configs, ARRAY_SIZE(configs));
  145. zmode = zx_tvenc_find_zmode(mode);
  146. if (!zmode) {
  147. DRM_DEV_ERROR(tvenc->dev, "failed to find zmode\n");
  148. return;
  149. }
  150. zx_writel(tvenc->mmio + VENC_VIDEO_INFO, zmode->video_info);
  151. zx_writel(tvenc->mmio + VENC_VIDEO_RES, zmode->video_res);
  152. zx_writel(tvenc->mmio + VENC_FIELD1_PARAM, zmode->field1_param);
  153. zx_writel(tvenc->mmio + VENC_FIELD2_PARAM, zmode->field2_param);
  154. zx_writel(tvenc->mmio + VENC_LINE_O_1, zmode->burst_line_odd1);
  155. zx_writel(tvenc->mmio + VENC_LINE_E_1, zmode->burst_line_even1);
  156. zx_writel(tvenc->mmio + VENC_LINE_O_2, zmode->burst_line_odd2);
  157. zx_writel(tvenc->mmio + VENC_LINE_E_2, zmode->burst_line_even2);
  158. zx_writel(tvenc->mmio + VENC_LINE_TIMING_PARAM,
  159. zmode->line_timing_param);
  160. zx_writel(tvenc->mmio + VENC_WEIGHT_VALUE, zmode->weight_value);
  161. zx_writel(tvenc->mmio + VENC_BLANK_BLACK_LEVEL,
  162. zmode->blank_black_level);
  163. zx_writel(tvenc->mmio + VENC_BURST_LEVEL, zmode->burst_level);
  164. zx_writel(tvenc->mmio + VENC_CONTROL_PARAM, zmode->control_param);
  165. zx_writel(tvenc->mmio + VENC_SUB_CARRIER_PHASE1,
  166. zmode->sub_carrier_phase1);
  167. zx_writel(tvenc->mmio + VENC_PHASE_LINE_INCR_CVBS,
  168. zmode->phase_line_incr_cvbs);
  169. }
  170. static void zx_tvenc_encoder_enable(struct drm_encoder *encoder)
  171. {
  172. struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
  173. struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
  174. /* Set bit to power up TVENC DAC */
  175. regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask,
  176. pwrctrl->mask);
  177. vou_inf_enable(VOU_TV_ENC, encoder->crtc);
  178. zx_writel(tvenc->mmio + VENC_ENABLE, 1);
  179. }
  180. static void zx_tvenc_encoder_disable(struct drm_encoder *encoder)
  181. {
  182. struct zx_tvenc *tvenc = to_zx_tvenc(encoder);
  183. struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
  184. zx_writel(tvenc->mmio + VENC_ENABLE, 0);
  185. vou_inf_disable(VOU_TV_ENC, encoder->crtc);
  186. /* Clear bit to power down TVENC DAC */
  187. regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask, 0);
  188. }
  189. static const struct drm_encoder_helper_funcs zx_tvenc_encoder_helper_funcs = {
  190. .enable = zx_tvenc_encoder_enable,
  191. .disable = zx_tvenc_encoder_disable,
  192. .mode_set = zx_tvenc_encoder_mode_set,
  193. };
  194. static const struct drm_encoder_funcs zx_tvenc_encoder_funcs = {
  195. .destroy = drm_encoder_cleanup,
  196. };
  197. static int zx_tvenc_connector_get_modes(struct drm_connector *connector)
  198. {
  199. struct zx_tvenc *tvenc = to_zx_tvenc(connector);
  200. struct device *dev = tvenc->dev;
  201. int i;
  202. for (i = 0; i < ARRAY_SIZE(tvenc_modes); i++) {
  203. const struct zx_tvenc_mode *zmode = tvenc_modes[i];
  204. struct drm_display_mode *mode;
  205. mode = drm_mode_duplicate(connector->dev, &zmode->mode);
  206. if (!mode) {
  207. DRM_DEV_ERROR(dev, "failed to duplicate drm mode\n");
  208. continue;
  209. }
  210. drm_mode_set_name(mode);
  211. drm_mode_probed_add(connector, mode);
  212. }
  213. return i;
  214. }
  215. static enum drm_mode_status
  216. zx_tvenc_connector_mode_valid(struct drm_connector *connector,
  217. struct drm_display_mode *mode)
  218. {
  219. struct zx_tvenc *tvenc = to_zx_tvenc(connector);
  220. const struct zx_tvenc_mode *zmode;
  221. zmode = zx_tvenc_find_zmode(mode);
  222. if (!zmode) {
  223. DRM_DEV_ERROR(tvenc->dev, "unsupported mode: %s\n", mode->name);
  224. return MODE_NOMODE;
  225. }
  226. return MODE_OK;
  227. }
  228. static struct drm_connector_helper_funcs zx_tvenc_connector_helper_funcs = {
  229. .get_modes = zx_tvenc_connector_get_modes,
  230. .mode_valid = zx_tvenc_connector_mode_valid,
  231. };
  232. static const struct drm_connector_funcs zx_tvenc_connector_funcs = {
  233. .fill_modes = drm_helper_probe_single_connector_modes,
  234. .destroy = drm_connector_cleanup,
  235. .reset = drm_atomic_helper_connector_reset,
  236. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  237. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  238. };
  239. static int zx_tvenc_register(struct drm_device *drm, struct zx_tvenc *tvenc)
  240. {
  241. struct drm_encoder *encoder = &tvenc->encoder;
  242. struct drm_connector *connector = &tvenc->connector;
  243. /*
  244. * The tvenc is designed to use aux channel, as there is a deflicker
  245. * block for the channel.
  246. */
  247. encoder->possible_crtcs = BIT(1);
  248. drm_encoder_init(drm, encoder, &zx_tvenc_encoder_funcs,
  249. DRM_MODE_ENCODER_TVDAC, NULL);
  250. drm_encoder_helper_add(encoder, &zx_tvenc_encoder_helper_funcs);
  251. connector->interlace_allowed = true;
  252. drm_connector_init(drm, connector, &zx_tvenc_connector_funcs,
  253. DRM_MODE_CONNECTOR_Composite);
  254. drm_connector_helper_add(connector, &zx_tvenc_connector_helper_funcs);
  255. drm_mode_connector_attach_encoder(connector, encoder);
  256. return 0;
  257. }
  258. static int zx_tvenc_pwrctrl_init(struct zx_tvenc *tvenc)
  259. {
  260. struct zx_tvenc_pwrctrl *pwrctrl = &tvenc->pwrctrl;
  261. struct device *dev = tvenc->dev;
  262. struct of_phandle_args out_args;
  263. struct regmap *regmap;
  264. int ret;
  265. ret = of_parse_phandle_with_fixed_args(dev->of_node,
  266. "zte,tvenc-power-control", 2, 0, &out_args);
  267. if (ret)
  268. return ret;
  269. regmap = syscon_node_to_regmap(out_args.np);
  270. if (IS_ERR(regmap)) {
  271. ret = PTR_ERR(regmap);
  272. goto out;
  273. }
  274. pwrctrl->regmap = regmap;
  275. pwrctrl->reg = out_args.args[0];
  276. pwrctrl->mask = out_args.args[1];
  277. out:
  278. of_node_put(out_args.np);
  279. return ret;
  280. }
  281. static int zx_tvenc_bind(struct device *dev, struct device *master, void *data)
  282. {
  283. struct platform_device *pdev = to_platform_device(dev);
  284. struct drm_device *drm = data;
  285. struct resource *res;
  286. struct zx_tvenc *tvenc;
  287. int ret;
  288. tvenc = devm_kzalloc(dev, sizeof(*tvenc), GFP_KERNEL);
  289. if (!tvenc)
  290. return -ENOMEM;
  291. tvenc->dev = dev;
  292. dev_set_drvdata(dev, tvenc);
  293. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  294. tvenc->mmio = devm_ioremap_resource(dev, res);
  295. if (IS_ERR(tvenc->mmio)) {
  296. ret = PTR_ERR(tvenc->mmio);
  297. DRM_DEV_ERROR(dev, "failed to remap tvenc region: %d\n", ret);
  298. return ret;
  299. }
  300. ret = zx_tvenc_pwrctrl_init(tvenc);
  301. if (ret) {
  302. DRM_DEV_ERROR(dev, "failed to init power control: %d\n", ret);
  303. return ret;
  304. }
  305. ret = zx_tvenc_register(drm, tvenc);
  306. if (ret) {
  307. DRM_DEV_ERROR(dev, "failed to register tvenc: %d\n", ret);
  308. return ret;
  309. }
  310. return 0;
  311. }
  312. static void zx_tvenc_unbind(struct device *dev, struct device *master,
  313. void *data)
  314. {
  315. /* Nothing to do */
  316. }
  317. static const struct component_ops zx_tvenc_component_ops = {
  318. .bind = zx_tvenc_bind,
  319. .unbind = zx_tvenc_unbind,
  320. };
  321. static int zx_tvenc_probe(struct platform_device *pdev)
  322. {
  323. return component_add(&pdev->dev, &zx_tvenc_component_ops);
  324. }
  325. static int zx_tvenc_remove(struct platform_device *pdev)
  326. {
  327. component_del(&pdev->dev, &zx_tvenc_component_ops);
  328. return 0;
  329. }
  330. static const struct of_device_id zx_tvenc_of_match[] = {
  331. { .compatible = "zte,zx296718-tvenc", },
  332. { /* end */ },
  333. };
  334. MODULE_DEVICE_TABLE(of, zx_tvenc_of_match);
  335. struct platform_driver zx_tvenc_driver = {
  336. .probe = zx_tvenc_probe,
  337. .remove = zx_tvenc_remove,
  338. .driver = {
  339. .name = "zx-tvenc",
  340. .of_match_table = zx_tvenc_of_match,
  341. },
  342. };