blob: 82a1edd74f2048d734d022ba25fdb0335272b5e5 [file] [log] [blame]
Philipp Zabelfcbc51e2013-04-08 18:04:38 +02001/*
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.
Philipp Zabelfcbc51e2013-04-08 18:04:38 +020014 */
15
16#include <linux/clk.h>
17#include <linux/clk-provider.h>
Russell King17b50012013-11-03 11:23:34 +000018#include <linux/component.h>
Philipp Zabelfcbc51e2013-04-08 18:04:38 +020019#include <linux/module.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010020#include <linux/i2c.h>
Philipp Zabelfcbc51e2013-04-08 18:04:38 +020021#include <linux/regmap.h>
22#include <linux/regulator/consumer.h>
23#include <linux/spinlock.h>
24#include <linux/videodev2.h>
25#include <drm/drmP.h>
Liu Ying255c35f2016-07-08 17:40:56 +080026#include <drm/drm_atomic_helper.h>
Philipp Zabelfcbc51e2013-04-08 18:04:38 +020027#include <drm/drm_fb_helper.h>
28#include <drm/drm_crtc_helper.h>
Philipp Zabel39b90042013-09-30 16:13:39 +020029#include <video/imx-ipu-v3.h>
Philipp Zabelfcbc51e2013-04-08 18:04:38 +020030
31#include "imx-drm.h"
32
33#define TVE_COM_CONF_REG 0x00
34#define TVE_TVDAC0_CONT_REG 0x28
35#define TVE_TVDAC1_CONT_REG 0x2c
36#define TVE_TVDAC2_CONT_REG 0x30
37#define TVE_CD_CONT_REG 0x34
38#define TVE_INT_CONT_REG 0x64
39#define TVE_STAT_REG 0x68
40#define TVE_TST_MODE_REG 0x6c
41#define TVE_MV_CONT_REG 0xdc
42
43/* TVE_COM_CONF_REG */
44#define TVE_SYNC_CH_2_EN BIT(22)
45#define TVE_SYNC_CH_1_EN BIT(21)
46#define TVE_SYNC_CH_0_EN BIT(20)
47#define TVE_TV_OUT_MODE_MASK (0x7 << 12)
48#define TVE_TV_OUT_DISABLE (0x0 << 12)
49#define TVE_TV_OUT_CVBS_0 (0x1 << 12)
50#define TVE_TV_OUT_CVBS_2 (0x2 << 12)
51#define TVE_TV_OUT_CVBS_0_2 (0x3 << 12)
52#define TVE_TV_OUT_SVIDEO_0_1 (0x4 << 12)
53#define TVE_TV_OUT_SVIDEO_0_1_CVBS2_2 (0x5 << 12)
54#define TVE_TV_OUT_YPBPR (0x6 << 12)
55#define TVE_TV_OUT_RGB (0x7 << 12)
56#define TVE_TV_STAND_MASK (0xf << 8)
57#define TVE_TV_STAND_HD_1080P30 (0xc << 8)
58#define TVE_P2I_CONV_EN BIT(7)
59#define TVE_INP_VIDEO_FORM BIT(6)
60#define TVE_INP_YCBCR_422 (0x0 << 6)
61#define TVE_INP_YCBCR_444 (0x1 << 6)
62#define TVE_DATA_SOURCE_MASK (0x3 << 4)
63#define TVE_DATA_SOURCE_BUS1 (0x0 << 4)
64#define TVE_DATA_SOURCE_BUS2 (0x1 << 4)
65#define TVE_DATA_SOURCE_EXT (0x2 << 4)
66#define TVE_DATA_SOURCE_TESTGEN (0x3 << 4)
67#define TVE_IPU_CLK_EN_OFS 3
68#define TVE_IPU_CLK_EN BIT(3)
69#define TVE_DAC_SAMP_RATE_OFS 1
70#define TVE_DAC_SAMP_RATE_WIDTH 2
71#define TVE_DAC_SAMP_RATE_MASK (0x3 << 1)
72#define TVE_DAC_FULL_RATE (0x0 << 1)
73#define TVE_DAC_DIV2_RATE (0x1 << 1)
74#define TVE_DAC_DIV4_RATE (0x2 << 1)
75#define TVE_EN BIT(0)
76
77/* TVE_TVDACx_CONT_REG */
78#define TVE_TVDAC_GAIN_MASK (0x3f << 0)
79
80/* TVE_CD_CONT_REG */
81#define TVE_CD_CH_2_SM_EN BIT(22)
82#define TVE_CD_CH_1_SM_EN BIT(21)
83#define TVE_CD_CH_0_SM_EN BIT(20)
84#define TVE_CD_CH_2_LM_EN BIT(18)
85#define TVE_CD_CH_1_LM_EN BIT(17)
86#define TVE_CD_CH_0_LM_EN BIT(16)
87#define TVE_CD_CH_2_REF_LVL BIT(10)
88#define TVE_CD_CH_1_REF_LVL BIT(9)
89#define TVE_CD_CH_0_REF_LVL BIT(8)
90#define TVE_CD_EN BIT(0)
91
92/* TVE_INT_CONT_REG */
93#define TVE_FRAME_END_IEN BIT(13)
94#define TVE_CD_MON_END_IEN BIT(2)
95#define TVE_CD_SM_IEN BIT(1)
96#define TVE_CD_LM_IEN BIT(0)
97
98/* TVE_TST_MODE_REG */
99#define TVE_TVDAC_TEST_MODE_MASK (0x7 << 0)
100
101#define con_to_tve(x) container_of(x, struct imx_tve, connector)
Liu Ying032003c2016-07-08 17:40:58 +0800102#define imx_enc_to_tve(x) container_of(x, struct imx_tve, imx_encoder)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200103
104enum {
105 TVE_MODE_TVOUT,
106 TVE_MODE_VGA,
107};
108
109struct imx_tve {
110 struct drm_connector connector;
Liu Ying032003c2016-07-08 17:40:58 +0800111 struct imx_drm_encoder imx_encoder;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200112 struct device *dev;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200113 spinlock_t lock; /* register lock */
114 bool enabled;
115 int mode;
116
117 struct regmap *regmap;
118 struct regulator *dac_reg;
119 struct i2c_adapter *ddc;
120 struct clk *clk;
121 struct clk *di_sel_clk;
122 struct clk_hw clk_hw_di;
123 struct clk *di_clk;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200124};
125
126static void tve_lock(void *__tve)
Fabio Estevam5d78bf82013-07-16 02:16:14 -0300127__acquires(&tve->lock)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200128{
129 struct imx_tve *tve = __tve;
Quentin Lambert63bc51642014-08-04 21:07:07 +0200130
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200131 spin_lock(&tve->lock);
132}
133
134static void tve_unlock(void *__tve)
Fabio Estevam5d78bf82013-07-16 02:16:14 -0300135__releases(&tve->lock)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200136{
137 struct imx_tve *tve = __tve;
Quentin Lambert63bc51642014-08-04 21:07:07 +0200138
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200139 spin_unlock(&tve->lock);
140}
141
142static void tve_enable(struct imx_tve *tve)
143{
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200144 int ret;
145
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200146 if (!tve->enabled) {
Valentina Manea89bc5be2013-10-25 11:52:20 +0300147 tve->enabled = true;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200148 clk_prepare_enable(tve->clk);
149 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
150 TVE_IPU_CLK_EN | TVE_EN,
151 TVE_IPU_CLK_EN | TVE_EN);
152 }
153
154 /* clear interrupt status register */
155 regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
156
157 /* cable detection irq disabled in VGA mode, enabled in TVOUT mode */
158 if (tve->mode == TVE_MODE_VGA)
159 regmap_write(tve->regmap, TVE_INT_CONT_REG, 0);
160 else
161 regmap_write(tve->regmap, TVE_INT_CONT_REG,
Andreas Werner89911e52013-08-11 17:20:23 +0200162 TVE_CD_SM_IEN |
163 TVE_CD_LM_IEN |
164 TVE_CD_MON_END_IEN);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200165}
166
167static void tve_disable(struct imx_tve *tve)
168{
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200169 int ret;
170
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200171 if (tve->enabled) {
Valentina Manea89bc5be2013-10-25 11:52:20 +0300172 tve->enabled = false;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200173 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
174 TVE_IPU_CLK_EN | TVE_EN, 0);
175 clk_disable_unprepare(tve->clk);
176 }
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200177}
178
179static int tve_setup_tvout(struct imx_tve *tve)
180{
181 return -ENOTSUPP;
182}
183
184static int tve_setup_vga(struct imx_tve *tve)
185{
186 unsigned int mask;
187 unsigned int val;
188 int ret;
189
190 /* set gain to (1 + 10/128) to provide 0.7V peak-to-peak amplitude */
191 ret = regmap_update_bits(tve->regmap, TVE_TVDAC0_CONT_REG,
192 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200193 if (ret)
194 return ret;
195
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200196 ret = regmap_update_bits(tve->regmap, TVE_TVDAC1_CONT_REG,
197 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200198 if (ret)
199 return ret;
200
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200201 ret = regmap_update_bits(tve->regmap, TVE_TVDAC2_CONT_REG,
202 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200203 if (ret)
204 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200205
206 /* set configuration register */
207 mask = TVE_DATA_SOURCE_MASK | TVE_INP_VIDEO_FORM;
208 val = TVE_DATA_SOURCE_BUS2 | TVE_INP_YCBCR_444;
209 mask |= TVE_TV_STAND_MASK | TVE_P2I_CONV_EN;
210 val |= TVE_TV_STAND_HD_1080P30 | 0;
211 mask |= TVE_TV_OUT_MODE_MASK | TVE_SYNC_CH_0_EN;
212 val |= TVE_TV_OUT_RGB | TVE_SYNC_CH_0_EN;
213 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG, mask, val);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200214 if (ret)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200215 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200216
217 /* set test mode (as documented) */
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200218 return regmap_update_bits(tve->regmap, TVE_TST_MODE_REG,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200219 TVE_TVDAC_TEST_MODE_MASK, 1);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200220}
221
222static enum drm_connector_status imx_tve_connector_detect(
223 struct drm_connector *connector, bool force)
224{
225 return connector_status_connected;
226}
227
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200228static int imx_tve_connector_get_modes(struct drm_connector *connector)
229{
230 struct imx_tve *tve = con_to_tve(connector);
231 struct edid *edid;
232 int ret = 0;
233
234 if (!tve->ddc)
235 return 0;
236
237 edid = drm_get_edid(connector, tve->ddc);
238 if (edid) {
239 drm_mode_connector_update_edid_property(connector, edid);
240 ret = drm_add_edid_modes(connector, edid);
241 kfree(edid);
242 }
243
244 return ret;
245}
246
247static int imx_tve_connector_mode_valid(struct drm_connector *connector,
248 struct drm_display_mode *mode)
249{
250 struct imx_tve *tve = con_to_tve(connector);
251 unsigned long rate;
Russell Kingbaa68c42013-11-09 11:20:55 +0000252
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200253 /* pixel clock with 2x oversampling */
254 rate = clk_round_rate(tve->clk, 2000UL * mode->clock) / 2000;
255 if (rate == mode->clock)
256 return MODE_OK;
257
258 /* pixel clock without oversampling */
259 rate = clk_round_rate(tve->clk, 1000UL * mode->clock) / 1000;
260 if (rate == mode->clock)
261 return MODE_OK;
262
263 dev_warn(tve->dev, "ignoring mode %dx%d\n",
264 mode->hdisplay, mode->vdisplay);
265
266 return MODE_BAD;
267}
268
269static struct drm_encoder *imx_tve_connector_best_encoder(
270 struct drm_connector *connector)
271{
272 struct imx_tve *tve = con_to_tve(connector);
273
Liu Ying032003c2016-07-08 17:40:58 +0800274 return &tve->imx_encoder.encoder;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200275}
276
277static void imx_tve_encoder_dpms(struct drm_encoder *encoder, int mode)
278{
Liu Ying032003c2016-07-08 17:40:58 +0800279 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
280 struct imx_tve *tve = imx_enc_to_tve(imx_encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200281 int ret;
282
283 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
284 TVE_TV_OUT_MODE_MASK, TVE_TV_OUT_DISABLE);
285 if (ret < 0)
286 dev_err(tve->dev, "failed to disable TVOUT: %d\n", ret);
287}
288
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200289static void imx_tve_encoder_mode_set(struct drm_encoder *encoder,
Steve Longerbeameb10d632014-12-18 18:00:24 -0800290 struct drm_display_mode *orig_mode,
291 struct drm_display_mode *mode)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200292{
Liu Ying032003c2016-07-08 17:40:58 +0800293 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
294 struct imx_tve *tve = imx_enc_to_tve(imx_encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200295 unsigned long rounded_rate;
296 unsigned long rate;
297 int div = 1;
298 int ret;
299
300 /*
301 * FIXME
302 * we should try 4k * mode->clock first,
303 * and enable 4x oversampling for lower resolutions
304 */
305 rate = 2000UL * mode->clock;
306 clk_set_rate(tve->clk, rate);
307 rounded_rate = clk_get_rate(tve->clk);
308 if (rounded_rate >= rate)
309 div = 2;
310 clk_set_rate(tve->di_clk, rounded_rate / div);
311
312 ret = clk_set_parent(tve->di_sel_clk, tve->di_clk);
313 if (ret < 0) {
314 dev_err(tve->dev, "failed to set di_sel parent to tve_di: %d\n",
315 ret);
316 }
317
318 if (tve->mode == TVE_MODE_VGA)
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200319 ret = tve_setup_vga(tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200320 else
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200321 ret = tve_setup_tvout(tve);
322 if (ret)
323 dev_err(tve->dev, "failed to set configuration: %d\n", ret);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200324}
325
326static void imx_tve_encoder_commit(struct drm_encoder *encoder)
327{
Liu Ying032003c2016-07-08 17:40:58 +0800328 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
329 struct imx_tve *tve = imx_enc_to_tve(imx_encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200330
331 tve_enable(tve);
332}
333
334static void imx_tve_encoder_disable(struct drm_encoder *encoder)
335{
Liu Ying032003c2016-07-08 17:40:58 +0800336 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
337 struct imx_tve *tve = imx_enc_to_tve(imx_encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200338
339 tve_disable(tve);
340}
341
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100342static const struct drm_connector_funcs imx_tve_connector_funcs = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200343 .dpms = drm_helper_connector_dpms,
344 .fill_modes = drm_helper_probe_single_connector_modes,
345 .detect = imx_tve_connector_detect,
Russell King1b3f7672013-11-03 13:30:48 +0000346 .destroy = imx_drm_connector_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800347 .reset = drm_atomic_helper_connector_reset,
348 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
349 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200350};
351
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100352static const struct drm_connector_helper_funcs imx_tve_connector_helper_funcs = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200353 .get_modes = imx_tve_connector_get_modes,
354 .best_encoder = imx_tve_connector_best_encoder,
355 .mode_valid = imx_tve_connector_mode_valid,
356};
357
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100358static const struct drm_encoder_funcs imx_tve_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000359 .destroy = imx_drm_encoder_destroy,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200360};
361
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100362static const struct drm_encoder_helper_funcs imx_tve_encoder_helper_funcs = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200363 .dpms = imx_tve_encoder_dpms,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200364 .mode_set = imx_tve_encoder_mode_set,
365 .commit = imx_tve_encoder_commit,
366 .disable = imx_tve_encoder_disable,
367};
368
369static irqreturn_t imx_tve_irq_handler(int irq, void *data)
370{
371 struct imx_tve *tve = data;
372 unsigned int val;
373
374 regmap_read(tve->regmap, TVE_STAT_REG, &val);
375
376 /* clear interrupt status register */
377 regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
378
379 return IRQ_HANDLED;
380}
381
382static unsigned long clk_tve_di_recalc_rate(struct clk_hw *hw,
383 unsigned long parent_rate)
384{
385 struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
386 unsigned int val;
387 int ret;
388
389 ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
390 if (ret < 0)
391 return 0;
392
393 switch (val & TVE_DAC_SAMP_RATE_MASK) {
394 case TVE_DAC_DIV4_RATE:
395 return parent_rate / 4;
396 case TVE_DAC_DIV2_RATE:
397 return parent_rate / 2;
398 case TVE_DAC_FULL_RATE:
399 default:
400 return parent_rate;
401 }
402
403 return 0;
404}
405
406static long clk_tve_di_round_rate(struct clk_hw *hw, unsigned long rate,
407 unsigned long *prate)
408{
409 unsigned long div;
410
411 div = *prate / rate;
412 if (div >= 4)
413 return *prate / 4;
414 else if (div >= 2)
415 return *prate / 2;
Catalina Mocanu7557b6e2014-09-24 14:27:36 -0700416 return *prate;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200417}
418
419static int clk_tve_di_set_rate(struct clk_hw *hw, unsigned long rate,
420 unsigned long parent_rate)
421{
422 struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
423 unsigned long div;
424 u32 val;
425 int ret;
426
427 div = parent_rate / rate;
428 if (div >= 4)
429 val = TVE_DAC_DIV4_RATE;
430 else if (div >= 2)
431 val = TVE_DAC_DIV2_RATE;
432 else
433 val = TVE_DAC_FULL_RATE;
434
Andreas Werner89911e52013-08-11 17:20:23 +0200435 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
436 TVE_DAC_SAMP_RATE_MASK, val);
437
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200438 if (ret < 0) {
439 dev_err(tve->dev, "failed to set divider: %d\n", ret);
440 return ret;
441 }
442
443 return 0;
444}
445
446static struct clk_ops clk_tve_di_ops = {
447 .round_rate = clk_tve_di_round_rate,
448 .set_rate = clk_tve_di_set_rate,
449 .recalc_rate = clk_tve_di_recalc_rate,
450};
451
452static int tve_clk_init(struct imx_tve *tve, void __iomem *base)
453{
454 const char *tve_di_parent[1];
455 struct clk_init_data init = {
456 .name = "tve_di",
457 .ops = &clk_tve_di_ops,
458 .num_parents = 1,
459 .flags = 0,
460 };
461
462 tve_di_parent[0] = __clk_get_name(tve->clk);
463 init.parent_names = (const char **)&tve_di_parent;
464
465 tve->clk_hw_di.init = &init;
466 tve->di_clk = clk_register(tve->dev, &tve->clk_hw_di);
467 if (IS_ERR(tve->di_clk)) {
468 dev_err(tve->dev, "failed to register TVE output clock: %ld\n",
469 PTR_ERR(tve->di_clk));
470 return PTR_ERR(tve->di_clk);
471 }
472
473 return 0;
474}
475
Russell King1b3f7672013-11-03 13:30:48 +0000476static int imx_tve_register(struct drm_device *drm, struct imx_tve *tve)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200477{
Russell Kingf2d66aa2013-11-03 15:52:16 +0000478 int encoder_type;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200479 int ret;
480
Russell Kingf2d66aa2013-11-03 15:52:16 +0000481 encoder_type = tve->mode == TVE_MODE_VGA ?
482 DRM_MODE_ENCODER_DAC : DRM_MODE_ENCODER_TVDAC;
483
Liu Ying032003c2016-07-08 17:40:58 +0800484 ret = imx_drm_encoder_parse_of(drm, &tve->imx_encoder.encoder,
Russell King1b3f7672013-11-03 13:30:48 +0000485 tve->dev->of_node);
486 if (ret)
487 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200488
Liu Ying032003c2016-07-08 17:40:58 +0800489 drm_encoder_helper_add(&tve->imx_encoder.encoder,
490 &imx_tve_encoder_helper_funcs);
491 drm_encoder_init(drm, &tve->imx_encoder.encoder, &imx_tve_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200492 encoder_type, NULL);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200493
494 drm_connector_helper_add(&tve->connector,
495 &imx_tve_connector_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000496 drm_connector_init(drm, &tve->connector, &imx_tve_connector_funcs,
497 DRM_MODE_CONNECTOR_VGA);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200498
Liu Ying032003c2016-07-08 17:40:58 +0800499 drm_mode_connector_attach_encoder(&tve->connector,
500 &tve->imx_encoder.encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200501
502 return 0;
503}
504
505static bool imx_tve_readable_reg(struct device *dev, unsigned int reg)
506{
507 return (reg % 4 == 0) && (reg <= 0xdc);
508}
509
510static struct regmap_config tve_regmap_config = {
511 .reg_bits = 32,
512 .val_bits = 32,
513 .reg_stride = 4,
514
515 .readable_reg = imx_tve_readable_reg,
516
517 .lock = tve_lock,
518 .unlock = tve_unlock,
519
520 .max_register = 0xdc,
521};
522
Aybuke Ozdemir8684ba72014-09-27 16:16:02 +0300523static const char * const imx_tve_modes[] = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200524 [TVE_MODE_TVOUT] = "tvout",
525 [TVE_MODE_VGA] = "vga",
526};
527
Liu Ying7fc6cb22013-12-24 10:17:44 +0800528static const int of_get_tve_mode(struct device_node *np)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200529{
530 const char *bm;
531 int ret, i;
532
533 ret = of_property_read_string(np, "fsl,tve-mode", &bm);
534 if (ret < 0)
535 return ret;
536
537 for (i = 0; i < ARRAY_SIZE(imx_tve_modes); i++)
538 if (!strcasecmp(bm, imx_tve_modes[i]))
539 return i;
540
541 return -EINVAL;
542}
543
Russell King17b50012013-11-03 11:23:34 +0000544static int imx_tve_bind(struct device *dev, struct device *master, void *data)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200545{
Russell King17b50012013-11-03 11:23:34 +0000546 struct platform_device *pdev = to_platform_device(dev);
Russell King1b3f7672013-11-03 13:30:48 +0000547 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000548 struct device_node *np = dev->of_node;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200549 struct device_node *ddc_node;
550 struct imx_tve *tve;
551 struct resource *res;
552 void __iomem *base;
553 unsigned int val;
554 int irq;
555 int ret;
556
Russell King17b50012013-11-03 11:23:34 +0000557 tve = devm_kzalloc(dev, sizeof(*tve), GFP_KERNEL);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200558 if (!tve)
559 return -ENOMEM;
560
Russell King17b50012013-11-03 11:23:34 +0000561 tve->dev = dev;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200562 spin_lock_init(&tve->lock);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200563
Shawn Guoa3fe9642014-04-10 14:19:05 +0800564 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200565 if (ddc_node) {
566 tve->ddc = of_find_i2c_adapter_by_node(ddc_node);
567 of_node_put(ddc_node);
568 }
569
570 tve->mode = of_get_tve_mode(np);
571 if (tve->mode != TVE_MODE_VGA) {
Russell King17b50012013-11-03 11:23:34 +0000572 dev_err(dev, "only VGA mode supported, currently\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200573 return -EINVAL;
574 }
575
576 if (tve->mode == TVE_MODE_VGA) {
Andreas Werner89911e52013-08-11 17:20:23 +0200577 ret = of_property_read_u32(np, "fsl,hsync-pin",
Liu Ying032003c2016-07-08 17:40:58 +0800578 &tve->imx_encoder.di_hsync_pin);
Andreas Werner89911e52013-08-11 17:20:23 +0200579
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200580 if (ret < 0) {
Russell King17b50012013-11-03 11:23:34 +0000581 dev_err(dev, "failed to get vsync pin\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200582 return ret;
583 }
584
Andreas Werner89911e52013-08-11 17:20:23 +0200585 ret |= of_property_read_u32(np, "fsl,vsync-pin",
Liu Ying032003c2016-07-08 17:40:58 +0800586 &tve->imx_encoder.di_vsync_pin);
Andreas Werner89911e52013-08-11 17:20:23 +0200587
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200588 if (ret < 0) {
Russell King17b50012013-11-03 11:23:34 +0000589 dev_err(dev, "failed to get vsync pin\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200590 return ret;
591 }
Liu Ying032003c2016-07-08 17:40:58 +0800592
593 tve->imx_encoder.bus_format = MEDIA_BUS_FMT_GBR888_1X24;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200594 }
595
596 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Russell King17b50012013-11-03 11:23:34 +0000597 base = devm_ioremap_resource(dev, res);
Laurent Navet9b43b562013-05-02 13:41:41 +0200598 if (IS_ERR(base))
599 return PTR_ERR(base);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200600
601 tve_regmap_config.lock_arg = tve;
Russell King17b50012013-11-03 11:23:34 +0000602 tve->regmap = devm_regmap_init_mmio_clk(dev, "tve", base,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200603 &tve_regmap_config);
604 if (IS_ERR(tve->regmap)) {
Russell King17b50012013-11-03 11:23:34 +0000605 dev_err(dev, "failed to init regmap: %ld\n",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200606 PTR_ERR(tve->regmap));
607 return PTR_ERR(tve->regmap);
608 }
609
610 irq = platform_get_irq(pdev, 0);
611 if (irq < 0) {
Russell King17b50012013-11-03 11:23:34 +0000612 dev_err(dev, "failed to get irq\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200613 return irq;
614 }
615
Russell King17b50012013-11-03 11:23:34 +0000616 ret = devm_request_threaded_irq(dev, irq, NULL,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200617 imx_tve_irq_handler, IRQF_ONESHOT,
618 "imx-tve", tve);
619 if (ret < 0) {
Russell King17b50012013-11-03 11:23:34 +0000620 dev_err(dev, "failed to request irq: %d\n", ret);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200621 return ret;
622 }
623
Russell King17b50012013-11-03 11:23:34 +0000624 tve->dac_reg = devm_regulator_get(dev, "dac");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200625 if (!IS_ERR(tve->dac_reg)) {
626 regulator_set_voltage(tve->dac_reg, 2750000, 2750000);
Fabio Estevamc7b0cf32013-05-21 11:24:44 -0300627 ret = regulator_enable(tve->dac_reg);
628 if (ret)
629 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200630 }
631
Russell King17b50012013-11-03 11:23:34 +0000632 tve->clk = devm_clk_get(dev, "tve");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200633 if (IS_ERR(tve->clk)) {
Russell King17b50012013-11-03 11:23:34 +0000634 dev_err(dev, "failed to get high speed tve clock: %ld\n",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200635 PTR_ERR(tve->clk));
636 return PTR_ERR(tve->clk);
637 }
638
639 /* this is the IPU DI clock input selector, can be parented to tve_di */
Russell King17b50012013-11-03 11:23:34 +0000640 tve->di_sel_clk = devm_clk_get(dev, "di_sel");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200641 if (IS_ERR(tve->di_sel_clk)) {
Russell King17b50012013-11-03 11:23:34 +0000642 dev_err(dev, "failed to get ipu di mux clock: %ld\n",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200643 PTR_ERR(tve->di_sel_clk));
644 return PTR_ERR(tve->di_sel_clk);
645 }
646
647 ret = tve_clk_init(tve, base);
648 if (ret < 0)
649 return ret;
650
651 ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
652 if (ret < 0) {
Rene Kolarikf582d9a2014-10-09 20:29:32 +0200653 dev_err(dev, "failed to read configuration register: %d\n",
654 ret);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200655 return ret;
656 }
657 if (val != 0x00100000) {
Russell King17b50012013-11-03 11:23:34 +0000658 dev_err(dev, "configuration register default value indicates this is not a TVEv2\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200659 return -ENODEV;
Joe Perchesa22526e2013-10-10 16:07:59 -0700660 }
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200661
662 /* disable cable detection for VGA mode */
663 ret = regmap_write(tve->regmap, TVE_CD_CONT_REG, 0);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200664 if (ret)
665 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200666
Russell King1b3f7672013-11-03 13:30:48 +0000667 ret = imx_tve_register(drm, tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200668 if (ret)
669 return ret;
670
Russell King17b50012013-11-03 11:23:34 +0000671 dev_set_drvdata(dev, tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200672
673 return 0;
674}
675
Russell King17b50012013-11-03 11:23:34 +0000676static void imx_tve_unbind(struct device *dev, struct device *master,
677 void *data)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200678{
Russell King17b50012013-11-03 11:23:34 +0000679 struct imx_tve *tve = dev_get_drvdata(dev);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200680
Russell King1b3f7672013-11-03 13:30:48 +0000681 tve->connector.funcs->destroy(&tve->connector);
Liu Ying032003c2016-07-08 17:40:58 +0800682 tve->imx_encoder.encoder.funcs->destroy(&tve->imx_encoder.encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200683
684 if (!IS_ERR(tve->dac_reg))
685 regulator_disable(tve->dac_reg);
Russell King17b50012013-11-03 11:23:34 +0000686}
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200687
Russell King17b50012013-11-03 11:23:34 +0000688static const struct component_ops imx_tve_ops = {
689 .bind = imx_tve_bind,
690 .unbind = imx_tve_unbind,
691};
692
693static int imx_tve_probe(struct platform_device *pdev)
694{
695 return component_add(&pdev->dev, &imx_tve_ops);
696}
697
698static int imx_tve_remove(struct platform_device *pdev)
699{
700 component_del(&pdev->dev, &imx_tve_ops);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200701 return 0;
702}
703
704static const struct of_device_id imx_tve_dt_ids[] = {
705 { .compatible = "fsl,imx53-tve", },
706 { /* sentinel */ }
707};
Luis de Bethencourt5e4789d2015-11-30 15:02:44 +0000708MODULE_DEVICE_TABLE(of, imx_tve_dt_ids);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200709
710static struct platform_driver imx_tve_driver = {
711 .probe = imx_tve_probe,
712 .remove = imx_tve_remove,
713 .driver = {
714 .of_match_table = imx_tve_dt_ids,
715 .name = "imx-tve",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200716 },
717};
718
719module_platform_driver(imx_tve_driver);
720
721MODULE_DESCRIPTION("i.MX Television Encoder driver");
722MODULE_AUTHOR("Philipp Zabel, Pengutronix");
723MODULE_LICENSE("GPL");
Fabio Estevam52db752c2013-08-18 21:40:04 -0300724MODULE_ALIAS("platform:imx-tve");