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