sun4i_tv.c 18 KB

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