sti_hda.c 24 KB

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