2 * Copyright (C) 2005-2006 Micronas USA Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
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.
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 Foundation,
15 * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/i2c.h>
21 #include <linux/videodev2.h>
22 #include <linux/ioctl.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-ctrls.h>
25 #include <linux/slab.h>
27 MODULE_DESCRIPTION("TW9903 I2C subdev driver");
28 MODULE_LICENSE("GPL v2");
31 * This driver is based on the wis-tw9903.c source that was in
32 * drivers/staging/media/go7007. That source had commented out code for
33 * saturation and scaling (neither seemed to work). If anyone ever gets
34 * hardware to test this driver, then that code might be useful to look at.
35 * You need to get the kernel sources of, say, kernel 3.8 where that
36 * wis-tw9903 driver is still present.
40 struct v4l2_subdev sd
;
41 struct v4l2_ctrl_handler hdl
;
45 static inline struct tw9903
*to_state(struct v4l2_subdev
*sd
)
47 return container_of(sd
, struct tw9903
, sd
);
50 static const u8 initial_registers
[] = {
51 0x02, 0x44, /* input 1, composite */
52 0x03, 0x92, /* correct digital format */
54 0x05, 0x80, /* or 0x00 for PAL */
55 0x06, 0x40, /* second internal current reference */
56 0x07, 0x02, /* window */
57 0x08, 0x14, /* window */
58 0x09, 0xf0, /* window */
59 0x0a, 0x81, /* window */
60 0x0b, 0xd0, /* window */
62 0x0d, 0x00, /* scaling */
63 0x0e, 0x11, /* scaling */
64 0x0f, 0x00, /* scaling */
65 0x10, 0x00, /* brightness */
66 0x11, 0x60, /* contrast */
67 0x12, 0x01, /* sharpness */
68 0x13, 0x7f, /* U gain */
69 0x14, 0x5a, /* V gain */
71 0x16, 0xc3, /* sharpness */
75 0x1c, 0x0f, /* video norm */
76 0x1d, 0x7f, /* video norm */
77 0x20, 0xa0, /* clamping gain (working 0x50) */
91 0x2e, 0xa5, /* burst PLL control (working: a9) */
92 0x2f, 0xe0, /* 0xea is blue test frame -- 0xe0 for normal */
98 0x06, 0xc0, /* reset device */
99 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
102 static int write_reg(struct v4l2_subdev
*sd
, u8 reg
, u8 value
)
104 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
106 return i2c_smbus_write_byte_data(client
, reg
, value
);
109 static int write_regs(struct v4l2_subdev
*sd
, const u8
*regs
)
113 for (i
= 0; regs
[i
] != 0x00; i
+= 2)
114 if (write_reg(sd
, regs
[i
], regs
[i
+ 1]) < 0)
119 static int tw9903_s_video_routing(struct v4l2_subdev
*sd
, u32 input
,
120 u32 output
, u32 config
)
122 write_reg(sd
, 0x02, 0x40 | (input
<< 1));
126 static int tw9903_s_std(struct v4l2_subdev
*sd
, v4l2_std_id norm
)
128 struct tw9903
*dec
= to_state(sd
);
129 bool is_60hz
= norm
& V4L2_STD_525_60
;
130 static const u8 config_60hz
[] = {
137 static const u8 config_50hz
[] = {
145 write_regs(sd
, is_60hz
? config_60hz
: config_50hz
);
151 static int tw9903_s_ctrl(struct v4l2_ctrl
*ctrl
)
153 struct tw9903
*dec
= container_of(ctrl
->handler
, struct tw9903
, hdl
);
154 struct v4l2_subdev
*sd
= &dec
->sd
;
157 case V4L2_CID_BRIGHTNESS
:
158 write_reg(sd
, 0x10, ctrl
->val
);
160 case V4L2_CID_CONTRAST
:
161 write_reg(sd
, 0x11, ctrl
->val
);
164 write_reg(sd
, 0x15, ctrl
->val
);
172 static int tw9903_log_status(struct v4l2_subdev
*sd
)
174 struct tw9903
*dec
= to_state(sd
);
175 bool is_60hz
= dec
->norm
& V4L2_STD_525_60
;
177 v4l2_info(sd
, "Standard: %d Hz\n", is_60hz
? 60 : 50);
178 v4l2_ctrl_subdev_log_status(sd
);
182 /* --------------------------------------------------------------------------*/
184 static const struct v4l2_ctrl_ops tw9903_ctrl_ops
= {
185 .s_ctrl
= tw9903_s_ctrl
,
188 static const struct v4l2_subdev_core_ops tw9903_core_ops
= {
189 .log_status
= tw9903_log_status
,
192 static const struct v4l2_subdev_video_ops tw9903_video_ops
= {
193 .s_std
= tw9903_s_std
,
194 .s_routing
= tw9903_s_video_routing
,
197 static const struct v4l2_subdev_ops tw9903_ops
= {
198 .core
= &tw9903_core_ops
,
199 .video
= &tw9903_video_ops
,
202 /* --------------------------------------------------------------------------*/
204 static int tw9903_probe(struct i2c_client
*client
,
205 const struct i2c_device_id
*id
)
208 struct v4l2_subdev
*sd
;
209 struct v4l2_ctrl_handler
*hdl
;
211 /* Check if the adapter supports the needed features */
212 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
215 v4l_info(client
, "chip found @ 0x%02x (%s)\n",
216 client
->addr
<< 1, client
->adapter
->name
);
218 dec
= devm_kzalloc(&client
->dev
, sizeof(*dec
), GFP_KERNEL
);
222 v4l2_i2c_subdev_init(sd
, client
, &tw9903_ops
);
224 v4l2_ctrl_handler_init(hdl
, 4);
225 v4l2_ctrl_new_std(hdl
, &tw9903_ctrl_ops
,
226 V4L2_CID_BRIGHTNESS
, -128, 127, 1, 0);
227 v4l2_ctrl_new_std(hdl
, &tw9903_ctrl_ops
,
228 V4L2_CID_CONTRAST
, 0, 255, 1, 0x60);
229 v4l2_ctrl_new_std(hdl
, &tw9903_ctrl_ops
,
230 V4L2_CID_HUE
, -128, 127, 1, 0);
231 sd
->ctrl_handler
= hdl
;
233 int err
= hdl
->error
;
235 v4l2_ctrl_handler_free(hdl
);
239 /* Initialize tw9903 */
240 dec
->norm
= V4L2_STD_NTSC
;
242 if (write_regs(sd
, initial_registers
) < 0) {
243 v4l2_err(client
, "error initializing TW9903\n");
250 static int tw9903_remove(struct i2c_client
*client
)
252 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
254 v4l2_device_unregister_subdev(sd
);
255 v4l2_ctrl_handler_free(&to_state(sd
)->hdl
);
259 /* ----------------------------------------------------------------------- */
261 static const struct i2c_device_id tw9903_id
[] = {
265 MODULE_DEVICE_TABLE(i2c
, tw9903_id
);
267 static struct i2c_driver tw9903_driver
= {
269 .owner
= THIS_MODULE
,
272 .probe
= tw9903_probe
,
273 .remove
= tw9903_remove
,
274 .id_table
= tw9903_id
,
276 module_i2c_driver(tw9903_driver
);