cx25840-core.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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 = 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 = 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 = 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 = 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 = 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 = 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 set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  615. {
  616. struct cx25840_state *state = i2c_get_clientdata(client);
  617. switch (ctrl->id) {
  618. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  619. state->pvr150_workaround = ctrl->value;
  620. set_input(client, state->vid_input, state->aud_input);
  621. break;
  622. case V4L2_CID_BRIGHTNESS:
  623. if (ctrl->value < 0 || ctrl->value > 255) {
  624. v4l_err(client, "invalid brightness setting %d\n",
  625. ctrl->value);
  626. return -ERANGE;
  627. }
  628. cx25840_write(client, 0x414, ctrl->value - 128);
  629. break;
  630. case V4L2_CID_CONTRAST:
  631. if (ctrl->value < 0 || ctrl->value > 127) {
  632. v4l_err(client, "invalid contrast setting %d\n",
  633. ctrl->value);
  634. return -ERANGE;
  635. }
  636. cx25840_write(client, 0x415, ctrl->value << 1);
  637. break;
  638. case V4L2_CID_SATURATION:
  639. if (ctrl->value < 0 || ctrl->value > 127) {
  640. v4l_err(client, "invalid saturation setting %d\n",
  641. ctrl->value);
  642. return -ERANGE;
  643. }
  644. cx25840_write(client, 0x420, ctrl->value << 1);
  645. cx25840_write(client, 0x421, ctrl->value << 1);
  646. break;
  647. case V4L2_CID_HUE:
  648. if (ctrl->value < -127 || ctrl->value > 127) {
  649. v4l_err(client, "invalid hue setting %d\n", ctrl->value);
  650. return -ERANGE;
  651. }
  652. cx25840_write(client, 0x422, ctrl->value);
  653. break;
  654. case V4L2_CID_AUDIO_VOLUME:
  655. case V4L2_CID_AUDIO_BASS:
  656. case V4L2_CID_AUDIO_TREBLE:
  657. case V4L2_CID_AUDIO_BALANCE:
  658. case V4L2_CID_AUDIO_MUTE:
  659. if (state->is_cx25836)
  660. return -EINVAL;
  661. return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
  662. default:
  663. return -EINVAL;
  664. }
  665. return 0;
  666. }
  667. static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  668. {
  669. struct cx25840_state *state = i2c_get_clientdata(client);
  670. switch (ctrl->id) {
  671. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  672. ctrl->value = state->pvr150_workaround;
  673. break;
  674. case V4L2_CID_BRIGHTNESS:
  675. ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
  676. break;
  677. case V4L2_CID_CONTRAST:
  678. ctrl->value = cx25840_read(client, 0x415) >> 1;
  679. break;
  680. case V4L2_CID_SATURATION:
  681. ctrl->value = cx25840_read(client, 0x420) >> 1;
  682. break;
  683. case V4L2_CID_HUE:
  684. ctrl->value = (s8)cx25840_read(client, 0x422);
  685. break;
  686. case V4L2_CID_AUDIO_VOLUME:
  687. case V4L2_CID_AUDIO_BASS:
  688. case V4L2_CID_AUDIO_TREBLE:
  689. case V4L2_CID_AUDIO_BALANCE:
  690. case V4L2_CID_AUDIO_MUTE:
  691. if (state->is_cx25836)
  692. return -EINVAL;
  693. return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
  694. default:
  695. return -EINVAL;
  696. }
  697. return 0;
  698. }
  699. /* ----------------------------------------------------------------------- */
  700. static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  701. {
  702. switch (fmt->type) {
  703. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  704. return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
  705. default:
  706. return -EINVAL;
  707. }
  708. return 0;
  709. }
  710. static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  711. {
  712. struct cx25840_state *state = i2c_get_clientdata(client);
  713. struct v4l2_pix_format *pix;
  714. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  715. int is_50Hz = !(state->std & V4L2_STD_525_60);
  716. switch (fmt->type) {
  717. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  718. pix = &(fmt->fmt.pix);
  719. Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
  720. Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
  721. Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
  722. Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
  723. Vlines = pix->height + (is_50Hz ? 4 : 7);
  724. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  725. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  726. v4l_err(client, "%dx%d is not a valid size!\n",
  727. pix->width, pix->height);
  728. return -ERANGE;
  729. }
  730. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  731. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  732. VSC &= 0x1fff;
  733. if (pix->width >= 385)
  734. filter = 0;
  735. else if (pix->width > 192)
  736. filter = 1;
  737. else if (pix->width > 96)
  738. filter = 2;
  739. else
  740. filter = 3;
  741. v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
  742. pix->width, pix->height, HSC, VSC);
  743. /* HSCALE=HSC */
  744. cx25840_write(client, 0x418, HSC & 0xff);
  745. cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
  746. cx25840_write(client, 0x41a, HSC >> 16);
  747. /* VSCALE=VSC */
  748. cx25840_write(client, 0x41c, VSC & 0xff);
  749. cx25840_write(client, 0x41d, VSC >> 8);
  750. /* VS_INTRLACE=1 VFILT=filter */
  751. cx25840_write(client, 0x41e, 0x8 | filter);
  752. break;
  753. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  754. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  755. case V4L2_BUF_TYPE_VBI_CAPTURE:
  756. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  757. default:
  758. return -EINVAL;
  759. }
  760. return 0;
  761. }
  762. /* ----------------------------------------------------------------------- */
  763. static void log_video_status(struct i2c_client *client)
  764. {
  765. static const char *const fmt_strs[] = {
  766. "0x0",
  767. "NTSC-M", "NTSC-J", "NTSC-4.43",
  768. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  769. "0x9", "0xA", "0xB",
  770. "SECAM",
  771. "0xD", "0xE", "0xF"
  772. };
  773. struct cx25840_state *state = i2c_get_clientdata(client);
  774. u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
  775. u8 gen_stat1 = cx25840_read(client, 0x40d);
  776. u8 gen_stat2 = cx25840_read(client, 0x40e);
  777. int vid_input = state->vid_input;
  778. v4l_info(client, "Video signal: %spresent\n",
  779. (gen_stat2 & 0x20) ? "" : "not ");
  780. v4l_info(client, "Detected format: %s\n",
  781. fmt_strs[gen_stat1 & 0xf]);
  782. v4l_info(client, "Specified standard: %s\n",
  783. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  784. if (vid_input >= CX25840_COMPOSITE1 &&
  785. vid_input <= CX25840_COMPOSITE8) {
  786. v4l_info(client, "Specified video input: Composite %d\n",
  787. vid_input - CX25840_COMPOSITE1 + 1);
  788. } else {
  789. v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  790. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  791. }
  792. v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
  793. }
  794. /* ----------------------------------------------------------------------- */
  795. static void log_audio_status(struct i2c_client *client)
  796. {
  797. struct cx25840_state *state = i2c_get_clientdata(client);
  798. u8 download_ctl = cx25840_read(client, 0x803);
  799. u8 mod_det_stat0 = cx25840_read(client, 0x804);
  800. u8 mod_det_stat1 = cx25840_read(client, 0x805);
  801. u8 audio_config = cx25840_read(client, 0x808);
  802. u8 pref_mode = cx25840_read(client, 0x809);
  803. u8 afc0 = cx25840_read(client, 0x80b);
  804. u8 mute_ctl = cx25840_read(client, 0x8d3);
  805. int aud_input = state->aud_input;
  806. char *p;
  807. switch (mod_det_stat0) {
  808. case 0x00: p = "mono"; break;
  809. case 0x01: p = "stereo"; break;
  810. case 0x02: p = "dual"; break;
  811. case 0x04: p = "tri"; break;
  812. case 0x10: p = "mono with SAP"; break;
  813. case 0x11: p = "stereo with SAP"; break;
  814. case 0x12: p = "dual with SAP"; break;
  815. case 0x14: p = "tri with SAP"; break;
  816. case 0xfe: p = "forced mode"; break;
  817. default: p = "not defined";
  818. }
  819. v4l_info(client, "Detected audio mode: %s\n", p);
  820. switch (mod_det_stat1) {
  821. case 0x00: p = "not defined"; break;
  822. case 0x01: p = "EIAJ"; break;
  823. case 0x02: p = "A2-M"; break;
  824. case 0x03: p = "A2-BG"; break;
  825. case 0x04: p = "A2-DK1"; break;
  826. case 0x05: p = "A2-DK2"; break;
  827. case 0x06: p = "A2-DK3"; break;
  828. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  829. case 0x08: p = "AM-L"; break;
  830. case 0x09: p = "NICAM-BG"; break;
  831. case 0x0a: p = "NICAM-DK"; break;
  832. case 0x0b: p = "NICAM-I"; break;
  833. case 0x0c: p = "NICAM-L"; break;
  834. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  835. case 0x0e: p = "IF FM Radio"; break;
  836. case 0x0f: p = "BTSC"; break;
  837. case 0x10: p = "high-deviation FM"; break;
  838. case 0x11: p = "very high-deviation FM"; break;
  839. case 0xfd: p = "unknown audio standard"; break;
  840. case 0xfe: p = "forced audio standard"; break;
  841. case 0xff: p = "no detected audio standard"; break;
  842. default: p = "not defined";
  843. }
  844. v4l_info(client, "Detected audio standard: %s\n", p);
  845. v4l_info(client, "Audio muted: %s\n",
  846. (state->unmute_volume >= 0) ? "yes" : "no");
  847. v4l_info(client, "Audio microcontroller: %s\n",
  848. (download_ctl & 0x10) ?
  849. ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
  850. switch (audio_config >> 4) {
  851. case 0x00: p = "undefined"; break;
  852. case 0x01: p = "BTSC"; break;
  853. case 0x02: p = "EIAJ"; break;
  854. case 0x03: p = "A2-M"; break;
  855. case 0x04: p = "A2-BG"; break;
  856. case 0x05: p = "A2-DK1"; break;
  857. case 0x06: p = "A2-DK2"; break;
  858. case 0x07: p = "A2-DK3"; break;
  859. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  860. case 0x09: p = "AM-L"; break;
  861. case 0x0a: p = "NICAM-BG"; break;
  862. case 0x0b: p = "NICAM-DK"; break;
  863. case 0x0c: p = "NICAM-I"; break;
  864. case 0x0d: p = "NICAM-L"; break;
  865. case 0x0e: p = "FM radio"; break;
  866. case 0x0f: p = "automatic detection"; break;
  867. default: p = "undefined";
  868. }
  869. v4l_info(client, "Configured audio standard: %s\n", p);
  870. if ((audio_config >> 4) < 0xF) {
  871. switch (audio_config & 0xF) {
  872. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  873. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  874. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  875. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  876. case 0x04: p = "STEREO"; break;
  877. case 0x05: p = "DUAL1 (AB)"; break;
  878. case 0x06: p = "DUAL2 (AC) (FM)"; break;
  879. case 0x07: p = "DUAL3 (BC) (FM)"; break;
  880. case 0x08: p = "DUAL4 (AC) (AM)"; break;
  881. case 0x09: p = "DUAL5 (BC) (AM)"; break;
  882. case 0x0a: p = "SAP"; break;
  883. default: p = "undefined";
  884. }
  885. v4l_info(client, "Configured audio mode: %s\n", p);
  886. } else {
  887. switch (audio_config & 0xF) {
  888. case 0x00: p = "BG"; break;
  889. case 0x01: p = "DK1"; break;
  890. case 0x02: p = "DK2"; break;
  891. case 0x03: p = "DK3"; break;
  892. case 0x04: p = "I"; break;
  893. case 0x05: p = "L"; break;
  894. case 0x06: p = "BTSC"; break;
  895. case 0x07: p = "EIAJ"; break;
  896. case 0x08: p = "A2-M"; break;
  897. case 0x09: p = "FM Radio"; break;
  898. case 0x0f: p = "automatic standard and mode detection"; break;
  899. default: p = "undefined";
  900. }
  901. v4l_info(client, "Configured audio system: %s\n", p);
  902. }
  903. if (aud_input) {
  904. v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
  905. } else {
  906. v4l_info(client, "Specified audio input: External\n");
  907. }
  908. switch (pref_mode & 0xf) {
  909. case 0: p = "mono/language A"; break;
  910. case 1: p = "language B"; break;
  911. case 2: p = "language C"; break;
  912. case 3: p = "analog fallback"; break;
  913. case 4: p = "stereo"; break;
  914. case 5: p = "language AC"; break;
  915. case 6: p = "language BC"; break;
  916. case 7: p = "language AB"; break;
  917. default: p = "undefined";
  918. }
  919. v4l_info(client, "Preferred audio mode: %s\n", p);
  920. if ((audio_config & 0xf) == 0xf) {
  921. switch ((afc0 >> 3) & 0x3) {
  922. case 0: p = "system DK"; break;
  923. case 1: p = "system L"; break;
  924. case 2: p = "autodetect"; break;
  925. default: p = "undefined";
  926. }
  927. v4l_info(client, "Selected 65 MHz format: %s\n", p);
  928. switch (afc0 & 0x7) {
  929. case 0: p = "chroma"; break;
  930. case 1: p = "BTSC"; break;
  931. case 2: p = "EIAJ"; break;
  932. case 3: p = "A2-M"; break;
  933. case 4: p = "autodetect"; break;
  934. default: p = "undefined";
  935. }
  936. v4l_info(client, "Selected 45 MHz format: %s\n", p);
  937. }
  938. }
  939. /* ----------------------------------------------------------------------- */
  940. static int cx25840_command(struct i2c_client *client, unsigned int cmd,
  941. void *arg)
  942. {
  943. struct cx25840_state *state = i2c_get_clientdata(client);
  944. struct v4l2_tuner *vt = arg;
  945. struct v4l2_routing *route = arg;
  946. /* ignore these commands */
  947. switch (cmd) {
  948. case TUNER_SET_TYPE_ADDR:
  949. return 0;
  950. }
  951. if (!state->is_initialized) {
  952. v4l_dbg(1, cx25840_debug, client, "cmd %08x triggered fw load\n", cmd);
  953. /* initialize on first use */
  954. state->is_initialized = 1;
  955. if (state->is_cx25836)
  956. cx25836_initialize(client);
  957. else if (state->is_cx23885)
  958. cx23885_initialize(client);
  959. else
  960. cx25840_initialize(client);
  961. }
  962. switch (cmd) {
  963. #ifdef CONFIG_VIDEO_ADV_DEBUG
  964. /* ioctls to allow direct access to the
  965. * cx25840 registers for testing */
  966. case VIDIOC_DBG_G_REGISTER:
  967. case VIDIOC_DBG_S_REGISTER:
  968. {
  969. struct v4l2_register *reg = arg;
  970. if (!v4l2_chip_match_i2c_client(client, reg->match_type, reg->match_chip))
  971. return -EINVAL;
  972. if (!capable(CAP_SYS_ADMIN))
  973. return -EPERM;
  974. if (cmd == VIDIOC_DBG_G_REGISTER)
  975. reg->val = cx25840_read(client, reg->reg & 0x0fff);
  976. else
  977. cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
  978. break;
  979. }
  980. #endif
  981. case VIDIOC_INT_DECODE_VBI_LINE:
  982. return cx25840_vbi(client, cmd, arg);
  983. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  984. return cx25840_audio(client, cmd, arg);
  985. case VIDIOC_STREAMON:
  986. v4l_dbg(1, cx25840_debug, client, "enable output\n");
  987. if (state->is_cx23885) {
  988. u8 v = (cx25840_read(client, 0x421) | 0x0b);
  989. cx25840_write(client, 0x421, v);
  990. } else {
  991. cx25840_write(client, 0x115,
  992. state->is_cx25836 ? 0x0c : 0x8c);
  993. cx25840_write(client, 0x116,
  994. state->is_cx25836 ? 0x04 : 0x07);
  995. }
  996. break;
  997. case VIDIOC_STREAMOFF:
  998. v4l_dbg(1, cx25840_debug, client, "disable output\n");
  999. if (state->is_cx23885) {
  1000. u8 v = cx25840_read(client, 0x421) & ~(0x0b);
  1001. cx25840_write(client, 0x421, v);
  1002. } else {
  1003. cx25840_write(client, 0x115, 0x00);
  1004. cx25840_write(client, 0x116, 0x00);
  1005. }
  1006. break;
  1007. case VIDIOC_LOG_STATUS:
  1008. log_video_status(client);
  1009. if (!state->is_cx25836)
  1010. log_audio_status(client);
  1011. break;
  1012. case VIDIOC_G_CTRL:
  1013. return get_v4lctrl(client, (struct v4l2_control *)arg);
  1014. case VIDIOC_S_CTRL:
  1015. return set_v4lctrl(client, (struct v4l2_control *)arg);
  1016. case VIDIOC_QUERYCTRL:
  1017. {
  1018. struct v4l2_queryctrl *qc = arg;
  1019. switch (qc->id) {
  1020. case V4L2_CID_BRIGHTNESS:
  1021. case V4L2_CID_CONTRAST:
  1022. case V4L2_CID_SATURATION:
  1023. case V4L2_CID_HUE:
  1024. return v4l2_ctrl_query_fill_std(qc);
  1025. default:
  1026. break;
  1027. }
  1028. if (state->is_cx25836)
  1029. return -EINVAL;
  1030. switch (qc->id) {
  1031. case V4L2_CID_AUDIO_VOLUME:
  1032. return v4l2_ctrl_query_fill(qc, 0, 65535,
  1033. 65535 / 100, state->default_volume);
  1034. case V4L2_CID_AUDIO_MUTE:
  1035. case V4L2_CID_AUDIO_BALANCE:
  1036. case V4L2_CID_AUDIO_BASS:
  1037. case V4L2_CID_AUDIO_TREBLE:
  1038. return v4l2_ctrl_query_fill_std(qc);
  1039. default:
  1040. return -EINVAL;
  1041. }
  1042. return -EINVAL;
  1043. }
  1044. case VIDIOC_G_STD:
  1045. *(v4l2_std_id *)arg = state->std;
  1046. break;
  1047. case VIDIOC_S_STD:
  1048. if (state->radio == 0 && state->std == *(v4l2_std_id *)arg)
  1049. return 0;
  1050. state->radio = 0;
  1051. state->std = *(v4l2_std_id *)arg;
  1052. return set_v4lstd(client);
  1053. case AUDC_SET_RADIO:
  1054. state->radio = 1;
  1055. break;
  1056. case VIDIOC_INT_G_VIDEO_ROUTING:
  1057. route->input = state->vid_input;
  1058. route->output = 0;
  1059. break;
  1060. case VIDIOC_INT_S_VIDEO_ROUTING:
  1061. return set_input(client, route->input, state->aud_input);
  1062. case VIDIOC_INT_G_AUDIO_ROUTING:
  1063. if (state->is_cx25836)
  1064. return -EINVAL;
  1065. route->input = state->aud_input;
  1066. route->output = 0;
  1067. break;
  1068. case VIDIOC_INT_S_AUDIO_ROUTING:
  1069. if (state->is_cx25836)
  1070. return -EINVAL;
  1071. return set_input(client, state->vid_input, route->input);
  1072. case VIDIOC_S_FREQUENCY:
  1073. if (!state->is_cx25836) {
  1074. input_change(client);
  1075. }
  1076. break;
  1077. case VIDIOC_G_TUNER:
  1078. {
  1079. u8 vpres = cx25840_read(client, 0x40e) & 0x20;
  1080. u8 mode;
  1081. int val = 0;
  1082. if (state->radio)
  1083. break;
  1084. vt->signal = vpres ? 0xffff : 0x0;
  1085. if (state->is_cx25836)
  1086. break;
  1087. vt->capability |=
  1088. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  1089. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  1090. mode = cx25840_read(client, 0x804);
  1091. /* get rxsubchans and audmode */
  1092. if ((mode & 0xf) == 1)
  1093. val |= V4L2_TUNER_SUB_STEREO;
  1094. else
  1095. val |= V4L2_TUNER_SUB_MONO;
  1096. if (mode == 2 || mode == 4)
  1097. val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  1098. if (mode & 0x10)
  1099. val |= V4L2_TUNER_SUB_SAP;
  1100. vt->rxsubchans = val;
  1101. vt->audmode = state->audmode;
  1102. break;
  1103. }
  1104. case VIDIOC_S_TUNER:
  1105. if (state->radio || state->is_cx25836)
  1106. break;
  1107. switch (vt->audmode) {
  1108. case V4L2_TUNER_MODE_MONO:
  1109. /* mono -> mono
  1110. stereo -> mono
  1111. bilingual -> lang1 */
  1112. cx25840_and_or(client, 0x809, ~0xf, 0x00);
  1113. break;
  1114. case V4L2_TUNER_MODE_STEREO:
  1115. case V4L2_TUNER_MODE_LANG1:
  1116. /* mono -> mono
  1117. stereo -> stereo
  1118. bilingual -> lang1 */
  1119. cx25840_and_or(client, 0x809, ~0xf, 0x04);
  1120. break;
  1121. case V4L2_TUNER_MODE_LANG1_LANG2:
  1122. /* mono -> mono
  1123. stereo -> stereo
  1124. bilingual -> lang1/lang2 */
  1125. cx25840_and_or(client, 0x809, ~0xf, 0x07);
  1126. break;
  1127. case V4L2_TUNER_MODE_LANG2:
  1128. /* mono -> mono
  1129. stereo -> stereo
  1130. bilingual -> lang2 */
  1131. cx25840_and_or(client, 0x809, ~0xf, 0x01);
  1132. break;
  1133. default:
  1134. return -EINVAL;
  1135. }
  1136. state->audmode = vt->audmode;
  1137. break;
  1138. case VIDIOC_G_FMT:
  1139. return get_v4lfmt(client, (struct v4l2_format *)arg);
  1140. case VIDIOC_S_FMT:
  1141. return set_v4lfmt(client, (struct v4l2_format *)arg);
  1142. case VIDIOC_INT_RESET:
  1143. if (state->is_cx25836)
  1144. cx25836_initialize(client);
  1145. else if (state->is_cx23885)
  1146. cx23885_initialize(client);
  1147. else
  1148. cx25840_initialize(client);
  1149. break;
  1150. case VIDIOC_G_CHIP_IDENT:
  1151. return v4l2_chip_ident_i2c_client(client, arg, state->id, state->rev);
  1152. default:
  1153. return -EINVAL;
  1154. }
  1155. return 0;
  1156. }
  1157. /* ----------------------------------------------------------------------- */
  1158. static int cx25840_probe(struct i2c_client *client,
  1159. const struct i2c_device_id *did)
  1160. {
  1161. struct cx25840_state *state;
  1162. u32 id;
  1163. u16 device_id;
  1164. /* Check if the adapter supports the needed features */
  1165. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1166. return -EIO;
  1167. v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);
  1168. device_id = cx25840_read(client, 0x101) << 8;
  1169. device_id |= cx25840_read(client, 0x100);
  1170. v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);
  1171. /* The high byte of the device ID should be
  1172. * 0x83 for the cx2583x and 0x84 for the cx2584x */
  1173. if ((device_id & 0xff00) == 0x8300) {
  1174. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1175. }
  1176. else if ((device_id & 0xff00) == 0x8400) {
  1177. id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
  1178. } else if (device_id == 0x0000) {
  1179. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1180. } else if (device_id == 0x1313) {
  1181. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1182. }
  1183. else {
  1184. v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
  1185. return -ENODEV;
  1186. }
  1187. state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  1188. if (state == NULL) {
  1189. return -ENOMEM;
  1190. }
  1191. /* Note: revision '(device_id & 0x0f) == 2' was never built. The
  1192. marking skips from 0x1 == 22 to 0x3 == 23. */
  1193. v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
  1194. (device_id & 0xfff0) >> 4,
  1195. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : (device_id & 0x0f),
  1196. client->addr << 1, client->adapter->name);
  1197. i2c_set_clientdata(client, state);
  1198. state->c = client;
  1199. state->is_cx25836 = ((device_id & 0xff00) == 0x8300);
  1200. state->is_cx23885 = (device_id == 0x0000) || (device_id == 0x1313);
  1201. state->vid_input = CX25840_COMPOSITE7;
  1202. state->aud_input = CX25840_AUDIO8;
  1203. state->audclk_freq = 48000;
  1204. state->pvr150_workaround = 0;
  1205. state->audmode = V4L2_TUNER_MODE_LANG1;
  1206. state->unmute_volume = -1;
  1207. state->default_volume = 228 - cx25840_read(client, 0x8d4);
  1208. state->default_volume = ((state->default_volume / 2) + 23) << 9;
  1209. state->vbi_line_offset = 8;
  1210. state->id = id;
  1211. state->rev = device_id;
  1212. if (state->is_cx23885) {
  1213. /* Drive GPIO2 direction and values */
  1214. cx25840_write(client, 0x160, 0x1d);
  1215. cx25840_write(client, 0x164, 0x00);
  1216. }
  1217. return 0;
  1218. }
  1219. static int cx25840_remove(struct i2c_client *client)
  1220. {
  1221. kfree(i2c_get_clientdata(client));
  1222. return 0;
  1223. }
  1224. static const struct i2c_device_id cx25840_id[] = {
  1225. { "cx25840", 0 },
  1226. { }
  1227. };
  1228. MODULE_DEVICE_TABLE(i2c, cx25840_id);
  1229. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  1230. .name = "cx25840",
  1231. .driverid = I2C_DRIVERID_CX25840,
  1232. .command = cx25840_command,
  1233. .probe = cx25840_probe,
  1234. .remove = cx25840_remove,
  1235. .id_table = cx25840_id,
  1236. };