blob: 2c039ae7d0b2446b00074fd1608d3c0368935693 [file] [log] [blame]
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * bt856 - BT856A Digital Video Encoder (Rockwell Part)
3 *
4 * Copyright (C) 1999 Mike Bernson <mike@mlb.org>
5 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
6 *
7 * Modifications for LML33/DC10plus unified driver
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 *
10 * This code was modify/ported from the saa7111 driver written
11 * by Dave Perks.
12 *
13 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
14 * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
27#include <linux/module.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030028#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Hans Verkuila8c26df2008-09-07 07:59:35 -030030#include <linux/ioctl.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080031#include <linux/uaccess.h>
Hans Verkuila8c26df2008-09-07 07:59:35 -030032#include <linux/i2c.h>
Hans Verkuila91f56e2009-02-19 08:54:36 -030033#include <linux/videodev2.h>
34#include <media/v4l2-device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
37MODULE_AUTHOR("Mike Bernson & Dave Perks");
38MODULE_LICENSE("GPL");
39
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030040static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041module_param(debug, int, 0);
42MODULE_PARM_DESC(debug, "Debug level (0-1)");
43
Hans Verkuila91f56e2009-02-19 08:54:36 -030044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* ----------------------------------------------------------------------- */
46
Hans Verkuil187565c2008-08-30 06:03:09 -030047#define BT856_REG_OFFSET 0xDA
48#define BT856_NR_REG 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50struct bt856 {
Hans Verkuila91f56e2009-02-19 08:54:36 -030051 struct v4l2_subdev sd;
Jean Delvaref49a5ea2006-03-22 03:48:36 -030052 unsigned char reg[BT856_NR_REG];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Hans Verkuil107063c2009-02-18 17:26:06 -030054 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
Hans Verkuila91f56e2009-02-19 08:54:36 -030057static inline struct bt856 *to_bt856(struct v4l2_subdev *sd)
58{
59 return container_of(sd, struct bt856, sd);
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* ----------------------------------------------------------------------- */
63
Hans Verkuila91f56e2009-02-19 08:54:36 -030064static inline int bt856_write(struct bt856 *encoder, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Hans Verkuila91f56e2009-02-19 08:54:36 -030066 struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Hans Verkuil187565c2008-08-30 06:03:09 -030068 encoder->reg[reg - BT856_REG_OFFSET] = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return i2c_smbus_write_byte_data(client, reg, value);
70}
71
Hans Verkuila91f56e2009-02-19 08:54:36 -030072static inline int bt856_setbit(struct bt856 *encoder, u8 reg, u8 bit, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Hans Verkuila91f56e2009-02-19 08:54:36 -030074 return bt856_write(encoder, reg,
Hans Verkuila8c26df2008-09-07 07:59:35 -030075 (encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
76 (value ? (1 << bit) : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Hans Verkuila91f56e2009-02-19 08:54:36 -030079static void bt856_dump(struct bt856 *encoder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Hans Verkuila91f56e2009-02-19 08:54:36 -030083 v4l2_info(&encoder->sd, "register dump:\n");
Jean Delvaref49a5ea2006-03-22 03:48:36 -030084 for (i = 0; i < BT856_NR_REG; i += 2)
Hans Verkuila8c26df2008-09-07 07:59:35 -030085 printk(KERN_CONT " %02x", encoder->reg[i]);
86 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/* ----------------------------------------------------------------------- */
90
Hans Verkuila91f56e2009-02-19 08:54:36 -030091static int bt856_init(struct v4l2_subdev *sd, u32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Hans Verkuila91f56e2009-02-19 08:54:36 -030093 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Hans Verkuila91f56e2009-02-19 08:54:36 -030095 /* This is just for testing!!! */
96 v4l2_dbg(1, debug, sd, "init\n");
97 bt856_write(encoder, 0xdc, 0x18);
98 bt856_write(encoder, 0xda, 0);
99 bt856_write(encoder, 0xde, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Hans Verkuila91f56e2009-02-19 08:54:36 -0300101 bt856_setbit(encoder, 0xdc, 3, 1);
102 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
103 bt856_setbit(encoder, 0xdc, 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Hans Verkuila91f56e2009-02-19 08:54:36 -0300105 if (encoder->norm & V4L2_STD_NTSC)
106 bt856_setbit(encoder, 0xdc, 2, 0);
107 else
108 bt856_setbit(encoder, 0xdc, 2, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Hans Verkuila91f56e2009-02-19 08:54:36 -0300110 bt856_setbit(encoder, 0xdc, 1, 1);
111 bt856_setbit(encoder, 0xde, 4, 0);
112 bt856_setbit(encoder, 0xde, 3, 1);
113 if (debug != 0)
114 bt856_dump(encoder);
115 return 0;
116}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Hans Verkuila91f56e2009-02-19 08:54:36 -0300118static int bt856_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
119{
120 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Hans Verkuilcc5cef82009-03-06 10:15:01 -0300122 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Hans Verkuila91f56e2009-02-19 08:54:36 -0300124 if (std & V4L2_STD_NTSC) {
125 bt856_setbit(encoder, 0xdc, 2, 0);
126 } else if (std & V4L2_STD_PAL) {
127 bt856_setbit(encoder, 0xdc, 2, 1);
128 bt856_setbit(encoder, 0xda, 0, 0);
129 /*bt856_setbit(encoder, 0xda, 0, 1);*/
130 } else {
131 return -EINVAL;
Hans Verkuila8c26df2008-09-07 07:59:35 -0300132 }
Hans Verkuila91f56e2009-02-19 08:54:36 -0300133 encoder->norm = std;
134 if (debug != 0)
135 bt856_dump(encoder);
136 return 0;
137}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Hans Verkuil5325b422009-04-02 11:26:22 -0300139static int bt856_s_routing(struct v4l2_subdev *sd,
140 u32 input, u32 output, u32 config)
Hans Verkuila91f56e2009-02-19 08:54:36 -0300141{
142 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Hans Verkuil5325b422009-04-02 11:26:22 -0300144 v4l2_dbg(1, debug, sd, "set input %d\n", input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Hans Verkuila91f56e2009-02-19 08:54:36 -0300146 /* We only have video bus.
Hans Verkuil5325b422009-04-02 11:26:22 -0300147 * input= 0: input is from bt819
148 * input= 1: input is from ZR36060 */
149 switch (input) {
Hans Verkuila91f56e2009-02-19 08:54:36 -0300150 case 0:
151 bt856_setbit(encoder, 0xde, 4, 0);
152 bt856_setbit(encoder, 0xde, 3, 1);
153 bt856_setbit(encoder, 0xdc, 3, 1);
154 bt856_setbit(encoder, 0xdc, 6, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 break;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300156 case 1:
157 bt856_setbit(encoder, 0xde, 4, 0);
158 bt856_setbit(encoder, 0xde, 3, 1);
159 bt856_setbit(encoder, 0xdc, 3, 1);
160 bt856_setbit(encoder, 0xdc, 6, 1);
161 break;
162 case 2: /* Color bar */
163 bt856_setbit(encoder, 0xdc, 3, 0);
164 bt856_setbit(encoder, 0xde, 4, 1);
165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 default:
167 return -EINVAL;
168 }
169
Hans Verkuila91f56e2009-02-19 08:54:36 -0300170 if (debug != 0)
171 bt856_dump(encoder);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return 0;
173}
174
175/* ----------------------------------------------------------------------- */
176
Hans Verkuila91f56e2009-02-19 08:54:36 -0300177static const struct v4l2_subdev_core_ops bt856_core_ops = {
Hans Verkuila91f56e2009-02-19 08:54:36 -0300178 .init = bt856_init,
179};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Hans Verkuila91f56e2009-02-19 08:54:36 -0300181static const struct v4l2_subdev_video_ops bt856_video_ops = {
182 .s_std_output = bt856_s_std_output,
183 .s_routing = bt856_s_routing,
184};
185
186static const struct v4l2_subdev_ops bt856_ops = {
187 .core = &bt856_core_ops,
188 .video = &bt856_video_ops,
189};
190
191/* ----------------------------------------------------------------------- */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300192
Hans Verkuila8c26df2008-09-07 07:59:35 -0300193static int bt856_probe(struct i2c_client *client,
194 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct bt856 *encoder;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300197 struct v4l2_subdev *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Check if the adapter supports the needed features */
Hans Verkuila8c26df2008-09-07 07:59:35 -0300200 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
201 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Hans Verkuila8c26df2008-09-07 07:59:35 -0300203 v4l_info(client, "chip found @ 0x%x (%s)\n",
204 client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300206 encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
Hans Verkuila8c26df2008-09-07 07:59:35 -0300207 if (encoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -ENOMEM;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300209 sd = &encoder->sd;
210 v4l2_i2c_subdev_init(sd, client, &bt856_ops);
Hans Verkuil107063c2009-02-18 17:26:06 -0300211 encoder->norm = V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Hans Verkuila91f56e2009-02-19 08:54:36 -0300213 bt856_write(encoder, 0xdc, 0x18);
214 bt856_write(encoder, 0xda, 0);
215 bt856_write(encoder, 0xde, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Hans Verkuila91f56e2009-02-19 08:54:36 -0300217 bt856_setbit(encoder, 0xdc, 3, 1);
218 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
219 bt856_setbit(encoder, 0xdc, 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Hans Verkuil107063c2009-02-18 17:26:06 -0300221 if (encoder->norm & V4L2_STD_NTSC)
Hans Verkuila91f56e2009-02-19 08:54:36 -0300222 bt856_setbit(encoder, 0xdc, 2, 0);
Hans Verkuil107063c2009-02-18 17:26:06 -0300223 else
Hans Verkuila91f56e2009-02-19 08:54:36 -0300224 bt856_setbit(encoder, 0xdc, 2, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Hans Verkuila91f56e2009-02-19 08:54:36 -0300226 bt856_setbit(encoder, 0xdc, 1, 1);
227 bt856_setbit(encoder, 0xde, 4, 0);
228 bt856_setbit(encoder, 0xde, 3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (debug != 0)
Hans Verkuila91f56e2009-02-19 08:54:36 -0300231 bt856_dump(encoder);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233}
234
Hans Verkuila8c26df2008-09-07 07:59:35 -0300235static int bt856_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Hans Verkuila91f56e2009-02-19 08:54:36 -0300237 struct v4l2_subdev *sd = i2c_get_clientdata(client);
238
239 v4l2_device_unregister_subdev(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return 0;
241}
242
Hans Verkuila8c26df2008-09-07 07:59:35 -0300243static const struct i2c_device_id bt856_id[] = {
244 { "bt856", 0 },
245 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
Hans Verkuila8c26df2008-09-07 07:59:35 -0300247MODULE_DEVICE_TABLE(i2c, bt856_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Hans Verkuil3f417202010-09-15 15:32:24 -0300249static struct i2c_driver bt856_driver = {
250 .driver = {
Hans Verkuil3f417202010-09-15 15:32:24 -0300251 .name = "bt856",
252 },
253 .probe = bt856_probe,
254 .remove = bt856_remove,
255 .id_table = bt856_id,
Hans Verkuila8c26df2008-09-07 07:59:35 -0300256};
Hans Verkuil3f417202010-09-15 15:32:24 -0300257
Axel Linc6e8d862012-02-12 06:56:32 -0300258module_i2c_driver(bt856_driver);