blob: cd92aac5c3bcba231ba9d955236f99bea0800655 [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,
Liu Yingf6e396e2016-07-08 17:41:01 +0800150 TVE_EN, TVE_EN);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200151 }
152
153 /* clear interrupt status register */
154 regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
155
156 /* cable detection irq disabled in VGA mode, enabled in TVOUT mode */
157 if (tve->mode == TVE_MODE_VGA)
158 regmap_write(tve->regmap, TVE_INT_CONT_REG, 0);
159 else
160 regmap_write(tve->regmap, TVE_INT_CONT_REG,
Andreas Werner89911e52013-08-11 17:20:23 +0200161 TVE_CD_SM_IEN |
162 TVE_CD_LM_IEN |
163 TVE_CD_MON_END_IEN);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200164}
165
166static void tve_disable(struct imx_tve *tve)
167{
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200168 int ret;
169
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200170 if (tve->enabled) {
Valentina Manea89bc5be2013-10-25 11:52:20 +0300171 tve->enabled = false;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200172 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
Liu Yingf6e396e2016-07-08 17:41:01 +0800173 TVE_EN, 0);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200174 clk_disable_unprepare(tve->clk);
175 }
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200176}
177
178static int tve_setup_tvout(struct imx_tve *tve)
179{
180 return -ENOTSUPP;
181}
182
183static int tve_setup_vga(struct imx_tve *tve)
184{
185 unsigned int mask;
186 unsigned int val;
187 int ret;
188
189 /* set gain to (1 + 10/128) to provide 0.7V peak-to-peak amplitude */
190 ret = regmap_update_bits(tve->regmap, TVE_TVDAC0_CONT_REG,
191 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200192 if (ret)
193 return ret;
194
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200195 ret = regmap_update_bits(tve->regmap, TVE_TVDAC1_CONT_REG,
196 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200197 if (ret)
198 return ret;
199
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200200 ret = regmap_update_bits(tve->regmap, TVE_TVDAC2_CONT_REG,
201 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200202 if (ret)
203 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200204
205 /* set configuration register */
206 mask = TVE_DATA_SOURCE_MASK | TVE_INP_VIDEO_FORM;
207 val = TVE_DATA_SOURCE_BUS2 | TVE_INP_YCBCR_444;
208 mask |= TVE_TV_STAND_MASK | TVE_P2I_CONV_EN;
209 val |= TVE_TV_STAND_HD_1080P30 | 0;
210 mask |= TVE_TV_OUT_MODE_MASK | TVE_SYNC_CH_0_EN;
211 val |= TVE_TV_OUT_RGB | TVE_SYNC_CH_0_EN;
212 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG, mask, val);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200213 if (ret)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200214 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200215
216 /* set test mode (as documented) */
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200217 return regmap_update_bits(tve->regmap, TVE_TST_MODE_REG,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200218 TVE_TVDAC_TEST_MODE_MASK, 1);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200219}
220
221static enum drm_connector_status imx_tve_connector_detect(
222 struct drm_connector *connector, bool force)
223{
224 return connector_status_connected;
225}
226
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200227static int imx_tve_connector_get_modes(struct drm_connector *connector)
228{
229 struct imx_tve *tve = con_to_tve(connector);
230 struct edid *edid;
231 int ret = 0;
232
233 if (!tve->ddc)
234 return 0;
235
236 edid = drm_get_edid(connector, tve->ddc);
237 if (edid) {
238 drm_mode_connector_update_edid_property(connector, edid);
239 ret = drm_add_edid_modes(connector, edid);
240 kfree(edid);
241 }
242
243 return ret;
244}
245
246static int imx_tve_connector_mode_valid(struct drm_connector *connector,
247 struct drm_display_mode *mode)
248{
249 struct imx_tve *tve = con_to_tve(connector);
250 unsigned long rate;
Russell Kingbaa68c42013-11-09 11:20:55 +0000251
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200252 /* pixel clock with 2x oversampling */
253 rate = clk_round_rate(tve->clk, 2000UL * mode->clock) / 2000;
254 if (rate == mode->clock)
255 return MODE_OK;
256
257 /* pixel clock without oversampling */
258 rate = clk_round_rate(tve->clk, 1000UL * mode->clock) / 1000;
259 if (rate == mode->clock)
260 return MODE_OK;
261
262 dev_warn(tve->dev, "ignoring mode %dx%d\n",
263 mode->hdisplay, mode->vdisplay);
264
265 return MODE_BAD;
266}
267
268static struct drm_encoder *imx_tve_connector_best_encoder(
269 struct drm_connector *connector)
270{
271 struct imx_tve *tve = con_to_tve(connector);
272
Liu Ying032003c2016-07-08 17:40:58 +0800273 return &tve->imx_encoder.encoder;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200274}
275
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200276static void imx_tve_encoder_mode_set(struct drm_encoder *encoder,
Steve Longerbeameb10d632014-12-18 18:00:24 -0800277 struct drm_display_mode *orig_mode,
278 struct drm_display_mode *mode)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200279{
Liu Ying032003c2016-07-08 17:40:58 +0800280 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
281 struct imx_tve *tve = imx_enc_to_tve(imx_encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200282 unsigned long rounded_rate;
283 unsigned long rate;
284 int div = 1;
285 int ret;
286
287 /*
288 * FIXME
289 * we should try 4k * mode->clock first,
290 * and enable 4x oversampling for lower resolutions
291 */
292 rate = 2000UL * mode->clock;
293 clk_set_rate(tve->clk, rate);
294 rounded_rate = clk_get_rate(tve->clk);
295 if (rounded_rate >= rate)
296 div = 2;
297 clk_set_rate(tve->di_clk, rounded_rate / div);
298
299 ret = clk_set_parent(tve->di_sel_clk, tve->di_clk);
300 if (ret < 0) {
301 dev_err(tve->dev, "failed to set di_sel parent to tve_di: %d\n",
302 ret);
303 }
304
Liu Yingf6e396e2016-07-08 17:41:01 +0800305 regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
306 TVE_IPU_CLK_EN, TVE_IPU_CLK_EN);
307
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200308 if (tve->mode == TVE_MODE_VGA)
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200309 ret = tve_setup_vga(tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200310 else
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200311 ret = tve_setup_tvout(tve);
312 if (ret)
313 dev_err(tve->dev, "failed to set configuration: %d\n", ret);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200314}
315
Liu Yingf6e396e2016-07-08 17:41:01 +0800316static void imx_tve_encoder_enable(struct drm_encoder *encoder)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200317{
Liu Ying032003c2016-07-08 17:40:58 +0800318 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
319 struct imx_tve *tve = imx_enc_to_tve(imx_encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200320
321 tve_enable(tve);
322}
323
324static void imx_tve_encoder_disable(struct drm_encoder *encoder)
325{
Liu Ying032003c2016-07-08 17:40:58 +0800326 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
327 struct imx_tve *tve = imx_enc_to_tve(imx_encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200328
329 tve_disable(tve);
330}
331
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100332static const struct drm_connector_funcs imx_tve_connector_funcs = {
Liu Yingf6e396e2016-07-08 17:41:01 +0800333 .dpms = drm_atomic_helper_connector_dpms,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200334 .fill_modes = drm_helper_probe_single_connector_modes,
335 .detect = imx_tve_connector_detect,
Russell King1b3f7672013-11-03 13:30:48 +0000336 .destroy = imx_drm_connector_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800337 .reset = drm_atomic_helper_connector_reset,
338 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
339 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200340};
341
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100342static const struct drm_connector_helper_funcs imx_tve_connector_helper_funcs = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200343 .get_modes = imx_tve_connector_get_modes,
344 .best_encoder = imx_tve_connector_best_encoder,
345 .mode_valid = imx_tve_connector_mode_valid,
346};
347
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100348static const struct drm_encoder_funcs imx_tve_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000349 .destroy = imx_drm_encoder_destroy,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200350};
351
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100352static const struct drm_encoder_helper_funcs imx_tve_encoder_helper_funcs = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200353 .mode_set = imx_tve_encoder_mode_set,
Liu Yingf6e396e2016-07-08 17:41:01 +0800354 .enable = imx_tve_encoder_enable,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200355 .disable = imx_tve_encoder_disable,
356};
357
358static irqreturn_t imx_tve_irq_handler(int irq, void *data)
359{
360 struct imx_tve *tve = data;
361 unsigned int val;
362
363 regmap_read(tve->regmap, TVE_STAT_REG, &val);
364
365 /* clear interrupt status register */
366 regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
367
368 return IRQ_HANDLED;
369}
370
371static unsigned long clk_tve_di_recalc_rate(struct clk_hw *hw,
372 unsigned long parent_rate)
373{
374 struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
375 unsigned int val;
376 int ret;
377
378 ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
379 if (ret < 0)
380 return 0;
381
382 switch (val & TVE_DAC_SAMP_RATE_MASK) {
383 case TVE_DAC_DIV4_RATE:
384 return parent_rate / 4;
385 case TVE_DAC_DIV2_RATE:
386 return parent_rate / 2;
387 case TVE_DAC_FULL_RATE:
388 default:
389 return parent_rate;
390 }
391
392 return 0;
393}
394
395static long clk_tve_di_round_rate(struct clk_hw *hw, unsigned long rate,
396 unsigned long *prate)
397{
398 unsigned long div;
399
400 div = *prate / rate;
401 if (div >= 4)
402 return *prate / 4;
403 else if (div >= 2)
404 return *prate / 2;
Catalina Mocanu7557b6e2014-09-24 14:27:36 -0700405 return *prate;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200406}
407
408static int clk_tve_di_set_rate(struct clk_hw *hw, unsigned long rate,
409 unsigned long parent_rate)
410{
411 struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
412 unsigned long div;
413 u32 val;
414 int ret;
415
416 div = parent_rate / rate;
417 if (div >= 4)
418 val = TVE_DAC_DIV4_RATE;
419 else if (div >= 2)
420 val = TVE_DAC_DIV2_RATE;
421 else
422 val = TVE_DAC_FULL_RATE;
423
Andreas Werner89911e52013-08-11 17:20:23 +0200424 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
425 TVE_DAC_SAMP_RATE_MASK, val);
426
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200427 if (ret < 0) {
428 dev_err(tve->dev, "failed to set divider: %d\n", ret);
429 return ret;
430 }
431
432 return 0;
433}
434
435static struct clk_ops clk_tve_di_ops = {
436 .round_rate = clk_tve_di_round_rate,
437 .set_rate = clk_tve_di_set_rate,
438 .recalc_rate = clk_tve_di_recalc_rate,
439};
440
441static int tve_clk_init(struct imx_tve *tve, void __iomem *base)
442{
443 const char *tve_di_parent[1];
444 struct clk_init_data init = {
445 .name = "tve_di",
446 .ops = &clk_tve_di_ops,
447 .num_parents = 1,
448 .flags = 0,
449 };
450
451 tve_di_parent[0] = __clk_get_name(tve->clk);
452 init.parent_names = (const char **)&tve_di_parent;
453
454 tve->clk_hw_di.init = &init;
455 tve->di_clk = clk_register(tve->dev, &tve->clk_hw_di);
456 if (IS_ERR(tve->di_clk)) {
457 dev_err(tve->dev, "failed to register TVE output clock: %ld\n",
458 PTR_ERR(tve->di_clk));
459 return PTR_ERR(tve->di_clk);
460 }
461
462 return 0;
463}
464
Russell King1b3f7672013-11-03 13:30:48 +0000465static int imx_tve_register(struct drm_device *drm, struct imx_tve *tve)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200466{
Russell Kingf2d66aa2013-11-03 15:52:16 +0000467 int encoder_type;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200468 int ret;
469
Russell Kingf2d66aa2013-11-03 15:52:16 +0000470 encoder_type = tve->mode == TVE_MODE_VGA ?
471 DRM_MODE_ENCODER_DAC : DRM_MODE_ENCODER_TVDAC;
472
Liu Ying032003c2016-07-08 17:40:58 +0800473 ret = imx_drm_encoder_parse_of(drm, &tve->imx_encoder.encoder,
Russell King1b3f7672013-11-03 13:30:48 +0000474 tve->dev->of_node);
475 if (ret)
476 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200477
Liu Ying032003c2016-07-08 17:40:58 +0800478 drm_encoder_helper_add(&tve->imx_encoder.encoder,
479 &imx_tve_encoder_helper_funcs);
480 drm_encoder_init(drm, &tve->imx_encoder.encoder, &imx_tve_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200481 encoder_type, NULL);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200482
483 drm_connector_helper_add(&tve->connector,
484 &imx_tve_connector_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000485 drm_connector_init(drm, &tve->connector, &imx_tve_connector_funcs,
486 DRM_MODE_CONNECTOR_VGA);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200487
Liu Ying032003c2016-07-08 17:40:58 +0800488 drm_mode_connector_attach_encoder(&tve->connector,
489 &tve->imx_encoder.encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200490
491 return 0;
492}
493
494static bool imx_tve_readable_reg(struct device *dev, unsigned int reg)
495{
496 return (reg % 4 == 0) && (reg <= 0xdc);
497}
498
499static struct regmap_config tve_regmap_config = {
500 .reg_bits = 32,
501 .val_bits = 32,
502 .reg_stride = 4,
503
504 .readable_reg = imx_tve_readable_reg,
505
506 .lock = tve_lock,
507 .unlock = tve_unlock,
508
509 .max_register = 0xdc,
510};
511
Aybuke Ozdemir8684ba72014-09-27 16:16:02 +0300512static const char * const imx_tve_modes[] = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200513 [TVE_MODE_TVOUT] = "tvout",
514 [TVE_MODE_VGA] = "vga",
515};
516
Liu Ying7fc6cb22013-12-24 10:17:44 +0800517static const int of_get_tve_mode(struct device_node *np)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200518{
519 const char *bm;
520 int ret, i;
521
522 ret = of_property_read_string(np, "fsl,tve-mode", &bm);
523 if (ret < 0)
524 return ret;
525
526 for (i = 0; i < ARRAY_SIZE(imx_tve_modes); i++)
527 if (!strcasecmp(bm, imx_tve_modes[i]))
528 return i;
529
530 return -EINVAL;
531}
532
Russell King17b50012013-11-03 11:23:34 +0000533static int imx_tve_bind(struct device *dev, struct device *master, void *data)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200534{
Russell King17b50012013-11-03 11:23:34 +0000535 struct platform_device *pdev = to_platform_device(dev);
Russell King1b3f7672013-11-03 13:30:48 +0000536 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000537 struct device_node *np = dev->of_node;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200538 struct device_node *ddc_node;
539 struct imx_tve *tve;
540 struct resource *res;
541 void __iomem *base;
542 unsigned int val;
543 int irq;
544 int ret;
545
Russell King17b50012013-11-03 11:23:34 +0000546 tve = devm_kzalloc(dev, sizeof(*tve), GFP_KERNEL);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200547 if (!tve)
548 return -ENOMEM;
549
Russell King17b50012013-11-03 11:23:34 +0000550 tve->dev = dev;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200551 spin_lock_init(&tve->lock);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200552
Shawn Guoa3fe9642014-04-10 14:19:05 +0800553 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200554 if (ddc_node) {
555 tve->ddc = of_find_i2c_adapter_by_node(ddc_node);
556 of_node_put(ddc_node);
557 }
558
559 tve->mode = of_get_tve_mode(np);
560 if (tve->mode != TVE_MODE_VGA) {
Russell King17b50012013-11-03 11:23:34 +0000561 dev_err(dev, "only VGA mode supported, currently\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200562 return -EINVAL;
563 }
564
565 if (tve->mode == TVE_MODE_VGA) {
Andreas Werner89911e52013-08-11 17:20:23 +0200566 ret = of_property_read_u32(np, "fsl,hsync-pin",
Liu Ying032003c2016-07-08 17:40:58 +0800567 &tve->imx_encoder.di_hsync_pin);
Andreas Werner89911e52013-08-11 17:20:23 +0200568
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200569 if (ret < 0) {
Russell King17b50012013-11-03 11:23:34 +0000570 dev_err(dev, "failed to get vsync pin\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200571 return ret;
572 }
573
Andreas Werner89911e52013-08-11 17:20:23 +0200574 ret |= of_property_read_u32(np, "fsl,vsync-pin",
Liu Ying032003c2016-07-08 17:40:58 +0800575 &tve->imx_encoder.di_vsync_pin);
Andreas Werner89911e52013-08-11 17:20:23 +0200576
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200577 if (ret < 0) {
Russell King17b50012013-11-03 11:23:34 +0000578 dev_err(dev, "failed to get vsync pin\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200579 return ret;
580 }
Liu Ying032003c2016-07-08 17:40:58 +0800581
582 tve->imx_encoder.bus_format = MEDIA_BUS_FMT_GBR888_1X24;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200583 }
584
585 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Russell King17b50012013-11-03 11:23:34 +0000586 base = devm_ioremap_resource(dev, res);
Laurent Navet9b43b562013-05-02 13:41:41 +0200587 if (IS_ERR(base))
588 return PTR_ERR(base);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200589
590 tve_regmap_config.lock_arg = tve;
Russell King17b50012013-11-03 11:23:34 +0000591 tve->regmap = devm_regmap_init_mmio_clk(dev, "tve", base,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200592 &tve_regmap_config);
593 if (IS_ERR(tve->regmap)) {
Russell King17b50012013-11-03 11:23:34 +0000594 dev_err(dev, "failed to init regmap: %ld\n",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200595 PTR_ERR(tve->regmap));
596 return PTR_ERR(tve->regmap);
597 }
598
599 irq = platform_get_irq(pdev, 0);
600 if (irq < 0) {
Russell King17b50012013-11-03 11:23:34 +0000601 dev_err(dev, "failed to get irq\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200602 return irq;
603 }
604
Russell King17b50012013-11-03 11:23:34 +0000605 ret = devm_request_threaded_irq(dev, irq, NULL,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200606 imx_tve_irq_handler, IRQF_ONESHOT,
607 "imx-tve", tve);
608 if (ret < 0) {
Russell King17b50012013-11-03 11:23:34 +0000609 dev_err(dev, "failed to request irq: %d\n", ret);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200610 return ret;
611 }
612
Russell King17b50012013-11-03 11:23:34 +0000613 tve->dac_reg = devm_regulator_get(dev, "dac");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200614 if (!IS_ERR(tve->dac_reg)) {
615 regulator_set_voltage(tve->dac_reg, 2750000, 2750000);
Fabio Estevamc7b0cf32013-05-21 11:24:44 -0300616 ret = regulator_enable(tve->dac_reg);
617 if (ret)
618 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200619 }
620
Russell King17b50012013-11-03 11:23:34 +0000621 tve->clk = devm_clk_get(dev, "tve");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200622 if (IS_ERR(tve->clk)) {
Russell King17b50012013-11-03 11:23:34 +0000623 dev_err(dev, "failed to get high speed tve clock: %ld\n",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200624 PTR_ERR(tve->clk));
625 return PTR_ERR(tve->clk);
626 }
627
628 /* this is the IPU DI clock input selector, can be parented to tve_di */
Russell King17b50012013-11-03 11:23:34 +0000629 tve->di_sel_clk = devm_clk_get(dev, "di_sel");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200630 if (IS_ERR(tve->di_sel_clk)) {
Russell King17b50012013-11-03 11:23:34 +0000631 dev_err(dev, "failed to get ipu di mux clock: %ld\n",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200632 PTR_ERR(tve->di_sel_clk));
633 return PTR_ERR(tve->di_sel_clk);
634 }
635
636 ret = tve_clk_init(tve, base);
637 if (ret < 0)
638 return ret;
639
640 ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
641 if (ret < 0) {
Rene Kolarikf582d9a2014-10-09 20:29:32 +0200642 dev_err(dev, "failed to read configuration register: %d\n",
643 ret);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200644 return ret;
645 }
646 if (val != 0x00100000) {
Russell King17b50012013-11-03 11:23:34 +0000647 dev_err(dev, "configuration register default value indicates this is not a TVEv2\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200648 return -ENODEV;
Joe Perchesa22526e2013-10-10 16:07:59 -0700649 }
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200650
651 /* disable cable detection for VGA mode */
652 ret = regmap_write(tve->regmap, TVE_CD_CONT_REG, 0);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200653 if (ret)
654 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200655
Russell King1b3f7672013-11-03 13:30:48 +0000656 ret = imx_tve_register(drm, tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200657 if (ret)
658 return ret;
659
Russell King17b50012013-11-03 11:23:34 +0000660 dev_set_drvdata(dev, tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200661
662 return 0;
663}
664
Russell King17b50012013-11-03 11:23:34 +0000665static void imx_tve_unbind(struct device *dev, struct device *master,
666 void *data)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200667{
Russell King17b50012013-11-03 11:23:34 +0000668 struct imx_tve *tve = dev_get_drvdata(dev);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200669
Russell King1b3f7672013-11-03 13:30:48 +0000670 tve->connector.funcs->destroy(&tve->connector);
Liu Ying032003c2016-07-08 17:40:58 +0800671 tve->imx_encoder.encoder.funcs->destroy(&tve->imx_encoder.encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200672
673 if (!IS_ERR(tve->dac_reg))
674 regulator_disable(tve->dac_reg);
Russell King17b50012013-11-03 11:23:34 +0000675}
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200676
Russell King17b50012013-11-03 11:23:34 +0000677static const struct component_ops imx_tve_ops = {
678 .bind = imx_tve_bind,
679 .unbind = imx_tve_unbind,
680};
681
682static int imx_tve_probe(struct platform_device *pdev)
683{
684 return component_add(&pdev->dev, &imx_tve_ops);
685}
686
687static int imx_tve_remove(struct platform_device *pdev)
688{
689 component_del(&pdev->dev, &imx_tve_ops);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200690 return 0;
691}
692
693static const struct of_device_id imx_tve_dt_ids[] = {
694 { .compatible = "fsl,imx53-tve", },
695 { /* sentinel */ }
696};
Luis de Bethencourt5e4789d2015-11-30 15:02:44 +0000697MODULE_DEVICE_TABLE(of, imx_tve_dt_ids);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200698
699static struct platform_driver imx_tve_driver = {
700 .probe = imx_tve_probe,
701 .remove = imx_tve_remove,
702 .driver = {
703 .of_match_table = imx_tve_dt_ids,
704 .name = "imx-tve",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200705 },
706};
707
708module_platform_driver(imx_tve_driver);
709
710MODULE_DESCRIPTION("i.MX Television Encoder driver");
711MODULE_AUTHOR("Philipp Zabel, Pengutronix");
712MODULE_LICENSE("GPL");
Fabio Estevam52db752c2013-08-18 21:40:04 -0300713MODULE_ALIAS("platform:imx-tve");