sti_hda.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Author: Fabien Dessenne <fabien.dessenne@st.com> for STMicroelectronics.
  4. * License terms: GNU General Public License (GPL), version 2
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/component.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <drm/drmP.h>
  11. #include <drm/drm_atomic_helper.h>
  12. #include <drm/drm_crtc_helper.h>
  13. /* HDformatter registers */
  14. #define HDA_ANA_CFG 0x0000
  15. #define HDA_ANA_SCALE_CTRL_Y 0x0004
  16. #define HDA_ANA_SCALE_CTRL_CB 0x0008
  17. #define HDA_ANA_SCALE_CTRL_CR 0x000C
  18. #define HDA_ANA_ANC_CTRL 0x0010
  19. #define HDA_ANA_SRC_Y_CFG 0x0014
  20. #define HDA_COEFF_Y_PH1_TAP123 0x0018
  21. #define HDA_COEFF_Y_PH1_TAP456 0x001C
  22. #define HDA_COEFF_Y_PH2_TAP123 0x0020
  23. #define HDA_COEFF_Y_PH2_TAP456 0x0024
  24. #define HDA_COEFF_Y_PH3_TAP123 0x0028
  25. #define HDA_COEFF_Y_PH3_TAP456 0x002C
  26. #define HDA_COEFF_Y_PH4_TAP123 0x0030
  27. #define HDA_COEFF_Y_PH4_TAP456 0x0034
  28. #define HDA_ANA_SRC_C_CFG 0x0040
  29. #define HDA_COEFF_C_PH1_TAP123 0x0044
  30. #define HDA_COEFF_C_PH1_TAP456 0x0048
  31. #define HDA_COEFF_C_PH2_TAP123 0x004C
  32. #define HDA_COEFF_C_PH2_TAP456 0x0050
  33. #define HDA_COEFF_C_PH3_TAP123 0x0054
  34. #define HDA_COEFF_C_PH3_TAP456 0x0058
  35. #define HDA_COEFF_C_PH4_TAP123 0x005C
  36. #define HDA_COEFF_C_PH4_TAP456 0x0060
  37. #define HDA_SYNC_AWGI 0x0300
  38. /* HDA_ANA_CFG */
  39. #define CFG_AWG_ASYNC_EN BIT(0)
  40. #define CFG_AWG_ASYNC_HSYNC_MTD BIT(1)
  41. #define CFG_AWG_ASYNC_VSYNC_MTD BIT(2)
  42. #define CFG_AWG_SYNC_DEL BIT(3)
  43. #define CFG_AWG_FLTR_MODE_SHIFT 4
  44. #define CFG_AWG_FLTR_MODE_MASK (0xF << CFG_AWG_FLTR_MODE_SHIFT)
  45. #define CFG_AWG_FLTR_MODE_SD (0 << CFG_AWG_FLTR_MODE_SHIFT)
  46. #define CFG_AWG_FLTR_MODE_ED (1 << CFG_AWG_FLTR_MODE_SHIFT)
  47. #define CFG_AWG_FLTR_MODE_HD (2 << CFG_AWG_FLTR_MODE_SHIFT)
  48. #define CFG_SYNC_ON_PBPR_MASK BIT(8)
  49. #define CFG_PREFILTER_EN_MASK BIT(9)
  50. #define CFG_PBPR_SYNC_OFF_SHIFT 16
  51. #define CFG_PBPR_SYNC_OFF_MASK (0x7FF << CFG_PBPR_SYNC_OFF_SHIFT)
  52. #define CFG_PBPR_SYNC_OFF_VAL 0x117 /* Voltage dependent. stiH416 */
  53. /* Default scaling values */
  54. #define SCALE_CTRL_Y_DFLT 0x00C50256
  55. #define SCALE_CTRL_CB_DFLT 0x00DB0249
  56. #define SCALE_CTRL_CR_DFLT 0x00DB0249
  57. /* Video DACs control */
  58. #define VIDEO_DACS_CONTROL_MASK 0x0FFF
  59. #define VIDEO_DACS_CONTROL_SYSCFG2535 0x085C /* for stih416 */
  60. #define DAC_CFG_HD_OFF_SHIFT 5
  61. #define DAC_CFG_HD_OFF_MASK (0x7 << DAC_CFG_HD_OFF_SHIFT)
  62. #define VIDEO_DACS_CONTROL_SYSCFG5072 0x0120 /* for stih407 */
  63. #define DAC_CFG_HD_HZUVW_OFF_MASK BIT(1)
  64. /* Upsampler values for the alternative 2X Filter */
  65. #define SAMPLER_COEF_NB 8
  66. #define HDA_ANA_SRC_Y_CFG_ALT_2X 0x01130000
  67. static u32 coef_y_alt_2x[] = {
  68. 0x00FE83FB, 0x1F900401, 0x00000000, 0x00000000,
  69. 0x00F408F9, 0x055F7C25, 0x00000000, 0x00000000
  70. };
  71. #define HDA_ANA_SRC_C_CFG_ALT_2X 0x01750004
  72. static u32 coef_c_alt_2x[] = {
  73. 0x001305F7, 0x05274BD0, 0x00000000, 0x00000000,
  74. 0x0004907C, 0x09C80B9D, 0x00000000, 0x00000000
  75. };
  76. /* Upsampler values for the 4X Filter */
  77. #define HDA_ANA_SRC_Y_CFG_4X 0x01ED0005
  78. #define HDA_ANA_SRC_C_CFG_4X 0x01ED0004
  79. static u32 coef_yc_4x[] = {
  80. 0x00FC827F, 0x008FE20B, 0x00F684FC, 0x050F7C24,
  81. 0x00F4857C, 0x0A1F402E, 0x00FA027F, 0x0E076E1D
  82. };
  83. /* AWG instructions for some video modes */
  84. #define AWG_MAX_INST 64
  85. /* 720p@50 */
  86. static u32 AWGi_720p_50[] = {
  87. 0x00000971, 0x00000C26, 0x0000013B, 0x00000CDA,
  88. 0x00000104, 0x00000E7E, 0x00000E7F, 0x0000013B,
  89. 0x00000D8E, 0x00000104, 0x00001804, 0x00000971,
  90. 0x00000C26, 0x0000003B, 0x00000FB4, 0x00000FB5,
  91. 0x00000104, 0x00001AE8
  92. };
  93. #define NN_720p_50 ARRAY_SIZE(AWGi_720p_50)
  94. /* 720p@60 */
  95. static u32 AWGi_720p_60[] = {
  96. 0x00000971, 0x00000C26, 0x0000013B, 0x00000CDA,
  97. 0x00000104, 0x00000E7E, 0x00000E7F, 0x0000013B,
  98. 0x00000C44, 0x00000104, 0x00001804, 0x00000971,
  99. 0x00000C26, 0x0000003B, 0x00000F0F, 0x00000F10,
  100. 0x00000104, 0x00001AE8
  101. };
  102. #define NN_720p_60 ARRAY_SIZE(AWGi_720p_60)
  103. /* 1080p@30 */
  104. static u32 AWGi_1080p_30[] = {
  105. 0x00000971, 0x00000C2A, 0x0000013B, 0x00000C56,
  106. 0x00000104, 0x00000FDC, 0x00000FDD, 0x0000013B,
  107. 0x00000C2A, 0x00000104, 0x00001804, 0x00000971,
  108. 0x00000C2A, 0x0000003B, 0x00000EBE, 0x00000EBF,
  109. 0x00000EBF, 0x00000104, 0x00001A2F, 0x00001C4B,
  110. 0x00001C52
  111. };
  112. #define NN_1080p_30 ARRAY_SIZE(AWGi_1080p_30)
  113. /* 1080p@25 */
  114. static u32 AWGi_1080p_25[] = {
  115. 0x00000971, 0x00000C2A, 0x0000013B, 0x00000C56,
  116. 0x00000104, 0x00000FDC, 0x00000FDD, 0x0000013B,
  117. 0x00000DE2, 0x00000104, 0x00001804, 0x00000971,
  118. 0x00000C2A, 0x0000003B, 0x00000F51, 0x00000F51,
  119. 0x00000F52, 0x00000104, 0x00001A2F, 0x00001C4B,
  120. 0x00001C52
  121. };
  122. #define NN_1080p_25 ARRAY_SIZE(AWGi_1080p_25)
  123. /* 1080p@24 */
  124. static u32 AWGi_1080p_24[] = {
  125. 0x00000971, 0x00000C2A, 0x0000013B, 0x00000C56,
  126. 0x00000104, 0x00000FDC, 0x00000FDD, 0x0000013B,
  127. 0x00000E50, 0x00000104, 0x00001804, 0x00000971,
  128. 0x00000C2A, 0x0000003B, 0x00000F76, 0x00000F76,
  129. 0x00000F76, 0x00000104, 0x00001A2F, 0x00001C4B,
  130. 0x00001C52
  131. };
  132. #define NN_1080p_24 ARRAY_SIZE(AWGi_1080p_24)
  133. /* 720x480p@60 */
  134. static u32 AWGi_720x480p_60[] = {
  135. 0x00000904, 0x00000F18, 0x0000013B, 0x00001805,
  136. 0x00000904, 0x00000C3D, 0x0000003B, 0x00001A06
  137. };
  138. #define NN_720x480p_60 ARRAY_SIZE(AWGi_720x480p_60)
  139. /* Video mode category */
  140. enum sti_hda_vid_cat {
  141. VID_SD,
  142. VID_ED,
  143. VID_HD_74M,
  144. VID_HD_148M
  145. };
  146. struct sti_hda_video_config {
  147. struct drm_display_mode mode;
  148. u32 *awg_instr;
  149. int nb_instr;
  150. enum sti_hda_vid_cat vid_cat;
  151. };
  152. /* HD analog supported modes
  153. * Interlaced modes may be added when supported by the whole display chain
  154. */
  155. static const struct sti_hda_video_config hda_supported_modes[] = {
  156. /* 1080p30 74.250Mhz */
  157. {{DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2008,
  158. 2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
  159. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC)},
  160. AWGi_1080p_30, NN_1080p_30, VID_HD_74M},
  161. /* 1080p30 74.176Mhz */
  162. {{DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74176, 1920, 2008,
  163. 2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
  164. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC)},
  165. AWGi_1080p_30, NN_1080p_30, VID_HD_74M},
  166. /* 1080p24 74.250Mhz */
  167. {{DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2558,
  168. 2602, 2750, 0, 1080, 1084, 1089, 1125, 0,
  169. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC)},
  170. AWGi_1080p_24, NN_1080p_24, VID_HD_74M},
  171. /* 1080p24 74.176Mhz */
  172. {{DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74176, 1920, 2558,
  173. 2602, 2750, 0, 1080, 1084, 1089, 1125, 0,
  174. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC)},
  175. AWGi_1080p_24, NN_1080p_24, VID_HD_74M},
  176. /* 1080p25 74.250Mhz */
  177. {{DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2448,
  178. 2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
  179. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC)},
  180. AWGi_1080p_25, NN_1080p_25, VID_HD_74M},
  181. /* 720p60 74.250Mhz */
  182. {{DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1390,
  183. 1430, 1650, 0, 720, 725, 730, 750, 0,
  184. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC)},
  185. AWGi_720p_60, NN_720p_60, VID_HD_74M},
  186. /* 720p60 74.176Mhz */
  187. {{DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74176, 1280, 1390,
  188. 1430, 1650, 0, 720, 725, 730, 750, 0,
  189. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC)},
  190. AWGi_720p_60, NN_720p_60, VID_HD_74M},
  191. /* 720p50 74.250Mhz */
  192. {{DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1720,
  193. 1760, 1980, 0, 720, 725, 730, 750, 0,
  194. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC)},
  195. AWGi_720p_50, NN_720p_50, VID_HD_74M},
  196. /* 720x480p60 27.027Mhz */
  197. {{DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27027, 720, 736,
  198. 798, 858, 0, 480, 489, 495, 525, 0,
  199. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC)},
  200. AWGi_720x480p_60, NN_720x480p_60, VID_ED},
  201. /* 720x480p60 27.000Mhz */
  202. {{DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27000, 720, 736,
  203. 798, 858, 0, 480, 489, 495, 525, 0,
  204. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC)},
  205. AWGi_720x480p_60, NN_720x480p_60, VID_ED}
  206. };
  207. /**
  208. * STI hd analog structure
  209. *
  210. * @dev: driver device
  211. * @drm_dev: pointer to drm device
  212. * @mode: current display mode selected
  213. * @regs: HD analog register
  214. * @video_dacs_ctrl: video DACS control register
  215. * @enabled: true if HD analog is enabled else false
  216. */
  217. struct sti_hda {
  218. struct device dev;
  219. struct drm_device *drm_dev;
  220. struct drm_display_mode mode;
  221. void __iomem *regs;
  222. void __iomem *video_dacs_ctrl;
  223. struct clk *clk_pix;
  224. struct clk *clk_hddac;
  225. bool enabled;
  226. };
  227. struct sti_hda_connector {
  228. struct drm_connector drm_connector;
  229. struct drm_encoder *encoder;
  230. struct sti_hda *hda;
  231. };
  232. #define to_sti_hda_connector(x) \
  233. container_of(x, struct sti_hda_connector, drm_connector)
  234. static u32 hda_read(struct sti_hda *hda, int offset)
  235. {
  236. return readl(hda->regs + offset);
  237. }
  238. static void hda_write(struct sti_hda *hda, u32 val, int offset)
  239. {
  240. writel(val, hda->regs + offset);
  241. }
  242. /**
  243. * Search for a video mode in the supported modes table
  244. *
  245. * @mode: mode being searched
  246. * @idx: index of the found mode
  247. *
  248. * Return true if mode is found
  249. */
  250. static bool hda_get_mode_idx(struct drm_display_mode mode, int *idx)
  251. {
  252. unsigned int i;
  253. for (i = 0; i < ARRAY_SIZE(hda_supported_modes); i++)
  254. if (drm_mode_equal(&hda_supported_modes[i].mode, &mode)) {
  255. *idx = i;
  256. return true;
  257. }
  258. return false;
  259. }
  260. /**
  261. * Enable the HD DACS
  262. *
  263. * @hda: pointer to HD analog structure
  264. * @enable: true if HD DACS need to be enabled, else false
  265. */
  266. static void hda_enable_hd_dacs(struct sti_hda *hda, bool enable)
  267. {
  268. u32 mask;
  269. if (hda->video_dacs_ctrl) {
  270. u32 val;
  271. switch ((u32)hda->video_dacs_ctrl & VIDEO_DACS_CONTROL_MASK) {
  272. case VIDEO_DACS_CONTROL_SYSCFG2535:
  273. mask = DAC_CFG_HD_OFF_MASK;
  274. break;
  275. case VIDEO_DACS_CONTROL_SYSCFG5072:
  276. mask = DAC_CFG_HD_HZUVW_OFF_MASK;
  277. break;
  278. default:
  279. DRM_INFO("Video DACS control register not supported!");
  280. return;
  281. }
  282. val = readl(hda->video_dacs_ctrl);
  283. if (enable)
  284. val &= ~mask;
  285. else
  286. val |= mask;
  287. writel(val, hda->video_dacs_ctrl);
  288. }
  289. }
  290. /**
  291. * Configure AWG, writing instructions
  292. *
  293. * @hda: pointer to HD analog structure
  294. * @awg_instr: pointer to AWG instructions table
  295. * @nb: nb of AWG instructions
  296. */
  297. static void sti_hda_configure_awg(struct sti_hda *hda, u32 *awg_instr, int nb)
  298. {
  299. unsigned int i;
  300. DRM_DEBUG_DRIVER("\n");
  301. for (i = 0; i < nb; i++)
  302. hda_write(hda, awg_instr[i], HDA_SYNC_AWGI + i * 4);
  303. for (i = nb; i < AWG_MAX_INST; i++)
  304. hda_write(hda, 0, HDA_SYNC_AWGI + i * 4);
  305. }
  306. static void sti_hda_disable(struct drm_bridge *bridge)
  307. {
  308. struct sti_hda *hda = bridge->driver_private;
  309. u32 val;
  310. if (!hda->enabled)
  311. return;
  312. DRM_DEBUG_DRIVER("\n");
  313. /* Disable HD DAC and AWG */
  314. val = hda_read(hda, HDA_ANA_CFG);
  315. val &= ~CFG_AWG_ASYNC_EN;
  316. hda_write(hda, val, HDA_ANA_CFG);
  317. hda_write(hda, 0, HDA_ANA_ANC_CTRL);
  318. hda_enable_hd_dacs(hda, false);
  319. /* Disable/unprepare hda clock */
  320. clk_disable_unprepare(hda->clk_hddac);
  321. clk_disable_unprepare(hda->clk_pix);
  322. hda->enabled = false;
  323. }
  324. static void sti_hda_pre_enable(struct drm_bridge *bridge)
  325. {
  326. struct sti_hda *hda = bridge->driver_private;
  327. u32 val, i, mode_idx;
  328. u32 src_filter_y, src_filter_c;
  329. u32 *coef_y, *coef_c;
  330. u32 filter_mode;
  331. DRM_DEBUG_DRIVER("\n");
  332. if (hda->enabled)
  333. return;
  334. /* Prepare/enable clocks */
  335. if (clk_prepare_enable(hda->clk_pix))
  336. DRM_ERROR("Failed to prepare/enable hda_pix clk\n");
  337. if (clk_prepare_enable(hda->clk_hddac))
  338. DRM_ERROR("Failed to prepare/enable hda_hddac clk\n");
  339. if (!hda_get_mode_idx(hda->mode, &mode_idx)) {
  340. DRM_ERROR("Undefined mode\n");
  341. return;
  342. }
  343. switch (hda_supported_modes[mode_idx].vid_cat) {
  344. case VID_HD_148M:
  345. DRM_ERROR("Beyond HD analog capabilities\n");
  346. return;
  347. case VID_HD_74M:
  348. /* HD use alternate 2x filter */
  349. filter_mode = CFG_AWG_FLTR_MODE_HD;
  350. src_filter_y = HDA_ANA_SRC_Y_CFG_ALT_2X;
  351. src_filter_c = HDA_ANA_SRC_C_CFG_ALT_2X;
  352. coef_y = coef_y_alt_2x;
  353. coef_c = coef_c_alt_2x;
  354. break;
  355. case VID_ED:
  356. /* ED uses 4x filter */
  357. filter_mode = CFG_AWG_FLTR_MODE_ED;
  358. src_filter_y = HDA_ANA_SRC_Y_CFG_4X;
  359. src_filter_c = HDA_ANA_SRC_C_CFG_4X;
  360. coef_y = coef_yc_4x;
  361. coef_c = coef_yc_4x;
  362. break;
  363. case VID_SD:
  364. DRM_ERROR("Not supported\n");
  365. return;
  366. default:
  367. DRM_ERROR("Undefined resolution\n");
  368. return;
  369. }
  370. DRM_DEBUG_DRIVER("Using HDA mode #%d\n", mode_idx);
  371. /* Enable HD Video DACs */
  372. hda_enable_hd_dacs(hda, true);
  373. /* Configure scaler */
  374. hda_write(hda, SCALE_CTRL_Y_DFLT, HDA_ANA_SCALE_CTRL_Y);
  375. hda_write(hda, SCALE_CTRL_CB_DFLT, HDA_ANA_SCALE_CTRL_CB);
  376. hda_write(hda, SCALE_CTRL_CR_DFLT, HDA_ANA_SCALE_CTRL_CR);
  377. /* Configure sampler */
  378. hda_write(hda , src_filter_y, HDA_ANA_SRC_Y_CFG);
  379. hda_write(hda, src_filter_c, HDA_ANA_SRC_C_CFG);
  380. for (i = 0; i < SAMPLER_COEF_NB; i++) {
  381. hda_write(hda, coef_y[i], HDA_COEFF_Y_PH1_TAP123 + i * 4);
  382. hda_write(hda, coef_c[i], HDA_COEFF_C_PH1_TAP123 + i * 4);
  383. }
  384. /* Configure main HDFormatter */
  385. val = 0;
  386. val |= (hda->mode.flags & DRM_MODE_FLAG_INTERLACE) ?
  387. 0 : CFG_AWG_ASYNC_VSYNC_MTD;
  388. val |= (CFG_PBPR_SYNC_OFF_VAL << CFG_PBPR_SYNC_OFF_SHIFT);
  389. val |= filter_mode;
  390. hda_write(hda, val, HDA_ANA_CFG);
  391. /* Configure AWG */
  392. sti_hda_configure_awg(hda, hda_supported_modes[mode_idx].awg_instr,
  393. hda_supported_modes[mode_idx].nb_instr);
  394. /* Enable AWG */
  395. val = hda_read(hda, HDA_ANA_CFG);
  396. val |= CFG_AWG_ASYNC_EN;
  397. hda_write(hda, val, HDA_ANA_CFG);
  398. hda->enabled = true;
  399. }
  400. static void sti_hda_set_mode(struct drm_bridge *bridge,
  401. struct drm_display_mode *mode,
  402. struct drm_display_mode *adjusted_mode)
  403. {
  404. struct sti_hda *hda = bridge->driver_private;
  405. u32 mode_idx;
  406. int hddac_rate;
  407. int ret;
  408. DRM_DEBUG_DRIVER("\n");
  409. memcpy(&hda->mode, mode, sizeof(struct drm_display_mode));
  410. if (!hda_get_mode_idx(hda->mode, &mode_idx)) {
  411. DRM_ERROR("Undefined mode\n");
  412. return;
  413. }
  414. switch (hda_supported_modes[mode_idx].vid_cat) {
  415. case VID_HD_74M:
  416. /* HD use alternate 2x filter */
  417. hddac_rate = mode->clock * 1000 * 2;
  418. break;
  419. case VID_ED:
  420. /* ED uses 4x filter */
  421. hddac_rate = mode->clock * 1000 * 4;
  422. break;
  423. default:
  424. DRM_ERROR("Undefined mode\n");
  425. return;
  426. }
  427. /* HD DAC = 148.5Mhz or 108 Mhz */
  428. ret = clk_set_rate(hda->clk_hddac, hddac_rate);
  429. if (ret < 0)
  430. DRM_ERROR("Cannot set rate (%dHz) for hda_hddac clk\n",
  431. hddac_rate);
  432. /* HDformatter clock = compositor clock */
  433. ret = clk_set_rate(hda->clk_pix, mode->clock * 1000);
  434. if (ret < 0)
  435. DRM_ERROR("Cannot set rate (%dHz) for hda_pix clk\n",
  436. mode->clock * 1000);
  437. }
  438. static void sti_hda_bridge_nope(struct drm_bridge *bridge)
  439. {
  440. /* do nothing */
  441. }
  442. static const struct drm_bridge_funcs sti_hda_bridge_funcs = {
  443. .pre_enable = sti_hda_pre_enable,
  444. .enable = sti_hda_bridge_nope,
  445. .disable = sti_hda_disable,
  446. .post_disable = sti_hda_bridge_nope,
  447. .mode_set = sti_hda_set_mode,
  448. };
  449. static int sti_hda_connector_get_modes(struct drm_connector *connector)
  450. {
  451. unsigned int i;
  452. int count = 0;
  453. struct sti_hda_connector *hda_connector
  454. = to_sti_hda_connector(connector);
  455. struct sti_hda *hda = hda_connector->hda;
  456. DRM_DEBUG_DRIVER("\n");
  457. for (i = 0; i < ARRAY_SIZE(hda_supported_modes); i++) {
  458. struct drm_display_mode *mode =
  459. drm_mode_duplicate(hda->drm_dev,
  460. &hda_supported_modes[i].mode);
  461. if (!mode)
  462. continue;
  463. mode->vrefresh = drm_mode_vrefresh(mode);
  464. /* the first mode is the preferred mode */
  465. if (i == 0)
  466. mode->type |= DRM_MODE_TYPE_PREFERRED;
  467. drm_mode_probed_add(connector, mode);
  468. count++;
  469. }
  470. drm_mode_sort(&connector->modes);
  471. return count;
  472. }
  473. #define CLK_TOLERANCE_HZ 50
  474. static int sti_hda_connector_mode_valid(struct drm_connector *connector,
  475. struct drm_display_mode *mode)
  476. {
  477. int target = mode->clock * 1000;
  478. int target_min = target - CLK_TOLERANCE_HZ;
  479. int target_max = target + CLK_TOLERANCE_HZ;
  480. int result;
  481. int idx;
  482. struct sti_hda_connector *hda_connector
  483. = to_sti_hda_connector(connector);
  484. struct sti_hda *hda = hda_connector->hda;
  485. if (!hda_get_mode_idx(*mode, &idx)) {
  486. return MODE_BAD;
  487. } else {
  488. result = clk_round_rate(hda->clk_pix, target);
  489. DRM_DEBUG_DRIVER("target rate = %d => available rate = %d\n",
  490. target, result);
  491. if ((result < target_min) || (result > target_max)) {
  492. DRM_DEBUG_DRIVER("hda pixclk=%d not supported\n",
  493. target);
  494. return MODE_BAD;
  495. }
  496. }
  497. return MODE_OK;
  498. }
  499. struct drm_encoder *sti_hda_best_encoder(struct drm_connector *connector)
  500. {
  501. struct sti_hda_connector *hda_connector
  502. = to_sti_hda_connector(connector);
  503. /* Best encoder is the one associated during connector creation */
  504. return hda_connector->encoder;
  505. }
  506. static const
  507. struct drm_connector_helper_funcs sti_hda_connector_helper_funcs = {
  508. .get_modes = sti_hda_connector_get_modes,
  509. .mode_valid = sti_hda_connector_mode_valid,
  510. .best_encoder = sti_hda_best_encoder,
  511. };
  512. static enum drm_connector_status
  513. sti_hda_connector_detect(struct drm_connector *connector, bool force)
  514. {
  515. return connector_status_connected;
  516. }
  517. static void sti_hda_connector_destroy(struct drm_connector *connector)
  518. {
  519. struct sti_hda_connector *hda_connector
  520. = to_sti_hda_connector(connector);
  521. drm_connector_unregister(connector);
  522. drm_connector_cleanup(connector);
  523. kfree(hda_connector);
  524. }
  525. static const struct drm_connector_funcs sti_hda_connector_funcs = {
  526. .dpms = drm_atomic_helper_connector_dpms,
  527. .fill_modes = drm_helper_probe_single_connector_modes,
  528. .detect = sti_hda_connector_detect,
  529. .destroy = sti_hda_connector_destroy,
  530. .reset = drm_atomic_helper_connector_reset,
  531. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  532. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  533. };
  534. static struct drm_encoder *sti_hda_find_encoder(struct drm_device *dev)
  535. {
  536. struct drm_encoder *encoder;
  537. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  538. if (encoder->encoder_type == DRM_MODE_ENCODER_DAC)
  539. return encoder;
  540. }
  541. return NULL;
  542. }
  543. static int sti_hda_bind(struct device *dev, struct device *master, void *data)
  544. {
  545. struct sti_hda *hda = dev_get_drvdata(dev);
  546. struct drm_device *drm_dev = data;
  547. struct drm_encoder *encoder;
  548. struct sti_hda_connector *connector;
  549. struct drm_connector *drm_connector;
  550. struct drm_bridge *bridge;
  551. int err;
  552. /* Set the drm device handle */
  553. hda->drm_dev = drm_dev;
  554. encoder = sti_hda_find_encoder(drm_dev);
  555. if (!encoder)
  556. return -ENOMEM;
  557. connector = devm_kzalloc(dev, sizeof(*connector), GFP_KERNEL);
  558. if (!connector)
  559. return -ENOMEM;
  560. connector->hda = hda;
  561. bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
  562. if (!bridge)
  563. return -ENOMEM;
  564. bridge->driver_private = hda;
  565. bridge->funcs = &sti_hda_bridge_funcs;
  566. drm_bridge_attach(drm_dev, bridge);
  567. encoder->bridge = bridge;
  568. connector->encoder = encoder;
  569. drm_connector = (struct drm_connector *)connector;
  570. drm_connector->polled = DRM_CONNECTOR_POLL_HPD;
  571. drm_connector_init(drm_dev, drm_connector,
  572. &sti_hda_connector_funcs, DRM_MODE_CONNECTOR_Component);
  573. drm_connector_helper_add(drm_connector,
  574. &sti_hda_connector_helper_funcs);
  575. err = drm_connector_register(drm_connector);
  576. if (err)
  577. goto err_connector;
  578. err = drm_mode_connector_attach_encoder(drm_connector, encoder);
  579. if (err) {
  580. DRM_ERROR("Failed to attach a connector to a encoder\n");
  581. goto err_sysfs;
  582. }
  583. return 0;
  584. err_sysfs:
  585. drm_connector_unregister(drm_connector);
  586. err_connector:
  587. drm_connector_cleanup(drm_connector);
  588. return -EINVAL;
  589. }
  590. static void sti_hda_unbind(struct device *dev,
  591. struct device *master, void *data)
  592. {
  593. /* do nothing */
  594. }
  595. static const struct component_ops sti_hda_ops = {
  596. .bind = sti_hda_bind,
  597. .unbind = sti_hda_unbind,
  598. };
  599. static int sti_hda_probe(struct platform_device *pdev)
  600. {
  601. struct device *dev = &pdev->dev;
  602. struct sti_hda *hda;
  603. struct resource *res;
  604. DRM_INFO("%s\n", __func__);
  605. hda = devm_kzalloc(dev, sizeof(*hda), GFP_KERNEL);
  606. if (!hda)
  607. return -ENOMEM;
  608. hda->dev = pdev->dev;
  609. /* Get resources */
  610. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hda-reg");
  611. if (!res) {
  612. DRM_ERROR("Invalid hda resource\n");
  613. return -ENOMEM;
  614. }
  615. hda->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  616. if (!hda->regs)
  617. return -ENOMEM;
  618. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  619. "video-dacs-ctrl");
  620. if (res) {
  621. hda->video_dacs_ctrl = devm_ioremap_nocache(dev, res->start,
  622. resource_size(res));
  623. if (!hda->video_dacs_ctrl)
  624. return -ENOMEM;
  625. } else {
  626. /* If no existing video-dacs-ctrl resource continue the probe */
  627. DRM_DEBUG_DRIVER("No video-dacs-ctrl resource\n");
  628. hda->video_dacs_ctrl = NULL;
  629. }
  630. /* Get clock resources */
  631. hda->clk_pix = devm_clk_get(dev, "pix");
  632. if (IS_ERR(hda->clk_pix)) {
  633. DRM_ERROR("Cannot get hda_pix clock\n");
  634. return PTR_ERR(hda->clk_pix);
  635. }
  636. hda->clk_hddac = devm_clk_get(dev, "hddac");
  637. if (IS_ERR(hda->clk_hddac)) {
  638. DRM_ERROR("Cannot get hda_hddac clock\n");
  639. return PTR_ERR(hda->clk_hddac);
  640. }
  641. platform_set_drvdata(pdev, hda);
  642. return component_add(&pdev->dev, &sti_hda_ops);
  643. }
  644. static int sti_hda_remove(struct platform_device *pdev)
  645. {
  646. component_del(&pdev->dev, &sti_hda_ops);
  647. return 0;
  648. }
  649. static const struct of_device_id hda_of_match[] = {
  650. { .compatible = "st,stih416-hda", },
  651. { .compatible = "st,stih407-hda", },
  652. { /* end node */ }
  653. };
  654. MODULE_DEVICE_TABLE(of, hda_of_match);
  655. struct platform_driver sti_hda_driver = {
  656. .driver = {
  657. .name = "sti-hda",
  658. .owner = THIS_MODULE,
  659. .of_match_table = hda_of_match,
  660. },
  661. .probe = sti_hda_probe,
  662. .remove = sti_hda_remove,
  663. };
  664. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  665. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  666. MODULE_LICENSE("GPL");