tw2804.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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-subdev.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-chip-ident.h>
  26. #include <media/v4l2-ctrls.h>
  27. #define TW2804_REG_AUTOGAIN 0x02
  28. #define TW2804_REG_HUE 0x0f
  29. #define TW2804_REG_SATURATION 0x10
  30. #define TW2804_REG_CONTRAST 0x11
  31. #define TW2804_REG_BRIGHTNESS 0x12
  32. #define TW2804_REG_COLOR_KILLER 0x14
  33. #define TW2804_REG_GAIN 0x3c
  34. #define TW2804_REG_CHROMA_GAIN 0x3d
  35. #define TW2804_REG_BLUE_BALANCE 0x3e
  36. #define TW2804_REG_RED_BALANCE 0x3f
  37. struct tw2804 {
  38. struct v4l2_subdev sd;
  39. struct v4l2_ctrl_handler hdl;
  40. u8 channel:2;
  41. u8 input:1;
  42. int norm;
  43. };
  44. static const u8 global_registers[] = {
  45. 0x39, 0x00,
  46. 0x3a, 0xff,
  47. 0x3b, 0x84,
  48. 0x3c, 0x80,
  49. 0x3d, 0x80,
  50. 0x3e, 0x82,
  51. 0x3f, 0x82,
  52. 0x78, 0x00,
  53. 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
  54. };
  55. static const u8 channel_registers[] = {
  56. 0x01, 0xc4,
  57. 0x02, 0xa5,
  58. 0x03, 0x20,
  59. 0x04, 0xd0,
  60. 0x05, 0x20,
  61. 0x06, 0xd0,
  62. 0x07, 0x88,
  63. 0x08, 0x20,
  64. 0x09, 0x07,
  65. 0x0a, 0xf0,
  66. 0x0b, 0x07,
  67. 0x0c, 0xf0,
  68. 0x0d, 0x40,
  69. 0x0e, 0xd2,
  70. 0x0f, 0x80,
  71. 0x10, 0x80,
  72. 0x11, 0x80,
  73. 0x12, 0x80,
  74. 0x13, 0x1f,
  75. 0x14, 0x00,
  76. 0x15, 0x00,
  77. 0x16, 0x00,
  78. 0x17, 0x00,
  79. 0x18, 0xff,
  80. 0x19, 0xff,
  81. 0x1a, 0xff,
  82. 0x1b, 0xff,
  83. 0x1c, 0xff,
  84. 0x1d, 0xff,
  85. 0x1e, 0xff,
  86. 0x1f, 0xff,
  87. 0x20, 0x07,
  88. 0x21, 0x07,
  89. 0x22, 0x00,
  90. 0x23, 0x91,
  91. 0x24, 0x51,
  92. 0x25, 0x03,
  93. 0x26, 0x00,
  94. 0x27, 0x00,
  95. 0x28, 0x00,
  96. 0x29, 0x00,
  97. 0x2a, 0x00,
  98. 0x2b, 0x00,
  99. 0x2c, 0x00,
  100. 0x2d, 0x00,
  101. 0x2e, 0x00,
  102. 0x2f, 0x00,
  103. 0x30, 0x00,
  104. 0x31, 0x00,
  105. 0x32, 0x00,
  106. 0x33, 0x00,
  107. 0x34, 0x00,
  108. 0x35, 0x00,
  109. 0x36, 0x00,
  110. 0x37, 0x00,
  111. 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
  112. };
  113. static int write_reg(struct i2c_client *client, u8 reg, u8 value, u8 channel)
  114. {
  115. return i2c_smbus_write_byte_data(client, reg | (channel << 6), value);
  116. }
  117. static int write_regs(struct i2c_client *client, const u8 *regs, u8 channel)
  118. {
  119. int ret;
  120. int i;
  121. for (i = 0; regs[i] != 0xff; i += 2) {
  122. ret = i2c_smbus_write_byte_data(client,
  123. regs[i] | (channel << 6), regs[i + 1]);
  124. if (ret < 0)
  125. return ret;
  126. }
  127. return 0;
  128. }
  129. static int read_reg(struct i2c_client *client, u8 reg, u8 channel)
  130. {
  131. return i2c_smbus_read_byte_data(client, (reg) | (channel << 6));
  132. }
  133. static inline struct tw2804 *to_state(struct v4l2_subdev *sd)
  134. {
  135. return container_of(sd, struct tw2804, sd);
  136. }
  137. static inline struct tw2804 *to_state_from_ctrl(struct v4l2_ctrl *ctrl)
  138. {
  139. return container_of(ctrl->handler, struct tw2804, hdl);
  140. }
  141. static int tw2804_log_status(struct v4l2_subdev *sd)
  142. {
  143. struct tw2804 *state = to_state(sd);
  144. v4l2_info(sd, "Standard: %s\n",
  145. state->norm & V4L2_STD_525_60 ? "60 Hz" : "50 Hz");
  146. v4l2_info(sd, "Channel: %d\n", state->channel);
  147. v4l2_info(sd, "Input: %d\n", state->input);
  148. return v4l2_ctrl_subdev_log_status(sd);
  149. }
  150. /*
  151. * These volatile controls are needed because all four channels share
  152. * these controls. So a change made to them through one channel would
  153. * require another channel to be updated.
  154. *
  155. * Normally this would have been done in a different way, but since the one
  156. * board that uses this driver sees this single chip as if it was on four
  157. * different i2c adapters (each adapter belonging to a separate instance of
  158. * the same USB driver) there is no reliable method that I have found to let
  159. * the instances know about each other.
  160. *
  161. * So implementing these global registers as volatile is the best we can do.
  162. */
  163. static int tw2804_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  164. {
  165. struct tw2804 *state = to_state_from_ctrl(ctrl);
  166. struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
  167. switch (ctrl->id) {
  168. case V4L2_CID_GAIN:
  169. ctrl->val = read_reg(client, TW2804_REG_GAIN, 0);
  170. return 0;
  171. case V4L2_CID_CHROMA_GAIN:
  172. ctrl->val = read_reg(client, TW2804_REG_CHROMA_GAIN, 0);
  173. return 0;
  174. case V4L2_CID_BLUE_BALANCE:
  175. ctrl->val = read_reg(client, TW2804_REG_BLUE_BALANCE, 0);
  176. return 0;
  177. case V4L2_CID_RED_BALANCE:
  178. ctrl->val = read_reg(client, TW2804_REG_RED_BALANCE, 0);
  179. return 0;
  180. }
  181. return 0;
  182. }
  183. static int tw2804_s_ctrl(struct v4l2_ctrl *ctrl)
  184. {
  185. struct tw2804 *state = to_state_from_ctrl(ctrl);
  186. struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
  187. int addr;
  188. int reg;
  189. switch (ctrl->id) {
  190. case V4L2_CID_AUTOGAIN:
  191. addr = TW2804_REG_AUTOGAIN;
  192. reg = read_reg(client, addr, state->channel);
  193. if (reg < 0)
  194. return reg;
  195. if (ctrl->val == 0)
  196. reg &= ~(1 << 7);
  197. else
  198. reg |= 1 << 7;
  199. return write_reg(client, addr, reg, state->channel);
  200. case V4L2_CID_COLOR_KILLER:
  201. addr = TW2804_REG_COLOR_KILLER;
  202. reg = read_reg(client, addr, state->channel);
  203. if (reg < 0)
  204. return reg;
  205. reg = (reg & ~(0x03)) | (ctrl->val == 0 ? 0x02 : 0x03);
  206. return write_reg(client, addr, reg, state->channel);
  207. case V4L2_CID_GAIN:
  208. return write_reg(client, TW2804_REG_GAIN, ctrl->val, 0);
  209. case V4L2_CID_CHROMA_GAIN:
  210. return write_reg(client, TW2804_REG_CHROMA_GAIN, ctrl->val, 0);
  211. case V4L2_CID_BLUE_BALANCE:
  212. return write_reg(client, TW2804_REG_BLUE_BALANCE, ctrl->val, 0);
  213. case V4L2_CID_RED_BALANCE:
  214. return write_reg(client, TW2804_REG_RED_BALANCE, ctrl->val, 0);
  215. case V4L2_CID_BRIGHTNESS:
  216. return write_reg(client, TW2804_REG_BRIGHTNESS,
  217. ctrl->val, state->channel);
  218. case V4L2_CID_CONTRAST:
  219. return write_reg(client, TW2804_REG_CONTRAST,
  220. ctrl->val, state->channel);
  221. case V4L2_CID_SATURATION:
  222. return write_reg(client, TW2804_REG_SATURATION,
  223. ctrl->val, state->channel);
  224. case V4L2_CID_HUE:
  225. return write_reg(client, TW2804_REG_HUE,
  226. ctrl->val, state->channel);
  227. default:
  228. break;
  229. }
  230. return -EINVAL;
  231. }
  232. static int tw2804_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  233. {
  234. struct tw2804 *dec = to_state(sd);
  235. struct i2c_client *client = v4l2_get_subdevdata(sd);
  236. bool is_60hz = norm & V4L2_STD_525_60;
  237. u8 regs[] = {
  238. 0x01, is_60hz ? 0xc4 : 0x84,
  239. 0x09, is_60hz ? 0x07 : 0x04,
  240. 0x0a, is_60hz ? 0xf0 : 0x20,
  241. 0x0b, is_60hz ? 0x07 : 0x04,
  242. 0x0c, is_60hz ? 0xf0 : 0x20,
  243. 0x0d, is_60hz ? 0x40 : 0x4a,
  244. 0x16, is_60hz ? 0x00 : 0x40,
  245. 0x17, is_60hz ? 0x00 : 0x40,
  246. 0x20, is_60hz ? 0x07 : 0x0f,
  247. 0x21, is_60hz ? 0x07 : 0x0f,
  248. 0xff, 0xff,
  249. };
  250. write_regs(client, regs, dec->channel);
  251. dec->norm = norm;
  252. return 0;
  253. }
  254. static int tw2804_s_video_routing(struct v4l2_subdev *sd, u32 input, u32 output,
  255. u32 config)
  256. {
  257. struct tw2804 *dec = to_state(sd);
  258. struct i2c_client *client = v4l2_get_subdevdata(sd);
  259. int reg;
  260. if (config && config - 1 != dec->channel) {
  261. if (config > 4) {
  262. dev_err(&client->dev,
  263. "channel %d is not between 1 and 4!\n", config);
  264. return -EINVAL;
  265. }
  266. dec->channel = config - 1;
  267. dev_dbg(&client->dev, "initializing TW2804 channel %d\n",
  268. dec->channel);
  269. if (dec->channel == 0 &&
  270. write_regs(client, global_registers, 0) < 0) {
  271. dev_err(&client->dev,
  272. "error initializing TW2804 global registers\n");
  273. return -EIO;
  274. }
  275. if (write_regs(client, channel_registers, dec->channel) < 0) {
  276. dev_err(&client->dev,
  277. "error initializing TW2804 channel %d\n",
  278. dec->channel);
  279. return -EIO;
  280. }
  281. }
  282. if (input > 1)
  283. return -EINVAL;
  284. if (input == dec->input)
  285. return 0;
  286. reg = read_reg(client, 0x22, dec->channel);
  287. if (reg >= 0) {
  288. if (input == 0)
  289. reg &= ~(1 << 2);
  290. else
  291. reg |= 1 << 2;
  292. reg = write_reg(client, 0x22, reg, dec->channel);
  293. }
  294. if (reg >= 0)
  295. dec->input = input;
  296. else
  297. return reg;
  298. return 0;
  299. }
  300. static const struct v4l2_ctrl_ops tw2804_ctrl_ops = {
  301. .g_volatile_ctrl = tw2804_g_volatile_ctrl,
  302. .s_ctrl = tw2804_s_ctrl,
  303. };
  304. static const struct v4l2_subdev_video_ops tw2804_video_ops = {
  305. .s_routing = tw2804_s_video_routing,
  306. };
  307. static const struct v4l2_subdev_core_ops tw2804_core_ops = {
  308. .log_status = tw2804_log_status,
  309. .s_std = tw2804_s_std,
  310. };
  311. static const struct v4l2_subdev_ops tw2804_ops = {
  312. .core = &tw2804_core_ops,
  313. .video = &tw2804_video_ops,
  314. };
  315. static int tw2804_probe(struct i2c_client *client,
  316. const struct i2c_device_id *id)
  317. {
  318. struct i2c_adapter *adapter = client->adapter;
  319. struct tw2804 *state;
  320. struct v4l2_subdev *sd;
  321. struct v4l2_ctrl *ctrl;
  322. int err;
  323. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  324. return -ENODEV;
  325. state = kzalloc(sizeof(struct tw2804), GFP_KERNEL);
  326. if (state == NULL)
  327. return -ENOMEM;
  328. sd = &state->sd;
  329. v4l2_i2c_subdev_init(sd, client, &tw2804_ops);
  330. state->channel = -1;
  331. state->norm = V4L2_STD_NTSC;
  332. v4l2_ctrl_handler_init(&state->hdl, 10);
  333. v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
  334. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  335. v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
  336. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  337. v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
  338. V4L2_CID_SATURATION, 0, 255, 1, 128);
  339. v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
  340. V4L2_CID_HUE, 0, 255, 1, 128);
  341. v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
  342. V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
  343. v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
  344. V4L2_CID_AUTOGAIN, 0, 1, 1, 0);
  345. ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
  346. V4L2_CID_GAIN, 0, 255, 1, 128);
  347. if (ctrl)
  348. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  349. ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
  350. V4L2_CID_CHROMA_GAIN, 0, 255, 1, 128);
  351. if (ctrl)
  352. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  353. ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
  354. V4L2_CID_BLUE_BALANCE, 0, 255, 1, 122);
  355. if (ctrl)
  356. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  357. ctrl = v4l2_ctrl_new_std(&state->hdl, &tw2804_ctrl_ops,
  358. V4L2_CID_RED_BALANCE, 0, 255, 1, 122);
  359. if (ctrl)
  360. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  361. sd->ctrl_handler = &state->hdl;
  362. err = state->hdl.error;
  363. if (err) {
  364. v4l2_ctrl_handler_free(&state->hdl);
  365. kfree(state);
  366. return err;
  367. }
  368. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  369. client->addr << 1, client->adapter->name);
  370. return 0;
  371. }
  372. static int tw2804_remove(struct i2c_client *client)
  373. {
  374. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  375. struct tw2804 *state = to_state(sd);
  376. v4l2_device_unregister_subdev(sd);
  377. v4l2_ctrl_handler_free(&state->hdl);
  378. kfree(state);
  379. return 0;
  380. }
  381. static const struct i2c_device_id tw2804_id[] = {
  382. { "tw2804", 0 },
  383. { }
  384. };
  385. MODULE_DEVICE_TABLE(i2c, tw2804_id);
  386. static struct i2c_driver tw2804_driver = {
  387. .driver = {
  388. .name = "tw2804",
  389. },
  390. .probe = tw2804_probe,
  391. .remove = tw2804_remove,
  392. .id_table = tw2804_id,
  393. };
  394. module_i2c_driver(tw2804_driver);
  395. MODULE_LICENSE("GPL v2");
  396. MODULE_DESCRIPTION("TW2804/TW2802 V4L2 i2c driver");
  397. MODULE_AUTHOR("Micronas USA Inc");