imx-tve.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * i.MX drm driver - Television Encoder (TVEv2)
  3. *
  4. * Copyright (C) 2013 Philipp Zabel, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/component.h>
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/videodev2.h>
  24. #include <drm/drmP.h>
  25. #include <drm/drm_fb_helper.h>
  26. #include <drm/drm_crtc_helper.h>
  27. #include <video/imx-ipu-v3.h>
  28. #include "imx-drm.h"
  29. #define TVE_COM_CONF_REG 0x00
  30. #define TVE_TVDAC0_CONT_REG 0x28
  31. #define TVE_TVDAC1_CONT_REG 0x2c
  32. #define TVE_TVDAC2_CONT_REG 0x30
  33. #define TVE_CD_CONT_REG 0x34
  34. #define TVE_INT_CONT_REG 0x64
  35. #define TVE_STAT_REG 0x68
  36. #define TVE_TST_MODE_REG 0x6c
  37. #define TVE_MV_CONT_REG 0xdc
  38. /* TVE_COM_CONF_REG */
  39. #define TVE_SYNC_CH_2_EN BIT(22)
  40. #define TVE_SYNC_CH_1_EN BIT(21)
  41. #define TVE_SYNC_CH_0_EN BIT(20)
  42. #define TVE_TV_OUT_MODE_MASK (0x7 << 12)
  43. #define TVE_TV_OUT_DISABLE (0x0 << 12)
  44. #define TVE_TV_OUT_CVBS_0 (0x1 << 12)
  45. #define TVE_TV_OUT_CVBS_2 (0x2 << 12)
  46. #define TVE_TV_OUT_CVBS_0_2 (0x3 << 12)
  47. #define TVE_TV_OUT_SVIDEO_0_1 (0x4 << 12)
  48. #define TVE_TV_OUT_SVIDEO_0_1_CVBS2_2 (0x5 << 12)
  49. #define TVE_TV_OUT_YPBPR (0x6 << 12)
  50. #define TVE_TV_OUT_RGB (0x7 << 12)
  51. #define TVE_TV_STAND_MASK (0xf << 8)
  52. #define TVE_TV_STAND_HD_1080P30 (0xc << 8)
  53. #define TVE_P2I_CONV_EN BIT(7)
  54. #define TVE_INP_VIDEO_FORM BIT(6)
  55. #define TVE_INP_YCBCR_422 (0x0 << 6)
  56. #define TVE_INP_YCBCR_444 (0x1 << 6)
  57. #define TVE_DATA_SOURCE_MASK (0x3 << 4)
  58. #define TVE_DATA_SOURCE_BUS1 (0x0 << 4)
  59. #define TVE_DATA_SOURCE_BUS2 (0x1 << 4)
  60. #define TVE_DATA_SOURCE_EXT (0x2 << 4)
  61. #define TVE_DATA_SOURCE_TESTGEN (0x3 << 4)
  62. #define TVE_IPU_CLK_EN_OFS 3
  63. #define TVE_IPU_CLK_EN BIT(3)
  64. #define TVE_DAC_SAMP_RATE_OFS 1
  65. #define TVE_DAC_SAMP_RATE_WIDTH 2
  66. #define TVE_DAC_SAMP_RATE_MASK (0x3 << 1)
  67. #define TVE_DAC_FULL_RATE (0x0 << 1)
  68. #define TVE_DAC_DIV2_RATE (0x1 << 1)
  69. #define TVE_DAC_DIV4_RATE (0x2 << 1)
  70. #define TVE_EN BIT(0)
  71. /* TVE_TVDACx_CONT_REG */
  72. #define TVE_TVDAC_GAIN_MASK (0x3f << 0)
  73. /* TVE_CD_CONT_REG */
  74. #define TVE_CD_CH_2_SM_EN BIT(22)
  75. #define TVE_CD_CH_1_SM_EN BIT(21)
  76. #define TVE_CD_CH_0_SM_EN BIT(20)
  77. #define TVE_CD_CH_2_LM_EN BIT(18)
  78. #define TVE_CD_CH_1_LM_EN BIT(17)
  79. #define TVE_CD_CH_0_LM_EN BIT(16)
  80. #define TVE_CD_CH_2_REF_LVL BIT(10)
  81. #define TVE_CD_CH_1_REF_LVL BIT(9)
  82. #define TVE_CD_CH_0_REF_LVL BIT(8)
  83. #define TVE_CD_EN BIT(0)
  84. /* TVE_INT_CONT_REG */
  85. #define TVE_FRAME_END_IEN BIT(13)
  86. #define TVE_CD_MON_END_IEN BIT(2)
  87. #define TVE_CD_SM_IEN BIT(1)
  88. #define TVE_CD_LM_IEN BIT(0)
  89. /* TVE_TST_MODE_REG */
  90. #define TVE_TVDAC_TEST_MODE_MASK (0x7 << 0)
  91. #define con_to_tve(x) container_of(x, struct imx_tve, connector)
  92. #define enc_to_tve(x) container_of(x, struct imx_tve, encoder)
  93. enum {
  94. TVE_MODE_TVOUT,
  95. TVE_MODE_VGA,
  96. };
  97. struct imx_tve {
  98. struct drm_connector connector;
  99. struct drm_encoder encoder;
  100. struct device *dev;
  101. spinlock_t lock; /* register lock */
  102. bool enabled;
  103. int mode;
  104. struct regmap *regmap;
  105. struct regulator *dac_reg;
  106. struct i2c_adapter *ddc;
  107. struct clk *clk;
  108. struct clk *di_sel_clk;
  109. struct clk_hw clk_hw_di;
  110. struct clk *di_clk;
  111. int vsync_pin;
  112. int hsync_pin;
  113. };
  114. static void tve_lock(void *__tve)
  115. __acquires(&tve->lock)
  116. {
  117. struct imx_tve *tve = __tve;
  118. spin_lock(&tve->lock);
  119. }
  120. static void tve_unlock(void *__tve)
  121. __releases(&tve->lock)
  122. {
  123. struct imx_tve *tve = __tve;
  124. spin_unlock(&tve->lock);
  125. }
  126. static void tve_enable(struct imx_tve *tve)
  127. {
  128. int ret;
  129. if (!tve->enabled) {
  130. tve->enabled = true;
  131. clk_prepare_enable(tve->clk);
  132. ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
  133. TVE_IPU_CLK_EN | TVE_EN,
  134. TVE_IPU_CLK_EN | TVE_EN);
  135. }
  136. /* clear interrupt status register */
  137. regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
  138. /* cable detection irq disabled in VGA mode, enabled in TVOUT mode */
  139. if (tve->mode == TVE_MODE_VGA)
  140. regmap_write(tve->regmap, TVE_INT_CONT_REG, 0);
  141. else
  142. regmap_write(tve->regmap, TVE_INT_CONT_REG,
  143. TVE_CD_SM_IEN |
  144. TVE_CD_LM_IEN |
  145. TVE_CD_MON_END_IEN);
  146. }
  147. static void tve_disable(struct imx_tve *tve)
  148. {
  149. int ret;
  150. if (tve->enabled) {
  151. tve->enabled = false;
  152. ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
  153. TVE_IPU_CLK_EN | TVE_EN, 0);
  154. clk_disable_unprepare(tve->clk);
  155. }
  156. }
  157. static int tve_setup_tvout(struct imx_tve *tve)
  158. {
  159. return -ENOTSUPP;
  160. }
  161. static int tve_setup_vga(struct imx_tve *tve)
  162. {
  163. unsigned int mask;
  164. unsigned int val;
  165. int ret;
  166. /* set gain to (1 + 10/128) to provide 0.7V peak-to-peak amplitude */
  167. ret = regmap_update_bits(tve->regmap, TVE_TVDAC0_CONT_REG,
  168. TVE_TVDAC_GAIN_MASK, 0x0a);
  169. if (ret)
  170. return ret;
  171. ret = regmap_update_bits(tve->regmap, TVE_TVDAC1_CONT_REG,
  172. TVE_TVDAC_GAIN_MASK, 0x0a);
  173. if (ret)
  174. return ret;
  175. ret = regmap_update_bits(tve->regmap, TVE_TVDAC2_CONT_REG,
  176. TVE_TVDAC_GAIN_MASK, 0x0a);
  177. if (ret)
  178. return ret;
  179. /* set configuration register */
  180. mask = TVE_DATA_SOURCE_MASK | TVE_INP_VIDEO_FORM;
  181. val = TVE_DATA_SOURCE_BUS2 | TVE_INP_YCBCR_444;
  182. mask |= TVE_TV_STAND_MASK | TVE_P2I_CONV_EN;
  183. val |= TVE_TV_STAND_HD_1080P30 | 0;
  184. mask |= TVE_TV_OUT_MODE_MASK | TVE_SYNC_CH_0_EN;
  185. val |= TVE_TV_OUT_RGB | TVE_SYNC_CH_0_EN;
  186. ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG, mask, val);
  187. if (ret)
  188. return ret;
  189. /* set test mode (as documented) */
  190. return regmap_update_bits(tve->regmap, TVE_TST_MODE_REG,
  191. TVE_TVDAC_TEST_MODE_MASK, 1);
  192. }
  193. static enum drm_connector_status imx_tve_connector_detect(
  194. struct drm_connector *connector, bool force)
  195. {
  196. return connector_status_connected;
  197. }
  198. static int imx_tve_connector_get_modes(struct drm_connector *connector)
  199. {
  200. struct imx_tve *tve = con_to_tve(connector);
  201. struct edid *edid;
  202. int ret = 0;
  203. if (!tve->ddc)
  204. return 0;
  205. edid = drm_get_edid(connector, tve->ddc);
  206. if (edid) {
  207. drm_mode_connector_update_edid_property(connector, edid);
  208. ret = drm_add_edid_modes(connector, edid);
  209. kfree(edid);
  210. }
  211. return ret;
  212. }
  213. static int imx_tve_connector_mode_valid(struct drm_connector *connector,
  214. struct drm_display_mode *mode)
  215. {
  216. struct imx_tve *tve = con_to_tve(connector);
  217. unsigned long rate;
  218. /* pixel clock with 2x oversampling */
  219. rate = clk_round_rate(tve->clk, 2000UL * mode->clock) / 2000;
  220. if (rate == mode->clock)
  221. return MODE_OK;
  222. /* pixel clock without oversampling */
  223. rate = clk_round_rate(tve->clk, 1000UL * mode->clock) / 1000;
  224. if (rate == mode->clock)
  225. return MODE_OK;
  226. dev_warn(tve->dev, "ignoring mode %dx%d\n",
  227. mode->hdisplay, mode->vdisplay);
  228. return MODE_BAD;
  229. }
  230. static struct drm_encoder *imx_tve_connector_best_encoder(
  231. struct drm_connector *connector)
  232. {
  233. struct imx_tve *tve = con_to_tve(connector);
  234. return &tve->encoder;
  235. }
  236. static void imx_tve_encoder_dpms(struct drm_encoder *encoder, int mode)
  237. {
  238. struct imx_tve *tve = enc_to_tve(encoder);
  239. int ret;
  240. ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
  241. TVE_TV_OUT_MODE_MASK, TVE_TV_OUT_DISABLE);
  242. if (ret < 0)
  243. dev_err(tve->dev, "failed to disable TVOUT: %d\n", ret);
  244. }
  245. static bool imx_tve_encoder_mode_fixup(struct drm_encoder *encoder,
  246. const struct drm_display_mode *mode,
  247. struct drm_display_mode *adjusted_mode)
  248. {
  249. return true;
  250. }
  251. static void imx_tve_encoder_prepare(struct drm_encoder *encoder)
  252. {
  253. struct imx_tve *tve = enc_to_tve(encoder);
  254. tve_disable(tve);
  255. switch (tve->mode) {
  256. case TVE_MODE_VGA:
  257. imx_drm_set_bus_format_pins(encoder, MEDIA_BUS_FMT_GBR888_1X24,
  258. tve->hsync_pin, tve->vsync_pin);
  259. break;
  260. case TVE_MODE_TVOUT:
  261. imx_drm_set_bus_format(encoder, MEDIA_BUS_FMT_YUV8_1X24);
  262. break;
  263. }
  264. }
  265. static void imx_tve_encoder_mode_set(struct drm_encoder *encoder,
  266. struct drm_display_mode *orig_mode,
  267. struct drm_display_mode *mode)
  268. {
  269. struct imx_tve *tve = enc_to_tve(encoder);
  270. unsigned long rounded_rate;
  271. unsigned long rate;
  272. int div = 1;
  273. int ret;
  274. /*
  275. * FIXME
  276. * we should try 4k * mode->clock first,
  277. * and enable 4x oversampling for lower resolutions
  278. */
  279. rate = 2000UL * mode->clock;
  280. clk_set_rate(tve->clk, rate);
  281. rounded_rate = clk_get_rate(tve->clk);
  282. if (rounded_rate >= rate)
  283. div = 2;
  284. clk_set_rate(tve->di_clk, rounded_rate / div);
  285. ret = clk_set_parent(tve->di_sel_clk, tve->di_clk);
  286. if (ret < 0) {
  287. dev_err(tve->dev, "failed to set di_sel parent to tve_di: %d\n",
  288. ret);
  289. }
  290. if (tve->mode == TVE_MODE_VGA)
  291. ret = tve_setup_vga(tve);
  292. else
  293. ret = tve_setup_tvout(tve);
  294. if (ret)
  295. dev_err(tve->dev, "failed to set configuration: %d\n", ret);
  296. }
  297. static void imx_tve_encoder_commit(struct drm_encoder *encoder)
  298. {
  299. struct imx_tve *tve = enc_to_tve(encoder);
  300. tve_enable(tve);
  301. }
  302. static void imx_tve_encoder_disable(struct drm_encoder *encoder)
  303. {
  304. struct imx_tve *tve = enc_to_tve(encoder);
  305. tve_disable(tve);
  306. }
  307. static struct drm_connector_funcs imx_tve_connector_funcs = {
  308. .dpms = drm_helper_connector_dpms,
  309. .fill_modes = drm_helper_probe_single_connector_modes,
  310. .detect = imx_tve_connector_detect,
  311. .destroy = imx_drm_connector_destroy,
  312. };
  313. static struct drm_connector_helper_funcs imx_tve_connector_helper_funcs = {
  314. .get_modes = imx_tve_connector_get_modes,
  315. .best_encoder = imx_tve_connector_best_encoder,
  316. .mode_valid = imx_tve_connector_mode_valid,
  317. };
  318. static struct drm_encoder_funcs imx_tve_encoder_funcs = {
  319. .destroy = imx_drm_encoder_destroy,
  320. };
  321. static struct drm_encoder_helper_funcs imx_tve_encoder_helper_funcs = {
  322. .dpms = imx_tve_encoder_dpms,
  323. .mode_fixup = imx_tve_encoder_mode_fixup,
  324. .prepare = imx_tve_encoder_prepare,
  325. .mode_set = imx_tve_encoder_mode_set,
  326. .commit = imx_tve_encoder_commit,
  327. .disable = imx_tve_encoder_disable,
  328. };
  329. static irqreturn_t imx_tve_irq_handler(int irq, void *data)
  330. {
  331. struct imx_tve *tve = data;
  332. unsigned int val;
  333. regmap_read(tve->regmap, TVE_STAT_REG, &val);
  334. /* clear interrupt status register */
  335. regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
  336. return IRQ_HANDLED;
  337. }
  338. static unsigned long clk_tve_di_recalc_rate(struct clk_hw *hw,
  339. unsigned long parent_rate)
  340. {
  341. struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
  342. unsigned int val;
  343. int ret;
  344. ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
  345. if (ret < 0)
  346. return 0;
  347. switch (val & TVE_DAC_SAMP_RATE_MASK) {
  348. case TVE_DAC_DIV4_RATE:
  349. return parent_rate / 4;
  350. case TVE_DAC_DIV2_RATE:
  351. return parent_rate / 2;
  352. case TVE_DAC_FULL_RATE:
  353. default:
  354. return parent_rate;
  355. }
  356. return 0;
  357. }
  358. static long clk_tve_di_round_rate(struct clk_hw *hw, unsigned long rate,
  359. unsigned long *prate)
  360. {
  361. unsigned long div;
  362. div = *prate / rate;
  363. if (div >= 4)
  364. return *prate / 4;
  365. else if (div >= 2)
  366. return *prate / 2;
  367. return *prate;
  368. }
  369. static int clk_tve_di_set_rate(struct clk_hw *hw, unsigned long rate,
  370. unsigned long parent_rate)
  371. {
  372. struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
  373. unsigned long div;
  374. u32 val;
  375. int ret;
  376. div = parent_rate / rate;
  377. if (div >= 4)
  378. val = TVE_DAC_DIV4_RATE;
  379. else if (div >= 2)
  380. val = TVE_DAC_DIV2_RATE;
  381. else
  382. val = TVE_DAC_FULL_RATE;
  383. ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
  384. TVE_DAC_SAMP_RATE_MASK, val);
  385. if (ret < 0) {
  386. dev_err(tve->dev, "failed to set divider: %d\n", ret);
  387. return ret;
  388. }
  389. return 0;
  390. }
  391. static struct clk_ops clk_tve_di_ops = {
  392. .round_rate = clk_tve_di_round_rate,
  393. .set_rate = clk_tve_di_set_rate,
  394. .recalc_rate = clk_tve_di_recalc_rate,
  395. };
  396. static int tve_clk_init(struct imx_tve *tve, void __iomem *base)
  397. {
  398. const char *tve_di_parent[1];
  399. struct clk_init_data init = {
  400. .name = "tve_di",
  401. .ops = &clk_tve_di_ops,
  402. .num_parents = 1,
  403. .flags = 0,
  404. };
  405. tve_di_parent[0] = __clk_get_name(tve->clk);
  406. init.parent_names = (const char **)&tve_di_parent;
  407. tve->clk_hw_di.init = &init;
  408. tve->di_clk = clk_register(tve->dev, &tve->clk_hw_di);
  409. if (IS_ERR(tve->di_clk)) {
  410. dev_err(tve->dev, "failed to register TVE output clock: %ld\n",
  411. PTR_ERR(tve->di_clk));
  412. return PTR_ERR(tve->di_clk);
  413. }
  414. return 0;
  415. }
  416. static int imx_tve_register(struct drm_device *drm, struct imx_tve *tve)
  417. {
  418. int encoder_type;
  419. int ret;
  420. encoder_type = tve->mode == TVE_MODE_VGA ?
  421. DRM_MODE_ENCODER_DAC : DRM_MODE_ENCODER_TVDAC;
  422. ret = imx_drm_encoder_parse_of(drm, &tve->encoder,
  423. tve->dev->of_node);
  424. if (ret)
  425. return ret;
  426. drm_encoder_helper_add(&tve->encoder, &imx_tve_encoder_helper_funcs);
  427. drm_encoder_init(drm, &tve->encoder, &imx_tve_encoder_funcs,
  428. encoder_type);
  429. drm_connector_helper_add(&tve->connector,
  430. &imx_tve_connector_helper_funcs);
  431. drm_connector_init(drm, &tve->connector, &imx_tve_connector_funcs,
  432. DRM_MODE_CONNECTOR_VGA);
  433. drm_mode_connector_attach_encoder(&tve->connector, &tve->encoder);
  434. return 0;
  435. }
  436. static bool imx_tve_readable_reg(struct device *dev, unsigned int reg)
  437. {
  438. return (reg % 4 == 0) && (reg <= 0xdc);
  439. }
  440. static struct regmap_config tve_regmap_config = {
  441. .reg_bits = 32,
  442. .val_bits = 32,
  443. .reg_stride = 4,
  444. .readable_reg = imx_tve_readable_reg,
  445. .lock = tve_lock,
  446. .unlock = tve_unlock,
  447. .max_register = 0xdc,
  448. };
  449. static const char * const imx_tve_modes[] = {
  450. [TVE_MODE_TVOUT] = "tvout",
  451. [TVE_MODE_VGA] = "vga",
  452. };
  453. static const int of_get_tve_mode(struct device_node *np)
  454. {
  455. const char *bm;
  456. int ret, i;
  457. ret = of_property_read_string(np, "fsl,tve-mode", &bm);
  458. if (ret < 0)
  459. return ret;
  460. for (i = 0; i < ARRAY_SIZE(imx_tve_modes); i++)
  461. if (!strcasecmp(bm, imx_tve_modes[i]))
  462. return i;
  463. return -EINVAL;
  464. }
  465. static int imx_tve_bind(struct device *dev, struct device *master, void *data)
  466. {
  467. struct platform_device *pdev = to_platform_device(dev);
  468. struct drm_device *drm = data;
  469. struct device_node *np = dev->of_node;
  470. struct device_node *ddc_node;
  471. struct imx_tve *tve;
  472. struct resource *res;
  473. void __iomem *base;
  474. unsigned int val;
  475. int irq;
  476. int ret;
  477. tve = devm_kzalloc(dev, sizeof(*tve), GFP_KERNEL);
  478. if (!tve)
  479. return -ENOMEM;
  480. tve->dev = dev;
  481. spin_lock_init(&tve->lock);
  482. ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
  483. if (ddc_node) {
  484. tve->ddc = of_find_i2c_adapter_by_node(ddc_node);
  485. of_node_put(ddc_node);
  486. }
  487. tve->mode = of_get_tve_mode(np);
  488. if (tve->mode != TVE_MODE_VGA) {
  489. dev_err(dev, "only VGA mode supported, currently\n");
  490. return -EINVAL;
  491. }
  492. if (tve->mode == TVE_MODE_VGA) {
  493. ret = of_property_read_u32(np, "fsl,hsync-pin",
  494. &tve->hsync_pin);
  495. if (ret < 0) {
  496. dev_err(dev, "failed to get vsync pin\n");
  497. return ret;
  498. }
  499. ret |= of_property_read_u32(np, "fsl,vsync-pin",
  500. &tve->vsync_pin);
  501. if (ret < 0) {
  502. dev_err(dev, "failed to get vsync pin\n");
  503. return ret;
  504. }
  505. }
  506. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  507. base = devm_ioremap_resource(dev, res);
  508. if (IS_ERR(base))
  509. return PTR_ERR(base);
  510. tve_regmap_config.lock_arg = tve;
  511. tve->regmap = devm_regmap_init_mmio_clk(dev, "tve", base,
  512. &tve_regmap_config);
  513. if (IS_ERR(tve->regmap)) {
  514. dev_err(dev, "failed to init regmap: %ld\n",
  515. PTR_ERR(tve->regmap));
  516. return PTR_ERR(tve->regmap);
  517. }
  518. irq = platform_get_irq(pdev, 0);
  519. if (irq < 0) {
  520. dev_err(dev, "failed to get irq\n");
  521. return irq;
  522. }
  523. ret = devm_request_threaded_irq(dev, irq, NULL,
  524. imx_tve_irq_handler, IRQF_ONESHOT,
  525. "imx-tve", tve);
  526. if (ret < 0) {
  527. dev_err(dev, "failed to request irq: %d\n", ret);
  528. return ret;
  529. }
  530. tve->dac_reg = devm_regulator_get(dev, "dac");
  531. if (!IS_ERR(tve->dac_reg)) {
  532. regulator_set_voltage(tve->dac_reg, 2750000, 2750000);
  533. ret = regulator_enable(tve->dac_reg);
  534. if (ret)
  535. return ret;
  536. }
  537. tve->clk = devm_clk_get(dev, "tve");
  538. if (IS_ERR(tve->clk)) {
  539. dev_err(dev, "failed to get high speed tve clock: %ld\n",
  540. PTR_ERR(tve->clk));
  541. return PTR_ERR(tve->clk);
  542. }
  543. /* this is the IPU DI clock input selector, can be parented to tve_di */
  544. tve->di_sel_clk = devm_clk_get(dev, "di_sel");
  545. if (IS_ERR(tve->di_sel_clk)) {
  546. dev_err(dev, "failed to get ipu di mux clock: %ld\n",
  547. PTR_ERR(tve->di_sel_clk));
  548. return PTR_ERR(tve->di_sel_clk);
  549. }
  550. ret = tve_clk_init(tve, base);
  551. if (ret < 0)
  552. return ret;
  553. ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
  554. if (ret < 0) {
  555. dev_err(dev, "failed to read configuration register: %d\n",
  556. ret);
  557. return ret;
  558. }
  559. if (val != 0x00100000) {
  560. dev_err(dev, "configuration register default value indicates this is not a TVEv2\n");
  561. return -ENODEV;
  562. }
  563. /* disable cable detection for VGA mode */
  564. ret = regmap_write(tve->regmap, TVE_CD_CONT_REG, 0);
  565. if (ret)
  566. return ret;
  567. ret = imx_tve_register(drm, tve);
  568. if (ret)
  569. return ret;
  570. dev_set_drvdata(dev, tve);
  571. return 0;
  572. }
  573. static void imx_tve_unbind(struct device *dev, struct device *master,
  574. void *data)
  575. {
  576. struct imx_tve *tve = dev_get_drvdata(dev);
  577. tve->connector.funcs->destroy(&tve->connector);
  578. tve->encoder.funcs->destroy(&tve->encoder);
  579. if (!IS_ERR(tve->dac_reg))
  580. regulator_disable(tve->dac_reg);
  581. }
  582. static const struct component_ops imx_tve_ops = {
  583. .bind = imx_tve_bind,
  584. .unbind = imx_tve_unbind,
  585. };
  586. static int imx_tve_probe(struct platform_device *pdev)
  587. {
  588. return component_add(&pdev->dev, &imx_tve_ops);
  589. }
  590. static int imx_tve_remove(struct platform_device *pdev)
  591. {
  592. component_del(&pdev->dev, &imx_tve_ops);
  593. return 0;
  594. }
  595. static const struct of_device_id imx_tve_dt_ids[] = {
  596. { .compatible = "fsl,imx53-tve", },
  597. { /* sentinel */ }
  598. };
  599. MODULE_DEVICE_TABLE(of, imx_tve_dt_ids);
  600. static struct platform_driver imx_tve_driver = {
  601. .probe = imx_tve_probe,
  602. .remove = imx_tve_remove,
  603. .driver = {
  604. .of_match_table = imx_tve_dt_ids,
  605. .name = "imx-tve",
  606. },
  607. };
  608. module_platform_driver(imx_tve_driver);
  609. MODULE_DESCRIPTION("i.MX Television Encoder driver");
  610. MODULE_AUTHOR("Philipp Zabel, Pengutronix");
  611. MODULE_LICENSE("GPL");
  612. MODULE_ALIAS("platform:imx-tve");