1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005-2006 Micronas USA Inc.
6 #include <linux/module.h>
7 #include <linux/init.h>
9 #include <linux/videodev2.h>
10 #include <linux/ioctl.h>
11 #include <media/v4l2-device.h>
12 #include <media/v4l2-ctrls.h>
13 #include <linux/slab.h>
15 MODULE_DESCRIPTION("TW9903 I2C subdev driver");
16 MODULE_LICENSE("GPL v2");
19 * This driver is based on the wis-tw9903.c source that was in
20 * drivers/staging/media/go7007. That source had commented out code for
21 * saturation and scaling (neither seemed to work). If anyone ever gets
22 * hardware to test this driver, then that code might be useful to look at.
23 * You need to get the kernel sources of, say, kernel 3.8 where that
24 * wis-tw9903 driver is still present.
28 struct v4l2_subdev sd
;
29 struct v4l2_ctrl_handler hdl
;
33 static inline struct tw9903
*to_state(struct v4l2_subdev
*sd
)
35 return container_of(sd
, struct tw9903
, sd
);
38 static const u8 initial_registers
[] = {
39 0x02, 0x44, /* input 1, composite */
40 0x03, 0x92, /* correct digital format */
42 0x05, 0x80, /* or 0x00 for PAL */
43 0x06, 0x40, /* second internal current reference */
44 0x07, 0x02, /* window */
45 0x08, 0x14, /* window */
46 0x09, 0xf0, /* window */
47 0x0a, 0x81, /* window */
48 0x0b, 0xd0, /* window */
50 0x0d, 0x00, /* scaling */
51 0x0e, 0x11, /* scaling */
52 0x0f, 0x00, /* scaling */
53 0x10, 0x00, /* brightness */
54 0x11, 0x60, /* contrast */
55 0x12, 0x01, /* sharpness */
56 0x13, 0x7f, /* U gain */
57 0x14, 0x5a, /* V gain */
59 0x16, 0xc3, /* sharpness */
63 0x1c, 0x0f, /* video norm */
64 0x1d, 0x7f, /* video norm */
65 0x20, 0xa0, /* clamping gain (working 0x50) */
79 0x2e, 0xa5, /* burst PLL control (working: a9) */
80 0x2f, 0xe0, /* 0xea is blue test frame -- 0xe0 for normal */
86 0x06, 0xc0, /* reset device */
87 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
90 static int write_reg(struct v4l2_subdev
*sd
, u8 reg
, u8 value
)
92 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
94 return i2c_smbus_write_byte_data(client
, reg
, value
);
97 static int write_regs(struct v4l2_subdev
*sd
, const u8
*regs
)
101 for (i
= 0; regs
[i
] != 0x00; i
+= 2)
102 if (write_reg(sd
, regs
[i
], regs
[i
+ 1]) < 0)
107 static int tw9903_s_video_routing(struct v4l2_subdev
*sd
, u32 input
,
108 u32 output
, u32 config
)
110 write_reg(sd
, 0x02, 0x40 | (input
<< 1));
114 static int tw9903_s_std(struct v4l2_subdev
*sd
, v4l2_std_id norm
)
116 struct tw9903
*dec
= to_state(sd
);
117 bool is_60hz
= norm
& V4L2_STD_525_60
;
118 static const u8 config_60hz
[] = {
125 static const u8 config_50hz
[] = {
133 write_regs(sd
, is_60hz
? config_60hz
: config_50hz
);
139 static int tw9903_s_ctrl(struct v4l2_ctrl
*ctrl
)
141 struct tw9903
*dec
= container_of(ctrl
->handler
, struct tw9903
, hdl
);
142 struct v4l2_subdev
*sd
= &dec
->sd
;
145 case V4L2_CID_BRIGHTNESS
:
146 write_reg(sd
, 0x10, ctrl
->val
);
148 case V4L2_CID_CONTRAST
:
149 write_reg(sd
, 0x11, ctrl
->val
);
152 write_reg(sd
, 0x15, ctrl
->val
);
160 static int tw9903_log_status(struct v4l2_subdev
*sd
)
162 struct tw9903
*dec
= to_state(sd
);
163 bool is_60hz
= dec
->norm
& V4L2_STD_525_60
;
165 v4l2_info(sd
, "Standard: %d Hz\n", is_60hz
? 60 : 50);
166 v4l2_ctrl_subdev_log_status(sd
);
170 /* --------------------------------------------------------------------------*/
172 static const struct v4l2_ctrl_ops tw9903_ctrl_ops
= {
173 .s_ctrl
= tw9903_s_ctrl
,
176 static const struct v4l2_subdev_core_ops tw9903_core_ops
= {
177 .log_status
= tw9903_log_status
,
180 static const struct v4l2_subdev_video_ops tw9903_video_ops
= {
181 .s_std
= tw9903_s_std
,
182 .s_routing
= tw9903_s_video_routing
,
185 static const struct v4l2_subdev_ops tw9903_ops
= {
186 .core
= &tw9903_core_ops
,
187 .video
= &tw9903_video_ops
,
190 /* --------------------------------------------------------------------------*/
192 static int tw9903_probe(struct i2c_client
*client
,
193 const struct i2c_device_id
*id
)
196 struct v4l2_subdev
*sd
;
197 struct v4l2_ctrl_handler
*hdl
;
199 /* Check if the adapter supports the needed features */
200 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
203 v4l_info(client
, "chip found @ 0x%02x (%s)\n",
204 client
->addr
<< 1, client
->adapter
->name
);
206 dec
= devm_kzalloc(&client
->dev
, sizeof(*dec
), GFP_KERNEL
);
210 v4l2_i2c_subdev_init(sd
, client
, &tw9903_ops
);
212 v4l2_ctrl_handler_init(hdl
, 4);
213 v4l2_ctrl_new_std(hdl
, &tw9903_ctrl_ops
,
214 V4L2_CID_BRIGHTNESS
, -128, 127, 1, 0);
215 v4l2_ctrl_new_std(hdl
, &tw9903_ctrl_ops
,
216 V4L2_CID_CONTRAST
, 0, 255, 1, 0x60);
217 v4l2_ctrl_new_std(hdl
, &tw9903_ctrl_ops
,
218 V4L2_CID_HUE
, -128, 127, 1, 0);
219 sd
->ctrl_handler
= hdl
;
221 int err
= hdl
->error
;
223 v4l2_ctrl_handler_free(hdl
);
227 /* Initialize tw9903 */
228 dec
->norm
= V4L2_STD_NTSC
;
230 if (write_regs(sd
, initial_registers
) < 0) {
231 v4l2_err(client
, "error initializing TW9903\n");
238 static int tw9903_remove(struct i2c_client
*client
)
240 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
242 v4l2_device_unregister_subdev(sd
);
243 v4l2_ctrl_handler_free(&to_state(sd
)->hdl
);
247 /* ----------------------------------------------------------------------- */
249 static const struct i2c_device_id tw9903_id
[] = {
253 MODULE_DEVICE_TABLE(i2c
, tw9903_id
);
255 static struct i2c_driver tw9903_driver
= {
259 .probe
= tw9903_probe
,
260 .remove
= tw9903_remove
,
261 .id_table
= tw9903_id
,
263 module_i2c_driver(tw9903_driver
);