tw2804.c 11 KB

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