tw2804.c 10 KB

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