sti_hda.c 22 KB

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