tw2804.c 11 KB

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