blob: 8be03fe5928cfb1f8e3b0f2f00e37b3b24def1b6 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * vpx3220a, vpx3216b & vpx3214c video decoder driver version 0.0.1
4 *
5 * Copyright (C) 2001 Laurent Pinchart <lpinchart@freegates.be>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/delay.h>
11#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080013#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/i2c.h>
Hans Verkuil107063c2009-02-18 17:26:06 -030015#include <linux/videodev2.h>
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030016#include <media/v4l2-device.h>
Hans Verkuil9a775322010-12-12 08:45:59 -030017#include <media/v4l2-ctrls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Hans Verkuil23848b62008-09-07 08:01:39 -030019MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
20MODULE_AUTHOR("Laurent Pinchart");
21MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030023static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024module_param(debug, int, 0);
25MODULE_PARM_DESC(debug, "Debug level (0-1)");
26
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define VPX_TIMEOUT_COUNT 10
29
30/* ----------------------------------------------------------------------- */
31
32struct vpx3220 {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030033 struct v4l2_subdev sd;
Hans Verkuil9a775322010-12-12 08:45:59 -030034 struct v4l2_ctrl_handler hdl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 unsigned char reg[255];
36
Hans Verkuil107063c2009-02-18 17:26:06 -030037 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 int input;
39 int enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030042static inline struct vpx3220 *to_vpx3220(struct v4l2_subdev *sd)
43{
44 return container_of(sd, struct vpx3220, sd);
45}
46
Hans Verkuil9a775322010-12-12 08:45:59 -030047static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
48{
49 return &container_of(ctrl->handler, struct vpx3220, hdl)->sd;
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static char *inputs[] = { "internal", "composite", "svideo" };
53
54/* ----------------------------------------------------------------------- */
Hans Verkuil23848b62008-09-07 08:01:39 -030055
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030056static inline int vpx3220_write(struct v4l2_subdev *sd, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030058 struct i2c_client *client = v4l2_get_subdevdata(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct vpx3220 *decoder = i2c_get_clientdata(client);
60
61 decoder->reg[reg] = value;
62 return i2c_smbus_write_byte_data(client, reg, value);
63}
64
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030065static inline int vpx3220_read(struct v4l2_subdev *sd, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030067 struct i2c_client *client = v4l2_get_subdevdata(sd);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return i2c_smbus_read_byte_data(client, reg);
70}
71
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030072static int vpx3220_fp_status(struct v4l2_subdev *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 unsigned char status;
75 unsigned int i;
76
77 for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030078 status = vpx3220_read(sd, 0x29);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 if (!(status & 4))
81 return 0;
82
83 udelay(10);
84
85 if (need_resched())
86 cond_resched();
87 }
88
89 return -1;
90}
91
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030092static int vpx3220_fp_write(struct v4l2_subdev *sd, u8 fpaddr, u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030094 struct i2c_client *client = v4l2_get_subdevdata(sd);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /* Write the 16-bit address to the FPWR register */
97 if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -030098 v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -1;
100 }
101
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300102 if (vpx3220_fp_status(sd) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return -1;
104
105 /* Write the 16-bit data to the FPDAT register */
106 if (i2c_smbus_write_word_data(client, 0x28, swab16(data)) == -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
111 return 0;
112}
113
Dan Carpenter39de7d92016-01-06 08:05:52 -0200114static int vpx3220_fp_read(struct v4l2_subdev *sd, u16 fpaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300116 struct i2c_client *client = v4l2_get_subdevdata(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 s16 data;
118
119 /* Write the 16-bit address to the FPRD register */
120 if (i2c_smbus_write_word_data(client, 0x26, swab16(fpaddr)) == -1) {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300121 v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return -1;
123 }
124
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300125 if (vpx3220_fp_status(sd) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return -1;
127
128 /* Read the 16-bit data from the FPDAT register */
129 data = i2c_smbus_read_word_data(client, 0x28);
130 if (data == -1) {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300131 v4l2_dbg(1, debug, sd, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -1;
133 }
134
135 return swab16(data);
136}
137
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300138static int vpx3220_write_block(struct v4l2_subdev *sd, const u8 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 u8 reg;
141 int ret = -1;
142
143 while (len >= 2) {
144 reg = *data++;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300145 ret = vpx3220_write(sd, reg, *data++);
Hans Verkuil23848b62008-09-07 08:01:39 -0300146 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 break;
148 len -= 2;
149 }
150
151 return ret;
152}
153
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300154static int vpx3220_write_fp_block(struct v4l2_subdev *sd,
Hans Verkuil23848b62008-09-07 08:01:39 -0300155 const u16 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 u8 reg;
158 int ret = 0;
159
160 while (len > 1) {
161 reg = *data++;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300162 ret |= vpx3220_fp_write(sd, reg, *data++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 len -= 2;
164 }
165
166 return ret;
167}
168
169/* ---------------------------------------------------------------------- */
170
171static const unsigned short init_ntsc[] = {
172 0x1c, 0x00, /* NTSC tint angle */
173 0x88, 17, /* Window 1 vertical */
174 0x89, 240, /* Vertical lines in */
175 0x8a, 240, /* Vertical lines out */
176 0x8b, 000, /* Horizontal begin */
177 0x8c, 640, /* Horizontal length */
178 0x8d, 640, /* Number of pixels */
179 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700180 0xf0, 0x73, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * mode, latch windows */
182 0xf2, 0x13, /* NTSC M, composite input */
183 0xe7, 0x1e1, /* Enable vertical standard
184 * locking @ 240 lines */
185};
186
187static const unsigned short init_pal[] = {
188 0x88, 23, /* Window 1 vertical begin */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700189 0x89, 288, /* Vertical lines in (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * skipped by the VFE) */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700191 0x8a, 288, /* Vertical lines out (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * skipped by the VFE) */
193 0x8b, 16, /* Horizontal begin */
194 0x8c, 768, /* Horizontal length */
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500195 0x8d, 784, /* Number of pixels
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * Must be >= Horizontal begin + Horizontal length */
197 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700198 0xf0, 0x77, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * mode, latch windows */
200 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700201 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
204static const unsigned short init_secam[] = {
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700205 0x88, 23, /* Window 1 vertical begin */
206 0x89, 288, /* Vertical lines in (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * skipped by the VFE) */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700208 0x8a, 288, /* Vertical lines out (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * skipped by the VFE) */
210 0x8b, 16, /* Horizontal begin */
211 0x8c, 768, /* Horizontal length */
212 0x8d, 784, /* Number of pixels
213 * Must be >= Horizontal begin + Horizontal length */
214 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700215 0xf0, 0x77, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * mode, latch windows */
217 0xf2, 0x3d5, /* SECAM, composite input */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700218 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
221static const unsigned char init_common[] = {
222 0xf2, 0x00, /* Disable all outputs */
223 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
224 * (clamp off) */
225 0xd8, 0xa8, /* HREF/VREF active high, VREF
226 * pulse = 2, Odd/Even flag */
227 0x20, 0x03, /* IF compensation 0dB/oct */
228 0xe0, 0xff, /* Open up all comparators */
229 0xe1, 0x00,
230 0xe2, 0x7f,
231 0xe3, 0x80,
232 0xe4, 0x7f,
233 0xe5, 0x80,
234 0xe6, 0x00, /* Brightness set to 0 */
235 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
236 * 10 to 8 2-bit error diffusion */
237 0xe8, 0xf8, /* YUV422, CbCr binary offset,
238 * ... (p.32) */
239 0xea, 0x18, /* LLC2 connected, output FIFO
240 * reset with VACTintern */
241 0xf0, 0x8a, /* Half full level to 10, bus
242 * shuffler [7:0, 23:16, 15:8] */
243 0xf1, 0x18, /* Single clock, sync mode, no
244 * FE delay, no HLEN counter */
245 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
246 * strength to 2 */
247 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
248 * ALPHA strength to 4 */
249};
250
251static const unsigned short init_fp[] = {
252 0x59, 0,
253 0xa0, 2070, /* ACC reference */
254 0xa3, 0,
255 0xa4, 0,
256 0xa8, 30,
257 0xb2, 768,
258 0xbe, 27,
259 0x58, 0,
260 0x26, 0,
261 0x4b, 0x298, /* PLL gain */
262};
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300265static int vpx3220_init(struct v4l2_subdev *sd, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300267 struct vpx3220 *decoder = to_vpx3220(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300269 vpx3220_write_block(sd, init_common, sizeof(init_common));
270 vpx3220_write_fp_block(sd, init_fp, sizeof(init_fp) >> 1);
271 if (decoder->norm & V4L2_STD_NTSC)
272 vpx3220_write_fp_block(sd, init_ntsc, sizeof(init_ntsc) >> 1);
273 else if (decoder->norm & V4L2_STD_PAL)
274 vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
275 else if (decoder->norm & V4L2_STD_SECAM)
276 vpx3220_write_fp_block(sd, init_secam, sizeof(init_secam) >> 1);
277 else
278 vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
279 return 0;
280}
281
282static int vpx3220_status(struct v4l2_subdev *sd, u32 *pstatus, v4l2_std_id *pstd)
283{
284 int res = V4L2_IN_ST_NO_SIGNAL, status;
Hans Verkuil32cb3b02013-05-29 10:19:01 -0300285 v4l2_std_id std = pstd ? *pstd : V4L2_STD_ALL;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300286
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300287 status = vpx3220_fp_read(sd, 0x0f3);
288
289 v4l2_dbg(1, debug, sd, "status: 0x%04x\n", status);
290
291 if (status < 0)
292 return status;
293
294 if ((status & 0x20) == 0) {
295 res = 0;
296
297 switch (status & 0x18) {
298 case 0x00:
299 case 0x10:
300 case 0x14:
301 case 0x18:
Hans Verkuil32cb3b02013-05-29 10:19:01 -0300302 std &= V4L2_STD_PAL;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300303 break;
304
305 case 0x08:
Hans Verkuil32cb3b02013-05-29 10:19:01 -0300306 std &= V4L2_STD_SECAM;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300307 break;
308
309 case 0x04:
310 case 0x0c:
311 case 0x1c:
Hans Verkuil32cb3b02013-05-29 10:19:01 -0300312 std &= V4L2_STD_NTSC;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300313 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Hans Verkuil32cb3b02013-05-29 10:19:01 -0300315 } else {
316 std = V4L2_STD_UNKNOWN;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300317 }
318 if (pstd)
319 *pstd = std;
320 if (pstatus)
Hans Verkuilba088312011-08-25 10:52:53 -0300321 *pstatus = res;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300322 return 0;
323}
324
325static int vpx3220_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
326{
Hans Verkuilbccfa442009-03-30 06:55:27 -0300327 v4l2_dbg(1, debug, sd, "querystd\n");
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300328 return vpx3220_status(sd, NULL, std);
329}
330
331static int vpx3220_g_input_status(struct v4l2_subdev *sd, u32 *status)
332{
Hans Verkuilbccfa442009-03-30 06:55:27 -0300333 v4l2_dbg(1, debug, sd, "g_input_status\n");
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300334 return vpx3220_status(sd, status, NULL);
335}
336
337static int vpx3220_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
338{
339 struct vpx3220 *decoder = to_vpx3220(sd);
340 int temp_input;
341
342 /* Here we back up the input selection because it gets
343 overwritten when we fill the registers with the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300344 chosen video norm */
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300345 temp_input = vpx3220_fp_read(sd, 0xf2);
346
Hans Verkuilbccfa442009-03-30 06:55:27 -0300347 v4l2_dbg(1, debug, sd, "s_std %llx\n", (unsigned long long)std);
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300348 if (std & V4L2_STD_NTSC) {
349 vpx3220_write_fp_block(sd, init_ntsc, sizeof(init_ntsc) >> 1);
350 v4l2_dbg(1, debug, sd, "norm switched to NTSC\n");
351 } else if (std & V4L2_STD_PAL) {
352 vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
353 v4l2_dbg(1, debug, sd, "norm switched to PAL\n");
354 } else if (std & V4L2_STD_SECAM) {
355 vpx3220_write_fp_block(sd, init_secam, sizeof(init_secam) >> 1);
356 v4l2_dbg(1, debug, sd, "norm switched to SECAM\n");
357 } else {
358 return -EINVAL;
Hans Verkuil23848b62008-09-07 08:01:39 -0300359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300361 decoder->norm = std;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300363 /* And here we set the backed up video input again */
364 vpx3220_fp_write(sd, 0xf2, temp_input | 0x0010);
365 udelay(10);
366 return 0;
367}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Hans Verkuil5325b422009-04-02 11:26:22 -0300369static int vpx3220_s_routing(struct v4l2_subdev *sd,
370 u32 input, u32 output, u32 config)
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300371{
372 int data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Hans Verkuil5325b422009-04-02 11:26:22 -0300374 /* RJ: input = 0: ST8 (PCTV) input
375 input = 1: COMPOSITE input
376 input = 2: SVHS input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Colin Ian King9ecb6712019-10-06 12:04:29 -0300378 static const int input_vals[3][2] = {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300379 {0x0c, 0},
380 {0x0d, 0},
381 {0x0e, 1}
382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Roel Kluinf14a2972009-10-23 07:59:42 -0300384 if (input > 2)
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300385 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Hans Verkuil5325b422009-04-02 11:26:22 -0300387 v4l2_dbg(1, debug, sd, "input switched to %s\n", inputs[input]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Hans Verkuil5325b422009-04-02 11:26:22 -0300389 vpx3220_write(sd, 0x33, input_vals[input][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300391 data = vpx3220_fp_read(sd, 0xf2) & ~(0x0020);
392 if (data < 0)
393 return data;
394 /* 0x0010 is required to latch the setting */
395 vpx3220_fp_write(sd, 0xf2,
Hans Verkuil5325b422009-04-02 11:26:22 -0300396 data | (input_vals[input][1] << 5) | 0x0010);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300398 udelay(10);
399 return 0;
400}
401
402static int vpx3220_s_stream(struct v4l2_subdev *sd, int enable)
403{
Hans Verkuilbccfa442009-03-30 06:55:27 -0300404 v4l2_dbg(1, debug, sd, "s_stream %s\n", enable ? "on" : "off");
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300405
406 vpx3220_write(sd, 0xf2, (enable ? 0x1b : 0x00));
407 return 0;
408}
409
Hans Verkuil9a775322010-12-12 08:45:59 -0300410static int vpx3220_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300411{
Hans Verkuil9a775322010-12-12 08:45:59 -0300412 struct v4l2_subdev *sd = to_sd(ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300414 switch (ctrl->id) {
415 case V4L2_CID_BRIGHTNESS:
Hans Verkuil9a775322010-12-12 08:45:59 -0300416 vpx3220_write(sd, 0xe6, ctrl->val);
417 return 0;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300418 case V4L2_CID_CONTRAST:
Hans Verkuil9a775322010-12-12 08:45:59 -0300419 /* Bit 7 and 8 is for noise shaping */
420 vpx3220_write(sd, 0xe7, ctrl->val + 192);
421 return 0;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300422 case V4L2_CID_SATURATION:
Hans Verkuil9a775322010-12-12 08:45:59 -0300423 vpx3220_fp_write(sd, 0xa0, ctrl->val);
424 return 0;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300425 case V4L2_CID_HUE:
Hans Verkuil9a775322010-12-12 08:45:59 -0300426 vpx3220_fp_write(sd, 0x1c, ctrl->val);
427 return 0;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300428 }
Hans Verkuil9a775322010-12-12 08:45:59 -0300429 return -EINVAL;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300430}
431
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300432/* ----------------------------------------------------------------------- */
433
Hans Verkuil9a775322010-12-12 08:45:59 -0300434static const struct v4l2_ctrl_ops vpx3220_ctrl_ops = {
435 .s_ctrl = vpx3220_s_ctrl,
436};
437
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300438static const struct v4l2_subdev_core_ops vpx3220_core_ops = {
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300439 .init = vpx3220_init,
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300440};
441
442static const struct v4l2_subdev_video_ops vpx3220_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300443 .s_std = vpx3220_s_std,
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300444 .s_routing = vpx3220_s_routing,
445 .s_stream = vpx3220_s_stream,
446 .querystd = vpx3220_querystd,
447 .g_input_status = vpx3220_g_input_status,
448};
449
450static const struct v4l2_subdev_ops vpx3220_ops = {
451 .core = &vpx3220_core_ops,
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300452 .video = &vpx3220_video_ops,
453};
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/* -----------------------------------------------------------------------
Joe Perchesc84e6032008-02-03 17:18:59 +0200456 * Client management code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
458
Hans Verkuil23848b62008-09-07 08:01:39 -0300459static int vpx3220_probe(struct i2c_client *client,
460 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct vpx3220 *decoder;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300463 struct v4l2_subdev *sd;
Hans Verkuil23848b62008-09-07 08:01:39 -0300464 const char *name = NULL;
465 u8 ver;
466 u16 pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 /* Check if the adapter supports the needed features */
Hans Verkuil23848b62008-09-07 08:01:39 -0300469 if (!i2c_check_functionality(client->adapter,
470 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
471 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300473 decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
Hans Verkuil23848b62008-09-07 08:01:39 -0300474 if (decoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return -ENOMEM;
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300476 sd = &decoder->sd;
477 v4l2_i2c_subdev_init(sd, client, &vpx3220_ops);
Hans Verkuil107063c2009-02-18 17:26:06 -0300478 decoder->norm = V4L2_STD_PAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 decoder->input = 0;
480 decoder->enable = 1;
Hans Verkuil9a775322010-12-12 08:45:59 -0300481 v4l2_ctrl_handler_init(&decoder->hdl, 4);
482 v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
483 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
484 v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
485 V4L2_CID_CONTRAST, 0, 63, 1, 32);
486 v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
487 V4L2_CID_SATURATION, 0, 4095, 1, 2048);
488 v4l2_ctrl_new_std(&decoder->hdl, &vpx3220_ctrl_ops,
489 V4L2_CID_HUE, -512, 511, 1, 0);
490 sd->ctrl_handler = &decoder->hdl;
491 if (decoder->hdl.error) {
492 int err = decoder->hdl.error;
493
494 v4l2_ctrl_handler_free(&decoder->hdl);
Hans Verkuil9a775322010-12-12 08:45:59 -0300495 return err;
496 }
497 v4l2_ctrl_handler_setup(&decoder->hdl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Hans Verkuil23848b62008-09-07 08:01:39 -0300499 ver = i2c_smbus_read_byte_data(client, 0x00);
500 pn = (i2c_smbus_read_byte_data(client, 0x02) << 8) +
501 i2c_smbus_read_byte_data(client, 0x01);
502 if (ver == 0xec) {
503 switch (pn) {
504 case 0x4680:
505 name = "vpx3220a";
506 break;
507 case 0x4260:
508 name = "vpx3216b";
509 break;
510 case 0x4280:
511 name = "vpx3214c";
512 break;
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Hans Verkuil23848b62008-09-07 08:01:39 -0300515 if (name)
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300516 v4l2_info(sd, "%s found @ 0x%x (%s)\n", name,
Hans Verkuil23848b62008-09-07 08:01:39 -0300517 client->addr << 1, client->adapter->name);
518 else
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300519 v4l2_info(sd, "chip (%02x:%04x) found @ 0x%x (%s)\n",
Hans Verkuil23848b62008-09-07 08:01:39 -0300520 ver, pn, client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300522 vpx3220_write_block(sd, init_common, sizeof(init_common));
523 vpx3220_write_fp_block(sd, init_fp, sizeof(init_fp) >> 1);
524 /* Default to PAL */
525 vpx3220_write_fp_block(sd, init_pal, sizeof(init_pal) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return 0;
527}
528
Hans Verkuil23848b62008-09-07 08:01:39 -0300529static int vpx3220_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300531 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Hans Verkuil9a775322010-12-12 08:45:59 -0300532 struct vpx3220 *decoder = to_vpx3220(sd);
Hans Verkuil7e5eaad2009-02-19 14:36:53 -0300533
534 v4l2_device_unregister_subdev(sd);
Hans Verkuil9a775322010-12-12 08:45:59 -0300535 v4l2_ctrl_handler_free(&decoder->hdl);
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300536
Hans Verkuil23848b62008-09-07 08:01:39 -0300537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Hans Verkuil23848b62008-09-07 08:01:39 -0300540static const struct i2c_device_id vpx3220_id[] = {
541 { "vpx3220a", 0 },
542 { "vpx3216b", 0 },
543 { "vpx3214c", 0 },
544 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545};
Hans Verkuil23848b62008-09-07 08:01:39 -0300546MODULE_DEVICE_TABLE(i2c, vpx3220_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Hans Verkuilc2d999f2010-09-15 15:44:11 -0300548static struct i2c_driver vpx3220_driver = {
549 .driver = {
Hans Verkuilc2d999f2010-09-15 15:44:11 -0300550 .name = "vpx3220",
551 },
552 .probe = vpx3220_probe,
553 .remove = vpx3220_remove,
554 .id_table = vpx3220_id,
Hans Verkuil23848b62008-09-07 08:01:39 -0300555};
Hans Verkuilc2d999f2010-09-15 15:44:11 -0300556
Axel Linc6e8d862012-02-12 06:56:32 -0300557module_i2c_driver(vpx3220_driver);