1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * upd64031A - NEC Electronics Ghost Reduction for NTSC in Japan
5 * 2003 by T.Adachi <tadachi@tadachi-net.com>
6 * 2003 by Takeru KOMORIYA <komoriya@paken.org>
7 * 2006 by Hans Verkuil <hverkuil@xs4all.nl>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/i2c.h>
14 #include <linux/videodev2.h>
15 #include <linux/slab.h>
16 #include <media/v4l2-device.h>
17 #include <media/i2c/upd64031a.h>
19 /* --------------------- read registers functions define -------------------- */
22 #define GR_MODE_MASK 0xc0
23 #define DIRECT_3DYCS_CONNECT_MASK 0xc0
24 #define SYNC_CIRCUIT_MASK 0xa0
26 /* -------------------------------------------------------------------------- */
28 MODULE_DESCRIPTION("uPD64031A driver");
29 MODULE_AUTHOR("T. Adachi, Takeru KOMORIYA, Hans Verkuil");
30 MODULE_LICENSE("GPL");
33 module_param(debug
, int, 0644);
35 MODULE_PARM_DESC(debug
, "Debug level (0-1)");
39 R00
= 0, R01
, R02
, R03
, R04
,
40 R05
, R06
, R07
, R08
, R09
,
41 R0A
, R0B
, R0C
, R0D
, R0E
, R0F
,
43 R10, R11, R12, R13, R14,
49 struct upd64031a_state
{
50 struct v4l2_subdev sd
;
53 u8 direct_3dycs_connect
;
58 static inline struct upd64031a_state
*to_state(struct v4l2_subdev
*sd
)
60 return container_of(sd
, struct upd64031a_state
, sd
);
63 static u8 upd64031a_init
[] = {
64 0x00, 0xb8, 0x48, 0xd2, 0xe6,
65 0x03, 0x10, 0x0b, 0xaf, 0x7f,
66 0x00, 0x00, 0x1d, 0x5e, 0x00,
70 /* ------------------------------------------------------------------------ */
72 static u8
upd64031a_read(struct v4l2_subdev
*sd
, u8 reg
)
74 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
77 if (reg
>= sizeof(buf
))
79 i2c_master_recv(client
, buf
, 2);
83 /* ------------------------------------------------------------------------ */
85 static void upd64031a_write(struct v4l2_subdev
*sd
, u8 reg
, u8 val
)
87 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
92 v4l2_dbg(1, debug
, sd
, "write reg: %02X val: %02X\n", reg
, val
);
93 if (i2c_master_send(client
, buf
, 2) != 2)
94 v4l2_err(sd
, "I/O error write 0x%02x/0x%02x\n", reg
, val
);
97 /* ------------------------------------------------------------------------ */
99 /* The input changed due to new input or channel changed */
100 static int upd64031a_s_frequency(struct v4l2_subdev
*sd
, const struct v4l2_frequency
*freq
)
102 struct upd64031a_state
*state
= to_state(sd
);
103 u8 reg
= state
->regs
[R00
];
105 v4l2_dbg(1, debug
, sd
, "changed input or channel\n");
106 upd64031a_write(sd
, R00
, reg
| 0x10);
107 upd64031a_write(sd
, R00
, reg
& ~0x10);
111 /* ------------------------------------------------------------------------ */
113 static int upd64031a_s_routing(struct v4l2_subdev
*sd
,
114 u32 input
, u32 output
, u32 config
)
116 struct upd64031a_state
*state
= to_state(sd
);
119 state
->gr_mode
= (input
& 3) << 6;
120 state
->direct_3dycs_connect
= (input
& 0xc) << 4;
121 state
->ext_comp_sync
=
122 (input
& UPD64031A_COMPOSITE_EXTERNAL
) << 1;
123 state
->ext_vert_sync
=
124 (input
& UPD64031A_VERTICAL_EXTERNAL
) << 2;
125 r00
= (state
->regs
[R00
] & ~GR_MODE_MASK
) | state
->gr_mode
;
126 r05
= (state
->regs
[R00
] & ~SYNC_CIRCUIT_MASK
) |
127 state
->ext_comp_sync
| state
->ext_vert_sync
;
128 r08
= (state
->regs
[R08
] & ~DIRECT_3DYCS_CONNECT_MASK
) |
129 state
->direct_3dycs_connect
;
130 upd64031a_write(sd
, R00
, r00
);
131 upd64031a_write(sd
, R05
, r05
);
132 upd64031a_write(sd
, R08
, r08
);
133 return upd64031a_s_frequency(sd
, NULL
);
136 static int upd64031a_log_status(struct v4l2_subdev
*sd
)
138 v4l2_info(sd
, "Status: SA00=0x%02x SA01=0x%02x\n",
139 upd64031a_read(sd
, 0), upd64031a_read(sd
, 1));
143 #ifdef CONFIG_VIDEO_ADV_DEBUG
144 static int upd64031a_g_register(struct v4l2_subdev
*sd
, struct v4l2_dbg_register
*reg
)
146 reg
->val
= upd64031a_read(sd
, reg
->reg
& 0xff);
151 static int upd64031a_s_register(struct v4l2_subdev
*sd
, const struct v4l2_dbg_register
*reg
)
153 upd64031a_write(sd
, reg
->reg
& 0xff, reg
->val
& 0xff);
158 /* ----------------------------------------------------------------------- */
160 static const struct v4l2_subdev_core_ops upd64031a_core_ops
= {
161 .log_status
= upd64031a_log_status
,
162 #ifdef CONFIG_VIDEO_ADV_DEBUG
163 .g_register
= upd64031a_g_register
,
164 .s_register
= upd64031a_s_register
,
168 static const struct v4l2_subdev_tuner_ops upd64031a_tuner_ops
= {
169 .s_frequency
= upd64031a_s_frequency
,
172 static const struct v4l2_subdev_video_ops upd64031a_video_ops
= {
173 .s_routing
= upd64031a_s_routing
,
176 static const struct v4l2_subdev_ops upd64031a_ops
= {
177 .core
= &upd64031a_core_ops
,
178 .tuner
= &upd64031a_tuner_ops
,
179 .video
= &upd64031a_video_ops
,
182 /* ------------------------------------------------------------------------ */
184 /* i2c implementation */
186 static int upd64031a_probe(struct i2c_client
*client
,
187 const struct i2c_device_id
*id
)
189 struct upd64031a_state
*state
;
190 struct v4l2_subdev
*sd
;
193 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
196 v4l_info(client
, "chip found @ 0x%x (%s)\n",
197 client
->addr
<< 1, client
->adapter
->name
);
199 state
= devm_kzalloc(&client
->dev
, sizeof(*state
), GFP_KERNEL
);
203 v4l2_i2c_subdev_init(sd
, client
, &upd64031a_ops
);
204 memcpy(state
->regs
, upd64031a_init
, sizeof(state
->regs
));
205 state
->gr_mode
= UPD64031A_GR_ON
<< 6;
206 state
->direct_3dycs_connect
= UPD64031A_3DYCS_COMPOSITE
<< 4;
207 state
->ext_comp_sync
= state
->ext_vert_sync
= 0;
208 for (i
= 0; i
< TOT_REGS
; i
++)
209 upd64031a_write(sd
, i
, state
->regs
[i
]);
213 static int upd64031a_remove(struct i2c_client
*client
)
215 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
217 v4l2_device_unregister_subdev(sd
);
221 /* ----------------------------------------------------------------------- */
223 static const struct i2c_device_id upd64031a_id
[] = {
227 MODULE_DEVICE_TABLE(i2c
, upd64031a_id
);
229 static struct i2c_driver upd64031a_driver
= {
233 .probe
= upd64031a_probe
,
234 .remove
= upd64031a_remove
,
235 .id_table
= upd64031a_id
,
238 module_i2c_driver(upd64031a_driver
);