sti_hda.c 21 KB

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