1 /*
2  * Copyright (C) 2010 Texas Instruments Inc
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation version 2.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/ctype.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25 #include <linux/videodev2.h>
26 #include <linux/slab.h>
27 
28 #include <mach/hardware.h>
29 #include <mach/mux.h>
30 #include <mach/io.h>
31 #include <mach/i2c.h>
32 
33 #include <linux/io.h>
34 
35 #include <media/davinci/vpbe_types.h>
36 #include <media/davinci/vpbe_venc.h>
37 #include <media/davinci/vpss.h>
38 #include <media/v4l2-device.h>
39 
40 #include "vpbe_venc_regs.h"
41 
42 #define MODULE_NAME	VPBE_VENC_SUBDEV_NAME
43 
44 static int debug = 2;
45 module_param(debug, int, 0644);
46 MODULE_PARM_DESC(debug, "Debug level 0-2");
47 
48 struct venc_state {
49 	struct v4l2_subdev sd;
50 	struct venc_callback *callback;
51 	struct venc_platform_data *pdata;
52 	struct device *pdev;
53 	u32 output;
54 	v4l2_std_id std;
55 	spinlock_t lock;
56 	void __iomem *venc_base;
57 	void __iomem *vdaccfg_reg;
58 };
59 
to_state(struct v4l2_subdev * sd)60 static inline struct venc_state *to_state(struct v4l2_subdev *sd)
61 {
62 	return container_of(sd, struct venc_state, sd);
63 }
64 
venc_read(struct v4l2_subdev * sd,u32 offset)65 static inline u32 venc_read(struct v4l2_subdev *sd, u32 offset)
66 {
67 	struct venc_state *venc = to_state(sd);
68 
69 	return readl(venc->venc_base + offset);
70 }
71 
venc_write(struct v4l2_subdev * sd,u32 offset,u32 val)72 static inline u32 venc_write(struct v4l2_subdev *sd, u32 offset, u32 val)
73 {
74 	struct venc_state *venc = to_state(sd);
75 
76 	writel(val, (venc->venc_base + offset));
77 
78 	return val;
79 }
80 
venc_modify(struct v4l2_subdev * sd,u32 offset,u32 val,u32 mask)81 static inline u32 venc_modify(struct v4l2_subdev *sd, u32 offset,
82 				 u32 val, u32 mask)
83 {
84 	u32 new_val = (venc_read(sd, offset) & ~mask) | (val & mask);
85 
86 	venc_write(sd, offset, new_val);
87 
88 	return new_val;
89 }
90 
vdaccfg_write(struct v4l2_subdev * sd,u32 val)91 static inline u32 vdaccfg_write(struct v4l2_subdev *sd, u32 val)
92 {
93 	struct venc_state *venc = to_state(sd);
94 
95 	writel(val, venc->vdaccfg_reg);
96 
97 	val = readl(venc->vdaccfg_reg);
98 
99 	return val;
100 }
101 
102 #define VDAC_COMPONENT	0x543
103 #define VDAC_S_VIDEO	0x210
104 /* This function sets the dac of the VPBE for various outputs
105  */
venc_set_dac(struct v4l2_subdev * sd,u32 out_index)106 static int venc_set_dac(struct v4l2_subdev *sd, u32 out_index)
107 {
108 	switch (out_index) {
109 	case 0:
110 		v4l2_dbg(debug, 1, sd, "Setting output to Composite\n");
111 		venc_write(sd, VENC_DACSEL, 0);
112 		break;
113 	case 1:
114 		v4l2_dbg(debug, 1, sd, "Setting output to Component\n");
115 		venc_write(sd, VENC_DACSEL, VDAC_COMPONENT);
116 		break;
117 	case 2:
118 		v4l2_dbg(debug, 1, sd, "Setting output to S-video\n");
119 		venc_write(sd, VENC_DACSEL, VDAC_S_VIDEO);
120 		break;
121 	default:
122 		return -EINVAL;
123 	}
124 
125 	return 0;
126 }
127 
venc_enabledigitaloutput(struct v4l2_subdev * sd,int benable)128 static void venc_enabledigitaloutput(struct v4l2_subdev *sd, int benable)
129 {
130 	struct venc_state *venc = to_state(sd);
131 	struct venc_platform_data *pdata = venc->pdata;
132 	v4l2_dbg(debug, 2, sd, "venc_enabledigitaloutput\n");
133 
134 	if (benable) {
135 		venc_write(sd, VENC_VMOD, 0);
136 		venc_write(sd, VENC_CVBS, 0);
137 		venc_write(sd, VENC_LCDOUT, 0);
138 		venc_write(sd, VENC_HSPLS, 0);
139 		venc_write(sd, VENC_HSTART, 0);
140 		venc_write(sd, VENC_HVALID, 0);
141 		venc_write(sd, VENC_HINT, 0);
142 		venc_write(sd, VENC_VSPLS, 0);
143 		venc_write(sd, VENC_VSTART, 0);
144 		venc_write(sd, VENC_VVALID, 0);
145 		venc_write(sd, VENC_VINT, 0);
146 		venc_write(sd, VENC_YCCCTL, 0);
147 		venc_write(sd, VENC_DACSEL, 0);
148 
149 	} else {
150 		venc_write(sd, VENC_VMOD, 0);
151 		/* disable VCLK output pin enable */
152 		venc_write(sd, VENC_VIDCTL, 0x141);
153 
154 		/* Disable output sync pins */
155 		venc_write(sd, VENC_SYNCCTL, 0);
156 
157 		/* Disable DCLOCK */
158 		venc_write(sd, VENC_DCLKCTL, 0);
159 		venc_write(sd, VENC_DRGBX1, 0x0000057C);
160 
161 		/* Disable LCD output control (accepting default polarity) */
162 		venc_write(sd, VENC_LCDOUT, 0);
163 		if (pdata->venc_type != VPBE_VERSION_3)
164 			venc_write(sd, VENC_CMPNT, 0x100);
165 		venc_write(sd, VENC_HSPLS, 0);
166 		venc_write(sd, VENC_HINT, 0);
167 		venc_write(sd, VENC_HSTART, 0);
168 		venc_write(sd, VENC_HVALID, 0);
169 
170 		venc_write(sd, VENC_VSPLS, 0);
171 		venc_write(sd, VENC_VINT, 0);
172 		venc_write(sd, VENC_VSTART, 0);
173 		venc_write(sd, VENC_VVALID, 0);
174 
175 		venc_write(sd, VENC_HSDLY, 0);
176 		venc_write(sd, VENC_VSDLY, 0);
177 
178 		venc_write(sd, VENC_YCCCTL, 0);
179 		venc_write(sd, VENC_VSTARTA, 0);
180 
181 		/* Set OSD clock and OSD Sync Adavance registers */
182 		venc_write(sd, VENC_OSDCLK0, 1);
183 		venc_write(sd, VENC_OSDCLK1, 2);
184 	}
185 }
186 
187 #define VDAC_CONFIG_SD_V3	0x0E21A6B6
188 #define VDAC_CONFIG_SD_V2	0x081141CF
189 /*
190  * setting NTSC mode
191  */
venc_set_ntsc(struct v4l2_subdev * sd)192 static int venc_set_ntsc(struct v4l2_subdev *sd)
193 {
194 	u32 val;
195 	struct venc_state *venc = to_state(sd);
196 	struct venc_platform_data *pdata = venc->pdata;
197 
198 	v4l2_dbg(debug, 2, sd, "venc_set_ntsc\n");
199 
200 	/* Setup clock at VPSS & VENC for SD */
201 	vpss_enable_clock(VPSS_VENC_CLOCK_SEL, 1);
202 	if (pdata->setup_clock(VPBE_ENC_STD, V4L2_STD_525_60) < 0)
203 		return -EINVAL;
204 
205 	venc_enabledigitaloutput(sd, 0);
206 
207 	if (pdata->venc_type == VPBE_VERSION_3) {
208 		venc_write(sd, VENC_CLKCTL, 0x01);
209 		venc_write(sd, VENC_VIDCTL, 0);
210 		val = vdaccfg_write(sd, VDAC_CONFIG_SD_V3);
211 	} else if (pdata->venc_type == VPBE_VERSION_2) {
212 		venc_write(sd, VENC_CLKCTL, 0x01);
213 		venc_write(sd, VENC_VIDCTL, 0);
214 		vdaccfg_write(sd, VDAC_CONFIG_SD_V2);
215 	} else {
216 		/* to set VENC CLK DIV to 1 - final clock is 54 MHz */
217 		venc_modify(sd, VENC_VIDCTL, 0, 1 << 1);
218 		/* Set REC656 Mode */
219 		venc_write(sd, VENC_YCCCTL, 0x1);
220 		venc_modify(sd, VENC_VDPRO, 0, VENC_VDPRO_DAFRQ);
221 		venc_modify(sd, VENC_VDPRO, 0, VENC_VDPRO_DAUPS);
222 	}
223 
224 	venc_write(sd, VENC_VMOD, 0);
225 	venc_modify(sd, VENC_VMOD, (1 << VENC_VMOD_VIE_SHIFT),
226 			VENC_VMOD_VIE);
227 	venc_modify(sd, VENC_VMOD, (0 << VENC_VMOD_VMD), VENC_VMOD_VMD);
228 	venc_modify(sd, VENC_VMOD, (0 << VENC_VMOD_TVTYP_SHIFT),
229 			VENC_VMOD_TVTYP);
230 	venc_write(sd, VENC_DACTST, 0x0);
231 	venc_modify(sd, VENC_VMOD, VENC_VMOD_VENC, VENC_VMOD_VENC);
232 
233 	return 0;
234 }
235 
236 /*
237  * setting PAL mode
238  */
venc_set_pal(struct v4l2_subdev * sd)239 static int venc_set_pal(struct v4l2_subdev *sd)
240 {
241 	struct venc_state *venc = to_state(sd);
242 	struct venc_platform_data *pdata = venc->pdata;
243 
244 	v4l2_dbg(debug, 2, sd, "venc_set_pal\n");
245 
246 	/* Setup clock at VPSS & VENC for SD */
247 	vpss_enable_clock(VPSS_VENC_CLOCK_SEL, 1);
248 	if (venc->pdata->setup_clock(VPBE_ENC_STD, V4L2_STD_625_50) < 0)
249 		return -EINVAL;
250 
251 	venc_enabledigitaloutput(sd, 0);
252 
253 	if (pdata->venc_type == VPBE_VERSION_3) {
254 		venc_write(sd, VENC_CLKCTL, 0x1);
255 		venc_write(sd, VENC_VIDCTL, 0);
256 		vdaccfg_write(sd, VDAC_CONFIG_SD_V3);
257 	} else if (pdata->venc_type == VPBE_VERSION_2) {
258 		venc_write(sd, VENC_CLKCTL, 0x1);
259 		venc_write(sd, VENC_VIDCTL, 0);
260 		vdaccfg_write(sd, VDAC_CONFIG_SD_V2);
261 	} else {
262 		/* to set VENC CLK DIV to 1 - final clock is 54 MHz */
263 		venc_modify(sd, VENC_VIDCTL, 0, 1 << 1);
264 		/* Set REC656 Mode */
265 		venc_write(sd, VENC_YCCCTL, 0x1);
266 	}
267 
268 	venc_modify(sd, VENC_SYNCCTL, 1 << VENC_SYNCCTL_OVD_SHIFT,
269 			VENC_SYNCCTL_OVD);
270 	venc_write(sd, VENC_VMOD, 0);
271 	venc_modify(sd, VENC_VMOD,
272 			(1 << VENC_VMOD_VIE_SHIFT),
273 			VENC_VMOD_VIE);
274 	venc_modify(sd, VENC_VMOD,
275 			(0 << VENC_VMOD_VMD), VENC_VMOD_VMD);
276 	venc_modify(sd, VENC_VMOD,
277 			(1 << VENC_VMOD_TVTYP_SHIFT),
278 			VENC_VMOD_TVTYP);
279 	venc_write(sd, VENC_DACTST, 0x0);
280 	venc_modify(sd, VENC_VMOD, VENC_VMOD_VENC, VENC_VMOD_VENC);
281 
282 	return 0;
283 }
284 
285 #define VDAC_CONFIG_HD_V2	0x081141EF
286 /*
287  * venc_set_480p59_94
288  *
289  * This function configures the video encoder to EDTV(525p) component setting.
290  */
venc_set_480p59_94(struct v4l2_subdev * sd)291 static int venc_set_480p59_94(struct v4l2_subdev *sd)
292 {
293 	struct venc_state *venc = to_state(sd);
294 	struct venc_platform_data *pdata = venc->pdata;
295 
296 	v4l2_dbg(debug, 2, sd, "venc_set_480p59_94\n");
297 	if ((pdata->venc_type != VPBE_VERSION_1) &&
298 	    (pdata->venc_type != VPBE_VERSION_2))
299 		return -EINVAL;
300 
301 	/* Setup clock at VPSS & VENC for SD */
302 	if (pdata->setup_clock(VPBE_ENC_DV_PRESET, V4L2_DV_480P59_94) < 0)
303 		return -EINVAL;
304 
305 	venc_enabledigitaloutput(sd, 0);
306 
307 	if (pdata->venc_type == VPBE_VERSION_2)
308 		vdaccfg_write(sd, VDAC_CONFIG_HD_V2);
309 	venc_write(sd, VENC_OSDCLK0, 0);
310 	venc_write(sd, VENC_OSDCLK1, 1);
311 
312 	if (pdata->venc_type == VPBE_VERSION_1) {
313 		venc_modify(sd, VENC_VDPRO, VENC_VDPRO_DAFRQ,
314 			    VENC_VDPRO_DAFRQ);
315 		venc_modify(sd, VENC_VDPRO, VENC_VDPRO_DAUPS,
316 			    VENC_VDPRO_DAUPS);
317 	}
318 
319 	venc_write(sd, VENC_VMOD, 0);
320 	venc_modify(sd, VENC_VMOD, (1 << VENC_VMOD_VIE_SHIFT),
321 		    VENC_VMOD_VIE);
322 	venc_modify(sd, VENC_VMOD, VENC_VMOD_HDMD, VENC_VMOD_HDMD);
323 	venc_modify(sd, VENC_VMOD, (HDTV_525P << VENC_VMOD_TVTYP_SHIFT),
324 		    VENC_VMOD_TVTYP);
325 	venc_modify(sd, VENC_VMOD, VENC_VMOD_VDMD_YCBCR8 <<
326 		    VENC_VMOD_VDMD_SHIFT, VENC_VMOD_VDMD);
327 
328 	venc_modify(sd, VENC_VMOD, VENC_VMOD_VENC, VENC_VMOD_VENC);
329 
330 	return 0;
331 }
332 
333 /*
334  * venc_set_625p
335  *
336  * This function configures the video encoder to HDTV(625p) component setting
337  */
venc_set_576p50(struct v4l2_subdev * sd)338 static int venc_set_576p50(struct v4l2_subdev *sd)
339 {
340 	struct venc_state *venc = to_state(sd);
341 	struct venc_platform_data *pdata = venc->pdata;
342 
343 	v4l2_dbg(debug, 2, sd, "venc_set_576p50\n");
344 
345 	if ((pdata->venc_type != VPBE_VERSION_1) &&
346 	  (pdata->venc_type != VPBE_VERSION_2))
347 		return -EINVAL;
348 	/* Setup clock at VPSS & VENC for SD */
349 	if (pdata->setup_clock(VPBE_ENC_DV_PRESET, V4L2_DV_576P50) < 0)
350 		return -EINVAL;
351 
352 	venc_enabledigitaloutput(sd, 0);
353 
354 	if (pdata->venc_type == VPBE_VERSION_2)
355 		vdaccfg_write(sd, VDAC_CONFIG_HD_V2);
356 
357 	venc_write(sd, VENC_OSDCLK0, 0);
358 	venc_write(sd, VENC_OSDCLK1, 1);
359 
360 	if (pdata->venc_type == VPBE_VERSION_1) {
361 		venc_modify(sd, VENC_VDPRO, VENC_VDPRO_DAFRQ,
362 			    VENC_VDPRO_DAFRQ);
363 		venc_modify(sd, VENC_VDPRO, VENC_VDPRO_DAUPS,
364 			    VENC_VDPRO_DAUPS);
365 	}
366 
367 	venc_write(sd, VENC_VMOD, 0);
368 	venc_modify(sd, VENC_VMOD, (1 << VENC_VMOD_VIE_SHIFT),
369 		    VENC_VMOD_VIE);
370 	venc_modify(sd, VENC_VMOD, VENC_VMOD_HDMD, VENC_VMOD_HDMD);
371 	venc_modify(sd, VENC_VMOD, (HDTV_625P << VENC_VMOD_TVTYP_SHIFT),
372 		    VENC_VMOD_TVTYP);
373 
374 	venc_modify(sd, VENC_VMOD, VENC_VMOD_VDMD_YCBCR8 <<
375 		    VENC_VMOD_VDMD_SHIFT, VENC_VMOD_VDMD);
376 	venc_modify(sd, VENC_VMOD, VENC_VMOD_VENC, VENC_VMOD_VENC);
377 
378 	return 0;
379 }
380 
381 /*
382  * venc_set_720p60_internal - Setup 720p60 in venc for dm365 only
383  */
venc_set_720p60_internal(struct v4l2_subdev * sd)384 static int venc_set_720p60_internal(struct v4l2_subdev *sd)
385 {
386 	struct venc_state *venc = to_state(sd);
387 	struct venc_platform_data *pdata = venc->pdata;
388 
389 	if (pdata->setup_clock(VPBE_ENC_DV_PRESET, V4L2_DV_720P60) < 0)
390 		return -EINVAL;
391 
392 	venc_enabledigitaloutput(sd, 0);
393 
394 	venc_write(sd, VENC_OSDCLK0, 0);
395 	venc_write(sd, VENC_OSDCLK1, 1);
396 
397 	venc_write(sd, VENC_VMOD, 0);
398 	/* DM365 component HD mode */
399 	venc_modify(sd, VENC_VMOD, (1 << VENC_VMOD_VIE_SHIFT),
400 	    VENC_VMOD_VIE);
401 	venc_modify(sd, VENC_VMOD, VENC_VMOD_HDMD, VENC_VMOD_HDMD);
402 	venc_modify(sd, VENC_VMOD, (HDTV_720P << VENC_VMOD_TVTYP_SHIFT),
403 		    VENC_VMOD_TVTYP);
404 	venc_modify(sd, VENC_VMOD, VENC_VMOD_VENC, VENC_VMOD_VENC);
405 	venc_write(sd, VENC_XHINTVL, 0);
406 	return 0;
407 }
408 
409 /*
410  * venc_set_1080i30_internal - Setup 1080i30 in venc for dm365 only
411  */
venc_set_1080i30_internal(struct v4l2_subdev * sd)412 static int venc_set_1080i30_internal(struct v4l2_subdev *sd)
413 {
414 	struct venc_state *venc = to_state(sd);
415 	struct venc_platform_data *pdata = venc->pdata;
416 
417 	if (pdata->setup_clock(VPBE_ENC_DV_PRESET, V4L2_DV_1080P30) < 0)
418 		return -EINVAL;
419 
420 	venc_enabledigitaloutput(sd, 0);
421 
422 	venc_write(sd, VENC_OSDCLK0, 0);
423 	venc_write(sd, VENC_OSDCLK1, 1);
424 
425 
426 	venc_write(sd, VENC_VMOD, 0);
427 	/* DM365 component HD mode */
428 	venc_modify(sd, VENC_VMOD, (1 << VENC_VMOD_VIE_SHIFT),
429 		    VENC_VMOD_VIE);
430 	venc_modify(sd, VENC_VMOD, VENC_VMOD_HDMD, VENC_VMOD_HDMD);
431 	venc_modify(sd, VENC_VMOD, (HDTV_1080I << VENC_VMOD_TVTYP_SHIFT),
432 		    VENC_VMOD_TVTYP);
433 	venc_modify(sd, VENC_VMOD, VENC_VMOD_VENC, VENC_VMOD_VENC);
434 	venc_write(sd, VENC_XHINTVL, 0);
435 	return 0;
436 }
437 
venc_s_std_output(struct v4l2_subdev * sd,v4l2_std_id norm)438 static int venc_s_std_output(struct v4l2_subdev *sd, v4l2_std_id norm)
439 {
440 	v4l2_dbg(debug, 1, sd, "venc_s_std_output\n");
441 
442 	if (norm & V4L2_STD_525_60)
443 		return venc_set_ntsc(sd);
444 	else if (norm & V4L2_STD_625_50)
445 		return venc_set_pal(sd);
446 
447 	return -EINVAL;
448 }
449 
venc_s_dv_preset(struct v4l2_subdev * sd,struct v4l2_dv_preset * dv_preset)450 static int venc_s_dv_preset(struct v4l2_subdev *sd,
451 			    struct v4l2_dv_preset *dv_preset)
452 {
453 	struct venc_state *venc = to_state(sd);
454 	int ret;
455 
456 	v4l2_dbg(debug, 1, sd, "venc_s_dv_preset\n");
457 
458 	if (dv_preset->preset == V4L2_DV_576P50)
459 		return venc_set_576p50(sd);
460 	else if (dv_preset->preset == V4L2_DV_480P59_94)
461 		return venc_set_480p59_94(sd);
462 	else if ((dv_preset->preset == V4L2_DV_720P60) &&
463 			(venc->pdata->venc_type == VPBE_VERSION_2)) {
464 		/* TBD setup internal 720p mode here */
465 		ret = venc_set_720p60_internal(sd);
466 		/* for DM365 VPBE, there is DAC inside */
467 		vdaccfg_write(sd, VDAC_CONFIG_HD_V2);
468 		return ret;
469 	} else if ((dv_preset->preset == V4L2_DV_1080I30) &&
470 		(venc->pdata->venc_type == VPBE_VERSION_2)) {
471 		/* TBD setup internal 1080i mode here */
472 		ret = venc_set_1080i30_internal(sd);
473 		/* for DM365 VPBE, there is DAC inside */
474 		vdaccfg_write(sd, VDAC_CONFIG_HD_V2);
475 		return ret;
476 	}
477 	return -EINVAL;
478 }
479 
venc_s_routing(struct v4l2_subdev * sd,u32 input,u32 output,u32 config)480 static int venc_s_routing(struct v4l2_subdev *sd, u32 input, u32 output,
481 			  u32 config)
482 {
483 	struct venc_state *venc = to_state(sd);
484 	int ret;
485 
486 	v4l2_dbg(debug, 1, sd, "venc_s_routing\n");
487 
488 	ret = venc_set_dac(sd, output);
489 	if (!ret)
490 		venc->output = output;
491 
492 	return ret;
493 }
494 
venc_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)495 static long venc_ioctl(struct v4l2_subdev *sd,
496 			unsigned int cmd,
497 			void *arg)
498 {
499 	u32 val;
500 
501 	switch (cmd) {
502 	case VENC_GET_FLD:
503 		val = venc_read(sd, VENC_VSTAT);
504 		*((int *)arg) = ((val & VENC_VSTAT_FIDST) ==
505 		VENC_VSTAT_FIDST);
506 		break;
507 	default:
508 		v4l2_err(sd, "Wrong IOCTL cmd\n");
509 		break;
510 	}
511 
512 	return 0;
513 }
514 
515 static const struct v4l2_subdev_core_ops venc_core_ops = {
516 	.ioctl      = venc_ioctl,
517 };
518 
519 static const struct v4l2_subdev_video_ops venc_video_ops = {
520 	.s_routing = venc_s_routing,
521 	.s_std_output = venc_s_std_output,
522 	.s_dv_preset = venc_s_dv_preset,
523 };
524 
525 static const struct v4l2_subdev_ops venc_ops = {
526 	.core = &venc_core_ops,
527 	.video = &venc_video_ops,
528 };
529 
venc_initialize(struct v4l2_subdev * sd)530 static int venc_initialize(struct v4l2_subdev *sd)
531 {
532 	struct venc_state *venc = to_state(sd);
533 	int ret;
534 
535 	/* Set default to output to composite and std to NTSC */
536 	venc->output = 0;
537 	venc->std = V4L2_STD_525_60;
538 
539 	ret = venc_s_routing(sd, 0, venc->output, 0);
540 	if (ret < 0) {
541 		v4l2_err(sd, "Error setting output during init\n");
542 		return -EINVAL;
543 	}
544 
545 	ret = venc_s_std_output(sd, venc->std);
546 	if (ret < 0) {
547 		v4l2_err(sd, "Error setting std during init\n");
548 		return -EINVAL;
549 	}
550 
551 	return ret;
552 }
553 
venc_device_get(struct device * dev,void * data)554 static int venc_device_get(struct device *dev, void *data)
555 {
556 	struct platform_device *pdev = to_platform_device(dev);
557 	struct venc_state **venc = data;
558 
559 	if (strcmp(MODULE_NAME, pdev->name) == 0)
560 		*venc = platform_get_drvdata(pdev);
561 
562 	return 0;
563 }
564 
venc_sub_dev_init(struct v4l2_device * v4l2_dev,const char * venc_name)565 struct v4l2_subdev *venc_sub_dev_init(struct v4l2_device *v4l2_dev,
566 		const char *venc_name)
567 {
568 	struct venc_state *venc;
569 	int err;
570 
571 	err = bus_for_each_dev(&platform_bus_type, NULL, &venc,
572 			venc_device_get);
573 	if (venc == NULL)
574 		return NULL;
575 
576 	v4l2_subdev_init(&venc->sd, &venc_ops);
577 
578 	strcpy(venc->sd.name, venc_name);
579 	if (v4l2_device_register_subdev(v4l2_dev, &venc->sd) < 0) {
580 		v4l2_err(v4l2_dev,
581 			"vpbe unable to register venc sub device\n");
582 		return NULL;
583 	}
584 	if (venc_initialize(&venc->sd)) {
585 		v4l2_err(v4l2_dev,
586 			"vpbe venc initialization failed\n");
587 		return NULL;
588 	}
589 
590 	return &venc->sd;
591 }
592 EXPORT_SYMBOL(venc_sub_dev_init);
593 
venc_probe(struct platform_device * pdev)594 static int venc_probe(struct platform_device *pdev)
595 {
596 	struct venc_state *venc;
597 	struct resource *res;
598 	int ret;
599 
600 	venc = kzalloc(sizeof(struct venc_state), GFP_KERNEL);
601 	if (venc == NULL)
602 		return -ENOMEM;
603 
604 	venc->pdev = &pdev->dev;
605 	venc->pdata = pdev->dev.platform_data;
606 	if (NULL == venc->pdata) {
607 		dev_err(venc->pdev, "Unable to get platform data for"
608 			" VENC sub device");
609 		ret = -ENOENT;
610 		goto free_mem;
611 	}
612 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
613 	if (!res) {
614 		dev_err(venc->pdev,
615 			"Unable to get VENC register address map\n");
616 		ret = -ENODEV;
617 		goto free_mem;
618 	}
619 
620 	if (!request_mem_region(res->start, resource_size(res), "venc")) {
621 		dev_err(venc->pdev, "Unable to reserve VENC MMIO region\n");
622 		ret = -ENODEV;
623 		goto free_mem;
624 	}
625 
626 	venc->venc_base = ioremap_nocache(res->start, resource_size(res));
627 	if (!venc->venc_base) {
628 		dev_err(venc->pdev, "Unable to map VENC IO space\n");
629 		ret = -ENODEV;
630 		goto release_venc_mem_region;
631 	}
632 
633 	if (venc->pdata->venc_type != VPBE_VERSION_1) {
634 		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
635 		if (!res) {
636 			dev_err(venc->pdev,
637 				"Unable to get VDAC_CONFIG address map\n");
638 			ret = -ENODEV;
639 			goto unmap_venc_io;
640 		}
641 
642 		if (!request_mem_region(res->start,
643 					resource_size(res), "venc")) {
644 			dev_err(venc->pdev,
645 				"Unable to reserve VDAC_CONFIG  MMIO region\n");
646 			ret = -ENODEV;
647 			goto unmap_venc_io;
648 		}
649 
650 		venc->vdaccfg_reg = ioremap_nocache(res->start,
651 						    resource_size(res));
652 		if (!venc->vdaccfg_reg) {
653 			dev_err(venc->pdev,
654 				"Unable to map VDAC_CONFIG IO space\n");
655 			ret = -ENODEV;
656 			goto release_vdaccfg_mem_region;
657 		}
658 	}
659 	spin_lock_init(&venc->lock);
660 	platform_set_drvdata(pdev, venc);
661 	dev_notice(venc->pdev, "VENC sub device probe success\n");
662 	return 0;
663 
664 release_vdaccfg_mem_region:
665 	release_mem_region(res->start, resource_size(res));
666 unmap_venc_io:
667 	iounmap(venc->venc_base);
668 release_venc_mem_region:
669 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
670 	release_mem_region(res->start, resource_size(res));
671 free_mem:
672 	kfree(venc);
673 	return ret;
674 }
675 
venc_remove(struct platform_device * pdev)676 static int venc_remove(struct platform_device *pdev)
677 {
678 	struct venc_state *venc = platform_get_drvdata(pdev);
679 	struct resource *res;
680 
681 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
682 	iounmap((void *)venc->venc_base);
683 	release_mem_region(res->start, resource_size(res));
684 	if (venc->pdata->venc_type != VPBE_VERSION_1) {
685 		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
686 		iounmap((void *)venc->vdaccfg_reg);
687 		release_mem_region(res->start, resource_size(res));
688 	}
689 	kfree(venc);
690 
691 	return 0;
692 }
693 
694 static struct platform_driver venc_driver = {
695 	.probe		= venc_probe,
696 	.remove		= venc_remove,
697 	.driver		= {
698 		.name	= MODULE_NAME,
699 		.owner	= THIS_MODULE,
700 	},
701 };
702 
703 module_platform_driver(venc_driver);
704 
705 MODULE_LICENSE("GPL");
706 MODULE_DESCRIPTION("VPBE VENC Driver");
707 MODULE_AUTHOR("Texas Instruments");
708