1 /* $NetBSD: nouveau_dispnv04_tvnv04.c,v 1.6 2021/12/18 23:45:32 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.6 2021/12/18 23:45:32 riastradh Exp $");
31
32 #include "nouveau_drv.h"
33 #include "nouveau_reg.h"
34 #include "nouveau_encoder.h"
35 #include "nouveau_connector.h"
36 #include "nouveau_crtc.h"
37 #include "hw.h"
38 #include <drm/drm_crtc_helper.h>
39
40 #include <drm/i2c/ch7006.h>
41
42 static struct nvkm_i2c_bus_probe nv04_tv_encoder_info[] = {
43 {
44 {
45 I2C_BOARD_INFO("ch7006", 0x75),
46 .platform_data = &(struct ch7006_encoder_params) {
47 CH7006_FORMAT_RGB24m12I, CH7006_CLOCK_MASTER,
48 0, 0, 0,
49 CH7006_SYNC_SLAVE, CH7006_SYNC_SEPARATED,
50 CH7006_POUT_3_3V, CH7006_ACTIVE_HSYNC
51 }
52 },
53 0
54 },
55 { }
56 };
57
nv04_tv_identify(struct drm_device * dev,int i2c_index)58 int nv04_tv_identify(struct drm_device *dev, int i2c_index)
59 {
60 struct nouveau_drm *drm = nouveau_drm(dev);
61 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
62 struct nvkm_i2c_bus *bus = nvkm_i2c_bus_find(i2c, i2c_index);
63 if (bus) {
64 return nvkm_i2c_bus_probe(bus, "TV encoder",
65 nv04_tv_encoder_info,
66 NULL, NULL);
67 }
68 return -ENODEV;
69 }
70
71
72 #define PLLSEL_TV_CRTC1_MASK \
73 (NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK1 \
74 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK1)
75 #define PLLSEL_TV_CRTC2_MASK \
76 (NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK2 \
77 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK2)
78
nv04_tv_dpms(struct drm_encoder * encoder,int mode)79 static void nv04_tv_dpms(struct drm_encoder *encoder, int mode)
80 {
81 struct drm_device *dev = encoder->dev;
82 struct nouveau_drm *drm = nouveau_drm(dev);
83 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
84 struct nv04_mode_state *state = &nv04_display(dev)->mode_reg;
85 uint8_t crtc1A;
86
87 NV_DEBUG(drm, "Setting dpms mode %d on TV encoder (output %d)\n",
88 mode, nv_encoder->dcb->index);
89
90 state->pllsel &= ~(PLLSEL_TV_CRTC1_MASK | PLLSEL_TV_CRTC2_MASK);
91
92 if (mode == DRM_MODE_DPMS_ON) {
93 int head = nouveau_crtc(encoder->crtc)->index;
94 crtc1A = NVReadVgaCrtc(dev, head, NV_CIO_CRE_RPC1_INDEX);
95
96 state->pllsel |= head ? PLLSEL_TV_CRTC2_MASK :
97 PLLSEL_TV_CRTC1_MASK;
98
99 /* Inhibit hsync */
100 crtc1A |= 0x80;
101
102 NVWriteVgaCrtc(dev, head, NV_CIO_CRE_RPC1_INDEX, crtc1A);
103 }
104
105 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_PLL_COEFF_SELECT, state->pllsel);
106
107 get_slave_funcs(encoder)->dpms(encoder, mode);
108 }
109
nv04_tv_bind(struct drm_device * dev,int head,bool bind)110 static void nv04_tv_bind(struct drm_device *dev, int head, bool bind)
111 {
112 struct nv04_crtc_reg *state = &nv04_display(dev)->mode_reg.crtc_reg[head];
113
114 state->tv_setup = 0;
115
116 if (bind)
117 state->CRTC[NV_CIO_CRE_49] |= 0x10;
118 else
119 state->CRTC[NV_CIO_CRE_49] &= ~0x10;
120
121 NVWriteVgaCrtc(dev, head, NV_CIO_CRE_LCD__INDEX,
122 state->CRTC[NV_CIO_CRE_LCD__INDEX]);
123 NVWriteVgaCrtc(dev, head, NV_CIO_CRE_49,
124 state->CRTC[NV_CIO_CRE_49]);
125 NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_SETUP,
126 state->tv_setup);
127 }
128
nv04_tv_prepare(struct drm_encoder * encoder)129 static void nv04_tv_prepare(struct drm_encoder *encoder)
130 {
131 struct drm_device *dev = encoder->dev;
132 int head = nouveau_crtc(encoder->crtc)->index;
133 const struct drm_encoder_helper_funcs *helper = encoder->helper_private;
134
135 helper->dpms(encoder, DRM_MODE_DPMS_OFF);
136
137 nv04_dfp_disable(dev, head);
138
139 if (nv_two_heads(dev))
140 nv04_tv_bind(dev, head ^ 1, false);
141
142 nv04_tv_bind(dev, head, true);
143 }
144
nv04_tv_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)145 static void nv04_tv_mode_set(struct drm_encoder *encoder,
146 struct drm_display_mode *mode,
147 struct drm_display_mode *adjusted_mode)
148 {
149 struct drm_device *dev = encoder->dev;
150 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
151 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
152
153 regp->tv_htotal = adjusted_mode->htotal;
154 regp->tv_vtotal = adjusted_mode->vtotal;
155
156 /* These delay the TV signals with respect to the VGA port,
157 * they might be useful if we ever allow a CRTC to drive
158 * multiple outputs.
159 */
160 regp->tv_hskew = 1;
161 regp->tv_hsync_delay = 1;
162 regp->tv_hsync_delay2 = 64;
163 regp->tv_vskew = 1;
164 regp->tv_vsync_delay = 1;
165
166 get_slave_funcs(encoder)->mode_set(encoder, mode, adjusted_mode);
167 }
168
nv04_tv_commit(struct drm_encoder * encoder)169 static void nv04_tv_commit(struct drm_encoder *encoder)
170 {
171 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
172 struct drm_device *dev = encoder->dev;
173 struct nouveau_drm *drm = nouveau_drm(dev);
174 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
175 const struct drm_encoder_helper_funcs *helper = encoder->helper_private;
176
177 helper->dpms(encoder, DRM_MODE_DPMS_ON);
178
179 NV_DEBUG(drm, "Output %s is running on CRTC %d using output %c\n",
180 nouveau_encoder_connector_get(nv_encoder)->base.name,
181 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 #ifndef __NetBSD__ /* XXX How can this possibly be right? */
190 kfree(encoder->helper_private);
191 #endif
192 kfree(nouveau_encoder(encoder));
193 }
194
195 static const struct drm_encoder_funcs nv04_tv_funcs = {
196 .destroy = nv04_tv_destroy,
197 };
198
199 static const struct drm_encoder_helper_funcs nv04_tv_helper_funcs = {
200 .dpms = nv04_tv_dpms,
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 nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
216 struct nvkm_i2c_bus *bus = nvkm_i2c_bus_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 NULL);
234 drm_encoder_helper_add(encoder, &nv04_tv_helper_funcs);
235
236 nv_encoder->enc_save = drm_i2c_encoder_save;
237 nv_encoder->enc_restore = drm_i2c_encoder_restore;
238
239 encoder->possible_crtcs = entry->heads;
240 encoder->possible_clones = 0;
241 nv_encoder->dcb = entry;
242 nv_encoder->or = ffs(entry->or) - 1;
243
244 /* Run the slave-specific initialization */
245 ret = drm_i2c_encoder_init(dev, to_encoder_slave(encoder),
246 &bus->i2c,
247 &nv04_tv_encoder_info[type].dev);
248 if (ret < 0)
249 goto fail_cleanup;
250
251 /* Attach it to the specified connector. */
252 get_slave_funcs(encoder)->create_resources(encoder, connector);
253 drm_connector_attach_encoder(connector, encoder);
254
255 return 0;
256
257 fail_cleanup:
258 drm_encoder_cleanup(encoder);
259 kfree(nv_encoder);
260 return ret;
261 }
262