cx25840-core.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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. /* 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. v4l_dbg(1, cx25840_debug, client, "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. v4l_err(client, "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. v4l_err(client, "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. v4l_err(client, "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. v4l_err(client, "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. v4l_err(client, "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. v4l_err(client, "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. default:
  379. return -EINVAL;
  380. }
  381. return 0;
  382. }
  383. static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  384. {
  385. struct cx25840_state *state = i2c_get_clientdata(client);
  386. switch (ctrl->id) {
  387. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  388. ctrl->value = state->pvr150_workaround;
  389. break;
  390. case V4L2_CID_BRIGHTNESS:
  391. ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
  392. break;
  393. case V4L2_CID_CONTRAST:
  394. ctrl->value = cx25840_read(client, 0x415) >> 1;
  395. break;
  396. case V4L2_CID_SATURATION:
  397. ctrl->value = cx25840_read(client, 0x420) >> 1;
  398. break;
  399. case V4L2_CID_HUE:
  400. ctrl->value = (s8)cx25840_read(client, 0x422);
  401. break;
  402. case V4L2_CID_AUDIO_VOLUME:
  403. case V4L2_CID_AUDIO_BASS:
  404. case V4L2_CID_AUDIO_TREBLE:
  405. case V4L2_CID_AUDIO_BALANCE:
  406. case V4L2_CID_AUDIO_MUTE:
  407. return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
  408. default:
  409. return -EINVAL;
  410. }
  411. return 0;
  412. }
  413. /* ----------------------------------------------------------------------- */
  414. static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  415. {
  416. switch (fmt->type) {
  417. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  418. return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
  419. default:
  420. return -EINVAL;
  421. }
  422. return 0;
  423. }
  424. static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  425. {
  426. struct v4l2_pix_format *pix;
  427. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  428. int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
  429. switch (fmt->type) {
  430. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  431. pix = &(fmt->fmt.pix);
  432. Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
  433. Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
  434. Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
  435. Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
  436. Vlines = pix->height + (is_pal ? 4 : 7);
  437. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  438. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  439. v4l_err(client, "%dx%d is not a valid size!\n",
  440. pix->width, pix->height);
  441. return -ERANGE;
  442. }
  443. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  444. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  445. VSC &= 0x1fff;
  446. if (pix->width >= 385)
  447. filter = 0;
  448. else if (pix->width > 192)
  449. filter = 1;
  450. else if (pix->width > 96)
  451. filter = 2;
  452. else
  453. filter = 3;
  454. v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
  455. pix->width, pix->height, HSC, VSC);
  456. /* HSCALE=HSC */
  457. cx25840_write(client, 0x418, HSC & 0xff);
  458. cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
  459. cx25840_write(client, 0x41a, HSC >> 16);
  460. /* VSCALE=VSC */
  461. cx25840_write(client, 0x41c, VSC & 0xff);
  462. cx25840_write(client, 0x41d, VSC >> 8);
  463. /* VS_INTRLACE=1 VFILT=filter */
  464. cx25840_write(client, 0x41e, 0x8 | filter);
  465. break;
  466. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  467. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  468. case V4L2_BUF_TYPE_VBI_CAPTURE:
  469. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  470. default:
  471. return -EINVAL;
  472. }
  473. return 0;
  474. }
  475. /* ----------------------------------------------------------------------- */
  476. static struct v4l2_queryctrl cx25840_qctrl[] = {
  477. {
  478. .id = V4L2_CID_BRIGHTNESS,
  479. .type = V4L2_CTRL_TYPE_INTEGER,
  480. .name = "Brightness",
  481. .minimum = 0,
  482. .maximum = 255,
  483. .step = 1,
  484. .default_value = 128,
  485. .flags = 0,
  486. }, {
  487. .id = V4L2_CID_CONTRAST,
  488. .type = V4L2_CTRL_TYPE_INTEGER,
  489. .name = "Contrast",
  490. .minimum = 0,
  491. .maximum = 255,
  492. .step = 1,
  493. .default_value = 64,
  494. .flags = 0,
  495. }, {
  496. .id = V4L2_CID_SATURATION,
  497. .type = V4L2_CTRL_TYPE_INTEGER,
  498. .name = "Saturation",
  499. .minimum = 0,
  500. .maximum = 255,
  501. .step = 1,
  502. .default_value = 64,
  503. .flags = 0,
  504. }, {
  505. .id = V4L2_CID_HUE,
  506. .type = V4L2_CTRL_TYPE_INTEGER,
  507. .name = "Hue",
  508. .minimum = -128,
  509. .maximum = 127,
  510. .step = 1,
  511. .default_value = 0,
  512. .flags = 0,
  513. }, {
  514. .id = V4L2_CID_AUDIO_VOLUME,
  515. .type = V4L2_CTRL_TYPE_INTEGER,
  516. .name = "Volume",
  517. .minimum = 0,
  518. .maximum = 65535,
  519. .step = 65535/100,
  520. .default_value = 58880,
  521. .flags = 0,
  522. }, {
  523. .id = V4L2_CID_AUDIO_BALANCE,
  524. .type = V4L2_CTRL_TYPE_INTEGER,
  525. .name = "Balance",
  526. .minimum = 0,
  527. .maximum = 65535,
  528. .step = 65535/100,
  529. .default_value = 32768,
  530. .flags = 0,
  531. }, {
  532. .id = V4L2_CID_AUDIO_MUTE,
  533. .type = V4L2_CTRL_TYPE_BOOLEAN,
  534. .name = "Mute",
  535. .minimum = 0,
  536. .maximum = 1,
  537. .step = 1,
  538. .default_value = 1,
  539. .flags = 0,
  540. }, {
  541. .id = V4L2_CID_AUDIO_BASS,
  542. .type = V4L2_CTRL_TYPE_INTEGER,
  543. .name = "Bass",
  544. .minimum = 0,
  545. .maximum = 65535,
  546. .step = 65535/100,
  547. .default_value = 32768,
  548. }, {
  549. .id = V4L2_CID_AUDIO_TREBLE,
  550. .type = V4L2_CTRL_TYPE_INTEGER,
  551. .name = "Treble",
  552. .minimum = 0,
  553. .maximum = 65535,
  554. .step = 65535/100,
  555. .default_value = 32768,
  556. },
  557. };
  558. /* ----------------------------------------------------------------------- */
  559. static int cx25840_command(struct i2c_client *client, unsigned int cmd,
  560. void *arg)
  561. {
  562. struct cx25840_state *state = i2c_get_clientdata(client);
  563. struct v4l2_tuner *vt = arg;
  564. switch (cmd) {
  565. #ifdef CONFIG_VIDEO_ADV_DEBUG
  566. /* ioctls to allow direct access to the
  567. * cx25840 registers for testing */
  568. case VIDIOC_INT_G_REGISTER:
  569. {
  570. struct v4l2_register *reg = arg;
  571. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  572. return -EINVAL;
  573. reg->val = cx25840_read(client, reg->reg & 0x0fff);
  574. break;
  575. }
  576. case VIDIOC_INT_S_REGISTER:
  577. {
  578. struct v4l2_register *reg = arg;
  579. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  580. return -EINVAL;
  581. if (!capable(CAP_SYS_ADMIN))
  582. return -EPERM;
  583. cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
  584. break;
  585. }
  586. #endif
  587. case VIDIOC_INT_DECODE_VBI_LINE:
  588. return cx25840_vbi(client, cmd, arg);
  589. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  590. return cx25840_audio(client, cmd, arg);
  591. case VIDIOC_STREAMON:
  592. v4l_dbg(1, cx25840_debug, client, "enable output\n");
  593. cx25840_write(client, 0x115, 0x8c);
  594. cx25840_write(client, 0x116, 0x07);
  595. break;
  596. case VIDIOC_STREAMOFF:
  597. v4l_dbg(1, cx25840_debug, client, "disable output\n");
  598. cx25840_write(client, 0x115, 0x00);
  599. cx25840_write(client, 0x116, 0x00);
  600. break;
  601. case VIDIOC_LOG_STATUS:
  602. log_status(client);
  603. break;
  604. case VIDIOC_G_CTRL:
  605. return get_v4lctrl(client, (struct v4l2_control *)arg);
  606. case VIDIOC_S_CTRL:
  607. return set_v4lctrl(client, (struct v4l2_control *)arg);
  608. case VIDIOC_QUERYCTRL:
  609. {
  610. struct v4l2_queryctrl *qc = arg;
  611. int i;
  612. for (i = 0; i < ARRAY_SIZE(cx25840_qctrl); i++)
  613. if (qc->id && qc->id == cx25840_qctrl[i].id) {
  614. memcpy(qc, &cx25840_qctrl[i], sizeof(*qc));
  615. return 0;
  616. }
  617. return -EINVAL;
  618. }
  619. case VIDIOC_G_STD:
  620. *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
  621. break;
  622. case VIDIOC_S_STD:
  623. state->radio = 0;
  624. return set_v4lstd(client, *(v4l2_std_id *)arg);
  625. case AUDC_SET_RADIO:
  626. state->radio = 1;
  627. break;
  628. case VIDIOC_G_INPUT:
  629. *(int *)arg = state->vid_input;
  630. break;
  631. case VIDIOC_S_INPUT:
  632. return set_input(client, *(enum cx25840_video_input *)arg, state->aud_input);
  633. case VIDIOC_S_AUDIO:
  634. {
  635. struct v4l2_audio *input = arg;
  636. return set_input(client, state->vid_input, input->index);
  637. }
  638. case VIDIOC_G_AUDIO:
  639. {
  640. struct v4l2_audio *input = arg;
  641. memset(input, 0, sizeof(*input));
  642. input->index = state->aud_input;
  643. break;
  644. }
  645. case VIDIOC_S_FREQUENCY:
  646. input_change(client);
  647. break;
  648. case VIDIOC_G_TUNER:
  649. {
  650. u8 mode = cx25840_read(client, 0x804);
  651. u8 pref = cx25840_read(client, 0x809) & 0xf;
  652. u8 vpres = cx25840_read(client, 0x80a) & 0x10;
  653. int val = 0;
  654. if (state->radio)
  655. break;
  656. vt->capability |=
  657. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  658. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  659. vt->signal = vpres ? 0xffff : 0x0;
  660. /* get rxsubchans and audmode */
  661. if ((mode & 0xf) == 1)
  662. val |= V4L2_TUNER_SUB_STEREO;
  663. else
  664. val |= V4L2_TUNER_SUB_MONO;
  665. if (mode == 2 || mode == 4)
  666. val |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  667. if (mode & 0x10)
  668. val |= V4L2_TUNER_SUB_SAP;
  669. vt->rxsubchans = val;
  670. switch (pref) {
  671. case 0:
  672. vt->audmode = V4L2_TUNER_MODE_MONO;
  673. break;
  674. case 1:
  675. case 2:
  676. vt->audmode = V4L2_TUNER_MODE_LANG2;
  677. break;
  678. case 4:
  679. default:
  680. vt->audmode = V4L2_TUNER_MODE_STEREO;
  681. }
  682. break;
  683. }
  684. case VIDIOC_S_TUNER:
  685. switch (vt->audmode) {
  686. case V4L2_TUNER_MODE_MONO:
  687. case V4L2_TUNER_MODE_LANG1:
  688. /* Force PREF_MODE to MONO */
  689. cx25840_and_or(client, 0x809, ~0xf, 0x00);
  690. break;
  691. case V4L2_TUNER_MODE_STEREO:
  692. /* Force PREF_MODE to STEREO */
  693. cx25840_and_or(client, 0x809, ~0xf, 0x04);
  694. break;
  695. case V4L2_TUNER_MODE_LANG2:
  696. /* Force PREF_MODE to LANG2 */
  697. cx25840_and_or(client, 0x809, ~0xf, 0x01);
  698. break;
  699. }
  700. break;
  701. case VIDIOC_G_FMT:
  702. return get_v4lfmt(client, (struct v4l2_format *)arg);
  703. case VIDIOC_S_FMT:
  704. return set_v4lfmt(client, (struct v4l2_format *)arg);
  705. case VIDIOC_INT_RESET:
  706. cx25840_initialize(client, 0);
  707. break;
  708. case VIDIOC_INT_G_CHIP_IDENT:
  709. *(enum v4l2_chip_ident *)arg =
  710. V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf);
  711. break;
  712. default:
  713. return -EINVAL;
  714. }
  715. return 0;
  716. }
  717. /* ----------------------------------------------------------------------- */
  718. static struct i2c_driver i2c_driver_cx25840;
  719. static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
  720. int kind)
  721. {
  722. struct i2c_client *client;
  723. struct cx25840_state *state;
  724. u16 device_id;
  725. /* Check if the adapter supports the needed features
  726. * Not until kernel version 2.6.11 did the bit-algo
  727. * correctly report that it would do an I2C-level xfer */
  728. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  729. return 0;
  730. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  731. if (client == 0)
  732. return -ENOMEM;
  733. client->addr = address;
  734. client->adapter = adapter;
  735. client->driver = &i2c_driver_cx25840;
  736. snprintf(client->name, sizeof(client->name) - 1, "cx25840");
  737. v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", address << 1);
  738. device_id = cx25840_read(client, 0x101) << 8;
  739. device_id |= cx25840_read(client, 0x100);
  740. /* The high byte of the device ID should be
  741. * 0x84 if chip is present */
  742. if ((device_id & 0xff00) != 0x8400) {
  743. v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
  744. kfree(client);
  745. return 0;
  746. }
  747. v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
  748. (device_id & 0xfff0) >> 4,
  749. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
  750. address << 1, adapter->name);
  751. state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  752. if (state == NULL) {
  753. kfree(client);
  754. return -ENOMEM;
  755. }
  756. i2c_set_clientdata(client, state);
  757. memset(state, 0, sizeof(struct cx25840_state));
  758. state->vid_input = CX25840_COMPOSITE7;
  759. state->aud_input = CX25840_AUDIO8;
  760. state->audclk_freq = 48000;
  761. state->pvr150_workaround = 0;
  762. cx25840_initialize(client, 1);
  763. i2c_attach_client(client);
  764. return 0;
  765. }
  766. static int cx25840_attach_adapter(struct i2c_adapter *adapter)
  767. {
  768. if (adapter->class & I2C_CLASS_TV_ANALOG)
  769. return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
  770. return 0;
  771. }
  772. static int cx25840_detach_client(struct i2c_client *client)
  773. {
  774. struct cx25840_state *state = i2c_get_clientdata(client);
  775. int err;
  776. err = i2c_detach_client(client);
  777. if (err) {
  778. return err;
  779. }
  780. kfree(state);
  781. kfree(client);
  782. return 0;
  783. }
  784. /* ----------------------------------------------------------------------- */
  785. static struct i2c_driver i2c_driver_cx25840 = {
  786. .driver = {
  787. .name = "cx25840",
  788. },
  789. .id = I2C_DRIVERID_CX25840,
  790. .attach_adapter = cx25840_attach_adapter,
  791. .detach_client = cx25840_detach_client,
  792. .command = cx25840_command,
  793. };
  794. static int __init m__init(void)
  795. {
  796. return i2c_add_driver(&i2c_driver_cx25840);
  797. }
  798. static void __exit m__exit(void)
  799. {
  800. i2c_del_driver(&i2c_driver_cx25840);
  801. }
  802. module_init(m__init);
  803. module_exit(m__exit);
  804. /* ----------------------------------------------------------------------- */
  805. static void log_status(struct i2c_client *client)
  806. {
  807. static const char *const fmt_strs[] = {
  808. "0x0",
  809. "NTSC-M", "NTSC-J", "NTSC-4.43",
  810. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  811. "0x9", "0xA", "0xB",
  812. "SECAM",
  813. "0xD", "0xE", "0xF"
  814. };
  815. struct cx25840_state *state = i2c_get_clientdata(client);
  816. u8 microctrl_vidfmt = cx25840_read(client, 0x80a);
  817. u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
  818. u8 gen_stat1 = cx25840_read(client, 0x40d);
  819. u8 download_ctl = cx25840_read(client, 0x803);
  820. u8 mod_det_stat0 = cx25840_read(client, 0x804);
  821. u8 mod_det_stat1 = cx25840_read(client, 0x805);
  822. u8 audio_config = cx25840_read(client, 0x808);
  823. u8 pref_mode = cx25840_read(client, 0x809);
  824. u8 afc0 = cx25840_read(client, 0x80b);
  825. u8 mute_ctl = cx25840_read(client, 0x8d3);
  826. int vid_input = state->vid_input;
  827. int aud_input = state->aud_input;
  828. char *p;
  829. v4l_info(client, "Video signal: %spresent\n",
  830. (microctrl_vidfmt & 0x10) ? "" : "not ");
  831. v4l_info(client, "Detected format: %s\n",
  832. fmt_strs[gen_stat1 & 0xf]);
  833. switch (mod_det_stat0) {
  834. case 0x00: p = "mono"; break;
  835. case 0x01: p = "stereo"; break;
  836. case 0x02: p = "dual"; break;
  837. case 0x04: p = "tri"; break;
  838. case 0x10: p = "mono with SAP"; break;
  839. case 0x11: p = "stereo with SAP"; break;
  840. case 0x12: p = "dual with SAP"; break;
  841. case 0x14: p = "tri with SAP"; break;
  842. case 0xfe: p = "forced mode"; break;
  843. default: p = "not defined";
  844. }
  845. v4l_info(client, "Detected audio mode: %s\n", p);
  846. switch (mod_det_stat1) {
  847. case 0x00: p = "not defined"; break;
  848. case 0x01: p = "EIAJ"; break;
  849. case 0x02: p = "A2-M"; break;
  850. case 0x03: p = "A2-BG"; break;
  851. case 0x04: p = "A2-DK1"; break;
  852. case 0x05: p = "A2-DK2"; break;
  853. case 0x06: p = "A2-DK3"; break;
  854. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  855. case 0x08: p = "AM-L"; break;
  856. case 0x09: p = "NICAM-BG"; break;
  857. case 0x0a: p = "NICAM-DK"; break;
  858. case 0x0b: p = "NICAM-I"; break;
  859. case 0x0c: p = "NICAM-L"; break;
  860. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  861. case 0x0e: p = "IF FM Radio"; break;
  862. case 0x0f: p = "BTSC"; break;
  863. case 0x10: p = "high-deviation FM"; break;
  864. case 0x11: p = "very high-deviation FM"; break;
  865. case 0xfd: p = "unknown audio standard"; break;
  866. case 0xfe: p = "forced audio standard"; break;
  867. case 0xff: p = "no detected audio standard"; break;
  868. default: p = "not defined";
  869. }
  870. v4l_info(client, "Detected audio standard: %s\n", p);
  871. v4l_info(client, "Audio muted: %s\n",
  872. (mute_ctl & 0x2) ? "yes" : "no");
  873. v4l_info(client, "Audio microcontroller: %s\n",
  874. (download_ctl & 0x10) ? "running" : "stopped");
  875. switch (audio_config >> 4) {
  876. case 0x00: p = "undefined"; break;
  877. case 0x01: p = "BTSC"; break;
  878. case 0x02: p = "EIAJ"; break;
  879. case 0x03: p = "A2-M"; break;
  880. case 0x04: p = "A2-BG"; break;
  881. case 0x05: p = "A2-DK1"; break;
  882. case 0x06: p = "A2-DK2"; break;
  883. case 0x07: p = "A2-DK3"; break;
  884. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  885. case 0x09: p = "AM-L"; break;
  886. case 0x0a: p = "NICAM-BG"; break;
  887. case 0x0b: p = "NICAM-DK"; break;
  888. case 0x0c: p = "NICAM-I"; break;
  889. case 0x0d: p = "NICAM-L"; break;
  890. case 0x0e: p = "FM radio"; break;
  891. case 0x0f: p = "automatic detection"; break;
  892. default: p = "undefined";
  893. }
  894. v4l_info(client, "Configured audio standard: %s\n", p);
  895. if ((audio_config >> 4) < 0xF) {
  896. switch (audio_config & 0xF) {
  897. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  898. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  899. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  900. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  901. case 0x04: p = "STEREO"; break;
  902. case 0x05: p = "DUAL1 (AB)"; break;
  903. case 0x06: p = "DUAL2 (AC) (FM)"; break;
  904. case 0x07: p = "DUAL3 (BC) (FM)"; break;
  905. case 0x08: p = "DUAL4 (AC) (AM)"; break;
  906. case 0x09: p = "DUAL5 (BC) (AM)"; break;
  907. case 0x0a: p = "SAP"; break;
  908. default: p = "undefined";
  909. }
  910. v4l_info(client, "Configured audio mode: %s\n", p);
  911. } else {
  912. switch (audio_config & 0xF) {
  913. case 0x00: p = "BG"; break;
  914. case 0x01: p = "DK1"; break;
  915. case 0x02: p = "DK2"; break;
  916. case 0x03: p = "DK3"; break;
  917. case 0x04: p = "I"; break;
  918. case 0x05: p = "L"; break;
  919. case 0x06: p = "BTSC"; break;
  920. case 0x07: p = "EIAJ"; break;
  921. case 0x08: p = "A2-M"; break;
  922. case 0x09: p = "FM Radio"; break;
  923. case 0x0f: p = "automatic standard and mode detection"; break;
  924. default: p = "undefined";
  925. }
  926. v4l_info(client, "Configured audio system: %s\n", p);
  927. }
  928. v4l_info(client, "Specified standard: %s\n",
  929. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  930. if (vid_input >= CX25840_COMPOSITE1 &&
  931. vid_input <= CX25840_COMPOSITE8) {
  932. v4l_info(client, "Specified video input: Composite %d\n",
  933. vid_input - CX25840_COMPOSITE1 + 1);
  934. } else {
  935. v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  936. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  937. }
  938. if (aud_input) {
  939. v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
  940. } else {
  941. v4l_info(client, "Specified audio input: External\n");
  942. }
  943. v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
  944. switch (pref_mode & 0xf) {
  945. case 0: p = "mono/language A"; break;
  946. case 1: p = "language B"; break;
  947. case 2: p = "language C"; break;
  948. case 3: p = "analog fallback"; break;
  949. case 4: p = "stereo"; break;
  950. case 5: p = "language AC"; break;
  951. case 6: p = "language BC"; break;
  952. case 7: p = "language AB"; break;
  953. default: p = "undefined";
  954. }
  955. v4l_info(client, "Preferred audio mode: %s\n", p);
  956. if ((audio_config & 0xf) == 0xf) {
  957. switch ((afc0 >> 3) & 0x3) {
  958. case 0: p = "system DK"; break;
  959. case 1: p = "system L"; break;
  960. case 2: p = "autodetect"; break;
  961. default: p = "undefined";
  962. }
  963. v4l_info(client, "Selected 65 MHz format: %s\n", p);
  964. switch (afc0 & 0x7) {
  965. case 0: p = "chroma"; break;
  966. case 1: p = "BTSC"; break;
  967. case 2: p = "EIAJ"; break;
  968. case 3: p = "A2-M"; break;
  969. case 4: p = "autodetect"; break;
  970. default: p = "undefined";
  971. }
  972. v4l_info(client, "Selected 45 MHz format: %s\n", p);
  973. }
  974. }