hdmi_drv.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * Samsung HDMI interface driver
  3. *
  4. * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
  5. *
  6. * Tomasz Stanislawski, <t.stanislaws@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published
  10. * by the Free Software Foundiation. either version 2 of the License,
  11. * or (at your option) any later version
  12. */
  13. #ifdef CONFIG_VIDEO_SAMSUNG_S5P_HDMI_DEBUG
  14. #define DEBUG
  15. #endif
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/io.h>
  19. #include <linux/i2c.h>
  20. #include <linux/platform_device.h>
  21. #include <media/v4l2-subdev.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/delay.h>
  26. #include <linux/bug.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/clk.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <media/s5p_hdmi.h>
  31. #include <media/v4l2-common.h>
  32. #include <media/v4l2-dev.h>
  33. #include <media/v4l2-device.h>
  34. #include "regs-hdmi.h"
  35. MODULE_AUTHOR("Tomasz Stanislawski, <t.stanislaws@samsung.com>");
  36. MODULE_DESCRIPTION("Samsung HDMI");
  37. MODULE_LICENSE("GPL");
  38. /* default preset configured on probe */
  39. #define HDMI_DEFAULT_PRESET V4L2_DV_480P59_94
  40. struct hdmi_pulse {
  41. u32 beg;
  42. u32 end;
  43. };
  44. struct hdmi_timings {
  45. struct hdmi_pulse hact;
  46. u32 hsyn_pol; /* 0 - high, 1 - low */
  47. struct hdmi_pulse hsyn;
  48. u32 interlaced;
  49. struct hdmi_pulse vact[2];
  50. u32 vsyn_pol; /* 0 - high, 1 - low */
  51. u32 vsyn_off;
  52. struct hdmi_pulse vsyn[2];
  53. };
  54. struct hdmi_resources {
  55. struct clk *hdmi;
  56. struct clk *sclk_hdmi;
  57. struct clk *sclk_pixel;
  58. struct clk *sclk_hdmiphy;
  59. struct clk *hdmiphy;
  60. struct regulator_bulk_data *regul_bulk;
  61. int regul_count;
  62. };
  63. struct hdmi_device {
  64. /** base address of HDMI registers */
  65. void __iomem *regs;
  66. /** HDMI interrupt */
  67. unsigned int irq;
  68. /** pointer to device parent */
  69. struct device *dev;
  70. /** subdev generated by HDMI device */
  71. struct v4l2_subdev sd;
  72. /** V4L2 device structure */
  73. struct v4l2_device v4l2_dev;
  74. /** subdev of HDMIPHY interface */
  75. struct v4l2_subdev *phy_sd;
  76. /** subdev of MHL interface */
  77. struct v4l2_subdev *mhl_sd;
  78. /** configuration of current graphic mode */
  79. const struct hdmi_timings *cur_conf;
  80. /** current preset */
  81. u32 cur_preset;
  82. /** other resources */
  83. struct hdmi_resources res;
  84. };
  85. static struct platform_device_id hdmi_driver_types[] = {
  86. {
  87. .name = "s5pv210-hdmi",
  88. }, {
  89. .name = "exynos4-hdmi",
  90. }, {
  91. /* end node */
  92. }
  93. };
  94. static const struct v4l2_subdev_ops hdmi_sd_ops;
  95. static struct hdmi_device *sd_to_hdmi_dev(struct v4l2_subdev *sd)
  96. {
  97. return container_of(sd, struct hdmi_device, sd);
  98. }
  99. static inline
  100. void hdmi_write(struct hdmi_device *hdev, u32 reg_id, u32 value)
  101. {
  102. writel(value, hdev->regs + reg_id);
  103. }
  104. static inline
  105. void hdmi_write_mask(struct hdmi_device *hdev, u32 reg_id, u32 value, u32 mask)
  106. {
  107. u32 old = readl(hdev->regs + reg_id);
  108. value = (value & mask) | (old & ~mask);
  109. writel(value, hdev->regs + reg_id);
  110. }
  111. static inline
  112. void hdmi_writeb(struct hdmi_device *hdev, u32 reg_id, u8 value)
  113. {
  114. writeb(value, hdev->regs + reg_id);
  115. }
  116. static inline
  117. void hdmi_writebn(struct hdmi_device *hdev, u32 reg_id, int n, u32 value)
  118. {
  119. switch (n) {
  120. default:
  121. writeb(value >> 24, hdev->regs + reg_id + 12);
  122. case 3:
  123. writeb(value >> 16, hdev->regs + reg_id + 8);
  124. case 2:
  125. writeb(value >> 8, hdev->regs + reg_id + 4);
  126. case 1:
  127. writeb(value >> 0, hdev->regs + reg_id + 0);
  128. }
  129. }
  130. static inline u32 hdmi_read(struct hdmi_device *hdev, u32 reg_id)
  131. {
  132. return readl(hdev->regs + reg_id);
  133. }
  134. static irqreturn_t hdmi_irq_handler(int irq, void *dev_data)
  135. {
  136. struct hdmi_device *hdev = dev_data;
  137. u32 intc_flag;
  138. (void)irq;
  139. intc_flag = hdmi_read(hdev, HDMI_INTC_FLAG);
  140. /* clearing flags for HPD plug/unplug */
  141. if (intc_flag & HDMI_INTC_FLAG_HPD_UNPLUG) {
  142. printk(KERN_INFO "unplugged\n");
  143. hdmi_write_mask(hdev, HDMI_INTC_FLAG, ~0,
  144. HDMI_INTC_FLAG_HPD_UNPLUG);
  145. }
  146. if (intc_flag & HDMI_INTC_FLAG_HPD_PLUG) {
  147. printk(KERN_INFO "plugged\n");
  148. hdmi_write_mask(hdev, HDMI_INTC_FLAG, ~0,
  149. HDMI_INTC_FLAG_HPD_PLUG);
  150. }
  151. return IRQ_HANDLED;
  152. }
  153. static void hdmi_reg_init(struct hdmi_device *hdev)
  154. {
  155. /* enable HPD interrupts */
  156. hdmi_write_mask(hdev, HDMI_INTC_CON, ~0, HDMI_INTC_EN_GLOBAL |
  157. HDMI_INTC_EN_HPD_PLUG | HDMI_INTC_EN_HPD_UNPLUG);
  158. /* choose DVI mode */
  159. hdmi_write_mask(hdev, HDMI_MODE_SEL,
  160. HDMI_MODE_DVI_EN, HDMI_MODE_MASK);
  161. hdmi_write_mask(hdev, HDMI_CON_2, ~0,
  162. HDMI_DVI_PERAMBLE_EN | HDMI_DVI_BAND_EN);
  163. /* disable bluescreen */
  164. hdmi_write_mask(hdev, HDMI_CON_0, 0, HDMI_BLUE_SCR_EN);
  165. /* choose bluescreen (fecal) color */
  166. hdmi_writeb(hdev, HDMI_BLUE_SCREEN_0, 0x12);
  167. hdmi_writeb(hdev, HDMI_BLUE_SCREEN_1, 0x34);
  168. hdmi_writeb(hdev, HDMI_BLUE_SCREEN_2, 0x56);
  169. }
  170. static void hdmi_timing_apply(struct hdmi_device *hdev,
  171. const struct hdmi_timings *t)
  172. {
  173. /* setting core registers */
  174. hdmi_writebn(hdev, HDMI_H_BLANK_0, 2, t->hact.beg);
  175. hdmi_writebn(hdev, HDMI_H_SYNC_GEN_0, 3,
  176. (t->hsyn_pol << 20) | (t->hsyn.end << 10) | t->hsyn.beg);
  177. hdmi_writeb(hdev, HDMI_VSYNC_POL, t->vsyn_pol);
  178. hdmi_writebn(hdev, HDMI_V_BLANK_0, 3,
  179. (t->vact[0].beg << 11) | t->vact[0].end);
  180. hdmi_writebn(hdev, HDMI_V_SYNC_GEN_1_0, 3,
  181. (t->vsyn[0].beg << 12) | t->vsyn[0].end);
  182. if (t->interlaced) {
  183. u32 vsyn_trans = t->hsyn.beg + t->vsyn_off;
  184. hdmi_writeb(hdev, HDMI_INT_PRO_MODE, 1);
  185. hdmi_writebn(hdev, HDMI_H_V_LINE_0, 3,
  186. (t->hact.end << 12) | t->vact[1].end);
  187. hdmi_writebn(hdev, HDMI_V_BLANK_F_0, 3,
  188. (t->vact[1].end << 11) | t->vact[1].beg);
  189. hdmi_writebn(hdev, HDMI_V_SYNC_GEN_2_0, 3,
  190. (t->vsyn[1].beg << 12) | t->vsyn[1].end);
  191. hdmi_writebn(hdev, HDMI_V_SYNC_GEN_3_0, 3,
  192. (vsyn_trans << 12) | vsyn_trans);
  193. } else {
  194. hdmi_writeb(hdev, HDMI_INT_PRO_MODE, 0);
  195. hdmi_writebn(hdev, HDMI_H_V_LINE_0, 3,
  196. (t->hact.end << 12) | t->vact[0].end);
  197. }
  198. /* Timing generator registers */
  199. hdmi_writebn(hdev, HDMI_TG_H_FSZ_L, 2, t->hact.end);
  200. hdmi_writebn(hdev, HDMI_TG_HACT_ST_L, 2, t->hact.beg);
  201. hdmi_writebn(hdev, HDMI_TG_HACT_SZ_L, 2, t->hact.end - t->hact.beg);
  202. hdmi_writebn(hdev, HDMI_TG_VSYNC_L, 2, t->vsyn[0].beg);
  203. hdmi_writebn(hdev, HDMI_TG_VACT_ST_L, 2, t->vact[0].beg);
  204. hdmi_writebn(hdev, HDMI_TG_VACT_SZ_L, 2,
  205. t->vact[0].end - t->vact[0].beg);
  206. hdmi_writebn(hdev, HDMI_TG_VSYNC_TOP_HDMI_L, 2, t->vsyn[0].beg);
  207. hdmi_writebn(hdev, HDMI_TG_FIELD_TOP_HDMI_L, 2, t->vsyn[0].beg);
  208. if (t->interlaced) {
  209. hdmi_write_mask(hdev, HDMI_TG_CMD, ~0, HDMI_TG_FIELD_EN);
  210. hdmi_writebn(hdev, HDMI_TG_V_FSZ_L, 2, t->vact[1].end);
  211. hdmi_writebn(hdev, HDMI_TG_VSYNC2_L, 2, t->vsyn[1].beg);
  212. hdmi_writebn(hdev, HDMI_TG_FIELD_CHG_L, 2, t->vact[0].end);
  213. hdmi_writebn(hdev, HDMI_TG_VACT_ST2_L, 2, t->vact[1].beg);
  214. hdmi_writebn(hdev, HDMI_TG_VSYNC_BOT_HDMI_L, 2, t->vsyn[1].beg);
  215. hdmi_writebn(hdev, HDMI_TG_FIELD_BOT_HDMI_L, 2, t->vsyn[1].beg);
  216. } else {
  217. hdmi_write_mask(hdev, HDMI_TG_CMD, 0, HDMI_TG_FIELD_EN);
  218. hdmi_writebn(hdev, HDMI_TG_V_FSZ_L, 2, t->vact[0].end);
  219. }
  220. }
  221. static int hdmi_conf_apply(struct hdmi_device *hdmi_dev)
  222. {
  223. struct device *dev = hdmi_dev->dev;
  224. const struct hdmi_timings *conf = hdmi_dev->cur_conf;
  225. struct v4l2_dv_preset preset;
  226. int ret;
  227. dev_dbg(dev, "%s\n", __func__);
  228. /* reset hdmiphy */
  229. hdmi_write_mask(hdmi_dev, HDMI_PHY_RSTOUT, ~0, HDMI_PHY_SW_RSTOUT);
  230. mdelay(10);
  231. hdmi_write_mask(hdmi_dev, HDMI_PHY_RSTOUT, 0, HDMI_PHY_SW_RSTOUT);
  232. mdelay(10);
  233. /* configure presets */
  234. preset.preset = hdmi_dev->cur_preset;
  235. ret = v4l2_subdev_call(hdmi_dev->phy_sd, video, s_dv_preset, &preset);
  236. if (ret) {
  237. dev_err(dev, "failed to set preset (%u)\n", preset.preset);
  238. return ret;
  239. }
  240. /* resetting HDMI core */
  241. hdmi_write_mask(hdmi_dev, HDMI_CORE_RSTOUT, 0, HDMI_CORE_SW_RSTOUT);
  242. mdelay(10);
  243. hdmi_write_mask(hdmi_dev, HDMI_CORE_RSTOUT, ~0, HDMI_CORE_SW_RSTOUT);
  244. mdelay(10);
  245. hdmi_reg_init(hdmi_dev);
  246. /* setting core registers */
  247. hdmi_timing_apply(hdmi_dev, conf);
  248. return 0;
  249. }
  250. static void hdmi_dumpregs(struct hdmi_device *hdev, char *prefix)
  251. {
  252. #define DUMPREG(reg_id) \
  253. dev_dbg(hdev->dev, "%s:" #reg_id " = %08x\n", prefix, \
  254. readl(hdev->regs + reg_id))
  255. dev_dbg(hdev->dev, "%s: ---- CONTROL REGISTERS ----\n", prefix);
  256. DUMPREG(HDMI_INTC_FLAG);
  257. DUMPREG(HDMI_INTC_CON);
  258. DUMPREG(HDMI_HPD_STATUS);
  259. DUMPREG(HDMI_PHY_RSTOUT);
  260. DUMPREG(HDMI_PHY_VPLL);
  261. DUMPREG(HDMI_PHY_CMU);
  262. DUMPREG(HDMI_CORE_RSTOUT);
  263. dev_dbg(hdev->dev, "%s: ---- CORE REGISTERS ----\n", prefix);
  264. DUMPREG(HDMI_CON_0);
  265. DUMPREG(HDMI_CON_1);
  266. DUMPREG(HDMI_CON_2);
  267. DUMPREG(HDMI_SYS_STATUS);
  268. DUMPREG(HDMI_PHY_STATUS);
  269. DUMPREG(HDMI_STATUS_EN);
  270. DUMPREG(HDMI_HPD);
  271. DUMPREG(HDMI_MODE_SEL);
  272. DUMPREG(HDMI_HPD_GEN);
  273. DUMPREG(HDMI_DC_CONTROL);
  274. DUMPREG(HDMI_VIDEO_PATTERN_GEN);
  275. dev_dbg(hdev->dev, "%s: ---- CORE SYNC REGISTERS ----\n", prefix);
  276. DUMPREG(HDMI_H_BLANK_0);
  277. DUMPREG(HDMI_H_BLANK_1);
  278. DUMPREG(HDMI_V_BLANK_0);
  279. DUMPREG(HDMI_V_BLANK_1);
  280. DUMPREG(HDMI_V_BLANK_2);
  281. DUMPREG(HDMI_H_V_LINE_0);
  282. DUMPREG(HDMI_H_V_LINE_1);
  283. DUMPREG(HDMI_H_V_LINE_2);
  284. DUMPREG(HDMI_VSYNC_POL);
  285. DUMPREG(HDMI_INT_PRO_MODE);
  286. DUMPREG(HDMI_V_BLANK_F_0);
  287. DUMPREG(HDMI_V_BLANK_F_1);
  288. DUMPREG(HDMI_V_BLANK_F_2);
  289. DUMPREG(HDMI_H_SYNC_GEN_0);
  290. DUMPREG(HDMI_H_SYNC_GEN_1);
  291. DUMPREG(HDMI_H_SYNC_GEN_2);
  292. DUMPREG(HDMI_V_SYNC_GEN_1_0);
  293. DUMPREG(HDMI_V_SYNC_GEN_1_1);
  294. DUMPREG(HDMI_V_SYNC_GEN_1_2);
  295. DUMPREG(HDMI_V_SYNC_GEN_2_0);
  296. DUMPREG(HDMI_V_SYNC_GEN_2_1);
  297. DUMPREG(HDMI_V_SYNC_GEN_2_2);
  298. DUMPREG(HDMI_V_SYNC_GEN_3_0);
  299. DUMPREG(HDMI_V_SYNC_GEN_3_1);
  300. DUMPREG(HDMI_V_SYNC_GEN_3_2);
  301. dev_dbg(hdev->dev, "%s: ---- TG REGISTERS ----\n", prefix);
  302. DUMPREG(HDMI_TG_CMD);
  303. DUMPREG(HDMI_TG_H_FSZ_L);
  304. DUMPREG(HDMI_TG_H_FSZ_H);
  305. DUMPREG(HDMI_TG_HACT_ST_L);
  306. DUMPREG(HDMI_TG_HACT_ST_H);
  307. DUMPREG(HDMI_TG_HACT_SZ_L);
  308. DUMPREG(HDMI_TG_HACT_SZ_H);
  309. DUMPREG(HDMI_TG_V_FSZ_L);
  310. DUMPREG(HDMI_TG_V_FSZ_H);
  311. DUMPREG(HDMI_TG_VSYNC_L);
  312. DUMPREG(HDMI_TG_VSYNC_H);
  313. DUMPREG(HDMI_TG_VSYNC2_L);
  314. DUMPREG(HDMI_TG_VSYNC2_H);
  315. DUMPREG(HDMI_TG_VACT_ST_L);
  316. DUMPREG(HDMI_TG_VACT_ST_H);
  317. DUMPREG(HDMI_TG_VACT_SZ_L);
  318. DUMPREG(HDMI_TG_VACT_SZ_H);
  319. DUMPREG(HDMI_TG_FIELD_CHG_L);
  320. DUMPREG(HDMI_TG_FIELD_CHG_H);
  321. DUMPREG(HDMI_TG_VACT_ST2_L);
  322. DUMPREG(HDMI_TG_VACT_ST2_H);
  323. DUMPREG(HDMI_TG_VSYNC_TOP_HDMI_L);
  324. DUMPREG(HDMI_TG_VSYNC_TOP_HDMI_H);
  325. DUMPREG(HDMI_TG_VSYNC_BOT_HDMI_L);
  326. DUMPREG(HDMI_TG_VSYNC_BOT_HDMI_H);
  327. DUMPREG(HDMI_TG_FIELD_TOP_HDMI_L);
  328. DUMPREG(HDMI_TG_FIELD_TOP_HDMI_H);
  329. DUMPREG(HDMI_TG_FIELD_BOT_HDMI_L);
  330. DUMPREG(HDMI_TG_FIELD_BOT_HDMI_H);
  331. #undef DUMPREG
  332. }
  333. static const struct hdmi_timings hdmi_timings_480p = {
  334. .hact = { .beg = 138, .end = 858 },
  335. .hsyn_pol = 1,
  336. .hsyn = { .beg = 16, .end = 16 + 62 },
  337. .interlaced = 0,
  338. .vact[0] = { .beg = 42 + 3, .end = 522 + 3 },
  339. .vsyn_pol = 1,
  340. .vsyn[0] = { .beg = 6 + 3, .end = 12 + 3},
  341. };
  342. static const struct hdmi_timings hdmi_timings_576p50 = {
  343. .hact = { .beg = 144, .end = 864 },
  344. .hsyn_pol = 1,
  345. .hsyn = { .beg = 12, .end = 12 + 64 },
  346. .interlaced = 0,
  347. .vact[0] = { .beg = 44 + 5, .end = 620 + 5 },
  348. .vsyn_pol = 1,
  349. .vsyn[0] = { .beg = 0 + 5, .end = 5 + 5},
  350. };
  351. static const struct hdmi_timings hdmi_timings_720p60 = {
  352. .hact = { .beg = 370, .end = 1650 },
  353. .hsyn_pol = 0,
  354. .hsyn = { .beg = 110, .end = 110 + 40 },
  355. .interlaced = 0,
  356. .vact[0] = { .beg = 25 + 5, .end = 745 + 5 },
  357. .vsyn_pol = 0,
  358. .vsyn[0] = { .beg = 0 + 5, .end = 5 + 5},
  359. };
  360. static const struct hdmi_timings hdmi_timings_720p50 = {
  361. .hact = { .beg = 700, .end = 1980 },
  362. .hsyn_pol = 0,
  363. .hsyn = { .beg = 440, .end = 440 + 40 },
  364. .interlaced = 0,
  365. .vact[0] = { .beg = 25 + 5, .end = 745 + 5 },
  366. .vsyn_pol = 0,
  367. .vsyn[0] = { .beg = 0 + 5, .end = 5 + 5},
  368. };
  369. static const struct hdmi_timings hdmi_timings_1080p24 = {
  370. .hact = { .beg = 830, .end = 2750 },
  371. .hsyn_pol = 0,
  372. .hsyn = { .beg = 638, .end = 638 + 44 },
  373. .interlaced = 0,
  374. .vact[0] = { .beg = 41 + 4, .end = 1121 + 4 },
  375. .vsyn_pol = 0,
  376. .vsyn[0] = { .beg = 0 + 4, .end = 5 + 4},
  377. };
  378. static const struct hdmi_timings hdmi_timings_1080p60 = {
  379. .hact = { .beg = 280, .end = 2200 },
  380. .hsyn_pol = 0,
  381. .hsyn = { .beg = 88, .end = 88 + 44 },
  382. .interlaced = 0,
  383. .vact[0] = { .beg = 41 + 4, .end = 1121 + 4 },
  384. .vsyn_pol = 0,
  385. .vsyn[0] = { .beg = 0 + 4, .end = 5 + 4},
  386. };
  387. static const struct hdmi_timings hdmi_timings_1080i60 = {
  388. .hact = { .beg = 280, .end = 2200 },
  389. .hsyn_pol = 0,
  390. .hsyn = { .beg = 88, .end = 88 + 44 },
  391. .interlaced = 1,
  392. .vact[0] = { .beg = 20 + 2, .end = 560 + 2 },
  393. .vact[1] = { .beg = 583 + 2, .end = 1123 + 2 },
  394. .vsyn_pol = 0,
  395. .vsyn_off = 1100,
  396. .vsyn[0] = { .beg = 0 + 2, .end = 5 + 2},
  397. .vsyn[1] = { .beg = 562 + 2, .end = 567 + 2},
  398. };
  399. static const struct hdmi_timings hdmi_timings_1080i50 = {
  400. .hact = { .beg = 720, .end = 2640 },
  401. .hsyn_pol = 0,
  402. .hsyn = { .beg = 528, .end = 528 + 44 },
  403. .interlaced = 1,
  404. .vact[0] = { .beg = 20 + 2, .end = 560 + 2 },
  405. .vact[1] = { .beg = 583 + 2, .end = 1123 + 2 },
  406. .vsyn_pol = 0,
  407. .vsyn_off = 1320,
  408. .vsyn[0] = { .beg = 0 + 2, .end = 5 + 2},
  409. .vsyn[1] = { .beg = 562 + 2, .end = 567 + 2},
  410. };
  411. static const struct hdmi_timings hdmi_timings_1080p50 = {
  412. .hact = { .beg = 720, .end = 2640 },
  413. .hsyn_pol = 0,
  414. .hsyn = { .beg = 528, .end = 528 + 44 },
  415. .interlaced = 0,
  416. .vact[0] = { .beg = 41 + 4, .end = 1121 + 4 },
  417. .vsyn_pol = 0,
  418. .vsyn[0] = { .beg = 0 + 4, .end = 5 + 4},
  419. };
  420. static const struct {
  421. u32 preset;
  422. const struct hdmi_timings *timings;
  423. } hdmi_timings[] = {
  424. { V4L2_DV_480P59_94, &hdmi_timings_480p },
  425. { V4L2_DV_576P50, &hdmi_timings_576p50 },
  426. { V4L2_DV_720P50, &hdmi_timings_720p50 },
  427. { V4L2_DV_720P59_94, &hdmi_timings_720p60 },
  428. { V4L2_DV_720P60, &hdmi_timings_720p60 },
  429. { V4L2_DV_1080P24, &hdmi_timings_1080p24 },
  430. { V4L2_DV_1080P30, &hdmi_timings_1080p60 },
  431. { V4L2_DV_1080P50, &hdmi_timings_1080p50 },
  432. { V4L2_DV_1080I50, &hdmi_timings_1080i50 },
  433. { V4L2_DV_1080I60, &hdmi_timings_1080i60 },
  434. { V4L2_DV_1080P60, &hdmi_timings_1080p60 },
  435. };
  436. static const struct hdmi_timings *hdmi_preset2timings(u32 preset)
  437. {
  438. int i;
  439. for (i = 0; i < ARRAY_SIZE(hdmi_timings); ++i)
  440. if (hdmi_timings[i].preset == preset)
  441. return hdmi_timings[i].timings;
  442. return NULL;
  443. }
  444. static int hdmi_streamon(struct hdmi_device *hdev)
  445. {
  446. struct device *dev = hdev->dev;
  447. struct hdmi_resources *res = &hdev->res;
  448. int ret, tries;
  449. dev_dbg(dev, "%s\n", __func__);
  450. ret = v4l2_subdev_call(hdev->phy_sd, video, s_stream, 1);
  451. if (ret)
  452. return ret;
  453. /* waiting for HDMIPHY's PLL to get to steady state */
  454. for (tries = 100; tries; --tries) {
  455. u32 val = hdmi_read(hdev, HDMI_PHY_STATUS);
  456. if (val & HDMI_PHY_STATUS_READY)
  457. break;
  458. mdelay(1);
  459. }
  460. /* steady state not achieved */
  461. if (tries == 0) {
  462. dev_err(dev, "hdmiphy's pll could not reach steady state.\n");
  463. v4l2_subdev_call(hdev->phy_sd, video, s_stream, 0);
  464. hdmi_dumpregs(hdev, "hdmiphy - s_stream");
  465. return -EIO;
  466. }
  467. /* starting MHL */
  468. ret = v4l2_subdev_call(hdev->mhl_sd, video, s_stream, 1);
  469. if (hdev->mhl_sd && ret) {
  470. v4l2_subdev_call(hdev->phy_sd, video, s_stream, 0);
  471. hdmi_dumpregs(hdev, "mhl - s_stream");
  472. return -EIO;
  473. }
  474. /* hdmiphy clock is used for HDMI in streaming mode */
  475. clk_disable(res->sclk_hdmi);
  476. clk_set_parent(res->sclk_hdmi, res->sclk_hdmiphy);
  477. clk_enable(res->sclk_hdmi);
  478. /* enable HDMI and timing generator */
  479. hdmi_write_mask(hdev, HDMI_CON_0, ~0, HDMI_EN);
  480. hdmi_write_mask(hdev, HDMI_TG_CMD, ~0, HDMI_TG_EN);
  481. hdmi_dumpregs(hdev, "streamon");
  482. return 0;
  483. }
  484. static int hdmi_streamoff(struct hdmi_device *hdev)
  485. {
  486. struct device *dev = hdev->dev;
  487. struct hdmi_resources *res = &hdev->res;
  488. dev_dbg(dev, "%s\n", __func__);
  489. hdmi_write_mask(hdev, HDMI_CON_0, 0, HDMI_EN);
  490. hdmi_write_mask(hdev, HDMI_TG_CMD, 0, HDMI_TG_EN);
  491. /* pixel(vpll) clock is used for HDMI in config mode */
  492. clk_disable(res->sclk_hdmi);
  493. clk_set_parent(res->sclk_hdmi, res->sclk_pixel);
  494. clk_enable(res->sclk_hdmi);
  495. v4l2_subdev_call(hdev->mhl_sd, video, s_stream, 0);
  496. v4l2_subdev_call(hdev->phy_sd, video, s_stream, 0);
  497. hdmi_dumpregs(hdev, "streamoff");
  498. return 0;
  499. }
  500. static int hdmi_s_stream(struct v4l2_subdev *sd, int enable)
  501. {
  502. struct hdmi_device *hdev = sd_to_hdmi_dev(sd);
  503. struct device *dev = hdev->dev;
  504. dev_dbg(dev, "%s(%d)\n", __func__, enable);
  505. if (enable)
  506. return hdmi_streamon(hdev);
  507. return hdmi_streamoff(hdev);
  508. }
  509. static void hdmi_resource_poweron(struct hdmi_resources *res)
  510. {
  511. /* turn HDMI power on */
  512. regulator_bulk_enable(res->regul_count, res->regul_bulk);
  513. /* power-on hdmi physical interface */
  514. clk_enable(res->hdmiphy);
  515. /* use VPP as parent clock; HDMIPHY is not working yet */
  516. clk_set_parent(res->sclk_hdmi, res->sclk_pixel);
  517. /* turn clocks on */
  518. clk_enable(res->sclk_hdmi);
  519. }
  520. static void hdmi_resource_poweroff(struct hdmi_resources *res)
  521. {
  522. /* turn clocks off */
  523. clk_disable(res->sclk_hdmi);
  524. /* power-off hdmiphy */
  525. clk_disable(res->hdmiphy);
  526. /* turn HDMI power off */
  527. regulator_bulk_disable(res->regul_count, res->regul_bulk);
  528. }
  529. static int hdmi_s_power(struct v4l2_subdev *sd, int on)
  530. {
  531. struct hdmi_device *hdev = sd_to_hdmi_dev(sd);
  532. int ret;
  533. if (on)
  534. ret = pm_runtime_get_sync(hdev->dev);
  535. else
  536. ret = pm_runtime_put_sync(hdev->dev);
  537. /* only values < 0 indicate errors */
  538. return IS_ERR_VALUE(ret) ? ret : 0;
  539. }
  540. static int hdmi_s_dv_preset(struct v4l2_subdev *sd,
  541. struct v4l2_dv_preset *preset)
  542. {
  543. struct hdmi_device *hdev = sd_to_hdmi_dev(sd);
  544. struct device *dev = hdev->dev;
  545. const struct hdmi_timings *conf;
  546. conf = hdmi_preset2timings(preset->preset);
  547. if (conf == NULL) {
  548. dev_err(dev, "preset (%u) not supported\n", preset->preset);
  549. return -EINVAL;
  550. }
  551. hdev->cur_conf = conf;
  552. hdev->cur_preset = preset->preset;
  553. return 0;
  554. }
  555. static int hdmi_g_dv_preset(struct v4l2_subdev *sd,
  556. struct v4l2_dv_preset *preset)
  557. {
  558. memset(preset, 0, sizeof(*preset));
  559. preset->preset = sd_to_hdmi_dev(sd)->cur_preset;
  560. return 0;
  561. }
  562. static int hdmi_g_mbus_fmt(struct v4l2_subdev *sd,
  563. struct v4l2_mbus_framefmt *fmt)
  564. {
  565. struct hdmi_device *hdev = sd_to_hdmi_dev(sd);
  566. const struct hdmi_timings *t = hdev->cur_conf;
  567. dev_dbg(hdev->dev, "%s\n", __func__);
  568. if (!hdev->cur_conf)
  569. return -EINVAL;
  570. memset(fmt, 0, sizeof *fmt);
  571. fmt->width = t->hact.end - t->hact.beg;
  572. fmt->height = t->vact[0].end - t->vact[0].beg;
  573. fmt->code = V4L2_MBUS_FMT_FIXED; /* means RGB888 */
  574. fmt->colorspace = V4L2_COLORSPACE_SRGB;
  575. if (t->interlaced) {
  576. fmt->field = V4L2_FIELD_INTERLACED;
  577. fmt->height *= 2;
  578. } else {
  579. fmt->field = V4L2_FIELD_NONE;
  580. }
  581. return 0;
  582. }
  583. static int hdmi_enum_dv_presets(struct v4l2_subdev *sd,
  584. struct v4l2_dv_enum_preset *preset)
  585. {
  586. if (preset->index >= ARRAY_SIZE(hdmi_timings))
  587. return -EINVAL;
  588. return v4l_fill_dv_preset_info(hdmi_timings[preset->index].preset,
  589. preset);
  590. }
  591. static const struct v4l2_subdev_core_ops hdmi_sd_core_ops = {
  592. .s_power = hdmi_s_power,
  593. };
  594. static const struct v4l2_subdev_video_ops hdmi_sd_video_ops = {
  595. .s_dv_preset = hdmi_s_dv_preset,
  596. .g_dv_preset = hdmi_g_dv_preset,
  597. .enum_dv_presets = hdmi_enum_dv_presets,
  598. .g_mbus_fmt = hdmi_g_mbus_fmt,
  599. .s_stream = hdmi_s_stream,
  600. };
  601. static const struct v4l2_subdev_ops hdmi_sd_ops = {
  602. .core = &hdmi_sd_core_ops,
  603. .video = &hdmi_sd_video_ops,
  604. };
  605. static int hdmi_runtime_suspend(struct device *dev)
  606. {
  607. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  608. struct hdmi_device *hdev = sd_to_hdmi_dev(sd);
  609. dev_dbg(dev, "%s\n", __func__);
  610. v4l2_subdev_call(hdev->mhl_sd, core, s_power, 0);
  611. hdmi_resource_poweroff(&hdev->res);
  612. return 0;
  613. }
  614. static int hdmi_runtime_resume(struct device *dev)
  615. {
  616. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  617. struct hdmi_device *hdev = sd_to_hdmi_dev(sd);
  618. int ret = 0;
  619. dev_dbg(dev, "%s\n", __func__);
  620. hdmi_resource_poweron(&hdev->res);
  621. ret = hdmi_conf_apply(hdev);
  622. if (ret)
  623. goto fail;
  624. /* starting MHL */
  625. ret = v4l2_subdev_call(hdev->mhl_sd, core, s_power, 1);
  626. if (hdev->mhl_sd && ret)
  627. goto fail;
  628. dev_dbg(dev, "poweron succeed\n");
  629. return 0;
  630. fail:
  631. hdmi_resource_poweroff(&hdev->res);
  632. dev_err(dev, "poweron failed\n");
  633. return ret;
  634. }
  635. static const struct dev_pm_ops hdmi_pm_ops = {
  636. .runtime_suspend = hdmi_runtime_suspend,
  637. .runtime_resume = hdmi_runtime_resume,
  638. };
  639. static void hdmi_resources_cleanup(struct hdmi_device *hdev)
  640. {
  641. struct hdmi_resources *res = &hdev->res;
  642. dev_dbg(hdev->dev, "HDMI resource cleanup\n");
  643. /* put clocks, power */
  644. if (res->regul_count)
  645. regulator_bulk_free(res->regul_count, res->regul_bulk);
  646. /* kfree is NULL-safe */
  647. kfree(res->regul_bulk);
  648. if (!IS_ERR_OR_NULL(res->hdmiphy))
  649. clk_put(res->hdmiphy);
  650. if (!IS_ERR_OR_NULL(res->sclk_hdmiphy))
  651. clk_put(res->sclk_hdmiphy);
  652. if (!IS_ERR_OR_NULL(res->sclk_pixel))
  653. clk_put(res->sclk_pixel);
  654. if (!IS_ERR_OR_NULL(res->sclk_hdmi))
  655. clk_put(res->sclk_hdmi);
  656. if (!IS_ERR_OR_NULL(res->hdmi))
  657. clk_put(res->hdmi);
  658. memset(res, 0, sizeof *res);
  659. }
  660. static int hdmi_resources_init(struct hdmi_device *hdev)
  661. {
  662. struct device *dev = hdev->dev;
  663. struct hdmi_resources *res = &hdev->res;
  664. static char *supply[] = {
  665. "hdmi-en",
  666. "vdd",
  667. "vdd_osc",
  668. "vdd_pll",
  669. };
  670. int i, ret;
  671. dev_dbg(dev, "HDMI resource init\n");
  672. memset(res, 0, sizeof *res);
  673. /* get clocks, power */
  674. res->hdmi = clk_get(dev, "hdmi");
  675. if (IS_ERR_OR_NULL(res->hdmi)) {
  676. dev_err(dev, "failed to get clock 'hdmi'\n");
  677. goto fail;
  678. }
  679. res->sclk_hdmi = clk_get(dev, "sclk_hdmi");
  680. if (IS_ERR_OR_NULL(res->sclk_hdmi)) {
  681. dev_err(dev, "failed to get clock 'sclk_hdmi'\n");
  682. goto fail;
  683. }
  684. res->sclk_pixel = clk_get(dev, "sclk_pixel");
  685. if (IS_ERR_OR_NULL(res->sclk_pixel)) {
  686. dev_err(dev, "failed to get clock 'sclk_pixel'\n");
  687. goto fail;
  688. }
  689. res->sclk_hdmiphy = clk_get(dev, "sclk_hdmiphy");
  690. if (IS_ERR_OR_NULL(res->sclk_hdmiphy)) {
  691. dev_err(dev, "failed to get clock 'sclk_hdmiphy'\n");
  692. goto fail;
  693. }
  694. res->hdmiphy = clk_get(dev, "hdmiphy");
  695. if (IS_ERR_OR_NULL(res->hdmiphy)) {
  696. dev_err(dev, "failed to get clock 'hdmiphy'\n");
  697. goto fail;
  698. }
  699. res->regul_bulk = kcalloc(ARRAY_SIZE(supply),
  700. sizeof(res->regul_bulk[0]), GFP_KERNEL);
  701. if (!res->regul_bulk) {
  702. dev_err(dev, "failed to get memory for regulators\n");
  703. goto fail;
  704. }
  705. for (i = 0; i < ARRAY_SIZE(supply); ++i) {
  706. res->regul_bulk[i].supply = supply[i];
  707. res->regul_bulk[i].consumer = NULL;
  708. }
  709. ret = regulator_bulk_get(dev, ARRAY_SIZE(supply), res->regul_bulk);
  710. if (ret) {
  711. dev_err(dev, "failed to get regulators\n");
  712. goto fail;
  713. }
  714. res->regul_count = ARRAY_SIZE(supply);
  715. return 0;
  716. fail:
  717. dev_err(dev, "HDMI resource init - failed\n");
  718. hdmi_resources_cleanup(hdev);
  719. return -ENODEV;
  720. }
  721. static int __devinit hdmi_probe(struct platform_device *pdev)
  722. {
  723. struct device *dev = &pdev->dev;
  724. struct resource *res;
  725. struct i2c_adapter *adapter;
  726. struct v4l2_subdev *sd;
  727. struct hdmi_device *hdmi_dev = NULL;
  728. struct s5p_hdmi_platform_data *pdata = dev->platform_data;
  729. int ret;
  730. dev_dbg(dev, "probe start\n");
  731. if (!pdata) {
  732. dev_err(dev, "platform data is missing\n");
  733. ret = -ENODEV;
  734. goto fail;
  735. }
  736. hdmi_dev = devm_kzalloc(&pdev->dev, sizeof(*hdmi_dev), GFP_KERNEL);
  737. if (!hdmi_dev) {
  738. dev_err(dev, "out of memory\n");
  739. ret = -ENOMEM;
  740. goto fail;
  741. }
  742. hdmi_dev->dev = dev;
  743. ret = hdmi_resources_init(hdmi_dev);
  744. if (ret)
  745. goto fail;
  746. /* mapping HDMI registers */
  747. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  748. if (res == NULL) {
  749. dev_err(dev, "get memory resource failed.\n");
  750. ret = -ENXIO;
  751. goto fail_init;
  752. }
  753. hdmi_dev->regs = devm_ioremap(&pdev->dev, res->start,
  754. resource_size(res));
  755. if (hdmi_dev->regs == NULL) {
  756. dev_err(dev, "register mapping failed.\n");
  757. ret = -ENXIO;
  758. goto fail_init;
  759. }
  760. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  761. if (res == NULL) {
  762. dev_err(dev, "get interrupt resource failed.\n");
  763. ret = -ENXIO;
  764. goto fail_init;
  765. }
  766. ret = devm_request_irq(&pdev->dev, res->start, hdmi_irq_handler, 0,
  767. "hdmi", hdmi_dev);
  768. if (ret) {
  769. dev_err(dev, "request interrupt failed.\n");
  770. goto fail_init;
  771. }
  772. hdmi_dev->irq = res->start;
  773. /* setting v4l2 name to prevent WARN_ON in v4l2_device_register */
  774. strlcpy(hdmi_dev->v4l2_dev.name, dev_name(dev),
  775. sizeof(hdmi_dev->v4l2_dev.name));
  776. /* passing NULL owner prevents driver from erasing drvdata */
  777. ret = v4l2_device_register(NULL, &hdmi_dev->v4l2_dev);
  778. if (ret) {
  779. dev_err(dev, "could not register v4l2 device.\n");
  780. goto fail_init;
  781. }
  782. /* testing if hdmiphy info is present */
  783. if (!pdata->hdmiphy_info) {
  784. dev_err(dev, "hdmiphy info is missing in platform data\n");
  785. ret = -ENXIO;
  786. goto fail_vdev;
  787. }
  788. adapter = i2c_get_adapter(pdata->hdmiphy_bus);
  789. if (adapter == NULL) {
  790. dev_err(dev, "hdmiphy adapter request failed\n");
  791. ret = -ENXIO;
  792. goto fail_vdev;
  793. }
  794. hdmi_dev->phy_sd = v4l2_i2c_new_subdev_board(&hdmi_dev->v4l2_dev,
  795. adapter, pdata->hdmiphy_info, NULL);
  796. /* on failure or not adapter is no longer useful */
  797. i2c_put_adapter(adapter);
  798. if (hdmi_dev->phy_sd == NULL) {
  799. dev_err(dev, "missing subdev for hdmiphy\n");
  800. ret = -ENODEV;
  801. goto fail_vdev;
  802. }
  803. /* initialization of MHL interface if present */
  804. if (pdata->mhl_info) {
  805. adapter = i2c_get_adapter(pdata->mhl_bus);
  806. if (adapter == NULL) {
  807. dev_err(dev, "MHL adapter request failed\n");
  808. ret = -ENXIO;
  809. goto fail_vdev;
  810. }
  811. hdmi_dev->mhl_sd = v4l2_i2c_new_subdev_board(
  812. &hdmi_dev->v4l2_dev, adapter,
  813. pdata->mhl_info, NULL);
  814. /* on failure or not adapter is no longer useful */
  815. i2c_put_adapter(adapter);
  816. if (hdmi_dev->mhl_sd == NULL) {
  817. dev_err(dev, "missing subdev for MHL\n");
  818. ret = -ENODEV;
  819. goto fail_vdev;
  820. }
  821. }
  822. clk_enable(hdmi_dev->res.hdmi);
  823. pm_runtime_enable(dev);
  824. sd = &hdmi_dev->sd;
  825. v4l2_subdev_init(sd, &hdmi_sd_ops);
  826. sd->owner = THIS_MODULE;
  827. strlcpy(sd->name, "s5p-hdmi", sizeof sd->name);
  828. hdmi_dev->cur_preset = HDMI_DEFAULT_PRESET;
  829. /* FIXME: missing fail preset is not supported */
  830. hdmi_dev->cur_conf = hdmi_preset2timings(hdmi_dev->cur_preset);
  831. /* storing subdev for call that have only access to struct device */
  832. dev_set_drvdata(dev, sd);
  833. dev_info(dev, "probe successful\n");
  834. return 0;
  835. fail_vdev:
  836. v4l2_device_unregister(&hdmi_dev->v4l2_dev);
  837. fail_init:
  838. hdmi_resources_cleanup(hdmi_dev);
  839. fail:
  840. dev_err(dev, "probe failed\n");
  841. return ret;
  842. }
  843. static int __devexit hdmi_remove(struct platform_device *pdev)
  844. {
  845. struct device *dev = &pdev->dev;
  846. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  847. struct hdmi_device *hdmi_dev = sd_to_hdmi_dev(sd);
  848. pm_runtime_disable(dev);
  849. clk_disable(hdmi_dev->res.hdmi);
  850. v4l2_device_unregister(&hdmi_dev->v4l2_dev);
  851. disable_irq(hdmi_dev->irq);
  852. hdmi_resources_cleanup(hdmi_dev);
  853. dev_info(dev, "remove successful\n");
  854. return 0;
  855. }
  856. static struct platform_driver hdmi_driver __refdata = {
  857. .probe = hdmi_probe,
  858. .remove = __devexit_p(hdmi_remove),
  859. .id_table = hdmi_driver_types,
  860. .driver = {
  861. .name = "s5p-hdmi",
  862. .owner = THIS_MODULE,
  863. .pm = &hdmi_pm_ops,
  864. }
  865. };
  866. module_platform_driver(hdmi_driver);