ths7303.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * ths7303/53- THS7303/53 Video Amplifier driver
  3. *
  4. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  5. * Copyright 2013 Cisco Systems, Inc. and/or its affiliates.
  6. *
  7. * Author: Chaithrika U S <chaithrika@ti.com>
  8. *
  9. * Contributors:
  10. * Hans Verkuil <hverkuil@kernel.org>
  11. * Lad, Prabhakar <prabhakar.lad@ti.com>
  12. * Martin Bugge <marbugge@cisco.com>
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation version 2.
  17. *
  18. * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  19. * kind, whether express or implied; without even the implied warranty
  20. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include <linux/i2c.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <media/i2c/ths7303.h>
  27. #include <media/v4l2-device.h>
  28. #define THS7303_CHANNEL_1 1
  29. #define THS7303_CHANNEL_2 2
  30. #define THS7303_CHANNEL_3 3
  31. struct ths7303_state {
  32. struct v4l2_subdev sd;
  33. const struct ths7303_platform_data *pdata;
  34. struct v4l2_bt_timings bt;
  35. int std_id;
  36. int stream_on;
  37. };
  38. enum ths7303_filter_mode {
  39. THS7303_FILTER_MODE_480I_576I,
  40. THS7303_FILTER_MODE_480P_576P,
  41. THS7303_FILTER_MODE_720P_1080I,
  42. THS7303_FILTER_MODE_1080P,
  43. THS7303_FILTER_MODE_DISABLE
  44. };
  45. MODULE_DESCRIPTION("TI THS7303 video amplifier driver");
  46. MODULE_AUTHOR("Chaithrika U S");
  47. MODULE_LICENSE("GPL");
  48. static inline struct ths7303_state *to_state(struct v4l2_subdev *sd)
  49. {
  50. return container_of(sd, struct ths7303_state, sd);
  51. }
  52. static int ths7303_read(struct v4l2_subdev *sd, u8 reg)
  53. {
  54. struct i2c_client *client = v4l2_get_subdevdata(sd);
  55. return i2c_smbus_read_byte_data(client, reg);
  56. }
  57. static int ths7303_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  58. {
  59. struct i2c_client *client = v4l2_get_subdevdata(sd);
  60. int ret;
  61. int i;
  62. for (i = 0; i < 3; i++) {
  63. ret = i2c_smbus_write_byte_data(client, reg, val);
  64. if (ret == 0)
  65. return 0;
  66. }
  67. return ret;
  68. }
  69. /* following function is used to set ths7303 */
  70. static int ths7303_setval(struct v4l2_subdev *sd,
  71. enum ths7303_filter_mode mode)
  72. {
  73. struct i2c_client *client = v4l2_get_subdevdata(sd);
  74. struct ths7303_state *state = to_state(sd);
  75. const struct ths7303_platform_data *pdata = state->pdata;
  76. u8 val, sel = 0;
  77. int err, disable = 0;
  78. if (!client)
  79. return -EINVAL;
  80. switch (mode) {
  81. case THS7303_FILTER_MODE_1080P:
  82. sel = 0x3; /*1080p and SXGA/UXGA */
  83. break;
  84. case THS7303_FILTER_MODE_720P_1080I:
  85. sel = 0x2; /*720p, 1080i and SVGA/XGA */
  86. break;
  87. case THS7303_FILTER_MODE_480P_576P:
  88. sel = 0x1; /* EDTV 480p/576p and VGA */
  89. break;
  90. case THS7303_FILTER_MODE_480I_576I:
  91. sel = 0x0; /* SDTV, S-Video, 480i/576i */
  92. break;
  93. default:
  94. /* disable all channels */
  95. disable = 1;
  96. }
  97. val = (sel << 6) | (sel << 3);
  98. if (!disable)
  99. val |= (pdata->ch_1 & 0x27);
  100. err = ths7303_write(sd, THS7303_CHANNEL_1, val);
  101. if (err)
  102. goto out;
  103. val = (sel << 6) | (sel << 3);
  104. if (!disable)
  105. val |= (pdata->ch_2 & 0x27);
  106. err = ths7303_write(sd, THS7303_CHANNEL_2, val);
  107. if (err)
  108. goto out;
  109. val = (sel << 6) | (sel << 3);
  110. if (!disable)
  111. val |= (pdata->ch_3 & 0x27);
  112. err = ths7303_write(sd, THS7303_CHANNEL_3, val);
  113. if (err)
  114. goto out;
  115. return 0;
  116. out:
  117. pr_info("write byte data failed\n");
  118. return err;
  119. }
  120. static int ths7303_s_std_output(struct v4l2_subdev *sd, v4l2_std_id norm)
  121. {
  122. struct ths7303_state *state = to_state(sd);
  123. if (norm & (V4L2_STD_ALL & ~V4L2_STD_SECAM)) {
  124. state->std_id = 1;
  125. state->bt.pixelclock = 0;
  126. return ths7303_setval(sd, THS7303_FILTER_MODE_480I_576I);
  127. }
  128. return ths7303_setval(sd, THS7303_FILTER_MODE_DISABLE);
  129. }
  130. static int ths7303_config(struct v4l2_subdev *sd)
  131. {
  132. struct ths7303_state *state = to_state(sd);
  133. int res;
  134. if (!state->stream_on) {
  135. ths7303_write(sd, THS7303_CHANNEL_1,
  136. (ths7303_read(sd, THS7303_CHANNEL_1) & 0xf8) |
  137. 0x00);
  138. ths7303_write(sd, THS7303_CHANNEL_2,
  139. (ths7303_read(sd, THS7303_CHANNEL_2) & 0xf8) |
  140. 0x00);
  141. ths7303_write(sd, THS7303_CHANNEL_3,
  142. (ths7303_read(sd, THS7303_CHANNEL_3) & 0xf8) |
  143. 0x00);
  144. return 0;
  145. }
  146. if (state->bt.pixelclock > 120000000)
  147. res = ths7303_setval(sd, THS7303_FILTER_MODE_1080P);
  148. else if (state->bt.pixelclock > 70000000)
  149. res = ths7303_setval(sd, THS7303_FILTER_MODE_720P_1080I);
  150. else if (state->bt.pixelclock > 20000000)
  151. res = ths7303_setval(sd, THS7303_FILTER_MODE_480P_576P);
  152. else if (state->std_id)
  153. res = ths7303_setval(sd, THS7303_FILTER_MODE_480I_576I);
  154. else
  155. /* disable all channels */
  156. res = ths7303_setval(sd, THS7303_FILTER_MODE_DISABLE);
  157. return res;
  158. }
  159. static int ths7303_s_stream(struct v4l2_subdev *sd, int enable)
  160. {
  161. struct ths7303_state *state = to_state(sd);
  162. state->stream_on = enable;
  163. return ths7303_config(sd);
  164. }
  165. /* for setting filter for HD output */
  166. static int ths7303_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
  167. struct v4l2_dv_timings *dv_timings)
  168. {
  169. struct ths7303_state *state = to_state(sd);
  170. if (!dv_timings || dv_timings->type != V4L2_DV_BT_656_1120)
  171. return -EINVAL;
  172. state->bt = dv_timings->bt;
  173. state->std_id = 0;
  174. return ths7303_config(sd);
  175. }
  176. static const struct v4l2_subdev_video_ops ths7303_video_ops = {
  177. .s_stream = ths7303_s_stream,
  178. .s_std_output = ths7303_s_std_output,
  179. };
  180. #ifdef CONFIG_VIDEO_ADV_DEBUG
  181. static int ths7303_g_register(struct v4l2_subdev *sd,
  182. struct v4l2_dbg_register *reg)
  183. {
  184. reg->size = 1;
  185. reg->val = ths7303_read(sd, reg->reg);
  186. return 0;
  187. }
  188. static int ths7303_s_register(struct v4l2_subdev *sd,
  189. const struct v4l2_dbg_register *reg)
  190. {
  191. ths7303_write(sd, reg->reg, reg->val);
  192. return 0;
  193. }
  194. #endif
  195. static const char * const stc_lpf_sel_txt[4] = {
  196. "500-kHz Filter",
  197. "2.5-MHz Filter",
  198. "5-MHz Filter",
  199. "5-MHz Filter",
  200. };
  201. static const char * const in_mux_sel_txt[2] = {
  202. "Input A Select",
  203. "Input B Select",
  204. };
  205. static const char * const lpf_freq_sel_txt[4] = {
  206. "9-MHz LPF",
  207. "16-MHz LPF",
  208. "35-MHz LPF",
  209. "Bypass LPF",
  210. };
  211. static const char * const in_bias_sel_dis_cont_txt[8] = {
  212. "Disable Channel",
  213. "Mute Function - No Output",
  214. "DC Bias Select",
  215. "DC Bias + 250 mV Offset Select",
  216. "AC Bias Select",
  217. "Sync Tip Clamp with low bias",
  218. "Sync Tip Clamp with mid bias",
  219. "Sync Tip Clamp with high bias",
  220. };
  221. static void ths7303_log_channel_status(struct v4l2_subdev *sd, u8 reg)
  222. {
  223. u8 val = ths7303_read(sd, reg);
  224. if ((val & 0x7) == 0) {
  225. v4l2_info(sd, "Channel %d Off\n", reg);
  226. return;
  227. }
  228. v4l2_info(sd, "Channel %d On\n", reg);
  229. v4l2_info(sd, " value 0x%x\n", val);
  230. v4l2_info(sd, " %s\n", stc_lpf_sel_txt[(val >> 6) & 0x3]);
  231. v4l2_info(sd, " %s\n", in_mux_sel_txt[(val >> 5) & 0x1]);
  232. v4l2_info(sd, " %s\n", lpf_freq_sel_txt[(val >> 3) & 0x3]);
  233. v4l2_info(sd, " %s\n", in_bias_sel_dis_cont_txt[(val >> 0) & 0x7]);
  234. }
  235. static int ths7303_log_status(struct v4l2_subdev *sd)
  236. {
  237. struct ths7303_state *state = to_state(sd);
  238. v4l2_info(sd, "stream %s\n", state->stream_on ? "On" : "Off");
  239. if (state->bt.pixelclock) {
  240. struct v4l2_bt_timings *bt = &state->bt;
  241. u32 frame_width, frame_height;
  242. frame_width = V4L2_DV_BT_FRAME_WIDTH(bt);
  243. frame_height = V4L2_DV_BT_FRAME_HEIGHT(bt);
  244. v4l2_info(sd,
  245. "timings: %dx%d%s%d (%dx%d). Pix freq. = %d Hz. Polarities = 0x%x\n",
  246. bt->width, bt->height, bt->interlaced ? "i" : "p",
  247. (frame_height * frame_width) > 0 ?
  248. (int)bt->pixelclock /
  249. (frame_height * frame_width) : 0,
  250. frame_width, frame_height,
  251. (int)bt->pixelclock, bt->polarities);
  252. } else {
  253. v4l2_info(sd, "no timings set\n");
  254. }
  255. ths7303_log_channel_status(sd, THS7303_CHANNEL_1);
  256. ths7303_log_channel_status(sd, THS7303_CHANNEL_2);
  257. ths7303_log_channel_status(sd, THS7303_CHANNEL_3);
  258. return 0;
  259. }
  260. static const struct v4l2_subdev_core_ops ths7303_core_ops = {
  261. .log_status = ths7303_log_status,
  262. #ifdef CONFIG_VIDEO_ADV_DEBUG
  263. .g_register = ths7303_g_register,
  264. .s_register = ths7303_s_register,
  265. #endif
  266. };
  267. static const struct v4l2_subdev_pad_ops ths7303_pad_ops = {
  268. .s_dv_timings = ths7303_s_dv_timings,
  269. };
  270. static const struct v4l2_subdev_ops ths7303_ops = {
  271. .core = &ths7303_core_ops,
  272. .video = &ths7303_video_ops,
  273. .pad = &ths7303_pad_ops,
  274. };
  275. static int ths7303_probe(struct i2c_client *client)
  276. {
  277. struct ths7303_platform_data *pdata = client->dev.platform_data;
  278. struct ths7303_state *state;
  279. struct v4l2_subdev *sd;
  280. if (pdata == NULL) {
  281. dev_err(&client->dev, "No platform data\n");
  282. return -EINVAL;
  283. }
  284. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  285. return -ENODEV;
  286. v4l_info(client, "chip found @ 0x%x (%s)\n",
  287. client->addr << 1, client->adapter->name);
  288. state = devm_kzalloc(&client->dev, sizeof(struct ths7303_state),
  289. GFP_KERNEL);
  290. if (!state)
  291. return -ENOMEM;
  292. state->pdata = pdata;
  293. sd = &state->sd;
  294. v4l2_i2c_subdev_init(sd, client, &ths7303_ops);
  295. /* set to default 480I_576I filter mode */
  296. if (ths7303_setval(sd, THS7303_FILTER_MODE_480I_576I) < 0) {
  297. v4l_err(client, "Setting to 480I_576I filter mode failed!\n");
  298. return -EINVAL;
  299. }
  300. return 0;
  301. }
  302. static void ths7303_remove(struct i2c_client *client)
  303. {
  304. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  305. v4l2_device_unregister_subdev(sd);
  306. }
  307. static const struct i2c_device_id ths7303_id[] = {
  308. { "ths7303" },
  309. { "ths7353" },
  310. {}
  311. };
  312. MODULE_DEVICE_TABLE(i2c, ths7303_id);
  313. static struct i2c_driver ths7303_driver = {
  314. .driver = {
  315. .name = "ths73x3",
  316. },
  317. .probe = ths7303_probe,
  318. .remove = ths7303_remove,
  319. .id_table = ths7303_id,
  320. };
  321. module_i2c_driver(ths7303_driver);