1 /*	$NetBSD: nouveau_dispnv04_tvnv04.c,v 1.2 2014/08/06 15:01:34 riastradh Exp $	*/
2 
3 /*
4  * Copyright (C) 2009 Francisco Jerez.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial
17  * portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: nouveau_dispnv04_tvnv04.c,v 1.2 2014/08/06 15:01:34 riastradh Exp $");
31 
32 #include <linux/err.h>
33 
34 #include <drm/drmP.h>
35 #include "nouveau_drm.h"
36 #include "nouveau_reg.h"
37 #include "nouveau_encoder.h"
38 #include "nouveau_connector.h"
39 #include "nouveau_crtc.h"
40 #include "hw.h"
41 #include <drm/drm_crtc_helper.h>
42 
43 #include <drm/i2c/ch7006.h>
44 
45 #include <subdev/i2c.h>
46 
47 static struct nouveau_i2c_board_info nv04_tv_encoder_info[] = {
48 	{
49 		{
50 			I2C_BOARD_INFO("ch7006", 0x75),
51 			.platform_data = &(struct ch7006_encoder_params) {
52 				CH7006_FORMAT_RGB24m12I, CH7006_CLOCK_MASTER,
53 				0, 0, 0,
54 				CH7006_SYNC_SLAVE, CH7006_SYNC_SEPARATED,
55 				CH7006_POUT_3_3V, CH7006_ACTIVE_HSYNC
56 			}
57 		},
58 		0
59 	},
60 	{ }
61 };
62 
nv04_tv_identify(struct drm_device * dev,int i2c_index)63 int nv04_tv_identify(struct drm_device *dev, int i2c_index)
64 {
65 	struct nouveau_drm *drm = nouveau_drm(dev);
66 	struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
67 
68 	return i2c->identify(i2c, i2c_index, "TV encoder",
69 			     nv04_tv_encoder_info, NULL, NULL);
70 }
71 
72 
73 #define PLLSEL_TV_CRTC1_MASK				\
74 	(NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK1		\
75 	 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK1)
76 #define PLLSEL_TV_CRTC2_MASK				\
77 	(NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK2		\
78 	 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK2)
79 
nv04_tv_dpms(struct drm_encoder * encoder,int mode)80 static void nv04_tv_dpms(struct drm_encoder *encoder, int mode)
81 {
82 	struct drm_device *dev = encoder->dev;
83 	struct nouveau_drm *drm = nouveau_drm(dev);
84 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
85 	struct nv04_mode_state *state = &nv04_display(dev)->mode_reg;
86 	uint8_t crtc1A;
87 
88 	NV_DEBUG(drm, "Setting dpms mode %d on TV encoder (output %d)\n",
89 		 mode, nv_encoder->dcb->index);
90 
91 	state->pllsel &= ~(PLLSEL_TV_CRTC1_MASK | PLLSEL_TV_CRTC2_MASK);
92 
93 	if (mode == DRM_MODE_DPMS_ON) {
94 		int head = nouveau_crtc(encoder->crtc)->index;
95 		crtc1A = NVReadVgaCrtc(dev, head, NV_CIO_CRE_RPC1_INDEX);
96 
97 		state->pllsel |= head ? PLLSEL_TV_CRTC2_MASK :
98 					PLLSEL_TV_CRTC1_MASK;
99 
100 		/* Inhibit hsync */
101 		crtc1A |= 0x80;
102 
103 		NVWriteVgaCrtc(dev, head, NV_CIO_CRE_RPC1_INDEX, crtc1A);
104 	}
105 
106 	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_PLL_COEFF_SELECT, state->pllsel);
107 
108 	get_slave_funcs(encoder)->dpms(encoder, mode);
109 }
110 
nv04_tv_bind(struct drm_device * dev,int head,bool bind)111 static void nv04_tv_bind(struct drm_device *dev, int head, bool bind)
112 {
113 	struct nv04_crtc_reg *state = &nv04_display(dev)->mode_reg.crtc_reg[head];
114 
115 	state->tv_setup = 0;
116 
117 	if (bind)
118 		state->CRTC[NV_CIO_CRE_49] |= 0x10;
119 	else
120 		state->CRTC[NV_CIO_CRE_49] &= ~0x10;
121 
122 	NVWriteVgaCrtc(dev, head, NV_CIO_CRE_LCD__INDEX,
123 		       state->CRTC[NV_CIO_CRE_LCD__INDEX]);
124 	NVWriteVgaCrtc(dev, head, NV_CIO_CRE_49,
125 		       state->CRTC[NV_CIO_CRE_49]);
126 	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_SETUP,
127 		      state->tv_setup);
128 }
129 
nv04_tv_prepare(struct drm_encoder * encoder)130 static void nv04_tv_prepare(struct drm_encoder *encoder)
131 {
132 	struct drm_device *dev = encoder->dev;
133 	int head = nouveau_crtc(encoder->crtc)->index;
134 	struct drm_encoder_helper_funcs *helper = encoder->helper_private;
135 
136 	helper->dpms(encoder, DRM_MODE_DPMS_OFF);
137 
138 	nv04_dfp_disable(dev, head);
139 
140 	if (nv_two_heads(dev))
141 		nv04_tv_bind(dev, head ^ 1, false);
142 
143 	nv04_tv_bind(dev, head, true);
144 }
145 
nv04_tv_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)146 static void nv04_tv_mode_set(struct drm_encoder *encoder,
147 			     struct drm_display_mode *mode,
148 			     struct drm_display_mode *adjusted_mode)
149 {
150 	struct drm_device *dev = encoder->dev;
151 	struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
152 	struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
153 
154 	regp->tv_htotal = adjusted_mode->htotal;
155 	regp->tv_vtotal = adjusted_mode->vtotal;
156 
157 	/* These delay the TV signals with respect to the VGA port,
158 	 * they might be useful if we ever allow a CRTC to drive
159 	 * multiple outputs.
160 	 */
161 	regp->tv_hskew = 1;
162 	regp->tv_hsync_delay = 1;
163 	regp->tv_hsync_delay2 = 64;
164 	regp->tv_vskew = 1;
165 	regp->tv_vsync_delay = 1;
166 
167 	get_slave_funcs(encoder)->mode_set(encoder, mode, adjusted_mode);
168 }
169 
nv04_tv_commit(struct drm_encoder * encoder)170 static void nv04_tv_commit(struct drm_encoder *encoder)
171 {
172 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
173 	struct drm_device *dev = encoder->dev;
174 	struct nouveau_drm *drm = nouveau_drm(dev);
175 	struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
176 	struct drm_encoder_helper_funcs *helper = encoder->helper_private;
177 
178 	helper->dpms(encoder, DRM_MODE_DPMS_ON);
179 
180 	NV_DEBUG(drm, "Output %s is running on CRTC %d using output %c\n",
181 		 drm_get_connector_name(&nouveau_encoder_connector_get(nv_encoder)->base), nv_crtc->index, '@' + ffs(nv_encoder->dcb->or));
182 }
183 
nv04_tv_destroy(struct drm_encoder * encoder)184 static void nv04_tv_destroy(struct drm_encoder *encoder)
185 {
186 	get_slave_funcs(encoder)->destroy(encoder);
187 	drm_encoder_cleanup(encoder);
188 
189 	kfree(encoder->helper_private);
190 	kfree(nouveau_encoder(encoder));
191 }
192 
193 static const struct drm_encoder_funcs nv04_tv_funcs = {
194 	.destroy = nv04_tv_destroy,
195 };
196 
197 static const struct drm_encoder_helper_funcs nv04_tv_helper_funcs = {
198 	.dpms = nv04_tv_dpms,
199 	.save = drm_i2c_encoder_save,
200 	.restore = drm_i2c_encoder_restore,
201 	.mode_fixup = drm_i2c_encoder_mode_fixup,
202 	.prepare = nv04_tv_prepare,
203 	.commit = nv04_tv_commit,
204 	.mode_set = nv04_tv_mode_set,
205 	.detect = drm_i2c_encoder_detect,
206 };
207 
208 int
nv04_tv_create(struct drm_connector * connector,struct dcb_output * entry)209 nv04_tv_create(struct drm_connector *connector, struct dcb_output *entry)
210 {
211 	struct nouveau_encoder *nv_encoder;
212 	struct drm_encoder *encoder;
213 	struct drm_device *dev = connector->dev;
214 	struct nouveau_drm *drm = nouveau_drm(dev);
215 	struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
216 	struct nouveau_i2c_port *port = i2c->find(i2c, entry->i2c_index);
217 	int type, ret;
218 
219 	/* Ensure that we can talk to this encoder */
220 	type = nv04_tv_identify(dev, entry->i2c_index);
221 	if (type < 0)
222 		return type;
223 
224 	/* Allocate the necessary memory */
225 	nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
226 	if (!nv_encoder)
227 		return -ENOMEM;
228 
229 	/* Initialize the common members */
230 	encoder = to_drm_encoder(nv_encoder);
231 
232 	drm_encoder_init(dev, encoder, &nv04_tv_funcs, DRM_MODE_ENCODER_TVDAC);
233 	drm_encoder_helper_add(encoder, &nv04_tv_helper_funcs);
234 
235 	encoder->possible_crtcs = entry->heads;
236 	encoder->possible_clones = 0;
237 	nv_encoder->dcb = entry;
238 	nv_encoder->or = ffs(entry->or) - 1;
239 
240 	/* Run the slave-specific initialization */
241 	ret = drm_i2c_encoder_init(dev, to_encoder_slave(encoder),
242 				   &port->adapter,
243 				   &nv04_tv_encoder_info[type].dev);
244 	if (ret < 0)
245 		goto fail_cleanup;
246 
247 	/* Attach it to the specified connector. */
248 	get_slave_funcs(encoder)->create_resources(encoder, connector);
249 	drm_mode_connector_attach_encoder(connector, encoder);
250 
251 	return 0;
252 
253 fail_cleanup:
254 	drm_encoder_cleanup(encoder);
255 	kfree(nv_encoder);
256 	return ret;
257 }
258