sun4i_tv.c 18 KB

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