blob: 73bc50c919d78649b96c08ad185fde27a9972187 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * bt819 - BT819A VideoStream Decoder (Rockwell Part)
4 *
5 * Copyright (C) 1999 Mike Bernson <mike@mlb.org>
6 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
7 *
8 * Modifications for LML33/DC10plus unified driver
9 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
12 * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
13 *
14 * This code was modify/ported from the saa7111 driver written
15 * by Dave Perks.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
18#include <linux/module.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030019#include <linux/types.h>
Hans Verkuil7fd011f2008-10-13 07:30:15 -030020#include <linux/ioctl.h>
Hans Verkuil28839132009-02-19 08:08:07 -030021#include <linux/delay.h>
Hans Verkuil7fd011f2008-10-13 07:30:15 -030022#include <linux/i2c.h>
Hans Verkuilc2179ad2009-02-19 06:36:36 -030023#include <linux/videodev2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Hans Verkuilc2179ad2009-02-19 06:36:36 -030025#include <media/v4l2-device.h>
Hans Verkuilbd84a652010-12-12 08:19:41 -030026#include <media/v4l2-ctrls.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020027#include <media/i2c/bt819.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29MODULE_DESCRIPTION("Brooktree-819 video decoder driver");
30MODULE_AUTHOR("Mike Bernson & Dave Perks");
31MODULE_LICENSE("GPL");
32
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030033static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034module_param(debug, int, 0);
35MODULE_PARM_DESC(debug, "Debug level (0-1)");
36
Hans Verkuilc2179ad2009-02-19 06:36:36 -030037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* ----------------------------------------------------------------------- */
39
40struct bt819 {
Hans Verkuilc2179ad2009-02-19 06:36:36 -030041 struct v4l2_subdev sd;
Hans Verkuilbd84a652010-12-12 08:19:41 -030042 struct v4l2_ctrl_handler hdl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned char reg[32];
44
Hans Verkuil107063c2009-02-18 17:26:06 -030045 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 int input;
47 int enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Hans Verkuilc2179ad2009-02-19 06:36:36 -030050static inline struct bt819 *to_bt819(struct v4l2_subdev *sd)
51{
52 return container_of(sd, struct bt819, sd);
53}
54
Hans Verkuilbd84a652010-12-12 08:19:41 -030055static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
56{
57 return &container_of(ctrl->handler, struct bt819, hdl)->sd;
58}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060struct timing {
61 int hactive;
62 int hdelay;
63 int vactive;
64 int vdelay;
65 int hscale;
66 int vscale;
67};
68
69/* for values, see the bt819 datasheet */
70static struct timing timing_data[] = {
71 {864 - 24, 20, 625 - 2, 1, 0x0504, 0x0000},
72 {858 - 24, 20, 525 - 2, 1, 0x00f8, 0x0000},
73};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* ----------------------------------------------------------------------- */
76
Hans Verkuilc2179ad2009-02-19 06:36:36 -030077static inline int bt819_write(struct bt819 *decoder, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Hans Verkuilc2179ad2009-02-19 06:36:36 -030079 struct i2c_client *client = v4l2_get_subdevdata(&decoder->sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 decoder->reg[reg] = value;
82 return i2c_smbus_write_byte_data(client, reg, value);
83}
84
Hans Verkuilc2179ad2009-02-19 06:36:36 -030085static inline int bt819_setbit(struct bt819 *decoder, u8 reg, u8 bit, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Hans Verkuilc2179ad2009-02-19 06:36:36 -030087 return bt819_write(decoder, reg,
Hans Verkuil7fd011f2008-10-13 07:30:15 -030088 (decoder->reg[reg] & ~(1 << bit)) | (value ? (1 << bit) : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Hans Verkuilc2179ad2009-02-19 06:36:36 -030091static int bt819_write_block(struct bt819 *decoder, const u8 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Hans Verkuilc2179ad2009-02-19 06:36:36 -030093 struct i2c_client *client = v4l2_get_subdevdata(&decoder->sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int ret = -1;
95 u8 reg;
96
97 /* the bt819 has an autoincrement function, use it if
98 * the adapter understands raw I2C */
99 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
100 /* do raw I2C, not smbus compatible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 u8 block_data[32];
Jean Delvare9aa45e32006-03-22 03:48:35 -0300102 int block_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 while (len >= 2) {
Jean Delvare9aa45e32006-03-22 03:48:35 -0300105 block_len = 0;
106 block_data[block_len++] = reg = data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 do {
Jean Delvare9aa45e32006-03-22 03:48:35 -0300108 block_data[block_len++] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 decoder->reg[reg++] = data[1];
110 len -= 2;
111 data += 2;
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300112 } while (len >= 2 && data[0] == reg && block_len < 32);
113 ret = i2c_master_send(client, block_data, block_len);
114 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 break;
116 }
117 } else {
118 /* do some slow I2C emulation kind of thing */
119 while (len >= 2) {
120 reg = *data++;
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300121 ret = bt819_write(decoder, reg, *data++);
122 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 break;
124 len -= 2;
125 }
126 }
127
128 return ret;
129}
130
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300131static inline int bt819_read(struct bt819 *decoder, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300133 struct i2c_client *client = v4l2_get_subdevdata(&decoder->sd);
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return i2c_smbus_read_byte_data(client, reg);
136}
137
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300138static int bt819_init(struct v4l2_subdev *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 static unsigned char init[] = {
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300141 /*0x1f, 0x00,*/ /* Reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 0x01, 0x59, /* 0x01 input format */
143 0x02, 0x00, /* 0x02 temporal decimation */
144 0x03, 0x12, /* 0x03 Cropping msb */
145 0x04, 0x16, /* 0x04 Vertical Delay, lsb */
146 0x05, 0xe0, /* 0x05 Vertical Active lsb */
147 0x06, 0x80, /* 0x06 Horizontal Delay lsb */
148 0x07, 0xd0, /* 0x07 Horizontal Active lsb */
149 0x08, 0x00, /* 0x08 Horizontal Scaling msb */
150 0x09, 0xf8, /* 0x09 Horizontal Scaling lsb */
151 0x0a, 0x00, /* 0x0a Brightness control */
152 0x0b, 0x30, /* 0x0b Miscellaneous control */
153 0x0c, 0xd8, /* 0x0c Luma Gain lsb */
154 0x0d, 0xfe, /* 0x0d Chroma Gain (U) lsb */
155 0x0e, 0xb4, /* 0x0e Chroma Gain (V) msb */
156 0x0f, 0x00, /* 0x0f Hue control */
157 0x12, 0x04, /* 0x12 Output Format */
Mauro Carvalho Chehabf8a76472019-02-18 14:28:58 -0500158 0x13, 0x20, /* 0x13 Vertical Scaling msb 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 chroma comb OFF, line drop scaling, interlace scaling
Andy Shevchenkofa7662a2019-10-24 09:23:04 -0300160 BUG? Why does turning the chroma comb on screw up color?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 Bug in the bt819 stepping on my board?
162 */
Mauro Carvalho Chehabf8a76472019-02-18 14:28:58 -0500163 0x14, 0x00, /* 0x14 Vertical Scaling lsb */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300164 0x16, 0x07, /* 0x16 Video Timing Polarity
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 ACTIVE=active low
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300166 FIELD: high=odd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 vreset=active high,
168 hreset=active high */
169 0x18, 0x68, /* 0x18 AGC Delay */
170 0x19, 0x5d, /* 0x19 Burst Gate Delay */
171 0x1a, 0x80, /* 0x1a ADC Interface */
172 };
173
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300174 struct bt819 *decoder = to_bt819(sd);
Hans Verkuil107063c2009-02-18 17:26:06 -0300175 struct timing *timing = &timing_data[(decoder->norm & V4L2_STD_525_60) ? 1 : 0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 init[0x03 * 2 - 1] =
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300178 (((timing->vdelay >> 8) & 0x03) << 6) |
179 (((timing->vactive >> 8) & 0x03) << 4) |
180 (((timing->hdelay >> 8) & 0x03) << 2) |
181 ((timing->hactive >> 8) & 0x03);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 init[0x04 * 2 - 1] = timing->vdelay & 0xff;
183 init[0x05 * 2 - 1] = timing->vactive & 0xff;
184 init[0x06 * 2 - 1] = timing->hdelay & 0xff;
185 init[0x07 * 2 - 1] = timing->hactive & 0xff;
186 init[0x08 * 2 - 1] = timing->hscale >> 8;
187 init[0x09 * 2 - 1] = timing->hscale & 0xff;
188 /* 0x15 in array is address 0x19 */
Hans Verkuil107063c2009-02-18 17:26:06 -0300189 init[0x15 * 2 - 1] = (decoder->norm & V4L2_STD_625_50) ? 115 : 93; /* Chroma burst delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* reset */
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300191 bt819_write(decoder, 0x1f, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 mdelay(1);
193
194 /* init */
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300195 return bt819_write_block(decoder, init, sizeof(init));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198/* ----------------------------------------------------------------------- */
199
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300200static int bt819_status(struct v4l2_subdev *sd, u32 *pstatus, v4l2_std_id *pstd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300202 struct bt819 *decoder = to_bt819(sd);
203 int status = bt819_read(decoder, 0x00);
204 int res = V4L2_IN_ST_NO_SIGNAL;
Hans Verkuilddc7f722013-05-29 10:18:55 -0300205 v4l2_std_id std = pstd ? *pstd : V4L2_STD_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300207 if ((status & 0x80))
208 res = 0;
Hans Verkuilddc7f722013-05-29 10:18:55 -0300209 else
210 std = V4L2_STD_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300212 if ((status & 0x10))
Hans Verkuilddc7f722013-05-29 10:18:55 -0300213 std &= V4L2_STD_PAL;
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300214 else
Hans Verkuilddc7f722013-05-29 10:18:55 -0300215 std &= V4L2_STD_NTSC;
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300216 if (pstd)
217 *pstd = std;
218 if (pstatus)
Hans Verkuilba088312011-08-25 10:52:53 -0300219 *pstatus = res;
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300220
221 v4l2_dbg(1, debug, sd, "get status %x\n", status);
222 return 0;
223}
224
225static int bt819_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
226{
227 return bt819_status(sd, NULL, std);
228}
229
230static int bt819_g_input_status(struct v4l2_subdev *sd, u32 *status)
231{
232 return bt819_status(sd, status, NULL);
233}
234
235static int bt819_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
236{
237 struct bt819 *decoder = to_bt819(sd);
238 struct timing *timing = NULL;
239
Hans Verkuilcc5cef82009-03-06 10:15:01 -0300240 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300241
Hans Verkuil1cd3c0f2009-03-08 17:04:38 -0300242 if (sd->v4l2_dev == NULL || sd->v4l2_dev->notify == NULL)
243 v4l2_err(sd, "no notify found!\n");
244
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300245 if (std & V4L2_STD_NTSC) {
Márton Némethb9fb9b72010-01-16 14:08:39 -0300246 v4l2_subdev_notify(sd, BT819_FIFO_RESET_LOW, NULL);
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300247 bt819_setbit(decoder, 0x01, 0, 1);
248 bt819_setbit(decoder, 0x01, 1, 0);
249 bt819_setbit(decoder, 0x01, 5, 0);
250 bt819_write(decoder, 0x18, 0x68);
251 bt819_write(decoder, 0x19, 0x5d);
252 /* bt819_setbit(decoder, 0x1a, 5, 1); */
253 timing = &timing_data[1];
254 } else if (std & V4L2_STD_PAL) {
Márton Némethb9fb9b72010-01-16 14:08:39 -0300255 v4l2_subdev_notify(sd, BT819_FIFO_RESET_LOW, NULL);
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300256 bt819_setbit(decoder, 0x01, 0, 1);
257 bt819_setbit(decoder, 0x01, 1, 1);
258 bt819_setbit(decoder, 0x01, 5, 1);
259 bt819_write(decoder, 0x18, 0x7f);
260 bt819_write(decoder, 0x19, 0x72);
261 /* bt819_setbit(decoder, 0x1a, 5, 0); */
262 timing = &timing_data[0];
263 } else {
Hans Verkuilcc5cef82009-03-06 10:15:01 -0300264 v4l2_dbg(1, debug, sd, "unsupported norm %llx\n",
265 (unsigned long long)std);
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300266 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300268 bt819_write(decoder, 0x03,
269 (((timing->vdelay >> 8) & 0x03) << 6) |
270 (((timing->vactive >> 8) & 0x03) << 4) |
271 (((timing->hdelay >> 8) & 0x03) << 2) |
272 ((timing->hactive >> 8) & 0x03));
273 bt819_write(decoder, 0x04, timing->vdelay & 0xff);
274 bt819_write(decoder, 0x05, timing->vactive & 0xff);
275 bt819_write(decoder, 0x06, timing->hdelay & 0xff);
276 bt819_write(decoder, 0x07, timing->hactive & 0xff);
277 bt819_write(decoder, 0x08, (timing->hscale >> 8) & 0xff);
278 bt819_write(decoder, 0x09, timing->hscale & 0xff);
279 decoder->norm = std;
Márton Némethb9fb9b72010-01-16 14:08:39 -0300280 v4l2_subdev_notify(sd, BT819_FIFO_RESET_HIGH, NULL);
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300281 return 0;
282}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Hans Verkuil5325b422009-04-02 11:26:22 -0300284static int bt819_s_routing(struct v4l2_subdev *sd,
285 u32 input, u32 output, u32 config)
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300286{
287 struct bt819 *decoder = to_bt819(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Hans Verkuil5325b422009-04-02 11:26:22 -0300289 v4l2_dbg(1, debug, sd, "set input %x\n", input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Roel Kluinf14a2972009-10-23 07:59:42 -0300291 if (input > 7)
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300292 return -EINVAL;
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300293
Hans Verkuil1cd3c0f2009-03-08 17:04:38 -0300294 if (sd->v4l2_dev == NULL || sd->v4l2_dev->notify == NULL)
295 v4l2_err(sd, "no notify found!\n");
296
Hans Verkuil5325b422009-04-02 11:26:22 -0300297 if (decoder->input != input) {
Márton Némethb9fb9b72010-01-16 14:08:39 -0300298 v4l2_subdev_notify(sd, BT819_FIFO_RESET_LOW, NULL);
Hans Verkuil5325b422009-04-02 11:26:22 -0300299 decoder->input = input;
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300300 /* select mode */
301 if (decoder->input == 0) {
302 bt819_setbit(decoder, 0x0b, 6, 0);
303 bt819_setbit(decoder, 0x1a, 1, 1);
Hans Verkuil107063c2009-02-18 17:26:06 -0300304 } else {
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300305 bt819_setbit(decoder, 0x0b, 6, 1);
306 bt819_setbit(decoder, 0x1a, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Márton Némethb9fb9b72010-01-16 14:08:39 -0300308 v4l2_subdev_notify(sd, BT819_FIFO_RESET_HIGH, NULL);
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300309 }
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300310 return 0;
311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300313static int bt819_s_stream(struct v4l2_subdev *sd, int enable)
314{
315 struct bt819 *decoder = to_bt819(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300317 v4l2_dbg(1, debug, sd, "enable output %x\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300319 if (decoder->enable != enable) {
320 decoder->enable = enable;
321 bt819_setbit(decoder, 0x16, 7, !enable);
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300322 }
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300323 return 0;
324}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Hans Verkuilbd84a652010-12-12 08:19:41 -0300326static int bt819_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300327{
Hans Verkuilbd84a652010-12-12 08:19:41 -0300328 struct v4l2_subdev *sd = to_sd(ctrl);
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300329 struct bt819 *decoder = to_bt819(sd);
330 int temp;
331
332 switch (ctrl->id) {
333 case V4L2_CID_BRIGHTNESS:
Hans Verkuilbd84a652010-12-12 08:19:41 -0300334 bt819_write(decoder, 0x0a, ctrl->val);
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300335 break;
336
337 case V4L2_CID_CONTRAST:
Hans Verkuilbd84a652010-12-12 08:19:41 -0300338 bt819_write(decoder, 0x0c, ctrl->val & 0xff);
339 bt819_setbit(decoder, 0x0b, 2, ((ctrl->val >> 8) & 0x01));
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300340 break;
341
342 case V4L2_CID_SATURATION:
Hans Verkuilbd84a652010-12-12 08:19:41 -0300343 bt819_write(decoder, 0x0d, (ctrl->val >> 7) & 0xff);
344 bt819_setbit(decoder, 0x0b, 1, ((ctrl->val >> 15) & 0x01));
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300345
346 /* Ratio between U gain and V gain must stay the same as
347 the ratio between the default U and V gain values. */
Hans Verkuilbd84a652010-12-12 08:19:41 -0300348 temp = (ctrl->val * 180) / 254;
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300349 bt819_write(decoder, 0x0e, (temp >> 7) & 0xff);
350 bt819_setbit(decoder, 0x0b, 0, (temp >> 15) & 0x01);
351 break;
352
353 case V4L2_CID_HUE:
Hans Verkuilbd84a652010-12-12 08:19:41 -0300354 bt819_write(decoder, 0x0f, ctrl->val);
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300355 break;
356
357 default:
358 return -EINVAL;
359 }
360 return 0;
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/* ----------------------------------------------------------------------- */
364
Hans Verkuilbd84a652010-12-12 08:19:41 -0300365static const struct v4l2_ctrl_ops bt819_ctrl_ops = {
366 .s_ctrl = bt819_s_ctrl,
367};
368
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300369static const struct v4l2_subdev_video_ops bt819_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300370 .s_std = bt819_s_std,
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300371 .s_routing = bt819_s_routing,
372 .s_stream = bt819_s_stream,
373 .querystd = bt819_querystd,
374 .g_input_status = bt819_g_input_status,
375};
376
377static const struct v4l2_subdev_ops bt819_ops = {
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300378 .video = &bt819_video_ops,
379};
380
381/* ----------------------------------------------------------------------- */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300382
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300383static int bt819_probe(struct i2c_client *client,
384 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300386 int i, ver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct bt819 *decoder;
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300388 struct v4l2_subdev *sd;
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300389 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /* Check if the adapter supports the needed features */
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300392 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
393 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300395 decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300396 if (decoder == NULL)
397 return -ENOMEM;
398 sd = &decoder->sd;
399 v4l2_i2c_subdev_init(sd, client, &bt819_ops);
400
401 ver = bt819_read(decoder, 0x17);
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300402 switch (ver & 0xf0) {
403 case 0x70:
404 name = "bt819a";
405 break;
406 case 0x60:
407 name = "bt817a";
408 break;
409 case 0x20:
410 name = "bt815a";
411 break;
412 default:
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300413 v4l2_dbg(1, debug, sd,
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300414 "unknown chip version 0x%02x\n", ver);
415 return -ENODEV;
416 }
417
418 v4l_info(client, "%s found @ 0x%x (%s)\n", name,
419 client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Hans Verkuil107063c2009-02-18 17:26:06 -0300421 decoder->norm = V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 decoder->input = 0;
423 decoder->enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300425 i = bt819_init(sd);
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300426 if (i < 0)
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300427 v4l2_dbg(1, debug, sd, "init status %d\n", i);
Hans Verkuilbd84a652010-12-12 08:19:41 -0300428
429 v4l2_ctrl_handler_init(&decoder->hdl, 4);
430 v4l2_ctrl_new_std(&decoder->hdl, &bt819_ctrl_ops,
431 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
432 v4l2_ctrl_new_std(&decoder->hdl, &bt819_ctrl_ops,
433 V4L2_CID_CONTRAST, 0, 511, 1, 0xd8);
434 v4l2_ctrl_new_std(&decoder->hdl, &bt819_ctrl_ops,
435 V4L2_CID_SATURATION, 0, 511, 1, 0xfe);
436 v4l2_ctrl_new_std(&decoder->hdl, &bt819_ctrl_ops,
437 V4L2_CID_HUE, -128, 127, 1, 0);
438 sd->ctrl_handler = &decoder->hdl;
439 if (decoder->hdl.error) {
440 int err = decoder->hdl.error;
441
442 v4l2_ctrl_handler_free(&decoder->hdl);
Hans Verkuilbd84a652010-12-12 08:19:41 -0300443 return err;
444 }
445 v4l2_ctrl_handler_setup(&decoder->hdl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
447}
448
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300449static int bt819_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300451 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Hans Verkuilbd84a652010-12-12 08:19:41 -0300452 struct bt819 *decoder = to_bt819(sd);
Hans Verkuilc2179ad2009-02-19 06:36:36 -0300453
454 v4l2_device_unregister_subdev(sd);
Hans Verkuilbd84a652010-12-12 08:19:41 -0300455 v4l2_ctrl_handler_free(&decoder->hdl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
457}
458
459/* ----------------------------------------------------------------------- */
460
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300461static const struct i2c_device_id bt819_id[] = {
462 { "bt819a", 0 },
463 { "bt817a", 0 },
464 { "bt815a", 0 },
465 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466};
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300467MODULE_DEVICE_TABLE(i2c, bt819_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Hans Verkuil00cc8db2010-09-15 15:15:55 -0300469static struct i2c_driver bt819_driver = {
470 .driver = {
Hans Verkuil00cc8db2010-09-15 15:15:55 -0300471 .name = "bt819",
472 },
473 .probe = bt819_probe,
474 .remove = bt819_remove,
475 .id_table = bt819_id,
Hans Verkuil7fd011f2008-10-13 07:30:15 -0300476};
Hans Verkuil00cc8db2010-09-15 15:15:55 -0300477
Axel Linc6e8d862012-02-12 06:56:32 -0300478module_i2c_driver(bt819_driver);