cx25840-core.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. int cx25840_debug = 0;
  40. module_param(cx25840_debug, bool, 0644);
  41. MODULE_PARM_DESC(cx25840_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 inline 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 inline 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. /* NTSC */
  187. if (state->pvr150_workaround) {
  188. /* Certain Hauppauge PVR150 models have a hardware bug
  189. that causes audio to drop out. For these models the
  190. audio standard must be set explicitly.
  191. To be precise: it affects cards with tuner models
  192. 85, 99 and 112 (model numbers from tveeprom). */
  193. if (std == V4L2_STD_NTSC_M_JP) {
  194. /* Japan uses EIAJ audio standard */
  195. cx25840_write(client, 0x808, 0x2f);
  196. } else {
  197. /* Others use the BTSC audio standard */
  198. cx25840_write(client, 0x808, 0x1f);
  199. }
  200. /* South Korea uses the A2-M (aka Zweiton M) audio
  201. standard, and should set 0x808 to 0x3f, but I don't
  202. know how to detect this. */
  203. } else if (std == V4L2_STD_NTSC_M_JP) {
  204. /* Japan uses EIAJ audio standard */
  205. cx25840_write(client, 0x808, 0xf7);
  206. } else {
  207. /* Others use the BTSC audio standard */
  208. cx25840_write(client, 0x808, 0xf6);
  209. }
  210. /* South Korea uses the A2-M (aka Zweiton M) audio standard,
  211. and should set 0x808 to 0xf8, but I don't know how to
  212. detect this. */
  213. cx25840_write(client, 0x80b, 0x00);
  214. }
  215. if (cx25840_read(client, 0x803) & 0x10) {
  216. /* restart audio decoder microcontroller */
  217. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  218. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  219. }
  220. }
  221. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  222. enum cx25840_audio_input aud_input)
  223. {
  224. struct cx25840_state *state = i2c_get_clientdata(client);
  225. u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
  226. vid_input <= CX25840_COMPOSITE8);
  227. u8 reg;
  228. cx25840_dbg("decoder set video input %d, audio input %d\n",
  229. vid_input, aud_input);
  230. if (is_composite) {
  231. reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
  232. } else {
  233. int luma = vid_input & 0xf0;
  234. int chroma = vid_input & 0xf00;
  235. if ((vid_input & ~0xff0) ||
  236. luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
  237. chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
  238. cx25840_err("0x%04x is not a valid video input!\n", vid_input);
  239. return -EINVAL;
  240. }
  241. reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
  242. if (chroma >= CX25840_SVIDEO_CHROMA7) {
  243. reg &= 0x3f;
  244. reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
  245. } else {
  246. reg &= 0xcf;
  247. reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
  248. }
  249. }
  250. switch (aud_input) {
  251. case CX25840_AUDIO_SERIAL:
  252. /* do nothing, use serial audio input */
  253. break;
  254. case CX25840_AUDIO4: reg &= ~0x30; break;
  255. case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
  256. case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
  257. case CX25840_AUDIO7: reg &= ~0xc0; break;
  258. case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
  259. default:
  260. cx25840_err("0x%04x is not a valid audio input!\n", aud_input);
  261. return -EINVAL;
  262. }
  263. cx25840_write(client, 0x103, reg);
  264. /* Set INPUT_MODE to Composite (0) or S-Video (1) */
  265. cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
  266. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  267. cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
  268. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
  269. if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
  270. cx25840_and_or(client, 0x102, ~0x4, 4);
  271. else
  272. cx25840_and_or(client, 0x102, ~0x4, 0);
  273. state->vid_input = vid_input;
  274. state->aud_input = aud_input;
  275. cx25840_audio_set_path(client);
  276. input_change(client);
  277. return 0;
  278. }
  279. /* ----------------------------------------------------------------------- */
  280. static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
  281. {
  282. u8 fmt=0; /* zero is autodetect */
  283. /* First tests should be against specific std */
  284. if (std & V4L2_STD_NTSC_M_JP) {
  285. fmt=0x2;
  286. } else if (std & V4L2_STD_NTSC_443) {
  287. fmt=0x3;
  288. } else if (std & V4L2_STD_PAL_M) {
  289. fmt=0x5;
  290. } else if (std & V4L2_STD_PAL_N) {
  291. fmt=0x6;
  292. } else if (std & V4L2_STD_PAL_Nc) {
  293. fmt=0x7;
  294. } else if (std & V4L2_STD_PAL_60) {
  295. fmt=0x8;
  296. } else {
  297. /* Then, test against generic ones */
  298. if (std & V4L2_STD_NTSC) {
  299. fmt=0x1;
  300. } else if (std & V4L2_STD_PAL) {
  301. fmt=0x4;
  302. } else if (std & V4L2_STD_SECAM) {
  303. fmt=0xc;
  304. }
  305. }
  306. cx25840_and_or(client, 0x400, ~0xf, fmt);
  307. cx25840_vbi_setup(client);
  308. return 0;
  309. }
  310. v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
  311. {
  312. /* check VID_FMT_SEL first */
  313. u8 fmt = cx25840_read(client, 0x400) & 0xf;
  314. if (!fmt) {
  315. /* check AFD_FMT_STAT if set to autodetect */
  316. fmt = cx25840_read(client, 0x40d) & 0xf;
  317. }
  318. switch (fmt) {
  319. case 0x1: return V4L2_STD_NTSC_M;
  320. case 0x2: return V4L2_STD_NTSC_M_JP;
  321. case 0x3: return V4L2_STD_NTSC_443;
  322. case 0x4: return V4L2_STD_PAL;
  323. case 0x5: return V4L2_STD_PAL_M;
  324. case 0x6: return V4L2_STD_PAL_N;
  325. case 0x7: return V4L2_STD_PAL_Nc;
  326. case 0x8: return V4L2_STD_PAL_60;
  327. case 0xc: return V4L2_STD_SECAM;
  328. default: return V4L2_STD_UNKNOWN;
  329. }
  330. }
  331. /* ----------------------------------------------------------------------- */
  332. static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  333. {
  334. struct cx25840_state *state = i2c_get_clientdata(client);
  335. switch (ctrl->id) {
  336. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  337. state->pvr150_workaround = ctrl->value;
  338. set_input(client, state->vid_input, state->aud_input);
  339. break;
  340. case V4L2_CID_BRIGHTNESS:
  341. if (ctrl->value < 0 || ctrl->value > 255) {
  342. cx25840_err("invalid brightness setting %d\n",
  343. ctrl->value);
  344. return -ERANGE;
  345. }
  346. cx25840_write(client, 0x414, ctrl->value - 128);
  347. break;
  348. case V4L2_CID_CONTRAST:
  349. if (ctrl->value < 0 || ctrl->value > 127) {
  350. cx25840_err("invalid contrast setting %d\n",
  351. ctrl->value);
  352. return -ERANGE;
  353. }
  354. cx25840_write(client, 0x415, ctrl->value << 1);
  355. break;
  356. case V4L2_CID_SATURATION:
  357. if (ctrl->value < 0 || ctrl->value > 127) {
  358. cx25840_err("invalid saturation setting %d\n",
  359. ctrl->value);
  360. return -ERANGE;
  361. }
  362. cx25840_write(client, 0x420, ctrl->value << 1);
  363. cx25840_write(client, 0x421, ctrl->value << 1);
  364. break;
  365. case V4L2_CID_HUE:
  366. if (ctrl->value < -127 || ctrl->value > 127) {
  367. cx25840_err("invalid hue setting %d\n", ctrl->value);
  368. return -ERANGE;
  369. }
  370. cx25840_write(client, 0x422, ctrl->value);
  371. break;
  372. case V4L2_CID_AUDIO_VOLUME:
  373. case V4L2_CID_AUDIO_BASS:
  374. case V4L2_CID_AUDIO_TREBLE:
  375. case V4L2_CID_AUDIO_BALANCE:
  376. case V4L2_CID_AUDIO_MUTE:
  377. return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
  378. }
  379. return 0;
  380. }
  381. static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  382. {
  383. struct cx25840_state *state = i2c_get_clientdata(client);
  384. switch (ctrl->id) {
  385. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  386. ctrl->value = state->pvr150_workaround;
  387. break;
  388. case V4L2_CID_BRIGHTNESS:
  389. ctrl->value = cx25840_read(client, 0x414) + 128;
  390. break;
  391. case V4L2_CID_CONTRAST:
  392. ctrl->value = cx25840_read(client, 0x415) >> 1;
  393. break;
  394. case V4L2_CID_SATURATION:
  395. ctrl->value = cx25840_read(client, 0x420) >> 1;
  396. break;
  397. case V4L2_CID_HUE:
  398. ctrl->value = cx25840_read(client, 0x422);
  399. break;
  400. case V4L2_CID_AUDIO_VOLUME:
  401. case V4L2_CID_AUDIO_BASS:
  402. case V4L2_CID_AUDIO_TREBLE:
  403. case V4L2_CID_AUDIO_BALANCE:
  404. case V4L2_CID_AUDIO_MUTE:
  405. return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
  406. default:
  407. return -EINVAL;
  408. }
  409. return 0;
  410. }
  411. /* ----------------------------------------------------------------------- */
  412. static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  413. {
  414. switch (fmt->type) {
  415. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  416. return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
  417. default:
  418. return -EINVAL;
  419. }
  420. return 0;
  421. }
  422. static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  423. {
  424. struct v4l2_pix_format *pix;
  425. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  426. int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
  427. switch (fmt->type) {
  428. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  429. pix = &(fmt->fmt.pix);
  430. Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
  431. Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
  432. Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
  433. Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
  434. Vlines = pix->height + (is_pal ? 4 : 7);
  435. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  436. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  437. cx25840_err("%dx%d is not a valid size!\n",
  438. pix->width, pix->height);
  439. return -ERANGE;
  440. }
  441. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  442. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  443. VSC &= 0x1fff;
  444. if (pix->width >= 385)
  445. filter = 0;
  446. else if (pix->width > 192)
  447. filter = 1;
  448. else if (pix->width > 96)
  449. filter = 2;
  450. else
  451. filter = 3;
  452. cx25840_dbg("decoder set size %dx%d -> scale %ux%u\n",
  453. pix->width, pix->height, HSC, VSC);
  454. /* HSCALE=HSC */
  455. cx25840_write(client, 0x418, HSC & 0xff);
  456. cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
  457. cx25840_write(client, 0x41a, HSC >> 16);
  458. /* VSCALE=VSC */
  459. cx25840_write(client, 0x41c, VSC & 0xff);
  460. cx25840_write(client, 0x41d, VSC >> 8);
  461. /* VS_INTRLACE=1 VFILT=filter */
  462. cx25840_write(client, 0x41e, 0x8 | filter);
  463. break;
  464. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  465. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  466. case V4L2_BUF_TYPE_VBI_CAPTURE:
  467. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  468. default:
  469. return -EINVAL;
  470. }
  471. return 0;
  472. }
  473. /* ----------------------------------------------------------------------- */
  474. static int cx25840_command(struct i2c_client *client, unsigned int cmd,
  475. void *arg)
  476. {
  477. struct cx25840_state *state = i2c_get_clientdata(client);
  478. struct v4l2_tuner *vt = arg;
  479. int result = 0;
  480. switch (cmd) {
  481. case 0:
  482. break;
  483. #ifdef CONFIG_VIDEO_ADV_DEBUG
  484. /* ioctls to allow direct access to the
  485. * cx25840 registers for testing */
  486. case VIDIOC_INT_G_REGISTER:
  487. {
  488. struct v4l2_register *reg = arg;
  489. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  490. return -EINVAL;
  491. reg->val = cx25840_read(client, reg->reg & 0x0fff);
  492. break;
  493. }
  494. case VIDIOC_INT_S_REGISTER:
  495. {
  496. struct v4l2_register *reg = arg;
  497. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  498. return -EINVAL;
  499. if (!capable(CAP_SYS_ADMIN))
  500. return -EPERM;
  501. cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
  502. break;
  503. }
  504. #endif
  505. case VIDIOC_INT_DECODE_VBI_LINE:
  506. return cx25840_vbi(client, cmd, arg);
  507. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  508. result = cx25840_audio(client, cmd, arg);
  509. break;
  510. case VIDIOC_STREAMON:
  511. cx25840_dbg("enable output\n");
  512. cx25840_write(client, 0x115, 0x8c);
  513. cx25840_write(client, 0x116, 0x07);
  514. break;
  515. case VIDIOC_STREAMOFF:
  516. cx25840_dbg("disable output\n");
  517. cx25840_write(client, 0x115, 0x00);
  518. cx25840_write(client, 0x116, 0x00);
  519. break;
  520. case VIDIOC_LOG_STATUS:
  521. log_status(client);
  522. break;
  523. case VIDIOC_G_CTRL:
  524. result = get_v4lctrl(client, (struct v4l2_control *)arg);
  525. break;
  526. case VIDIOC_S_CTRL:
  527. result = set_v4lctrl(client, (struct v4l2_control *)arg);
  528. break;
  529. case VIDIOC_G_STD:
  530. *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
  531. break;
  532. case VIDIOC_S_STD:
  533. result = set_v4lstd(client, *(v4l2_std_id *)arg);
  534. break;
  535. case VIDIOC_G_INPUT:
  536. *(int *)arg = state->vid_input;
  537. break;
  538. case VIDIOC_S_INPUT:
  539. result = set_input(client, *(enum cx25840_video_input *)arg, state->aud_input);
  540. break;
  541. case VIDIOC_S_AUDIO:
  542. {
  543. struct v4l2_audio *input = arg;
  544. result = set_input(client, state->vid_input, input->index);
  545. break;
  546. }
  547. case VIDIOC_G_AUDIO:
  548. {
  549. struct v4l2_audio *input = arg;
  550. memset(input, 0, sizeof(*input));
  551. input->index = state->aud_input;
  552. break;
  553. }
  554. case VIDIOC_S_FREQUENCY:
  555. input_change(client);
  556. break;
  557. case VIDIOC_G_TUNER:
  558. {
  559. u8 mode = cx25840_read(client, 0x804);
  560. u8 pref = cx25840_read(client, 0x809) & 0xf;
  561. u8 vpres = cx25840_read(client, 0x80a) & 0x10;
  562. int val = 0;
  563. vt->capability |=
  564. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  565. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  566. vt->signal = vpres ? 0xffff : 0x0;
  567. /* get rxsubchans and audmode */
  568. if ((mode & 0xf) == 1)
  569. val |= V4L2_TUNER_SUB_STEREO;
  570. else
  571. val |= V4L2_TUNER_SUB_MONO;
  572. if (mode == 2 || mode == 4)
  573. val |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  574. if (mode & 0x10)
  575. val |= V4L2_TUNER_SUB_SAP;
  576. vt->rxsubchans = val;
  577. switch (pref) {
  578. case 0:
  579. vt->audmode = V4L2_TUNER_MODE_MONO;
  580. break;
  581. case 1:
  582. case 2:
  583. vt->audmode = V4L2_TUNER_MODE_LANG2;
  584. break;
  585. case 4:
  586. default:
  587. vt->audmode = V4L2_TUNER_MODE_STEREO;
  588. }
  589. break;
  590. }
  591. case VIDIOC_S_TUNER:
  592. switch (vt->audmode) {
  593. case V4L2_TUNER_MODE_MONO:
  594. case V4L2_TUNER_MODE_LANG1:
  595. /* Force PREF_MODE to MONO */
  596. cx25840_and_or(client, 0x809, ~0xf, 0x00);
  597. break;
  598. case V4L2_TUNER_MODE_STEREO:
  599. /* Force PREF_MODE to STEREO */
  600. cx25840_and_or(client, 0x809, ~0xf, 0x04);
  601. break;
  602. case V4L2_TUNER_MODE_LANG2:
  603. /* Force PREF_MODE to LANG2 */
  604. cx25840_and_or(client, 0x809, ~0xf, 0x01);
  605. break;
  606. }
  607. break;
  608. case VIDIOC_G_FMT:
  609. result = get_v4lfmt(client, (struct v4l2_format *)arg);
  610. break;
  611. case VIDIOC_S_FMT:
  612. result = set_v4lfmt(client, (struct v4l2_format *)arg);
  613. break;
  614. case VIDIOC_INT_RESET:
  615. cx25840_initialize(client, 0);
  616. break;
  617. case VIDIOC_INT_G_CHIP_IDENT:
  618. *(enum v4l2_chip_ident *)arg =
  619. V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf);
  620. break;
  621. default:
  622. cx25840_err("invalid ioctl %x\n", cmd);
  623. return -EINVAL;
  624. }
  625. return result;
  626. }
  627. /* ----------------------------------------------------------------------- */
  628. static struct i2c_driver i2c_driver_cx25840;
  629. static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
  630. int kind)
  631. {
  632. struct i2c_client *client;
  633. struct cx25840_state *state;
  634. u16 device_id;
  635. /* Check if the adapter supports the needed features
  636. * Not until kernel version 2.6.11 did the bit-algo
  637. * correctly report that it would do an I2C-level xfer */
  638. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  639. return 0;
  640. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  641. if (client == 0)
  642. return -ENOMEM;
  643. memset(client, 0, sizeof(struct i2c_client));
  644. client->addr = address;
  645. client->adapter = adapter;
  646. client->driver = &i2c_driver_cx25840;
  647. snprintf(client->name, sizeof(client->name) - 1, "cx25840");
  648. cx25840_dbg("detecting cx25840 client on address 0x%x\n", address << 1);
  649. device_id = cx25840_read(client, 0x101) << 8;
  650. device_id |= cx25840_read(client, 0x100);
  651. /* The high byte of the device ID should be
  652. * 0x84 if chip is present */
  653. if ((device_id & 0xff00) != 0x8400) {
  654. cx25840_dbg("cx25840 not found\n");
  655. kfree(client);
  656. return 0;
  657. }
  658. cx25840_info("cx25%3x-2%x found @ 0x%x (%s)\n",
  659. (device_id & 0xfff0) >> 4,
  660. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
  661. address << 1, adapter->name);
  662. state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  663. if (state == NULL) {
  664. kfree(client);
  665. return -ENOMEM;
  666. }
  667. i2c_set_clientdata(client, state);
  668. memset(state, 0, sizeof(struct cx25840_state));
  669. state->vid_input = CX25840_COMPOSITE7;
  670. state->aud_input = CX25840_AUDIO8;
  671. state->audclk_freq = 48000;
  672. state->pvr150_workaround = 0;
  673. cx25840_initialize(client, 1);
  674. i2c_attach_client(client);
  675. return 0;
  676. }
  677. static int cx25840_attach_adapter(struct i2c_adapter *adapter)
  678. {
  679. if (adapter->class & I2C_CLASS_TV_ANALOG)
  680. return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
  681. return 0;
  682. }
  683. static int cx25840_detach_client(struct i2c_client *client)
  684. {
  685. struct cx25840_state *state = i2c_get_clientdata(client);
  686. int err;
  687. err = i2c_detach_client(client);
  688. if (err) {
  689. return err;
  690. }
  691. kfree(state);
  692. kfree(client);
  693. return 0;
  694. }
  695. /* ----------------------------------------------------------------------- */
  696. static struct i2c_driver i2c_driver_cx25840 = {
  697. .driver = {
  698. .name = "cx25840",
  699. },
  700. .id = I2C_DRIVERID_CX25840,
  701. .attach_adapter = cx25840_attach_adapter,
  702. .detach_client = cx25840_detach_client,
  703. .command = cx25840_command,
  704. };
  705. static int __init m__init(void)
  706. {
  707. return i2c_add_driver(&i2c_driver_cx25840);
  708. }
  709. static void __exit m__exit(void)
  710. {
  711. i2c_del_driver(&i2c_driver_cx25840);
  712. }
  713. module_init(m__init);
  714. module_exit(m__exit);
  715. /* ----------------------------------------------------------------------- */
  716. static void log_status(struct i2c_client *client)
  717. {
  718. static const char *const fmt_strs[] = {
  719. "0x0",
  720. "NTSC-M", "NTSC-J", "NTSC-4.43",
  721. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  722. "0x9", "0xA", "0xB",
  723. "SECAM",
  724. "0xD", "0xE", "0xF"
  725. };
  726. struct cx25840_state *state = i2c_get_clientdata(client);
  727. u8 microctrl_vidfmt = cx25840_read(client, 0x80a);
  728. u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
  729. u8 gen_stat1 = cx25840_read(client, 0x40d);
  730. u8 download_ctl = cx25840_read(client, 0x803);
  731. u8 mod_det_stat0 = cx25840_read(client, 0x804);
  732. u8 mod_det_stat1 = cx25840_read(client, 0x805);
  733. u8 audio_config = cx25840_read(client, 0x808);
  734. u8 pref_mode = cx25840_read(client, 0x809);
  735. u8 afc0 = cx25840_read(client, 0x80b);
  736. u8 mute_ctl = cx25840_read(client, 0x8d3);
  737. int vid_input = state->vid_input;
  738. int aud_input = state->aud_input;
  739. char *p;
  740. cx25840_info("Video signal: %spresent\n",
  741. (microctrl_vidfmt & 0x10) ? "" : "not ");
  742. cx25840_info("Detected format: %s\n",
  743. fmt_strs[gen_stat1 & 0xf]);
  744. switch (mod_det_stat0) {
  745. case 0x00: p = "mono"; break;
  746. case 0x01: p = "stereo"; break;
  747. case 0x02: p = "dual"; break;
  748. case 0x04: p = "tri"; break;
  749. case 0x10: p = "mono with SAP"; break;
  750. case 0x11: p = "stereo with SAP"; break;
  751. case 0x12: p = "dual with SAP"; break;
  752. case 0x14: p = "tri with SAP"; break;
  753. case 0xfe: p = "forced mode"; break;
  754. default: p = "not defined";
  755. }
  756. cx25840_info("Detected audio mode: %s\n", p);
  757. switch (mod_det_stat1) {
  758. case 0x00: p = "not defined"; break;
  759. case 0x01: p = "EIAJ"; break;
  760. case 0x02: p = "A2-M"; break;
  761. case 0x03: p = "A2-BG"; break;
  762. case 0x04: p = "A2-DK1"; break;
  763. case 0x05: p = "A2-DK2"; break;
  764. case 0x06: p = "A2-DK3"; break;
  765. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  766. case 0x08: p = "AM-L"; break;
  767. case 0x09: p = "NICAM-BG"; break;
  768. case 0x0a: p = "NICAM-DK"; break;
  769. case 0x0b: p = "NICAM-I"; break;
  770. case 0x0c: p = "NICAM-L"; break;
  771. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  772. case 0x0e: p = "IF FM Radio"; break;
  773. case 0x0f: p = "BTSC"; break;
  774. case 0x10: p = "high-deviation FM"; break;
  775. case 0x11: p = "very high-deviation FM"; break;
  776. case 0xfd: p = "unknown audio standard"; break;
  777. case 0xfe: p = "forced audio standard"; break;
  778. case 0xff: p = "no detected audio standard"; break;
  779. default: p = "not defined";
  780. }
  781. cx25840_info("Detected audio standard: %s\n", p);
  782. cx25840_info("Audio muted: %s\n",
  783. (mute_ctl & 0x2) ? "yes" : "no");
  784. cx25840_info("Audio microcontroller: %s\n",
  785. (download_ctl & 0x10) ? "running" : "stopped");
  786. switch (audio_config >> 4) {
  787. case 0x00: p = "undefined"; break;
  788. case 0x01: p = "BTSC"; break;
  789. case 0x02: p = "EIAJ"; break;
  790. case 0x03: p = "A2-M"; break;
  791. case 0x04: p = "A2-BG"; break;
  792. case 0x05: p = "A2-DK1"; break;
  793. case 0x06: p = "A2-DK2"; break;
  794. case 0x07: p = "A2-DK3"; break;
  795. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  796. case 0x09: p = "AM-L"; break;
  797. case 0x0a: p = "NICAM-BG"; break;
  798. case 0x0b: p = "NICAM-DK"; break;
  799. case 0x0c: p = "NICAM-I"; break;
  800. case 0x0d: p = "NICAM-L"; break;
  801. case 0x0e: p = "FM radio"; break;
  802. case 0x0f: p = "automatic detection"; break;
  803. default: p = "undefined";
  804. }
  805. cx25840_info("Configured audio standard: %s\n", p);
  806. if ((audio_config >> 4) < 0xF) {
  807. switch (audio_config & 0xF) {
  808. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  809. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  810. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  811. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  812. case 0x04: p = "STEREO"; break;
  813. case 0x05: p = "DUAL1 (AB)"; break;
  814. case 0x06: p = "DUAL2 (AC) (FM)"; break;
  815. case 0x07: p = "DUAL3 (BC) (FM)"; break;
  816. case 0x08: p = "DUAL4 (AC) (AM)"; break;
  817. case 0x09: p = "DUAL5 (BC) (AM)"; break;
  818. case 0x0a: p = "SAP"; break;
  819. default: p = "undefined";
  820. }
  821. cx25840_info("Configured audio mode: %s\n", p);
  822. } else {
  823. switch (audio_config & 0xF) {
  824. case 0x00: p = "BG"; break;
  825. case 0x01: p = "DK1"; break;
  826. case 0x02: p = "DK2"; break;
  827. case 0x03: p = "DK3"; break;
  828. case 0x04: p = "I"; break;
  829. case 0x05: p = "L"; break;
  830. case 0x06: p = "BTSC"; break;
  831. case 0x07: p = "EIAJ"; break;
  832. case 0x08: p = "A2-M"; break;
  833. case 0x09: p = "FM Radio"; break;
  834. case 0x0f: p = "automatic standard and mode detection"; break;
  835. default: p = "undefined";
  836. }
  837. cx25840_info("Configured audio system: %s\n", p);
  838. }
  839. cx25840_info("Specified standard: %s\n",
  840. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  841. if (vid_input >= CX25840_COMPOSITE1 &&
  842. vid_input <= CX25840_COMPOSITE8) {
  843. cx25840_info("Specified video input: Composite %d\n",
  844. vid_input - CX25840_COMPOSITE1 + 1);
  845. } else {
  846. cx25840_info("Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  847. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  848. }
  849. if (aud_input) {
  850. cx25840_info("Specified audio input: Tuner (In%d)\n", aud_input);
  851. } else {
  852. cx25840_info("Specified audio input: External\n");
  853. }
  854. cx25840_info("Specified audioclock freq: %d Hz\n", state->audclk_freq);
  855. switch (pref_mode & 0xf) {
  856. case 0: p = "mono/language A"; break;
  857. case 1: p = "language B"; break;
  858. case 2: p = "language C"; break;
  859. case 3: p = "analog fallback"; break;
  860. case 4: p = "stereo"; break;
  861. case 5: p = "language AC"; break;
  862. case 6: p = "language BC"; break;
  863. case 7: p = "language AB"; break;
  864. default: p = "undefined";
  865. }
  866. cx25840_info("Preferred audio mode: %s\n", p);
  867. if ((audio_config & 0xf) == 0xf) {
  868. switch ((afc0 >> 3) & 0x3) {
  869. case 0: p = "system DK"; break;
  870. case 1: p = "system L"; break;
  871. case 2: p = "autodetect"; break;
  872. default: p = "undefined";
  873. }
  874. cx25840_info("Selected 65 MHz format: %s\n", p);
  875. switch (afc0 & 0x7) {
  876. case 0: p = "chroma"; break;
  877. case 1: p = "BTSC"; break;
  878. case 2: p = "EIAJ"; break;
  879. case 3: p = "A2-M"; break;
  880. case 4: p = "autodetect"; break;
  881. default: p = "undefined";
  882. }
  883. cx25840_info("Selected 45 MHz format: %s\n", p);
  884. }
  885. }