upd64083.c 5.2 KB

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