xref: /openbmc/linux/drivers/media/i2c/upd64083.c (revision 234489ac)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * upd6408x - NEC Electronics 3-Dimensional Y/C separation driver
4  *
5  * 2003 by T.Adachi (tadachi@tadachi-net.com)
6  * 2003 by Takeru KOMORIYA <komoriya@paken.org>
7  * 2006 by Hans Verkuil <hverkuil@xs4all.nl>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/i2c.h>
13 #include <linux/videodev2.h>
14 #include <linux/slab.h>
15 #include <media/v4l2-device.h>
16 #include <media/i2c/upd64083.h>
17 
18 MODULE_DESCRIPTION("uPD64083 driver");
19 MODULE_AUTHOR("T. Adachi, Takeru KOMORIYA, Hans Verkuil");
20 MODULE_LICENSE("GPL");
21 
22 static bool debug;
23 module_param(debug, bool, 0644);
24 
25 MODULE_PARM_DESC(debug, "Debug level (0-1)");
26 
27 
28 enum {
29 	R00 = 0, R01, R02, R03, R04,
30 	R05, R06, R07, R08, R09,
31 	R0A, R0B, R0C, R0D, R0E, R0F,
32 	R10, R11, R12, R13, R14,
33 	R15, R16,
34 	TOT_REGS
35 };
36 
37 struct upd64083_state {
38 	struct v4l2_subdev sd;
39 	u8 mode;
40 	u8 ext_y_adc;
41 	u8 regs[TOT_REGS];
42 };
43 
44 static inline struct upd64083_state *to_state(struct v4l2_subdev *sd)
45 {
46 	return container_of(sd, struct upd64083_state, sd);
47 }
48 
49 /* Initial values when used in combination with the
50    NEC upd64031a ghost reduction chip. */
51 static u8 upd64083_init[] = {
52 	0x1f, 0x01, 0xa0, 0x2d, 0x29,  /* we use EXCSS=0 */
53 	0x36, 0xdd, 0x05, 0x56, 0x48,
54 	0x00, 0x3a, 0xa0, 0x05, 0x08,
55 	0x44, 0x60, 0x08, 0x52, 0xf8,
56 	0x53, 0x60, 0x10
57 };
58 
59 /* ------------------------------------------------------------------------ */
60 
61 static void upd64083_write(struct v4l2_subdev *sd, u8 reg, u8 val)
62 {
63 	struct i2c_client *client = v4l2_get_subdevdata(sd);
64 	u8 buf[2];
65 
66 	buf[0] = reg;
67 	buf[1] = val;
68 	v4l2_dbg(1, debug, sd, "write reg: %02x val: %02x\n", reg, val);
69 	if (i2c_master_send(client, buf, 2) != 2)
70 		v4l2_err(sd, "I/O error write 0x%02x/0x%02x\n", reg, val);
71 }
72 
73 /* ------------------------------------------------------------------------ */
74 
75 #ifdef CONFIG_VIDEO_ADV_DEBUG
76 static u8 upd64083_read(struct v4l2_subdev *sd, u8 reg)
77 {
78 	struct i2c_client *client = v4l2_get_subdevdata(sd);
79 	u8 buf[7];
80 
81 	if (reg >= sizeof(buf))
82 		return 0xff;
83 	i2c_master_recv(client, buf, sizeof(buf));
84 	return buf[reg];
85 }
86 #endif
87 
88 /* ------------------------------------------------------------------------ */
89 
90 static int upd64083_s_routing(struct v4l2_subdev *sd,
91 			      u32 input, u32 output, u32 config)
92 {
93 	struct upd64083_state *state = to_state(sd);
94 	u8 r00, r02;
95 
96 	if (input > 7 || (input & 6) == 6)
97 		return -EINVAL;
98 	state->mode = (input & 3) << 6;
99 	state->ext_y_adc = (input & UPD64083_EXT_Y_ADC) << 3;
100 	r00 = (state->regs[R00] & ~(3 << 6)) | state->mode;
101 	r02 = (state->regs[R02] & ~(1 << 5)) | state->ext_y_adc;
102 	upd64083_write(sd, R00, r00);
103 	upd64083_write(sd, R02, r02);
104 	return 0;
105 }
106 
107 #ifdef CONFIG_VIDEO_ADV_DEBUG
108 static int upd64083_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
109 {
110 	reg->val = upd64083_read(sd, reg->reg & 0xff);
111 	reg->size = 1;
112 	return 0;
113 }
114 
115 static int upd64083_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
116 {
117 	upd64083_write(sd, reg->reg & 0xff, reg->val & 0xff);
118 	return 0;
119 }
120 #endif
121 
122 static int upd64083_log_status(struct v4l2_subdev *sd)
123 {
124 	struct i2c_client *client = v4l2_get_subdevdata(sd);
125 	u8 buf[7];
126 
127 	i2c_master_recv(client, buf, 7);
128 	v4l2_info(sd, "Status: SA00=%02x SA01=%02x SA02=%02x SA03=%02x "
129 		      "SA04=%02x SA05=%02x SA06=%02x\n",
130 		buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
131 	return 0;
132 }
133 
134 /* ----------------------------------------------------------------------- */
135 
136 static const struct v4l2_subdev_core_ops upd64083_core_ops = {
137 	.log_status = upd64083_log_status,
138 #ifdef CONFIG_VIDEO_ADV_DEBUG
139 	.g_register = upd64083_g_register,
140 	.s_register = upd64083_s_register,
141 #endif
142 };
143 
144 static const struct v4l2_subdev_video_ops upd64083_video_ops = {
145 	.s_routing = upd64083_s_routing,
146 };
147 
148 static const struct v4l2_subdev_ops upd64083_ops = {
149 	.core = &upd64083_core_ops,
150 	.video = &upd64083_video_ops,
151 };
152 
153 /* ------------------------------------------------------------------------ */
154 
155 /* i2c implementation */
156 
157 static int upd64083_probe(struct i2c_client *client)
158 {
159 	struct upd64083_state *state;
160 	struct v4l2_subdev *sd;
161 	int i;
162 
163 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
164 		return -EIO;
165 
166 	v4l_info(client, "chip found @ 0x%x (%s)\n",
167 			client->addr << 1, client->adapter->name);
168 
169 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
170 	if (state == NULL)
171 		return -ENOMEM;
172 	sd = &state->sd;
173 	v4l2_i2c_subdev_init(sd, client, &upd64083_ops);
174 	/* Initially assume that a ghost reduction chip is present */
175 	state->mode = 0;  /* YCS mode */
176 	state->ext_y_adc = (1 << 5);
177 	memcpy(state->regs, upd64083_init, TOT_REGS);
178 	for (i = 0; i < TOT_REGS; i++)
179 		upd64083_write(sd, i, state->regs[i]);
180 	return 0;
181 }
182 
183 static void upd64083_remove(struct i2c_client *client)
184 {
185 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
186 
187 	v4l2_device_unregister_subdev(sd);
188 }
189 
190 /* ----------------------------------------------------------------------- */
191 
192 static const struct i2c_device_id upd64083_id[] = {
193 	{ "upd64083", 0 },
194 	{ }
195 };
196 MODULE_DEVICE_TABLE(i2c, upd64083_id);
197 
198 static struct i2c_driver upd64083_driver = {
199 	.driver = {
200 		.name	= "upd64083",
201 	},
202 	.probe_new	= upd64083_probe,
203 	.remove		= upd64083_remove,
204 	.id_table	= upd64083_id,
205 };
206 
207 module_i2c_driver(upd64083_driver);
208