sun4i_tv.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * Copyright (C) 2015 Free Electrons
  3. * Copyright (C) 2015 NextThing Co
  4. *
  5. * Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/component.h>
  14. #include <linux/of_address.h>
  15. #include <linux/regmap.h>
  16. #include <linux/reset.h>
  17. #include <drm/drmP.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_crtc_helper.h>
  20. #include <drm/drm_of.h>
  21. #include <drm/drm_panel.h>
  22. #include "sun4i_crtc.h"
  23. #include "sun4i_drv.h"
  24. #include "sun4i_tcon.h"
  25. #include "sunxi_engine.h"
  26. #define SUN4I_TVE_EN_REG 0x000
  27. #define SUN4I_TVE_EN_DAC_MAP_MASK GENMASK(19, 4)
  28. #define SUN4I_TVE_EN_DAC_MAP(dac, out) (((out) & 0xf) << (dac + 1) * 4)
  29. #define SUN4I_TVE_EN_ENABLE BIT(0)
  30. #define SUN4I_TVE_CFG0_REG 0x004
  31. #define SUN4I_TVE_CFG0_DAC_CONTROL_54M BIT(26)
  32. #define SUN4I_TVE_CFG0_CORE_DATAPATH_54M BIT(25)
  33. #define SUN4I_TVE_CFG0_CORE_CONTROL_54M BIT(24)
  34. #define SUN4I_TVE_CFG0_YC_EN BIT(17)
  35. #define SUN4I_TVE_CFG0_COMP_EN BIT(16)
  36. #define SUN4I_TVE_CFG0_RES(x) ((x) & 0xf)
  37. #define SUN4I_TVE_CFG0_RES_480i SUN4I_TVE_CFG0_RES(0)
  38. #define SUN4I_TVE_CFG0_RES_576i SUN4I_TVE_CFG0_RES(1)
  39. #define SUN4I_TVE_DAC0_REG 0x008
  40. #define SUN4I_TVE_DAC0_CLOCK_INVERT BIT(24)
  41. #define SUN4I_TVE_DAC0_LUMA(x) (((x) & 3) << 20)
  42. #define SUN4I_TVE_DAC0_LUMA_0_4 SUN4I_TVE_DAC0_LUMA(3)
  43. #define SUN4I_TVE_DAC0_CHROMA(x) (((x) & 3) << 18)
  44. #define SUN4I_TVE_DAC0_CHROMA_0_75 SUN4I_TVE_DAC0_CHROMA(3)
  45. #define SUN4I_TVE_DAC0_INTERNAL_DAC(x) (((x) & 3) << 16)
  46. #define SUN4I_TVE_DAC0_INTERNAL_DAC_37_5_OHMS SUN4I_TVE_DAC0_INTERNAL_DAC(3)
  47. #define SUN4I_TVE_DAC0_DAC_EN(dac) BIT(dac)
  48. #define SUN4I_TVE_NOTCH_REG 0x00c
  49. #define SUN4I_TVE_NOTCH_DAC0_TO_DAC_DLY(dac, x) ((4 - (x)) << (dac * 3))
  50. #define SUN4I_TVE_CHROMA_FREQ_REG 0x010
  51. #define SUN4I_TVE_PORCH_REG 0x014
  52. #define SUN4I_TVE_PORCH_BACK(x) ((x) << 16)
  53. #define SUN4I_TVE_PORCH_FRONT(x) (x)
  54. #define SUN4I_TVE_LINE_REG 0x01c
  55. #define SUN4I_TVE_LINE_FIRST(x) ((x) << 16)
  56. #define SUN4I_TVE_LINE_NUMBER(x) (x)
  57. #define SUN4I_TVE_LEVEL_REG 0x020
  58. #define SUN4I_TVE_LEVEL_BLANK(x) ((x) << 16)
  59. #define SUN4I_TVE_LEVEL_BLACK(x) (x)
  60. #define SUN4I_TVE_DAC1_REG 0x024
  61. #define SUN4I_TVE_DAC1_AMPLITUDE(dac, x) ((x) << (dac * 8))
  62. #define SUN4I_TVE_DETECT_STA_REG 0x038
  63. #define SUN4I_TVE_DETECT_STA_DAC(dac) BIT((dac * 8))
  64. #define SUN4I_TVE_DETECT_STA_UNCONNECTED 0
  65. #define SUN4I_TVE_DETECT_STA_CONNECTED 1
  66. #define SUN4I_TVE_DETECT_STA_GROUND 2
  67. #define SUN4I_TVE_CB_CR_LVL_REG 0x10c
  68. #define SUN4I_TVE_CB_CR_LVL_CR_BURST(x) ((x) << 8)
  69. #define SUN4I_TVE_CB_CR_LVL_CB_BURST(x) (x)
  70. #define SUN4I_TVE_TINT_BURST_PHASE_REG 0x110
  71. #define SUN4I_TVE_TINT_BURST_PHASE_CHROMA(x) (x)
  72. #define SUN4I_TVE_BURST_WIDTH_REG 0x114
  73. #define SUN4I_TVE_BURST_WIDTH_BREEZEWAY(x) ((x) << 16)
  74. #define SUN4I_TVE_BURST_WIDTH_BURST_WIDTH(x) ((x) << 8)
  75. #define SUN4I_TVE_BURST_WIDTH_HSYNC_WIDTH(x) (x)
  76. #define SUN4I_TVE_CB_CR_GAIN_REG 0x118
  77. #define SUN4I_TVE_CB_CR_GAIN_CR(x) ((x) << 8)
  78. #define SUN4I_TVE_CB_CR_GAIN_CB(x) (x)
  79. #define SUN4I_TVE_SYNC_VBI_REG 0x11c
  80. #define SUN4I_TVE_SYNC_VBI_SYNC(x) ((x) << 16)
  81. #define SUN4I_TVE_SYNC_VBI_VBLANK(x) (x)
  82. #define SUN4I_TVE_ACTIVE_LINE_REG 0x124
  83. #define SUN4I_TVE_ACTIVE_LINE(x) (x)
  84. #define SUN4I_TVE_CHROMA_REG 0x128
  85. #define SUN4I_TVE_CHROMA_COMP_GAIN(x) ((x) & 3)
  86. #define SUN4I_TVE_CHROMA_COMP_GAIN_50 SUN4I_TVE_CHROMA_COMP_GAIN(2)
  87. #define SUN4I_TVE_12C_REG 0x12c
  88. #define SUN4I_TVE_12C_NOTCH_WIDTH_WIDE BIT(8)
  89. #define SUN4I_TVE_12C_COMP_YUV_EN BIT(0)
  90. #define SUN4I_TVE_RESYNC_REG 0x130
  91. #define SUN4I_TVE_RESYNC_FIELD BIT(31)
  92. #define SUN4I_TVE_RESYNC_LINE(x) ((x) << 16)
  93. #define SUN4I_TVE_RESYNC_PIXEL(x) (x)
  94. #define SUN4I_TVE_SLAVE_REG 0x134
  95. #define SUN4I_TVE_WSS_DATA2_REG 0x244
  96. struct color_gains {
  97. u16 cb;
  98. u16 cr;
  99. };
  100. struct burst_levels {
  101. u16 cb;
  102. u16 cr;
  103. };
  104. struct video_levels {
  105. u16 black;
  106. u16 blank;
  107. };
  108. struct resync_parameters {
  109. bool field;
  110. u16 line;
  111. u16 pixel;
  112. };
  113. struct tv_mode {
  114. char *name;
  115. u32 mode;
  116. u32 chroma_freq;
  117. u16 back_porch;
  118. u16 front_porch;
  119. u16 line_number;
  120. u16 vblank_level;
  121. u32 hdisplay;
  122. u16 hfront_porch;
  123. u16 hsync_len;
  124. u16 hback_porch;
  125. u32 vdisplay;
  126. u16 vfront_porch;
  127. u16 vsync_len;
  128. u16 vback_porch;
  129. bool yc_en;
  130. bool dac3_en;
  131. bool dac_bit25_en;
  132. const struct color_gains *color_gains;
  133. const struct burst_levels *burst_levels;
  134. const struct video_levels *video_levels;
  135. const struct resync_parameters *resync_params;
  136. };
  137. struct sun4i_tv {
  138. struct drm_connector connector;
  139. struct drm_encoder encoder;
  140. struct clk *clk;
  141. struct regmap *regs;
  142. struct reset_control *reset;
  143. struct sun4i_drv *drv;
  144. };
  145. static const struct video_levels ntsc_video_levels = {
  146. .black = 282, .blank = 240,
  147. };
  148. static const struct video_levels pal_video_levels = {
  149. .black = 252, .blank = 252,
  150. };
  151. static const struct burst_levels ntsc_burst_levels = {
  152. .cb = 79, .cr = 0,
  153. };
  154. static const struct burst_levels pal_burst_levels = {
  155. .cb = 40, .cr = 40,
  156. };
  157. static const struct color_gains ntsc_color_gains = {
  158. .cb = 160, .cr = 160,
  159. };
  160. static const struct color_gains pal_color_gains = {
  161. .cb = 224, .cr = 224,
  162. };
  163. static const struct resync_parameters ntsc_resync_parameters = {
  164. .field = false, .line = 14, .pixel = 12,
  165. };
  166. static const struct resync_parameters pal_resync_parameters = {
  167. .field = true, .line = 13, .pixel = 12,
  168. };
  169. static const struct tv_mode tv_modes[] = {
  170. {
  171. .name = "NTSC",
  172. .mode = SUN4I_TVE_CFG0_RES_480i,
  173. .chroma_freq = 0x21f07c1f,
  174. .yc_en = true,
  175. .dac3_en = true,
  176. .dac_bit25_en = true,
  177. .back_porch = 118,
  178. .front_porch = 32,
  179. .line_number = 525,
  180. .hdisplay = 720,
  181. .hfront_porch = 18,
  182. .hsync_len = 2,
  183. .hback_porch = 118,
  184. .vdisplay = 480,
  185. .vfront_porch = 26,
  186. .vsync_len = 2,
  187. .vback_porch = 17,
  188. .vblank_level = 240,
  189. .color_gains = &ntsc_color_gains,
  190. .burst_levels = &ntsc_burst_levels,
  191. .video_levels = &ntsc_video_levels,
  192. .resync_params = &ntsc_resync_parameters,
  193. },
  194. {
  195. .name = "PAL",
  196. .mode = SUN4I_TVE_CFG0_RES_576i,
  197. .chroma_freq = 0x2a098acb,
  198. .back_porch = 138,
  199. .front_porch = 24,
  200. .line_number = 625,
  201. .hdisplay = 720,
  202. .hfront_porch = 3,
  203. .hsync_len = 2,
  204. .hback_porch = 139,
  205. .vdisplay = 576,
  206. .vfront_porch = 28,
  207. .vsync_len = 2,
  208. .vback_porch = 19,
  209. .vblank_level = 252,
  210. .color_gains = &pal_color_gains,
  211. .burst_levels = &pal_burst_levels,
  212. .video_levels = &pal_video_levels,
  213. .resync_params = &pal_resync_parameters,
  214. },
  215. };
  216. static inline struct sun4i_tv *
  217. drm_encoder_to_sun4i_tv(struct drm_encoder *encoder)
  218. {
  219. return container_of(encoder, struct sun4i_tv,
  220. encoder);
  221. }
  222. static inline struct sun4i_tv *
  223. drm_connector_to_sun4i_tv(struct drm_connector *connector)
  224. {
  225. return container_of(connector, struct sun4i_tv,
  226. connector);
  227. }
  228. /*
  229. * FIXME: If only the drm_display_mode private field was usable, this
  230. * could go away...
  231. *
  232. * So far, it doesn't seem to be preserved when the mode is passed by
  233. * to mode_set for some reason.
  234. */
  235. static const struct tv_mode *sun4i_tv_find_tv_by_mode(const struct drm_display_mode *mode)
  236. {
  237. int i;
  238. /* First try to identify the mode by name */
  239. for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
  240. const struct tv_mode *tv_mode = &tv_modes[i];
  241. DRM_DEBUG_DRIVER("Comparing mode %s vs %s",
  242. mode->name, tv_mode->name);
  243. if (!strcmp(mode->name, tv_mode->name))
  244. return tv_mode;
  245. }
  246. /* Then by number of lines */
  247. for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
  248. const struct tv_mode *tv_mode = &tv_modes[i];
  249. DRM_DEBUG_DRIVER("Comparing mode %s vs %s (X: %d vs %d)",
  250. mode->name, tv_mode->name,
  251. mode->vdisplay, tv_mode->vdisplay);
  252. if (mode->vdisplay == tv_mode->vdisplay)
  253. return tv_mode;
  254. }
  255. return NULL;
  256. }
  257. static void sun4i_tv_mode_to_drm_mode(const struct tv_mode *tv_mode,
  258. struct drm_display_mode *mode)
  259. {
  260. DRM_DEBUG_DRIVER("Creating mode %s\n", mode->name);
  261. mode->type = DRM_MODE_TYPE_DRIVER;
  262. mode->clock = 13500;
  263. mode->flags = DRM_MODE_FLAG_INTERLACE;
  264. mode->hdisplay = tv_mode->hdisplay;
  265. mode->hsync_start = mode->hdisplay + tv_mode->hfront_porch;
  266. mode->hsync_end = mode->hsync_start + tv_mode->hsync_len;
  267. mode->htotal = mode->hsync_end + tv_mode->hback_porch;
  268. mode->vdisplay = tv_mode->vdisplay;
  269. mode->vsync_start = mode->vdisplay + tv_mode->vfront_porch;
  270. mode->vsync_end = mode->vsync_start + tv_mode->vsync_len;
  271. mode->vtotal = mode->vsync_end + tv_mode->vback_porch;
  272. }
  273. static void sun4i_tv_disable(struct drm_encoder *encoder)
  274. {
  275. struct sun4i_tv *tv = drm_encoder_to_sun4i_tv(encoder);
  276. struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
  277. struct sun4i_tcon *tcon = crtc->tcon;
  278. DRM_DEBUG_DRIVER("Disabling the TV Output\n");
  279. sun4i_tcon_channel_disable(tcon, 1);
  280. regmap_update_bits(tv->regs, SUN4I_TVE_EN_REG,
  281. SUN4I_TVE_EN_ENABLE,
  282. 0);
  283. sunxi_engine_disable_color_correction(crtc->engine);
  284. }
  285. static void sun4i_tv_enable(struct drm_encoder *encoder)
  286. {
  287. struct sun4i_tv *tv = drm_encoder_to_sun4i_tv(encoder);
  288. struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
  289. struct sun4i_tcon *tcon = crtc->tcon;
  290. DRM_DEBUG_DRIVER("Enabling the TV Output\n");
  291. sunxi_engine_apply_color_correction(crtc->engine);
  292. regmap_update_bits(tv->regs, SUN4I_TVE_EN_REG,
  293. SUN4I_TVE_EN_ENABLE,
  294. SUN4I_TVE_EN_ENABLE);
  295. sun4i_tcon_channel_enable(tcon, 1);
  296. }
  297. static void sun4i_tv_mode_set(struct drm_encoder *encoder,
  298. struct drm_display_mode *mode,
  299. struct drm_display_mode *adjusted_mode)
  300. {
  301. struct sun4i_tv *tv = drm_encoder_to_sun4i_tv(encoder);
  302. struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
  303. struct sun4i_tcon *tcon = crtc->tcon;
  304. const struct tv_mode *tv_mode = sun4i_tv_find_tv_by_mode(mode);
  305. sun4i_tcon1_mode_set(tcon, mode);
  306. sun4i_tcon_set_mux(tcon, 1, encoder);
  307. /* Enable and map the DAC to the output */
  308. regmap_update_bits(tv->regs, SUN4I_TVE_EN_REG,
  309. SUN4I_TVE_EN_DAC_MAP_MASK,
  310. SUN4I_TVE_EN_DAC_MAP(0, 1) |
  311. SUN4I_TVE_EN_DAC_MAP(1, 2) |
  312. SUN4I_TVE_EN_DAC_MAP(2, 3) |
  313. SUN4I_TVE_EN_DAC_MAP(3, 4));
  314. /* Set PAL settings */
  315. regmap_write(tv->regs, SUN4I_TVE_CFG0_REG,
  316. tv_mode->mode |
  317. (tv_mode->yc_en ? SUN4I_TVE_CFG0_YC_EN : 0) |
  318. SUN4I_TVE_CFG0_COMP_EN |
  319. SUN4I_TVE_CFG0_DAC_CONTROL_54M |
  320. SUN4I_TVE_CFG0_CORE_DATAPATH_54M |
  321. SUN4I_TVE_CFG0_CORE_CONTROL_54M);
  322. /* Configure the DAC for a composite output */
  323. regmap_write(tv->regs, SUN4I_TVE_DAC0_REG,
  324. SUN4I_TVE_DAC0_DAC_EN(0) |
  325. (tv_mode->dac3_en ? SUN4I_TVE_DAC0_DAC_EN(3) : 0) |
  326. SUN4I_TVE_DAC0_INTERNAL_DAC_37_5_OHMS |
  327. SUN4I_TVE_DAC0_CHROMA_0_75 |
  328. SUN4I_TVE_DAC0_LUMA_0_4 |
  329. SUN4I_TVE_DAC0_CLOCK_INVERT |
  330. (tv_mode->dac_bit25_en ? BIT(25) : 0) |
  331. BIT(30));
  332. /* Configure the sample delay between DAC0 and the other DAC */
  333. regmap_write(tv->regs, SUN4I_TVE_NOTCH_REG,
  334. SUN4I_TVE_NOTCH_DAC0_TO_DAC_DLY(1, 0) |
  335. SUN4I_TVE_NOTCH_DAC0_TO_DAC_DLY(2, 0));
  336. regmap_write(tv->regs, SUN4I_TVE_CHROMA_FREQ_REG,
  337. tv_mode->chroma_freq);
  338. /* Set the front and back porch */
  339. regmap_write(tv->regs, SUN4I_TVE_PORCH_REG,
  340. SUN4I_TVE_PORCH_BACK(tv_mode->back_porch) |
  341. SUN4I_TVE_PORCH_FRONT(tv_mode->front_porch));
  342. /* Set the lines setup */
  343. regmap_write(tv->regs, SUN4I_TVE_LINE_REG,
  344. SUN4I_TVE_LINE_FIRST(22) |
  345. SUN4I_TVE_LINE_NUMBER(tv_mode->line_number));
  346. regmap_write(tv->regs, SUN4I_TVE_LEVEL_REG,
  347. SUN4I_TVE_LEVEL_BLANK(tv_mode->video_levels->blank) |
  348. SUN4I_TVE_LEVEL_BLACK(tv_mode->video_levels->black));
  349. regmap_write(tv->regs, SUN4I_TVE_DAC1_REG,
  350. SUN4I_TVE_DAC1_AMPLITUDE(0, 0x18) |
  351. SUN4I_TVE_DAC1_AMPLITUDE(1, 0x18) |
  352. SUN4I_TVE_DAC1_AMPLITUDE(2, 0x18) |
  353. SUN4I_TVE_DAC1_AMPLITUDE(3, 0x18));
  354. regmap_write(tv->regs, SUN4I_TVE_CB_CR_LVL_REG,
  355. SUN4I_TVE_CB_CR_LVL_CB_BURST(tv_mode->burst_levels->cb) |
  356. SUN4I_TVE_CB_CR_LVL_CR_BURST(tv_mode->burst_levels->cr));
  357. /* Set burst width for a composite output */
  358. regmap_write(tv->regs, SUN4I_TVE_BURST_WIDTH_REG,
  359. SUN4I_TVE_BURST_WIDTH_HSYNC_WIDTH(126) |
  360. SUN4I_TVE_BURST_WIDTH_BURST_WIDTH(68) |
  361. SUN4I_TVE_BURST_WIDTH_BREEZEWAY(22));
  362. regmap_write(tv->regs, SUN4I_TVE_CB_CR_GAIN_REG,
  363. SUN4I_TVE_CB_CR_GAIN_CB(tv_mode->color_gains->cb) |
  364. SUN4I_TVE_CB_CR_GAIN_CR(tv_mode->color_gains->cr));
  365. regmap_write(tv->regs, SUN4I_TVE_SYNC_VBI_REG,
  366. SUN4I_TVE_SYNC_VBI_SYNC(0x10) |
  367. SUN4I_TVE_SYNC_VBI_VBLANK(tv_mode->vblank_level));
  368. regmap_write(tv->regs, SUN4I_TVE_ACTIVE_LINE_REG,
  369. SUN4I_TVE_ACTIVE_LINE(1440));
  370. /* Set composite chroma gain to 50 % */
  371. regmap_write(tv->regs, SUN4I_TVE_CHROMA_REG,
  372. SUN4I_TVE_CHROMA_COMP_GAIN_50);
  373. regmap_write(tv->regs, SUN4I_TVE_12C_REG,
  374. SUN4I_TVE_12C_COMP_YUV_EN |
  375. SUN4I_TVE_12C_NOTCH_WIDTH_WIDE);
  376. regmap_write(tv->regs, SUN4I_TVE_RESYNC_REG,
  377. SUN4I_TVE_RESYNC_PIXEL(tv_mode->resync_params->pixel) |
  378. SUN4I_TVE_RESYNC_LINE(tv_mode->resync_params->line) |
  379. (tv_mode->resync_params->field ?
  380. SUN4I_TVE_RESYNC_FIELD : 0));
  381. regmap_write(tv->regs, SUN4I_TVE_SLAVE_REG, 0);
  382. }
  383. static struct drm_encoder_helper_funcs sun4i_tv_helper_funcs = {
  384. .disable = sun4i_tv_disable,
  385. .enable = sun4i_tv_enable,
  386. .mode_set = sun4i_tv_mode_set,
  387. };
  388. static void sun4i_tv_destroy(struct drm_encoder *encoder)
  389. {
  390. drm_encoder_cleanup(encoder);
  391. }
  392. static struct drm_encoder_funcs sun4i_tv_funcs = {
  393. .destroy = sun4i_tv_destroy,
  394. };
  395. static int sun4i_tv_comp_get_modes(struct drm_connector *connector)
  396. {
  397. int i;
  398. for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
  399. struct drm_display_mode *mode;
  400. const struct tv_mode *tv_mode = &tv_modes[i];
  401. mode = drm_mode_create(connector->dev);
  402. if (!mode) {
  403. DRM_ERROR("Failed to create a new display mode\n");
  404. return 0;
  405. }
  406. strcpy(mode->name, tv_mode->name);
  407. sun4i_tv_mode_to_drm_mode(tv_mode, mode);
  408. drm_mode_probed_add(connector, mode);
  409. }
  410. return i;
  411. }
  412. static int sun4i_tv_comp_mode_valid(struct drm_connector *connector,
  413. struct drm_display_mode *mode)
  414. {
  415. /* TODO */
  416. return MODE_OK;
  417. }
  418. static struct drm_connector_helper_funcs sun4i_tv_comp_connector_helper_funcs = {
  419. .get_modes = sun4i_tv_comp_get_modes,
  420. .mode_valid = sun4i_tv_comp_mode_valid,
  421. };
  422. static void
  423. sun4i_tv_comp_connector_destroy(struct drm_connector *connector)
  424. {
  425. drm_connector_cleanup(connector);
  426. }
  427. static const struct drm_connector_funcs sun4i_tv_comp_connector_funcs = {
  428. .fill_modes = drm_helper_probe_single_connector_modes,
  429. .destroy = sun4i_tv_comp_connector_destroy,
  430. .reset = drm_atomic_helper_connector_reset,
  431. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  432. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  433. };
  434. static struct regmap_config sun4i_tv_regmap_config = {
  435. .reg_bits = 32,
  436. .val_bits = 32,
  437. .reg_stride = 4,
  438. .max_register = SUN4I_TVE_WSS_DATA2_REG,
  439. .name = "tv-encoder",
  440. };
  441. static int sun4i_tv_bind(struct device *dev, struct device *master,
  442. void *data)
  443. {
  444. struct platform_device *pdev = to_platform_device(dev);
  445. struct drm_device *drm = data;
  446. struct sun4i_drv *drv = drm->dev_private;
  447. struct sun4i_tv *tv;
  448. struct resource *res;
  449. void __iomem *regs;
  450. int ret;
  451. tv = devm_kzalloc(dev, sizeof(*tv), GFP_KERNEL);
  452. if (!tv)
  453. return -ENOMEM;
  454. tv->drv = drv;
  455. dev_set_drvdata(dev, tv);
  456. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  457. regs = devm_ioremap_resource(dev, res);
  458. if (IS_ERR(regs)) {
  459. dev_err(dev, "Couldn't map the TV encoder registers\n");
  460. return PTR_ERR(regs);
  461. }
  462. tv->regs = devm_regmap_init_mmio(dev, regs,
  463. &sun4i_tv_regmap_config);
  464. if (IS_ERR(tv->regs)) {
  465. dev_err(dev, "Couldn't create the TV encoder regmap\n");
  466. return PTR_ERR(tv->regs);
  467. }
  468. tv->reset = devm_reset_control_get(dev, NULL);
  469. if (IS_ERR(tv->reset)) {
  470. dev_err(dev, "Couldn't get our reset line\n");
  471. return PTR_ERR(tv->reset);
  472. }
  473. ret = reset_control_deassert(tv->reset);
  474. if (ret) {
  475. dev_err(dev, "Couldn't deassert our reset line\n");
  476. return ret;
  477. }
  478. tv->clk = devm_clk_get(dev, NULL);
  479. if (IS_ERR(tv->clk)) {
  480. dev_err(dev, "Couldn't get the TV encoder clock\n");
  481. ret = PTR_ERR(tv->clk);
  482. goto err_assert_reset;
  483. }
  484. clk_prepare_enable(tv->clk);
  485. drm_encoder_helper_add(&tv->encoder,
  486. &sun4i_tv_helper_funcs);
  487. ret = drm_encoder_init(drm,
  488. &tv->encoder,
  489. &sun4i_tv_funcs,
  490. DRM_MODE_ENCODER_TVDAC,
  491. NULL);
  492. if (ret) {
  493. dev_err(dev, "Couldn't initialise the TV encoder\n");
  494. goto err_disable_clk;
  495. }
  496. tv->encoder.possible_crtcs = drm_of_find_possible_crtcs(drm,
  497. dev->of_node);
  498. if (!tv->encoder.possible_crtcs) {
  499. ret = -EPROBE_DEFER;
  500. goto err_disable_clk;
  501. }
  502. drm_connector_helper_add(&tv->connector,
  503. &sun4i_tv_comp_connector_helper_funcs);
  504. ret = drm_connector_init(drm, &tv->connector,
  505. &sun4i_tv_comp_connector_funcs,
  506. DRM_MODE_CONNECTOR_Composite);
  507. if (ret) {
  508. dev_err(dev,
  509. "Couldn't initialise the Composite connector\n");
  510. goto err_cleanup_connector;
  511. }
  512. tv->connector.interlace_allowed = true;
  513. drm_mode_connector_attach_encoder(&tv->connector, &tv->encoder);
  514. return 0;
  515. err_cleanup_connector:
  516. drm_encoder_cleanup(&tv->encoder);
  517. err_disable_clk:
  518. clk_disable_unprepare(tv->clk);
  519. err_assert_reset:
  520. reset_control_assert(tv->reset);
  521. return ret;
  522. }
  523. static void sun4i_tv_unbind(struct device *dev, struct device *master,
  524. void *data)
  525. {
  526. struct sun4i_tv *tv = dev_get_drvdata(dev);
  527. drm_connector_cleanup(&tv->connector);
  528. drm_encoder_cleanup(&tv->encoder);
  529. clk_disable_unprepare(tv->clk);
  530. }
  531. static const struct component_ops sun4i_tv_ops = {
  532. .bind = sun4i_tv_bind,
  533. .unbind = sun4i_tv_unbind,
  534. };
  535. static int sun4i_tv_probe(struct platform_device *pdev)
  536. {
  537. return component_add(&pdev->dev, &sun4i_tv_ops);
  538. }
  539. static int sun4i_tv_remove(struct platform_device *pdev)
  540. {
  541. component_del(&pdev->dev, &sun4i_tv_ops);
  542. return 0;
  543. }
  544. static const struct of_device_id sun4i_tv_of_table[] = {
  545. { .compatible = "allwinner,sun4i-a10-tv-encoder" },
  546. { }
  547. };
  548. MODULE_DEVICE_TABLE(of, sun4i_tv_of_table);
  549. static struct platform_driver sun4i_tv_platform_driver = {
  550. .probe = sun4i_tv_probe,
  551. .remove = sun4i_tv_remove,
  552. .driver = {
  553. .name = "sun4i-tve",
  554. .of_match_table = sun4i_tv_of_table,
  555. },
  556. };
  557. module_platform_driver(sun4i_tv_platform_driver);
  558. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  559. MODULE_DESCRIPTION("Allwinner A10 TV Encoder Driver");
  560. MODULE_LICENSE("GPL");