2 * adv7343 - ADV7343 Video Encoder Driver
4 * The encoder hardware does not support SECAM.
6 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
12 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/ctype.h>
21 #include <linux/i2c.h>
22 #include <linux/device.h>
23 #include <linux/delay.h>
24 #include <linux/module.h>
25 #include <linux/videodev2.h>
26 #include <linux/uaccess.h>
27 #include <linux/version.h>
29 #include <media/adv7343.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-chip-ident.h>
33 #include "adv7343_regs.h"
35 MODULE_DESCRIPTION("ADV7343 video encoder driver");
36 MODULE_LICENSE("GPL");
39 module_param(debug, int, 0644);
40 MODULE_PARM_DESC(debug, "Debug level 0-1");
42 struct adv7343_state {
43 struct v4l2_subdev sd;
57 static inline struct adv7343_state *to_state(struct v4l2_subdev *sd)
59 return container_of(sd, struct adv7343_state, sd);
62 static inline int adv7343_write(struct v4l2_subdev *sd, u8 reg, u8 value)
64 struct i2c_client *client = v4l2_get_subdevdata(sd);
66 return i2c_smbus_write_byte_data(client, reg, value);
69 static const u8 adv7343_init_reg_val[] = {
70 ADV7343_SOFT_RESET, ADV7343_SOFT_RESET_DEFAULT,
71 ADV7343_POWER_MODE_REG, ADV7343_POWER_MODE_REG_DEFAULT,
73 ADV7343_HD_MODE_REG1, ADV7343_HD_MODE_REG1_DEFAULT,
74 ADV7343_HD_MODE_REG2, ADV7343_HD_MODE_REG2_DEFAULT,
75 ADV7343_HD_MODE_REG3, ADV7343_HD_MODE_REG3_DEFAULT,
76 ADV7343_HD_MODE_REG4, ADV7343_HD_MODE_REG4_DEFAULT,
77 ADV7343_HD_MODE_REG5, ADV7343_HD_MODE_REG5_DEFAULT,
78 ADV7343_HD_MODE_REG6, ADV7343_HD_MODE_REG6_DEFAULT,
79 ADV7343_HD_MODE_REG7, ADV7343_HD_MODE_REG7_DEFAULT,
81 ADV7343_SD_MODE_REG1, ADV7343_SD_MODE_REG1_DEFAULT,
82 ADV7343_SD_MODE_REG2, ADV7343_SD_MODE_REG2_DEFAULT,
83 ADV7343_SD_MODE_REG3, ADV7343_SD_MODE_REG3_DEFAULT,
84 ADV7343_SD_MODE_REG4, ADV7343_SD_MODE_REG4_DEFAULT,
85 ADV7343_SD_MODE_REG5, ADV7343_SD_MODE_REG5_DEFAULT,
86 ADV7343_SD_MODE_REG6, ADV7343_SD_MODE_REG6_DEFAULT,
87 ADV7343_SD_MODE_REG7, ADV7343_SD_MODE_REG7_DEFAULT,
88 ADV7343_SD_MODE_REG8, ADV7343_SD_MODE_REG8_DEFAULT,
90 ADV7343_SD_HUE_REG, ADV7343_SD_HUE_REG_DEFAULT,
91 ADV7343_SD_CGMS_WSS0, ADV7343_SD_CGMS_WSS0_DEFAULT,
92 ADV7343_SD_BRIGHTNESS_WSS, ADV7343_SD_BRIGHTNESS_WSS_DEFAULT,
97 * FSC(reg) = FSC (HZ) * --------
100 static const struct adv7343_std_info stdinfo[] = {
102 /* FSC(Hz) = 3,579,545.45 Hz */
103 SD_STD_NTSC, 569408542, V4L2_STD_NTSC,
105 /* FSC(Hz) = 3,575,611.00 Hz */
106 SD_STD_PAL_M, 568782678, V4L2_STD_PAL_M,
108 /* FSC(Hz) = 3,582,056.00 */
109 SD_STD_PAL_N, 569807903, V4L2_STD_PAL_Nc,
111 /* FSC(Hz) = 4,433,618.75 Hz */
112 SD_STD_PAL_N, 705268427, V4L2_STD_PAL_N,
114 /* FSC(Hz) = 4,433,618.75 Hz */
115 SD_STD_PAL_BDGHI, 705268427, V4L2_STD_PAL,
117 /* FSC(Hz) = 4,433,618.75 Hz */
118 SD_STD_NTSC, 705268427, V4L2_STD_NTSC_443,
120 /* FSC(Hz) = 4,433,618.75 Hz */
121 SD_STD_PAL_M, 705268427, V4L2_STD_PAL_60,
125 static int adv7343_setstd(struct v4l2_subdev *sd, v4l2_std_id std)
127 struct adv7343_state *state = to_state(sd);
128 struct adv7343_std_info *std_info;
129 int output_idx, num_std;
135 output_idx = state->output;
137 std_info = (struct adv7343_std_info *)stdinfo;
138 num_std = ARRAY_SIZE(stdinfo);
140 for (i = 0; i < num_std; i++) {
141 if (std_info[i].stdid & std)
146 v4l2_dbg(1, debug, sd,
147 "Invalid std or std is not supported: %llx\n",
148 (unsigned long long)std);
152 /* Set the standard */
153 val = state->reg80 & (~(SD_STD_MASK));
154 val |= std_info[i].standard_val3;
155 err = adv7343_write(sd, ADV7343_SD_MODE_REG1, val);
161 /* Configure the input mode register */
162 val = state->reg01 & (~((u8) INPUT_MODE_MASK));
163 val |= SD_INPUT_MODE;
164 err = adv7343_write(sd, ADV7343_MODE_SELECT_REG, val);
170 /* Program the sub carrier frequency registers */
171 fsc_ptr = (unsigned char *)&std_info[i].fsc_val;
172 reg = ADV7343_FSC_REG0;
173 for (i = 0; i < 4; i++, reg++, fsc_ptr++) {
174 err = adv7343_write(sd, reg, *fsc_ptr);
181 /* Filter settings */
182 if (std & (V4L2_STD_NTSC | V4L2_STD_NTSC_443))
184 else if (std & ~V4L2_STD_SECAM)
187 err = adv7343_write(sd, ADV7343_SD_MODE_REG1, val);
195 v4l2_err(sd, "Error setting std, write failed\n");
200 static int adv7343_setoutput(struct v4l2_subdev *sd, u32 output_type)
202 struct adv7343_state *state = to_state(sd);
206 if (output_type > ADV7343_SVIDEO_ID) {
207 v4l2_dbg(1, debug, sd,
208 "Invalid output type or output type not supported:%d\n",
213 /* Enable Appropriate DAC */
214 val = state->reg00 & 0x03;
216 if (output_type == ADV7343_COMPOSITE_ID)
217 val |= ADV7343_COMPOSITE_POWER_VALUE;
218 else if (output_type == ADV7343_COMPONENT_ID)
219 val |= ADV7343_COMPONENT_POWER_VALUE;
221 val |= ADV7343_SVIDEO_POWER_VALUE;
223 err = adv7343_write(sd, ADV7343_POWER_MODE_REG, val);
229 /* Enable YUV output */
230 val = state->reg02 | YUV_OUTPUT_SELECT;
231 err = adv7343_write(sd, ADV7343_MODE_REG0, val);
237 /* configure SD DAC Output 2 and SD DAC Output 1 bit to zero */
238 val = state->reg82 & (SD_DAC_1_DI & SD_DAC_2_DI);
239 err = adv7343_write(sd, ADV7343_SD_MODE_REG2, val);
245 /* configure ED/HD Color DAC Swap and ED/HD RGB Input Enable bit to
247 val = state->reg35 & (HD_RGB_INPUT_DI & HD_DAC_SWAP_DI);
248 err = adv7343_write(sd, ADV7343_HD_MODE_REG6, val);
256 v4l2_err(sd, "Error setting output, write failed\n");
261 static int adv7343_log_status(struct v4l2_subdev *sd)
263 struct adv7343_state *state = to_state(sd);
265 v4l2_info(sd, "Standard: %llx\n", (unsigned long long)state->std);
266 v4l2_info(sd, "Output: %s\n", (state->output == 0) ? "Composite" :
267 ((state->output == 1) ? "Component" : "S-Video"));
271 static int adv7343_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
274 case V4L2_CID_BRIGHTNESS:
275 return v4l2_ctrl_query_fill(qc, ADV7343_BRIGHTNESS_MIN,
276 ADV7343_BRIGHTNESS_MAX, 1,
277 ADV7343_BRIGHTNESS_DEF);
279 return v4l2_ctrl_query_fill(qc, ADV7343_HUE_MIN,
283 return v4l2_ctrl_query_fill(qc, ADV7343_GAIN_MIN,
293 static int adv7343_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
295 struct adv7343_state *state = to_state(sd);
299 case V4L2_CID_BRIGHTNESS:
300 if (ctrl->value < ADV7343_BRIGHTNESS_MIN ||
301 ctrl->value > ADV7343_BRIGHTNESS_MAX) {
302 v4l2_dbg(1, debug, sd,
303 "invalid brightness settings %d\n",
308 state->bright = ctrl->value;
309 err = adv7343_write(sd, ADV7343_SD_BRIGHTNESS_WSS,
314 if (ctrl->value < ADV7343_HUE_MIN ||
315 ctrl->value > ADV7343_HUE_MAX) {
316 v4l2_dbg(1, debug, sd, "invalid hue settings %d\n",
321 state->hue = ctrl->value;
322 err = adv7343_write(sd, ADV7343_SD_HUE_REG, state->hue);
326 if (ctrl->value < ADV7343_GAIN_MIN ||
327 ctrl->value > ADV7343_GAIN_MAX) {
328 v4l2_dbg(1, debug, sd, "invalid gain settings %d\n",
333 if ((ctrl->value > POSITIVE_GAIN_MAX) &&
334 (ctrl->value < NEGATIVE_GAIN_MIN)) {
335 v4l2_dbg(1, debug, sd,
336 "gain settings not within the specified range\n");
340 state->gain = ctrl->value;
341 err = adv7343_write(sd, ADV7343_DAC2_OUTPUT_LEVEL, state->gain);
349 v4l2_err(sd, "Failed to set the encoder controls\n");
354 static int adv7343_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
356 struct adv7343_state *state = to_state(sd);
359 case V4L2_CID_BRIGHTNESS:
360 ctrl->value = state->bright;
364 ctrl->value = state->hue;
368 ctrl->value = state->gain;
378 static int adv7343_g_chip_ident(struct v4l2_subdev *sd,
379 struct v4l2_dbg_chip_ident *chip)
381 struct i2c_client *client = v4l2_get_subdevdata(sd);
383 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7343, 0);
386 static const struct v4l2_subdev_core_ops adv7343_core_ops = {
387 .log_status = adv7343_log_status,
388 .g_chip_ident = adv7343_g_chip_ident,
389 .g_ctrl = adv7343_g_ctrl,
390 .s_ctrl = adv7343_s_ctrl,
391 .queryctrl = adv7343_queryctrl,
394 static int adv7343_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
396 struct adv7343_state *state = to_state(sd);
399 if (state->std == std)
402 err = adv7343_setstd(sd, std);
409 static int adv7343_s_routing(struct v4l2_subdev *sd,
410 u32 input, u32 output, u32 config)
412 struct adv7343_state *state = to_state(sd);
415 if (state->output == output)
418 err = adv7343_setoutput(sd, output);
420 state->output = output;
425 static const struct v4l2_subdev_video_ops adv7343_video_ops = {
426 .s_std_output = adv7343_s_std_output,
427 .s_routing = adv7343_s_routing,
430 static const struct v4l2_subdev_ops adv7343_ops = {
431 .core = &adv7343_core_ops,
432 .video = &adv7343_video_ops,
435 static int adv7343_initialize(struct v4l2_subdev *sd)
437 struct adv7343_state *state = to_state(sd);
441 for (i = 0; i < ARRAY_SIZE(adv7343_init_reg_val); i += 2) {
443 err = adv7343_write(sd, adv7343_init_reg_val[i],
444 adv7343_init_reg_val[i+1]);
446 v4l2_err(sd, "Error initializing\n");
451 /* Configure for default video standard */
452 err = adv7343_setoutput(sd, state->output);
454 v4l2_err(sd, "Error setting output during init\n");
458 err = adv7343_setstd(sd, state->std);
460 v4l2_err(sd, "Error setting std during init\n");
467 static int adv7343_probe(struct i2c_client *client,
468 const struct i2c_device_id *id)
470 struct adv7343_state *state;
472 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
475 v4l_info(client, "chip found @ 0x%x (%s)\n",
476 client->addr << 1, client->adapter->name);
478 state = kzalloc(sizeof(struct adv7343_state), GFP_KERNEL);
486 state->reg80 = ADV7343_SD_MODE_REG1_DEFAULT;
487 state->reg82 = ADV7343_SD_MODE_REG2_DEFAULT;
489 state->output = ADV7343_COMPOSITE_ID;
490 state->std = V4L2_STD_NTSC;
492 v4l2_i2c_subdev_init(&state->sd, client, &adv7343_ops);
493 return adv7343_initialize(&state->sd);
496 static int adv7343_remove(struct i2c_client *client)
498 struct v4l2_subdev *sd = i2c_get_clientdata(client);
500 v4l2_device_unregister_subdev(sd);
506 static const struct i2c_device_id adv7343_id[] = {
511 MODULE_DEVICE_TABLE(i2c, adv7343_id);
513 static struct i2c_driver adv7343_driver = {
515 .owner = THIS_MODULE,
518 .probe = adv7343_probe,
519 .remove = adv7343_remove,
520 .id_table = adv7343_id,
523 static __init int init_adv7343(void)
525 return i2c_add_driver(&adv7343_driver);
528 static __exit void exit_adv7343(void)
530 i2c_del_driver(&adv7343_driver);
533 module_init(init_adv7343);
534 module_exit(exit_adv7343);