cx25840-core.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /* cx25840 - Conexant CX25840 audio/video decoder driver
  2. *
  3. * Copyright (C) 2004 Ulf Eklund
  4. *
  5. * Based on the saa7115 driver and on the first verison of Chris Kennedy's
  6. * cx25840 driver.
  7. *
  8. * Changes by Tyler Trafford <tatrafford@comcast.net>
  9. * - cleanup/rewrite for V4L2 API (2005)
  10. *
  11. * VBI support by Hans Verkuil <hverkuil@xs4all.nl>.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 2
  16. * of the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/slab.h>
  30. #include <linux/videodev2.h>
  31. #include <linux/i2c.h>
  32. #include <media/audiochip.h>
  33. #include <media/v4l2-common.h>
  34. #include "cx25840.h"
  35. MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
  36. MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
  37. MODULE_LICENSE("GPL");
  38. static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
  39. static int cx25840_debug;
  40. module_param_named(debug,cx25840_debug, int, 0644);
  41. MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
  42. I2C_CLIENT_INSMOD;
  43. /* ----------------------------------------------------------------------- */
  44. int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
  45. {
  46. u8 buffer[3];
  47. buffer[0] = addr >> 8;
  48. buffer[1] = addr & 0xff;
  49. buffer[2] = value;
  50. return i2c_master_send(client, buffer, 3);
  51. }
  52. int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
  53. {
  54. u8 buffer[6];
  55. buffer[0] = addr >> 8;
  56. buffer[1] = addr & 0xff;
  57. buffer[2] = value >> 24;
  58. buffer[3] = (value >> 16) & 0xff;
  59. buffer[4] = (value >> 8) & 0xff;
  60. buffer[5] = value & 0xff;
  61. return i2c_master_send(client, buffer, 6);
  62. }
  63. u8 cx25840_read(struct i2c_client * client, u16 addr)
  64. {
  65. u8 buffer[2];
  66. buffer[0] = addr >> 8;
  67. buffer[1] = addr & 0xff;
  68. if (i2c_master_send(client, buffer, 2) < 2)
  69. return 0;
  70. if (i2c_master_recv(client, buffer, 1) < 1)
  71. return 0;
  72. return buffer[0];
  73. }
  74. u32 cx25840_read4(struct i2c_client * client, u16 addr)
  75. {
  76. u8 buffer[4];
  77. buffer[0] = addr >> 8;
  78. buffer[1] = addr & 0xff;
  79. if (i2c_master_send(client, buffer, 2) < 2)
  80. return 0;
  81. if (i2c_master_recv(client, buffer, 4) < 4)
  82. return 0;
  83. return (buffer[0] << 24) | (buffer[1] << 16) |
  84. (buffer[2] << 8) | buffer[3];
  85. }
  86. int cx25840_and_or(struct i2c_client *client, u16 addr, u8 and_mask,
  87. u8 or_value)
  88. {
  89. return cx25840_write(client, addr,
  90. (cx25840_read(client, addr) & and_mask) |
  91. or_value);
  92. }
  93. /* ----------------------------------------------------------------------- */
  94. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  95. enum cx25840_audio_input aud_input);
  96. static void log_status(struct i2c_client *client);
  97. /* ----------------------------------------------------------------------- */
  98. static void init_dll1(struct i2c_client *client)
  99. {
  100. /* This is the Hauppauge sequence used to
  101. * initialize the Delay Lock Loop 1 (ADC DLL). */
  102. cx25840_write(client, 0x159, 0x23);
  103. cx25840_write(client, 0x15a, 0x87);
  104. cx25840_write(client, 0x15b, 0x06);
  105. cx25840_write(client, 0x159, 0xe1);
  106. cx25840_write(client, 0x15a, 0x86);
  107. cx25840_write(client, 0x159, 0xe0);
  108. cx25840_write(client, 0x159, 0xe1);
  109. cx25840_write(client, 0x15b, 0x10);
  110. }
  111. static void init_dll2(struct i2c_client *client)
  112. {
  113. /* This is the Hauppauge sequence used to
  114. * initialize the Delay Lock Loop 2 (ADC DLL). */
  115. cx25840_write(client, 0x15d, 0xe3);
  116. cx25840_write(client, 0x15e, 0x86);
  117. cx25840_write(client, 0x15f, 0x06);
  118. cx25840_write(client, 0x15d, 0xe1);
  119. cx25840_write(client, 0x15d, 0xe0);
  120. cx25840_write(client, 0x15d, 0xe1);
  121. }
  122. static void cx25840_initialize(struct i2c_client *client, int loadfw)
  123. {
  124. struct cx25840_state *state = i2c_get_clientdata(client);
  125. /* datasheet startup in numbered steps, refer to page 3-77 */
  126. /* 2. */
  127. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  128. /* The default of this register should be 4, but I get 0 instead.
  129. * Set this register to 4 manually. */
  130. cx25840_write(client, 0x000, 0x04);
  131. /* 3. */
  132. init_dll1(client);
  133. init_dll2(client);
  134. cx25840_write(client, 0x136, 0x0a);
  135. /* 4. */
  136. cx25840_write(client, 0x13c, 0x01);
  137. cx25840_write(client, 0x13c, 0x00);
  138. /* 5. */
  139. if (loadfw)
  140. cx25840_loadfw(client);
  141. /* 6. */
  142. cx25840_write(client, 0x115, 0x8c);
  143. cx25840_write(client, 0x116, 0x07);
  144. cx25840_write(client, 0x118, 0x02);
  145. /* 7. */
  146. cx25840_write(client, 0x4a5, 0x80);
  147. cx25840_write(client, 0x4a5, 0x00);
  148. cx25840_write(client, 0x402, 0x00);
  149. /* 8. */
  150. cx25840_write(client, 0x401, 0x18);
  151. cx25840_write(client, 0x4a2, 0x10);
  152. cx25840_write(client, 0x402, 0x04);
  153. /* 10. */
  154. cx25840_write(client, 0x8d3, 0x1f);
  155. cx25840_write(client, 0x8e3, 0x03);
  156. cx25840_vbi_setup(client);
  157. /* trial and error says these are needed to get audio */
  158. cx25840_write(client, 0x914, 0xa0);
  159. cx25840_write(client, 0x918, 0xa0);
  160. cx25840_write(client, 0x919, 0x01);
  161. /* stereo prefered */
  162. cx25840_write(client, 0x809, 0x04);
  163. /* AC97 shift */
  164. cx25840_write(client, 0x8cf, 0x0f);
  165. /* (re)set input */
  166. set_input(client, state->vid_input, state->aud_input);
  167. /* start microcontroller */
  168. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  169. }
  170. /* ----------------------------------------------------------------------- */
  171. static void input_change(struct i2c_client *client)
  172. {
  173. struct cx25840_state *state = i2c_get_clientdata(client);
  174. v4l2_std_id std = cx25840_get_v4lstd(client);
  175. /* Note: perhaps V4L2_STD_PAL_M should be handled as V4L2_STD_NTSC
  176. instead of V4L2_STD_PAL. Someone needs to test this. */
  177. if (std & V4L2_STD_PAL) {
  178. /* Follow tuner change procedure for PAL */
  179. cx25840_write(client, 0x808, 0xff);
  180. cx25840_write(client, 0x80b, 0x10);
  181. } else if (std & V4L2_STD_SECAM) {
  182. /* Select autodetect for SECAM */
  183. cx25840_write(client, 0x808, 0xff);
  184. cx25840_write(client, 0x80b, 0x10);
  185. } else if (std & V4L2_STD_NTSC) {
  186. /* Certain Hauppauge PVR150 models have a hardware bug
  187. that causes audio to drop out. For these models the
  188. audio standard must be set explicitly.
  189. To be precise: it affects cards with tuner models
  190. 85, 99 and 112 (model numbers from tveeprom). */
  191. int hw_fix = state->pvr150_workaround;
  192. if (std == V4L2_STD_NTSC_M_JP) {
  193. /* Japan uses EIAJ audio standard */
  194. cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
  195. } else if (std == V4L2_STD_NTSC_M_KR) {
  196. /* South Korea uses A2 audio standard */
  197. cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
  198. } else {
  199. /* Others use the BTSC audio standard */
  200. cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
  201. }
  202. cx25840_write(client, 0x80b, 0x00);
  203. }
  204. if (cx25840_read(client, 0x803) & 0x10) {
  205. /* restart audio decoder microcontroller */
  206. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  207. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  208. }
  209. }
  210. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  211. enum cx25840_audio_input aud_input)
  212. {
  213. struct cx25840_state *state = i2c_get_clientdata(client);
  214. u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
  215. vid_input <= CX25840_COMPOSITE8);
  216. u8 reg;
  217. v4l_dbg(1, cx25840_debug, client, "decoder set video input %d, audio input %d\n",
  218. vid_input, aud_input);
  219. if (is_composite) {
  220. reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
  221. } else {
  222. int luma = vid_input & 0xf0;
  223. int chroma = vid_input & 0xf00;
  224. if ((vid_input & ~0xff0) ||
  225. luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
  226. chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
  227. v4l_err(client, "0x%04x is not a valid video input!\n", vid_input);
  228. return -EINVAL;
  229. }
  230. reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
  231. if (chroma >= CX25840_SVIDEO_CHROMA7) {
  232. reg &= 0x3f;
  233. reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
  234. } else {
  235. reg &= 0xcf;
  236. reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
  237. }
  238. }
  239. switch (aud_input) {
  240. case CX25840_AUDIO_SERIAL:
  241. /* do nothing, use serial audio input */
  242. break;
  243. case CX25840_AUDIO4: reg &= ~0x30; break;
  244. case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
  245. case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
  246. case CX25840_AUDIO7: reg &= ~0xc0; break;
  247. case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
  248. default:
  249. v4l_err(client, "0x%04x is not a valid audio input!\n", aud_input);
  250. return -EINVAL;
  251. }
  252. cx25840_write(client, 0x103, reg);
  253. /* Set INPUT_MODE to Composite (0) or S-Video (1) */
  254. cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
  255. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  256. cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
  257. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
  258. if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
  259. cx25840_and_or(client, 0x102, ~0x4, 4);
  260. else
  261. cx25840_and_or(client, 0x102, ~0x4, 0);
  262. state->vid_input = vid_input;
  263. state->aud_input = aud_input;
  264. cx25840_audio_set_path(client);
  265. input_change(client);
  266. return 0;
  267. }
  268. /* ----------------------------------------------------------------------- */
  269. static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
  270. {
  271. u8 fmt=0; /* zero is autodetect */
  272. /* First tests should be against specific std */
  273. if (std == V4L2_STD_NTSC_M_JP) {
  274. fmt=0x2;
  275. } else if (std == V4L2_STD_NTSC_443) {
  276. fmt=0x3;
  277. } else if (std == V4L2_STD_PAL_M) {
  278. fmt=0x5;
  279. } else if (std == V4L2_STD_PAL_N) {
  280. fmt=0x6;
  281. } else if (std == V4L2_STD_PAL_Nc) {
  282. fmt=0x7;
  283. } else if (std == V4L2_STD_PAL_60) {
  284. fmt=0x8;
  285. } else {
  286. /* Then, test against generic ones */
  287. if (std & V4L2_STD_NTSC) {
  288. fmt=0x1;
  289. } else if (std & V4L2_STD_PAL) {
  290. fmt=0x4;
  291. } else if (std & V4L2_STD_SECAM) {
  292. fmt=0xc;
  293. }
  294. }
  295. cx25840_and_or(client, 0x400, ~0xf, fmt);
  296. cx25840_vbi_setup(client);
  297. return 0;
  298. }
  299. v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
  300. {
  301. /* check VID_FMT_SEL first */
  302. u8 fmt = cx25840_read(client, 0x400) & 0xf;
  303. if (!fmt) {
  304. /* check AFD_FMT_STAT if set to autodetect */
  305. fmt = cx25840_read(client, 0x40d) & 0xf;
  306. }
  307. switch (fmt) {
  308. case 0x1: return V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR;
  309. case 0x2: return V4L2_STD_NTSC_M_JP;
  310. case 0x3: return V4L2_STD_NTSC_443;
  311. case 0x4: return V4L2_STD_PAL;
  312. case 0x5: return V4L2_STD_PAL_M;
  313. case 0x6: return V4L2_STD_PAL_N;
  314. case 0x7: return V4L2_STD_PAL_Nc;
  315. case 0x8: return V4L2_STD_PAL_60;
  316. case 0xc: return V4L2_STD_SECAM;
  317. default: return V4L2_STD_UNKNOWN;
  318. }
  319. }
  320. /* ----------------------------------------------------------------------- */
  321. static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  322. {
  323. struct cx25840_state *state = i2c_get_clientdata(client);
  324. switch (ctrl->id) {
  325. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  326. state->pvr150_workaround = ctrl->value;
  327. set_input(client, state->vid_input, state->aud_input);
  328. break;
  329. case V4L2_CID_BRIGHTNESS:
  330. if (ctrl->value < 0 || ctrl->value > 255) {
  331. v4l_err(client, "invalid brightness setting %d\n",
  332. ctrl->value);
  333. return -ERANGE;
  334. }
  335. cx25840_write(client, 0x414, ctrl->value - 128);
  336. break;
  337. case V4L2_CID_CONTRAST:
  338. if (ctrl->value < 0 || ctrl->value > 127) {
  339. v4l_err(client, "invalid contrast setting %d\n",
  340. ctrl->value);
  341. return -ERANGE;
  342. }
  343. cx25840_write(client, 0x415, ctrl->value << 1);
  344. break;
  345. case V4L2_CID_SATURATION:
  346. if (ctrl->value < 0 || ctrl->value > 127) {
  347. v4l_err(client, "invalid saturation setting %d\n",
  348. ctrl->value);
  349. return -ERANGE;
  350. }
  351. cx25840_write(client, 0x420, ctrl->value << 1);
  352. cx25840_write(client, 0x421, ctrl->value << 1);
  353. break;
  354. case V4L2_CID_HUE:
  355. if (ctrl->value < -127 || ctrl->value > 127) {
  356. v4l_err(client, "invalid hue setting %d\n", ctrl->value);
  357. return -ERANGE;
  358. }
  359. cx25840_write(client, 0x422, ctrl->value);
  360. break;
  361. case V4L2_CID_AUDIO_VOLUME:
  362. case V4L2_CID_AUDIO_BASS:
  363. case V4L2_CID_AUDIO_TREBLE:
  364. case V4L2_CID_AUDIO_BALANCE:
  365. case V4L2_CID_AUDIO_MUTE:
  366. return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
  367. default:
  368. return -EINVAL;
  369. }
  370. return 0;
  371. }
  372. static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  373. {
  374. struct cx25840_state *state = i2c_get_clientdata(client);
  375. switch (ctrl->id) {
  376. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  377. ctrl->value = state->pvr150_workaround;
  378. break;
  379. case V4L2_CID_BRIGHTNESS:
  380. ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
  381. break;
  382. case V4L2_CID_CONTRAST:
  383. ctrl->value = cx25840_read(client, 0x415) >> 1;
  384. break;
  385. case V4L2_CID_SATURATION:
  386. ctrl->value = cx25840_read(client, 0x420) >> 1;
  387. break;
  388. case V4L2_CID_HUE:
  389. ctrl->value = (s8)cx25840_read(client, 0x422);
  390. break;
  391. case V4L2_CID_AUDIO_VOLUME:
  392. case V4L2_CID_AUDIO_BASS:
  393. case V4L2_CID_AUDIO_TREBLE:
  394. case V4L2_CID_AUDIO_BALANCE:
  395. case V4L2_CID_AUDIO_MUTE:
  396. return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
  397. default:
  398. return -EINVAL;
  399. }
  400. return 0;
  401. }
  402. /* ----------------------------------------------------------------------- */
  403. static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  404. {
  405. switch (fmt->type) {
  406. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  407. return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
  408. default:
  409. return -EINVAL;
  410. }
  411. return 0;
  412. }
  413. static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  414. {
  415. struct v4l2_pix_format *pix;
  416. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  417. int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
  418. switch (fmt->type) {
  419. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  420. pix = &(fmt->fmt.pix);
  421. Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
  422. Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
  423. Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
  424. Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
  425. Vlines = pix->height + (is_pal ? 4 : 7);
  426. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  427. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  428. v4l_err(client, "%dx%d is not a valid size!\n",
  429. pix->width, pix->height);
  430. return -ERANGE;
  431. }
  432. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  433. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  434. VSC &= 0x1fff;
  435. if (pix->width >= 385)
  436. filter = 0;
  437. else if (pix->width > 192)
  438. filter = 1;
  439. else if (pix->width > 96)
  440. filter = 2;
  441. else
  442. filter = 3;
  443. v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
  444. pix->width, pix->height, HSC, VSC);
  445. /* HSCALE=HSC */
  446. cx25840_write(client, 0x418, HSC & 0xff);
  447. cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
  448. cx25840_write(client, 0x41a, HSC >> 16);
  449. /* VSCALE=VSC */
  450. cx25840_write(client, 0x41c, VSC & 0xff);
  451. cx25840_write(client, 0x41d, VSC >> 8);
  452. /* VS_INTRLACE=1 VFILT=filter */
  453. cx25840_write(client, 0x41e, 0x8 | filter);
  454. break;
  455. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  456. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  457. case V4L2_BUF_TYPE_VBI_CAPTURE:
  458. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  459. default:
  460. return -EINVAL;
  461. }
  462. return 0;
  463. }
  464. /* ----------------------------------------------------------------------- */
  465. static struct v4l2_queryctrl cx25840_qctrl[] = {
  466. {
  467. .id = V4L2_CID_BRIGHTNESS,
  468. .type = V4L2_CTRL_TYPE_INTEGER,
  469. .name = "Brightness",
  470. .minimum = 0,
  471. .maximum = 255,
  472. .step = 1,
  473. .default_value = 128,
  474. .flags = 0,
  475. }, {
  476. .id = V4L2_CID_CONTRAST,
  477. .type = V4L2_CTRL_TYPE_INTEGER,
  478. .name = "Contrast",
  479. .minimum = 0,
  480. .maximum = 127,
  481. .step = 1,
  482. .default_value = 64,
  483. .flags = 0,
  484. }, {
  485. .id = V4L2_CID_SATURATION,
  486. .type = V4L2_CTRL_TYPE_INTEGER,
  487. .name = "Saturation",
  488. .minimum = 0,
  489. .maximum = 127,
  490. .step = 1,
  491. .default_value = 64,
  492. .flags = 0,
  493. }, {
  494. .id = V4L2_CID_HUE,
  495. .type = V4L2_CTRL_TYPE_INTEGER,
  496. .name = "Hue",
  497. .minimum = -128,
  498. .maximum = 127,
  499. .step = 1,
  500. .default_value = 0,
  501. .flags = 0,
  502. }, {
  503. .id = V4L2_CID_AUDIO_VOLUME,
  504. .type = V4L2_CTRL_TYPE_INTEGER,
  505. .name = "Volume",
  506. .minimum = 0,
  507. .maximum = 65535,
  508. .step = 65535/100,
  509. .default_value = 58880,
  510. .flags = 0,
  511. }, {
  512. .id = V4L2_CID_AUDIO_BALANCE,
  513. .type = V4L2_CTRL_TYPE_INTEGER,
  514. .name = "Balance",
  515. .minimum = 0,
  516. .maximum = 65535,
  517. .step = 65535/100,
  518. .default_value = 32768,
  519. .flags = 0,
  520. }, {
  521. .id = V4L2_CID_AUDIO_MUTE,
  522. .type = V4L2_CTRL_TYPE_BOOLEAN,
  523. .name = "Mute",
  524. .minimum = 0,
  525. .maximum = 1,
  526. .step = 1,
  527. .default_value = 1,
  528. .flags = 0,
  529. }, {
  530. .id = V4L2_CID_AUDIO_BASS,
  531. .type = V4L2_CTRL_TYPE_INTEGER,
  532. .name = "Bass",
  533. .minimum = 0,
  534. .maximum = 65535,
  535. .step = 65535/100,
  536. .default_value = 32768,
  537. }, {
  538. .id = V4L2_CID_AUDIO_TREBLE,
  539. .type = V4L2_CTRL_TYPE_INTEGER,
  540. .name = "Treble",
  541. .minimum = 0,
  542. .maximum = 65535,
  543. .step = 65535/100,
  544. .default_value = 32768,
  545. },
  546. };
  547. /* ----------------------------------------------------------------------- */
  548. static int cx25840_command(struct i2c_client *client, unsigned int cmd,
  549. void *arg)
  550. {
  551. struct cx25840_state *state = i2c_get_clientdata(client);
  552. struct v4l2_tuner *vt = arg;
  553. switch (cmd) {
  554. #ifdef CONFIG_VIDEO_ADV_DEBUG
  555. /* ioctls to allow direct access to the
  556. * cx25840 registers for testing */
  557. case VIDIOC_INT_G_REGISTER:
  558. {
  559. struct v4l2_register *reg = arg;
  560. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  561. return -EINVAL;
  562. reg->val = cx25840_read(client, reg->reg & 0x0fff);
  563. break;
  564. }
  565. case VIDIOC_INT_S_REGISTER:
  566. {
  567. struct v4l2_register *reg = arg;
  568. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  569. return -EINVAL;
  570. if (!capable(CAP_SYS_ADMIN))
  571. return -EPERM;
  572. cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
  573. break;
  574. }
  575. #endif
  576. case VIDIOC_INT_DECODE_VBI_LINE:
  577. return cx25840_vbi(client, cmd, arg);
  578. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  579. return cx25840_audio(client, cmd, arg);
  580. case VIDIOC_STREAMON:
  581. v4l_dbg(1, cx25840_debug, client, "enable output\n");
  582. cx25840_write(client, 0x115, 0x8c);
  583. cx25840_write(client, 0x116, 0x07);
  584. break;
  585. case VIDIOC_STREAMOFF:
  586. v4l_dbg(1, cx25840_debug, client, "disable output\n");
  587. cx25840_write(client, 0x115, 0x00);
  588. cx25840_write(client, 0x116, 0x00);
  589. break;
  590. case VIDIOC_LOG_STATUS:
  591. log_status(client);
  592. break;
  593. case VIDIOC_G_CTRL:
  594. return get_v4lctrl(client, (struct v4l2_control *)arg);
  595. case VIDIOC_S_CTRL:
  596. return set_v4lctrl(client, (struct v4l2_control *)arg);
  597. case VIDIOC_QUERYCTRL:
  598. {
  599. struct v4l2_queryctrl *qc = arg;
  600. int i;
  601. for (i = 0; i < ARRAY_SIZE(cx25840_qctrl); i++)
  602. if (qc->id && qc->id == cx25840_qctrl[i].id) {
  603. memcpy(qc, &cx25840_qctrl[i], sizeof(*qc));
  604. return 0;
  605. }
  606. return -EINVAL;
  607. }
  608. case VIDIOC_G_STD:
  609. *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
  610. break;
  611. case VIDIOC_S_STD:
  612. state->radio = 0;
  613. return set_v4lstd(client, *(v4l2_std_id *)arg);
  614. case AUDC_SET_RADIO:
  615. state->radio = 1;
  616. break;
  617. case VIDIOC_G_INPUT:
  618. *(int *)arg = state->vid_input;
  619. break;
  620. case VIDIOC_S_INPUT:
  621. return set_input(client, *(enum cx25840_video_input *)arg, state->aud_input);
  622. case VIDIOC_S_AUDIO:
  623. {
  624. struct v4l2_audio *input = arg;
  625. return set_input(client, state->vid_input, input->index);
  626. }
  627. case VIDIOC_G_AUDIO:
  628. {
  629. struct v4l2_audio *input = arg;
  630. memset(input, 0, sizeof(*input));
  631. input->index = state->aud_input;
  632. input->capability = V4L2_AUDCAP_STEREO;
  633. break;
  634. }
  635. case VIDIOC_S_FREQUENCY:
  636. input_change(client);
  637. break;
  638. case VIDIOC_G_TUNER:
  639. {
  640. u8 mode = cx25840_read(client, 0x804);
  641. u8 vpres = cx25840_read(client, 0x80a) & 0x10;
  642. int val = 0;
  643. if (state->radio)
  644. break;
  645. vt->capability |=
  646. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  647. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  648. vt->signal = vpres ? 0xffff : 0x0;
  649. /* get rxsubchans and audmode */
  650. if ((mode & 0xf) == 1)
  651. val |= V4L2_TUNER_SUB_STEREO;
  652. else
  653. val |= V4L2_TUNER_SUB_MONO;
  654. if (mode == 2 || mode == 4)
  655. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  656. if (mode & 0x10)
  657. val |= V4L2_TUNER_SUB_SAP;
  658. vt->rxsubchans = val;
  659. vt->audmode = state->audmode;
  660. break;
  661. }
  662. case VIDIOC_S_TUNER:
  663. if (state->radio)
  664. break;
  665. switch (vt->audmode) {
  666. case V4L2_TUNER_MODE_MONO:
  667. /* mono -> mono
  668. stereo -> mono
  669. bilingual -> lang1 */
  670. cx25840_and_or(client, 0x809, ~0xf, 0x00);
  671. break;
  672. case V4L2_TUNER_MODE_LANG1:
  673. /* mono -> mono
  674. stereo -> stereo
  675. bilingual -> lang1 */
  676. cx25840_and_or(client, 0x809, ~0xf, 0x04);
  677. break;
  678. case V4L2_TUNER_MODE_STEREO:
  679. /* mono -> mono
  680. stereo -> stereo
  681. bilingual -> lang1/lang2 */
  682. cx25840_and_or(client, 0x809, ~0xf, 0x07);
  683. break;
  684. case V4L2_TUNER_MODE_LANG2:
  685. /* mono -> mono
  686. stereo ->stereo
  687. bilingual -> lang2 */
  688. cx25840_and_or(client, 0x809, ~0xf, 0x01);
  689. break;
  690. default:
  691. return -EINVAL;
  692. }
  693. state->audmode = vt->audmode;
  694. break;
  695. case VIDIOC_G_FMT:
  696. return get_v4lfmt(client, (struct v4l2_format *)arg);
  697. case VIDIOC_S_FMT:
  698. return set_v4lfmt(client, (struct v4l2_format *)arg);
  699. case VIDIOC_INT_RESET:
  700. cx25840_initialize(client, 0);
  701. break;
  702. case VIDIOC_INT_G_CHIP_IDENT:
  703. *(enum v4l2_chip_ident *)arg =
  704. V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf);
  705. break;
  706. default:
  707. return -EINVAL;
  708. }
  709. return 0;
  710. }
  711. /* ----------------------------------------------------------------------- */
  712. static struct i2c_driver i2c_driver_cx25840;
  713. static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
  714. int kind)
  715. {
  716. struct i2c_client *client;
  717. struct cx25840_state *state;
  718. u16 device_id;
  719. /* Check if the adapter supports the needed features
  720. * Not until kernel version 2.6.11 did the bit-algo
  721. * correctly report that it would do an I2C-level xfer */
  722. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  723. return 0;
  724. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  725. if (client == 0)
  726. return -ENOMEM;
  727. client->addr = address;
  728. client->adapter = adapter;
  729. client->driver = &i2c_driver_cx25840;
  730. snprintf(client->name, sizeof(client->name) - 1, "cx25840");
  731. v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", address << 1);
  732. device_id = cx25840_read(client, 0x101) << 8;
  733. device_id |= cx25840_read(client, 0x100);
  734. /* The high byte of the device ID should be
  735. * 0x84 if chip is present */
  736. if ((device_id & 0xff00) != 0x8400) {
  737. v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
  738. kfree(client);
  739. return 0;
  740. }
  741. v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
  742. (device_id & 0xfff0) >> 4,
  743. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
  744. address << 1, adapter->name);
  745. state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  746. if (state == NULL) {
  747. kfree(client);
  748. return -ENOMEM;
  749. }
  750. i2c_set_clientdata(client, state);
  751. memset(state, 0, sizeof(struct cx25840_state));
  752. state->vid_input = CX25840_COMPOSITE7;
  753. state->aud_input = CX25840_AUDIO8;
  754. state->audclk_freq = 48000;
  755. state->pvr150_workaround = 0;
  756. state->audmode = V4L2_TUNER_MODE_LANG1;
  757. cx25840_initialize(client, 1);
  758. i2c_attach_client(client);
  759. return 0;
  760. }
  761. static int cx25840_attach_adapter(struct i2c_adapter *adapter)
  762. {
  763. if (adapter->class & I2C_CLASS_TV_ANALOG)
  764. return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
  765. return 0;
  766. }
  767. static int cx25840_detach_client(struct i2c_client *client)
  768. {
  769. struct cx25840_state *state = i2c_get_clientdata(client);
  770. int err;
  771. err = i2c_detach_client(client);
  772. if (err) {
  773. return err;
  774. }
  775. kfree(state);
  776. kfree(client);
  777. return 0;
  778. }
  779. /* ----------------------------------------------------------------------- */
  780. static struct i2c_driver i2c_driver_cx25840 = {
  781. .driver = {
  782. .name = "cx25840",
  783. },
  784. .id = I2C_DRIVERID_CX25840,
  785. .attach_adapter = cx25840_attach_adapter,
  786. .detach_client = cx25840_detach_client,
  787. .command = cx25840_command,
  788. };
  789. static int __init m__init(void)
  790. {
  791. return i2c_add_driver(&i2c_driver_cx25840);
  792. }
  793. static void __exit m__exit(void)
  794. {
  795. i2c_del_driver(&i2c_driver_cx25840);
  796. }
  797. module_init(m__init);
  798. module_exit(m__exit);
  799. /* ----------------------------------------------------------------------- */
  800. static void log_status(struct i2c_client *client)
  801. {
  802. static const char *const fmt_strs[] = {
  803. "0x0",
  804. "NTSC-M", "NTSC-J", "NTSC-4.43",
  805. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  806. "0x9", "0xA", "0xB",
  807. "SECAM",
  808. "0xD", "0xE", "0xF"
  809. };
  810. struct cx25840_state *state = i2c_get_clientdata(client);
  811. u8 microctrl_vidfmt = cx25840_read(client, 0x80a);
  812. u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
  813. u8 gen_stat1 = cx25840_read(client, 0x40d);
  814. u8 download_ctl = cx25840_read(client, 0x803);
  815. u8 mod_det_stat0 = cx25840_read(client, 0x804);
  816. u8 mod_det_stat1 = cx25840_read(client, 0x805);
  817. u8 audio_config = cx25840_read(client, 0x808);
  818. u8 pref_mode = cx25840_read(client, 0x809);
  819. u8 afc0 = cx25840_read(client, 0x80b);
  820. u8 mute_ctl = cx25840_read(client, 0x8d3);
  821. int vid_input = state->vid_input;
  822. int aud_input = state->aud_input;
  823. char *p;
  824. v4l_info(client, "Video signal: %spresent\n",
  825. (microctrl_vidfmt & 0x10) ? "" : "not ");
  826. v4l_info(client, "Detected format: %s\n",
  827. fmt_strs[gen_stat1 & 0xf]);
  828. switch (mod_det_stat0) {
  829. case 0x00: p = "mono"; break;
  830. case 0x01: p = "stereo"; break;
  831. case 0x02: p = "dual"; break;
  832. case 0x04: p = "tri"; break;
  833. case 0x10: p = "mono with SAP"; break;
  834. case 0x11: p = "stereo with SAP"; break;
  835. case 0x12: p = "dual with SAP"; break;
  836. case 0x14: p = "tri with SAP"; break;
  837. case 0xfe: p = "forced mode"; break;
  838. default: p = "not defined";
  839. }
  840. v4l_info(client, "Detected audio mode: %s\n", p);
  841. switch (mod_det_stat1) {
  842. case 0x00: p = "not defined"; break;
  843. case 0x01: p = "EIAJ"; break;
  844. case 0x02: p = "A2-M"; break;
  845. case 0x03: p = "A2-BG"; break;
  846. case 0x04: p = "A2-DK1"; break;
  847. case 0x05: p = "A2-DK2"; break;
  848. case 0x06: p = "A2-DK3"; break;
  849. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  850. case 0x08: p = "AM-L"; break;
  851. case 0x09: p = "NICAM-BG"; break;
  852. case 0x0a: p = "NICAM-DK"; break;
  853. case 0x0b: p = "NICAM-I"; break;
  854. case 0x0c: p = "NICAM-L"; break;
  855. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  856. case 0x0e: p = "IF FM Radio"; break;
  857. case 0x0f: p = "BTSC"; break;
  858. case 0x10: p = "high-deviation FM"; break;
  859. case 0x11: p = "very high-deviation FM"; break;
  860. case 0xfd: p = "unknown audio standard"; break;
  861. case 0xfe: p = "forced audio standard"; break;
  862. case 0xff: p = "no detected audio standard"; break;
  863. default: p = "not defined";
  864. }
  865. v4l_info(client, "Detected audio standard: %s\n", p);
  866. v4l_info(client, "Audio muted: %s\n",
  867. (mute_ctl & 0x2) ? "yes" : "no");
  868. v4l_info(client, "Audio microcontroller: %s\n",
  869. (download_ctl & 0x10) ? "running" : "stopped");
  870. switch (audio_config >> 4) {
  871. case 0x00: p = "undefined"; break;
  872. case 0x01: p = "BTSC"; break;
  873. case 0x02: p = "EIAJ"; break;
  874. case 0x03: p = "A2-M"; break;
  875. case 0x04: p = "A2-BG"; break;
  876. case 0x05: p = "A2-DK1"; break;
  877. case 0x06: p = "A2-DK2"; break;
  878. case 0x07: p = "A2-DK3"; break;
  879. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  880. case 0x09: p = "AM-L"; break;
  881. case 0x0a: p = "NICAM-BG"; break;
  882. case 0x0b: p = "NICAM-DK"; break;
  883. case 0x0c: p = "NICAM-I"; break;
  884. case 0x0d: p = "NICAM-L"; break;
  885. case 0x0e: p = "FM radio"; break;
  886. case 0x0f: p = "automatic detection"; break;
  887. default: p = "undefined";
  888. }
  889. v4l_info(client, "Configured audio standard: %s\n", p);
  890. if ((audio_config >> 4) < 0xF) {
  891. switch (audio_config & 0xF) {
  892. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  893. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  894. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  895. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  896. case 0x04: p = "STEREO"; break;
  897. case 0x05: p = "DUAL1 (AB)"; break;
  898. case 0x06: p = "DUAL2 (AC) (FM)"; break;
  899. case 0x07: p = "DUAL3 (BC) (FM)"; break;
  900. case 0x08: p = "DUAL4 (AC) (AM)"; break;
  901. case 0x09: p = "DUAL5 (BC) (AM)"; break;
  902. case 0x0a: p = "SAP"; break;
  903. default: p = "undefined";
  904. }
  905. v4l_info(client, "Configured audio mode: %s\n", p);
  906. } else {
  907. switch (audio_config & 0xF) {
  908. case 0x00: p = "BG"; break;
  909. case 0x01: p = "DK1"; break;
  910. case 0x02: p = "DK2"; break;
  911. case 0x03: p = "DK3"; break;
  912. case 0x04: p = "I"; break;
  913. case 0x05: p = "L"; break;
  914. case 0x06: p = "BTSC"; break;
  915. case 0x07: p = "EIAJ"; break;
  916. case 0x08: p = "A2-M"; break;
  917. case 0x09: p = "FM Radio"; break;
  918. case 0x0f: p = "automatic standard and mode detection"; break;
  919. default: p = "undefined";
  920. }
  921. v4l_info(client, "Configured audio system: %s\n", p);
  922. }
  923. v4l_info(client, "Specified standard: %s\n",
  924. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  925. if (vid_input >= CX25840_COMPOSITE1 &&
  926. vid_input <= CX25840_COMPOSITE8) {
  927. v4l_info(client, "Specified video input: Composite %d\n",
  928. vid_input - CX25840_COMPOSITE1 + 1);
  929. } else {
  930. v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  931. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  932. }
  933. if (aud_input) {
  934. v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
  935. } else {
  936. v4l_info(client, "Specified audio input: External\n");
  937. }
  938. v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
  939. switch (pref_mode & 0xf) {
  940. case 0: p = "mono/language A"; break;
  941. case 1: p = "language B"; break;
  942. case 2: p = "language C"; break;
  943. case 3: p = "analog fallback"; break;
  944. case 4: p = "stereo"; break;
  945. case 5: p = "language AC"; break;
  946. case 6: p = "language BC"; break;
  947. case 7: p = "language AB"; break;
  948. default: p = "undefined";
  949. }
  950. v4l_info(client, "Preferred audio mode: %s\n", p);
  951. if ((audio_config & 0xf) == 0xf) {
  952. switch ((afc0 >> 3) & 0x3) {
  953. case 0: p = "system DK"; break;
  954. case 1: p = "system L"; break;
  955. case 2: p = "autodetect"; break;
  956. default: p = "undefined";
  957. }
  958. v4l_info(client, "Selected 65 MHz format: %s\n", p);
  959. switch (afc0 & 0x7) {
  960. case 0: p = "chroma"; break;
  961. case 1: p = "BTSC"; break;
  962. case 2: p = "EIAJ"; break;
  963. case 3: p = "A2-M"; break;
  964. case 4: p = "autodetect"; break;
  965. default: p = "undefined";
  966. }
  967. v4l_info(client, "Selected 45 MHz format: %s\n", p);
  968. }
  969. }