Home | History | Annotate | Line # | Download | only in i2c
      1  1.3  riastrad /*	$NetBSD: ch7006_mode.c,v 1.3 2021/12/18 23:45:27 riastradh Exp $	*/
      2  1.3  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright (C) 2009 Francisco Jerez.
      5  1.1  riastrad  * All Rights Reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining
      8  1.1  riastrad  * a copy of this software and associated documentation files (the
      9  1.1  riastrad  * "Software"), to deal in the Software without restriction, including
     10  1.1  riastrad  * without limitation the rights to use, copy, modify, merge, publish,
     11  1.1  riastrad  * distribute, sublicense, and/or sell copies of the Software, and to
     12  1.1  riastrad  * permit persons to whom the Software is furnished to do so, subject to
     13  1.1  riastrad  * the following conditions:
     14  1.1  riastrad  *
     15  1.1  riastrad  * The above copyright notice and this permission notice (including the
     16  1.1  riastrad  * next paragraph) shall be included in all copies or substantial
     17  1.1  riastrad  * portions of the Software.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     20  1.1  riastrad  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     21  1.1  riastrad  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     22  1.1  riastrad  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
     23  1.1  riastrad  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     24  1.1  riastrad  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     25  1.1  riastrad  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     26  1.1  riastrad  *
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.3  riastrad #include <sys/cdefs.h>
     30  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: ch7006_mode.c,v 1.3 2021/12/18 23:45:27 riastradh Exp $");
     31  1.3  riastrad 
     32  1.1  riastrad #include "ch7006_priv.h"
     33  1.1  riastrad 
     34  1.3  riastrad const char * const ch7006_tv_norm_names[] = {
     35  1.1  riastrad 	[TV_NORM_PAL] = "PAL",
     36  1.1  riastrad 	[TV_NORM_PAL_M] = "PAL-M",
     37  1.1  riastrad 	[TV_NORM_PAL_N] = "PAL-N",
     38  1.1  riastrad 	[TV_NORM_PAL_NC] = "PAL-Nc",
     39  1.1  riastrad 	[TV_NORM_PAL_60] = "PAL-60",
     40  1.1  riastrad 	[TV_NORM_NTSC_M] = "NTSC-M",
     41  1.1  riastrad 	[TV_NORM_NTSC_J] = "NTSC-J",
     42  1.1  riastrad };
     43  1.1  riastrad 
     44  1.1  riastrad #define NTSC_LIKE_TIMINGS .vrefresh = 60 * fixed1/1.001,		\
     45  1.1  riastrad 		.vdisplay = 480,					\
     46  1.1  riastrad 		.vtotal = 525,						\
     47  1.1  riastrad 		.hvirtual = 660
     48  1.1  riastrad 
     49  1.1  riastrad #define PAL_LIKE_TIMINGS .vrefresh = 50 * fixed1,		\
     50  1.1  riastrad 		.vdisplay = 576,				\
     51  1.1  riastrad 		.vtotal = 625,					\
     52  1.1  riastrad 		.hvirtual = 810
     53  1.1  riastrad 
     54  1.3  riastrad const struct ch7006_tv_norm_info ch7006_tv_norms[] = {
     55  1.1  riastrad 	[TV_NORM_NTSC_M] = {
     56  1.1  riastrad 		NTSC_LIKE_TIMINGS,
     57  1.1  riastrad 		.black_level = 0.339 * fixed1,
     58  1.1  riastrad 		.subc_freq = 3579545 * fixed1,
     59  1.1  riastrad 		.dispmode = bitfs(CH7006_DISPMODE_OUTPUT_STD, NTSC),
     60  1.1  riastrad 		.voffset = 0,
     61  1.1  riastrad 	},
     62  1.1  riastrad 	[TV_NORM_NTSC_J] = {
     63  1.1  riastrad 		NTSC_LIKE_TIMINGS,
     64  1.1  riastrad 		.black_level = 0.286 * fixed1,
     65  1.1  riastrad 		.subc_freq = 3579545 * fixed1,
     66  1.1  riastrad 		.dispmode = bitfs(CH7006_DISPMODE_OUTPUT_STD, NTSC_J),
     67  1.1  riastrad 		.voffset = 0,
     68  1.1  riastrad 	},
     69  1.1  riastrad 	[TV_NORM_PAL] = {
     70  1.1  riastrad 		PAL_LIKE_TIMINGS,
     71  1.1  riastrad 		.black_level = 0.3 * fixed1,
     72  1.1  riastrad 		.subc_freq = 4433618.75 * fixed1,
     73  1.1  riastrad 		.dispmode = bitfs(CH7006_DISPMODE_OUTPUT_STD, PAL),
     74  1.1  riastrad 		.voffset = 0,
     75  1.1  riastrad 	},
     76  1.1  riastrad 	[TV_NORM_PAL_M] = {
     77  1.1  riastrad 		NTSC_LIKE_TIMINGS,
     78  1.1  riastrad 		.black_level = 0.339 * fixed1,
     79  1.1  riastrad 		.subc_freq = 3575611.433 * fixed1,
     80  1.1  riastrad 		.dispmode = bitfs(CH7006_DISPMODE_OUTPUT_STD, PAL_M),
     81  1.1  riastrad 		.voffset = 16,
     82  1.1  riastrad 	},
     83  1.1  riastrad 
     84  1.1  riastrad 	/* The following modes seem to work right but they're
     85  1.1  riastrad 	 * undocumented */
     86  1.1  riastrad 
     87  1.1  riastrad 	[TV_NORM_PAL_N] = {
     88  1.1  riastrad 		PAL_LIKE_TIMINGS,
     89  1.1  riastrad 		.black_level = 0.339 * fixed1,
     90  1.1  riastrad 		.subc_freq = 4433618.75 * fixed1,
     91  1.1  riastrad 		.dispmode = bitfs(CH7006_DISPMODE_OUTPUT_STD, PAL),
     92  1.1  riastrad 		.voffset = 0,
     93  1.1  riastrad 	},
     94  1.1  riastrad 	[TV_NORM_PAL_NC] = {
     95  1.1  riastrad 		PAL_LIKE_TIMINGS,
     96  1.1  riastrad 		.black_level = 0.3 * fixed1,
     97  1.1  riastrad 		.subc_freq = 3582056.25 * fixed1,
     98  1.1  riastrad 		.dispmode = bitfs(CH7006_DISPMODE_OUTPUT_STD, PAL),
     99  1.1  riastrad 		.voffset = 0,
    100  1.1  riastrad 	},
    101  1.1  riastrad 	[TV_NORM_PAL_60] = {
    102  1.1  riastrad 		NTSC_LIKE_TIMINGS,
    103  1.1  riastrad 		.black_level = 0.3 * fixed1,
    104  1.1  riastrad 		.subc_freq = 4433618.75 * fixed1,
    105  1.1  riastrad 		.dispmode = bitfs(CH7006_DISPMODE_OUTPUT_STD, PAL_M),
    106  1.1  riastrad 		.voffset = 16,
    107  1.1  riastrad 	},
    108  1.1  riastrad };
    109  1.1  riastrad 
    110  1.1  riastrad #define __MODE(f, hd, vd, ht, vt, hsynp, vsynp,				\
    111  1.1  riastrad 	       subc, scale, scale_mask, norm_mask, e_hd, e_vd) {	\
    112  1.1  riastrad 		.mode = {						\
    113  1.1  riastrad 			.name = #hd "x" #vd,				\
    114  1.1  riastrad 			.status = 0,					\
    115  1.1  riastrad 			.type = DRM_MODE_TYPE_DRIVER,			\
    116  1.1  riastrad 			.clock = f,					\
    117  1.1  riastrad 			.hdisplay = hd,					\
    118  1.1  riastrad 			.hsync_start = e_hd + 16,			\
    119  1.1  riastrad 			.hsync_end = e_hd + 80,				\
    120  1.1  riastrad 			.htotal = ht,					\
    121  1.1  riastrad 			.hskew = 0,					\
    122  1.1  riastrad 			.vdisplay = vd,					\
    123  1.1  riastrad 			.vsync_start = vd + 10,				\
    124  1.1  riastrad 			.vsync_end = vd + 26,				\
    125  1.1  riastrad 			.vtotal = vt,					\
    126  1.1  riastrad 			.vscan = 0,					\
    127  1.1  riastrad 			.flags = DRM_MODE_FLAG_##hsynp##HSYNC |		\
    128  1.1  riastrad 				DRM_MODE_FLAG_##vsynp##VSYNC,		\
    129  1.1  riastrad 			.vrefresh = 0,					\
    130  1.1  riastrad 		},							\
    131  1.1  riastrad 		.enc_hdisp = e_hd,					\
    132  1.1  riastrad 		.enc_vdisp = e_vd,					\
    133  1.1  riastrad 		.subc_coeff = subc * fixed1,				\
    134  1.1  riastrad 		.dispmode = bitfs(CH7006_DISPMODE_SCALING_RATIO, scale) | \
    135  1.1  riastrad 			    bitfs(CH7006_DISPMODE_INPUT_RES, e_hd##x##e_vd), \
    136  1.1  riastrad 		.valid_scales = scale_mask,				\
    137  1.1  riastrad 		.valid_norms = norm_mask				\
    138  1.1  riastrad 	 }
    139  1.1  riastrad 
    140  1.1  riastrad #define MODE(f, hd, vd, ht, vt, hsynp, vsynp,				\
    141  1.1  riastrad 	     subc, scale, scale_mask, norm_mask)			\
    142  1.1  riastrad 	__MODE(f, hd, vd, ht, vt, hsynp, vsynp, subc, scale,		\
    143  1.1  riastrad 	       scale_mask, norm_mask, hd, vd)
    144  1.1  riastrad 
    145  1.1  riastrad #define NTSC_LIKE (1 << TV_NORM_NTSC_M | 1 << TV_NORM_NTSC_J |		\
    146  1.1  riastrad 		   1 << TV_NORM_PAL_M | 1 << TV_NORM_PAL_60)
    147  1.1  riastrad 
    148  1.1  riastrad #define PAL_LIKE (1 << TV_NORM_PAL | 1 << TV_NORM_PAL_N | 1 << TV_NORM_PAL_NC)
    149  1.1  riastrad 
    150  1.3  riastrad const struct ch7006_mode ch7006_modes[] = {
    151  1.1  riastrad 	MODE(21000, 512, 384, 840, 500, N, N, 181.797557582, 5_4, 0x6, PAL_LIKE),
    152  1.1  riastrad 	MODE(26250, 512, 384, 840, 625, N, N, 145.438046066, 1_1, 0x1, PAL_LIKE),
    153  1.1  riastrad 	MODE(20140, 512, 384, 800, 420, N, N, 213.257083791, 5_4, 0x4, NTSC_LIKE),
    154  1.1  riastrad 	MODE(24671, 512, 384, 784, 525, N, N, 174.0874153, 1_1, 0x3, NTSC_LIKE),
    155  1.1  riastrad 	MODE(28125, 720, 400, 1125, 500, N, N, 135.742176298, 5_4, 0x6, PAL_LIKE),
    156  1.1  riastrad 	MODE(34875, 720, 400, 1116, 625, N, N, 109.469496898, 1_1, 0x1, PAL_LIKE),
    157  1.1  riastrad 	MODE(23790, 720, 400, 945, 420, N, N, 160.475642016, 5_4, 0x4, NTSC_LIKE),
    158  1.1  riastrad 	MODE(29455, 720, 400, 936, 525, N, N, 129.614941843, 1_1, 0x3, NTSC_LIKE),
    159  1.1  riastrad 	MODE(25000, 640, 400, 1000, 500, N, N, 152.709948279, 5_4, 0x6, PAL_LIKE),
    160  1.1  riastrad 	MODE(31500, 640, 400, 1008, 625, N, N, 121.198371646, 1_1, 0x1, PAL_LIKE),
    161  1.1  riastrad 	MODE(21147, 640, 400, 840, 420, N, N, 180.535097338, 5_4, 0x4, NTSC_LIKE),
    162  1.1  riastrad 	MODE(26434, 640, 400, 840, 525, N, N, 144.42807787, 1_1, 0x2, NTSC_LIKE),
    163  1.1  riastrad 	MODE(30210, 640, 400, 840, 600, N, N, 126.374568276, 7_8, 0x1, NTSC_LIKE),
    164  1.1  riastrad 	MODE(21000, 640, 480, 840, 500, N, N, 181.797557582, 5_4, 0x4, PAL_LIKE),
    165  1.1  riastrad 	MODE(26250, 640, 480, 840, 625, N, N, 145.438046066, 1_1, 0x2, PAL_LIKE),
    166  1.1  riastrad 	MODE(31500, 640, 480, 840, 750, N, N, 121.198371646, 5_6, 0x1, PAL_LIKE),
    167  1.1  riastrad 	MODE(24671, 640, 480, 784, 525, N, N, 174.0874153, 1_1, 0x4, NTSC_LIKE),
    168  1.1  riastrad 	MODE(28196, 640, 480, 784, 600, N, N, 152.326488422, 7_8, 0x2, NTSC_LIKE),
    169  1.1  riastrad 	MODE(30210, 640, 480, 800, 630, N, N, 142.171389101, 5_6, 0x1, NTSC_LIKE),
    170  1.1  riastrad 	__MODE(29500, 720, 576, 944, 625, P, P, 145.592111636, 1_1, 0x7, PAL_LIKE, 800, 600),
    171  1.1  riastrad 	MODE(36000, 800, 600, 960, 750, P, P, 119.304647022, 5_6, 0x6, PAL_LIKE),
    172  1.1  riastrad 	MODE(39000, 800, 600, 936, 836, P, P, 110.127366499, 3_4, 0x1, PAL_LIKE),
    173  1.1  riastrad 	MODE(39273, 800, 600, 1040, 630, P, P, 145.816809399, 5_6, 0x4, NTSC_LIKE),
    174  1.1  riastrad 	MODE(43636, 800, 600, 1040, 700, P, P, 131.235128487, 3_4, 0x2, NTSC_LIKE),
    175  1.1  riastrad 	MODE(47832, 800, 600, 1064, 750, P, P, 119.723275165, 7_10, 0x1, NTSC_LIKE),
    176  1.1  riastrad 	{}
    177  1.1  riastrad };
    178  1.1  riastrad 
    179  1.3  riastrad const struct ch7006_mode *ch7006_lookup_mode(struct drm_encoder *encoder,
    180  1.3  riastrad 					     const struct drm_display_mode *drm_mode)
    181  1.1  riastrad {
    182  1.1  riastrad 	struct ch7006_priv *priv = to_ch7006_priv(encoder);
    183  1.3  riastrad 	const struct ch7006_mode *mode;
    184  1.1  riastrad 
    185  1.1  riastrad 	for (mode = ch7006_modes; mode->mode.clock; mode++) {
    186  1.1  riastrad 
    187  1.1  riastrad 		if (~mode->valid_norms & 1<<priv->norm)
    188  1.1  riastrad 			continue;
    189  1.1  riastrad 
    190  1.1  riastrad 		if (mode->mode.hdisplay != drm_mode->hdisplay ||
    191  1.1  riastrad 		    mode->mode.vdisplay != drm_mode->vdisplay ||
    192  1.1  riastrad 		    mode->mode.vtotal != drm_mode->vtotal ||
    193  1.1  riastrad 		    mode->mode.htotal != drm_mode->htotal ||
    194  1.1  riastrad 		    mode->mode.clock != drm_mode->clock)
    195  1.1  riastrad 			continue;
    196  1.1  riastrad 
    197  1.1  riastrad 		return mode;
    198  1.1  riastrad 	}
    199  1.1  riastrad 
    200  1.1  riastrad 	return NULL;
    201  1.1  riastrad }
    202  1.1  riastrad 
    203  1.1  riastrad /* Some common HW state calculation code */
    204  1.1  riastrad 
    205  1.1  riastrad void ch7006_setup_levels(struct drm_encoder *encoder)
    206  1.1  riastrad {
    207  1.1  riastrad 	struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
    208  1.1  riastrad 	struct ch7006_priv *priv = to_ch7006_priv(encoder);
    209  1.1  riastrad 	uint8_t *regs = priv->state.regs;
    210  1.3  riastrad 	const struct ch7006_tv_norm_info *norm = &ch7006_tv_norms[priv->norm];
    211  1.1  riastrad 	int gain;
    212  1.1  riastrad 	int black_level;
    213  1.1  riastrad 
    214  1.1  riastrad 	/* Set DAC_GAIN if the voltage drop between white and black is
    215  1.1  riastrad 	 * high enough. */
    216  1.1  riastrad 	if (norm->black_level < 339*fixed1/1000) {
    217  1.1  riastrad 		gain = 76;
    218  1.1  riastrad 
    219  1.1  riastrad 		regs[CH7006_INPUT_FORMAT] |= CH7006_INPUT_FORMAT_DAC_GAIN;
    220  1.1  riastrad 	} else {
    221  1.1  riastrad 		gain = 71;
    222  1.1  riastrad 
    223  1.1  riastrad 		regs[CH7006_INPUT_FORMAT] &= ~CH7006_INPUT_FORMAT_DAC_GAIN;
    224  1.1  riastrad 	}
    225  1.1  riastrad 
    226  1.1  riastrad 	black_level = round_fixed(norm->black_level*26625)/gain;
    227  1.1  riastrad 
    228  1.1  riastrad 	/* Correct it with the specified brightness. */
    229  1.1  riastrad 	black_level = interpolate(90, black_level, 208, priv->brightness);
    230  1.1  riastrad 
    231  1.1  riastrad 	regs[CH7006_BLACK_LEVEL] = bitf(CH7006_BLACK_LEVEL_0, black_level);
    232  1.1  riastrad 
    233  1.1  riastrad 	ch7006_dbg(client, "black level: %d\n", black_level);
    234  1.1  riastrad }
    235  1.1  riastrad 
    236  1.1  riastrad void ch7006_setup_subcarrier(struct drm_encoder *encoder)
    237  1.1  riastrad {
    238  1.1  riastrad 	struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
    239  1.1  riastrad 	struct ch7006_priv *priv = to_ch7006_priv(encoder);
    240  1.1  riastrad 	struct ch7006_state *state = &priv->state;
    241  1.3  riastrad 	const struct ch7006_tv_norm_info *norm = &ch7006_tv_norms[priv->norm];
    242  1.3  riastrad 	const struct ch7006_mode *mode = priv->mode;
    243  1.1  riastrad 	uint32_t subc_inc;
    244  1.1  riastrad 
    245  1.1  riastrad 	subc_inc = round_fixed((mode->subc_coeff >> 8)
    246  1.1  riastrad 			       * (norm->subc_freq >> 24));
    247  1.1  riastrad 
    248  1.1  riastrad 	setbitf(state, CH7006_SUBC_INC0, 28, subc_inc);
    249  1.1  riastrad 	setbitf(state, CH7006_SUBC_INC1, 24, subc_inc);
    250  1.1  riastrad 	setbitf(state, CH7006_SUBC_INC2, 20, subc_inc);
    251  1.1  riastrad 	setbitf(state, CH7006_SUBC_INC3, 16, subc_inc);
    252  1.1  riastrad 	setbitf(state, CH7006_SUBC_INC4, 12, subc_inc);
    253  1.1  riastrad 	setbitf(state, CH7006_SUBC_INC5, 8, subc_inc);
    254  1.1  riastrad 	setbitf(state, CH7006_SUBC_INC6, 4, subc_inc);
    255  1.1  riastrad 	setbitf(state, CH7006_SUBC_INC7, 0, subc_inc);
    256  1.1  riastrad 
    257  1.1  riastrad 	ch7006_dbg(client, "subcarrier inc: %u\n", subc_inc);
    258  1.1  riastrad }
    259  1.1  riastrad 
    260  1.1  riastrad void ch7006_setup_pll(struct drm_encoder *encoder)
    261  1.1  riastrad {
    262  1.1  riastrad 	struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
    263  1.1  riastrad 	struct ch7006_priv *priv = to_ch7006_priv(encoder);
    264  1.1  riastrad 	uint8_t *regs = priv->state.regs;
    265  1.3  riastrad 	const struct ch7006_mode *mode = priv->mode;
    266  1.1  riastrad 	int n, best_n = 0;
    267  1.1  riastrad 	int m, best_m = 0;
    268  1.1  riastrad 	int freq, best_freq = 0;
    269  1.1  riastrad 
    270  1.1  riastrad 	for (n = 0; n < CH7006_MAXN; n++) {
    271  1.1  riastrad 		for (m = 0; m < CH7006_MAXM; m++) {
    272  1.1  riastrad 			freq = CH7006_FREQ0*(n+2)/(m+2);
    273  1.1  riastrad 
    274  1.1  riastrad 			if (abs(freq - mode->mode.clock) <
    275  1.1  riastrad 			    abs(best_freq - mode->mode.clock)) {
    276  1.1  riastrad 				best_freq = freq;
    277  1.1  riastrad 				best_n = n;
    278  1.1  riastrad 				best_m = m;
    279  1.1  riastrad 			}
    280  1.1  riastrad 		}
    281  1.1  riastrad 	}
    282  1.1  riastrad 
    283  1.1  riastrad 	regs[CH7006_PLLOV] = bitf(CH7006_PLLOV_N_8, best_n) |
    284  1.1  riastrad 		bitf(CH7006_PLLOV_M_8, best_m);
    285  1.1  riastrad 
    286  1.1  riastrad 	regs[CH7006_PLLM] = bitf(CH7006_PLLM_0, best_m);
    287  1.1  riastrad 	regs[CH7006_PLLN] = bitf(CH7006_PLLN_0, best_n);
    288  1.1  riastrad 
    289  1.1  riastrad 	if (best_n < 108)
    290  1.1  riastrad 		regs[CH7006_PLL_CONTROL] |= CH7006_PLL_CONTROL_CAPACITOR;
    291  1.1  riastrad 	else
    292  1.1  riastrad 		regs[CH7006_PLL_CONTROL] &= ~CH7006_PLL_CONTROL_CAPACITOR;
    293  1.1  riastrad 
    294  1.1  riastrad 	ch7006_dbg(client, "n=%d m=%d f=%d c=%d\n",
    295  1.1  riastrad 		   best_n, best_m, best_freq, best_n < 108);
    296  1.1  riastrad }
    297  1.1  riastrad 
    298  1.1  riastrad void ch7006_setup_power_state(struct drm_encoder *encoder)
    299  1.1  riastrad {
    300  1.1  riastrad 	struct ch7006_priv *priv = to_ch7006_priv(encoder);
    301  1.1  riastrad 	uint8_t *power = &priv->state.regs[CH7006_POWER];
    302  1.1  riastrad 	int subconnector;
    303  1.1  riastrad 
    304  1.1  riastrad 	subconnector = priv->select_subconnector ? priv->select_subconnector :
    305  1.1  riastrad 							priv->subconnector;
    306  1.1  riastrad 
    307  1.1  riastrad 	*power = CH7006_POWER_RESET;
    308  1.1  riastrad 
    309  1.1  riastrad 	if (priv->last_dpms == DRM_MODE_DPMS_ON) {
    310  1.1  riastrad 		switch (subconnector) {
    311  1.1  riastrad 		case DRM_MODE_SUBCONNECTOR_SVIDEO:
    312  1.1  riastrad 			*power |= bitfs(CH7006_POWER_LEVEL, CVBS_OFF);
    313  1.1  riastrad 			break;
    314  1.1  riastrad 		case DRM_MODE_SUBCONNECTOR_Composite:
    315  1.1  riastrad 			*power |= bitfs(CH7006_POWER_LEVEL, SVIDEO_OFF);
    316  1.1  riastrad 			break;
    317  1.1  riastrad 		case DRM_MODE_SUBCONNECTOR_SCART:
    318  1.1  riastrad 			*power |= bitfs(CH7006_POWER_LEVEL, NORMAL) |
    319  1.1  riastrad 				CH7006_POWER_SCART;
    320  1.1  riastrad 			break;
    321  1.1  riastrad 		}
    322  1.1  riastrad 
    323  1.1  riastrad 	} else {
    324  1.1  riastrad 		if (priv->chip_version >= 0x20)
    325  1.1  riastrad 			*power |= bitfs(CH7006_POWER_LEVEL, FULL_POWER_OFF);
    326  1.1  riastrad 		else
    327  1.1  riastrad 			*power |= bitfs(CH7006_POWER_LEVEL, POWER_OFF);
    328  1.1  riastrad 	}
    329  1.1  riastrad }
    330  1.1  riastrad 
    331  1.1  riastrad void ch7006_setup_properties(struct drm_encoder *encoder)
    332  1.1  riastrad {
    333  1.1  riastrad 	struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
    334  1.1  riastrad 	struct ch7006_priv *priv = to_ch7006_priv(encoder);
    335  1.1  riastrad 	struct ch7006_state *state = &priv->state;
    336  1.3  riastrad 	const struct ch7006_tv_norm_info *norm = &ch7006_tv_norms[priv->norm];
    337  1.3  riastrad 	const struct ch7006_mode *ch_mode = priv->mode;
    338  1.3  riastrad 	const struct drm_display_mode *mode = &ch_mode->mode;
    339  1.1  riastrad 	uint8_t *regs = state->regs;
    340  1.1  riastrad 	int flicker, contrast, hpos, vpos;
    341  1.1  riastrad 	uint64_t scale, aspect;
    342  1.1  riastrad 
    343  1.1  riastrad 	flicker = interpolate(0, 2, 3, priv->flicker);
    344  1.1  riastrad 	regs[CH7006_FFILTER] = bitf(CH7006_FFILTER_TEXT, flicker) |
    345  1.1  riastrad 		bitf(CH7006_FFILTER_LUMA, flicker) |
    346  1.1  riastrad 		bitf(CH7006_FFILTER_CHROMA, 1);
    347  1.1  riastrad 
    348  1.1  riastrad 	contrast = interpolate(0, 5, 7, priv->contrast);
    349  1.1  riastrad 	regs[CH7006_CONTRAST] = bitf(CH7006_CONTRAST_0, contrast);
    350  1.1  riastrad 
    351  1.1  riastrad 	scale = norm->vtotal*fixed1;
    352  1.1  riastrad 	do_div(scale, mode->vtotal);
    353  1.1  riastrad 
    354  1.1  riastrad 	aspect = ch_mode->enc_hdisp*fixed1;
    355  1.1  riastrad 	do_div(aspect, ch_mode->enc_vdisp);
    356  1.1  riastrad 
    357  1.1  riastrad 	hpos = round_fixed((norm->hvirtual * aspect - mode->hdisplay * scale)
    358  1.1  riastrad 			   * priv->hmargin * mode->vtotal) / norm->vtotal / 100 / 4;
    359  1.1  riastrad 
    360  1.1  riastrad 	setbitf(state, CH7006_POV, HPOS_8, hpos);
    361  1.1  riastrad 	setbitf(state, CH7006_HPOS, 0, hpos);
    362  1.1  riastrad 
    363  1.1  riastrad 	vpos = max(0, norm->vdisplay - round_fixed(mode->vdisplay*scale)
    364  1.1  riastrad 		   + norm->voffset) * priv->vmargin / 100 / 2;
    365  1.1  riastrad 
    366  1.1  riastrad 	setbitf(state, CH7006_POV, VPOS_8, vpos);
    367  1.1  riastrad 	setbitf(state, CH7006_VPOS, 0, vpos);
    368  1.1  riastrad 
    369  1.1  riastrad 	ch7006_dbg(client, "hpos: %d, vpos: %d\n", hpos, vpos);
    370  1.1  riastrad }
    371  1.1  riastrad 
    372  1.1  riastrad /* HW access functions */
    373  1.1  riastrad 
    374  1.1  riastrad void ch7006_write(struct i2c_client *client, uint8_t addr, uint8_t val)
    375  1.1  riastrad {
    376  1.1  riastrad 	uint8_t buf[] = {addr, val};
    377  1.1  riastrad 	int ret;
    378  1.1  riastrad 
    379  1.1  riastrad 	ret = i2c_master_send(client, buf, ARRAY_SIZE(buf));
    380  1.1  riastrad 	if (ret < 0)
    381  1.1  riastrad 		ch7006_err(client, "Error %d writing to subaddress 0x%x\n",
    382  1.1  riastrad 			   ret, addr);
    383  1.1  riastrad }
    384  1.1  riastrad 
    385  1.1  riastrad uint8_t ch7006_read(struct i2c_client *client, uint8_t addr)
    386  1.1  riastrad {
    387  1.1  riastrad 	uint8_t val;
    388  1.1  riastrad 	int ret;
    389  1.1  riastrad 
    390  1.1  riastrad 	ret = i2c_master_send(client, &addr, sizeof(addr));
    391  1.1  riastrad 	if (ret < 0)
    392  1.1  riastrad 		goto fail;
    393  1.1  riastrad 
    394  1.1  riastrad 	ret = i2c_master_recv(client, &val, sizeof(val));
    395  1.1  riastrad 	if (ret < 0)
    396  1.1  riastrad 		goto fail;
    397  1.1  riastrad 
    398  1.1  riastrad 	return val;
    399  1.1  riastrad 
    400  1.1  riastrad fail:
    401  1.1  riastrad 	ch7006_err(client, "Error %d reading from subaddress 0x%x\n",
    402  1.1  riastrad 		   ret, addr);
    403  1.1  riastrad 	return 0;
    404  1.1  riastrad }
    405  1.1  riastrad 
    406  1.1  riastrad void ch7006_state_load(struct i2c_client *client,
    407  1.1  riastrad 		       struct ch7006_state *state)
    408  1.1  riastrad {
    409  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_POWER);
    410  1.1  riastrad 
    411  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_DISPMODE);
    412  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_FFILTER);
    413  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_BWIDTH);
    414  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_INPUT_FORMAT);
    415  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_CLKMODE);
    416  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_START_ACTIVE);
    417  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_POV);
    418  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_BLACK_LEVEL);
    419  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_HPOS);
    420  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_VPOS);
    421  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_INPUT_SYNC);
    422  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_DETECT);
    423  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_CONTRAST);
    424  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_PLLOV);
    425  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_PLLM);
    426  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_PLLN);
    427  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_BCLKOUT);
    428  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_SUBC_INC0);
    429  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_SUBC_INC1);
    430  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_SUBC_INC2);
    431  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_SUBC_INC3);
    432  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_SUBC_INC4);
    433  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_SUBC_INC5);
    434  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_SUBC_INC6);
    435  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_SUBC_INC7);
    436  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_PLL_CONTROL);
    437  1.1  riastrad 	ch7006_load_reg(client, state, CH7006_CALC_SUBC_INC0);
    438  1.1  riastrad }
    439  1.1  riastrad 
    440  1.1  riastrad void ch7006_state_save(struct i2c_client *client,
    441  1.1  riastrad 		       struct ch7006_state *state)
    442  1.1  riastrad {
    443  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_POWER);
    444  1.1  riastrad 
    445  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_DISPMODE);
    446  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_FFILTER);
    447  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_BWIDTH);
    448  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_INPUT_FORMAT);
    449  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_CLKMODE);
    450  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_START_ACTIVE);
    451  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_POV);
    452  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_BLACK_LEVEL);
    453  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_HPOS);
    454  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_VPOS);
    455  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_INPUT_SYNC);
    456  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_DETECT);
    457  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_CONTRAST);
    458  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_PLLOV);
    459  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_PLLM);
    460  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_PLLN);
    461  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_BCLKOUT);
    462  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_SUBC_INC0);
    463  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_SUBC_INC1);
    464  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_SUBC_INC2);
    465  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_SUBC_INC3);
    466  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_SUBC_INC4);
    467  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_SUBC_INC5);
    468  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_SUBC_INC6);
    469  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_SUBC_INC7);
    470  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_PLL_CONTROL);
    471  1.1  riastrad 	ch7006_save_reg(client, state, CH7006_CALC_SUBC_INC0);
    472  1.1  riastrad 
    473  1.1  riastrad 	state->regs[CH7006_FFILTER] = (state->regs[CH7006_FFILTER] & 0xf0) |
    474  1.1  riastrad 		(state->regs[CH7006_FFILTER] & 0x0c) >> 2 |
    475  1.1  riastrad 		(state->regs[CH7006_FFILTER] & 0x03) << 2;
    476  1.1  riastrad }
    477