blob: 182da6ab3845305ab738bbeef1c6953e804027bc [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.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
31#include <linux/module.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030032#include <linux/types.h>
Hans Verkuila8c26df2008-09-07 07:59:35 -030033#include <linux/ioctl.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030034#include <asm/uaccess.h>
Hans Verkuila8c26df2008-09-07 07:59:35 -030035#include <linux/i2c.h>
36#include <linux/i2c-id.h>
Hans Verkuila91f56e2009-02-19 08:54:36 -030037#include <linux/videodev2.h>
38#include <media/v4l2-device.h>
39#include <media/v4l2-chip-ident.h>
Hans Verkuila8c26df2008-09-07 07:59:35 -030040#include <media/v4l2-i2c-drv-legacy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
43MODULE_AUTHOR("Mike Bernson & Dave Perks");
44MODULE_LICENSE("GPL");
45
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030046static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047module_param(debug, int, 0);
48MODULE_PARM_DESC(debug, "Debug level (0-1)");
49
Hans Verkuila91f56e2009-02-19 08:54:36 -030050static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
51
52I2C_CLIENT_INSMOD;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* ----------------------------------------------------------------------- */
55
Hans Verkuil187565c2008-08-30 06:03:09 -030056#define BT856_REG_OFFSET 0xDA
57#define BT856_NR_REG 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59struct bt856 {
Hans Verkuila91f56e2009-02-19 08:54:36 -030060 struct v4l2_subdev sd;
Jean Delvaref49a5ea2006-03-22 03:48:36 -030061 unsigned char reg[BT856_NR_REG];
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Hans Verkuil107063c2009-02-18 17:26:06 -030063 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Hans Verkuila91f56e2009-02-19 08:54:36 -030066static inline struct bt856 *to_bt856(struct v4l2_subdev *sd)
67{
68 return container_of(sd, struct bt856, sd);
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* ----------------------------------------------------------------------- */
72
Hans Verkuila91f56e2009-02-19 08:54:36 -030073static inline int bt856_write(struct bt856 *encoder, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Hans Verkuila91f56e2009-02-19 08:54:36 -030075 struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Hans Verkuil187565c2008-08-30 06:03:09 -030077 encoder->reg[reg - BT856_REG_OFFSET] = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return i2c_smbus_write_byte_data(client, reg, value);
79}
80
Hans Verkuila91f56e2009-02-19 08:54:36 -030081static inline int bt856_setbit(struct bt856 *encoder, u8 reg, u8 bit, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Hans Verkuila91f56e2009-02-19 08:54:36 -030083 return bt856_write(encoder, reg,
Hans Verkuila8c26df2008-09-07 07:59:35 -030084 (encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
85 (value ? (1 << bit) : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Hans Verkuila91f56e2009-02-19 08:54:36 -030088static void bt856_dump(struct bt856 *encoder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Hans Verkuila91f56e2009-02-19 08:54:36 -030092 v4l2_info(&encoder->sd, "register dump:\n");
Jean Delvaref49a5ea2006-03-22 03:48:36 -030093 for (i = 0; i < BT856_NR_REG; i += 2)
Hans Verkuila8c26df2008-09-07 07:59:35 -030094 printk(KERN_CONT " %02x", encoder->reg[i]);
95 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98/* ----------------------------------------------------------------------- */
99
Hans Verkuila91f56e2009-02-19 08:54:36 -0300100static int bt856_init(struct v4l2_subdev *sd, u32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Hans Verkuila91f56e2009-02-19 08:54:36 -0300102 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Hans Verkuila91f56e2009-02-19 08:54:36 -0300104 /* This is just for testing!!! */
105 v4l2_dbg(1, debug, sd, "init\n");
106 bt856_write(encoder, 0xdc, 0x18);
107 bt856_write(encoder, 0xda, 0);
108 bt856_write(encoder, 0xde, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Hans Verkuila91f56e2009-02-19 08:54:36 -0300110 bt856_setbit(encoder, 0xdc, 3, 1);
111 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
112 bt856_setbit(encoder, 0xdc, 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Hans Verkuila91f56e2009-02-19 08:54:36 -0300114 if (encoder->norm & V4L2_STD_NTSC)
115 bt856_setbit(encoder, 0xdc, 2, 0);
116 else
117 bt856_setbit(encoder, 0xdc, 2, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Hans Verkuila91f56e2009-02-19 08:54:36 -0300119 bt856_setbit(encoder, 0xdc, 1, 1);
120 bt856_setbit(encoder, 0xde, 4, 0);
121 bt856_setbit(encoder, 0xde, 3, 1);
122 if (debug != 0)
123 bt856_dump(encoder);
124 return 0;
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Hans Verkuila91f56e2009-02-19 08:54:36 -0300127static int bt856_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
128{
129 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Hans Verkuila91f56e2009-02-19 08:54:36 -0300131 v4l2_dbg(1, debug, sd, "set norm %llx\n", std);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Hans Verkuila91f56e2009-02-19 08:54:36 -0300133 if (std & V4L2_STD_NTSC) {
134 bt856_setbit(encoder, 0xdc, 2, 0);
135 } else if (std & V4L2_STD_PAL) {
136 bt856_setbit(encoder, 0xdc, 2, 1);
137 bt856_setbit(encoder, 0xda, 0, 0);
138 /*bt856_setbit(encoder, 0xda, 0, 1);*/
139 } else {
140 return -EINVAL;
Hans Verkuila8c26df2008-09-07 07:59:35 -0300141 }
Hans Verkuila91f56e2009-02-19 08:54:36 -0300142 encoder->norm = std;
143 if (debug != 0)
144 bt856_dump(encoder);
145 return 0;
146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Hans Verkuila91f56e2009-02-19 08:54:36 -0300148static int bt856_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
149{
150 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Hans Verkuila91f56e2009-02-19 08:54:36 -0300152 v4l2_dbg(1, debug, sd, "set input %d\n", route->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Hans Verkuila91f56e2009-02-19 08:54:36 -0300154 /* We only have video bus.
155 * route->input= 0: input is from bt819
156 * route->input= 1: input is from ZR36060 */
157 switch (route->input) {
158 case 0:
159 bt856_setbit(encoder, 0xde, 4, 0);
160 bt856_setbit(encoder, 0xde, 3, 1);
161 bt856_setbit(encoder, 0xdc, 3, 1);
162 bt856_setbit(encoder, 0xdc, 6, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 break;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300164 case 1:
165 bt856_setbit(encoder, 0xde, 4, 0);
166 bt856_setbit(encoder, 0xde, 3, 1);
167 bt856_setbit(encoder, 0xdc, 3, 1);
168 bt856_setbit(encoder, 0xdc, 6, 1);
169 break;
170 case 2: /* Color bar */
171 bt856_setbit(encoder, 0xdc, 3, 0);
172 bt856_setbit(encoder, 0xde, 4, 1);
173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 default:
175 return -EINVAL;
176 }
177
Hans Verkuila91f56e2009-02-19 08:54:36 -0300178 if (debug != 0)
179 bt856_dump(encoder);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
181}
182
Hans Verkuila91f56e2009-02-19 08:54:36 -0300183static int bt856_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
184{
185 struct i2c_client *client = v4l2_get_subdevdata(sd);
186
187 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_BT856, 0);
188}
189
190static int bt856_command(struct i2c_client *client, unsigned cmd, void *arg)
191{
192 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/* ----------------------------------------------------------------------- */
196
Hans Verkuila91f56e2009-02-19 08:54:36 -0300197static const struct v4l2_subdev_core_ops bt856_core_ops = {
198 .g_chip_ident = bt856_g_chip_ident,
199 .init = bt856_init,
200};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Hans Verkuila91f56e2009-02-19 08:54:36 -0300202static const struct v4l2_subdev_video_ops bt856_video_ops = {
203 .s_std_output = bt856_s_std_output,
204 .s_routing = bt856_s_routing,
205};
206
207static const struct v4l2_subdev_ops bt856_ops = {
208 .core = &bt856_core_ops,
209 .video = &bt856_video_ops,
210};
211
212/* ----------------------------------------------------------------------- */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300213
Hans Verkuila8c26df2008-09-07 07:59:35 -0300214static int bt856_probe(struct i2c_client *client,
215 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct bt856 *encoder;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300218 struct v4l2_subdev *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* Check if the adapter supports the needed features */
Hans Verkuila8c26df2008-09-07 07:59:35 -0300221 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
222 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Hans Verkuila8c26df2008-09-07 07:59:35 -0300224 v4l_info(client, "chip found @ 0x%x (%s)\n",
225 client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Panagiotis Issaris74081872006-01-11 19:40:56 -0200227 encoder = kzalloc(sizeof(struct bt856), GFP_KERNEL);
Hans Verkuila8c26df2008-09-07 07:59:35 -0300228 if (encoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -ENOMEM;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300230 sd = &encoder->sd;
231 v4l2_i2c_subdev_init(sd, client, &bt856_ops);
Hans Verkuil107063c2009-02-18 17:26:06 -0300232 encoder->norm = V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Hans Verkuila91f56e2009-02-19 08:54:36 -0300234 bt856_write(encoder, 0xdc, 0x18);
235 bt856_write(encoder, 0xda, 0);
236 bt856_write(encoder, 0xde, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Hans Verkuila91f56e2009-02-19 08:54:36 -0300238 bt856_setbit(encoder, 0xdc, 3, 1);
239 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
240 bt856_setbit(encoder, 0xdc, 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Hans Verkuil107063c2009-02-18 17:26:06 -0300242 if (encoder->norm & V4L2_STD_NTSC)
Hans Verkuila91f56e2009-02-19 08:54:36 -0300243 bt856_setbit(encoder, 0xdc, 2, 0);
Hans Verkuil107063c2009-02-18 17:26:06 -0300244 else
Hans Verkuila91f56e2009-02-19 08:54:36 -0300245 bt856_setbit(encoder, 0xdc, 2, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Hans Verkuila91f56e2009-02-19 08:54:36 -0300247 bt856_setbit(encoder, 0xdc, 1, 1);
248 bt856_setbit(encoder, 0xde, 4, 0);
249 bt856_setbit(encoder, 0xde, 3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (debug != 0)
Hans Verkuila91f56e2009-02-19 08:54:36 -0300252 bt856_dump(encoder);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254}
255
Hans Verkuila8c26df2008-09-07 07:59:35 -0300256static int bt856_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Hans Verkuila91f56e2009-02-19 08:54:36 -0300258 struct v4l2_subdev *sd = i2c_get_clientdata(client);
259
260 v4l2_device_unregister_subdev(sd);
261 kfree(to_bt856(sd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
263}
264
Hans Verkuila8c26df2008-09-07 07:59:35 -0300265static const struct i2c_device_id bt856_id[] = {
266 { "bt856", 0 },
267 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268};
Hans Verkuila8c26df2008-09-07 07:59:35 -0300269MODULE_DEVICE_TABLE(i2c, bt856_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Hans Verkuila8c26df2008-09-07 07:59:35 -0300271static struct v4l2_i2c_driver_data v4l2_i2c_data = {
272 .name = "bt856",
273 .driverid = I2C_DRIVERID_BT856,
274 .command = bt856_command,
275 .probe = bt856_probe,
276 .remove = bt856_remove,
277 .id_table = bt856_id,
278};