blob: 7fc163d0253cb46592ed24b2409a5847524fe8e0 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Hans Verkuila8c26df2008-09-07 07:59:35 -030034#include <linux/ioctl.h>
Mauro Carvalho Chehab18f3fa12007-07-02 15:39:57 -030035#include <asm/uaccess.h>
Hans Verkuila8c26df2008-09-07 07:59:35 -030036#include <linux/i2c.h>
Hans Verkuila91f56e2009-02-19 08:54:36 -030037#include <linux/videodev2.h>
38#include <media/v4l2-device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
41MODULE_AUTHOR("Mike Bernson & Dave Perks");
42MODULE_LICENSE("GPL");
43
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030044static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045module_param(debug, int, 0);
46MODULE_PARM_DESC(debug, "Debug level (0-1)");
47
Hans Verkuila91f56e2009-02-19 08:54:36 -030048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* ----------------------------------------------------------------------- */
50
Hans Verkuil187565c2008-08-30 06:03:09 -030051#define BT856_REG_OFFSET 0xDA
52#define BT856_NR_REG 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54struct bt856 {
Hans Verkuila91f56e2009-02-19 08:54:36 -030055 struct v4l2_subdev sd;
Jean Delvaref49a5ea2006-03-22 03:48:36 -030056 unsigned char reg[BT856_NR_REG];
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Hans Verkuil107063c2009-02-18 17:26:06 -030058 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Hans Verkuila91f56e2009-02-19 08:54:36 -030061static inline struct bt856 *to_bt856(struct v4l2_subdev *sd)
62{
63 return container_of(sd, struct bt856, sd);
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* ----------------------------------------------------------------------- */
67
Hans Verkuila91f56e2009-02-19 08:54:36 -030068static inline int bt856_write(struct bt856 *encoder, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Hans Verkuila91f56e2009-02-19 08:54:36 -030070 struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Hans Verkuil187565c2008-08-30 06:03:09 -030072 encoder->reg[reg - BT856_REG_OFFSET] = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return i2c_smbus_write_byte_data(client, reg, value);
74}
75
Hans Verkuila91f56e2009-02-19 08:54:36 -030076static inline int bt856_setbit(struct bt856 *encoder, u8 reg, u8 bit, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Hans Verkuila91f56e2009-02-19 08:54:36 -030078 return bt856_write(encoder, reg,
Hans Verkuila8c26df2008-09-07 07:59:35 -030079 (encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
80 (value ? (1 << bit) : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Hans Verkuila91f56e2009-02-19 08:54:36 -030083static void bt856_dump(struct bt856 *encoder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Hans Verkuila91f56e2009-02-19 08:54:36 -030087 v4l2_info(&encoder->sd, "register dump:\n");
Jean Delvaref49a5ea2006-03-22 03:48:36 -030088 for (i = 0; i < BT856_NR_REG; i += 2)
Hans Verkuila8c26df2008-09-07 07:59:35 -030089 printk(KERN_CONT " %02x", encoder->reg[i]);
90 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/* ----------------------------------------------------------------------- */
94
Hans Verkuila91f56e2009-02-19 08:54:36 -030095static int bt856_init(struct v4l2_subdev *sd, u32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Hans Verkuila91f56e2009-02-19 08:54:36 -030097 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Hans Verkuila91f56e2009-02-19 08:54:36 -030099 /* This is just for testing!!! */
100 v4l2_dbg(1, debug, sd, "init\n");
101 bt856_write(encoder, 0xdc, 0x18);
102 bt856_write(encoder, 0xda, 0);
103 bt856_write(encoder, 0xde, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Hans Verkuila91f56e2009-02-19 08:54:36 -0300105 bt856_setbit(encoder, 0xdc, 3, 1);
106 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
107 bt856_setbit(encoder, 0xdc, 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Hans Verkuila91f56e2009-02-19 08:54:36 -0300109 if (encoder->norm & V4L2_STD_NTSC)
110 bt856_setbit(encoder, 0xdc, 2, 0);
111 else
112 bt856_setbit(encoder, 0xdc, 2, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Hans Verkuila91f56e2009-02-19 08:54:36 -0300114 bt856_setbit(encoder, 0xdc, 1, 1);
115 bt856_setbit(encoder, 0xde, 4, 0);
116 bt856_setbit(encoder, 0xde, 3, 1);
117 if (debug != 0)
118 bt856_dump(encoder);
119 return 0;
120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Hans Verkuila91f56e2009-02-19 08:54:36 -0300122static int bt856_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
123{
124 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Hans Verkuilcc5cef82009-03-06 10:15:01 -0300126 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Hans Verkuila91f56e2009-02-19 08:54:36 -0300128 if (std & V4L2_STD_NTSC) {
129 bt856_setbit(encoder, 0xdc, 2, 0);
130 } else if (std & V4L2_STD_PAL) {
131 bt856_setbit(encoder, 0xdc, 2, 1);
132 bt856_setbit(encoder, 0xda, 0, 0);
133 /*bt856_setbit(encoder, 0xda, 0, 1);*/
134 } else {
135 return -EINVAL;
Hans Verkuila8c26df2008-09-07 07:59:35 -0300136 }
Hans Verkuila91f56e2009-02-19 08:54:36 -0300137 encoder->norm = std;
138 if (debug != 0)
139 bt856_dump(encoder);
140 return 0;
141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Hans Verkuil5325b422009-04-02 11:26:22 -0300143static int bt856_s_routing(struct v4l2_subdev *sd,
144 u32 input, u32 output, u32 config)
Hans Verkuila91f56e2009-02-19 08:54:36 -0300145{
146 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Hans Verkuil5325b422009-04-02 11:26:22 -0300148 v4l2_dbg(1, debug, sd, "set input %d\n", input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Hans Verkuila91f56e2009-02-19 08:54:36 -0300150 /* We only have video bus.
Hans Verkuil5325b422009-04-02 11:26:22 -0300151 * input= 0: input is from bt819
152 * input= 1: input is from ZR36060 */
153 switch (input) {
Hans Verkuila91f56e2009-02-19 08:54:36 -0300154 case 0:
155 bt856_setbit(encoder, 0xde, 4, 0);
156 bt856_setbit(encoder, 0xde, 3, 1);
157 bt856_setbit(encoder, 0xdc, 3, 1);
158 bt856_setbit(encoder, 0xdc, 6, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 break;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300160 case 1:
161 bt856_setbit(encoder, 0xde, 4, 0);
162 bt856_setbit(encoder, 0xde, 3, 1);
163 bt856_setbit(encoder, 0xdc, 3, 1);
164 bt856_setbit(encoder, 0xdc, 6, 1);
165 break;
166 case 2: /* Color bar */
167 bt856_setbit(encoder, 0xdc, 3, 0);
168 bt856_setbit(encoder, 0xde, 4, 1);
169 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 default:
171 return -EINVAL;
172 }
173
Hans Verkuila91f56e2009-02-19 08:54:36 -0300174 if (debug != 0)
175 bt856_dump(encoder);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 0;
177}
178
179/* ----------------------------------------------------------------------- */
180
Hans Verkuila91f56e2009-02-19 08:54:36 -0300181static const struct v4l2_subdev_core_ops bt856_core_ops = {
Hans Verkuila91f56e2009-02-19 08:54:36 -0300182 .init = bt856_init,
183};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Hans Verkuila91f56e2009-02-19 08:54:36 -0300185static const struct v4l2_subdev_video_ops bt856_video_ops = {
186 .s_std_output = bt856_s_std_output,
187 .s_routing = bt856_s_routing,
188};
189
190static const struct v4l2_subdev_ops bt856_ops = {
191 .core = &bt856_core_ops,
192 .video = &bt856_video_ops,
193};
194
195/* ----------------------------------------------------------------------- */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300196
Hans Verkuila8c26df2008-09-07 07:59:35 -0300197static int bt856_probe(struct i2c_client *client,
198 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct bt856 *encoder;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300201 struct v4l2_subdev *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* Check if the adapter supports the needed features */
Hans Verkuila8c26df2008-09-07 07:59:35 -0300204 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
205 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Hans Verkuila8c26df2008-09-07 07:59:35 -0300207 v4l_info(client, "chip found @ 0x%x (%s)\n",
208 client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300210 encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
Hans Verkuila8c26df2008-09-07 07:59:35 -0300211 if (encoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return -ENOMEM;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300213 sd = &encoder->sd;
214 v4l2_i2c_subdev_init(sd, client, &bt856_ops);
Hans Verkuil107063c2009-02-18 17:26:06 -0300215 encoder->norm = V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Hans Verkuila91f56e2009-02-19 08:54:36 -0300217 bt856_write(encoder, 0xdc, 0x18);
218 bt856_write(encoder, 0xda, 0);
219 bt856_write(encoder, 0xde, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Hans Verkuila91f56e2009-02-19 08:54:36 -0300221 bt856_setbit(encoder, 0xdc, 3, 1);
222 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
223 bt856_setbit(encoder, 0xdc, 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Hans Verkuil107063c2009-02-18 17:26:06 -0300225 if (encoder->norm & V4L2_STD_NTSC)
Hans Verkuila91f56e2009-02-19 08:54:36 -0300226 bt856_setbit(encoder, 0xdc, 2, 0);
Hans Verkuil107063c2009-02-18 17:26:06 -0300227 else
Hans Verkuila91f56e2009-02-19 08:54:36 -0300228 bt856_setbit(encoder, 0xdc, 2, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Hans Verkuila91f56e2009-02-19 08:54:36 -0300230 bt856_setbit(encoder, 0xdc, 1, 1);
231 bt856_setbit(encoder, 0xde, 4, 0);
232 bt856_setbit(encoder, 0xde, 3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (debug != 0)
Hans Verkuila91f56e2009-02-19 08:54:36 -0300235 bt856_dump(encoder);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return 0;
237}
238
Hans Verkuila8c26df2008-09-07 07:59:35 -0300239static int bt856_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Hans Verkuila91f56e2009-02-19 08:54:36 -0300241 struct v4l2_subdev *sd = i2c_get_clientdata(client);
242
243 v4l2_device_unregister_subdev(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245}
246
Hans Verkuila8c26df2008-09-07 07:59:35 -0300247static const struct i2c_device_id bt856_id[] = {
248 { "bt856", 0 },
249 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
Hans Verkuila8c26df2008-09-07 07:59:35 -0300251MODULE_DEVICE_TABLE(i2c, bt856_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Hans Verkuil3f417202010-09-15 15:32:24 -0300253static struct i2c_driver bt856_driver = {
254 .driver = {
255 .owner = THIS_MODULE,
256 .name = "bt856",
257 },
258 .probe = bt856_probe,
259 .remove = bt856_remove,
260 .id_table = bt856_id,
Hans Verkuila8c26df2008-09-07 07:59:35 -0300261};
Hans Verkuil3f417202010-09-15 15:32:24 -0300262
Axel Linc6e8d862012-02-12 06:56:32 -0300263module_i2c_driver(bt856_driver);