blob: a0bcfefc28fcdb058002db73013d16c232c27402 [file] [log] [blame]
Scott Jiang202ea1f2012-03-08 17:44:15 -03001/*
2 * adv7183.c Analog Devices ADV7183 video decoder driver
3 *
4 * Copyright (c) 2011 Analog Devices Inc.
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 version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <linux/delay.h>
21#include <linux/errno.h>
22#include <linux/gpio.h>
23#include <linux/i2c.h>
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/types.h>
28#include <linux/videodev2.h>
29
30#include <media/adv7183.h>
Scott Jiang202ea1f2012-03-08 17:44:15 -030031#include <media/v4l2-ctrls.h>
32#include <media/v4l2-device.h>
33
34#include "adv7183_regs.h"
35
36struct adv7183 {
37 struct v4l2_subdev sd;
38 struct v4l2_ctrl_handler hdl;
39
40 v4l2_std_id std; /* Current set standard */
41 u32 input;
42 u32 output;
43 unsigned reset_pin;
44 unsigned oe_pin;
45 struct v4l2_mbus_framefmt fmt;
46};
47
48/* EXAMPLES USING 27 MHz CLOCK
49 * Mode 1 CVBS Input (Composite Video on AIN5)
50 * All standards are supported through autodetect, 8-bit, 4:2:2, ITU-R BT.656 output on P15 to P8.
51 */
52static const unsigned char adv7183_init_regs[] = {
53 ADV7183_IN_CTRL, 0x04, /* CVBS input on AIN5 */
54 ADV7183_DIGI_CLAMP_CTRL_1, 0x00, /* Slow down digital clamps */
55 ADV7183_SHAP_FILT_CTRL, 0x41, /* Set CSFM to SH1 */
56 ADV7183_ADC_CTRL, 0x16, /* Power down ADC 1 and ADC 2 */
57 ADV7183_CTI_DNR_CTRL_4, 0x04, /* Set DNR threshold to 4 for flat response */
58 /* ADI recommended programming sequence */
59 ADV7183_ADI_CTRL, 0x80,
60 ADV7183_CTI_DNR_CTRL_4, 0x20,
61 0x52, 0x18,
62 0x58, 0xED,
63 0x77, 0xC5,
64 0x7C, 0x93,
65 0x7D, 0x00,
66 0xD0, 0x48,
67 0xD5, 0xA0,
68 0xD7, 0xEA,
69 ADV7183_SD_SATURATION_CR, 0x3E,
70 ADV7183_PAL_V_END, 0x3E,
71 ADV7183_PAL_F_TOGGLE, 0x0F,
72 ADV7183_ADI_CTRL, 0x00,
73};
74
75static inline struct adv7183 *to_adv7183(struct v4l2_subdev *sd)
76{
77 return container_of(sd, struct adv7183, sd);
78}
79static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
80{
81 return &container_of(ctrl->handler, struct adv7183, hdl)->sd;
82}
83
84static inline int adv7183_read(struct v4l2_subdev *sd, unsigned char reg)
85{
86 struct i2c_client *client = v4l2_get_subdevdata(sd);
87
88 return i2c_smbus_read_byte_data(client, reg);
89}
90
91static inline int adv7183_write(struct v4l2_subdev *sd, unsigned char reg,
92 unsigned char value)
93{
94 struct i2c_client *client = v4l2_get_subdevdata(sd);
95
96 return i2c_smbus_write_byte_data(client, reg, value);
97}
98
99static int adv7183_writeregs(struct v4l2_subdev *sd,
100 const unsigned char *regs, unsigned int num)
101{
102 unsigned char reg, data;
103 unsigned int cnt = 0;
104
105 if (num & 0x1) {
106 v4l2_err(sd, "invalid regs array\n");
107 return -1;
108 }
109
110 while (cnt < num) {
111 reg = *regs++;
112 data = *regs++;
113 cnt += 2;
114
115 adv7183_write(sd, reg, data);
116 }
117 return 0;
118}
119
120static int adv7183_log_status(struct v4l2_subdev *sd)
121{
122 struct adv7183 *decoder = to_adv7183(sd);
123
124 v4l2_info(sd, "adv7183: Input control = 0x%02x\n",
125 adv7183_read(sd, ADV7183_IN_CTRL));
126 v4l2_info(sd, "adv7183: Video selection = 0x%02x\n",
127 adv7183_read(sd, ADV7183_VD_SEL));
128 v4l2_info(sd, "adv7183: Output control = 0x%02x\n",
129 adv7183_read(sd, ADV7183_OUT_CTRL));
130 v4l2_info(sd, "adv7183: Extended output control = 0x%02x\n",
131 adv7183_read(sd, ADV7183_EXT_OUT_CTRL));
132 v4l2_info(sd, "adv7183: Autodetect enable = 0x%02x\n",
133 adv7183_read(sd, ADV7183_AUTO_DET_EN));
134 v4l2_info(sd, "adv7183: Contrast = 0x%02x\n",
135 adv7183_read(sd, ADV7183_CONTRAST));
136 v4l2_info(sd, "adv7183: Brightness = 0x%02x\n",
137 adv7183_read(sd, ADV7183_BRIGHTNESS));
138 v4l2_info(sd, "adv7183: Hue = 0x%02x\n",
139 adv7183_read(sd, ADV7183_HUE));
140 v4l2_info(sd, "adv7183: Default value Y = 0x%02x\n",
141 adv7183_read(sd, ADV7183_DEF_Y));
142 v4l2_info(sd, "adv7183: Default value C = 0x%02x\n",
143 adv7183_read(sd, ADV7183_DEF_C));
144 v4l2_info(sd, "adv7183: ADI control = 0x%02x\n",
145 adv7183_read(sd, ADV7183_ADI_CTRL));
146 v4l2_info(sd, "adv7183: Power Management = 0x%02x\n",
147 adv7183_read(sd, ADV7183_POW_MANAGE));
148 v4l2_info(sd, "adv7183: Status 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
149 adv7183_read(sd, ADV7183_STATUS_1),
150 adv7183_read(sd, ADV7183_STATUS_2),
151 adv7183_read(sd, ADV7183_STATUS_3));
152 v4l2_info(sd, "adv7183: Ident = 0x%02x\n",
153 adv7183_read(sd, ADV7183_IDENT));
154 v4l2_info(sd, "adv7183: Analog clamp control = 0x%02x\n",
155 adv7183_read(sd, ADV7183_ANAL_CLAMP_CTRL));
156 v4l2_info(sd, "adv7183: Digital clamp control 1 = 0x%02x\n",
157 adv7183_read(sd, ADV7183_DIGI_CLAMP_CTRL_1));
158 v4l2_info(sd, "adv7183: Shaping filter control 1 and 2 = 0x%02x 0x%02x\n",
159 adv7183_read(sd, ADV7183_SHAP_FILT_CTRL),
160 adv7183_read(sd, ADV7183_SHAP_FILT_CTRL_2));
161 v4l2_info(sd, "adv7183: Comb filter control = 0x%02x\n",
162 adv7183_read(sd, ADV7183_COMB_FILT_CTRL));
163 v4l2_info(sd, "adv7183: ADI control 2 = 0x%02x\n",
164 adv7183_read(sd, ADV7183_ADI_CTRL_2));
165 v4l2_info(sd, "adv7183: Pixel delay control = 0x%02x\n",
166 adv7183_read(sd, ADV7183_PIX_DELAY_CTRL));
167 v4l2_info(sd, "adv7183: Misc gain control = 0x%02x\n",
168 adv7183_read(sd, ADV7183_MISC_GAIN_CTRL));
169 v4l2_info(sd, "adv7183: AGC mode control = 0x%02x\n",
170 adv7183_read(sd, ADV7183_AGC_MODE_CTRL));
171 v4l2_info(sd, "adv7183: Chroma gain control 1 and 2 = 0x%02x 0x%02x\n",
172 adv7183_read(sd, ADV7183_CHRO_GAIN_CTRL_1),
173 adv7183_read(sd, ADV7183_CHRO_GAIN_CTRL_2));
174 v4l2_info(sd, "adv7183: Luma gain control 1 and 2 = 0x%02x 0x%02x\n",
175 adv7183_read(sd, ADV7183_LUMA_GAIN_CTRL_1),
176 adv7183_read(sd, ADV7183_LUMA_GAIN_CTRL_2));
177 v4l2_info(sd, "adv7183: Vsync field control 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
178 adv7183_read(sd, ADV7183_VS_FIELD_CTRL_1),
179 adv7183_read(sd, ADV7183_VS_FIELD_CTRL_2),
180 adv7183_read(sd, ADV7183_VS_FIELD_CTRL_3));
Masanari Iida6d3be302013-09-30 23:19:09 +0900181 v4l2_info(sd, "adv7183: Hsync position control 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
Scott Jiang202ea1f2012-03-08 17:44:15 -0300182 adv7183_read(sd, ADV7183_HS_POS_CTRL_1),
183 adv7183_read(sd, ADV7183_HS_POS_CTRL_2),
184 adv7183_read(sd, ADV7183_HS_POS_CTRL_3));
185 v4l2_info(sd, "adv7183: Polarity = 0x%02x\n",
186 adv7183_read(sd, ADV7183_POLARITY));
187 v4l2_info(sd, "adv7183: ADC control = 0x%02x\n",
188 adv7183_read(sd, ADV7183_ADC_CTRL));
189 v4l2_info(sd, "adv7183: SD offset Cb and Cr = 0x%02x 0x%02x\n",
190 adv7183_read(sd, ADV7183_SD_OFFSET_CB),
191 adv7183_read(sd, ADV7183_SD_OFFSET_CR));
192 v4l2_info(sd, "adv7183: SD saturation Cb and Cr = 0x%02x 0x%02x\n",
193 adv7183_read(sd, ADV7183_SD_SATURATION_CB),
194 adv7183_read(sd, ADV7183_SD_SATURATION_CR));
195 v4l2_info(sd, "adv7183: Drive strength = 0x%02x\n",
196 adv7183_read(sd, ADV7183_DRIVE_STR));
197 v4l2_ctrl_handler_log_status(&decoder->hdl, sd->name);
198 return 0;
199}
200
201static int adv7183_g_std(struct v4l2_subdev *sd, v4l2_std_id *std)
202{
203 struct adv7183 *decoder = to_adv7183(sd);
204
205 *std = decoder->std;
206 return 0;
207}
208
209static int adv7183_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
210{
211 struct adv7183 *decoder = to_adv7183(sd);
212 int reg;
213
214 reg = adv7183_read(sd, ADV7183_IN_CTRL) & 0xF;
215 if (std == V4L2_STD_PAL_60)
216 reg |= 0x60;
217 else if (std == V4L2_STD_NTSC_443)
218 reg |= 0x70;
219 else if (std == V4L2_STD_PAL_N)
220 reg |= 0x90;
221 else if (std == V4L2_STD_PAL_M)
222 reg |= 0xA0;
223 else if (std == V4L2_STD_PAL_Nc)
224 reg |= 0xC0;
225 else if (std & V4L2_STD_PAL)
226 reg |= 0x80;
227 else if (std & V4L2_STD_NTSC)
228 reg |= 0x50;
229 else if (std & V4L2_STD_SECAM)
230 reg |= 0xE0;
231 else
232 return -EINVAL;
233 adv7183_write(sd, ADV7183_IN_CTRL, reg);
234
235 decoder->std = std;
236
237 return 0;
238}
239
240static int adv7183_reset(struct v4l2_subdev *sd, u32 val)
241{
242 int reg;
243
244 reg = adv7183_read(sd, ADV7183_POW_MANAGE) | 0x80;
245 adv7183_write(sd, ADV7183_POW_MANAGE, reg);
246 /* wait 5ms before any further i2c writes are performed */
247 usleep_range(5000, 10000);
248 return 0;
249}
250
251static int adv7183_s_routing(struct v4l2_subdev *sd,
252 u32 input, u32 output, u32 config)
253{
254 struct adv7183 *decoder = to_adv7183(sd);
255 int reg;
256
257 if ((input > ADV7183_COMPONENT1) || (output > ADV7183_16BIT_OUT))
258 return -EINVAL;
259
260 if (input != decoder->input) {
261 decoder->input = input;
262 reg = adv7183_read(sd, ADV7183_IN_CTRL) & 0xF0;
263 switch (input) {
264 case ADV7183_COMPOSITE1:
265 reg |= 0x1;
266 break;
267 case ADV7183_COMPOSITE2:
268 reg |= 0x2;
269 break;
270 case ADV7183_COMPOSITE3:
271 reg |= 0x3;
272 break;
273 case ADV7183_COMPOSITE4:
274 reg |= 0x4;
275 break;
276 case ADV7183_COMPOSITE5:
277 reg |= 0x5;
278 break;
279 case ADV7183_COMPOSITE6:
280 reg |= 0xB;
281 break;
282 case ADV7183_COMPOSITE7:
283 reg |= 0xC;
284 break;
285 case ADV7183_COMPOSITE8:
286 reg |= 0xD;
287 break;
288 case ADV7183_COMPOSITE9:
289 reg |= 0xE;
290 break;
291 case ADV7183_COMPOSITE10:
292 reg |= 0xF;
293 break;
294 case ADV7183_SVIDEO0:
295 reg |= 0x6;
296 break;
297 case ADV7183_SVIDEO1:
298 reg |= 0x7;
299 break;
300 case ADV7183_SVIDEO2:
301 reg |= 0x8;
302 break;
303 case ADV7183_COMPONENT0:
304 reg |= 0x9;
305 break;
306 case ADV7183_COMPONENT1:
307 reg |= 0xA;
308 break;
309 default:
310 break;
311 }
312 adv7183_write(sd, ADV7183_IN_CTRL, reg);
313 }
314
315 if (output != decoder->output) {
316 decoder->output = output;
317 reg = adv7183_read(sd, ADV7183_OUT_CTRL) & 0xC0;
318 switch (output) {
319 case ADV7183_16BIT_OUT:
320 reg |= 0x9;
321 break;
322 default:
323 reg |= 0xC;
324 break;
325 }
326 adv7183_write(sd, ADV7183_OUT_CTRL, reg);
327 }
328
329 return 0;
330}
331
332static int adv7183_s_ctrl(struct v4l2_ctrl *ctrl)
333{
334 struct v4l2_subdev *sd = to_sd(ctrl);
335 int val = ctrl->val;
336
337 switch (ctrl->id) {
338 case V4L2_CID_BRIGHTNESS:
339 if (val < 0)
340 val = 127 - val;
341 adv7183_write(sd, ADV7183_BRIGHTNESS, val);
342 break;
343 case V4L2_CID_CONTRAST:
344 adv7183_write(sd, ADV7183_CONTRAST, val);
345 break;
346 case V4L2_CID_SATURATION:
347 adv7183_write(sd, ADV7183_SD_SATURATION_CB, val >> 8);
348 adv7183_write(sd, ADV7183_SD_SATURATION_CR, (val & 0xFF));
349 break;
350 case V4L2_CID_HUE:
351 adv7183_write(sd, ADV7183_SD_OFFSET_CB, val >> 8);
352 adv7183_write(sd, ADV7183_SD_OFFSET_CR, (val & 0xFF));
353 break;
354 default:
355 return -EINVAL;
356 }
357
358 return 0;
359}
360
361static int adv7183_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
362{
363 struct adv7183 *decoder = to_adv7183(sd);
364 int reg;
365
366 /* enable autodetection block */
367 reg = adv7183_read(sd, ADV7183_IN_CTRL) & 0xF;
368 adv7183_write(sd, ADV7183_IN_CTRL, reg);
369
370 /* wait autodetection switch */
371 mdelay(10);
372
373 /* get autodetection result */
374 reg = adv7183_read(sd, ADV7183_STATUS_1);
375 switch ((reg >> 0x4) & 0x7) {
376 case 0:
Hans Verkuil7dd8fbbe2013-05-29 10:18:54 -0300377 *std &= V4L2_STD_NTSC;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300378 break;
379 case 1:
Hans Verkuil7dd8fbbe2013-05-29 10:18:54 -0300380 *std &= V4L2_STD_NTSC_443;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300381 break;
382 case 2:
Hans Verkuil7dd8fbbe2013-05-29 10:18:54 -0300383 *std &= V4L2_STD_PAL_M;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300384 break;
385 case 3:
Hans Verkuil7dd8fbbe2013-05-29 10:18:54 -0300386 *std &= V4L2_STD_PAL_60;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300387 break;
388 case 4:
Hans Verkuil7dd8fbbe2013-05-29 10:18:54 -0300389 *std &= V4L2_STD_PAL;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300390 break;
391 case 5:
Hans Verkuil7dd8fbbe2013-05-29 10:18:54 -0300392 *std &= V4L2_STD_SECAM;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300393 break;
394 case 6:
Hans Verkuil7dd8fbbe2013-05-29 10:18:54 -0300395 *std &= V4L2_STD_PAL_Nc;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300396 break;
397 case 7:
Hans Verkuil7dd8fbbe2013-05-29 10:18:54 -0300398 *std &= V4L2_STD_SECAM;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300399 break;
400 default:
401 *std = V4L2_STD_UNKNOWN;
402 break;
403 }
404
405 /* after std detection, write back user set std */
406 adv7183_s_std(sd, decoder->std);
407 return 0;
408}
409
410static int adv7183_g_input_status(struct v4l2_subdev *sd, u32 *status)
411{
412 int reg;
413
414 *status = V4L2_IN_ST_NO_SIGNAL;
415 reg = adv7183_read(sd, ADV7183_STATUS_1);
416 if (reg < 0)
417 return reg;
418 if (reg & 0x1)
419 *status = 0;
420 return 0;
421}
422
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300423static int adv7183_enum_mbus_code(struct v4l2_subdev *sd,
424 struct v4l2_subdev_pad_config *cfg,
425 struct v4l2_subdev_mbus_code_enum *code)
Scott Jiang202ea1f2012-03-08 17:44:15 -0300426{
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300427 if (code->pad || code->index > 0)
Scott Jiang202ea1f2012-03-08 17:44:15 -0300428 return -EINVAL;
429
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300430 code->code = MEDIA_BUS_FMT_UYVY8_2X8;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300431 return 0;
432}
433
434static int adv7183_try_mbus_fmt(struct v4l2_subdev *sd,
435 struct v4l2_mbus_framefmt *fmt)
436{
437 struct adv7183 *decoder = to_adv7183(sd);
438
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300439 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300440 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
441 if (decoder->std & V4L2_STD_525_60) {
442 fmt->field = V4L2_FIELD_SEQ_TB;
443 fmt->width = 720;
444 fmt->height = 480;
445 } else {
446 fmt->field = V4L2_FIELD_SEQ_BT;
447 fmt->width = 720;
448 fmt->height = 576;
449 }
450 return 0;
451}
452
453static int adv7183_s_mbus_fmt(struct v4l2_subdev *sd,
454 struct v4l2_mbus_framefmt *fmt)
455{
456 struct adv7183 *decoder = to_adv7183(sd);
457
458 adv7183_try_mbus_fmt(sd, fmt);
459 decoder->fmt = *fmt;
460 return 0;
461}
462
463static int adv7183_g_mbus_fmt(struct v4l2_subdev *sd,
464 struct v4l2_mbus_framefmt *fmt)
465{
466 struct adv7183 *decoder = to_adv7183(sd);
467
468 *fmt = decoder->fmt;
469 return 0;
470}
471
472static int adv7183_s_stream(struct v4l2_subdev *sd, int enable)
473{
474 struct adv7183 *decoder = to_adv7183(sd);
475
476 if (enable)
Laurent Pinchart95323362013-05-02 11:11:50 -0300477 gpio_set_value(decoder->oe_pin, 0);
Scott Jiang202ea1f2012-03-08 17:44:15 -0300478 else
Laurent Pinchart95323362013-05-02 11:11:50 -0300479 gpio_set_value(decoder->oe_pin, 1);
Scott Jiang202ea1f2012-03-08 17:44:15 -0300480 udelay(1);
481 return 0;
482}
483
Scott Jiang202ea1f2012-03-08 17:44:15 -0300484#ifdef CONFIG_VIDEO_ADV_DEBUG
485static int adv7183_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
486{
Scott Jiang202ea1f2012-03-08 17:44:15 -0300487 reg->val = adv7183_read(sd, reg->reg & 0xff);
488 reg->size = 1;
489 return 0;
490}
491
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300492static int adv7183_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
Scott Jiang202ea1f2012-03-08 17:44:15 -0300493{
Scott Jiang202ea1f2012-03-08 17:44:15 -0300494 adv7183_write(sd, reg->reg & 0xff, reg->val & 0xff);
495 return 0;
496}
497#endif
498
499static const struct v4l2_ctrl_ops adv7183_ctrl_ops = {
500 .s_ctrl = adv7183_s_ctrl,
501};
502
503static const struct v4l2_subdev_core_ops adv7183_core_ops = {
504 .log_status = adv7183_log_status,
Scott Jiang202ea1f2012-03-08 17:44:15 -0300505 .reset = adv7183_reset,
Scott Jiang202ea1f2012-03-08 17:44:15 -0300506#ifdef CONFIG_VIDEO_ADV_DEBUG
507 .g_register = adv7183_g_register,
508 .s_register = adv7183_s_register,
509#endif
510};
511
512static const struct v4l2_subdev_video_ops adv7183_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300513 .g_std = adv7183_g_std,
514 .s_std = adv7183_s_std,
Scott Jiang202ea1f2012-03-08 17:44:15 -0300515 .s_routing = adv7183_s_routing,
516 .querystd = adv7183_querystd,
517 .g_input_status = adv7183_g_input_status,
Scott Jiang202ea1f2012-03-08 17:44:15 -0300518 .try_mbus_fmt = adv7183_try_mbus_fmt,
519 .s_mbus_fmt = adv7183_s_mbus_fmt,
520 .g_mbus_fmt = adv7183_g_mbus_fmt,
521 .s_stream = adv7183_s_stream,
522};
523
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300524static const struct v4l2_subdev_pad_ops adv7183_pad_ops = {
525 .enum_mbus_code = adv7183_enum_mbus_code,
526};
527
Scott Jiang202ea1f2012-03-08 17:44:15 -0300528static const struct v4l2_subdev_ops adv7183_ops = {
529 .core = &adv7183_core_ops,
530 .video = &adv7183_video_ops,
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300531 .pad = &adv7183_pad_ops,
Scott Jiang202ea1f2012-03-08 17:44:15 -0300532};
533
534static int adv7183_probe(struct i2c_client *client,
535 const struct i2c_device_id *id)
536{
537 struct adv7183 *decoder;
538 struct v4l2_subdev *sd;
539 struct v4l2_ctrl_handler *hdl;
540 int ret;
541 struct v4l2_mbus_framefmt fmt;
542 const unsigned *pin_array;
543
544 /* Check if the adapter supports the needed features */
545 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
546 return -EIO;
547
548 v4l_info(client, "chip found @ 0x%02x (%s)\n",
549 client->addr << 1, client->adapter->name);
550
551 pin_array = client->dev.platform_data;
552 if (pin_array == NULL)
553 return -EINVAL;
554
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300555 decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
Scott Jiang202ea1f2012-03-08 17:44:15 -0300556 if (decoder == NULL)
557 return -ENOMEM;
558
559 decoder->reset_pin = pin_array[0];
560 decoder->oe_pin = pin_array[1];
561
Laurent Pinchartb015ba22013-05-02 08:29:43 -0300562 if (devm_gpio_request_one(&client->dev, decoder->reset_pin,
563 GPIOF_OUT_INIT_LOW, "ADV7183 Reset")) {
Scott Jiang202ea1f2012-03-08 17:44:15 -0300564 v4l_err(client, "failed to request GPIO %d\n", decoder->reset_pin);
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300565 return -EBUSY;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300566 }
567
Laurent Pinchartb015ba22013-05-02 08:29:43 -0300568 if (devm_gpio_request_one(&client->dev, decoder->oe_pin,
569 GPIOF_OUT_INIT_HIGH,
570 "ADV7183 Output Enable")) {
Scott Jiang202ea1f2012-03-08 17:44:15 -0300571 v4l_err(client, "failed to request GPIO %d\n", decoder->oe_pin);
Laurent Pinchartb015ba22013-05-02 08:29:43 -0300572 return -EBUSY;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300573 }
574
575 sd = &decoder->sd;
576 v4l2_i2c_subdev_init(sd, client, &adv7183_ops);
577
578 hdl = &decoder->hdl;
579 v4l2_ctrl_handler_init(hdl, 4);
580 v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
581 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
582 v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
583 V4L2_CID_CONTRAST, 0, 0xFF, 1, 0x80);
584 v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
585 V4L2_CID_SATURATION, 0, 0xFFFF, 1, 0x8080);
586 v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
587 V4L2_CID_HUE, 0, 0xFFFF, 1, 0x8080);
588 /* hook the control handler into the driver */
589 sd->ctrl_handler = hdl;
590 if (hdl->error) {
591 ret = hdl->error;
592
593 v4l2_ctrl_handler_free(hdl);
Laurent Pinchartb015ba22013-05-02 08:29:43 -0300594 return ret;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300595 }
596
597 /* v4l2 doesn't support an autodetect standard, pick PAL as default */
598 decoder->std = V4L2_STD_PAL;
599 decoder->input = ADV7183_COMPOSITE4;
600 decoder->output = ADV7183_8BIT_OUT;
601
Scott Jiang202ea1f2012-03-08 17:44:15 -0300602 /* reset chip */
Scott Jiang202ea1f2012-03-08 17:44:15 -0300603 /* reset pulse width at least 5ms */
604 mdelay(10);
Laurent Pinchart95323362013-05-02 11:11:50 -0300605 gpio_set_value(decoder->reset_pin, 1);
Scott Jiang202ea1f2012-03-08 17:44:15 -0300606 /* wait 5ms before any further i2c writes are performed */
607 mdelay(5);
608
609 adv7183_writeregs(sd, adv7183_init_regs, ARRAY_SIZE(adv7183_init_regs));
610 adv7183_s_std(sd, decoder->std);
611 fmt.width = 720;
612 fmt.height = 576;
613 adv7183_s_mbus_fmt(sd, &fmt);
614
615 /* initialize the hardware to the default control values */
616 ret = v4l2_ctrl_handler_setup(hdl);
617 if (ret) {
618 v4l2_ctrl_handler_free(hdl);
Laurent Pinchartb015ba22013-05-02 08:29:43 -0300619 return ret;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300620 }
621
622 return 0;
Scott Jiang202ea1f2012-03-08 17:44:15 -0300623}
624
625static int adv7183_remove(struct i2c_client *client)
626{
627 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Scott Jiang202ea1f2012-03-08 17:44:15 -0300628
629 v4l2_device_unregister_subdev(sd);
630 v4l2_ctrl_handler_free(sd->ctrl_handler);
Scott Jiang202ea1f2012-03-08 17:44:15 -0300631 return 0;
632}
633
634static const struct i2c_device_id adv7183_id[] = {
635 {"adv7183", 0},
636 {},
637};
638
639MODULE_DEVICE_TABLE(i2c, adv7183_id);
640
641static struct i2c_driver adv7183_driver = {
642 .driver = {
643 .owner = THIS_MODULE,
644 .name = "adv7183",
645 },
646 .probe = adv7183_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800647 .remove = adv7183_remove,
Scott Jiang202ea1f2012-03-08 17:44:15 -0300648 .id_table = adv7183_id,
649};
650
Wei Yongjun01aea0b2012-10-08 10:13:41 -0300651module_i2c_driver(adv7183_driver);
Scott Jiang202ea1f2012-03-08 17:44:15 -0300652
653MODULE_DESCRIPTION("Analog Devices ADV7183 video decoder driver");
654MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
655MODULE_LICENSE("GPL v2");