saa6752hs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/string.h>
  4. #include <linux/timer.h>
  5. #include <linux/delay.h>
  6. #include <linux/errno.h>
  7. #include <linux/slab.h>
  8. #include <linux/poll.h>
  9. #include <linux/i2c.h>
  10. #include <linux/types.h>
  11. #include <linux/videodev2.h>
  12. #include <media/v4l2-common.h>
  13. #include <linux/init.h>
  14. #include <linux/crc32.h>
  15. #define MPEG_VIDEO_TARGET_BITRATE_MAX 27000
  16. #define MPEG_VIDEO_MAX_BITRATE_MAX 27000
  17. #define MPEG_TOTAL_TARGET_BITRATE_MAX 27000
  18. #define MPEG_PID_MAX ((1 << 14) - 1)
  19. /* Addresses to scan */
  20. static unsigned short normal_i2c[] = {0x20, I2C_CLIENT_END};
  21. I2C_CLIENT_INSMOD;
  22. MODULE_DESCRIPTION("device driver for saa6752hs MPEG2 encoder");
  23. MODULE_AUTHOR("Andrew de Quincey");
  24. MODULE_LICENSE("GPL");
  25. static struct i2c_driver driver;
  26. static struct i2c_client client_template;
  27. enum saa6752hs_videoformat {
  28. SAA6752HS_VF_D1 = 0, /* standard D1 video format: 720x576 */
  29. SAA6752HS_VF_2_3_D1 = 1,/* 2/3D1 video format: 480x576 */
  30. SAA6752HS_VF_1_2_D1 = 2,/* 1/2D1 video format: 352x576 */
  31. SAA6752HS_VF_SIF = 3, /* SIF video format: 352x288 */
  32. SAA6752HS_VF_UNKNOWN,
  33. };
  34. struct saa6752hs_mpeg_params {
  35. /* transport streams */
  36. __u16 ts_pid_pmt;
  37. __u16 ts_pid_audio;
  38. __u16 ts_pid_video;
  39. __u16 ts_pid_pcr;
  40. /* audio */
  41. enum v4l2_mpeg_audio_l2_bitrate au_l2_bitrate;
  42. /* video */
  43. enum v4l2_mpeg_video_aspect vi_aspect;
  44. enum v4l2_mpeg_video_bitrate_mode vi_bitrate_mode;
  45. __u32 vi_bitrate;
  46. __u32 vi_bitrate_peak;
  47. };
  48. static const struct v4l2_format v4l2_format_table[] =
  49. {
  50. [SAA6752HS_VF_D1] =
  51. { .fmt = { .pix = { .width = 720, .height = 576 }}},
  52. [SAA6752HS_VF_2_3_D1] =
  53. { .fmt = { .pix = { .width = 480, .height = 576 }}},
  54. [SAA6752HS_VF_1_2_D1] =
  55. { .fmt = { .pix = { .width = 352, .height = 576 }}},
  56. [SAA6752HS_VF_SIF] =
  57. { .fmt = { .pix = { .width = 352, .height = 288 }}},
  58. [SAA6752HS_VF_UNKNOWN] =
  59. { .fmt = { .pix = { .width = 0, .height = 0}}},
  60. };
  61. struct saa6752hs_state {
  62. struct i2c_client client;
  63. struct saa6752hs_mpeg_params params;
  64. enum saa6752hs_videoformat video_format;
  65. v4l2_std_id standard;
  66. };
  67. enum saa6752hs_command {
  68. SAA6752HS_COMMAND_RESET = 0,
  69. SAA6752HS_COMMAND_STOP = 1,
  70. SAA6752HS_COMMAND_START = 2,
  71. SAA6752HS_COMMAND_PAUSE = 3,
  72. SAA6752HS_COMMAND_RECONFIGURE = 4,
  73. SAA6752HS_COMMAND_SLEEP = 5,
  74. SAA6752HS_COMMAND_RECONFIGURE_FORCE = 6,
  75. SAA6752HS_COMMAND_MAX
  76. };
  77. /* ---------------------------------------------------------------------- */
  78. static u8 PAT[] = {
  79. 0xc2, /* i2c register */
  80. 0x00, /* table number for encoder */
  81. 0x47, /* sync */
  82. 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid(0) */
  83. 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
  84. 0x00, /* PSI pointer to start of table */
  85. 0x00, /* tid(0) */
  86. 0xb0, 0x0d, /* section_syntax_indicator(1), section_length(13) */
  87. 0x00, 0x01, /* transport_stream_id(1) */
  88. 0xc1, /* version_number(0), current_next_indicator(1) */
  89. 0x00, 0x00, /* section_number(0), last_section_number(0) */
  90. 0x00, 0x01, /* program_number(1) */
  91. 0xe0, 0x00, /* PMT PID */
  92. 0x00, 0x00, 0x00, 0x00 /* CRC32 */
  93. };
  94. static u8 PMT[] = {
  95. 0xc2, /* i2c register */
  96. 0x01, /* table number for encoder */
  97. 0x47, /* sync */
  98. 0x40, 0x00, /* transport_error_indicator(0), payload_unit_start(1), transport_priority(0), pid */
  99. 0x10, /* transport_scrambling_control(00), adaptation_field_control(01), continuity_counter(0) */
  100. 0x00, /* PSI pointer to start of table */
  101. 0x02, /* tid(2) */
  102. 0xb0, 0x17, /* section_syntax_indicator(1), section_length(23) */
  103. 0x00, 0x01, /* program_number(1) */
  104. 0xc1, /* version_number(0), current_next_indicator(1) */
  105. 0x00, 0x00, /* section_number(0), last_section_number(0) */
  106. 0xe0, 0x00, /* PCR_PID */
  107. 0xf0, 0x00, /* program_info_length(0) */
  108. 0x02, 0xe0, 0x00, 0xf0, 0x00, /* video stream type(2), pid */
  109. 0x04, 0xe0, 0x00, 0xf0, 0x00, /* audio stream type(4), pid */
  110. 0x00, 0x00, 0x00, 0x00 /* CRC32 */
  111. };
  112. static struct saa6752hs_mpeg_params param_defaults =
  113. {
  114. .ts_pid_pmt = 16,
  115. .ts_pid_video = 260,
  116. .ts_pid_audio = 256,
  117. .ts_pid_pcr = 259,
  118. .vi_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
  119. .vi_bitrate = 4000,
  120. .vi_bitrate_peak = 6000,
  121. .vi_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  122. .au_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_256K,
  123. };
  124. /* ---------------------------------------------------------------------- */
  125. static int saa6752hs_chip_command(struct i2c_client* client,
  126. enum saa6752hs_command command)
  127. {
  128. unsigned char buf[3];
  129. unsigned long timeout;
  130. int status = 0;
  131. /* execute the command */
  132. switch(command) {
  133. case SAA6752HS_COMMAND_RESET:
  134. buf[0] = 0x00;
  135. break;
  136. case SAA6752HS_COMMAND_STOP:
  137. buf[0] = 0x03;
  138. break;
  139. case SAA6752HS_COMMAND_START:
  140. buf[0] = 0x02;
  141. break;
  142. case SAA6752HS_COMMAND_PAUSE:
  143. buf[0] = 0x04;
  144. break;
  145. case SAA6752HS_COMMAND_RECONFIGURE:
  146. buf[0] = 0x05;
  147. break;
  148. case SAA6752HS_COMMAND_SLEEP:
  149. buf[0] = 0x06;
  150. break;
  151. case SAA6752HS_COMMAND_RECONFIGURE_FORCE:
  152. buf[0] = 0x07;
  153. break;
  154. default:
  155. return -EINVAL;
  156. }
  157. /* set it and wait for it to be so */
  158. i2c_master_send(client, buf, 1);
  159. timeout = jiffies + HZ * 3;
  160. for (;;) {
  161. /* get the current status */
  162. buf[0] = 0x10;
  163. i2c_master_send(client, buf, 1);
  164. i2c_master_recv(client, buf, 1);
  165. if (!(buf[0] & 0x20))
  166. break;
  167. if (time_after(jiffies,timeout)) {
  168. status = -ETIMEDOUT;
  169. break;
  170. }
  171. msleep(10);
  172. }
  173. /* delay a bit to let encoder settle */
  174. msleep(50);
  175. return status;
  176. }
  177. static int saa6752hs_set_bitrate(struct i2c_client* client,
  178. struct saa6752hs_mpeg_params* params)
  179. {
  180. u8 buf[3];
  181. int tot_bitrate;
  182. /* set the bitrate mode */
  183. buf[0] = 0x71;
  184. buf[1] = (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ? 0 : 1;
  185. i2c_master_send(client, buf, 2);
  186. /* set the video bitrate */
  187. if (params->vi_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
  188. /* set the target bitrate */
  189. buf[0] = 0x80;
  190. buf[1] = params->vi_bitrate >> 8;
  191. buf[2] = params->vi_bitrate & 0xff;
  192. i2c_master_send(client, buf, 3);
  193. /* set the max bitrate */
  194. buf[0] = 0x81;
  195. buf[1] = params->vi_bitrate_peak >> 8;
  196. buf[2] = params->vi_bitrate_peak & 0xff;
  197. i2c_master_send(client, buf, 3);
  198. tot_bitrate = params->vi_bitrate_peak;
  199. } else {
  200. /* set the target bitrate (no max bitrate for CBR) */
  201. buf[0] = 0x81;
  202. buf[1] = params->vi_bitrate >> 8;
  203. buf[2] = params->vi_bitrate & 0xff;
  204. i2c_master_send(client, buf, 3);
  205. tot_bitrate = params->vi_bitrate;
  206. }
  207. /* set the audio bitrate */
  208. buf[0] = 0x94;
  209. buf[1] = (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 0 : 1;
  210. i2c_master_send(client, buf, 2);
  211. tot_bitrate += (V4L2_MPEG_AUDIO_L2_BITRATE_256K == params->au_l2_bitrate) ? 256 : 384;
  212. /* Note: the total max bitrate is determined by adding the video and audio
  213. bitrates together and also adding an extra 768kbit/s to stay on the
  214. safe side. If more control should be required, then an extra MPEG control
  215. should be added. */
  216. tot_bitrate += 768;
  217. if (tot_bitrate > MPEG_TOTAL_TARGET_BITRATE_MAX)
  218. tot_bitrate = MPEG_TOTAL_TARGET_BITRATE_MAX;
  219. /* set the total bitrate */
  220. buf[0] = 0xb1;
  221. buf[1] = tot_bitrate >> 8;
  222. buf[2] = tot_bitrate & 0xff;
  223. i2c_master_send(client, buf, 3);
  224. return 0;
  225. }
  226. static void saa6752hs_set_subsampling(struct i2c_client* client,
  227. struct v4l2_format* f)
  228. {
  229. struct saa6752hs_state *h = i2c_get_clientdata(client);
  230. int dist_352, dist_480, dist_720;
  231. /*
  232. FIXME: translate and round width/height into EMPRESS
  233. subsample type:
  234. type | PAL | NTSC
  235. ---------------------------
  236. SIF | 352x288 | 352x240
  237. 1/2 D1 | 352x576 | 352x480
  238. 2/3 D1 | 480x576 | 480x480
  239. D1 | 720x576 | 720x480
  240. */
  241. dist_352 = abs(f->fmt.pix.width - 352);
  242. dist_480 = abs(f->fmt.pix.width - 480);
  243. dist_720 = abs(f->fmt.pix.width - 720);
  244. if (dist_720 < dist_480) {
  245. f->fmt.pix.width = 720;
  246. f->fmt.pix.height = 576;
  247. h->video_format = SAA6752HS_VF_D1;
  248. }
  249. else if (dist_480 < dist_352) {
  250. f->fmt.pix.width = 480;
  251. f->fmt.pix.height = 576;
  252. h->video_format = SAA6752HS_VF_2_3_D1;
  253. }
  254. else {
  255. f->fmt.pix.width = 352;
  256. if (abs(f->fmt.pix.height - 576) <
  257. abs(f->fmt.pix.height - 288)) {
  258. f->fmt.pix.height = 576;
  259. h->video_format = SAA6752HS_VF_1_2_D1;
  260. }
  261. else {
  262. f->fmt.pix.height = 288;
  263. h->video_format = SAA6752HS_VF_SIF;
  264. }
  265. }
  266. }
  267. static int handle_ctrl(struct saa6752hs_mpeg_params *params,
  268. struct v4l2_ext_control *ctrl, unsigned int cmd)
  269. {
  270. int old = 0, new;
  271. int set = (cmd == VIDIOC_S_EXT_CTRLS);
  272. new = ctrl->value;
  273. switch (ctrl->id) {
  274. case V4L2_CID_MPEG_STREAM_TYPE:
  275. old = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
  276. if (set && new != old)
  277. return -ERANGE;
  278. new = old;
  279. break;
  280. case V4L2_CID_MPEG_STREAM_PID_PMT:
  281. old = params->ts_pid_pmt;
  282. if (set && new > MPEG_PID_MAX)
  283. return -ERANGE;
  284. if (new > MPEG_PID_MAX)
  285. new = MPEG_PID_MAX;
  286. params->ts_pid_pmt = new;
  287. break;
  288. case V4L2_CID_MPEG_STREAM_PID_AUDIO:
  289. old = params->ts_pid_audio;
  290. if (set && new > MPEG_PID_MAX)
  291. return -ERANGE;
  292. if (new > MPEG_PID_MAX)
  293. new = MPEG_PID_MAX;
  294. params->ts_pid_audio = new;
  295. break;
  296. case V4L2_CID_MPEG_STREAM_PID_VIDEO:
  297. old = params->ts_pid_video;
  298. if (set && new > MPEG_PID_MAX)
  299. return -ERANGE;
  300. if (new > MPEG_PID_MAX)
  301. new = MPEG_PID_MAX;
  302. params->ts_pid_video = new;
  303. break;
  304. case V4L2_CID_MPEG_STREAM_PID_PCR:
  305. old = params->ts_pid_pcr;
  306. if (set && new > MPEG_PID_MAX)
  307. return -ERANGE;
  308. if (new > MPEG_PID_MAX)
  309. new = MPEG_PID_MAX;
  310. params->ts_pid_pcr = new;
  311. break;
  312. case V4L2_CID_MPEG_AUDIO_ENCODING:
  313. old = V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
  314. if (set && new != old)
  315. return -ERANGE;
  316. new = old;
  317. break;
  318. case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
  319. old = params->au_l2_bitrate;
  320. if (set && new != V4L2_MPEG_AUDIO_L2_BITRATE_256K &&
  321. new != V4L2_MPEG_AUDIO_L2_BITRATE_384K)
  322. return -ERANGE;
  323. if (new <= V4L2_MPEG_AUDIO_L2_BITRATE_256K)
  324. new = V4L2_MPEG_AUDIO_L2_BITRATE_256K;
  325. else
  326. new = V4L2_MPEG_AUDIO_L2_BITRATE_384K;
  327. params->au_l2_bitrate = new;
  328. break;
  329. case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
  330. old = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
  331. if (set && new != old)
  332. return -ERANGE;
  333. new = old;
  334. break;
  335. case V4L2_CID_MPEG_VIDEO_ENCODING:
  336. old = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
  337. if (set && new != old)
  338. return -ERANGE;
  339. new = old;
  340. break;
  341. case V4L2_CID_MPEG_VIDEO_ASPECT:
  342. old = params->vi_aspect;
  343. if (set && new != V4L2_MPEG_VIDEO_ASPECT_16x9 &&
  344. new != V4L2_MPEG_VIDEO_ASPECT_4x3)
  345. return -ERANGE;
  346. if (new != V4L2_MPEG_VIDEO_ASPECT_16x9)
  347. new = V4L2_MPEG_VIDEO_ASPECT_4x3;
  348. params->vi_aspect = new;
  349. break;
  350. case V4L2_CID_MPEG_VIDEO_BITRATE:
  351. old = params->vi_bitrate * 1000;
  352. new = 1000 * (new / 1000);
  353. if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
  354. return -ERANGE;
  355. if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
  356. new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
  357. params->vi_bitrate = new / 1000;
  358. break;
  359. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
  360. old = params->vi_bitrate_peak * 1000;
  361. new = 1000 * (new / 1000);
  362. if (set && new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
  363. return -ERANGE;
  364. if (new > MPEG_VIDEO_TARGET_BITRATE_MAX * 1000)
  365. new = MPEG_VIDEO_TARGET_BITRATE_MAX * 1000;
  366. params->vi_bitrate_peak = new / 1000;
  367. break;
  368. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  369. old = params->vi_bitrate_mode;
  370. params->vi_bitrate_mode = new;
  371. break;
  372. default:
  373. return -EINVAL;
  374. }
  375. if (cmd == VIDIOC_G_EXT_CTRLS)
  376. ctrl->value = old;
  377. else
  378. ctrl->value = new;
  379. return 0;
  380. }
  381. static int saa6752hs_init(struct i2c_client* client)
  382. {
  383. unsigned char buf[9], buf2[4];
  384. struct saa6752hs_state *h;
  385. u32 crc;
  386. unsigned char localPAT[256];
  387. unsigned char localPMT[256];
  388. h = i2c_get_clientdata(client);
  389. /* Set video format - must be done first as it resets other settings */
  390. buf[0] = 0x41;
  391. buf[1] = h->video_format;
  392. i2c_master_send(client, buf, 2);
  393. /* Set number of lines in input signal */
  394. buf[0] = 0x40;
  395. buf[1] = 0x00;
  396. if (h->standard & V4L2_STD_525_60)
  397. buf[1] = 0x01;
  398. i2c_master_send(client, buf, 2);
  399. /* set bitrate */
  400. saa6752hs_set_bitrate(client, &h->params);
  401. /* Set GOP structure {3, 13} */
  402. buf[0] = 0x72;
  403. buf[1] = 0x03;
  404. buf[2] = 0x0D;
  405. i2c_master_send(client,buf,3);
  406. /* Set minimum Q-scale {4} */
  407. buf[0] = 0x82;
  408. buf[1] = 0x04;
  409. i2c_master_send(client,buf,2);
  410. /* Set maximum Q-scale {12} */
  411. buf[0] = 0x83;
  412. buf[1] = 0x0C;
  413. i2c_master_send(client,buf,2);
  414. /* Set Output Protocol */
  415. buf[0] = 0xD0;
  416. buf[1] = 0x81;
  417. i2c_master_send(client,buf,2);
  418. /* Set video output stream format {TS} */
  419. buf[0] = 0xB0;
  420. buf[1] = 0x05;
  421. i2c_master_send(client,buf,2);
  422. /* compute PAT */
  423. memcpy(localPAT, PAT, sizeof(PAT));
  424. localPAT[17] = 0xe0 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
  425. localPAT[18] = h->params.ts_pid_pmt & 0xff;
  426. crc = crc32_be(~0, &localPAT[7], sizeof(PAT) - 7 - 4);
  427. localPAT[sizeof(PAT) - 4] = (crc >> 24) & 0xFF;
  428. localPAT[sizeof(PAT) - 3] = (crc >> 16) & 0xFF;
  429. localPAT[sizeof(PAT) - 2] = (crc >> 8) & 0xFF;
  430. localPAT[sizeof(PAT) - 1] = crc & 0xFF;
  431. /* compute PMT */
  432. memcpy(localPMT, PMT, sizeof(PMT));
  433. localPMT[3] = 0x40 | ((h->params.ts_pid_pmt >> 8) & 0x0f);
  434. localPMT[4] = h->params.ts_pid_pmt & 0xff;
  435. localPMT[15] = 0xE0 | ((h->params.ts_pid_pcr >> 8) & 0x0F);
  436. localPMT[16] = h->params.ts_pid_pcr & 0xFF;
  437. localPMT[20] = 0xE0 | ((h->params.ts_pid_video >> 8) & 0x0F);
  438. localPMT[21] = h->params.ts_pid_video & 0xFF;
  439. localPMT[25] = 0xE0 | ((h->params.ts_pid_audio >> 8) & 0x0F);
  440. localPMT[26] = h->params.ts_pid_audio & 0xFF;
  441. crc = crc32_be(~0, &localPMT[7], sizeof(PMT) - 7 - 4);
  442. localPMT[sizeof(PMT) - 4] = (crc >> 24) & 0xFF;
  443. localPMT[sizeof(PMT) - 3] = (crc >> 16) & 0xFF;
  444. localPMT[sizeof(PMT) - 2] = (crc >> 8) & 0xFF;
  445. localPMT[sizeof(PMT) - 1] = crc & 0xFF;
  446. /* Set Audio PID */
  447. buf[0] = 0xC1;
  448. buf[1] = (h->params.ts_pid_audio >> 8) & 0xFF;
  449. buf[2] = h->params.ts_pid_audio & 0xFF;
  450. i2c_master_send(client,buf,3);
  451. /* Set Video PID */
  452. buf[0] = 0xC0;
  453. buf[1] = (h->params.ts_pid_video >> 8) & 0xFF;
  454. buf[2] = h->params.ts_pid_video & 0xFF;
  455. i2c_master_send(client,buf,3);
  456. /* Set PCR PID */
  457. buf[0] = 0xC4;
  458. buf[1] = (h->params.ts_pid_pcr >> 8) & 0xFF;
  459. buf[2] = h->params.ts_pid_pcr & 0xFF;
  460. i2c_master_send(client,buf,3);
  461. /* Send SI tables */
  462. i2c_master_send(client,localPAT,sizeof(PAT));
  463. i2c_master_send(client,localPMT,sizeof(PMT));
  464. /* mute then unmute audio. This removes buzzing artefacts */
  465. buf[0] = 0xa4;
  466. buf[1] = 1;
  467. i2c_master_send(client, buf, 2);
  468. buf[1] = 0;
  469. i2c_master_send(client, buf, 2);
  470. /* start it going */
  471. saa6752hs_chip_command(client, SAA6752HS_COMMAND_START);
  472. /* readout current state */
  473. buf[0] = 0xE1;
  474. buf[1] = 0xA7;
  475. buf[2] = 0xFE;
  476. buf[3] = 0x82;
  477. buf[4] = 0xB0;
  478. i2c_master_send(client, buf, 5);
  479. i2c_master_recv(client, buf2, 4);
  480. /* change aspect ratio */
  481. buf[0] = 0xE0;
  482. buf[1] = 0xA7;
  483. buf[2] = 0xFE;
  484. buf[3] = 0x82;
  485. buf[4] = 0xB0;
  486. buf[5] = buf2[0];
  487. switch(h->params.vi_aspect) {
  488. case V4L2_MPEG_VIDEO_ASPECT_16x9:
  489. buf[6] = buf2[1] | 0x40;
  490. break;
  491. case V4L2_MPEG_VIDEO_ASPECT_4x3:
  492. default:
  493. buf[6] = buf2[1] & 0xBF;
  494. break;
  495. break;
  496. }
  497. buf[7] = buf2[2];
  498. buf[8] = buf2[3];
  499. i2c_master_send(client, buf, 9);
  500. return 0;
  501. }
  502. static int saa6752hs_attach(struct i2c_adapter *adap, int addr, int kind)
  503. {
  504. struct saa6752hs_state *h;
  505. if (NULL == (h = kzalloc(sizeof(*h), GFP_KERNEL)))
  506. return -ENOMEM;
  507. h->client = client_template;
  508. h->params = param_defaults;
  509. h->client.adapter = adap;
  510. h->client.addr = addr;
  511. /* Assume 625 input lines */
  512. h->standard = 0;
  513. i2c_set_clientdata(&h->client, h);
  514. i2c_attach_client(&h->client);
  515. v4l_info(&h->client,"saa6752hs: chip found @ 0x%x\n", addr<<1);
  516. return 0;
  517. }
  518. static int saa6752hs_probe(struct i2c_adapter *adap)
  519. {
  520. if (adap->class & I2C_CLASS_TV_ANALOG)
  521. return i2c_probe(adap, &addr_data, saa6752hs_attach);
  522. return 0;
  523. }
  524. static int saa6752hs_detach(struct i2c_client *client)
  525. {
  526. struct saa6752hs_state *h;
  527. h = i2c_get_clientdata(client);
  528. i2c_detach_client(client);
  529. kfree(h);
  530. return 0;
  531. }
  532. static int
  533. saa6752hs_command(struct i2c_client *client, unsigned int cmd, void *arg)
  534. {
  535. struct saa6752hs_state *h = i2c_get_clientdata(client);
  536. struct v4l2_ext_controls *ctrls = arg;
  537. struct saa6752hs_mpeg_params params;
  538. int err = 0;
  539. int i;
  540. switch (cmd) {
  541. case VIDIOC_S_EXT_CTRLS:
  542. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  543. return -EINVAL;
  544. if (ctrls->count == 0) {
  545. /* apply settings and start encoder */
  546. saa6752hs_init(client);
  547. break;
  548. }
  549. /* fall through */
  550. case VIDIOC_TRY_EXT_CTRLS:
  551. case VIDIOC_G_EXT_CTRLS:
  552. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
  553. return -EINVAL;
  554. params = h->params;
  555. for (i = 0; i < ctrls->count; i++) {
  556. if ((err = handle_ctrl(&params, ctrls->controls + i, cmd))) {
  557. ctrls->error_idx = i;
  558. return err;
  559. }
  560. }
  561. h->params = params;
  562. break;
  563. case VIDIOC_G_FMT:
  564. {
  565. struct v4l2_format *f = arg;
  566. if (h->video_format == SAA6752HS_VF_UNKNOWN)
  567. h->video_format = SAA6752HS_VF_D1;
  568. f->fmt.pix.width =
  569. v4l2_format_table[h->video_format].fmt.pix.width;
  570. f->fmt.pix.height =
  571. v4l2_format_table[h->video_format].fmt.pix.height;
  572. break ;
  573. }
  574. case VIDIOC_S_FMT:
  575. {
  576. struct v4l2_format *f = arg;
  577. saa6752hs_set_subsampling(client, f);
  578. break;
  579. }
  580. case VIDIOC_S_STD:
  581. h->standard = *((v4l2_std_id *) arg);
  582. break;
  583. default:
  584. /* nothing */
  585. break;
  586. }
  587. return err;
  588. }
  589. /* ----------------------------------------------------------------------- */
  590. static struct i2c_driver driver = {
  591. .driver = {
  592. .name = "saa6752hs",
  593. },
  594. .id = I2C_DRIVERID_SAA6752HS,
  595. .attach_adapter = saa6752hs_probe,
  596. .detach_client = saa6752hs_detach,
  597. .command = saa6752hs_command,
  598. };
  599. static struct i2c_client client_template =
  600. {
  601. .name = "saa6752hs",
  602. .driver = &driver,
  603. };
  604. static int __init saa6752hs_init_module(void)
  605. {
  606. return i2c_add_driver(&driver);
  607. }
  608. static void __exit saa6752hs_cleanup_module(void)
  609. {
  610. i2c_del_driver(&driver);
  611. }
  612. module_init(saa6752hs_init_module);
  613. module_exit(saa6752hs_cleanup_module);
  614. /*
  615. * Overrides for Emacs so that we follow Linus's tabbing style.
  616. * ---------------------------------------------------------------------------
  617. * Local variables:
  618. * c-basic-offset: 8
  619. * End:
  620. */