sti_hda.c 24 KB

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