hdmi_drv.c 27 KB

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