blob: 0c6bf12d45b10e632f8d53b6ec794ba1ff3afb36 [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
Fabio Estevamfc12bcc2017-02-08 10:47:49 -0200101#define IMX_TVE_DAC_VOLTAGE 2750000
102
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200103enum {
104 TVE_MODE_TVOUT,
105 TVE_MODE_VGA,
106};
107
108struct imx_tve {
109 struct drm_connector connector;
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200110 struct drm_encoder encoder;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200111 struct device *dev;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200112 spinlock_t lock; /* register lock */
113 bool enabled;
114 int mode;
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200115 int di_hsync_pin;
116 int di_vsync_pin;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200117
118 struct regmap *regmap;
119 struct regulator *dac_reg;
120 struct i2c_adapter *ddc;
121 struct clk *clk;
122 struct clk *di_sel_clk;
123 struct clk_hw clk_hw_di;
124 struct clk *di_clk;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200125};
126
Philipp Zabel3df07392016-07-06 15:47:11 +0200127static inline struct imx_tve *con_to_tve(struct drm_connector *c)
128{
129 return container_of(c, struct imx_tve, connector);
130}
131
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200132static inline struct imx_tve *enc_to_tve(struct drm_encoder *e)
133{
134 return container_of(e, struct imx_tve, encoder);
135}
136
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200137static void tve_lock(void *__tve)
Fabio Estevam5d78bf82013-07-16 02:16:14 -0300138__acquires(&tve->lock)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200139{
140 struct imx_tve *tve = __tve;
Quentin Lambert63bc5162014-08-04 21:07:07 +0200141
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200142 spin_lock(&tve->lock);
143}
144
145static void tve_unlock(void *__tve)
Fabio Estevam5d78bf82013-07-16 02:16:14 -0300146__releases(&tve->lock)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200147{
148 struct imx_tve *tve = __tve;
Quentin Lambert63bc5162014-08-04 21:07:07 +0200149
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200150 spin_unlock(&tve->lock);
151}
152
153static void tve_enable(struct imx_tve *tve)
154{
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200155 int ret;
156
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200157 if (!tve->enabled) {
Valentina Manea89bc5be2013-10-25 11:52:20 +0300158 tve->enabled = true;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200159 clk_prepare_enable(tve->clk);
160 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
Liu Yingf6e396e2016-07-08 17:41:01 +0800161 TVE_EN, TVE_EN);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200162 }
163
164 /* clear interrupt status register */
165 regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
166
167 /* cable detection irq disabled in VGA mode, enabled in TVOUT mode */
168 if (tve->mode == TVE_MODE_VGA)
169 regmap_write(tve->regmap, TVE_INT_CONT_REG, 0);
170 else
171 regmap_write(tve->regmap, TVE_INT_CONT_REG,
Andreas Werner89911e52013-08-11 17:20:23 +0200172 TVE_CD_SM_IEN |
173 TVE_CD_LM_IEN |
174 TVE_CD_MON_END_IEN);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200175}
176
177static void tve_disable(struct imx_tve *tve)
178{
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200179 int ret;
180
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200181 if (tve->enabled) {
Valentina Manea89bc5be2013-10-25 11:52:20 +0300182 tve->enabled = false;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200183 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
Liu Yingf6e396e2016-07-08 17:41:01 +0800184 TVE_EN, 0);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200185 clk_disable_unprepare(tve->clk);
186 }
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200187}
188
189static int tve_setup_tvout(struct imx_tve *tve)
190{
191 return -ENOTSUPP;
192}
193
194static int tve_setup_vga(struct imx_tve *tve)
195{
196 unsigned int mask;
197 unsigned int val;
198 int ret;
199
200 /* set gain to (1 + 10/128) to provide 0.7V peak-to-peak amplitude */
201 ret = regmap_update_bits(tve->regmap, TVE_TVDAC0_CONT_REG,
202 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200203 if (ret)
204 return ret;
205
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200206 ret = regmap_update_bits(tve->regmap, TVE_TVDAC1_CONT_REG,
207 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200208 if (ret)
209 return ret;
210
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200211 ret = regmap_update_bits(tve->regmap, TVE_TVDAC2_CONT_REG,
212 TVE_TVDAC_GAIN_MASK, 0x0a);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200213 if (ret)
214 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200215
216 /* set configuration register */
217 mask = TVE_DATA_SOURCE_MASK | TVE_INP_VIDEO_FORM;
218 val = TVE_DATA_SOURCE_BUS2 | TVE_INP_YCBCR_444;
219 mask |= TVE_TV_STAND_MASK | TVE_P2I_CONV_EN;
220 val |= TVE_TV_STAND_HD_1080P30 | 0;
221 mask |= TVE_TV_OUT_MODE_MASK | TVE_SYNC_CH_0_EN;
222 val |= TVE_TV_OUT_RGB | TVE_SYNC_CH_0_EN;
223 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG, mask, val);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200224 if (ret)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200225 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200226
227 /* set test mode (as documented) */
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200228 return regmap_update_bits(tve->regmap, TVE_TST_MODE_REG,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200229 TVE_TVDAC_TEST_MODE_MASK, 1);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200230}
231
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200232static int imx_tve_connector_get_modes(struct drm_connector *connector)
233{
234 struct imx_tve *tve = con_to_tve(connector);
235 struct edid *edid;
236 int ret = 0;
237
238 if (!tve->ddc)
239 return 0;
240
241 edid = drm_get_edid(connector, tve->ddc);
242 if (edid) {
243 drm_mode_connector_update_edid_property(connector, edid);
244 ret = drm_add_edid_modes(connector, edid);
245 kfree(edid);
246 }
247
248 return ret;
249}
250
251static int imx_tve_connector_mode_valid(struct drm_connector *connector,
252 struct drm_display_mode *mode)
253{
254 struct imx_tve *tve = con_to_tve(connector);
255 unsigned long rate;
Russell Kingbaa68c42013-11-09 11:20:55 +0000256
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200257 /* pixel clock with 2x oversampling */
258 rate = clk_round_rate(tve->clk, 2000UL * mode->clock) / 2000;
259 if (rate == mode->clock)
260 return MODE_OK;
261
262 /* pixel clock without oversampling */
263 rate = clk_round_rate(tve->clk, 1000UL * mode->clock) / 1000;
264 if (rate == mode->clock)
265 return MODE_OK;
266
267 dev_warn(tve->dev, "ignoring mode %dx%d\n",
268 mode->hdisplay, mode->vdisplay);
269
270 return MODE_BAD;
271}
272
273static struct drm_encoder *imx_tve_connector_best_encoder(
274 struct drm_connector *connector)
275{
276 struct imx_tve *tve = con_to_tve(connector);
277
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200278 return &tve->encoder;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200279}
280
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200281static void imx_tve_encoder_mode_set(struct drm_encoder *encoder,
Steve Longerbeameb10d632014-12-18 18:00:24 -0800282 struct drm_display_mode *orig_mode,
283 struct drm_display_mode *mode)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200284{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200285 struct imx_tve *tve = enc_to_tve(encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200286 unsigned long rounded_rate;
287 unsigned long rate;
288 int div = 1;
289 int ret;
290
291 /*
292 * FIXME
293 * we should try 4k * mode->clock first,
294 * and enable 4x oversampling for lower resolutions
295 */
296 rate = 2000UL * mode->clock;
297 clk_set_rate(tve->clk, rate);
298 rounded_rate = clk_get_rate(tve->clk);
299 if (rounded_rate >= rate)
300 div = 2;
301 clk_set_rate(tve->di_clk, rounded_rate / div);
302
303 ret = clk_set_parent(tve->di_sel_clk, tve->di_clk);
304 if (ret < 0) {
305 dev_err(tve->dev, "failed to set di_sel parent to tve_di: %d\n",
306 ret);
307 }
308
Liu Yingf6e396e2016-07-08 17:41:01 +0800309 regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
310 TVE_IPU_CLK_EN, TVE_IPU_CLK_EN);
311
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200312 if (tve->mode == TVE_MODE_VGA)
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200313 ret = tve_setup_vga(tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200314 else
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200315 ret = tve_setup_tvout(tve);
316 if (ret)
317 dev_err(tve->dev, "failed to set configuration: %d\n", ret);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200318}
319
Liu Yingf6e396e2016-07-08 17:41:01 +0800320static void imx_tve_encoder_enable(struct drm_encoder *encoder)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200321{
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_enable(tve);
325}
326
327static void imx_tve_encoder_disable(struct drm_encoder *encoder)
328{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200329 struct imx_tve *tve = enc_to_tve(encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200330
331 tve_disable(tve);
332}
333
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200334static int imx_tve_atomic_check(struct drm_encoder *encoder,
335 struct drm_crtc_state *crtc_state,
336 struct drm_connector_state *conn_state)
337{
338 struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
339 struct imx_tve *tve = enc_to_tve(encoder);
340
341 imx_crtc_state->bus_format = MEDIA_BUS_FMT_GBR888_1X24;
342 imx_crtc_state->di_hsync_pin = tve->di_hsync_pin;
343 imx_crtc_state->di_vsync_pin = tve->di_vsync_pin;
344
345 return 0;
346}
347
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100348static const struct drm_connector_funcs imx_tve_connector_funcs = {
Liu Yingf6e396e2016-07-08 17:41:01 +0800349 .dpms = drm_atomic_helper_connector_dpms,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200350 .fill_modes = drm_helper_probe_single_connector_modes,
Russell King1b3f7672013-11-03 13:30:48 +0000351 .destroy = imx_drm_connector_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800352 .reset = drm_atomic_helper_connector_reset,
353 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
354 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200355};
356
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100357static const struct drm_connector_helper_funcs imx_tve_connector_helper_funcs = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200358 .get_modes = imx_tve_connector_get_modes,
359 .best_encoder = imx_tve_connector_best_encoder,
360 .mode_valid = imx_tve_connector_mode_valid,
361};
362
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100363static const struct drm_encoder_funcs imx_tve_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000364 .destroy = imx_drm_encoder_destroy,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200365};
366
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100367static const struct drm_encoder_helper_funcs imx_tve_encoder_helper_funcs = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200368 .mode_set = imx_tve_encoder_mode_set,
Liu Yingf6e396e2016-07-08 17:41:01 +0800369 .enable = imx_tve_encoder_enable,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200370 .disable = imx_tve_encoder_disable,
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200371 .atomic_check = imx_tve_atomic_check,
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200372};
373
374static irqreturn_t imx_tve_irq_handler(int irq, void *data)
375{
376 struct imx_tve *tve = data;
377 unsigned int val;
378
379 regmap_read(tve->regmap, TVE_STAT_REG, &val);
380
381 /* clear interrupt status register */
382 regmap_write(tve->regmap, TVE_STAT_REG, 0xffffffff);
383
384 return IRQ_HANDLED;
385}
386
387static unsigned long clk_tve_di_recalc_rate(struct clk_hw *hw,
388 unsigned long parent_rate)
389{
390 struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
391 unsigned int val;
392 int ret;
393
394 ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
395 if (ret < 0)
396 return 0;
397
398 switch (val & TVE_DAC_SAMP_RATE_MASK) {
399 case TVE_DAC_DIV4_RATE:
400 return parent_rate / 4;
401 case TVE_DAC_DIV2_RATE:
402 return parent_rate / 2;
403 case TVE_DAC_FULL_RATE:
404 default:
405 return parent_rate;
406 }
407
408 return 0;
409}
410
411static long clk_tve_di_round_rate(struct clk_hw *hw, unsigned long rate,
412 unsigned long *prate)
413{
414 unsigned long div;
415
416 div = *prate / rate;
417 if (div >= 4)
418 return *prate / 4;
419 else if (div >= 2)
420 return *prate / 2;
Catalina Mocanu7557b6e2014-09-24 14:27:36 -0700421 return *prate;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200422}
423
424static int clk_tve_di_set_rate(struct clk_hw *hw, unsigned long rate,
425 unsigned long parent_rate)
426{
427 struct imx_tve *tve = container_of(hw, struct imx_tve, clk_hw_di);
428 unsigned long div;
429 u32 val;
430 int ret;
431
432 div = parent_rate / rate;
433 if (div >= 4)
434 val = TVE_DAC_DIV4_RATE;
435 else if (div >= 2)
436 val = TVE_DAC_DIV2_RATE;
437 else
438 val = TVE_DAC_FULL_RATE;
439
Andreas Werner89911e52013-08-11 17:20:23 +0200440 ret = regmap_update_bits(tve->regmap, TVE_COM_CONF_REG,
441 TVE_DAC_SAMP_RATE_MASK, val);
442
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200443 if (ret < 0) {
444 dev_err(tve->dev, "failed to set divider: %d\n", ret);
445 return ret;
446 }
447
448 return 0;
449}
450
451static struct clk_ops clk_tve_di_ops = {
452 .round_rate = clk_tve_di_round_rate,
453 .set_rate = clk_tve_di_set_rate,
454 .recalc_rate = clk_tve_di_recalc_rate,
455};
456
457static int tve_clk_init(struct imx_tve *tve, void __iomem *base)
458{
459 const char *tve_di_parent[1];
460 struct clk_init_data init = {
461 .name = "tve_di",
462 .ops = &clk_tve_di_ops,
463 .num_parents = 1,
464 .flags = 0,
465 };
466
467 tve_di_parent[0] = __clk_get_name(tve->clk);
468 init.parent_names = (const char **)&tve_di_parent;
469
470 tve->clk_hw_di.init = &init;
471 tve->di_clk = clk_register(tve->dev, &tve->clk_hw_di);
472 if (IS_ERR(tve->di_clk)) {
473 dev_err(tve->dev, "failed to register TVE output clock: %ld\n",
474 PTR_ERR(tve->di_clk));
475 return PTR_ERR(tve->di_clk);
476 }
477
478 return 0;
479}
480
Russell King1b3f7672013-11-03 13:30:48 +0000481static int imx_tve_register(struct drm_device *drm, struct imx_tve *tve)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200482{
Russell Kingf2d66aa2013-11-03 15:52:16 +0000483 int encoder_type;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200484 int ret;
485
Russell Kingf2d66aa2013-11-03 15:52:16 +0000486 encoder_type = tve->mode == TVE_MODE_VGA ?
487 DRM_MODE_ENCODER_DAC : DRM_MODE_ENCODER_TVDAC;
488
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200489 ret = imx_drm_encoder_parse_of(drm, &tve->encoder, tve->dev->of_node);
Russell King1b3f7672013-11-03 13:30:48 +0000490 if (ret)
491 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200492
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200493 drm_encoder_helper_add(&tve->encoder, &imx_tve_encoder_helper_funcs);
494 drm_encoder_init(drm, &tve->encoder, &imx_tve_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200495 encoder_type, NULL);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200496
497 drm_connector_helper_add(&tve->connector,
498 &imx_tve_connector_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000499 drm_connector_init(drm, &tve->connector, &imx_tve_connector_funcs,
500 DRM_MODE_CONNECTOR_VGA);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200501
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200502 drm_mode_connector_attach_encoder(&tve->connector, &tve->encoder);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200503
504 return 0;
505}
506
507static bool imx_tve_readable_reg(struct device *dev, unsigned int reg)
508{
509 return (reg % 4 == 0) && (reg <= 0xdc);
510}
511
512static struct regmap_config tve_regmap_config = {
513 .reg_bits = 32,
514 .val_bits = 32,
515 .reg_stride = 4,
516
517 .readable_reg = imx_tve_readable_reg,
518
519 .lock = tve_lock,
520 .unlock = tve_unlock,
521
522 .max_register = 0xdc,
523};
524
Aybuke Ozdemir8684ba72014-09-27 16:16:02 +0300525static const char * const imx_tve_modes[] = {
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200526 [TVE_MODE_TVOUT] = "tvout",
527 [TVE_MODE_VGA] = "vga",
528};
529
Liu Ying7fc6cb22013-12-24 10:17:44 +0800530static const int of_get_tve_mode(struct device_node *np)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200531{
532 const char *bm;
533 int ret, i;
534
535 ret = of_property_read_string(np, "fsl,tve-mode", &bm);
536 if (ret < 0)
537 return ret;
538
539 for (i = 0; i < ARRAY_SIZE(imx_tve_modes); i++)
540 if (!strcasecmp(bm, imx_tve_modes[i]))
541 return i;
542
543 return -EINVAL;
544}
545
Russell King17b50012013-11-03 11:23:34 +0000546static int imx_tve_bind(struct device *dev, struct device *master, void *data)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200547{
Russell King17b50012013-11-03 11:23:34 +0000548 struct platform_device *pdev = to_platform_device(dev);
Russell King1b3f7672013-11-03 13:30:48 +0000549 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000550 struct device_node *np = dev->of_node;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200551 struct device_node *ddc_node;
552 struct imx_tve *tve;
553 struct resource *res;
554 void __iomem *base;
555 unsigned int val;
556 int irq;
557 int ret;
558
Russell King17b50012013-11-03 11:23:34 +0000559 tve = devm_kzalloc(dev, sizeof(*tve), GFP_KERNEL);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200560 if (!tve)
561 return -ENOMEM;
562
Russell King17b50012013-11-03 11:23:34 +0000563 tve->dev = dev;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200564 spin_lock_init(&tve->lock);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200565
Shawn Guoa3fe9642014-04-10 14:19:05 +0800566 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200567 if (ddc_node) {
568 tve->ddc = of_find_i2c_adapter_by_node(ddc_node);
569 of_node_put(ddc_node);
570 }
571
572 tve->mode = of_get_tve_mode(np);
573 if (tve->mode != TVE_MODE_VGA) {
Russell King17b50012013-11-03 11:23:34 +0000574 dev_err(dev, "only VGA mode supported, currently\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200575 return -EINVAL;
576 }
577
578 if (tve->mode == TVE_MODE_VGA) {
Andreas Werner89911e52013-08-11 17:20:23 +0200579 ret = of_property_read_u32(np, "fsl,hsync-pin",
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200580 &tve->di_hsync_pin);
Andreas Werner89911e52013-08-11 17:20:23 +0200581
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200582 if (ret < 0) {
Fabio Estevamae8308b2016-07-09 18:57:44 -0300583 dev_err(dev, "failed to get hsync pin\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200584 return ret;
585 }
586
Fabio Estevam4f7a5122016-07-09 18:57:43 -0300587 ret = of_property_read_u32(np, "fsl,vsync-pin",
588 &tve->di_vsync_pin);
Andreas Werner89911e52013-08-11 17:20:23 +0200589
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200590 if (ret < 0) {
Russell King17b50012013-11-03 11:23:34 +0000591 dev_err(dev, "failed to get vsync pin\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200592 return ret;
593 }
594 }
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)) {
Fabio Estevamfc12bcc2017-02-08 10:47:49 -0200626 if (regulator_get_voltage(tve->dac_reg) != IMX_TVE_DAC_VOLTAGE)
627 dev_warn(dev, "dac voltage is not %d uV\n", IMX_TVE_DAC_VOLTAGE);
Fabio Estevamc7b0cf32013-05-21 11:24:44 -0300628 ret = regulator_enable(tve->dac_reg);
629 if (ret)
630 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200631 }
632
Russell King17b50012013-11-03 11:23:34 +0000633 tve->clk = devm_clk_get(dev, "tve");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200634 if (IS_ERR(tve->clk)) {
Russell King17b50012013-11-03 11:23:34 +0000635 dev_err(dev, "failed to get high speed tve clock: %ld\n",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200636 PTR_ERR(tve->clk));
637 return PTR_ERR(tve->clk);
638 }
639
640 /* this is the IPU DI clock input selector, can be parented to tve_di */
Russell King17b50012013-11-03 11:23:34 +0000641 tve->di_sel_clk = devm_clk_get(dev, "di_sel");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200642 if (IS_ERR(tve->di_sel_clk)) {
Russell King17b50012013-11-03 11:23:34 +0000643 dev_err(dev, "failed to get ipu di mux clock: %ld\n",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200644 PTR_ERR(tve->di_sel_clk));
645 return PTR_ERR(tve->di_sel_clk);
646 }
647
648 ret = tve_clk_init(tve, base);
649 if (ret < 0)
650 return ret;
651
652 ret = regmap_read(tve->regmap, TVE_COM_CONF_REG, &val);
653 if (ret < 0) {
Rene Kolarikf582d9a2014-10-09 20:29:32 +0200654 dev_err(dev, "failed to read configuration register: %d\n",
655 ret);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200656 return ret;
657 }
658 if (val != 0x00100000) {
Russell King17b50012013-11-03 11:23:34 +0000659 dev_err(dev, "configuration register default value indicates this is not a TVEv2\n");
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200660 return -ENODEV;
Joe Perchesa22526e2013-10-10 16:07:59 -0700661 }
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200662
663 /* disable cable detection for VGA mode */
664 ret = regmap_write(tve->regmap, TVE_CD_CONT_REG, 0);
Fabio Estevamf555e7e2015-01-20 20:44:07 -0200665 if (ret)
666 return ret;
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200667
Russell King1b3f7672013-11-03 13:30:48 +0000668 ret = imx_tve_register(drm, tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200669 if (ret)
670 return ret;
671
Russell King17b50012013-11-03 11:23:34 +0000672 dev_set_drvdata(dev, tve);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200673
674 return 0;
675}
676
Russell King17b50012013-11-03 11:23:34 +0000677static void imx_tve_unbind(struct device *dev, struct device *master,
678 void *data)
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200679{
Russell King17b50012013-11-03 11:23:34 +0000680 struct imx_tve *tve = dev_get_drvdata(dev);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200681
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200682 if (!IS_ERR(tve->dac_reg))
683 regulator_disable(tve->dac_reg);
Russell King17b50012013-11-03 11:23:34 +0000684}
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200685
Russell King17b50012013-11-03 11:23:34 +0000686static const struct component_ops imx_tve_ops = {
687 .bind = imx_tve_bind,
688 .unbind = imx_tve_unbind,
689};
690
691static int imx_tve_probe(struct platform_device *pdev)
692{
693 return component_add(&pdev->dev, &imx_tve_ops);
694}
695
696static int imx_tve_remove(struct platform_device *pdev)
697{
698 component_del(&pdev->dev, &imx_tve_ops);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200699 return 0;
700}
701
702static const struct of_device_id imx_tve_dt_ids[] = {
703 { .compatible = "fsl,imx53-tve", },
704 { /* sentinel */ }
705};
Luis de Bethencourt5e4789d2015-11-30 15:02:44 +0000706MODULE_DEVICE_TABLE(of, imx_tve_dt_ids);
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200707
708static struct platform_driver imx_tve_driver = {
709 .probe = imx_tve_probe,
710 .remove = imx_tve_remove,
711 .driver = {
712 .of_match_table = imx_tve_dt_ids,
713 .name = "imx-tve",
Philipp Zabelfcbc51e2013-04-08 18:04:38 +0200714 },
715};
716
717module_platform_driver(imx_tve_driver);
718
719MODULE_DESCRIPTION("i.MX Television Encoder driver");
720MODULE_AUTHOR("Philipp Zabel, Pengutronix");
721MODULE_LICENSE("GPL");
Fabio Estevam52db752c2013-08-18 21:40:04 -0300722MODULE_ALIAS("platform:imx-tve");