tw9903.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005-2006 Micronas USA Inc.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/i2c.h>
  8. #include <linux/videodev2.h>
  9. #include <linux/ioctl.h>
  10. #include <media/v4l2-device.h>
  11. #include <media/v4l2-ctrls.h>
  12. #include <linux/slab.h>
  13. MODULE_DESCRIPTION("TW9903 I2C subdev driver");
  14. MODULE_LICENSE("GPL v2");
  15. /*
  16. * This driver is based on the wis-tw9903.c source that was in
  17. * drivers/staging/media/go7007. That source had commented out code for
  18. * saturation and scaling (neither seemed to work). If anyone ever gets
  19. * hardware to test this driver, then that code might be useful to look at.
  20. * You need to get the kernel sources of, say, kernel 3.8 where that
  21. * wis-tw9903 driver is still present.
  22. */
  23. struct tw9903 {
  24. struct v4l2_subdev sd;
  25. struct v4l2_ctrl_handler hdl;
  26. v4l2_std_id norm;
  27. };
  28. static inline struct tw9903 *to_state(struct v4l2_subdev *sd)
  29. {
  30. return container_of(sd, struct tw9903, sd);
  31. }
  32. static const u8 initial_registers[] = {
  33. 0x02, 0x44, /* input 1, composite */
  34. 0x03, 0x92, /* correct digital format */
  35. 0x04, 0x00,
  36. 0x05, 0x80, /* or 0x00 for PAL */
  37. 0x06, 0x40, /* second internal current reference */
  38. 0x07, 0x02, /* window */
  39. 0x08, 0x14, /* window */
  40. 0x09, 0xf0, /* window */
  41. 0x0a, 0x81, /* window */
  42. 0x0b, 0xd0, /* window */
  43. 0x0c, 0x8c,
  44. 0x0d, 0x00, /* scaling */
  45. 0x0e, 0x11, /* scaling */
  46. 0x0f, 0x00, /* scaling */
  47. 0x10, 0x00, /* brightness */
  48. 0x11, 0x60, /* contrast */
  49. 0x12, 0x01, /* sharpness */
  50. 0x13, 0x7f, /* U gain */
  51. 0x14, 0x5a, /* V gain */
  52. 0x15, 0x00, /* hue */
  53. 0x16, 0xc3, /* sharpness */
  54. 0x18, 0x00,
  55. 0x19, 0x58, /* vbi */
  56. 0x1a, 0x80,
  57. 0x1c, 0x0f, /* video norm */
  58. 0x1d, 0x7f, /* video norm */
  59. 0x20, 0xa0, /* clamping gain (working 0x50) */
  60. 0x21, 0x22,
  61. 0x22, 0xf0,
  62. 0x23, 0xfe,
  63. 0x24, 0x3c,
  64. 0x25, 0x38,
  65. 0x26, 0x44,
  66. 0x27, 0x20,
  67. 0x28, 0x00,
  68. 0x29, 0x15,
  69. 0x2a, 0xa0,
  70. 0x2b, 0x44,
  71. 0x2c, 0x37,
  72. 0x2d, 0x00,
  73. 0x2e, 0xa5, /* burst PLL control (working: a9) */
  74. 0x2f, 0xe0, /* 0xea is blue test frame -- 0xe0 for normal */
  75. 0x31, 0x00,
  76. 0x33, 0x22,
  77. 0x34, 0x11,
  78. 0x35, 0x35,
  79. 0x3b, 0x05,
  80. 0x06, 0xc0, /* reset device */
  81. 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
  82. };
  83. static int write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
  84. {
  85. struct i2c_client *client = v4l2_get_subdevdata(sd);
  86. return i2c_smbus_write_byte_data(client, reg, value);
  87. }
  88. static int write_regs(struct v4l2_subdev *sd, const u8 *regs)
  89. {
  90. int i;
  91. for (i = 0; regs[i] != 0x00; i += 2)
  92. if (write_reg(sd, regs[i], regs[i + 1]) < 0)
  93. return -1;
  94. return 0;
  95. }
  96. static int tw9903_s_video_routing(struct v4l2_subdev *sd, u32 input,
  97. u32 output, u32 config)
  98. {
  99. write_reg(sd, 0x02, 0x40 | (input << 1));
  100. return 0;
  101. }
  102. static int tw9903_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  103. {
  104. struct tw9903 *dec = to_state(sd);
  105. bool is_60hz = norm & V4L2_STD_525_60;
  106. static const u8 config_60hz[] = {
  107. 0x05, 0x80,
  108. 0x07, 0x02,
  109. 0x08, 0x14,
  110. 0x09, 0xf0,
  111. 0, 0,
  112. };
  113. static const u8 config_50hz[] = {
  114. 0x05, 0x00,
  115. 0x07, 0x12,
  116. 0x08, 0x18,
  117. 0x09, 0x20,
  118. 0, 0,
  119. };
  120. write_regs(sd, is_60hz ? config_60hz : config_50hz);
  121. dec->norm = norm;
  122. return 0;
  123. }
  124. static int tw9903_s_ctrl(struct v4l2_ctrl *ctrl)
  125. {
  126. struct tw9903 *dec = container_of(ctrl->handler, struct tw9903, hdl);
  127. struct v4l2_subdev *sd = &dec->sd;
  128. switch (ctrl->id) {
  129. case V4L2_CID_BRIGHTNESS:
  130. write_reg(sd, 0x10, ctrl->val);
  131. break;
  132. case V4L2_CID_CONTRAST:
  133. write_reg(sd, 0x11, ctrl->val);
  134. break;
  135. case V4L2_CID_HUE:
  136. write_reg(sd, 0x15, ctrl->val);
  137. break;
  138. default:
  139. return -EINVAL;
  140. }
  141. return 0;
  142. }
  143. static int tw9903_log_status(struct v4l2_subdev *sd)
  144. {
  145. struct tw9903 *dec = to_state(sd);
  146. bool is_60hz = dec->norm & V4L2_STD_525_60;
  147. v4l2_info(sd, "Standard: %d Hz\n", is_60hz ? 60 : 50);
  148. v4l2_ctrl_subdev_log_status(sd);
  149. return 0;
  150. }
  151. /* --------------------------------------------------------------------------*/
  152. static const struct v4l2_ctrl_ops tw9903_ctrl_ops = {
  153. .s_ctrl = tw9903_s_ctrl,
  154. };
  155. static const struct v4l2_subdev_core_ops tw9903_core_ops = {
  156. .log_status = tw9903_log_status,
  157. };
  158. static const struct v4l2_subdev_video_ops tw9903_video_ops = {
  159. .s_std = tw9903_s_std,
  160. .s_routing = tw9903_s_video_routing,
  161. };
  162. static const struct v4l2_subdev_ops tw9903_ops = {
  163. .core = &tw9903_core_ops,
  164. .video = &tw9903_video_ops,
  165. };
  166. /* --------------------------------------------------------------------------*/
  167. static int tw9903_probe(struct i2c_client *client,
  168. const struct i2c_device_id *id)
  169. {
  170. struct tw9903 *dec;
  171. struct v4l2_subdev *sd;
  172. struct v4l2_ctrl_handler *hdl;
  173. /* Check if the adapter supports the needed features */
  174. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  175. return -EIO;
  176. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  177. client->addr << 1, client->adapter->name);
  178. dec = devm_kzalloc(&client->dev, sizeof(*dec), GFP_KERNEL);
  179. if (dec == NULL)
  180. return -ENOMEM;
  181. sd = &dec->sd;
  182. v4l2_i2c_subdev_init(sd, client, &tw9903_ops);
  183. hdl = &dec->hdl;
  184. v4l2_ctrl_handler_init(hdl, 4);
  185. v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
  186. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  187. v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
  188. V4L2_CID_CONTRAST, 0, 255, 1, 0x60);
  189. v4l2_ctrl_new_std(hdl, &tw9903_ctrl_ops,
  190. V4L2_CID_HUE, -128, 127, 1, 0);
  191. sd->ctrl_handler = hdl;
  192. if (hdl->error) {
  193. int err = hdl->error;
  194. v4l2_ctrl_handler_free(hdl);
  195. return err;
  196. }
  197. /* Initialize tw9903 */
  198. dec->norm = V4L2_STD_NTSC;
  199. if (write_regs(sd, initial_registers) < 0) {
  200. v4l2_err(client, "error initializing TW9903\n");
  201. return -EINVAL;
  202. }
  203. return 0;
  204. }
  205. static void tw9903_remove(struct i2c_client *client)
  206. {
  207. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  208. v4l2_device_unregister_subdev(sd);
  209. v4l2_ctrl_handler_free(&to_state(sd)->hdl);
  210. }
  211. /* ----------------------------------------------------------------------- */
  212. static const struct i2c_device_id tw9903_id[] = {
  213. { "tw9903", 0 },
  214. { }
  215. };
  216. MODULE_DEVICE_TABLE(i2c, tw9903_id);
  217. static struct i2c_driver tw9903_driver = {
  218. .driver = {
  219. .name = "tw9903",
  220. },
  221. .probe = tw9903_probe,
  222. .remove = tw9903_remove,
  223. .id_table = tw9903_id,
  224. };
  225. module_i2c_driver(tw9903_driver);