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 <linux/slab.h>
24 #include <media/v4l2-subdev.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-chip-ident.h>
27 #include <media/v4l2-ctrls.h>
29 #define TW2804_REG_AUTOGAIN 0x02
30 #define TW2804_REG_HUE 0x0f
31 #define TW2804_REG_SATURATION 0x10
32 #define TW2804_REG_CONTRAST 0x11
33 #define TW2804_REG_BRIGHTNESS 0x12
34 #define TW2804_REG_COLOR_KILLER 0x14
35 #define TW2804_REG_GAIN 0x3c
36 #define TW2804_REG_CHROMA_GAIN 0x3d
37 #define TW2804_REG_BLUE_BALANCE 0x3e
38 #define TW2804_REG_RED_BALANCE 0x3f
41 struct v4l2_subdev sd;
42 struct v4l2_ctrl_handler hdl;
48 static const u8 global_registers[] = {
57 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
60 static const u8 channel_registers[] = {
116 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
119 static int write_reg(struct i2c_client *client, u8 reg, u8 value, u8 channel)
121 return i2c_smbus_write_byte_data(client, reg | (channel << 6), value);
124 static int write_regs(struct i2c_client *client, const u8 *regs, u8 channel)
129 for (i = 0; regs[i] != 0xff; i += 2) {
130 ret = i2c_smbus_write_byte_data(client,
131 regs[i] | (channel << 6), regs[i + 1]);
138 static int read_reg(struct i2c_client *client, u8 reg, u8 channel)
140 return i2c_smbus_read_byte_data(client, (reg) | (channel << 6));
143 static inline struct tw2804 *to_state(struct v4l2_subdev *sd)
145 return container_of(sd, struct tw2804, sd);
148 static inline struct tw2804 *to_state_from_ctrl(struct v4l2_ctrl *ctrl)
150 return container_of(ctrl->handler, struct tw2804, hdl);
153 static int tw2804_log_status(struct v4l2_subdev *sd)
155 struct tw2804 *state = to_state(sd);
157 v4l2_info(sd, "Standard: %s\n",
158 state->norm & V4L2_STD_525_60 ? "60 Hz" : "50 Hz");
159 v4l2_info(sd, "Channel: %d\n", state->channel);
160 v4l2_info(sd, "Input: %d\n", state->input);
161 return v4l2_ctrl_subdev_log_status(sd);
165 * These volatile controls are needed because all four channels share
166 * these controls. So a change made to them through one channel would
167 * require another channel to be updated.
169 * Normally this would have been done in a different way, but since the one
170 * board that uses this driver sees this single chip as if it was on four
171 * different i2c adapters (each adapter belonging to a separate instance of
172 * the same USB driver) there is no reliable method that I have found to let
173 * the instances know about each other.
175 * So implementing these global registers as volatile is the best we can do.
177 static int tw2804_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
179 struct tw2804 *state = to_state_from_ctrl(ctrl);
180 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
184 ctrl->val = read_reg(client, TW2804_REG_GAIN, 0);
187 case V4L2_CID_CHROMA_GAIN:
188 ctrl->val = read_reg(client, TW2804_REG_CHROMA_GAIN, 0);
191 case V4L2_CID_BLUE_BALANCE:
192 ctrl->val = read_reg(client, TW2804_REG_BLUE_BALANCE, 0);
195 case V4L2_CID_RED_BALANCE:
196 ctrl->val = read_reg(client, TW2804_REG_RED_BALANCE, 0);
202 static int tw2804_s_ctrl(struct v4l2_ctrl *ctrl)
204 struct tw2804 *state = to_state_from_ctrl(ctrl);
205 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
210 case V4L2_CID_AUTOGAIN:
211 addr = TW2804_REG_AUTOGAIN;
212 reg = read_reg(client, addr, state->channel);
219 return write_reg(client, addr, reg, state->channel);
221 case V4L2_CID_COLOR_KILLER:
222 addr = TW2804_REG_COLOR_KILLER;
223 reg = read_reg(client, addr, state->channel);
226 reg = (reg & ~(0x03)) | (ctrl->val == 0 ? 0x02 : 0x03);
227 return write_reg(client, addr, reg, state->channel);
230 return write_reg(client, TW2804_REG_GAIN, ctrl->val, 0);
232 case V4L2_CID_CHROMA_GAIN:
233 return write_reg(client, TW2804_REG_CHROMA_GAIN, ctrl->val, 0);
235 case V4L2_CID_BLUE_BALANCE:
236 return write_reg(client, TW2804_REG_BLUE_BALANCE, ctrl->val, 0);
238 case V4L2_CID_RED_BALANCE:
239 return write_reg(client, TW2804_REG_RED_BALANCE, ctrl->val, 0);
241 case V4L2_CID_BRIGHTNESS:
242 return write_reg(client, TW2804_REG_BRIGHTNESS,
243 ctrl->val, state->channel);
245 case V4L2_CID_CONTRAST:
246 return write_reg(client, TW2804_REG_CONTRAST,
247 ctrl->val, state->channel);
249 case V4L2_CID_SATURATION:
250 return write_reg(client, TW2804_REG_SATURATION,
251 ctrl->val, state->channel);
254 return write_reg(client, TW2804_REG_HUE,
255 ctrl->val, state->channel);
263 static int tw2804_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
265 struct tw2804 *dec = to_state(sd);
266 struct i2c_client *client = v4l2_get_subdevdata(sd);
267 bool is_60hz = norm & V4L2_STD_525_60;
269 0x01, is_60hz ? 0xc4 : 0x84,
270 0x09, is_60hz ? 0x07 : 0x04,
271 0x0a, is_60hz ? 0xf0 : 0x20,
272 0x0b, is_60hz ? 0x07 : 0x04,
273 0x0c, is_60hz ? 0xf0 : 0x20,
274 0x0d, is_60hz ? 0x40 : 0x4a,
275 0x16, is_60hz ? 0x00 : 0x40,
276 0x17, is_60hz ? 0x00 : 0x40,
277 0x20, is_60hz ? 0x07 : 0x0f,
278 0x21, is_60hz ? 0x07 : 0x0f,
282 write_regs(client, regs, dec->channel);
287 static int tw2804_s_video_routing(struct v4l2_subdev *sd, u32 input, u32 output,
290 struct tw2804 *dec = to_state(sd);
291 struct i2c_client *client = v4l2_get_subdevdata(sd);
294 if (config && config - 1 != dec->channel) {
296 dev_err(&client->dev,
297 "channel %d is not between 1 and 4!\n", config);
300 dec->channel = config - 1;
301 dev_dbg(&client->dev, "initializing TW2804 channel %d\n",
303 if (dec->channel == 0 &&
304 write_regs(client, global_registers, 0) < 0) {
305 dev_err(&client->dev,
306 "error initializing TW2804 global registers\n");
309 if (write_regs(client, channel_registers, dec->channel) < 0) {
310 dev_err(&client->dev,
311 "error initializing TW2804 channel %d\n",
320 if (input == dec->input)
323 reg = read_reg(client, 0x22, dec->channel);
330 reg = write_reg(client, 0x22, reg, dec->channel);
340 static const struct v4l2_ctrl_ops tw2804_ctrl_ops = {
341 .g_volatile_ctrl = tw2804_g_volatile_ctrl,
342 .s_ctrl = tw2804_s_ctrl,
345 static const struct v4l2_subdev_video_ops tw2804_video_ops = {
346 .s_routing = tw2804_s_video_routing,
349 static const struct v4l2_subdev_core_ops tw2804_core_ops = {
350 .log_status = tw2804_log_status,
351 .s_std = tw2804_s_std,
354 static const struct v4l2_subdev_ops tw2804_ops = {
355 .core = &tw2804_core_ops,
356 .video = &tw2804_video_ops,
359 static int tw2804_probe(struct i2c_client *client,
360 const struct i2c_device_id *id)
362 struct i2c_adapter *adapter = client->adapter;
363 struct tw2804 *state;
364 struct v4l2_subdev *sd;
365 struct v4l2_ctrl *ctrl;
368 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
371 state = kzalloc(sizeof(struct tw2804), GFP_KERNEL);
376 v4l2_i2c_subdev_init(sd, client, &tw2804_ops);
378 state->norm = V4L2_STD_NTSC;
380 v4l2_ctrl_handler_init(&state->hdl, 10);
381 v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
382 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
383 v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
384 V4L2_CID_CONTRAST, 0, 255, 1, 128);
385 v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
386 V4L2_CID_SATURATION, 0, 255, 1, 128);
387 v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
388 V4L2_CID_HUE, 0, 255, 1, 128);
389 v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
390 V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
391 v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
392 V4L2_CID_AUTOGAIN, 0, 1, 1, 0);
393 ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
394 V4L2_CID_GAIN, 0, 255, 1, 128);
396 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
397 ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
398 V4L2_CID_CHROMA_GAIN, 0, 255, 1, 128);
400 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
401 ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
402 V4L2_CID_BLUE_BALANCE, 0, 255, 1, 122);
404 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
405 ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
406 V4L2_CID_RED_BALANCE, 0, 255, 1, 122);
408 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
409 sd->ctrl_handler = &state->hdl;
410 err = state->hdl.error;
412 v4l2_ctrl_handler_free(&state->hdl);
417 v4l_info(client, "chip found @ 0x%02x (%s)\n",
418 client->addr << 1, client->adapter->name);
423 static int tw2804_remove(struct i2c_client *client)
425 struct v4l2_subdev *sd = i2c_get_clientdata(client);
426 struct tw2804 *state = to_state(sd);
428 v4l2_device_unregister_subdev(sd);
429 v4l2_ctrl_handler_free(&state->hdl);
434 static const struct i2c_device_id tw2804_id[] = {
438 MODULE_DEVICE_TABLE(i2c, tw2804_id);
440 static struct i2c_driver tw2804_driver = {
444 .probe = tw2804_probe,
445 .remove = tw2804_remove,
446 .id_table = tw2804_id,
449 module_i2c_driver(tw2804_driver);
451 MODULE_LICENSE("GPL v2");
452 MODULE_DESCRIPTION("TW2804/TW2802 V4L2 i2c driver");
453 MODULE_AUTHOR("Micronas USA Inc");