blob: 8f8aa4a631222e312fa2ddedba37c84ddab92f00 [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
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200101enum {
102 TVE_MODE_TVOUT,
103 TVE_MODE_VGA,
104};
105
106struct imx_tve {
107 struct drm_connector connector;
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200108 struct drm_encoder encoder;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200109 struct device *dev;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200110 spinlock_t lock; /* register lock */
111 bool enabled;
112 int mode;
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200113 int di_hsync_pin;
114 int di_vsync_pin;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200115
116 struct regmap *regmap;
117 struct regulator *dac_reg;
118 struct i2c_adapter *ddc;
119 struct clk *clk;
120 struct clk *di_sel_clk;
121 struct clk_hw clk_hw_di;
122 struct clk *di_clk;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200123};
124
Philipp Zabel3df07392016-07-06 15:47:11 +0200125static inline struct imx_tve *con_to_tve(struct drm_connector *c)
126{
127 return container_of(c, struct imx_tve, connector);
128}
129
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200130static inline struct imx_tve *enc_to_tve(struct drm_encoder *e)
131{
132 return container_of(e, struct imx_tve, encoder);
133}
134
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200135static void tve_lock(void *__tve)
Fabio Estevam5d78bf82013-07-16 02:16:14 -0300136__acquires(&tve->lock)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200137{
138 struct imx_tve *tve = __tve;
Quentin Lambert63bc5162014-08-04 21:07:07 +0200139
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200140 spin_lock(&tve->lock);
141}
142
143static void tve_unlock(void *__tve)
Fabio Estevam5d78bf82013-07-16 02:16:14 -0300144__releases(&tve->lock)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200145{
146 struct imx_tve *tve = __tve;
Quentin Lambert63bc5162014-08-04 21:07:07 +0200147
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200148 spin_unlock(&tve->lock);
149}
150
151static void tve_enable(struct imx_tve *tve)
152{
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200153 if (!tve->enabled) {
Valentina Manea89bc5be2013-10-25 11:52:20 +0300154 tve->enabled = true;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200155 clk_prepare_enable(tve->clk);
Fabio Estevam88017bd02017-01-04 13:44:56 -0200156 regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
157 TVE_EN, TVE_EN);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200158 }
159
160 /* clear interrupt status register */
161 regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
162
163 /* cable detection irq disabled in VGA mode, enabled in TVOUT mode */
164 if (tve->mode == TVE_MODE_VGA)
165 regmap_write(tve->regmap, TVE_INT_CONT_REG, 0);
166 else
167 regmap_write(tve->regmap, TVE_INT_CONT_REG,
Andreas Werner89911e52013-08-11 17:20:23 +0200168 TVE_CD_SM_IEN |
169 TVE_CD_LM_IEN |
170 TVE_CD_MON_END_IEN);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200171}
172
173static void tve_disable(struct imx_tve *tve)
174{
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200175 if (tve->enabled) {
Valentina Manea89bc5be2013-10-25 11:52:20 +0300176 tve->enabled = false;
Fabio Estevam88017bd02017-01-04 13:44:56 -0200177 regmap_update_bits(tve->regmap, TVE_COM_CONF_REG, TVE_EN, 0);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200178 clk_disable_unprepare(tve->clk);
179 }
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200180}
181
182static int tve_setup_tvout(struct imx_tve *tve)
183{
184 return -ENOTSUPP;
185}
186
187static int tve_setup_vga(struct imx_tve *tve)
188{
189 unsigned int mask;
190 unsigned int val;
191 int ret;
192
193 /* set gain to (1 + 10/128) to provide 0.7V peak-to-peak amplitude */
194 ret = regmap_update_bits(tve->regmap, TVE_TVDAC0_CONT_REG,
195 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200196 if (ret)
197 return ret;
198
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200199 ret = regmap_update_bits(tve->regmap, TVE_TVDAC1_CONT_REG,
200 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200201 if (ret)
202 return ret;
203
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200204 ret = regmap_update_bits(tve->regmap, TVE_TVDAC2_CONT_REG,
205 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200206 if (ret)
207 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200208
209 /* set configuration register */
210 mask = TVE_DATA_SOURCE_MASK | TVE_INP_VIDEO_FORM;
211 val = TVE_DATA_SOURCE_BUS2 | TVE_INP_YCBCR_444;
212 mask |= TVE_TV_STAND_MASK | TVE_P2I_CONV_EN;
213 val |= TVE_TV_STAND_HD_1080P30 | 0;
214 mask |= TVE_TV_OUT_MODE_MASK | TVE_SYNC_CH_0_EN;
215 val |= TVE_TV_OUT_RGB | TVE_SYNC_CH_0_EN;
216 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG, mask, val);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200217 if (ret)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200218 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200219
220 /* set test mode (as documented) */
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200221 return regmap_update_bits(tve->regmap, TVE_TST_MODE_REG,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200222 TVE_TVDAC_TEST_MODE_MASK, 1);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200223}
224
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200225static int imx_tve_connector_get_modes(struct drm_connector *connector)
226{
227 struct imx_tve *tve = con_to_tve(connector);
228 struct edid *edid;
229 int ret = 0;
230
231 if (!tve->ddc)
232 return 0;
233
234 edid = drm_get_edid(connector, tve->ddc);
235 if (edid) {
236 drm_mode_connector_update_edid_property(connector, edid);
237 ret = drm_add_edid_modes(connector, edid);
238 kfree(edid);
239 }
240
241 return ret;
242}
243
244static int imx_tve_connector_mode_valid(struct drm_connector *connector,
245 struct drm_display_mode *mode)
246{
247 struct imx_tve *tve = con_to_tve(connector);
248 unsigned long rate;
Russell Kingbaa68c42013-11-09 11:20:55 +0000249
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200250 /* pixel clock with 2x oversampling */
251 rate = clk_round_rate(tve->clk, 2000UL * mode->clock) / 2000;
252 if (rate == mode->clock)
253 return MODE_OK;
254
255 /* pixel clock without oversampling */
256 rate = clk_round_rate(tve->clk, 1000UL * mode->clock) / 1000;
257 if (rate == mode->clock)
258 return MODE_OK;
259
260 dev_warn(tve->dev, "ignoring mode %dx%d\n",
261 mode->hdisplay, mode->vdisplay);
262
263 return MODE_BAD;
264}
265
266static struct drm_encoder *imx_tve_connector_best_encoder(
267 struct drm_connector *connector)
268{
269 struct imx_tve *tve = con_to_tve(connector);
270
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200271 return &tve->encoder;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200272}
273
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200274static void imx_tve_encoder_mode_set(struct drm_encoder *encoder,
Steve Longerbeameb10d632014-12-18 18:00:24 -0800275 struct drm_display_mode *orig_mode,
276 struct drm_display_mode *mode)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200277{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200278 struct imx_tve *tve = enc_to_tve(encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200279 unsigned long rounded_rate;
280 unsigned long rate;
281 int div = 1;
282 int ret;
283
284 /*
285 * FIXME
286 * we should try 4k * mode->clock first,
287 * and enable 4x oversampling for lower resolutions
288 */
289 rate = 2000UL * mode->clock;
290 clk_set_rate(tve->clk, rate);
291 rounded_rate = clk_get_rate(tve->clk);
292 if (rounded_rate >= rate)
293 div = 2;
294 clk_set_rate(tve->di_clk, rounded_rate / div);
295
296 ret = clk_set_parent(tve->di_sel_clk, tve->di_clk);
297 if (ret < 0) {
298 dev_err(tve->dev, "failed to set di_sel parent to tve_di: %d\n",
299 ret);
300 }
301
Liu Yingf6e396e2016-07-08 17:41:01 +0800302 regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
303 TVE_IPU_CLK_EN, TVE_IPU_CLK_EN);
304
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200305 if (tve->mode == TVE_MODE_VGA)
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200306 ret = tve_setup_vga(tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200307 else
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200308 ret = tve_setup_tvout(tve);
309 if (ret)
310 dev_err(tve->dev, "failed to set configuration: %d\n", ret);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200311}
312
Liu Yingf6e396e2016-07-08 17:41:01 +0800313static void imx_tve_encoder_enable(struct drm_encoder *encoder)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200314{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200315 struct imx_tve *tve = enc_to_tve(encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200316
317 tve_enable(tve);
318}
319
320static void imx_tve_encoder_disable(struct drm_encoder *encoder)
321{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200322 struct imx_tve *tve = enc_to_tve(encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200323
324 tve_disable(tve);
325}
326
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200327static int imx_tve_atomic_check(struct drm_encoder *encoder,
328 struct drm_crtc_state *crtc_state,
329 struct drm_connector_state *conn_state)
330{
331 struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
332 struct imx_tve *tve = enc_to_tve(encoder);
333
334 imx_crtc_state->bus_format = MEDIA_BUS_FMT_GBR888_1X24;
335 imx_crtc_state->di_hsync_pin = tve->di_hsync_pin;
336 imx_crtc_state->di_vsync_pin = tve->di_vsync_pin;
337
338 return 0;
339}
340
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100341static const struct drm_connector_funcs imx_tve_connector_funcs = {
Liu Yingf6e396e2016-07-08 17:41:01 +0800342 .dpms = drm_atomic_helper_connector_dpms,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200343 .fill_modes = drm_helper_probe_single_connector_modes,
Russell King1b3f7672013-11-03 13:30:48 +0000344 .destroy = imx_drm_connector_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800345 .reset = drm_atomic_helper_connector_reset,
346 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
347 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200348};
349
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100350static const struct drm_connector_helper_funcs imx_tve_connector_helper_funcs = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200351 .get_modes = imx_tve_connector_get_modes,
352 .best_encoder = imx_tve_connector_best_encoder,
353 .mode_valid = imx_tve_connector_mode_valid,
354};
355
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100356static const struct drm_encoder_funcs imx_tve_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000357 .destroy = imx_drm_encoder_destroy,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200358};
359
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100360static const struct drm_encoder_helper_funcs imx_tve_encoder_helper_funcs = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200361 .mode_set = imx_tve_encoder_mode_set,
Liu Yingf6e396e2016-07-08 17:41:01 +0800362 .enable = imx_tve_encoder_enable,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200363 .disable = imx_tve_encoder_disable,
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200364 .atomic_check = imx_tve_atomic_check,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200365};
366
367static irqreturn_t imx_tve_irq_handler(int irq, void *data)
368{
369 struct imx_tve *tve = data;
370 unsigned int val;
371
372 regmap_read(tve->regmap, TVE_STAT_REG, &val);
373
374 /* clear interrupt status register */
375 regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
376
377 return IRQ_HANDLED;
378}
379
380static unsigned long clk_tve_di_recalc_rate(struct clk_hw *hw,
381 unsigned long parent_rate)
382{
383 struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
384 unsigned int val;
385 int ret;
386
387 ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
388 if (ret < 0)
389 return 0;
390
391 switch (val & TVE_DAC_SAMP_RATE_MASK) {
392 case TVE_DAC_DIV4_RATE:
393 return parent_rate / 4;
394 case TVE_DAC_DIV2_RATE:
395 return parent_rate / 2;
396 case TVE_DAC_FULL_RATE:
397 default:
398 return parent_rate;
399 }
400
401 return 0;
402}
403
404static long clk_tve_di_round_rate(struct clk_hw *hw, unsigned long rate,
405 unsigned long *prate)
406{
407 unsigned long div;
408
409 div = *prate / rate;
410 if (div >= 4)
411 return *prate / 4;
412 else if (div >= 2)
413 return *prate / 2;
Catalina Mocanu7557b6e2014-09-24 14:27:36 -0700414 return *prate;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200415}
416
417static int clk_tve_di_set_rate(struct clk_hw *hw, unsigned long rate,
418 unsigned long parent_rate)
419{
420 struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
421 unsigned long div;
422 u32 val;
423 int ret;
424
425 div = parent_rate / rate;
426 if (div >= 4)
427 val = TVE_DAC_DIV4_RATE;
428 else if (div >= 2)
429 val = TVE_DAC_DIV2_RATE;
430 else
431 val = TVE_DAC_FULL_RATE;
432
Andreas Werner89911e52013-08-11 17:20:23 +0200433 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
434 TVE_DAC_SAMP_RATE_MASK, val);
435
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200436 if (ret < 0) {
437 dev_err(tve->dev, "failed to set divider: %d\n", ret);
438 return ret;
439 }
440
441 return 0;
442}
443
444static struct clk_ops clk_tve_di_ops = {
445 .round_rate = clk_tve_di_round_rate,
446 .set_rate = clk_tve_di_set_rate,
447 .recalc_rate = clk_tve_di_recalc_rate,
448};
449
450static int tve_clk_init(struct imx_tve *tve, void __iomem *base)
451{
452 const char *tve_di_parent[1];
453 struct clk_init_data init = {
454 .name = "tve_di",
455 .ops = &clk_tve_di_ops,
456 .num_parents = 1,
457 .flags = 0,
458 };
459
460 tve_di_parent[0] = __clk_get_name(tve->clk);
461 init.parent_names = (const char **)&tve_di_parent;
462
463 tve->clk_hw_di.init = &init;
464 tve->di_clk = clk_register(tve->dev, &tve->clk_hw_di);
465 if (IS_ERR(tve->di_clk)) {
466 dev_err(tve->dev, "failed to register TVE output clock: %ld\n",
467 PTR_ERR(tve->di_clk));
468 return PTR_ERR(tve->di_clk);
469 }
470
471 return 0;
472}
473
Russell King1b3f7672013-11-03 13:30:48 +0000474static int imx_tve_register(struct drm_device *drm, struct imx_tve *tve)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200475{
Russell Kingf2d66aa2013-11-03 15:52:16 +0000476 int encoder_type;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200477 int ret;
478
Russell Kingf2d66aa2013-11-03 15:52:16 +0000479 encoder_type = tve->mode == TVE_MODE_VGA ?
480 DRM_MODE_ENCODER_DAC : DRM_MODE_ENCODER_TVDAC;
481
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200482 ret = imx_drm_encoder_parse_of(drm, &tve->encoder, tve->dev->of_node);
Russell King1b3f7672013-11-03 13:30:48 +0000483 if (ret)
484 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200485
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200486 drm_encoder_helper_add(&tve->encoder, &imx_tve_encoder_helper_funcs);
487 drm_encoder_init(drm, &tve->encoder, &imx_tve_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200488 encoder_type, NULL);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200489
490 drm_connector_helper_add(&tve->connector,
491 &imx_tve_connector_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000492 drm_connector_init(drm, &tve->connector, &imx_tve_connector_funcs,
493 DRM_MODE_CONNECTOR_VGA);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200494
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200495 drm_mode_connector_attach_encoder(&tve->connector, &tve->encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200496
497 return 0;
498}
499
500static bool imx_tve_readable_reg(struct device *dev, unsigned int reg)
501{
502 return (reg % 4 == 0) && (reg <= 0xdc);
503}
504
505static struct regmap_config tve_regmap_config = {
506 .reg_bits = 32,
507 .val_bits = 32,
508 .reg_stride = 4,
509
510 .readable_reg = imx_tve_readable_reg,
511
512 .lock = tve_lock,
513 .unlock = tve_unlock,
514
515 .max_register = 0xdc,
516};
517
Aybuke Ozdemir8684ba72014-09-27 16:16:02 +0300518static const char * const imx_tve_modes[] = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200519 [TVE_MODE_TVOUT] = "tvout",
520 [TVE_MODE_VGA] = "vga",
521};
522
Liu Ying7fc6cb22013-12-24 10:17:44 +0800523static const int of_get_tve_mode(struct device_node *np)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200524{
525 const char *bm;
526 int ret, i;
527
528 ret = of_property_read_string(np, "fsl,tve-mode", &bm);
529 if (ret < 0)
530 return ret;
531
532 for (i = 0; i < ARRAY_SIZE(imx_tve_modes); i++)
533 if (!strcasecmp(bm, imx_tve_modes[i]))
534 return i;
535
536 return -EINVAL;
537}
538
Russell King17b50012013-11-03 11:23:34 +0000539static int imx_tve_bind(struct device *dev, struct device *master, void *data)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200540{
Russell King17b50012013-11-03 11:23:34 +0000541 struct platform_device *pdev = to_platform_device(dev);
Russell King1b3f7672013-11-03 13:30:48 +0000542 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000543 struct device_node *np = dev->of_node;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200544 struct device_node *ddc_node;
545 struct imx_tve *tve;
546 struct resource *res;
547 void __iomem *base;
548 unsigned int val;
549 int irq;
550 int ret;
551
Russell King17b50012013-11-03 11:23:34 +0000552 tve = devm_kzalloc(dev, sizeof(*tve), GFP_KERNEL);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200553 if (!tve)
554 return -ENOMEM;
555
Russell King17b50012013-11-03 11:23:34 +0000556 tve->dev = dev;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200557 spin_lock_init(&tve->lock);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200558
Shawn Guoa3fe9642014-04-10 14:19:05 +0800559 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200560 if (ddc_node) {
561 tve->ddc = of_find_i2c_adapter_by_node(ddc_node);
562 of_node_put(ddc_node);
563 }
564
565 tve->mode = of_get_tve_mode(np);
566 if (tve->mode != TVE_MODE_VGA) {
Russell King17b50012013-11-03 11:23:34 +0000567 dev_err(dev, "only VGA mode supported, currently\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200568 return -EINVAL;
569 }
570
571 if (tve->mode == TVE_MODE_VGA) {
Andreas Werner89911e52013-08-11 17:20:23 +0200572 ret = of_property_read_u32(np, "fsl,hsync-pin",
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200573 &tve->di_hsync_pin);
Andreas Werner89911e52013-08-11 17:20:23 +0200574
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200575 if (ret < 0) {
Fabio Estevamae8308b2016-07-09 18:57:44 -0300576 dev_err(dev, "failed to get hsync pin\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200577 return ret;
578 }
579
Fabio Estevam4f7a5122016-07-09 18:57:43 -0300580 ret = of_property_read_u32(np, "fsl,vsync-pin",
581 &tve->di_vsync_pin);
Andreas Werner89911e52013-08-11 17:20:23 +0200582
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200583 if (ret < 0) {
Russell King17b50012013-11-03 11:23:34 +0000584 dev_err(dev, "failed to get vsync pin\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200585 return ret;
586 }
587 }
588
589 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Russell King17b50012013-11-03 11:23:34 +0000590 base = devm_ioremap_resource(dev, res);
Laurent Navet9b43b562013-05-02 13:41:41 +0200591 if (IS_ERR(base))
592 return PTR_ERR(base);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200593
594 tve_regmap_config.lock_arg = tve;
Russell King17b50012013-11-03 11:23:34 +0000595 tve->regmap = devm_regmap_init_mmio_clk(dev, "tve", base,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200596 &tve_regmap_config);
597 if (IS_ERR(tve->regmap)) {
Russell King17b50012013-11-03 11:23:34 +0000598 dev_err(dev, "failed to init regmap: %ld\n",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200599 PTR_ERR(tve->regmap));
600 return PTR_ERR(tve->regmap);
601 }
602
603 irq = platform_get_irq(pdev, 0);
604 if (irq < 0) {
Russell King17b50012013-11-03 11:23:34 +0000605 dev_err(dev, "failed to get irq\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200606 return irq;
607 }
608
Russell King17b50012013-11-03 11:23:34 +0000609 ret = devm_request_threaded_irq(dev, irq, NULL,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200610 imx_tve_irq_handler, IRQF_ONESHOT,
611 "imx-tve", tve);
612 if (ret < 0) {
Russell King17b50012013-11-03 11:23:34 +0000613 dev_err(dev, "failed to request irq: %d\n", ret);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200614 return ret;
615 }
616
Russell King17b50012013-11-03 11:23:34 +0000617 tve->dac_reg = devm_regulator_get(dev, "dac");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200618 if (!IS_ERR(tve->dac_reg)) {
Fabio Estevamdeb65872016-07-09 18:57:42 -0300619 ret = regulator_set_voltage(tve->dac_reg, 2750000, 2750000);
620 if (ret)
621 return ret;
Fabio Estevamc7b0cf32013-05-21 11:24:44 -0300622 ret = regulator_enable(tve->dac_reg);
623 if (ret)
624 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200625 }
626
Russell King17b50012013-11-03 11:23:34 +0000627 tve->clk = devm_clk_get(dev, "tve");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200628 if (IS_ERR(tve->clk)) {
Russell King17b50012013-11-03 11:23:34 +0000629 dev_err(dev, "failed to get high speed tve clock: %ld\n",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200630 PTR_ERR(tve->clk));
631 return PTR_ERR(tve->clk);
632 }
633
634 /* this is the IPU DI clock input selector, can be parented to tve_di */
Russell King17b50012013-11-03 11:23:34 +0000635 tve->di_sel_clk = devm_clk_get(dev, "di_sel");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200636 if (IS_ERR(tve->di_sel_clk)) {
Russell King17b50012013-11-03 11:23:34 +0000637 dev_err(dev, "failed to get ipu di mux clock: %ld\n",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200638 PTR_ERR(tve->di_sel_clk));
639 return PTR_ERR(tve->di_sel_clk);
640 }
641
642 ret = tve_clk_init(tve, base);
643 if (ret < 0)
644 return ret;
645
646 ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
647 if (ret < 0) {
Rene Kolarikf582d9a2014-10-09 20:29:32 +0200648 dev_err(dev, "failed to read configuration register: %d\n",
649 ret);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200650 return ret;
651 }
652 if (val != 0x00100000) {
Russell King17b50012013-11-03 11:23:34 +0000653 dev_err(dev, "configuration register default value indicates this is not a TVEv2\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200654 return -ENODEV;
Joe Perchesa22526e2013-10-10 16:07:59 -0700655 }
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200656
657 /* disable cable detection for VGA mode */
658 ret = regmap_write(tve->regmap, TVE_CD_CONT_REG, 0);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200659 if (ret)
660 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200661
Russell King1b3f7672013-11-03 13:30:48 +0000662 ret = imx_tve_register(drm, tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200663 if (ret)
664 return ret;
665
Russell King17b50012013-11-03 11:23:34 +0000666 dev_set_drvdata(dev, tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200667
668 return 0;
669}
670
Russell King17b50012013-11-03 11:23:34 +0000671static void imx_tve_unbind(struct device *dev, struct device *master,
672 void *data)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200673{
Russell King17b50012013-11-03 11:23:34 +0000674 struct imx_tve *tve = dev_get_drvdata(dev);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200675
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200676 if (!IS_ERR(tve->dac_reg))
677 regulator_disable(tve->dac_reg);
Russell King17b50012013-11-03 11:23:34 +0000678}
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200679
Russell King17b50012013-11-03 11:23:34 +0000680static const struct component_ops imx_tve_ops = {
681 .bind = imx_tve_bind,
682 .unbind = imx_tve_unbind,
683};
684
685static int imx_tve_probe(struct platform_device *pdev)
686{
687 return component_add(&pdev->dev, &imx_tve_ops);
688}
689
690static int imx_tve_remove(struct platform_device *pdev)
691{
692 component_del(&pdev->dev, &imx_tve_ops);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200693 return 0;
694}
695
696static const struct of_device_id imx_tve_dt_ids[] = {
697 { .compatible = "fsl,imx53-tve", },
698 { /* sentinel */ }
699};
Luis de Bethencourt5e4789d2015-11-30 15:02:44 +0000700MODULE_DEVICE_TABLE(of, imx_tve_dt_ids);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200701
702static struct platform_driver imx_tve_driver = {
703 .probe = imx_tve_probe,
704 .remove = imx_tve_remove,
705 .driver = {
706 .of_match_table = imx_tve_dt_ids,
707 .name = "imx-tve",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200708 },
709};
710
711module_platform_driver(imx_tve_driver);
712
713MODULE_DESCRIPTION("i.MX Television Encoder driver");
714MODULE_AUTHOR("Philipp Zabel, Pengutronix");
715MODULE_LICENSE("GPL");
Fabio Estevam52db752c2013-08-18 21:40:04 -0300716MODULE_ALIAS("platform:imx-tve");