blob: c3549fa55b62cab9ff406b8d203293f1acd7b0a3 [file] [log] [blame]
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * vpx3220a, vpx3216b & vpx3214c video decoder driver version 0.0.1
3 *
4 * Copyright (C) 2001 Laurent Pinchart <lpinchart@freegates.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/i2c.h>
Hans Verkuil107063c2009-02-18 17:26:06 -030024#include <linux/videodev2.h>
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030025#include <media/v4l2-device.h>
Hans Verkuil9a775322010-12-12 08:45:59 -030026#include <media/v4l2-ctrls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Hans Verkuil23848b62008-09-07 08:01:39 -030028MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
29MODULE_AUTHOR("Laurent Pinchart");
30MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030032static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033module_param(debug, int, 0);
34MODULE_PARM_DESC(debug, "Debug level (0-1)");
35
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define VPX_TIMEOUT_COUNT 10
38
39/* ----------------------------------------------------------------------- */
40
41struct vpx3220 {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030042 struct v4l2_subdev sd;
Hans Verkuil9a775322010-12-12 08:45:59 -030043 struct v4l2_ctrl_handler hdl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 unsigned char reg[255];
45
Hans Verkuil107063c2009-02-18 17:26:06 -030046 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int input;
48 int enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030051static inline struct vpx3220 *to_vpx3220(struct v4l2_subdev *sd)
52{
53 return container_of(sd, struct vpx3220, sd);
54}
55
Hans Verkuil9a775322010-12-12 08:45:59 -030056static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
57{
58 return &container_of(ctrl->handler, struct vpx3220, hdl)->sd;
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static char *inputs[] = { "internal", "composite", "svideo" };
62
63/* ----------------------------------------------------------------------- */
Hans Verkuil23848b62008-09-07 08:01:39 -030064
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030065static inline int vpx3220_write(struct v4l2_subdev *sd, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030067 struct i2c_client *client = v4l2_get_subdevdata(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 struct vpx3220 *decoder = i2c_get_clientdata(client);
69
70 decoder->reg[reg] = value;
71 return i2c_smbus_write_byte_data(client, reg, value);
72}
73
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030074static inline int vpx3220_read(struct v4l2_subdev *sd, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030076 struct i2c_client *client = v4l2_get_subdevdata(sd);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return i2c_smbus_read_byte_data(client, reg);
79}
80
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030081static int vpx3220_fp_status(struct v4l2_subdev *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 unsigned char status;
84 unsigned int i;
85
86 for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030087 status = vpx3220_read(sd, 0x29);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 if (!(status & 4))
90 return 0;
91
92 udelay(10);
93
94 if (need_resched())
95 cond_resched();
96 }
97
98 return -1;
99}
100
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300101static int vpx3220_fp_write(struct v4l2_subdev *sd, u8 fpaddr, u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300103 struct i2c_client *client = v4l2_get_subdevdata(sd);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* Write the 16-bit address to the FPWR register */
106 if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300107 v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -1;
109 }
110
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300111 if (vpx3220_fp_status(sd) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -1;
113
114 /* Write the 16-bit data to the FPDAT register */
115 if (i2c_smbus_write_word_data(client, 0x28, swab16(data)) == -1) {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300116 v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -1;
118 }
119
120 return 0;
121}
122
Dan Carpenter39de7d92016-01-06 08:05:52 -0200123static int vpx3220_fp_read(struct v4l2_subdev *sd, u16 fpaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300125 struct i2c_client *client = v4l2_get_subdevdata(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 s16 data;
127
128 /* Write the 16-bit address to the FPRD register */
129 if (i2c_smbus_write_word_data(client, 0x26, swab16(fpaddr)) == -1) {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300130 v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return -1;
132 }
133
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300134 if (vpx3220_fp_status(sd) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -1;
136
137 /* Read the 16-bit data from the FPDAT register */
138 data = i2c_smbus_read_word_data(client, 0x28);
139 if (data == -1) {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300140 v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -1;
142 }
143
144 return swab16(data);
145}
146
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300147static int vpx3220_write_block(struct v4l2_subdev *sd, const u8 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 u8 reg;
150 int ret = -1;
151
152 while (len >= 2) {
153 reg = *data++;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300154 ret = vpx3220_write(sd, reg, *data++);
Hans Verkuil23848b62008-09-07 08:01:39 -0300155 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 break;
157 len -= 2;
158 }
159
160 return ret;
161}
162
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300163static int vpx3220_write_fp_block(struct v4l2_subdev *sd,
Hans Verkuil23848b62008-09-07 08:01:39 -0300164 const u16 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 u8 reg;
167 int ret = 0;
168
169 while (len > 1) {
170 reg = *data++;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300171 ret |= vpx3220_fp_write(sd, reg, *data++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 len -= 2;
173 }
174
175 return ret;
176}
177
178/* ---------------------------------------------------------------------- */
179
180static const unsigned short init_ntsc[] = {
181 0x1c, 0x00, /* NTSC tint angle */
182 0x88, 17, /* Window 1 vertical */
183 0x89, 240, /* Vertical lines in */
184 0x8a, 240, /* Vertical lines out */
185 0x8b, 000, /* Horizontal begin */
186 0x8c, 640, /* Horizontal length */
187 0x8d, 640, /* Number of pixels */
188 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700189 0xf0, 0x73, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * mode, latch windows */
191 0xf2, 0x13, /* NTSC M, composite input */
192 0xe7, 0x1e1, /* Enable vertical standard
193 * locking @ 240 lines */
194};
195
196static const unsigned short init_pal[] = {
197 0x88, 23, /* Window 1 vertical begin */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700198 0x89, 288, /* Vertical lines in (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * skipped by the VFE) */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700200 0x8a, 288, /* Vertical lines out (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * skipped by the VFE) */
202 0x8b, 16, /* Horizontal begin */
203 0x8c, 768, /* Horizontal length */
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500204 0x8d, 784, /* Number of pixels
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * Must be >= Horizontal begin + Horizontal length */
206 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700207 0xf0, 0x77, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * mode, latch windows */
209 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700210 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
213static const unsigned short init_secam[] = {
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700214 0x88, 23, /* Window 1 vertical begin */
215 0x89, 288, /* Vertical lines in (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * skipped by the VFE) */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700217 0x8a, 288, /* Vertical lines out (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * skipped by the VFE) */
219 0x8b, 16, /* Horizontal begin */
220 0x8c, 768, /* Horizontal length */
221 0x8d, 784, /* Number of pixels
222 * Must be >= Horizontal begin + Horizontal length */
223 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700224 0xf0, 0x77, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * mode, latch windows */
226 0xf2, 0x3d5, /* SECAM, composite input */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700227 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228};
229
230static const unsigned char init_common[] = {
231 0xf2, 0x00, /* Disable all outputs */
232 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
233 * (clamp off) */
234 0xd8, 0xa8, /* HREF/VREF active high, VREF
235 * pulse = 2, Odd/Even flag */
236 0x20, 0x03, /* IF compensation 0dB/oct */
237 0xe0, 0xff, /* Open up all comparators */
238 0xe1, 0x00,
239 0xe2, 0x7f,
240 0xe3, 0x80,
241 0xe4, 0x7f,
242 0xe5, 0x80,
243 0xe6, 0x00, /* Brightness set to 0 */
244 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
245 * 10 to 8 2-bit error diffusion */
246 0xe8, 0xf8, /* YUV422, CbCr binary offset,
247 * ... (p.32) */
248 0xea, 0x18, /* LLC2 connected, output FIFO
249 * reset with VACTintern */
250 0xf0, 0x8a, /* Half full level to 10, bus
251 * shuffler [7:0, 23:16, 15:8] */
252 0xf1, 0x18, /* Single clock, sync mode, no
253 * FE delay, no HLEN counter */
254 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
255 * strength to 2 */
256 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
257 * ALPHA strength to 4 */
258};
259
260static const unsigned short init_fp[] = {
261 0x59, 0,
262 0xa0, 2070, /* ACC reference */
263 0xa3, 0,
264 0xa4, 0,
265 0xa8, 30,
266 0xb2, 768,
267 0xbe, 27,
268 0x58, 0,
269 0x26, 0,
270 0x4b, 0x298, /* PLL gain */
271};
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300274static int vpx3220_init(struct v4l2_subdev *sd, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300276 struct vpx3220 *decoder = to_vpx3220(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300278 vpx3220_write_block(sd, init_common, sizeof(init_common));
279 vpx3220_write_fp_block(sd, init_fp, sizeof(init_fp) >> 1);
280 if (decoder->norm & V4L2_STD_NTSC)
281 vpx3220_write_fp_block(sd, init_ntsc, sizeof(init_ntsc) >> 1);
282 else if (decoder->norm & V4L2_STD_PAL)
283 vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
284 else if (decoder->norm & V4L2_STD_SECAM)
285 vpx3220_write_fp_block(sd, init_secam, sizeof(init_secam) >> 1);
286 else
287 vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
288 return 0;
289}
290
291static int vpx3220_status(struct v4l2_subdev *sd, u32 *pstatus, v4l2_std_id *pstd)
292{
293 int res = V4L2_IN_ST_NO_SIGNAL, status;
Hans Verkuil32cb3b02013-05-29 10:19:01 -0300294 v4l2_std_id std = pstd ? *pstd : V4L2_STD_ALL;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300295
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300296 status = vpx3220_fp_read(sd, 0x0f3);
297
298 v4l2_dbg(1, debug, sd, "status: 0x%04x\n", status);
299
300 if (status < 0)
301 return status;
302
303 if ((status & 0x20) == 0) {
304 res = 0;
305
306 switch (status & 0x18) {
307 case 0x00:
308 case 0x10:
309 case 0x14:
310 case 0x18:
Hans Verkuil32cb3b02013-05-29 10:19:01 -0300311 std &= V4L2_STD_PAL;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300312 break;
313
314 case 0x08:
Hans Verkuil32cb3b02013-05-29 10:19:01 -0300315 std &= V4L2_STD_SECAM;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300316 break;
317
318 case 0x04:
319 case 0x0c:
320 case 0x1c:
Hans Verkuil32cb3b02013-05-29 10:19:01 -0300321 std &= V4L2_STD_NTSC;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300322 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Hans Verkuil32cb3b02013-05-29 10:19:01 -0300324 } else {
325 std = V4L2_STD_UNKNOWN;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300326 }
327 if (pstd)
328 *pstd = std;
329 if (pstatus)
Hans Verkuilba088312011-08-25 10:52:53 -0300330 *pstatus = res;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300331 return 0;
332}
333
334static int vpx3220_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
335{
Hans Verkuilbccfa442009-03-30 06:55:27 -0300336 v4l2_dbg(1, debug, sd, "querystd\n");
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300337 return vpx3220_status(sd, NULL, std);
338}
339
340static int vpx3220_g_input_status(struct v4l2_subdev *sd, u32 *status)
341{
Hans Verkuilbccfa442009-03-30 06:55:27 -0300342 v4l2_dbg(1, debug, sd, "g_input_status\n");
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300343 return vpx3220_status(sd, status, NULL);
344}
345
346static int vpx3220_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
347{
348 struct vpx3220 *decoder = to_vpx3220(sd);
349 int temp_input;
350
351 /* Here we back up the input selection because it gets
352 overwritten when we fill the registers with the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300353 chosen video norm */
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300354 temp_input = vpx3220_fp_read(sd, 0xf2);
355
Hans Verkuilbccfa442009-03-30 06:55:27 -0300356 v4l2_dbg(1, debug, sd, "s_std %llx\n", (unsigned long long)std);
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300357 if (std & V4L2_STD_NTSC) {
358 vpx3220_write_fp_block(sd, init_ntsc, sizeof(init_ntsc) >> 1);
359 v4l2_dbg(1, debug, sd, "norm switched to NTSC\n");
360 } else if (std & V4L2_STD_PAL) {
361 vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
362 v4l2_dbg(1, debug, sd, "norm switched to PAL\n");
363 } else if (std & V4L2_STD_SECAM) {
364 vpx3220_write_fp_block(sd, init_secam, sizeof(init_secam) >> 1);
365 v4l2_dbg(1, debug, sd, "norm switched to SECAM\n");
366 } else {
367 return -EINVAL;
Hans Verkuil23848b62008-09-07 08:01:39 -0300368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300370 decoder->norm = std;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300372 /* And here we set the backed up video input again */
373 vpx3220_fp_write(sd, 0xf2, temp_input | 0x0010);
374 udelay(10);
375 return 0;
376}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Hans Verkuil5325b422009-04-02 11:26:22 -0300378static int vpx3220_s_routing(struct v4l2_subdev *sd,
379 u32 input, u32 output, u32 config)
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300380{
381 int data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Hans Verkuil5325b422009-04-02 11:26:22 -0300383 /* RJ: input = 0: ST8 (PCTV) input
384 input = 1: COMPOSITE input
385 input = 2: SVHS input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Hans Verkuil5325b422009-04-02 11:26:22 -0300387 const int input_vals[3][2] = {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300388 {0x0c, 0},
389 {0x0d, 0},
390 {0x0e, 1}
391 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Roel Kluinf14a2972009-10-23 07:59:42 -0300393 if (input > 2)
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Hans Verkuil5325b422009-04-02 11:26:22 -0300396 v4l2_dbg(1, debug, sd, "input switched to %s\n", inputs[input]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Hans Verkuil5325b422009-04-02 11:26:22 -0300398 vpx3220_write(sd, 0x33, input_vals[input][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300400 data = vpx3220_fp_read(sd, 0xf2) & ~(0x0020);
401 if (data < 0)
402 return data;
403 /* 0x0010 is required to latch the setting */
404 vpx3220_fp_write(sd, 0xf2,
Hans Verkuil5325b422009-04-02 11:26:22 -0300405 data | (input_vals[input][1] << 5) | 0x0010);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300407 udelay(10);
408 return 0;
409}
410
411static int vpx3220_s_stream(struct v4l2_subdev *sd, int enable)
412{
Hans Verkuilbccfa442009-03-30 06:55:27 -0300413 v4l2_dbg(1, debug, sd, "s_stream %s\n", enable ? "on" : "off");
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300414
415 vpx3220_write(sd, 0xf2, (enable ? 0x1b : 0x00));
416 return 0;
417}
418
Hans Verkuil9a775322010-12-12 08:45:59 -0300419static int vpx3220_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300420{
Hans Verkuil9a775322010-12-12 08:45:59 -0300421 struct v4l2_subdev *sd = to_sd(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300423 switch (ctrl->id) {
424 case V4L2_CID_BRIGHTNESS:
Hans Verkuil9a775322010-12-12 08:45:59 -0300425 vpx3220_write(sd, 0xe6, ctrl->val);
426 return 0;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300427 case V4L2_CID_CONTRAST:
Hans Verkuil9a775322010-12-12 08:45:59 -0300428 /* Bit 7 and 8 is for noise shaping */
429 vpx3220_write(sd, 0xe7, ctrl->val + 192);
430 return 0;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300431 case V4L2_CID_SATURATION:
Hans Verkuil9a775322010-12-12 08:45:59 -0300432 vpx3220_fp_write(sd, 0xa0, ctrl->val);
433 return 0;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300434 case V4L2_CID_HUE:
Hans Verkuil9a775322010-12-12 08:45:59 -0300435 vpx3220_fp_write(sd, 0x1c, ctrl->val);
436 return 0;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300437 }
Hans Verkuil9a775322010-12-12 08:45:59 -0300438 return -EINVAL;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300439}
440
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300441/* ----------------------------------------------------------------------- */
442
Hans Verkuil9a775322010-12-12 08:45:59 -0300443static const struct v4l2_ctrl_ops vpx3220_ctrl_ops = {
444 .s_ctrl = vpx3220_s_ctrl,
445};
446
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300447static const struct v4l2_subdev_core_ops vpx3220_core_ops = {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300448 .init = vpx3220_init,
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300449};
450
451static const struct v4l2_subdev_video_ops vpx3220_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300452 .s_std = vpx3220_s_std,
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300453 .s_routing = vpx3220_s_routing,
454 .s_stream = vpx3220_s_stream,
455 .querystd = vpx3220_querystd,
456 .g_input_status = vpx3220_g_input_status,
457};
458
459static const struct v4l2_subdev_ops vpx3220_ops = {
460 .core = &vpx3220_core_ops,
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300461 .video = &vpx3220_video_ops,
462};
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/* -----------------------------------------------------------------------
Joe Perchesc84e6032008-02-03 17:18:59 +0200465 * Client management code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
467
Hans Verkuil23848b62008-09-07 08:01:39 -0300468static int vpx3220_probe(struct i2c_client *client,
469 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct vpx3220 *decoder;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300472 struct v4l2_subdev *sd;
Hans Verkuil23848b62008-09-07 08:01:39 -0300473 const char *name = NULL;
474 u8 ver;
475 u16 pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /* Check if the adapter supports the needed features */
Hans Verkuil23848b62008-09-07 08:01:39 -0300478 if (!i2c_check_functionality(client->adapter,
479 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
480 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300482 decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
Hans Verkuil23848b62008-09-07 08:01:39 -0300483 if (decoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return -ENOMEM;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300485 sd = &decoder->sd;
486 v4l2_i2c_subdev_init(sd, client, &vpx3220_ops);
Hans Verkuil107063c2009-02-18 17:26:06 -0300487 decoder->norm = V4L2_STD_PAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 decoder->input = 0;
489 decoder->enable = 1;
Hans Verkuil9a775322010-12-12 08:45:59 -0300490 v4l2_ctrl_handler_init(&decoder->hdl, 4);
491 v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
492 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
493 v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
494 V4L2_CID_CONTRAST, 0, 63, 1, 32);
495 v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
496 V4L2_CID_SATURATION, 0, 4095, 1, 2048);
497 v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
498 V4L2_CID_HUE, -512, 511, 1, 0);
499 sd->ctrl_handler = &decoder->hdl;
500 if (decoder->hdl.error) {
501 int err = decoder->hdl.error;
502
503 v4l2_ctrl_handler_free(&decoder->hdl);
Hans Verkuil9a775322010-12-12 08:45:59 -0300504 return err;
505 }
506 v4l2_ctrl_handler_setup(&decoder->hdl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Hans Verkuil23848b62008-09-07 08:01:39 -0300508 ver = i2c_smbus_read_byte_data(client, 0x00);
509 pn = (i2c_smbus_read_byte_data(client, 0x02) << 8) +
510 i2c_smbus_read_byte_data(client, 0x01);
511 if (ver == 0xec) {
512 switch (pn) {
513 case 0x4680:
514 name = "vpx3220a";
515 break;
516 case 0x4260:
517 name = "vpx3216b";
518 break;
519 case 0x4280:
520 name = "vpx3214c";
521 break;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Hans Verkuil23848b62008-09-07 08:01:39 -0300524 if (name)
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300525 v4l2_info(sd, "%s found @ 0x%x (%s)\n", name,
Hans Verkuil23848b62008-09-07 08:01:39 -0300526 client->addr << 1, client->adapter->name);
527 else
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300528 v4l2_info(sd, "chip (%02x:%04x) found @ 0x%x (%s)\n",
Hans Verkuil23848b62008-09-07 08:01:39 -0300529 ver, pn, client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300531 vpx3220_write_block(sd, init_common, sizeof(init_common));
532 vpx3220_write_fp_block(sd, init_fp, sizeof(init_fp) >> 1);
533 /* Default to PAL */
534 vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
536}
537
Hans Verkuil23848b62008-09-07 08:01:39 -0300538static int vpx3220_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300540 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Hans Verkuil9a775322010-12-12 08:45:59 -0300541 struct vpx3220 *decoder = to_vpx3220(sd);
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300542
543 v4l2_device_unregister_subdev(sd);
Hans Verkuil9a775322010-12-12 08:45:59 -0300544 v4l2_ctrl_handler_free(&decoder->hdl);
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300545
Hans Verkuil23848b62008-09-07 08:01:39 -0300546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Hans Verkuil23848b62008-09-07 08:01:39 -0300549static const struct i2c_device_id vpx3220_id[] = {
550 { "vpx3220a", 0 },
551 { "vpx3216b", 0 },
552 { "vpx3214c", 0 },
553 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554};
Hans Verkuil23848b62008-09-07 08:01:39 -0300555MODULE_DEVICE_TABLE(i2c, vpx3220_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Hans Verkuilc2d999f2010-09-15 15:44:11 -0300557static struct i2c_driver vpx3220_driver = {
558 .driver = {
Hans Verkuilc2d999f2010-09-15 15:44:11 -0300559 .name = "vpx3220",
560 },
561 .probe = vpx3220_probe,
562 .remove = vpx3220_remove,
563 .id_table = vpx3220_id,
Hans Verkuil23848b62008-09-07 08:01:39 -0300564};
Hans Verkuilc2d999f2010-09-15 15:44:11 -0300565
Axel Linc6e8d862012-02-12 06:56:32 -0300566module_i2c_driver(vpx3220_driver);