tw9906.c 5.7 KB

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