cx25840-core.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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. * NTSC sliced VBI support by Christopher Neufeld <television@cneufeld.ca>
  14. * with additional fixes by Hans Verkuil <hverkuil@xs4all.nl>.
  15. *
  16. * CX23885 support by Steven Toth <stoth@linuxtv.org>.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version 2
  21. * of the License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include <linux/videodev2.h>
  36. #include <linux/i2c.h>
  37. #include <linux/delay.h>
  38. #include <media/v4l2-common.h>
  39. #include <media/v4l2-chip-ident.h>
  40. #include <media/v4l2-i2c-drv-legacy.h>
  41. #include <media/cx25840.h>
  42. #include "cx25840-core.h"
  43. MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
  44. MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
  45. MODULE_LICENSE("GPL");
  46. static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
  47. static int cx25840_debug;
  48. module_param_named(debug,cx25840_debug, int, 0644);
  49. MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
  50. I2C_CLIENT_INSMOD;
  51. /* ----------------------------------------------------------------------- */
  52. int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
  53. {
  54. u8 buffer[3];
  55. buffer[0] = addr >> 8;
  56. buffer[1] = addr & 0xff;
  57. buffer[2] = value;
  58. return i2c_master_send(client, buffer, 3);
  59. }
  60. int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
  61. {
  62. u8 buffer[6];
  63. buffer[0] = addr >> 8;
  64. buffer[1] = addr & 0xff;
  65. buffer[2] = value & 0xff;
  66. buffer[3] = (value >> 8) & 0xff;
  67. buffer[4] = (value >> 16) & 0xff;
  68. buffer[5] = value >> 24;
  69. return i2c_master_send(client, buffer, 6);
  70. }
  71. u8 cx25840_read(struct i2c_client * client, u16 addr)
  72. {
  73. u8 buffer[2];
  74. buffer[0] = addr >> 8;
  75. buffer[1] = addr & 0xff;
  76. if (i2c_master_send(client, buffer, 2) < 2)
  77. return 0;
  78. if (i2c_master_recv(client, buffer, 1) < 1)
  79. return 0;
  80. return buffer[0];
  81. }
  82. u32 cx25840_read4(struct i2c_client * client, u16 addr)
  83. {
  84. u8 buffer[4];
  85. buffer[0] = addr >> 8;
  86. buffer[1] = addr & 0xff;
  87. if (i2c_master_send(client, buffer, 2) < 2)
  88. return 0;
  89. if (i2c_master_recv(client, buffer, 4) < 4)
  90. return 0;
  91. return (buffer[3] << 24) | (buffer[2] << 16) |
  92. (buffer[1] << 8) | buffer[0];
  93. }
  94. int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
  95. u8 or_value)
  96. {
  97. return cx25840_write(client, addr,
  98. (cx25840_read(client, addr) & and_mask) |
  99. or_value);
  100. }
  101. /* ----------------------------------------------------------------------- */
  102. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  103. enum cx25840_audio_input aud_input);
  104. /* ----------------------------------------------------------------------- */
  105. static void init_dll1(struct i2c_client *client)
  106. {
  107. /* This is the Hauppauge sequence used to
  108. * initialize the Delay Lock Loop 1 (ADC DLL). */
  109. cx25840_write(client, 0x159, 0x23);
  110. cx25840_write(client, 0x15a, 0x87);
  111. cx25840_write(client, 0x15b, 0x06);
  112. udelay(10);
  113. cx25840_write(client, 0x159, 0xe1);
  114. udelay(10);
  115. cx25840_write(client, 0x15a, 0x86);
  116. cx25840_write(client, 0x159, 0xe0);
  117. cx25840_write(client, 0x159, 0xe1);
  118. cx25840_write(client, 0x15b, 0x10);
  119. }
  120. static void init_dll2(struct i2c_client *client)
  121. {
  122. /* This is the Hauppauge sequence used to
  123. * initialize the Delay Lock Loop 2 (ADC DLL). */
  124. cx25840_write(client, 0x15d, 0xe3);
  125. cx25840_write(client, 0x15e, 0x86);
  126. cx25840_write(client, 0x15f, 0x06);
  127. udelay(10);
  128. cx25840_write(client, 0x15d, 0xe1);
  129. cx25840_write(client, 0x15d, 0xe0);
  130. cx25840_write(client, 0x15d, 0xe1);
  131. }
  132. static void cx25836_initialize(struct i2c_client *client)
  133. {
  134. /* reset configuration is described on page 3-77 of the CX25836 datasheet */
  135. /* 2. */
  136. cx25840_and_or(client, 0x000, ~0x01, 0x01);
  137. cx25840_and_or(client, 0x000, ~0x01, 0x00);
  138. /* 3a. */
  139. cx25840_and_or(client, 0x15a, ~0x70, 0x00);
  140. /* 3b. */
  141. cx25840_and_or(client, 0x15b, ~0x1e, 0x06);
  142. /* 3c. */
  143. cx25840_and_or(client, 0x159, ~0x02, 0x02);
  144. /* 3d. */
  145. udelay(10);
  146. /* 3e. */
  147. cx25840_and_or(client, 0x159, ~0x02, 0x00);
  148. /* 3f. */
  149. cx25840_and_or(client, 0x159, ~0xc0, 0xc0);
  150. /* 3g. */
  151. cx25840_and_or(client, 0x159, ~0x01, 0x00);
  152. cx25840_and_or(client, 0x159, ~0x01, 0x01);
  153. /* 3h. */
  154. cx25840_and_or(client, 0x15b, ~0x1e, 0x10);
  155. }
  156. static void cx25840_work_handler(struct work_struct *work)
  157. {
  158. struct cx25840_state *state = container_of(work, struct cx25840_state, fw_work);
  159. cx25840_loadfw(state->c);
  160. wake_up(&state->fw_wait);
  161. }
  162. static void cx25840_initialize(struct i2c_client *client)
  163. {
  164. DEFINE_WAIT(wait);
  165. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  166. struct workqueue_struct *q;
  167. /* datasheet startup in numbered steps, refer to page 3-77 */
  168. /* 2. */
  169. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  170. /* The default of this register should be 4, but I get 0 instead.
  171. * Set this register to 4 manually. */
  172. cx25840_write(client, 0x000, 0x04);
  173. /* 3. */
  174. init_dll1(client);
  175. init_dll2(client);
  176. cx25840_write(client, 0x136, 0x0a);
  177. /* 4. */
  178. cx25840_write(client, 0x13c, 0x01);
  179. cx25840_write(client, 0x13c, 0x00);
  180. /* 5. */
  181. /* Do the firmware load in a work handler to prevent.
  182. Otherwise the kernel is blocked waiting for the
  183. bit-banging i2c interface to finish uploading the
  184. firmware. */
  185. INIT_WORK(&state->fw_work, cx25840_work_handler);
  186. init_waitqueue_head(&state->fw_wait);
  187. q = create_singlethread_workqueue("cx25840_fw");
  188. prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
  189. queue_work(q, &state->fw_work);
  190. schedule();
  191. finish_wait(&state->fw_wait, &wait);
  192. destroy_workqueue(q);
  193. /* 6. */
  194. cx25840_write(client, 0x115, 0x8c);
  195. cx25840_write(client, 0x116, 0x07);
  196. cx25840_write(client, 0x118, 0x02);
  197. /* 7. */
  198. cx25840_write(client, 0x4a5, 0x80);
  199. cx25840_write(client, 0x4a5, 0x00);
  200. cx25840_write(client, 0x402, 0x00);
  201. /* 8. */
  202. cx25840_and_or(client, 0x401, ~0x18, 0);
  203. cx25840_and_or(client, 0x4a2, ~0x10, 0x10);
  204. /* steps 8c and 8d are done in change_input() */
  205. /* 10. */
  206. cx25840_write(client, 0x8d3, 0x1f);
  207. cx25840_write(client, 0x8e3, 0x03);
  208. cx25840_std_setup(client);
  209. /* trial and error says these are needed to get audio */
  210. cx25840_write(client, 0x914, 0xa0);
  211. cx25840_write(client, 0x918, 0xa0);
  212. cx25840_write(client, 0x919, 0x01);
  213. /* stereo prefered */
  214. cx25840_write(client, 0x809, 0x04);
  215. /* AC97 shift */
  216. cx25840_write(client, 0x8cf, 0x0f);
  217. /* (re)set input */
  218. set_input(client, state->vid_input, state->aud_input);
  219. /* start microcontroller */
  220. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  221. }
  222. static void cx23885_initialize(struct i2c_client *client)
  223. {
  224. DEFINE_WAIT(wait);
  225. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  226. struct workqueue_struct *q;
  227. /* Internal Reset */
  228. cx25840_and_or(client, 0x102, ~0x01, 0x01);
  229. cx25840_and_or(client, 0x102, ~0x01, 0x00);
  230. /* Stop microcontroller */
  231. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  232. /* DIF in reset? */
  233. cx25840_write(client, 0x398, 0);
  234. /* Trust the default xtal, no division */
  235. /* This changes for the cx23888 products */
  236. cx25840_write(client, 0x2, 0x76);
  237. /* Bring down the regulator for AUX clk */
  238. cx25840_write(client, 0x1, 0x40);
  239. /* Sys PLL frac */
  240. cx25840_write4(client, 0x11c, 0x01d1744c);
  241. /* Sys PLL int */
  242. cx25840_write4(client, 0x118, 0x00000416);
  243. /* Disable DIF bypass */
  244. cx25840_write4(client, 0x33c, 0x00000001);
  245. /* DIF Src phase inc */
  246. cx25840_write4(client, 0x340, 0x0df7df83);
  247. /* Vid PLL frac */
  248. cx25840_write4(client, 0x10c, 0x01b6db7b);
  249. /* Vid PLL int */
  250. cx25840_write4(client, 0x108, 0x00000512);
  251. /* Luma */
  252. cx25840_write4(client, 0x414, 0x00107d12);
  253. /* Chroma */
  254. cx25840_write4(client, 0x420, 0x3d008282);
  255. /* Aux PLL frac */
  256. cx25840_write4(client, 0x114, 0x017dbf48);
  257. /* Aux PLL int */
  258. cx25840_write4(client, 0x110, 0x000a030e);
  259. /* ADC2 input select */
  260. cx25840_write(client, 0x102, 0x10);
  261. /* VIN1 & VIN5 */
  262. cx25840_write(client, 0x103, 0x11);
  263. /* Enable format auto detect */
  264. cx25840_write(client, 0x400, 0);
  265. /* Fast subchroma lock */
  266. /* White crush, Chroma AGC & Chroma Killer enabled */
  267. cx25840_write(client, 0x401, 0xe8);
  268. /* Select AFE clock pad output source */
  269. cx25840_write(client, 0x144, 0x05);
  270. /* Do the firmware load in a work handler to prevent.
  271. Otherwise the kernel is blocked waiting for the
  272. bit-banging i2c interface to finish uploading the
  273. firmware. */
  274. INIT_WORK(&state->fw_work, cx25840_work_handler);
  275. init_waitqueue_head(&state->fw_wait);
  276. q = create_singlethread_workqueue("cx25840_fw");
  277. prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
  278. queue_work(q, &state->fw_work);
  279. schedule();
  280. finish_wait(&state->fw_wait, &wait);
  281. destroy_workqueue(q);
  282. cx25840_std_setup(client);
  283. /* (re)set input */
  284. set_input(client, state->vid_input, state->aud_input);
  285. /* start microcontroller */
  286. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  287. }
  288. /* ----------------------------------------------------------------------- */
  289. void cx25840_std_setup(struct i2c_client *client)
  290. {
  291. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  292. v4l2_std_id std = state->std;
  293. int hblank, hactive, burst, vblank, vactive, sc;
  294. int vblank656, src_decimation;
  295. int luma_lpf, uv_lpf, comb;
  296. u32 pll_int, pll_frac, pll_post;
  297. /* datasheet startup, step 8d */
  298. if (std & ~V4L2_STD_NTSC)
  299. cx25840_write(client, 0x49f, 0x11);
  300. else
  301. cx25840_write(client, 0x49f, 0x14);
  302. if (std & V4L2_STD_625_50) {
  303. hblank = 132;
  304. hactive = 720;
  305. burst = 93;
  306. vblank = 36;
  307. vactive = 580;
  308. vblank656 = 40;
  309. src_decimation = 0x21f;
  310. luma_lpf = 2;
  311. if (std & V4L2_STD_SECAM) {
  312. uv_lpf = 0;
  313. comb = 0;
  314. sc = 0x0a425f;
  315. } else if (std == V4L2_STD_PAL_Nc) {
  316. uv_lpf = 1;
  317. comb = 0x20;
  318. sc = 556453;
  319. } else {
  320. uv_lpf = 1;
  321. comb = 0x20;
  322. sc = 688739;
  323. }
  324. } else {
  325. hactive = 720;
  326. hblank = 122;
  327. vactive = 487;
  328. luma_lpf = 1;
  329. uv_lpf = 1;
  330. src_decimation = 0x21f;
  331. if (std == V4L2_STD_PAL_60) {
  332. vblank = 26;
  333. vblank656 = 26;
  334. burst = 0x5b;
  335. luma_lpf = 2;
  336. comb = 0x20;
  337. sc = 688739;
  338. } else if (std == V4L2_STD_PAL_M) {
  339. vblank = 20;
  340. vblank656 = 24;
  341. burst = 0x61;
  342. comb = 0x20;
  343. sc = 555452;
  344. } else {
  345. vblank = 26;
  346. vblank656 = 26;
  347. burst = 0x5b;
  348. comb = 0x66;
  349. sc = 556063;
  350. }
  351. }
  352. /* DEBUG: Displays configured PLL frequency */
  353. pll_int = cx25840_read(client, 0x108);
  354. pll_frac = cx25840_read4(client, 0x10c) & 0x1ffffff;
  355. pll_post = cx25840_read(client, 0x109);
  356. v4l_dbg(1, cx25840_debug, client,
  357. "PLL regs = int: %u, frac: %u, post: %u\n",
  358. pll_int, pll_frac, pll_post);
  359. if (pll_post) {
  360. int fin, fsc;
  361. int pll = (28636363L * ((((u64)pll_int) << 25L) + pll_frac)) >> 25L;
  362. pll /= pll_post;
  363. v4l_dbg(1, cx25840_debug, client, "PLL = %d.%06d MHz\n",
  364. pll / 1000000, pll % 1000000);
  365. v4l_dbg(1, cx25840_debug, client, "PLL/8 = %d.%06d MHz\n",
  366. pll / 8000000, (pll / 8) % 1000000);
  367. fin = ((u64)src_decimation * pll) >> 12;
  368. v4l_dbg(1, cx25840_debug, client,
  369. "ADC Sampling freq = %d.%06d MHz\n",
  370. fin / 1000000, fin % 1000000);
  371. fsc = (((u64)sc) * pll) >> 24L;
  372. v4l_dbg(1, cx25840_debug, client,
  373. "Chroma sub-carrier freq = %d.%06d MHz\n",
  374. fsc / 1000000, fsc % 1000000);
  375. v4l_dbg(1, cx25840_debug, client, "hblank %i, hactive %i, "
  376. "vblank %i, vactive %i, vblank656 %i, src_dec %i, "
  377. "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x, "
  378. "sc 0x%06x\n",
  379. hblank, hactive, vblank, vactive, vblank656,
  380. src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
  381. }
  382. /* Sets horizontal blanking delay and active lines */
  383. cx25840_write(client, 0x470, hblank);
  384. cx25840_write(client, 0x471,
  385. 0xff & (((hblank >> 8) & 0x3) | (hactive << 4)));
  386. cx25840_write(client, 0x472, hactive >> 4);
  387. /* Sets burst gate delay */
  388. cx25840_write(client, 0x473, burst);
  389. /* Sets vertical blanking delay and active duration */
  390. cx25840_write(client, 0x474, vblank);
  391. cx25840_write(client, 0x475,
  392. 0xff & (((vblank >> 8) & 0x3) | (vactive << 4)));
  393. cx25840_write(client, 0x476, vactive >> 4);
  394. cx25840_write(client, 0x477, vblank656);
  395. /* Sets src decimation rate */
  396. cx25840_write(client, 0x478, 0xff & src_decimation);
  397. cx25840_write(client, 0x479, 0xff & (src_decimation >> 8));
  398. /* Sets Luma and UV Low pass filters */
  399. cx25840_write(client, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
  400. /* Enables comb filters */
  401. cx25840_write(client, 0x47b, comb);
  402. /* Sets SC Step*/
  403. cx25840_write(client, 0x47c, sc);
  404. cx25840_write(client, 0x47d, 0xff & sc >> 8);
  405. cx25840_write(client, 0x47e, 0xff & sc >> 16);
  406. /* Sets VBI parameters */
  407. if (std & V4L2_STD_625_50) {
  408. cx25840_write(client, 0x47f, 0x01);
  409. state->vbi_line_offset = 5;
  410. } else {
  411. cx25840_write(client, 0x47f, 0x00);
  412. state->vbi_line_offset = 8;
  413. }
  414. }
  415. /* ----------------------------------------------------------------------- */
  416. static void input_change(struct i2c_client *client)
  417. {
  418. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  419. v4l2_std_id std = state->std;
  420. /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
  421. if (std & V4L2_STD_SECAM) {
  422. cx25840_write(client, 0x402, 0);
  423. }
  424. else {
  425. cx25840_write(client, 0x402, 0x04);
  426. cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
  427. }
  428. cx25840_and_or(client, 0x401, ~0x60, 0);
  429. cx25840_and_or(client, 0x401, ~0x60, 0x60);
  430. cx25840_and_or(client, 0x810, ~0x01, 1);
  431. if (state->radio) {
  432. cx25840_write(client, 0x808, 0xf9);
  433. cx25840_write(client, 0x80b, 0x00);
  434. }
  435. else if (std & V4L2_STD_525_60) {
  436. /* Certain Hauppauge PVR150 models have a hardware bug
  437. that causes audio to drop out. For these models the
  438. audio standard must be set explicitly.
  439. To be precise: it affects cards with tuner models
  440. 85, 99 and 112 (model numbers from tveeprom). */
  441. int hw_fix = state->pvr150_workaround;
  442. if (std == V4L2_STD_NTSC_M_JP) {
  443. /* Japan uses EIAJ audio standard */
  444. cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
  445. } else if (std == V4L2_STD_NTSC_M_KR) {
  446. /* South Korea uses A2 audio standard */
  447. cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
  448. } else {
  449. /* Others use the BTSC audio standard */
  450. cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
  451. }
  452. cx25840_write(client, 0x80b, 0x00);
  453. } else if (std & V4L2_STD_PAL) {
  454. /* Follow tuner change procedure for PAL */
  455. cx25840_write(client, 0x808, 0xff);
  456. cx25840_write(client, 0x80b, 0x10);
  457. } else if (std & V4L2_STD_SECAM) {
  458. /* Select autodetect for SECAM */
  459. cx25840_write(client, 0x808, 0xff);
  460. cx25840_write(client, 0x80b, 0x10);
  461. }
  462. cx25840_and_or(client, 0x810, ~0x01, 0);
  463. }
  464. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  465. enum cx25840_audio_input aud_input)
  466. {
  467. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  468. u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
  469. vid_input <= CX25840_COMPOSITE8);
  470. u8 reg;
  471. v4l_dbg(1, cx25840_debug, client,
  472. "decoder set video input %d, audio input %d\n",
  473. vid_input, aud_input);
  474. if (vid_input >= CX25840_VIN1_CH1) {
  475. v4l_dbg(1, cx25840_debug, client, "vid_input 0x%x\n",
  476. vid_input);
  477. reg = vid_input & 0xff;
  478. if ((vid_input & CX25840_SVIDEO_ON) == CX25840_SVIDEO_ON)
  479. is_composite = 0;
  480. else
  481. is_composite = 1;
  482. v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",
  483. reg, is_composite);
  484. } else
  485. if (is_composite) {
  486. reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
  487. } else {
  488. int luma = vid_input & 0xf0;
  489. int chroma = vid_input & 0xf00;
  490. if ((vid_input & ~0xff0) ||
  491. luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA8 ||
  492. chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
  493. v4l_err(client, "0x%04x is not a valid video input!\n",
  494. vid_input);
  495. return -EINVAL;
  496. }
  497. reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
  498. if (chroma >= CX25840_SVIDEO_CHROMA7) {
  499. reg &= 0x3f;
  500. reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
  501. } else {
  502. reg &= 0xcf;
  503. reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
  504. }
  505. }
  506. /* The caller has previously prepared the correct routing
  507. * configuration in reg (for the cx23885) so we have no
  508. * need to attempt to flip bits for earlier av decoders.
  509. */
  510. if (!state->is_cx23885) {
  511. switch (aud_input) {
  512. case CX25840_AUDIO_SERIAL:
  513. /* do nothing, use serial audio input */
  514. break;
  515. case CX25840_AUDIO4: reg &= ~0x30; break;
  516. case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
  517. case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
  518. case CX25840_AUDIO7: reg &= ~0xc0; break;
  519. case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
  520. default:
  521. v4l_err(client, "0x%04x is not a valid audio input!\n",
  522. aud_input);
  523. return -EINVAL;
  524. }
  525. }
  526. cx25840_write(client, 0x103, reg);
  527. /* Set INPUT_MODE to Composite (0) or S-Video (1) */
  528. cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
  529. if (!state->is_cx23885) {
  530. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  531. cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
  532. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */
  533. if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
  534. cx25840_and_or(client, 0x102, ~0x4, 4);
  535. else
  536. cx25840_and_or(client, 0x102, ~0x4, 0);
  537. } else {
  538. if (is_composite)
  539. /* ADC2 input select channel 2 */
  540. cx25840_and_or(client, 0x102, ~0x2, 0);
  541. else
  542. /* ADC2 input select channel 3 */
  543. cx25840_and_or(client, 0x102, ~0x2, 2);
  544. }
  545. state->vid_input = vid_input;
  546. state->aud_input = aud_input;
  547. if (!state->is_cx25836) {
  548. cx25840_audio_set_path(client);
  549. input_change(client);
  550. }
  551. if (state->is_cx23885) {
  552. /* Audio channel 1 src : Parallel 1 */
  553. cx25840_write(client, 0x124, 0x03);
  554. /* Select AFE clock pad output source */
  555. cx25840_write(client, 0x144, 0x05);
  556. /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
  557. cx25840_write(client, 0x914, 0xa0);
  558. /* I2S_OUT_CTL:
  559. * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
  560. * I2S_OUT_MASTER_MODE = Master
  561. */
  562. cx25840_write(client, 0x918, 0xa0);
  563. cx25840_write(client, 0x919, 0x01);
  564. }
  565. return 0;
  566. }
  567. /* ----------------------------------------------------------------------- */
  568. static int set_v4lstd(struct i2c_client *client)
  569. {
  570. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  571. u8 fmt = 0; /* zero is autodetect */
  572. u8 pal_m = 0;
  573. /* First tests should be against specific std */
  574. if (state->std == V4L2_STD_NTSC_M_JP) {
  575. fmt = 0x2;
  576. } else if (state->std == V4L2_STD_NTSC_443) {
  577. fmt = 0x3;
  578. } else if (state->std == V4L2_STD_PAL_M) {
  579. pal_m = 1;
  580. fmt = 0x5;
  581. } else if (state->std == V4L2_STD_PAL_N) {
  582. fmt = 0x6;
  583. } else if (state->std == V4L2_STD_PAL_Nc) {
  584. fmt = 0x7;
  585. } else if (state->std == V4L2_STD_PAL_60) {
  586. fmt = 0x8;
  587. } else {
  588. /* Then, test against generic ones */
  589. if (state->std & V4L2_STD_NTSC)
  590. fmt = 0x1;
  591. else if (state->std & V4L2_STD_PAL)
  592. fmt = 0x4;
  593. else if (state->std & V4L2_STD_SECAM)
  594. fmt = 0xc;
  595. }
  596. v4l_dbg(1, cx25840_debug, client, "changing video std to fmt %i\n",fmt);
  597. /* Follow step 9 of section 3.16 in the cx25840 datasheet.
  598. Without this PAL may display a vertical ghosting effect.
  599. This happens for example with the Yuan MPC622. */
  600. if (fmt >= 4 && fmt < 8) {
  601. /* Set format to NTSC-M */
  602. cx25840_and_or(client, 0x400, ~0xf, 1);
  603. /* Turn off LCOMB */
  604. cx25840_and_or(client, 0x47b, ~6, 0);
  605. }
  606. cx25840_and_or(client, 0x400, ~0xf, fmt);
  607. cx25840_and_or(client, 0x403, ~0x3, pal_m);
  608. cx25840_std_setup(client);
  609. if (!state->is_cx25836)
  610. input_change(client);
  611. return 0;
  612. }
  613. /* ----------------------------------------------------------------------- */
  614. static int cx25840_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  615. {
  616. struct cx25840_state *state = to_state(sd);
  617. struct i2c_client *client = v4l2_get_subdevdata(sd);
  618. switch (ctrl->id) {
  619. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  620. state->pvr150_workaround = ctrl->value;
  621. set_input(client, state->vid_input, state->aud_input);
  622. break;
  623. case V4L2_CID_BRIGHTNESS:
  624. if (ctrl->value < 0 || ctrl->value > 255) {
  625. v4l_err(client, "invalid brightness setting %d\n",
  626. ctrl->value);
  627. return -ERANGE;
  628. }
  629. cx25840_write(client, 0x414, ctrl->value - 128);
  630. break;
  631. case V4L2_CID_CONTRAST:
  632. if (ctrl->value < 0 || ctrl->value > 127) {
  633. v4l_err(client, "invalid contrast setting %d\n",
  634. ctrl->value);
  635. return -ERANGE;
  636. }
  637. cx25840_write(client, 0x415, ctrl->value << 1);
  638. break;
  639. case V4L2_CID_SATURATION:
  640. if (ctrl->value < 0 || ctrl->value > 127) {
  641. v4l_err(client, "invalid saturation setting %d\n",
  642. ctrl->value);
  643. return -ERANGE;
  644. }
  645. cx25840_write(client, 0x420, ctrl->value << 1);
  646. cx25840_write(client, 0x421, ctrl->value << 1);
  647. break;
  648. case V4L2_CID_HUE:
  649. if (ctrl->value < -127 || ctrl->value > 127) {
  650. v4l_err(client, "invalid hue setting %d\n", ctrl->value);
  651. return -ERANGE;
  652. }
  653. cx25840_write(client, 0x422, ctrl->value);
  654. break;
  655. case V4L2_CID_AUDIO_VOLUME:
  656. case V4L2_CID_AUDIO_BASS:
  657. case V4L2_CID_AUDIO_TREBLE:
  658. case V4L2_CID_AUDIO_BALANCE:
  659. case V4L2_CID_AUDIO_MUTE:
  660. if (state->is_cx25836)
  661. return -EINVAL;
  662. return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
  663. default:
  664. return -EINVAL;
  665. }
  666. return 0;
  667. }
  668. static int cx25840_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  669. {
  670. struct cx25840_state *state = to_state(sd);
  671. struct i2c_client *client = v4l2_get_subdevdata(sd);
  672. switch (ctrl->id) {
  673. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  674. ctrl->value = state->pvr150_workaround;
  675. break;
  676. case V4L2_CID_BRIGHTNESS:
  677. ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
  678. break;
  679. case V4L2_CID_CONTRAST:
  680. ctrl->value = cx25840_read(client, 0x415) >> 1;
  681. break;
  682. case V4L2_CID_SATURATION:
  683. ctrl->value = cx25840_read(client, 0x420) >> 1;
  684. break;
  685. case V4L2_CID_HUE:
  686. ctrl->value = (s8)cx25840_read(client, 0x422);
  687. break;
  688. case V4L2_CID_AUDIO_VOLUME:
  689. case V4L2_CID_AUDIO_BASS:
  690. case V4L2_CID_AUDIO_TREBLE:
  691. case V4L2_CID_AUDIO_BALANCE:
  692. case V4L2_CID_AUDIO_MUTE:
  693. if (state->is_cx25836)
  694. return -EINVAL;
  695. return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
  696. default:
  697. return -EINVAL;
  698. }
  699. return 0;
  700. }
  701. /* ----------------------------------------------------------------------- */
  702. static int cx25840_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  703. {
  704. struct i2c_client *client = v4l2_get_subdevdata(sd);
  705. switch (fmt->type) {
  706. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  707. return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
  708. default:
  709. return -EINVAL;
  710. }
  711. return 0;
  712. }
  713. static int cx25840_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  714. {
  715. struct cx25840_state *state = to_state(sd);
  716. struct i2c_client *client = v4l2_get_subdevdata(sd);
  717. struct v4l2_pix_format *pix;
  718. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  719. int is_50Hz = !(state->std & V4L2_STD_525_60);
  720. switch (fmt->type) {
  721. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  722. pix = &(fmt->fmt.pix);
  723. Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
  724. Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
  725. Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
  726. Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
  727. Vlines = pix->height + (is_50Hz ? 4 : 7);
  728. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  729. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  730. v4l_err(client, "%dx%d is not a valid size!\n",
  731. pix->width, pix->height);
  732. return -ERANGE;
  733. }
  734. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  735. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  736. VSC &= 0x1fff;
  737. if (pix->width >= 385)
  738. filter = 0;
  739. else if (pix->width > 192)
  740. filter = 1;
  741. else if (pix->width > 96)
  742. filter = 2;
  743. else
  744. filter = 3;
  745. v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
  746. pix->width, pix->height, HSC, VSC);
  747. /* HSCALE=HSC */
  748. cx25840_write(client, 0x418, HSC & 0xff);
  749. cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
  750. cx25840_write(client, 0x41a, HSC >> 16);
  751. /* VSCALE=VSC */
  752. cx25840_write(client, 0x41c, VSC & 0xff);
  753. cx25840_write(client, 0x41d, VSC >> 8);
  754. /* VS_INTRLACE=1 VFILT=filter */
  755. cx25840_write(client, 0x41e, 0x8 | filter);
  756. break;
  757. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  758. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  759. case V4L2_BUF_TYPE_VBI_CAPTURE:
  760. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  761. default:
  762. return -EINVAL;
  763. }
  764. return 0;
  765. }
  766. /* ----------------------------------------------------------------------- */
  767. static void log_video_status(struct i2c_client *client)
  768. {
  769. static const char *const fmt_strs[] = {
  770. "0x0",
  771. "NTSC-M", "NTSC-J", "NTSC-4.43",
  772. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  773. "0x9", "0xA", "0xB",
  774. "SECAM",
  775. "0xD", "0xE", "0xF"
  776. };
  777. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  778. u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
  779. u8 gen_stat1 = cx25840_read(client, 0x40d);
  780. u8 gen_stat2 = cx25840_read(client, 0x40e);
  781. int vid_input = state->vid_input;
  782. v4l_info(client, "Video signal: %spresent\n",
  783. (gen_stat2 & 0x20) ? "" : "not ");
  784. v4l_info(client, "Detected format: %s\n",
  785. fmt_strs[gen_stat1 & 0xf]);
  786. v4l_info(client, "Specified standard: %s\n",
  787. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  788. if (vid_input >= CX25840_COMPOSITE1 &&
  789. vid_input <= CX25840_COMPOSITE8) {
  790. v4l_info(client, "Specified video input: Composite %d\n",
  791. vid_input - CX25840_COMPOSITE1 + 1);
  792. } else {
  793. v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  794. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  795. }
  796. v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
  797. }
  798. /* ----------------------------------------------------------------------- */
  799. static void log_audio_status(struct i2c_client *client)
  800. {
  801. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  802. u8 download_ctl = cx25840_read(client, 0x803);
  803. u8 mod_det_stat0 = cx25840_read(client, 0x804);
  804. u8 mod_det_stat1 = cx25840_read(client, 0x805);
  805. u8 audio_config = cx25840_read(client, 0x808);
  806. u8 pref_mode = cx25840_read(client, 0x809);
  807. u8 afc0 = cx25840_read(client, 0x80b);
  808. u8 mute_ctl = cx25840_read(client, 0x8d3);
  809. int aud_input = state->aud_input;
  810. char *p;
  811. switch (mod_det_stat0) {
  812. case 0x00: p = "mono"; break;
  813. case 0x01: p = "stereo"; break;
  814. case 0x02: p = "dual"; break;
  815. case 0x04: p = "tri"; break;
  816. case 0x10: p = "mono with SAP"; break;
  817. case 0x11: p = "stereo with SAP"; break;
  818. case 0x12: p = "dual with SAP"; break;
  819. case 0x14: p = "tri with SAP"; break;
  820. case 0xfe: p = "forced mode"; break;
  821. default: p = "not defined";
  822. }
  823. v4l_info(client, "Detected audio mode: %s\n", p);
  824. switch (mod_det_stat1) {
  825. case 0x00: p = "not defined"; break;
  826. case 0x01: p = "EIAJ"; break;
  827. case 0x02: p = "A2-M"; break;
  828. case 0x03: p = "A2-BG"; break;
  829. case 0x04: p = "A2-DK1"; break;
  830. case 0x05: p = "A2-DK2"; break;
  831. case 0x06: p = "A2-DK3"; break;
  832. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  833. case 0x08: p = "AM-L"; break;
  834. case 0x09: p = "NICAM-BG"; break;
  835. case 0x0a: p = "NICAM-DK"; break;
  836. case 0x0b: p = "NICAM-I"; break;
  837. case 0x0c: p = "NICAM-L"; break;
  838. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  839. case 0x0e: p = "IF FM Radio"; break;
  840. case 0x0f: p = "BTSC"; break;
  841. case 0x10: p = "high-deviation FM"; break;
  842. case 0x11: p = "very high-deviation FM"; break;
  843. case 0xfd: p = "unknown audio standard"; break;
  844. case 0xfe: p = "forced audio standard"; break;
  845. case 0xff: p = "no detected audio standard"; break;
  846. default: p = "not defined";
  847. }
  848. v4l_info(client, "Detected audio standard: %s\n", p);
  849. v4l_info(client, "Audio muted: %s\n",
  850. (state->unmute_volume >= 0) ? "yes" : "no");
  851. v4l_info(client, "Audio microcontroller: %s\n",
  852. (download_ctl & 0x10) ?
  853. ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
  854. switch (audio_config >> 4) {
  855. case 0x00: p = "undefined"; break;
  856. case 0x01: p = "BTSC"; break;
  857. case 0x02: p = "EIAJ"; break;
  858. case 0x03: p = "A2-M"; break;
  859. case 0x04: p = "A2-BG"; break;
  860. case 0x05: p = "A2-DK1"; break;
  861. case 0x06: p = "A2-DK2"; break;
  862. case 0x07: p = "A2-DK3"; break;
  863. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  864. case 0x09: p = "AM-L"; break;
  865. case 0x0a: p = "NICAM-BG"; break;
  866. case 0x0b: p = "NICAM-DK"; break;
  867. case 0x0c: p = "NICAM-I"; break;
  868. case 0x0d: p = "NICAM-L"; break;
  869. case 0x0e: p = "FM radio"; break;
  870. case 0x0f: p = "automatic detection"; break;
  871. default: p = "undefined";
  872. }
  873. v4l_info(client, "Configured audio standard: %s\n", p);
  874. if ((audio_config >> 4) < 0xF) {
  875. switch (audio_config & 0xF) {
  876. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  877. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  878. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  879. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  880. case 0x04: p = "STEREO"; break;
  881. case 0x05: p = "DUAL1 (AB)"; break;
  882. case 0x06: p = "DUAL2 (AC) (FM)"; break;
  883. case 0x07: p = "DUAL3 (BC) (FM)"; break;
  884. case 0x08: p = "DUAL4 (AC) (AM)"; break;
  885. case 0x09: p = "DUAL5 (BC) (AM)"; break;
  886. case 0x0a: p = "SAP"; break;
  887. default: p = "undefined";
  888. }
  889. v4l_info(client, "Configured audio mode: %s\n", p);
  890. } else {
  891. switch (audio_config & 0xF) {
  892. case 0x00: p = "BG"; break;
  893. case 0x01: p = "DK1"; break;
  894. case 0x02: p = "DK2"; break;
  895. case 0x03: p = "DK3"; break;
  896. case 0x04: p = "I"; break;
  897. case 0x05: p = "L"; break;
  898. case 0x06: p = "BTSC"; break;
  899. case 0x07: p = "EIAJ"; break;
  900. case 0x08: p = "A2-M"; break;
  901. case 0x09: p = "FM Radio"; break;
  902. case 0x0f: p = "automatic standard and mode detection"; break;
  903. default: p = "undefined";
  904. }
  905. v4l_info(client, "Configured audio system: %s\n", p);
  906. }
  907. if (aud_input) {
  908. v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
  909. } else {
  910. v4l_info(client, "Specified audio input: External\n");
  911. }
  912. switch (pref_mode & 0xf) {
  913. case 0: p = "mono/language A"; break;
  914. case 1: p = "language B"; break;
  915. case 2: p = "language C"; break;
  916. case 3: p = "analog fallback"; break;
  917. case 4: p = "stereo"; break;
  918. case 5: p = "language AC"; break;
  919. case 6: p = "language BC"; break;
  920. case 7: p = "language AB"; break;
  921. default: p = "undefined";
  922. }
  923. v4l_info(client, "Preferred audio mode: %s\n", p);
  924. if ((audio_config & 0xf) == 0xf) {
  925. switch ((afc0 >> 3) & 0x3) {
  926. case 0: p = "system DK"; break;
  927. case 1: p = "system L"; break;
  928. case 2: p = "autodetect"; break;
  929. default: p = "undefined";
  930. }
  931. v4l_info(client, "Selected 65 MHz format: %s\n", p);
  932. switch (afc0 & 0x7) {
  933. case 0: p = "chroma"; break;
  934. case 1: p = "BTSC"; break;
  935. case 2: p = "EIAJ"; break;
  936. case 3: p = "A2-M"; break;
  937. case 4: p = "autodetect"; break;
  938. default: p = "undefined";
  939. }
  940. v4l_info(client, "Selected 45 MHz format: %s\n", p);
  941. }
  942. }
  943. /* ----------------------------------------------------------------------- */
  944. static int cx25840_init(struct v4l2_subdev *sd, u32 val)
  945. {
  946. struct cx25840_state *state = to_state(sd);
  947. struct i2c_client *client = v4l2_get_subdevdata(sd);
  948. if (!state->is_initialized) {
  949. /* initialize on first use */
  950. state->is_initialized = 1;
  951. if (state->is_cx25836)
  952. cx25836_initialize(client);
  953. else if (state->is_cx23885)
  954. cx23885_initialize(client);
  955. else
  956. cx25840_initialize(client);
  957. }
  958. return 0;
  959. }
  960. #ifdef CONFIG_VIDEO_ADV_DEBUG
  961. static int cx25840_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  962. {
  963. struct i2c_client *client = v4l2_get_subdevdata(sd);
  964. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  965. return -EINVAL;
  966. if (!capable(CAP_SYS_ADMIN))
  967. return -EPERM;
  968. reg->size = 1;
  969. reg->val = cx25840_read(client, reg->reg & 0x0fff);
  970. return 0;
  971. }
  972. static int cx25840_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  973. {
  974. struct i2c_client *client = v4l2_get_subdevdata(sd);
  975. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  976. return -EINVAL;
  977. if (!capable(CAP_SYS_ADMIN))
  978. return -EPERM;
  979. cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
  980. return 0;
  981. }
  982. #endif
  983. static int cx25840_decode_vbi_line(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi)
  984. {
  985. struct i2c_client *client = v4l2_get_subdevdata(sd);
  986. return cx25840_vbi(client, VIDIOC_INT_DECODE_VBI_LINE, vbi);
  987. }
  988. static int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  989. {
  990. struct i2c_client *client = v4l2_get_subdevdata(sd);
  991. return cx25840_audio(client, VIDIOC_INT_AUDIO_CLOCK_FREQ, &freq);
  992. }
  993. static int cx25840_s_stream(struct v4l2_subdev *sd, int enable)
  994. {
  995. struct cx25840_state *state = to_state(sd);
  996. struct i2c_client *client = v4l2_get_subdevdata(sd);
  997. v4l_dbg(1, cx25840_debug, client, "%s output\n",
  998. enable ? "enable" : "disable");
  999. if (enable) {
  1000. if (state->is_cx23885) {
  1001. u8 v = (cx25840_read(client, 0x421) | 0x0b);
  1002. cx25840_write(client, 0x421, v);
  1003. } else {
  1004. cx25840_write(client, 0x115,
  1005. state->is_cx25836 ? 0x0c : 0x8c);
  1006. cx25840_write(client, 0x116,
  1007. state->is_cx25836 ? 0x04 : 0x07);
  1008. }
  1009. } else {
  1010. if (state->is_cx23885) {
  1011. u8 v = cx25840_read(client, 0x421) & ~(0x0b);
  1012. cx25840_write(client, 0x421, v);
  1013. } else {
  1014. cx25840_write(client, 0x115, 0x00);
  1015. cx25840_write(client, 0x116, 0x00);
  1016. }
  1017. }
  1018. return 0;
  1019. }
  1020. static int cx25840_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  1021. {
  1022. struct cx25840_state *state = to_state(sd);
  1023. switch (qc->id) {
  1024. case V4L2_CID_BRIGHTNESS:
  1025. case V4L2_CID_CONTRAST:
  1026. case V4L2_CID_SATURATION:
  1027. case V4L2_CID_HUE:
  1028. return v4l2_ctrl_query_fill_std(qc);
  1029. default:
  1030. break;
  1031. }
  1032. if (state->is_cx25836)
  1033. return -EINVAL;
  1034. switch (qc->id) {
  1035. case V4L2_CID_AUDIO_VOLUME:
  1036. return v4l2_ctrl_query_fill(qc, 0, 65535,
  1037. 65535 / 100, state->default_volume);
  1038. case V4L2_CID_AUDIO_MUTE:
  1039. case V4L2_CID_AUDIO_BALANCE:
  1040. case V4L2_CID_AUDIO_BASS:
  1041. case V4L2_CID_AUDIO_TREBLE:
  1042. return v4l2_ctrl_query_fill_std(qc);
  1043. default:
  1044. return -EINVAL;
  1045. }
  1046. return -EINVAL;
  1047. }
  1048. static int cx25840_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  1049. {
  1050. struct cx25840_state *state = to_state(sd);
  1051. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1052. if (state->radio == 0 && state->std == std)
  1053. return 0;
  1054. state->radio = 0;
  1055. state->std = std;
  1056. return set_v4lstd(client);
  1057. }
  1058. static int cx25840_s_radio(struct v4l2_subdev *sd)
  1059. {
  1060. struct cx25840_state *state = to_state(sd);
  1061. state->radio = 1;
  1062. return 0;
  1063. }
  1064. static int cx25840_s_video_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
  1065. {
  1066. struct cx25840_state *state = to_state(sd);
  1067. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1068. return set_input(client, route->input, state->aud_input);
  1069. }
  1070. static int cx25840_s_audio_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
  1071. {
  1072. struct cx25840_state *state = to_state(sd);
  1073. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1074. if (state->is_cx25836)
  1075. return -EINVAL;
  1076. return set_input(client, state->vid_input, route->input);
  1077. }
  1078. static int cx25840_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)
  1079. {
  1080. struct cx25840_state *state = to_state(sd);
  1081. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1082. if (!state->is_cx25836)
  1083. input_change(client);
  1084. return 0;
  1085. }
  1086. static int cx25840_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  1087. {
  1088. struct cx25840_state *state = to_state(sd);
  1089. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1090. u8 vpres = cx25840_read(client, 0x40e) & 0x20;
  1091. u8 mode;
  1092. int val = 0;
  1093. if (state->radio)
  1094. return 0;
  1095. vt->signal = vpres ? 0xffff : 0x0;
  1096. if (state->is_cx25836)
  1097. return 0;
  1098. vt->capability |=
  1099. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  1100. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  1101. mode = cx25840_read(client, 0x804);
  1102. /* get rxsubchans and audmode */
  1103. if ((mode & 0xf) == 1)
  1104. val |= V4L2_TUNER_SUB_STEREO;
  1105. else
  1106. val |= V4L2_TUNER_SUB_MONO;
  1107. if (mode == 2 || mode == 4)
  1108. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  1109. if (mode & 0x10)
  1110. val |= V4L2_TUNER_SUB_SAP;
  1111. vt->rxsubchans = val;
  1112. vt->audmode = state->audmode;
  1113. return 0;
  1114. }
  1115. static int cx25840_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  1116. {
  1117. struct cx25840_state *state = to_state(sd);
  1118. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1119. if (state->radio || state->is_cx25836)
  1120. return 0;
  1121. switch (vt->audmode) {
  1122. case V4L2_TUNER_MODE_MONO:
  1123. /* mono -> mono
  1124. stereo -> mono
  1125. bilingual -> lang1 */
  1126. cx25840_and_or(client, 0x809, ~0xf, 0x00);
  1127. break;
  1128. case V4L2_TUNER_MODE_STEREO:
  1129. case V4L2_TUNER_MODE_LANG1:
  1130. /* mono -> mono
  1131. stereo -> stereo
  1132. bilingual -> lang1 */
  1133. cx25840_and_or(client, 0x809, ~0xf, 0x04);
  1134. break;
  1135. case V4L2_TUNER_MODE_LANG1_LANG2:
  1136. /* mono -> mono
  1137. stereo -> stereo
  1138. bilingual -> lang1/lang2 */
  1139. cx25840_and_or(client, 0x809, ~0xf, 0x07);
  1140. break;
  1141. case V4L2_TUNER_MODE_LANG2:
  1142. /* mono -> mono
  1143. stereo -> stereo
  1144. bilingual -> lang2 */
  1145. cx25840_and_or(client, 0x809, ~0xf, 0x01);
  1146. break;
  1147. default:
  1148. return -EINVAL;
  1149. }
  1150. state->audmode = vt->audmode;
  1151. return 0;
  1152. }
  1153. static int cx25840_reset(struct v4l2_subdev *sd, u32 val)
  1154. {
  1155. struct cx25840_state *state = to_state(sd);
  1156. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1157. if (state->is_cx25836)
  1158. cx25836_initialize(client);
  1159. else if (state->is_cx23885)
  1160. cx23885_initialize(client);
  1161. else
  1162. cx25840_initialize(client);
  1163. return 0;
  1164. }
  1165. static int cx25840_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  1166. {
  1167. struct cx25840_state *state = to_state(sd);
  1168. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1169. return v4l2_chip_ident_i2c_client(client, chip, state->id, state->rev);
  1170. }
  1171. static int cx25840_log_status(struct v4l2_subdev *sd)
  1172. {
  1173. struct cx25840_state *state = to_state(sd);
  1174. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1175. log_video_status(client);
  1176. if (!state->is_cx25836)
  1177. log_audio_status(client);
  1178. return 0;
  1179. }
  1180. static int cx25840_command(struct i2c_client *client, unsigned cmd, void *arg)
  1181. {
  1182. return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
  1183. }
  1184. /* ----------------------------------------------------------------------- */
  1185. static const struct v4l2_subdev_core_ops cx25840_core_ops = {
  1186. .log_status = cx25840_log_status,
  1187. .g_chip_ident = cx25840_g_chip_ident,
  1188. .g_ctrl = cx25840_g_ctrl,
  1189. .s_ctrl = cx25840_s_ctrl,
  1190. .queryctrl = cx25840_queryctrl,
  1191. .reset = cx25840_reset,
  1192. .init = cx25840_init,
  1193. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1194. .g_register = cx25840_g_register,
  1195. .s_register = cx25840_s_register,
  1196. #endif
  1197. };
  1198. static const struct v4l2_subdev_tuner_ops cx25840_tuner_ops = {
  1199. .s_frequency = cx25840_s_frequency,
  1200. .s_std = cx25840_s_std,
  1201. .s_radio = cx25840_s_radio,
  1202. .g_tuner = cx25840_g_tuner,
  1203. .s_tuner = cx25840_s_tuner,
  1204. };
  1205. static const struct v4l2_subdev_audio_ops cx25840_audio_ops = {
  1206. .s_clock_freq = cx25840_s_clock_freq,
  1207. .s_routing = cx25840_s_audio_routing,
  1208. };
  1209. static const struct v4l2_subdev_video_ops cx25840_video_ops = {
  1210. .s_routing = cx25840_s_video_routing,
  1211. .g_fmt = cx25840_g_fmt,
  1212. .s_fmt = cx25840_s_fmt,
  1213. .decode_vbi_line = cx25840_decode_vbi_line,
  1214. .s_stream = cx25840_s_stream,
  1215. };
  1216. static const struct v4l2_subdev_ops cx25840_ops = {
  1217. .core = &cx25840_core_ops,
  1218. .tuner = &cx25840_tuner_ops,
  1219. .audio = &cx25840_audio_ops,
  1220. .video = &cx25840_video_ops,
  1221. };
  1222. /* ----------------------------------------------------------------------- */
  1223. static int cx25840_probe(struct i2c_client *client,
  1224. const struct i2c_device_id *did)
  1225. {
  1226. struct cx25840_state *state;
  1227. struct v4l2_subdev *sd;
  1228. u32 id;
  1229. u16 device_id;
  1230. /* Check if the adapter supports the needed features */
  1231. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1232. return -EIO;
  1233. v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);
  1234. device_id = cx25840_read(client, 0x101) << 8;
  1235. device_id |= cx25840_read(client, 0x100);
  1236. v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);
  1237. /* The high byte of the device ID should be
  1238. * 0x83 for the cx2583x and 0x84 for the cx2584x */
  1239. if ((device_id & 0xff00) == 0x8300) {
  1240. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1241. }
  1242. else if ((device_id & 0xff00) == 0x8400) {
  1243. id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
  1244. } else if (device_id == 0x0000) {
  1245. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1246. } else if (device_id == 0x1313) {
  1247. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1248. }
  1249. else {
  1250. v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
  1251. return -ENODEV;
  1252. }
  1253. state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  1254. if (state == NULL)
  1255. return -ENOMEM;
  1256. sd = &state->sd;
  1257. v4l2_i2c_subdev_init(sd, client, &cx25840_ops);
  1258. /* Note: revision '(device_id & 0x0f) == 2' was never built. The
  1259. marking skips from 0x1 == 22 to 0x3 == 23. */
  1260. v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
  1261. (device_id & 0xfff0) >> 4,
  1262. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : (device_id & 0x0f),
  1263. client->addr << 1, client->adapter->name);
  1264. state->c = client;
  1265. state->is_cx25836 = ((device_id & 0xff00) == 0x8300);
  1266. state->is_cx23885 = (device_id == 0x0000) || (device_id == 0x1313);
  1267. state->vid_input = CX25840_COMPOSITE7;
  1268. state->aud_input = CX25840_AUDIO8;
  1269. state->audclk_freq = 48000;
  1270. state->pvr150_workaround = 0;
  1271. state->audmode = V4L2_TUNER_MODE_LANG1;
  1272. state->unmute_volume = -1;
  1273. state->default_volume = 228 - cx25840_read(client, 0x8d4);
  1274. state->default_volume = ((state->default_volume / 2) + 23) << 9;
  1275. state->vbi_line_offset = 8;
  1276. state->id = id;
  1277. state->rev = device_id;
  1278. if (state->is_cx23885) {
  1279. /* Drive GPIO2 direction and values */
  1280. cx25840_write(client, 0x160, 0x1d);
  1281. cx25840_write(client, 0x164, 0x00);
  1282. }
  1283. return 0;
  1284. }
  1285. static int cx25840_remove(struct i2c_client *client)
  1286. {
  1287. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1288. v4l2_device_unregister_subdev(sd);
  1289. kfree(to_state(sd));
  1290. return 0;
  1291. }
  1292. static const struct i2c_device_id cx25840_id[] = {
  1293. { "cx25840", 0 },
  1294. { }
  1295. };
  1296. MODULE_DEVICE_TABLE(i2c, cx25840_id);
  1297. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  1298. .name = "cx25840",
  1299. .driverid = I2C_DRIVERID_CX25840,
  1300. .command = cx25840_command,
  1301. .probe = cx25840_probe,
  1302. .remove = cx25840_remove,
  1303. .id_table = cx25840_id,
  1304. };