blob: d6a1698caa2a0e74491ecf35bfafe01910f0cb0c [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Hans Verkuiladd953c2006-03-29 14:56:17 -03002/*
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>
Hans Verkuiladd953c2006-03-29 14:56:17 -03008 */
9
Hans Verkuiladd953c2006-03-29 14:56:17 -030010#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/i2c.h>
13#include <linux/videodev2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -030015#include <media/v4l2-device.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020016#include <media/i2c/upd64083.h>
Hans Verkuiladd953c2006-03-29 14:56:17 -030017
18MODULE_DESCRIPTION("uPD64083 driver");
19MODULE_AUTHOR("T. Adachi, Takeru KOMORIYA, Hans Verkuil");
20MODULE_LICENSE("GPL");
21
Rusty Russell90ab5ee2012-01-13 09:32:20 +103022static bool debug;
Hans Verkuiladd953c2006-03-29 14:56:17 -030023module_param(debug, bool, 0644);
24
25MODULE_PARM_DESC(debug, "Debug level (0-1)");
26
Hans Verkuiladd953c2006-03-29 14:56:17 -030027
28enum {
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
37struct upd64083_state {
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -030038 struct v4l2_subdev sd;
Hans Verkuiladd953c2006-03-29 14:56:17 -030039 u8 mode;
40 u8 ext_y_adc;
41 u8 regs[TOT_REGS];
42};
43
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -030044static inline struct upd64083_state *to_state(struct v4l2_subdev *sd)
45{
46 return container_of(sd, struct upd64083_state, sd);
47}
48
Hans Verkuiladd953c2006-03-29 14:56:17 -030049/* Initial values when used in combination with the
50 NEC upd64031a ghost reduction chip. */
51static 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
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -030061static void upd64083_write(struct v4l2_subdev *sd, u8 reg, u8 val)
Hans Verkuiladd953c2006-03-29 14:56:17 -030062{
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -030063 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuiladd953c2006-03-29 14:56:17 -030064 u8 buf[2];
65
66 buf[0] = reg;
67 buf[1] = val;
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -030068 v4l2_dbg(1, debug, sd, "write reg: %02x val: %02x\n", reg, val);
Hans Verkuiladd953c2006-03-29 14:56:17 -030069 if (i2c_master_send(client, buf, 2) != 2)
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -030070 v4l2_err(sd, "I/O error write 0x%02x/0x%02x\n", reg, val);
Hans Verkuiladd953c2006-03-29 14:56:17 -030071}
72
73/* ------------------------------------------------------------------------ */
74
75#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -030076static u8 upd64083_read(struct v4l2_subdev *sd, u8 reg)
Hans Verkuiladd953c2006-03-29 14:56:17 -030077{
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -030078 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuiladd953c2006-03-29 14:56:17 -030079 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
Hans Verkuil5325b422009-04-02 11:26:22 -030090static int upd64083_s_routing(struct v4l2_subdev *sd,
91 u32 input, u32 output, u32 config)
Hans Verkuiladd953c2006-03-29 14:56:17 -030092{
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -030093 struct upd64083_state *state = to_state(sd);
94 u8 r00, r02;
Hans Verkuiladd953c2006-03-29 14:56:17 -030095
Hans Verkuil5325b422009-04-02 11:26:22 -030096 if (input > 7 || (input & 6) == 6)
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -030097 return -EINVAL;
Hans Verkuil5325b422009-04-02 11:26:22 -030098 state->mode = (input & 3) << 6;
99 state->ext_y_adc = (input & UPD64083_EXT_Y_ADC) << 3;
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300100 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);
Hans Verkuiladd953c2006-03-29 14:56:17 -0300104 return 0;
105}
106
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300107#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300108static int upd64083_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300109{
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300110 reg->val = upd64083_read(sd, reg->reg & 0xff);
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300111 reg->size = 1;
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300112 return 0;
113}
114
Hans Verkuil977ba3b12013-03-24 08:28:46 -0300115static int upd64083_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300116{
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300117 upd64083_write(sd, reg->reg & 0xff, reg->val & 0xff);
118 return 0;
119}
120#endif
121
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300122static 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
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300134/* ----------------------------------------------------------------------- */
135
136static const struct v4l2_subdev_core_ops upd64083_core_ops = {
137 .log_status = upd64083_log_status,
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300138#ifdef CONFIG_VIDEO_ADV_DEBUG
139 .g_register = upd64083_g_register,
140 .s_register = upd64083_s_register,
141#endif
142};
143
144static const struct v4l2_subdev_video_ops upd64083_video_ops = {
145 .s_routing = upd64083_s_routing,
146};
147
148static const struct v4l2_subdev_ops upd64083_ops = {
149 .core = &upd64083_core_ops,
150 .video = &upd64083_video_ops,
151};
152
Hans Verkuiladd953c2006-03-29 14:56:17 -0300153/* ------------------------------------------------------------------------ */
154
155/* i2c implementation */
156
Jean Delvared2653e92008-04-29 23:11:39 +0200157static int upd64083_probe(struct i2c_client *client,
158 const struct i2c_device_id *id)
Hans Verkuiladd953c2006-03-29 14:56:17 -0300159{
Hans Verkuiladd953c2006-03-29 14:56:17 -0300160 struct upd64083_state *state;
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300161 struct v4l2_subdev *sd;
Hans Verkuiladd953c2006-03-29 14:56:17 -0300162 int i;
163
Hans Verkuil26666972007-09-14 04:58:06 -0300164 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Hans Verkuil188f3452007-09-16 10:47:15 -0300165 return -EIO;
Hans Verkuiladd953c2006-03-29 14:56:17 -0300166
Hans Verkuil8c9fc8d2007-12-12 07:30:02 -0300167 v4l_info(client, "chip found @ 0x%x (%s)\n",
168 client->addr << 1, client->adapter->name);
Hans Verkuiladd953c2006-03-29 14:56:17 -0300169
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300170 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuil8c9fc8d2007-12-12 07:30:02 -0300171 if (state == NULL)
Hans Verkuiladd953c2006-03-29 14:56:17 -0300172 return -ENOMEM;
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300173 sd = &state->sd;
174 v4l2_i2c_subdev_init(sd, client, &upd64083_ops);
Hans Verkuiladd953c2006-03-29 14:56:17 -0300175 /* Initially assume that a ghost reduction chip is present */
176 state->mode = 0; /* YCS mode */
177 state->ext_y_adc = (1 << 5);
178 memcpy(state->regs, upd64083_init, TOT_REGS);
Hans Verkuil8c9fc8d2007-12-12 07:30:02 -0300179 for (i = 0; i < TOT_REGS; i++)
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300180 upd64083_write(sd, i, state->regs[i]);
Hans Verkuiladd953c2006-03-29 14:56:17 -0300181 return 0;
182}
183
Hans Verkuil26666972007-09-14 04:58:06 -0300184static int upd64083_remove(struct i2c_client *client)
Hans Verkuiladd953c2006-03-29 14:56:17 -0300185{
Hans Verkuilcc7ca3e2008-11-29 13:00:30 -0300186 struct v4l2_subdev *sd = i2c_get_clientdata(client);
187
188 v4l2_device_unregister_subdev(sd);
Hans Verkuiladd953c2006-03-29 14:56:17 -0300189 return 0;
190}
191
192/* ----------------------------------------------------------------------- */
193
Jean Delvareaf294862008-05-18 20:49:40 +0200194static const struct i2c_device_id upd64083_id[] = {
195 { "upd64083", 0 },
196 { }
197};
198MODULE_DEVICE_TABLE(i2c, upd64083_id);
Hans Verkuil26666972007-09-14 04:58:06 -0300199
Hans Verkuil37a0cfc2010-09-15 15:46:37 -0300200static struct i2c_driver upd64083_driver = {
201 .driver = {
Hans Verkuil37a0cfc2010-09-15 15:46:37 -0300202 .name = "upd64083",
203 },
204 .probe = upd64083_probe,
205 .remove = upd64083_remove,
206 .id_table = upd64083_id,
Hans Verkuiladd953c2006-03-29 14:56:17 -0300207};
Hans Verkuil37a0cfc2010-09-15 15:46:37 -0300208
Axel Linc6e8d862012-02-12 06:56:32 -0300209module_i2c_driver(upd64083_driver);