ths7303.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 <hans.verkuil@cisco.com>
  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/ths7303.h>
  27. #include <media/v4l2-chip-ident.h>
  28. #include <media/v4l2-device.h>
  29. #define THS7303_CHANNEL_1 1
  30. #define THS7303_CHANNEL_2 2
  31. #define THS7303_CHANNEL_3 3
  32. struct ths7303_state {
  33. struct v4l2_subdev sd;
  34. struct ths7303_platform_data pdata;
  35. struct v4l2_bt_timings bt;
  36. int std_id;
  37. int stream_on;
  38. int driver_data;
  39. };
  40. enum ths7303_filter_mode {
  41. THS7303_FILTER_MODE_480I_576I,
  42. THS7303_FILTER_MODE_480P_576P,
  43. THS7303_FILTER_MODE_720P_1080I,
  44. THS7303_FILTER_MODE_1080P,
  45. THS7303_FILTER_MODE_DISABLE
  46. };
  47. MODULE_DESCRIPTION("TI THS7303 video amplifier driver");
  48. MODULE_AUTHOR("Chaithrika U S");
  49. MODULE_LICENSE("GPL");
  50. static int debug;
  51. module_param(debug, int, 0644);
  52. MODULE_PARM_DESC(debug, "Debug level 0-1");
  53. static inline struct ths7303_state *to_state(struct v4l2_subdev *sd)
  54. {
  55. return container_of(sd, struct ths7303_state, sd);
  56. }
  57. static int ths7303_read(struct v4l2_subdev *sd, u8 reg)
  58. {
  59. struct i2c_client *client = v4l2_get_subdevdata(sd);
  60. return i2c_smbus_read_byte_data(client, reg);
  61. }
  62. static int ths7303_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  63. {
  64. struct i2c_client *client = v4l2_get_subdevdata(sd);
  65. int ret;
  66. int i;
  67. for (i = 0; i < 3; i++) {
  68. ret = i2c_smbus_write_byte_data(client, reg, val);
  69. if (ret == 0)
  70. return 0;
  71. }
  72. return ret;
  73. }
  74. /* following function is used to set ths7303 */
  75. int ths7303_setval(struct v4l2_subdev *sd, enum ths7303_filter_mode mode)
  76. {
  77. struct i2c_client *client = v4l2_get_subdevdata(sd);
  78. struct ths7303_state *state = to_state(sd);
  79. struct ths7303_platform_data *pdata = &state->pdata;
  80. u8 val, sel = 0;
  81. int err, disable = 0;
  82. if (!client)
  83. return -EINVAL;
  84. switch (mode) {
  85. case THS7303_FILTER_MODE_1080P:
  86. sel = 0x3; /*1080p and SXGA/UXGA */
  87. break;
  88. case THS7303_FILTER_MODE_720P_1080I:
  89. sel = 0x2; /*720p, 1080i and SVGA/XGA */
  90. break;
  91. case THS7303_FILTER_MODE_480P_576P:
  92. sel = 0x1; /* EDTV 480p/576p and VGA */
  93. break;
  94. case THS7303_FILTER_MODE_480I_576I:
  95. sel = 0x0; /* SDTV, S-Video, 480i/576i */
  96. break;
  97. default:
  98. /* disable all channels */
  99. disable = 1;
  100. }
  101. val = (sel << 6) | (sel << 3);
  102. if (!disable)
  103. val |= (pdata->ch_1 & 0x27);
  104. err = ths7303_write(sd, THS7303_CHANNEL_1, val);
  105. if (err)
  106. goto out;
  107. val = (sel << 6) | (sel << 3);
  108. if (!disable)
  109. val |= (pdata->ch_2 & 0x27);
  110. err = ths7303_write(sd, THS7303_CHANNEL_2, val);
  111. if (err)
  112. goto out;
  113. val = (sel << 6) | (sel << 3);
  114. if (!disable)
  115. val |= (pdata->ch_3 & 0x27);
  116. err = ths7303_write(sd, THS7303_CHANNEL_3, val);
  117. if (err)
  118. goto out;
  119. return 0;
  120. out:
  121. pr_info("write byte data failed\n");
  122. return err;
  123. }
  124. static int ths7303_s_std_output(struct v4l2_subdev *sd, v4l2_std_id norm)
  125. {
  126. struct ths7303_state *state = to_state(sd);
  127. if (norm & (V4L2_STD_ALL & ~V4L2_STD_SECAM)) {
  128. state->std_id = 1;
  129. state->bt.pixelclock = 0;
  130. return ths7303_setval(sd, THS7303_FILTER_MODE_480I_576I);
  131. }
  132. return ths7303_setval(sd, THS7303_FILTER_MODE_DISABLE);
  133. }
  134. static int ths7303_config(struct v4l2_subdev *sd)
  135. {
  136. struct ths7303_state *state = to_state(sd);
  137. int res;
  138. if (!state->stream_on) {
  139. ths7303_write(sd, THS7303_CHANNEL_1,
  140. (ths7303_read(sd, THS7303_CHANNEL_1) & 0xf8) |
  141. 0x00);
  142. ths7303_write(sd, THS7303_CHANNEL_2,
  143. (ths7303_read(sd, THS7303_CHANNEL_2) & 0xf8) |
  144. 0x00);
  145. ths7303_write(sd, THS7303_CHANNEL_3,
  146. (ths7303_read(sd, THS7303_CHANNEL_3) & 0xf8) |
  147. 0x00);
  148. return 0;
  149. }
  150. if (state->bt.pixelclock > 120000000)
  151. res = ths7303_setval(sd, THS7303_FILTER_MODE_1080P);
  152. else if (state->bt.pixelclock > 70000000)
  153. res = ths7303_setval(sd, THS7303_FILTER_MODE_720P_1080I);
  154. else if (state->bt.pixelclock > 20000000)
  155. res = ths7303_setval(sd, THS7303_FILTER_MODE_480P_576P);
  156. else if (state->std_id)
  157. res = ths7303_setval(sd, THS7303_FILTER_MODE_480I_576I);
  158. else
  159. /* disable all channels */
  160. res = ths7303_setval(sd, THS7303_FILTER_MODE_DISABLE);
  161. return res;
  162. }
  163. static int ths7303_s_stream(struct v4l2_subdev *sd, int enable)
  164. {
  165. struct ths7303_state *state = to_state(sd);
  166. state->stream_on = enable;
  167. return ths7303_config(sd);
  168. }
  169. /* for setting filter for HD output */
  170. static int ths7303_s_dv_timings(struct v4l2_subdev *sd,
  171. struct v4l2_dv_timings *dv_timings)
  172. {
  173. struct ths7303_state *state = to_state(sd);
  174. if (!dv_timings || dv_timings->type != V4L2_DV_BT_656_1120)
  175. return -EINVAL;
  176. state->bt = dv_timings->bt;
  177. state->std_id = 0;
  178. return ths7303_config(sd);
  179. }
  180. static int ths7303_g_chip_ident(struct v4l2_subdev *sd,
  181. struct v4l2_dbg_chip_ident *chip)
  182. {
  183. struct i2c_client *client = v4l2_get_subdevdata(sd);
  184. struct ths7303_state *state = to_state(sd);
  185. return v4l2_chip_ident_i2c_client(client, chip, state->driver_data, 0);
  186. }
  187. static const struct v4l2_subdev_video_ops ths7303_video_ops = {
  188. .s_stream = ths7303_s_stream,
  189. .s_std_output = ths7303_s_std_output,
  190. .s_dv_timings = ths7303_s_dv_timings,
  191. };
  192. #ifdef CONFIG_VIDEO_ADV_DEBUG
  193. static int ths7303_g_register(struct v4l2_subdev *sd,
  194. struct v4l2_dbg_register *reg)
  195. {
  196. struct i2c_client *client = v4l2_get_subdevdata(sd);
  197. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  198. return -EINVAL;
  199. if (!capable(CAP_SYS_ADMIN))
  200. return -EPERM;
  201. reg->size = 1;
  202. reg->val = ths7303_read(sd, reg->reg);
  203. return 0;
  204. }
  205. static int ths7303_s_register(struct v4l2_subdev *sd,
  206. const struct v4l2_dbg_register *reg)
  207. {
  208. struct i2c_client *client = v4l2_get_subdevdata(sd);
  209. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  210. return -EINVAL;
  211. if (!capable(CAP_SYS_ADMIN))
  212. return -EPERM;
  213. ths7303_write(sd, reg->reg, reg->val);
  214. return 0;
  215. }
  216. #endif
  217. static const char * const stc_lpf_sel_txt[4] = {
  218. "500-kHz Filter",
  219. "2.5-MHz Filter",
  220. "5-MHz Filter",
  221. "5-MHz Filter",
  222. };
  223. static const char * const in_mux_sel_txt[2] = {
  224. "Input A Select",
  225. "Input B Select",
  226. };
  227. static const char * const lpf_freq_sel_txt[4] = {
  228. "9-MHz LPF",
  229. "16-MHz LPF",
  230. "35-MHz LPF",
  231. "Bypass LPF",
  232. };
  233. static const char * const in_bias_sel_dis_cont_txt[8] = {
  234. "Disable Channel",
  235. "Mute Function - No Output",
  236. "DC Bias Select",
  237. "DC Bias + 250 mV Offset Select",
  238. "AC Bias Select",
  239. "Sync Tip Clamp with low bias",
  240. "Sync Tip Clamp with mid bias",
  241. "Sync Tip Clamp with high bias",
  242. };
  243. static void ths7303_log_channel_status(struct v4l2_subdev *sd, u8 reg)
  244. {
  245. u8 val = ths7303_read(sd, reg);
  246. if ((val & 0x7) == 0) {
  247. v4l2_info(sd, "Channel %d Off\n", reg);
  248. return;
  249. }
  250. v4l2_info(sd, "Channel %d On\n", reg);
  251. v4l2_info(sd, " value 0x%x\n", val);
  252. v4l2_info(sd, " %s\n", stc_lpf_sel_txt[(val >> 6) & 0x3]);
  253. v4l2_info(sd, " %s\n", in_mux_sel_txt[(val >> 5) & 0x1]);
  254. v4l2_info(sd, " %s\n", lpf_freq_sel_txt[(val >> 3) & 0x3]);
  255. v4l2_info(sd, " %s\n", in_bias_sel_dis_cont_txt[(val >> 0) & 0x7]);
  256. }
  257. static int ths7303_log_status(struct v4l2_subdev *sd)
  258. {
  259. struct ths7303_state *state = to_state(sd);
  260. v4l2_info(sd, "stream %s\n", state->stream_on ? "On" : "Off");
  261. if (state->bt.pixelclock) {
  262. struct v4l2_bt_timings *bt = bt = &state->bt;
  263. u32 frame_width, frame_height;
  264. frame_width = bt->width + bt->hfrontporch +
  265. bt->hsync + bt->hbackporch;
  266. frame_height = bt->height + bt->vfrontporch +
  267. bt->vsync + bt->vbackporch;
  268. v4l2_info(sd,
  269. "timings: %dx%d%s%d (%dx%d). Pix freq. = %d Hz. Polarities = 0x%x\n",
  270. bt->width, bt->height, bt->interlaced ? "i" : "p",
  271. (frame_height * frame_width) > 0 ?
  272. (int)bt->pixelclock /
  273. (frame_height * frame_width) : 0,
  274. frame_width, frame_height,
  275. (int)bt->pixelclock, bt->polarities);
  276. } else {
  277. v4l2_info(sd, "no timings set\n");
  278. }
  279. ths7303_log_channel_status(sd, THS7303_CHANNEL_1);
  280. ths7303_log_channel_status(sd, THS7303_CHANNEL_2);
  281. ths7303_log_channel_status(sd, THS7303_CHANNEL_3);
  282. return 0;
  283. }
  284. static const struct v4l2_subdev_core_ops ths7303_core_ops = {
  285. .g_chip_ident = ths7303_g_chip_ident,
  286. .log_status = ths7303_log_status,
  287. #ifdef CONFIG_VIDEO_ADV_DEBUG
  288. .g_register = ths7303_g_register,
  289. .s_register = ths7303_s_register,
  290. #endif
  291. };
  292. static const struct v4l2_subdev_ops ths7303_ops = {
  293. .core = &ths7303_core_ops,
  294. .video = &ths7303_video_ops,
  295. };
  296. static int ths7303_setup(struct v4l2_subdev *sd)
  297. {
  298. struct ths7303_state *state = to_state(sd);
  299. struct ths7303_platform_data *pdata = &state->pdata;
  300. int ret;
  301. u8 mask;
  302. state->stream_on = pdata->init_enable;
  303. mask = state->stream_on ? 0xff : 0xf8;
  304. ret = ths7303_write(sd, THS7303_CHANNEL_1, pdata->ch_1 & mask);
  305. if (ret)
  306. return ret;
  307. ret = ths7303_write(sd, THS7303_CHANNEL_2, pdata->ch_2 & mask);
  308. if (ret)
  309. return ret;
  310. ret = ths7303_write(sd, THS7303_CHANNEL_3, pdata->ch_3 & mask);
  311. if (ret)
  312. return ret;
  313. return 0;
  314. }
  315. static int ths7303_probe(struct i2c_client *client,
  316. const struct i2c_device_id *id)
  317. {
  318. struct ths7303_platform_data *pdata = client->dev.platform_data;
  319. struct ths7303_state *state;
  320. struct v4l2_subdev *sd;
  321. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  322. return -ENODEV;
  323. v4l_info(client, "chip found @ 0x%x (%s)\n",
  324. client->addr << 1, client->adapter->name);
  325. state = devm_kzalloc(&client->dev, sizeof(struct ths7303_state),
  326. GFP_KERNEL);
  327. if (!state)
  328. return -ENOMEM;
  329. if (!pdata)
  330. v4l_warn(client, "No platform data, using default data!\n");
  331. else
  332. state->pdata = *pdata;
  333. sd = &state->sd;
  334. v4l2_i2c_subdev_init(sd, client, &ths7303_ops);
  335. /* store the driver data to differntiate the chip */
  336. state->driver_data = (int)id->driver_data;
  337. if (ths7303_setup(sd) < 0) {
  338. v4l_err(client, "init failed\n");
  339. return -EIO;
  340. }
  341. return 0;
  342. }
  343. static int ths7303_remove(struct i2c_client *client)
  344. {
  345. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  346. v4l2_device_unregister_subdev(sd);
  347. return 0;
  348. }
  349. static const struct i2c_device_id ths7303_id[] = {
  350. {"ths7303", V4L2_IDENT_THS7303},
  351. {"ths7353", V4L2_IDENT_THS7353},
  352. {},
  353. };
  354. MODULE_DEVICE_TABLE(i2c, ths7303_id);
  355. static struct i2c_driver ths7303_driver = {
  356. .driver = {
  357. .owner = THIS_MODULE,
  358. .name = "ths73x3",
  359. },
  360. .probe = ths7303_probe,
  361. .remove = ths7303_remove,
  362. .id_table = ths7303_id,
  363. };
  364. module_i2c_driver(ths7303_driver);