blob: c134fda270a177e5ffafbc2506c64dc9304d2b78 [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 * bt856 - BT856A Digital Video Encoder (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>
10 *
11 * This code was modify/ported from the saa7111 driver written
12 * by Dave Perks.
13 *
14 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
15 * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Hans Verkuila8c26df2008-09-07 07:59:35 -030021#include <linux/ioctl.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080022#include <linux/uaccess.h>
Hans Verkuila8c26df2008-09-07 07:59:35 -030023#include <linux/i2c.h>
Hans Verkuila91f56e2009-02-19 08:54:36 -030024#include <linux/videodev2.h>
25#include <media/v4l2-device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
28MODULE_AUTHOR("Mike Bernson & Dave Perks");
29MODULE_LICENSE("GPL");
30
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030031static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032module_param(debug, int, 0);
33MODULE_PARM_DESC(debug, "Debug level (0-1)");
34
Hans Verkuila91f56e2009-02-19 08:54:36 -030035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* ----------------------------------------------------------------------- */
37
Hans Verkuil187565c2008-08-30 06:03:09 -030038#define BT856_REG_OFFSET 0xDA
39#define BT856_NR_REG 6
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41struct bt856 {
Hans Verkuila91f56e2009-02-19 08:54:36 -030042 struct v4l2_subdev sd;
Jean Delvaref49a5ea2006-03-22 03:48:36 -030043 unsigned char reg[BT856_NR_REG];
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Hans Verkuil107063c2009-02-18 17:26:06 -030045 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Hans Verkuila91f56e2009-02-19 08:54:36 -030048static inline struct bt856 *to_bt856(struct v4l2_subdev *sd)
49{
50 return container_of(sd, struct bt856, sd);
51}
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* ----------------------------------------------------------------------- */
54
Hans Verkuila91f56e2009-02-19 08:54:36 -030055static inline int bt856_write(struct bt856 *encoder, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Hans Verkuila91f56e2009-02-19 08:54:36 -030057 struct i2c_client *client = v4l2_get_subdevdata(&encoder->sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Hans Verkuil187565c2008-08-30 06:03:09 -030059 encoder->reg[reg - BT856_REG_OFFSET] = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return i2c_smbus_write_byte_data(client, reg, value);
61}
62
Hans Verkuila91f56e2009-02-19 08:54:36 -030063static inline int bt856_setbit(struct bt856 *encoder, u8 reg, u8 bit, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Hans Verkuila91f56e2009-02-19 08:54:36 -030065 return bt856_write(encoder, reg,
Hans Verkuila8c26df2008-09-07 07:59:35 -030066 (encoder->reg[reg - BT856_REG_OFFSET] & ~(1 << bit)) |
67 (value ? (1 << bit) : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Hans Verkuila91f56e2009-02-19 08:54:36 -030070static void bt856_dump(struct bt856 *encoder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Hans Verkuila91f56e2009-02-19 08:54:36 -030074 v4l2_info(&encoder->sd, "register dump:\n");
Jean Delvaref49a5ea2006-03-22 03:48:36 -030075 for (i = 0; i < BT856_NR_REG; i += 2)
Hans Verkuila8c26df2008-09-07 07:59:35 -030076 printk(KERN_CONT " %02x", encoder->reg[i]);
77 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80/* ----------------------------------------------------------------------- */
81
Hans Verkuila91f56e2009-02-19 08:54:36 -030082static int bt856_init(struct v4l2_subdev *sd, u32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Hans Verkuila91f56e2009-02-19 08:54:36 -030084 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Hans Verkuila91f56e2009-02-19 08:54:36 -030086 /* This is just for testing!!! */
87 v4l2_dbg(1, debug, sd, "init\n");
88 bt856_write(encoder, 0xdc, 0x18);
89 bt856_write(encoder, 0xda, 0);
90 bt856_write(encoder, 0xde, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Hans Verkuila91f56e2009-02-19 08:54:36 -030092 bt856_setbit(encoder, 0xdc, 3, 1);
93 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
94 bt856_setbit(encoder, 0xdc, 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Hans Verkuila91f56e2009-02-19 08:54:36 -030096 if (encoder->norm & V4L2_STD_NTSC)
97 bt856_setbit(encoder, 0xdc, 2, 0);
98 else
99 bt856_setbit(encoder, 0xdc, 2, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Hans Verkuila91f56e2009-02-19 08:54:36 -0300101 bt856_setbit(encoder, 0xdc, 1, 1);
102 bt856_setbit(encoder, 0xde, 4, 0);
103 bt856_setbit(encoder, 0xde, 3, 1);
104 if (debug != 0)
105 bt856_dump(encoder);
106 return 0;
107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Hans Verkuila91f56e2009-02-19 08:54:36 -0300109static int bt856_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
110{
111 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Hans Verkuilcc5cef82009-03-06 10:15:01 -0300113 v4l2_dbg(1, debug, sd, "set norm %llx\n", (unsigned long long)std);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Hans Verkuila91f56e2009-02-19 08:54:36 -0300115 if (std & V4L2_STD_NTSC) {
116 bt856_setbit(encoder, 0xdc, 2, 0);
117 } else if (std & V4L2_STD_PAL) {
118 bt856_setbit(encoder, 0xdc, 2, 1);
119 bt856_setbit(encoder, 0xda, 0, 0);
120 /*bt856_setbit(encoder, 0xda, 0, 1);*/
121 } else {
122 return -EINVAL;
Hans Verkuila8c26df2008-09-07 07:59:35 -0300123 }
Hans Verkuila91f56e2009-02-19 08:54:36 -0300124 encoder->norm = std;
125 if (debug != 0)
126 bt856_dump(encoder);
127 return 0;
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Hans Verkuil5325b422009-04-02 11:26:22 -0300130static int bt856_s_routing(struct v4l2_subdev *sd,
131 u32 input, u32 output, u32 config)
Hans Verkuila91f56e2009-02-19 08:54:36 -0300132{
133 struct bt856 *encoder = to_bt856(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Hans Verkuil5325b422009-04-02 11:26:22 -0300135 v4l2_dbg(1, debug, sd, "set input %d\n", input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Hans Verkuila91f56e2009-02-19 08:54:36 -0300137 /* We only have video bus.
Hans Verkuil5325b422009-04-02 11:26:22 -0300138 * input= 0: input is from bt819
139 * input= 1: input is from ZR36060 */
140 switch (input) {
Hans Verkuila91f56e2009-02-19 08:54:36 -0300141 case 0:
142 bt856_setbit(encoder, 0xde, 4, 0);
143 bt856_setbit(encoder, 0xde, 3, 1);
144 bt856_setbit(encoder, 0xdc, 3, 1);
145 bt856_setbit(encoder, 0xdc, 6, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 break;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300147 case 1:
148 bt856_setbit(encoder, 0xde, 4, 0);
149 bt856_setbit(encoder, 0xde, 3, 1);
150 bt856_setbit(encoder, 0xdc, 3, 1);
151 bt856_setbit(encoder, 0xdc, 6, 1);
152 break;
153 case 2: /* Color bar */
154 bt856_setbit(encoder, 0xdc, 3, 0);
155 bt856_setbit(encoder, 0xde, 4, 1);
156 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 default:
158 return -EINVAL;
159 }
160
Hans Verkuila91f56e2009-02-19 08:54:36 -0300161 if (debug != 0)
162 bt856_dump(encoder);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return 0;
164}
165
166/* ----------------------------------------------------------------------- */
167
Hans Verkuila91f56e2009-02-19 08:54:36 -0300168static const struct v4l2_subdev_core_ops bt856_core_ops = {
Hans Verkuila91f56e2009-02-19 08:54:36 -0300169 .init = bt856_init,
170};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Hans Verkuila91f56e2009-02-19 08:54:36 -0300172static const struct v4l2_subdev_video_ops bt856_video_ops = {
173 .s_std_output = bt856_s_std_output,
174 .s_routing = bt856_s_routing,
175};
176
177static const struct v4l2_subdev_ops bt856_ops = {
178 .core = &bt856_core_ops,
179 .video = &bt856_video_ops,
180};
181
182/* ----------------------------------------------------------------------- */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300183
Hans Verkuila8c26df2008-09-07 07:59:35 -0300184static int bt856_probe(struct i2c_client *client,
185 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct bt856 *encoder;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300188 struct v4l2_subdev *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* Check if the adapter supports the needed features */
Hans Verkuila8c26df2008-09-07 07:59:35 -0300191 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
192 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Hans Verkuila8c26df2008-09-07 07:59:35 -0300194 v4l_info(client, "chip found @ 0x%x (%s)\n",
195 client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300197 encoder = devm_kzalloc(&client->dev, sizeof(*encoder), GFP_KERNEL);
Hans Verkuila8c26df2008-09-07 07:59:35 -0300198 if (encoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -ENOMEM;
Hans Verkuila91f56e2009-02-19 08:54:36 -0300200 sd = &encoder->sd;
201 v4l2_i2c_subdev_init(sd, client, &bt856_ops);
Hans Verkuil107063c2009-02-18 17:26:06 -0300202 encoder->norm = V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Hans Verkuila91f56e2009-02-19 08:54:36 -0300204 bt856_write(encoder, 0xdc, 0x18);
205 bt856_write(encoder, 0xda, 0);
206 bt856_write(encoder, 0xde, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Hans Verkuila91f56e2009-02-19 08:54:36 -0300208 bt856_setbit(encoder, 0xdc, 3, 1);
209 /*bt856_setbit(encoder, 0xdc, 6, 0);*/
210 bt856_setbit(encoder, 0xdc, 4, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Hans Verkuil107063c2009-02-18 17:26:06 -0300212 if (encoder->norm & V4L2_STD_NTSC)
Hans Verkuila91f56e2009-02-19 08:54:36 -0300213 bt856_setbit(encoder, 0xdc, 2, 0);
Hans Verkuil107063c2009-02-18 17:26:06 -0300214 else
Hans Verkuila91f56e2009-02-19 08:54:36 -0300215 bt856_setbit(encoder, 0xdc, 2, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Hans Verkuila91f56e2009-02-19 08:54:36 -0300217 bt856_setbit(encoder, 0xdc, 1, 1);
218 bt856_setbit(encoder, 0xde, 4, 0);
219 bt856_setbit(encoder, 0xde, 3, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 if (debug != 0)
Hans Verkuila91f56e2009-02-19 08:54:36 -0300222 bt856_dump(encoder);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return 0;
224}
225
Hans Verkuila8c26df2008-09-07 07:59:35 -0300226static int bt856_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Hans Verkuila91f56e2009-02-19 08:54:36 -0300228 struct v4l2_subdev *sd = i2c_get_clientdata(client);
229
230 v4l2_device_unregister_subdev(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0;
232}
233
Hans Verkuila8c26df2008-09-07 07:59:35 -0300234static const struct i2c_device_id bt856_id[] = {
235 { "bt856", 0 },
236 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
Hans Verkuila8c26df2008-09-07 07:59:35 -0300238MODULE_DEVICE_TABLE(i2c, bt856_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Hans Verkuil3f417202010-09-15 15:32:24 -0300240static struct i2c_driver bt856_driver = {
241 .driver = {
Hans Verkuil3f417202010-09-15 15:32:24 -0300242 .name = "bt856",
243 },
244 .probe = bt856_probe,
245 .remove = bt856_remove,
246 .id_table = bt856_id,
Hans Verkuila8c26df2008-09-07 07:59:35 -0300247};
Hans Verkuil3f417202010-09-15 15:32:24 -0300248
Axel Linc6e8d862012-02-12 06:56:32 -0300249module_i2c_driver(bt856_driver);