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 <linux/slab.h>
12 #include <media/v4l2-subdev.h>
13 #include <media/v4l2-device.h>
14 #include <media/v4l2-ctrls.h>
16 #define TW2804_REG_AUTOGAIN 0x02
17 #define TW2804_REG_HUE 0x0f
18 #define TW2804_REG_SATURATION 0x10
19 #define TW2804_REG_CONTRAST 0x11
20 #define TW2804_REG_BRIGHTNESS 0x12
21 #define TW2804_REG_COLOR_KILLER 0x14
22 #define TW2804_REG_GAIN 0x3c
23 #define TW2804_REG_CHROMA_GAIN 0x3d
24 #define TW2804_REG_BLUE_BALANCE 0x3e
25 #define TW2804_REG_RED_BALANCE 0x3f
28 struct v4l2_subdev sd
;
29 struct v4l2_ctrl_handler hdl
;
35 static const u8 global_registers
[] = {
44 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
47 static const u8 channel_registers
[] = {
103 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
106 static int write_reg(struct i2c_client
*client
, u8 reg
, u8 value
, u8 channel
)
108 return i2c_smbus_write_byte_data(client
, reg
| (channel
<< 6), value
);
111 static int write_regs(struct i2c_client
*client
, const u8
*regs
, u8 channel
)
116 for (i
= 0; regs
[i
] != 0xff; i
+= 2) {
117 ret
= i2c_smbus_write_byte_data(client
,
118 regs
[i
] | (channel
<< 6), regs
[i
+ 1]);
125 static int read_reg(struct i2c_client
*client
, u8 reg
, u8 channel
)
127 return i2c_smbus_read_byte_data(client
, (reg
) | (channel
<< 6));
130 static inline struct tw2804
*to_state(struct v4l2_subdev
*sd
)
132 return container_of(sd
, struct tw2804
, sd
);
135 static inline struct tw2804
*to_state_from_ctrl(struct v4l2_ctrl
*ctrl
)
137 return container_of(ctrl
->handler
, struct tw2804
, hdl
);
140 static int tw2804_log_status(struct v4l2_subdev
*sd
)
142 struct tw2804
*state
= to_state(sd
);
144 v4l2_info(sd
, "Standard: %s\n",
145 state
->norm
& V4L2_STD_525_60
? "60 Hz" : "50 Hz");
146 v4l2_info(sd
, "Channel: %d\n", state
->channel
);
147 v4l2_info(sd
, "Input: %d\n", state
->input
);
148 return v4l2_ctrl_subdev_log_status(sd
);
152 * These volatile controls are needed because all four channels share
153 * these controls. So a change made to them through one channel would
154 * require another channel to be updated.
156 * Normally this would have been done in a different way, but since the one
157 * board that uses this driver sees this single chip as if it was on four
158 * different i2c adapters (each adapter belonging to a separate instance of
159 * the same USB driver) there is no reliable method that I have found to let
160 * the instances know about each other.
162 * So implementing these global registers as volatile is the best we can do.
164 static int tw2804_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
166 struct tw2804
*state
= to_state_from_ctrl(ctrl
);
167 struct i2c_client
*client
= v4l2_get_subdevdata(&state
->sd
);
171 ctrl
->val
= read_reg(client
, TW2804_REG_GAIN
, 0);
174 case V4L2_CID_CHROMA_GAIN
:
175 ctrl
->val
= read_reg(client
, TW2804_REG_CHROMA_GAIN
, 0);
178 case V4L2_CID_BLUE_BALANCE
:
179 ctrl
->val
= read_reg(client
, TW2804_REG_BLUE_BALANCE
, 0);
182 case V4L2_CID_RED_BALANCE
:
183 ctrl
->val
= read_reg(client
, TW2804_REG_RED_BALANCE
, 0);
189 static int tw2804_s_ctrl(struct v4l2_ctrl
*ctrl
)
191 struct tw2804
*state
= to_state_from_ctrl(ctrl
);
192 struct i2c_client
*client
= v4l2_get_subdevdata(&state
->sd
);
197 case V4L2_CID_AUTOGAIN
:
198 addr
= TW2804_REG_AUTOGAIN
;
199 reg
= read_reg(client
, addr
, state
->channel
);
206 return write_reg(client
, addr
, reg
, state
->channel
);
208 case V4L2_CID_COLOR_KILLER
:
209 addr
= TW2804_REG_COLOR_KILLER
;
210 reg
= read_reg(client
, addr
, state
->channel
);
213 reg
= (reg
& ~(0x03)) | (ctrl
->val
== 0 ? 0x02 : 0x03);
214 return write_reg(client
, addr
, reg
, state
->channel
);
217 return write_reg(client
, TW2804_REG_GAIN
, ctrl
->val
, 0);
219 case V4L2_CID_CHROMA_GAIN
:
220 return write_reg(client
, TW2804_REG_CHROMA_GAIN
, ctrl
->val
, 0);
222 case V4L2_CID_BLUE_BALANCE
:
223 return write_reg(client
, TW2804_REG_BLUE_BALANCE
, ctrl
->val
, 0);
225 case V4L2_CID_RED_BALANCE
:
226 return write_reg(client
, TW2804_REG_RED_BALANCE
, ctrl
->val
, 0);
228 case V4L2_CID_BRIGHTNESS
:
229 return write_reg(client
, TW2804_REG_BRIGHTNESS
,
230 ctrl
->val
, state
->channel
);
232 case V4L2_CID_CONTRAST
:
233 return write_reg(client
, TW2804_REG_CONTRAST
,
234 ctrl
->val
, state
->channel
);
236 case V4L2_CID_SATURATION
:
237 return write_reg(client
, TW2804_REG_SATURATION
,
238 ctrl
->val
, state
->channel
);
241 return write_reg(client
, TW2804_REG_HUE
,
242 ctrl
->val
, state
->channel
);
250 static int tw2804_s_std(struct v4l2_subdev
*sd
, v4l2_std_id norm
)
252 struct tw2804
*dec
= to_state(sd
);
253 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
254 bool is_60hz
= norm
& V4L2_STD_525_60
;
256 0x01, is_60hz
? 0xc4 : 0x84,
257 0x09, is_60hz
? 0x07 : 0x04,
258 0x0a, is_60hz
? 0xf0 : 0x20,
259 0x0b, is_60hz
? 0x07 : 0x04,
260 0x0c, is_60hz
? 0xf0 : 0x20,
261 0x0d, is_60hz
? 0x40 : 0x4a,
262 0x16, is_60hz
? 0x00 : 0x40,
263 0x17, is_60hz
? 0x00 : 0x40,
264 0x20, is_60hz
? 0x07 : 0x0f,
265 0x21, is_60hz
? 0x07 : 0x0f,
269 write_regs(client
, regs
, dec
->channel
);
274 static int tw2804_s_video_routing(struct v4l2_subdev
*sd
, u32 input
, u32 output
,
277 struct tw2804
*dec
= to_state(sd
);
278 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
281 if (config
&& config
- 1 != dec
->channel
) {
283 dev_err(&client
->dev
,
284 "channel %d is not between 1 and 4!\n", config
);
287 dec
->channel
= config
- 1;
288 dev_dbg(&client
->dev
, "initializing TW2804 channel %d\n",
290 if (dec
->channel
== 0 &&
291 write_regs(client
, global_registers
, 0) < 0) {
292 dev_err(&client
->dev
,
293 "error initializing TW2804 global registers\n");
296 if (write_regs(client
, channel_registers
, dec
->channel
) < 0) {
297 dev_err(&client
->dev
,
298 "error initializing TW2804 channel %d\n",
307 if (input
== dec
->input
)
310 reg
= read_reg(client
, 0x22, dec
->channel
);
317 reg
= write_reg(client
, 0x22, reg
, dec
->channel
);
327 static const struct v4l2_ctrl_ops tw2804_ctrl_ops
= {
328 .g_volatile_ctrl
= tw2804_g_volatile_ctrl
,
329 .s_ctrl
= tw2804_s_ctrl
,
332 static const struct v4l2_subdev_video_ops tw2804_video_ops
= {
333 .s_std
= tw2804_s_std
,
334 .s_routing
= tw2804_s_video_routing
,
337 static const struct v4l2_subdev_core_ops tw2804_core_ops
= {
338 .log_status
= tw2804_log_status
,
341 static const struct v4l2_subdev_ops tw2804_ops
= {
342 .core
= &tw2804_core_ops
,
343 .video
= &tw2804_video_ops
,
346 static int tw2804_probe(struct i2c_client
*client
,
347 const struct i2c_device_id
*id
)
349 struct i2c_adapter
*adapter
= client
->adapter
;
350 struct tw2804
*state
;
351 struct v4l2_subdev
*sd
;
352 struct v4l2_ctrl
*ctrl
;
355 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
358 state
= devm_kzalloc(&client
->dev
, sizeof(*state
), GFP_KERNEL
);
362 v4l2_i2c_subdev_init(sd
, client
, &tw2804_ops
);
364 state
->norm
= V4L2_STD_NTSC
;
366 v4l2_ctrl_handler_init(&state
->hdl
, 10);
367 v4l2_ctrl_new_std(&state
->hdl
, &tw2804_ctrl_ops
,
368 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
369 v4l2_ctrl_new_std(&state
->hdl
, &tw2804_ctrl_ops
,
370 V4L2_CID_CONTRAST
, 0, 255, 1, 128);
371 v4l2_ctrl_new_std(&state
->hdl
, &tw2804_ctrl_ops
,
372 V4L2_CID_SATURATION
, 0, 255, 1, 128);
373 v4l2_ctrl_new_std(&state
->hdl
, &tw2804_ctrl_ops
,
374 V4L2_CID_HUE
, 0, 255, 1, 128);
375 v4l2_ctrl_new_std(&state
->hdl
, &tw2804_ctrl_ops
,
376 V4L2_CID_COLOR_KILLER
, 0, 1, 1, 0);
377 v4l2_ctrl_new_std(&state
->hdl
, &tw2804_ctrl_ops
,
378 V4L2_CID_AUTOGAIN
, 0, 1, 1, 0);
379 ctrl
= v4l2_ctrl_new_std(&state
->hdl
, &tw2804_ctrl_ops
,
380 V4L2_CID_GAIN
, 0, 255, 1, 128);
382 ctrl
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
383 ctrl
= v4l2_ctrl_new_std(&state
->hdl
, &tw2804_ctrl_ops
,
384 V4L2_CID_CHROMA_GAIN
, 0, 255, 1, 128);
386 ctrl
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
387 ctrl
= v4l2_ctrl_new_std(&state
->hdl
, &tw2804_ctrl_ops
,
388 V4L2_CID_BLUE_BALANCE
, 0, 255, 1, 122);
390 ctrl
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
391 ctrl
= v4l2_ctrl_new_std(&state
->hdl
, &tw2804_ctrl_ops
,
392 V4L2_CID_RED_BALANCE
, 0, 255, 1, 122);
394 ctrl
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
395 sd
->ctrl_handler
= &state
->hdl
;
396 err
= state
->hdl
.error
;
398 v4l2_ctrl_handler_free(&state
->hdl
);
402 v4l_info(client
, "chip found @ 0x%02x (%s)\n",
403 client
->addr
<< 1, client
->adapter
->name
);
408 static int tw2804_remove(struct i2c_client
*client
)
410 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
411 struct tw2804
*state
= to_state(sd
);
413 v4l2_device_unregister_subdev(sd
);
414 v4l2_ctrl_handler_free(&state
->hdl
);
418 static const struct i2c_device_id tw2804_id
[] = {
422 MODULE_DEVICE_TABLE(i2c
, tw2804_id
);
424 static struct i2c_driver tw2804_driver
= {
428 .probe
= tw2804_probe
,
429 .remove
= tw2804_remove
,
430 .id_table
= tw2804_id
,
433 module_i2c_driver(tw2804_driver
);
435 MODULE_LICENSE("GPL v2");
436 MODULE_DESCRIPTION("TW2804/TW2802 V4L2 i2c driver");
437 MODULE_AUTHOR("Micronas USA Inc");