sti_hda.c 23 KB

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