tw9906.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2005-2006 Micronas USA Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License (Version 2) as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/i2c.h>
  16. #include <linux/videodev2.h>
  17. #include <linux/ioctl.h>
  18. #include <linux/slab.h>
  19. #include <media/v4l2-device.h>
  20. #include <media/v4l2-ctrls.h>
  21. MODULE_DESCRIPTION("TW9906 I2C subdev driver");
  22. MODULE_LICENSE("GPL v2");
  23. struct tw9906 {
  24. struct v4l2_subdev sd;
  25. struct v4l2_ctrl_handler hdl;
  26. v4l2_std_id norm;
  27. };
  28. static inline struct tw9906 *to_state(struct v4l2_subdev *sd)
  29. {
  30. return container_of(sd, struct tw9906, sd);
  31. }
  32. static const u8 initial_registers[] = {
  33. 0x02, 0x40, /* input 0, composite */
  34. 0x03, 0xa2, /* correct digital format */
  35. 0x05, 0x81, /* or 0x01 for PAL */
  36. 0x07, 0x02, /* window */
  37. 0x08, 0x14, /* window */
  38. 0x09, 0xf0, /* window */
  39. 0x0a, 0x10, /* window */
  40. 0x0b, 0xd0, /* window */
  41. 0x0d, 0x00, /* scaling */
  42. 0x0e, 0x11, /* scaling */
  43. 0x0f, 0x00, /* scaling */
  44. 0x10, 0x00, /* brightness */
  45. 0x11, 0x60, /* contrast */
  46. 0x12, 0x11, /* sharpness */
  47. 0x13, 0x7e, /* U gain */
  48. 0x14, 0x7e, /* V gain */
  49. 0x15, 0x00, /* hue */
  50. 0x19, 0x57, /* vbi */
  51. 0x1a, 0x0f,
  52. 0x1b, 0x40,
  53. 0x29, 0x03,
  54. 0x55, 0x00,
  55. 0x6b, 0x26,
  56. 0x6c, 0x36,
  57. 0x6d, 0xf0,
  58. 0x6e, 0x41,
  59. 0x6f, 0x13,
  60. 0xad, 0x70,
  61. 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
  62. };
  63. static int write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
  64. {
  65. struct i2c_client *client = v4l2_get_subdevdata(sd);
  66. return i2c_smbus_write_byte_data(client, reg, value);
  67. }
  68. static int write_regs(struct v4l2_subdev *sd, const u8 *regs)
  69. {
  70. int i;
  71. for (i = 0; regs[i] != 0x00; i += 2)
  72. if (write_reg(sd, regs[i], regs[i + 1]) < 0)
  73. return -1;
  74. return 0;
  75. }
  76. static int tw9906_s_video_routing(struct v4l2_subdev *sd, u32 input,
  77. u32 output, u32 config)
  78. {
  79. write_reg(sd, 0x02, 0x40 | (input << 1));
  80. return 0;
  81. }
  82. static int tw9906_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  83. {
  84. struct tw9906 *dec = to_state(sd);
  85. bool is_60hz = norm & V4L2_STD_525_60;
  86. static const u8 config_60hz[] = {
  87. 0x05, 0x81,
  88. 0x07, 0x02,
  89. 0x08, 0x14,
  90. 0x09, 0xf0,
  91. 0, 0,
  92. };
  93. static const u8 config_50hz[] = {
  94. 0x05, 0x01,
  95. 0x07, 0x12,
  96. 0x08, 0x18,
  97. 0x09, 0x20,
  98. 0, 0,
  99. };
  100. write_regs(sd, is_60hz ? config_60hz : config_50hz);
  101. dec->norm = norm;
  102. return 0;
  103. }
  104. static int tw9906_s_ctrl(struct v4l2_ctrl *ctrl)
  105. {
  106. struct tw9906 *dec = container_of(ctrl->handler, struct tw9906, hdl);
  107. struct v4l2_subdev *sd = &dec->sd;
  108. switch (ctrl->id) {
  109. case V4L2_CID_BRIGHTNESS:
  110. write_reg(sd, 0x10, ctrl->val);
  111. break;
  112. case V4L2_CID_CONTRAST:
  113. write_reg(sd, 0x11, ctrl->val);
  114. break;
  115. case V4L2_CID_HUE:
  116. write_reg(sd, 0x15, ctrl->val);
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. return 0;
  122. }
  123. static int tw9906_log_status(struct v4l2_subdev *sd)
  124. {
  125. struct tw9906 *dec = to_state(sd);
  126. bool is_60hz = dec->norm & V4L2_STD_525_60;
  127. v4l2_info(sd, "Standard: %d Hz\n", is_60hz ? 60 : 50);
  128. v4l2_ctrl_subdev_log_status(sd);
  129. return 0;
  130. }
  131. /* --------------------------------------------------------------------------*/
  132. static const struct v4l2_ctrl_ops tw9906_ctrl_ops = {
  133. .s_ctrl = tw9906_s_ctrl,
  134. };
  135. static const struct v4l2_subdev_core_ops tw9906_core_ops = {
  136. .log_status = tw9906_log_status,
  137. };
  138. static const struct v4l2_subdev_video_ops tw9906_video_ops = {
  139. .s_std = tw9906_s_std,
  140. .s_routing = tw9906_s_video_routing,
  141. };
  142. static const struct v4l2_subdev_ops tw9906_ops = {
  143. .core = &tw9906_core_ops,
  144. .video = &tw9906_video_ops,
  145. };
  146. static int tw9906_probe(struct i2c_client *client,
  147. const struct i2c_device_id *id)
  148. {
  149. struct tw9906 *dec;
  150. struct v4l2_subdev *sd;
  151. struct v4l2_ctrl_handler *hdl;
  152. /* Check if the adapter supports the needed features */
  153. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  154. return -EIO;
  155. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  156. client->addr << 1, client->adapter->name);
  157. dec = devm_kzalloc(&client->dev, sizeof(*dec), GFP_KERNEL);
  158. if (dec == NULL)
  159. return -ENOMEM;
  160. sd = &dec->sd;
  161. v4l2_i2c_subdev_init(sd, client, &tw9906_ops);
  162. hdl = &dec->hdl;
  163. v4l2_ctrl_handler_init(hdl, 4);
  164. v4l2_ctrl_new_std(hdl, &tw9906_ctrl_ops,
  165. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  166. v4l2_ctrl_new_std(hdl, &tw9906_ctrl_ops,
  167. V4L2_CID_CONTRAST, 0, 255, 1, 0x60);
  168. v4l2_ctrl_new_std(hdl, &tw9906_ctrl_ops,
  169. V4L2_CID_HUE, -128, 127, 1, 0);
  170. sd->ctrl_handler = hdl;
  171. if (hdl->error) {
  172. int err = hdl->error;
  173. v4l2_ctrl_handler_free(hdl);
  174. return err;
  175. }
  176. /* Initialize tw9906 */
  177. dec->norm = V4L2_STD_NTSC;
  178. if (write_regs(sd, initial_registers) < 0) {
  179. v4l2_err(client, "error initializing TW9906\n");
  180. return -EINVAL;
  181. }
  182. return 0;
  183. }
  184. static int tw9906_remove(struct i2c_client *client)
  185. {
  186. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  187. v4l2_device_unregister_subdev(sd);
  188. v4l2_ctrl_handler_free(&to_state(sd)->hdl);
  189. return 0;
  190. }
  191. /* ----------------------------------------------------------------------- */
  192. static const struct i2c_device_id tw9906_id[] = {
  193. { "tw9906", 0 },
  194. { }
  195. };
  196. MODULE_DEVICE_TABLE(i2c, tw9906_id);
  197. static struct i2c_driver tw9906_driver = {
  198. .driver = {
  199. .name = "tw9906",
  200. },
  201. .probe = tw9906_probe,
  202. .remove = tw9906_remove,
  203. .id_table = tw9906_id,
  204. };
  205. module_i2c_driver(tw9906_driver);