adv7343.c 12 KB

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