cx25840-core.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  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. /* ignore this command */
  1183. if (cmd == TUNER_SET_TYPE_ADDR || cmd == TUNER_SET_CONFIG)
  1184. return 0;
  1185. /* Old-style drivers rely on initialization on first use, so
  1186. call the init whenever a command is issued to this driver.
  1187. New-style drivers using v4l2_subdev should call init explicitly. */
  1188. cx25840_init(i2c_get_clientdata(client), 0);
  1189. return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
  1190. }
  1191. /* ----------------------------------------------------------------------- */
  1192. static const struct v4l2_subdev_core_ops cx25840_core_ops = {
  1193. .log_status = cx25840_log_status,
  1194. .g_chip_ident = cx25840_g_chip_ident,
  1195. .g_ctrl = cx25840_g_ctrl,
  1196. .s_ctrl = cx25840_s_ctrl,
  1197. .queryctrl = cx25840_queryctrl,
  1198. .reset = cx25840_reset,
  1199. .init = cx25840_init,
  1200. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1201. .g_register = cx25840_g_register,
  1202. .s_register = cx25840_s_register,
  1203. #endif
  1204. };
  1205. static const struct v4l2_subdev_tuner_ops cx25840_tuner_ops = {
  1206. .s_frequency = cx25840_s_frequency,
  1207. .s_std = cx25840_s_std,
  1208. .s_radio = cx25840_s_radio,
  1209. .g_tuner = cx25840_g_tuner,
  1210. .s_tuner = cx25840_s_tuner,
  1211. };
  1212. static const struct v4l2_subdev_audio_ops cx25840_audio_ops = {
  1213. .s_clock_freq = cx25840_s_clock_freq,
  1214. .s_routing = cx25840_s_audio_routing,
  1215. };
  1216. static const struct v4l2_subdev_video_ops cx25840_video_ops = {
  1217. .s_routing = cx25840_s_video_routing,
  1218. .g_fmt = cx25840_g_fmt,
  1219. .s_fmt = cx25840_s_fmt,
  1220. .decode_vbi_line = cx25840_decode_vbi_line,
  1221. .s_stream = cx25840_s_stream,
  1222. };
  1223. static const struct v4l2_subdev_ops cx25840_ops = {
  1224. .core = &cx25840_core_ops,
  1225. .tuner = &cx25840_tuner_ops,
  1226. .audio = &cx25840_audio_ops,
  1227. .video = &cx25840_video_ops,
  1228. };
  1229. /* ----------------------------------------------------------------------- */
  1230. static int cx25840_probe(struct i2c_client *client,
  1231. const struct i2c_device_id *did)
  1232. {
  1233. struct cx25840_state *state;
  1234. struct v4l2_subdev *sd;
  1235. u32 id;
  1236. u16 device_id;
  1237. /* Check if the adapter supports the needed features */
  1238. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1239. return -EIO;
  1240. v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);
  1241. device_id = cx25840_read(client, 0x101) << 8;
  1242. device_id |= cx25840_read(client, 0x100);
  1243. v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);
  1244. /* The high byte of the device ID should be
  1245. * 0x83 for the cx2583x and 0x84 for the cx2584x */
  1246. if ((device_id & 0xff00) == 0x8300) {
  1247. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1248. }
  1249. else if ((device_id & 0xff00) == 0x8400) {
  1250. id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
  1251. } else if (device_id == 0x0000) {
  1252. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1253. } else if (device_id == 0x1313) {
  1254. id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
  1255. }
  1256. else {
  1257. v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
  1258. return -ENODEV;
  1259. }
  1260. state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  1261. if (state == NULL)
  1262. return -ENOMEM;
  1263. sd = &state->sd;
  1264. v4l2_i2c_subdev_init(sd, client, &cx25840_ops);
  1265. /* Note: revision '(device_id & 0x0f) == 2' was never built. The
  1266. marking skips from 0x1 == 22 to 0x3 == 23. */
  1267. v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
  1268. (device_id & 0xfff0) >> 4,
  1269. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : (device_id & 0x0f),
  1270. client->addr << 1, client->adapter->name);
  1271. state->c = client;
  1272. state->is_cx25836 = ((device_id & 0xff00) == 0x8300);
  1273. state->is_cx23885 = (device_id == 0x0000) || (device_id == 0x1313);
  1274. state->vid_input = CX25840_COMPOSITE7;
  1275. state->aud_input = CX25840_AUDIO8;
  1276. state->audclk_freq = 48000;
  1277. state->pvr150_workaround = 0;
  1278. state->audmode = V4L2_TUNER_MODE_LANG1;
  1279. state->unmute_volume = -1;
  1280. state->default_volume = 228 - cx25840_read(client, 0x8d4);
  1281. state->default_volume = ((state->default_volume / 2) + 23) << 9;
  1282. state->vbi_line_offset = 8;
  1283. state->id = id;
  1284. state->rev = device_id;
  1285. if (state->is_cx23885) {
  1286. /* Drive GPIO2 direction and values */
  1287. cx25840_write(client, 0x160, 0x1d);
  1288. cx25840_write(client, 0x164, 0x00);
  1289. }
  1290. return 0;
  1291. }
  1292. static int cx25840_remove(struct i2c_client *client)
  1293. {
  1294. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1295. v4l2_device_unregister_subdev(sd);
  1296. kfree(to_state(sd));
  1297. return 0;
  1298. }
  1299. static const struct i2c_device_id cx25840_id[] = {
  1300. { "cx25840", 0 },
  1301. { }
  1302. };
  1303. MODULE_DEVICE_TABLE(i2c, cx25840_id);
  1304. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  1305. .name = "cx25840",
  1306. .driverid = I2C_DRIVERID_CX25840,
  1307. .command = cx25840_command,
  1308. .probe = cx25840_probe,
  1309. .remove = cx25840_remove,
  1310. .id_table = cx25840_id,
  1311. };