adv7343.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * adv7343 - ADV7343 Video Encoder Driver
  3. *
  4. * The encoder hardware does not support SECAM.
  5. *
  6. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/ctype.h>
  20. #include <linux/i2c.h>
  21. #include <linux/device.h>
  22. #include <linux/delay.h>
  23. #include <linux/module.h>
  24. #include <linux/videodev2.h>
  25. #include <linux/uaccess.h>
  26. #include <media/adv7343.h>
  27. #include <media/v4l2-device.h>
  28. #include <media/v4l2-chip-ident.h>
  29. #include "adv7343_regs.h"
  30. MODULE_DESCRIPTION("ADV7343 video encoder driver");
  31. MODULE_LICENSE("GPL");
  32. static int debug;
  33. module_param(debug, int, 0644);
  34. MODULE_PARM_DESC(debug, "Debug level 0-1");
  35. struct adv7343_state {
  36. struct v4l2_subdev sd;
  37. u8 reg00;
  38. u8 reg01;
  39. u8 reg02;
  40. u8 reg35;
  41. u8 reg80;
  42. u8 reg82;
  43. int bright;
  44. int hue;
  45. int gain;
  46. u32 output;
  47. v4l2_std_id std;
  48. };
  49. static inline struct adv7343_state *to_state(struct v4l2_subdev *sd)
  50. {
  51. return container_of(sd, struct adv7343_state, sd);
  52. }
  53. static inline int adv7343_write(struct v4l2_subdev *sd, u8 reg, u8 value)
  54. {
  55. struct i2c_client *client = v4l2_get_subdevdata(sd);
  56. return i2c_smbus_write_byte_data(client, reg, value);
  57. }
  58. static const u8 adv7343_init_reg_val[] = {
  59. ADV7343_SOFT_RESET, ADV7343_SOFT_RESET_DEFAULT,
  60. ADV7343_POWER_MODE_REG, ADV7343_POWER_MODE_REG_DEFAULT,
  61. ADV7343_HD_MODE_REG1, ADV7343_HD_MODE_REG1_DEFAULT,
  62. ADV7343_HD_MODE_REG2, ADV7343_HD_MODE_REG2_DEFAULT,
  63. ADV7343_HD_MODE_REG3, ADV7343_HD_MODE_REG3_DEFAULT,
  64. ADV7343_HD_MODE_REG4, ADV7343_HD_MODE_REG4_DEFAULT,
  65. ADV7343_HD_MODE_REG5, ADV7343_HD_MODE_REG5_DEFAULT,
  66. ADV7343_HD_MODE_REG6, ADV7343_HD_MODE_REG6_DEFAULT,
  67. ADV7343_HD_MODE_REG7, ADV7343_HD_MODE_REG7_DEFAULT,
  68. ADV7343_SD_MODE_REG1, ADV7343_SD_MODE_REG1_DEFAULT,
  69. ADV7343_SD_MODE_REG2, ADV7343_SD_MODE_REG2_DEFAULT,
  70. ADV7343_SD_MODE_REG3, ADV7343_SD_MODE_REG3_DEFAULT,
  71. ADV7343_SD_MODE_REG4, ADV7343_SD_MODE_REG4_DEFAULT,
  72. ADV7343_SD_MODE_REG5, ADV7343_SD_MODE_REG5_DEFAULT,
  73. ADV7343_SD_MODE_REG6, ADV7343_SD_MODE_REG6_DEFAULT,
  74. ADV7343_SD_MODE_REG7, ADV7343_SD_MODE_REG7_DEFAULT,
  75. ADV7343_SD_MODE_REG8, ADV7343_SD_MODE_REG8_DEFAULT,
  76. ADV7343_SD_HUE_REG, ADV7343_SD_HUE_REG_DEFAULT,
  77. ADV7343_SD_CGMS_WSS0, ADV7343_SD_CGMS_WSS0_DEFAULT,
  78. ADV7343_SD_BRIGHTNESS_WSS, ADV7343_SD_BRIGHTNESS_WSS_DEFAULT,
  79. };
  80. /*
  81. * 2^32
  82. * FSC(reg) = FSC (HZ) * --------
  83. * 27000000
  84. */
  85. static const struct adv7343_std_info stdinfo[] = {
  86. {
  87. /* FSC(Hz) = 3,579,545.45 Hz */
  88. SD_STD_NTSC, 569408542, V4L2_STD_NTSC,
  89. }, {
  90. /* FSC(Hz) = 3,575,611.00 Hz */
  91. SD_STD_PAL_M, 568782678, V4L2_STD_PAL_M,
  92. }, {
  93. /* FSC(Hz) = 3,582,056.00 */
  94. SD_STD_PAL_N, 569807903, V4L2_STD_PAL_Nc,
  95. }, {
  96. /* FSC(Hz) = 4,433,618.75 Hz */
  97. SD_STD_PAL_N, 705268427, V4L2_STD_PAL_N,
  98. }, {
  99. /* FSC(Hz) = 4,433,618.75 Hz */
  100. SD_STD_PAL_BDGHI, 705268427, V4L2_STD_PAL,
  101. }, {
  102. /* FSC(Hz) = 4,433,618.75 Hz */
  103. SD_STD_NTSC, 705268427, V4L2_STD_NTSC_443,
  104. }, {
  105. /* FSC(Hz) = 4,433,618.75 Hz */
  106. SD_STD_PAL_M, 705268427, V4L2_STD_PAL_60,
  107. },
  108. };
  109. static int adv7343_setstd(struct v4l2_subdev *sd, v4l2_std_id std)
  110. {
  111. struct adv7343_state *state = to_state(sd);
  112. struct adv7343_std_info *std_info;
  113. int output_idx, num_std;
  114. char *fsc_ptr;
  115. u8 reg, val;
  116. int err = 0;
  117. int i = 0;
  118. output_idx = state->output;
  119. std_info = (struct adv7343_std_info *)stdinfo;
  120. num_std = ARRAY_SIZE(stdinfo);
  121. for (i = 0; i < num_std; i++) {
  122. if (std_info[i].stdid & std)
  123. break;
  124. }
  125. if (i == num_std) {
  126. v4l2_dbg(1, debug, sd,
  127. "Invalid std or std is not supported: %llx\n",
  128. (unsigned long long)std);
  129. return -EINVAL;
  130. }
  131. /* Set the standard */
  132. val = state->reg80 & (~(SD_STD_MASK));
  133. val |= std_info[i].standard_val3;
  134. err = adv7343_write(sd, ADV7343_SD_MODE_REG1, val);
  135. if (err < 0)
  136. goto setstd_exit;
  137. state->reg80 = val;
  138. /* Configure the input mode register */
  139. val = state->reg01 & (~((u8) INPUT_MODE_MASK));
  140. val |= SD_INPUT_MODE;
  141. err = adv7343_write(sd, ADV7343_MODE_SELECT_REG, val);
  142. if (err < 0)
  143. goto setstd_exit;
  144. state->reg01 = val;
  145. /* Program the sub carrier frequency registers */
  146. fsc_ptr = (unsigned char *)&std_info[i].fsc_val;
  147. reg = ADV7343_FSC_REG0;
  148. for (i = 0; i < 4; i++, reg++, fsc_ptr++) {
  149. err = adv7343_write(sd, reg, *fsc_ptr);
  150. if (err < 0)
  151. goto setstd_exit;
  152. }
  153. val = state->reg80;
  154. /* Filter settings */
  155. if (std & (V4L2_STD_NTSC | V4L2_STD_NTSC_443))
  156. val &= 0x03;
  157. else if (std & ~V4L2_STD_SECAM)
  158. val |= 0x04;
  159. err = adv7343_write(sd, ADV7343_SD_MODE_REG1, val);
  160. if (err < 0)
  161. goto setstd_exit;
  162. state->reg80 = val;
  163. setstd_exit:
  164. if (err != 0)
  165. v4l2_err(sd, "Error setting std, write failed\n");
  166. return err;
  167. }
  168. static int adv7343_setoutput(struct v4l2_subdev *sd, u32 output_type)
  169. {
  170. struct adv7343_state *state = to_state(sd);
  171. unsigned char val;
  172. int err = 0;
  173. if (output_type > ADV7343_SVIDEO_ID) {
  174. v4l2_dbg(1, debug, sd,
  175. "Invalid output type or output type not supported:%d\n",
  176. output_type);
  177. return -EINVAL;
  178. }
  179. /* Enable Appropriate DAC */
  180. val = state->reg00 & 0x03;
  181. if (output_type == ADV7343_COMPOSITE_ID)
  182. val |= ADV7343_COMPOSITE_POWER_VALUE;
  183. else if (output_type == ADV7343_COMPONENT_ID)
  184. val |= ADV7343_COMPONENT_POWER_VALUE;
  185. else
  186. val |= ADV7343_SVIDEO_POWER_VALUE;
  187. err = adv7343_write(sd, ADV7343_POWER_MODE_REG, val);
  188. if (err < 0)
  189. goto setoutput_exit;
  190. state->reg00 = val;
  191. /* Enable YUV output */
  192. val = state->reg02 | YUV_OUTPUT_SELECT;
  193. err = adv7343_write(sd, ADV7343_MODE_REG0, val);
  194. if (err < 0)
  195. goto setoutput_exit;
  196. state->reg02 = val;
  197. /* configure SD DAC Output 2 and SD DAC Output 1 bit to zero */
  198. val = state->reg82 & (SD_DAC_1_DI & SD_DAC_2_DI);
  199. err = adv7343_write(sd, ADV7343_SD_MODE_REG2, val);
  200. if (err < 0)
  201. goto setoutput_exit;
  202. state->reg82 = val;
  203. /* configure ED/HD Color DAC Swap and ED/HD RGB Input Enable bit to
  204. * zero */
  205. val = state->reg35 & (HD_RGB_INPUT_DI & HD_DAC_SWAP_DI);
  206. err = adv7343_write(sd, ADV7343_HD_MODE_REG6, val);
  207. if (err < 0)
  208. goto setoutput_exit;
  209. state->reg35 = val;
  210. setoutput_exit:
  211. if (err != 0)
  212. v4l2_err(sd, "Error setting output, write failed\n");
  213. return err;
  214. }
  215. static int adv7343_log_status(struct v4l2_subdev *sd)
  216. {
  217. struct adv7343_state *state = to_state(sd);
  218. v4l2_info(sd, "Standard: %llx\n", (unsigned long long)state->std);
  219. v4l2_info(sd, "Output: %s\n", (state->output == 0) ? "Composite" :
  220. ((state->output == 1) ? "Component" : "S-Video"));
  221. return 0;
  222. }
  223. static int adv7343_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  224. {
  225. switch (qc->id) {
  226. case V4L2_CID_BRIGHTNESS:
  227. return v4l2_ctrl_query_fill(qc, ADV7343_BRIGHTNESS_MIN,
  228. ADV7343_BRIGHTNESS_MAX, 1,
  229. ADV7343_BRIGHTNESS_DEF);
  230. case V4L2_CID_HUE:
  231. return v4l2_ctrl_query_fill(qc, ADV7343_HUE_MIN,
  232. ADV7343_HUE_MAX, 1 ,
  233. ADV7343_HUE_DEF);
  234. case V4L2_CID_GAIN:
  235. return v4l2_ctrl_query_fill(qc, ADV7343_GAIN_MIN,
  236. ADV7343_GAIN_MAX, 1,
  237. ADV7343_GAIN_DEF);
  238. default:
  239. break;
  240. }
  241. return 0;
  242. }
  243. static int adv7343_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  244. {
  245. struct adv7343_state *state = to_state(sd);
  246. int err = 0;
  247. switch (ctrl->id) {
  248. case V4L2_CID_BRIGHTNESS:
  249. if (ctrl->value < ADV7343_BRIGHTNESS_MIN ||
  250. ctrl->value > ADV7343_BRIGHTNESS_MAX) {
  251. v4l2_dbg(1, debug, sd,
  252. "invalid brightness settings %d\n",
  253. ctrl->value);
  254. return -ERANGE;
  255. }
  256. state->bright = ctrl->value;
  257. err = adv7343_write(sd, ADV7343_SD_BRIGHTNESS_WSS,
  258. state->bright);
  259. break;
  260. case V4L2_CID_HUE:
  261. if (ctrl->value < ADV7343_HUE_MIN ||
  262. ctrl->value > ADV7343_HUE_MAX) {
  263. v4l2_dbg(1, debug, sd, "invalid hue settings %d\n",
  264. ctrl->value);
  265. return -ERANGE;
  266. }
  267. state->hue = ctrl->value;
  268. err = adv7343_write(sd, ADV7343_SD_HUE_REG, state->hue);
  269. break;
  270. case V4L2_CID_GAIN:
  271. if (ctrl->value < ADV7343_GAIN_MIN ||
  272. ctrl->value > ADV7343_GAIN_MAX) {
  273. v4l2_dbg(1, debug, sd, "invalid gain settings %d\n",
  274. ctrl->value);
  275. return -ERANGE;
  276. }
  277. if ((ctrl->value > POSITIVE_GAIN_MAX) &&
  278. (ctrl->value < NEGATIVE_GAIN_MIN)) {
  279. v4l2_dbg(1, debug, sd,
  280. "gain settings not within the specified range\n");
  281. return -ERANGE;
  282. }
  283. state->gain = ctrl->value;
  284. err = adv7343_write(sd, ADV7343_DAC2_OUTPUT_LEVEL, state->gain);
  285. break;
  286. default:
  287. return -EINVAL;
  288. }
  289. if (err < 0)
  290. v4l2_err(sd, "Failed to set the encoder controls\n");
  291. return err;
  292. }
  293. static int adv7343_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  294. {
  295. struct adv7343_state *state = to_state(sd);
  296. switch (ctrl->id) {
  297. case V4L2_CID_BRIGHTNESS:
  298. ctrl->value = state->bright;
  299. break;
  300. case V4L2_CID_HUE:
  301. ctrl->value = state->hue;
  302. break;
  303. case V4L2_CID_GAIN:
  304. ctrl->value = state->gain;
  305. break;
  306. default:
  307. return -EINVAL;
  308. }
  309. return 0;
  310. }
  311. static int adv7343_g_chip_ident(struct v4l2_subdev *sd,
  312. struct v4l2_dbg_chip_ident *chip)
  313. {
  314. struct i2c_client *client = v4l2_get_subdevdata(sd);
  315. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7343, 0);
  316. }
  317. static const struct v4l2_subdev_core_ops adv7343_core_ops = {
  318. .log_status = adv7343_log_status,
  319. .g_chip_ident = adv7343_g_chip_ident,
  320. .g_ctrl = adv7343_g_ctrl,
  321. .s_ctrl = adv7343_s_ctrl,
  322. .queryctrl = adv7343_queryctrl,
  323. };
  324. static int adv7343_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  325. {
  326. struct adv7343_state *state = to_state(sd);
  327. int err = 0;
  328. if (state->std == std)
  329. return 0;
  330. err = adv7343_setstd(sd, std);
  331. if (!err)
  332. state->std = std;
  333. return err;
  334. }
  335. static int adv7343_s_routing(struct v4l2_subdev *sd,
  336. u32 input, u32 output, u32 config)
  337. {
  338. struct adv7343_state *state = to_state(sd);
  339. int err = 0;
  340. if (state->output == output)
  341. return 0;
  342. err = adv7343_setoutput(sd, output);
  343. if (!err)
  344. state->output = output;
  345. return err;
  346. }
  347. static const struct v4l2_subdev_video_ops adv7343_video_ops = {
  348. .s_std_output = adv7343_s_std_output,
  349. .s_routing = adv7343_s_routing,
  350. };
  351. static const struct v4l2_subdev_ops adv7343_ops = {
  352. .core = &adv7343_core_ops,
  353. .video = &adv7343_video_ops,
  354. };
  355. static int adv7343_initialize(struct v4l2_subdev *sd)
  356. {
  357. struct adv7343_state *state = to_state(sd);
  358. int err = 0;
  359. int i;
  360. for (i = 0; i < ARRAY_SIZE(adv7343_init_reg_val); i += 2) {
  361. err = adv7343_write(sd, adv7343_init_reg_val[i],
  362. adv7343_init_reg_val[i+1]);
  363. if (err) {
  364. v4l2_err(sd, "Error initializing\n");
  365. return err;
  366. }
  367. }
  368. /* Configure for default video standard */
  369. err = adv7343_setoutput(sd, state->output);
  370. if (err < 0) {
  371. v4l2_err(sd, "Error setting output during init\n");
  372. return -EINVAL;
  373. }
  374. err = adv7343_setstd(sd, state->std);
  375. if (err < 0) {
  376. v4l2_err(sd, "Error setting std during init\n");
  377. return -EINVAL;
  378. }
  379. return err;
  380. }
  381. static int adv7343_probe(struct i2c_client *client,
  382. const struct i2c_device_id *id)
  383. {
  384. struct adv7343_state *state;
  385. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  386. return -ENODEV;
  387. v4l_info(client, "chip found @ 0x%x (%s)\n",
  388. client->addr << 1, client->adapter->name);
  389. state = kzalloc(sizeof(struct adv7343_state), GFP_KERNEL);
  390. if (state == NULL)
  391. return -ENOMEM;
  392. state->reg00 = 0x80;
  393. state->reg01 = 0x00;
  394. state->reg02 = 0x20;
  395. state->reg35 = 0x00;
  396. state->reg80 = ADV7343_SD_MODE_REG1_DEFAULT;
  397. state->reg82 = ADV7343_SD_MODE_REG2_DEFAULT;
  398. state->output = ADV7343_COMPOSITE_ID;
  399. state->std = V4L2_STD_NTSC;
  400. v4l2_i2c_subdev_init(&state->sd, client, &adv7343_ops);
  401. return adv7343_initialize(&state->sd);
  402. }
  403. static int adv7343_remove(struct i2c_client *client)
  404. {
  405. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  406. v4l2_device_unregister_subdev(sd);
  407. kfree(to_state(sd));
  408. return 0;
  409. }
  410. static const struct i2c_device_id adv7343_id[] = {
  411. {"adv7343", 0},
  412. {},
  413. };
  414. MODULE_DEVICE_TABLE(i2c, adv7343_id);
  415. static struct i2c_driver adv7343_driver = {
  416. .driver = {
  417. .owner = THIS_MODULE,
  418. .name = "adv7343",
  419. },
  420. .probe = adv7343_probe,
  421. .remove = adv7343_remove,
  422. .id_table = adv7343_id,
  423. };
  424. static __init int init_adv7343(void)
  425. {
  426. return i2c_add_driver(&adv7343_driver);
  427. }
  428. static __exit void exit_adv7343(void)
  429. {
  430. i2c_del_driver(&adv7343_driver);
  431. }
  432. module_init(init_adv7343);
  433. module_exit(exit_adv7343);