cx25840-core.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /* cx25840 - Conexant CX25840 audio/video decoder driver
  2. *
  3. * Copyright (C) 2004 Ulf Eklund
  4. *
  5. * Based on the saa7115 driver and on the first verison of Chris Kennedy's
  6. * cx25840 driver.
  7. *
  8. * Changes by Tyler Trafford <tatrafford@comcast.net>
  9. * - cleanup/rewrite for V4L2 API (2005)
  10. *
  11. * VBI support by Hans Verkuil <hverkuil@xs4all.nl>.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 2
  16. * of the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/slab.h>
  30. #include <linux/videodev2.h>
  31. #include <linux/i2c.h>
  32. #include <media/audiochip.h>
  33. #include <media/v4l2-common.h>
  34. #include "cx25840.h"
  35. MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
  36. MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
  37. MODULE_LICENSE("GPL");
  38. static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
  39. int debug = 0;
  40. module_param(debug, bool, 0644);
  41. MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
  42. I2C_CLIENT_INSMOD;
  43. /* ----------------------------------------------------------------------- */
  44. int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
  45. {
  46. u8 buffer[3];
  47. buffer[0] = addr >> 8;
  48. buffer[1] = addr & 0xff;
  49. buffer[2] = value;
  50. return i2c_master_send(client, buffer, 3);
  51. }
  52. int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
  53. {
  54. u8 buffer[6];
  55. buffer[0] = addr >> 8;
  56. buffer[1] = addr & 0xff;
  57. buffer[2] = value >> 24;
  58. buffer[3] = (value >> 16) & 0xff;
  59. buffer[4] = (value >> 8) & 0xff;
  60. buffer[5] = value & 0xff;
  61. return i2c_master_send(client, buffer, 6);
  62. }
  63. u8 cx25840_read(struct i2c_client * client, u16 addr)
  64. {
  65. u8 buffer[2];
  66. buffer[0] = addr >> 8;
  67. buffer[1] = addr & 0xff;
  68. if (i2c_master_send(client, buffer, 2) < 2)
  69. return 0;
  70. if (i2c_master_recv(client, buffer, 1) < 1)
  71. return 0;
  72. return buffer[0];
  73. }
  74. u32 cx25840_read4(struct i2c_client * client, u16 addr)
  75. {
  76. u8 buffer[4];
  77. buffer[0] = addr >> 8;
  78. buffer[1] = addr & 0xff;
  79. if (i2c_master_send(client, buffer, 2) < 2)
  80. return 0;
  81. if (i2c_master_recv(client, buffer, 4) < 4)
  82. return 0;
  83. return (buffer[0] << 24) | (buffer[1] << 16) |
  84. (buffer[2] << 8) | buffer[3];
  85. }
  86. int cx25840_and_or(struct i2c_client *client, u16 addr, u8 and_mask,
  87. u8 or_value)
  88. {
  89. return cx25840_write(client, addr,
  90. (cx25840_read(client, addr) & and_mask) |
  91. or_value);
  92. }
  93. /* ----------------------------------------------------------------------- */
  94. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  95. enum cx25840_audio_input aud_input);
  96. static void log_status(struct i2c_client *client);
  97. /* ----------------------------------------------------------------------- */
  98. static inline void init_dll1(struct i2c_client *client)
  99. {
  100. /* This is the Hauppauge sequence used to
  101. * initialize the Delay Lock Loop 1 (ADC DLL). */
  102. cx25840_write(client, 0x159, 0x23);
  103. cx25840_write(client, 0x15a, 0x87);
  104. cx25840_write(client, 0x15b, 0x06);
  105. cx25840_write(client, 0x159, 0xe1);
  106. cx25840_write(client, 0x15a, 0x86);
  107. cx25840_write(client, 0x159, 0xe0);
  108. cx25840_write(client, 0x159, 0xe1);
  109. cx25840_write(client, 0x15b, 0x10);
  110. }
  111. static inline void init_dll2(struct i2c_client *client)
  112. {
  113. /* This is the Hauppauge sequence used to
  114. * initialize the Delay Lock Loop 2 (ADC DLL). */
  115. cx25840_write(client, 0x15d, 0xe3);
  116. cx25840_write(client, 0x15e, 0x86);
  117. cx25840_write(client, 0x15f, 0x06);
  118. cx25840_write(client, 0x15d, 0xe1);
  119. cx25840_write(client, 0x15d, 0xe0);
  120. cx25840_write(client, 0x15d, 0xe1);
  121. }
  122. static void cx25840_initialize(struct i2c_client *client, int loadfw)
  123. {
  124. struct cx25840_state *state = i2c_get_clientdata(client);
  125. /* datasheet startup in numbered steps, refer to page 3-77 */
  126. /* 2. */
  127. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  128. /* The default of this register should be 4, but I get 0 instead.
  129. * Set this register to 4 manually. */
  130. cx25840_write(client, 0x000, 0x04);
  131. /* 3. */
  132. init_dll1(client);
  133. init_dll2(client);
  134. cx25840_write(client, 0x136, 0x0a);
  135. /* 4. */
  136. cx25840_write(client, 0x13c, 0x01);
  137. cx25840_write(client, 0x13c, 0x00);
  138. /* 5. */
  139. if (loadfw)
  140. cx25840_loadfw(client);
  141. /* 6. */
  142. cx25840_write(client, 0x115, 0x8c);
  143. cx25840_write(client, 0x116, 0x07);
  144. cx25840_write(client, 0x118, 0x02);
  145. /* 7. */
  146. cx25840_write(client, 0x4a5, 0x80);
  147. cx25840_write(client, 0x4a5, 0x00);
  148. cx25840_write(client, 0x402, 0x00);
  149. /* 8. */
  150. cx25840_write(client, 0x401, 0x18);
  151. cx25840_write(client, 0x4a2, 0x10);
  152. cx25840_write(client, 0x402, 0x04);
  153. /* 10. */
  154. cx25840_write(client, 0x8d3, 0x1f);
  155. cx25840_write(client, 0x8e3, 0x03);
  156. cx25840_vbi_setup(client);
  157. /* trial and error says these are needed to get audio */
  158. cx25840_write(client, 0x914, 0xa0);
  159. cx25840_write(client, 0x918, 0xa0);
  160. cx25840_write(client, 0x919, 0x01);
  161. /* stereo prefered */
  162. cx25840_write(client, 0x809, 0x04);
  163. /* AC97 shift */
  164. cx25840_write(client, 0x8cf, 0x0f);
  165. /* (re)set input */
  166. set_input(client, state->vid_input, state->aud_input);
  167. /* start microcontroller */
  168. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  169. }
  170. /* ----------------------------------------------------------------------- */
  171. static void input_change(struct i2c_client *client)
  172. {
  173. struct cx25840_state *state = i2c_get_clientdata(client);
  174. v4l2_std_id std = cx25840_get_v4lstd(client);
  175. /* Note: perhaps V4L2_STD_PAL_M should be handled as V4L2_STD_NTSC
  176. instead of V4L2_STD_PAL. Someone needs to test this. */
  177. if (std & V4L2_STD_PAL) {
  178. /* Follow tuner change procedure for PAL */
  179. cx25840_write(client, 0x808, 0xff);
  180. cx25840_write(client, 0x80b, 0x10);
  181. } else if (std & V4L2_STD_SECAM) {
  182. /* Select autodetect for SECAM */
  183. cx25840_write(client, 0x808, 0xff);
  184. cx25840_write(client, 0x80b, 0x10);
  185. } else if (std & V4L2_STD_NTSC) {
  186. /* NTSC */
  187. if (state->pvr150_workaround) {
  188. /* Certain Hauppauge PVR150 models have a hardware bug
  189. that causes audio to drop out. For these models the
  190. audio standard must be set explicitly.
  191. To be precise: it affects cards with tuner models
  192. 85, 99 and 112 (model numbers from tveeprom). */
  193. if (std == V4L2_STD_NTSC_M_JP) {
  194. /* Japan uses EIAJ audio standard */
  195. cx25840_write(client, 0x808, 0x2f);
  196. } else {
  197. /* Others use the BTSC audio standard */
  198. cx25840_write(client, 0x808, 0x1f);
  199. }
  200. /* South Korea uses the A2-M (aka Zweiton M) audio
  201. standard, and should set 0x808 to 0x3f, but I don't
  202. know how to detect this. */
  203. } else if (std == V4L2_STD_NTSC_M_JP) {
  204. /* Japan uses EIAJ audio standard */
  205. cx25840_write(client, 0x808, 0xf7);
  206. } else {
  207. /* Others use the BTSC audio standard */
  208. cx25840_write(client, 0x808, 0xf6);
  209. }
  210. /* South Korea uses the A2-M (aka Zweiton M) audio standard,
  211. and should set 0x808 to 0xf8, but I don't know how to
  212. detect this. */
  213. cx25840_write(client, 0x80b, 0x00);
  214. }
  215. if (cx25840_read(client, 0x803) & 0x10) {
  216. /* restart audio decoder microcontroller */
  217. cx25840_and_or(client, 0x803, ~0x10, 0x00);
  218. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  219. }
  220. }
  221. static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
  222. enum cx25840_audio_input aud_input)
  223. {
  224. struct cx25840_state *state = i2c_get_clientdata(client);
  225. u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
  226. vid_input <= CX25840_COMPOSITE8);
  227. u8 reg;
  228. v4l_dbg(1, client, "decoder set video input %d, audio input %d\n",
  229. vid_input, aud_input);
  230. if (is_composite) {
  231. reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
  232. } else {
  233. int luma = vid_input & 0xf0;
  234. int chroma = vid_input & 0xf00;
  235. if ((vid_input & ~0xff0) ||
  236. luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
  237. chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
  238. v4l_err(client, "0x%04x is not a valid video input!\n", vid_input);
  239. return -EINVAL;
  240. }
  241. reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
  242. if (chroma >= CX25840_SVIDEO_CHROMA7) {
  243. reg &= 0x3f;
  244. reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
  245. } else {
  246. reg &= 0xcf;
  247. reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
  248. }
  249. }
  250. switch (aud_input) {
  251. case CX25840_AUDIO_SERIAL:
  252. /* do nothing, use serial audio input */
  253. break;
  254. case CX25840_AUDIO4: reg &= ~0x30; break;
  255. case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
  256. case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
  257. case CX25840_AUDIO7: reg &= ~0xc0; break;
  258. case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
  259. default:
  260. v4l_err(client, "0x%04x is not a valid audio input!\n", aud_input);
  261. return -EINVAL;
  262. }
  263. cx25840_write(client, 0x103, reg);
  264. /* Set INPUT_MODE to Composite (0) or S-Video (1) */
  265. cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
  266. /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
  267. cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
  268. /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
  269. if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
  270. cx25840_and_or(client, 0x102, ~0x4, 4);
  271. else
  272. cx25840_and_or(client, 0x102, ~0x4, 0);
  273. state->vid_input = vid_input;
  274. state->aud_input = aud_input;
  275. cx25840_audio_set_path(client);
  276. input_change(client);
  277. return 0;
  278. }
  279. /* ----------------------------------------------------------------------- */
  280. static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
  281. {
  282. u8 fmt=0; /* zero is autodetect */
  283. /* First tests should be against specific std */
  284. if (std & V4L2_STD_NTSC_M_JP) {
  285. fmt=0x2;
  286. } else if (std & V4L2_STD_NTSC_443) {
  287. fmt=0x3;
  288. } else if (std & V4L2_STD_PAL_M) {
  289. fmt=0x5;
  290. } else if (std & V4L2_STD_PAL_N) {
  291. fmt=0x6;
  292. } else if (std & V4L2_STD_PAL_Nc) {
  293. fmt=0x7;
  294. } else if (std & V4L2_STD_PAL_60) {
  295. fmt=0x8;
  296. } else {
  297. /* Then, test against generic ones */
  298. if (std & V4L2_STD_NTSC) {
  299. fmt=0x1;
  300. } else if (std & V4L2_STD_PAL) {
  301. fmt=0x4;
  302. } else if (std & V4L2_STD_SECAM) {
  303. fmt=0xc;
  304. }
  305. }
  306. cx25840_and_or(client, 0x400, ~0xf, fmt);
  307. cx25840_vbi_setup(client);
  308. return 0;
  309. }
  310. v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
  311. {
  312. /* check VID_FMT_SEL first */
  313. u8 fmt = cx25840_read(client, 0x400) & 0xf;
  314. if (!fmt) {
  315. /* check AFD_FMT_STAT if set to autodetect */
  316. fmt = cx25840_read(client, 0x40d) & 0xf;
  317. }
  318. switch (fmt) {
  319. case 0x1: return V4L2_STD_NTSC_M;
  320. case 0x2: return V4L2_STD_NTSC_M_JP;
  321. case 0x3: return V4L2_STD_NTSC_443;
  322. case 0x4: return V4L2_STD_PAL;
  323. case 0x5: return V4L2_STD_PAL_M;
  324. case 0x6: return V4L2_STD_PAL_N;
  325. case 0x7: return V4L2_STD_PAL_Nc;
  326. case 0x8: return V4L2_STD_PAL_60;
  327. case 0xc: return V4L2_STD_SECAM;
  328. default: return V4L2_STD_UNKNOWN;
  329. }
  330. }
  331. /* ----------------------------------------------------------------------- */
  332. static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  333. {
  334. struct cx25840_state *state = i2c_get_clientdata(client);
  335. switch (ctrl->id) {
  336. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  337. state->pvr150_workaround = ctrl->value;
  338. set_input(client, state->vid_input, state->aud_input);
  339. break;
  340. case V4L2_CID_BRIGHTNESS:
  341. if (ctrl->value < 0 || ctrl->value > 255) {
  342. v4l_err(client, "invalid brightness setting %d\n",
  343. ctrl->value);
  344. return -ERANGE;
  345. }
  346. cx25840_write(client, 0x414, ctrl->value - 128);
  347. break;
  348. case V4L2_CID_CONTRAST:
  349. if (ctrl->value < 0 || ctrl->value > 127) {
  350. v4l_err(client, "invalid contrast setting %d\n",
  351. ctrl->value);
  352. return -ERANGE;
  353. }
  354. cx25840_write(client, 0x415, ctrl->value << 1);
  355. break;
  356. case V4L2_CID_SATURATION:
  357. if (ctrl->value < 0 || ctrl->value > 127) {
  358. v4l_err(client, "invalid saturation setting %d\n",
  359. ctrl->value);
  360. return -ERANGE;
  361. }
  362. cx25840_write(client, 0x420, ctrl->value << 1);
  363. cx25840_write(client, 0x421, ctrl->value << 1);
  364. break;
  365. case V4L2_CID_HUE:
  366. if (ctrl->value < -127 || ctrl->value > 127) {
  367. v4l_err(client, "invalid hue setting %d\n", ctrl->value);
  368. return -ERANGE;
  369. }
  370. cx25840_write(client, 0x422, ctrl->value);
  371. break;
  372. case V4L2_CID_AUDIO_VOLUME:
  373. case V4L2_CID_AUDIO_BASS:
  374. case V4L2_CID_AUDIO_TREBLE:
  375. case V4L2_CID_AUDIO_BALANCE:
  376. case V4L2_CID_AUDIO_MUTE:
  377. return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
  378. default:
  379. return -EINVAL;
  380. }
  381. return 0;
  382. }
  383. static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  384. {
  385. struct cx25840_state *state = i2c_get_clientdata(client);
  386. switch (ctrl->id) {
  387. case CX25840_CID_ENABLE_PVR150_WORKAROUND:
  388. ctrl->value = state->pvr150_workaround;
  389. break;
  390. case V4L2_CID_BRIGHTNESS:
  391. ctrl->value = cx25840_read(client, 0x414) + 128;
  392. break;
  393. case V4L2_CID_CONTRAST:
  394. ctrl->value = cx25840_read(client, 0x415) >> 1;
  395. break;
  396. case V4L2_CID_SATURATION:
  397. ctrl->value = cx25840_read(client, 0x420) >> 1;
  398. break;
  399. case V4L2_CID_HUE:
  400. ctrl->value = cx25840_read(client, 0x422);
  401. break;
  402. case V4L2_CID_AUDIO_VOLUME:
  403. case V4L2_CID_AUDIO_BASS:
  404. case V4L2_CID_AUDIO_TREBLE:
  405. case V4L2_CID_AUDIO_BALANCE:
  406. case V4L2_CID_AUDIO_MUTE:
  407. return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
  408. default:
  409. return -EINVAL;
  410. }
  411. return 0;
  412. }
  413. /* ----------------------------------------------------------------------- */
  414. static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  415. {
  416. switch (fmt->type) {
  417. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  418. return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
  419. default:
  420. return -EINVAL;
  421. }
  422. return 0;
  423. }
  424. static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
  425. {
  426. struct v4l2_pix_format *pix;
  427. int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
  428. int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
  429. switch (fmt->type) {
  430. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  431. pix = &(fmt->fmt.pix);
  432. Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
  433. Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
  434. Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
  435. Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
  436. Vlines = pix->height + (is_pal ? 4 : 7);
  437. if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
  438. (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
  439. v4l_err(client, "%dx%d is not a valid size!\n",
  440. pix->width, pix->height);
  441. return -ERANGE;
  442. }
  443. HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
  444. VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
  445. VSC &= 0x1fff;
  446. if (pix->width >= 385)
  447. filter = 0;
  448. else if (pix->width > 192)
  449. filter = 1;
  450. else if (pix->width > 96)
  451. filter = 2;
  452. else
  453. filter = 3;
  454. v4l_dbg(1, client, "decoder set size %dx%d -> scale %ux%u\n",
  455. pix->width, pix->height, HSC, VSC);
  456. /* HSCALE=HSC */
  457. cx25840_write(client, 0x418, HSC & 0xff);
  458. cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
  459. cx25840_write(client, 0x41a, HSC >> 16);
  460. /* VSCALE=VSC */
  461. cx25840_write(client, 0x41c, VSC & 0xff);
  462. cx25840_write(client, 0x41d, VSC >> 8);
  463. /* VS_INTRLACE=1 VFILT=filter */
  464. cx25840_write(client, 0x41e, 0x8 | filter);
  465. break;
  466. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  467. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  468. case V4L2_BUF_TYPE_VBI_CAPTURE:
  469. return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
  470. default:
  471. return -EINVAL;
  472. }
  473. return 0;
  474. }
  475. /* ----------------------------------------------------------------------- */
  476. static int cx25840_command(struct i2c_client *client, unsigned int cmd,
  477. void *arg)
  478. {
  479. struct cx25840_state *state = i2c_get_clientdata(client);
  480. struct v4l2_tuner *vt = arg;
  481. switch (cmd) {
  482. #ifdef CONFIG_VIDEO_ADV_DEBUG
  483. /* ioctls to allow direct access to the
  484. * cx25840 registers for testing */
  485. case VIDIOC_INT_G_REGISTER:
  486. {
  487. struct v4l2_register *reg = arg;
  488. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  489. return -EINVAL;
  490. reg->val = cx25840_read(client, reg->reg & 0x0fff);
  491. break;
  492. }
  493. case VIDIOC_INT_S_REGISTER:
  494. {
  495. struct v4l2_register *reg = arg;
  496. if (reg->i2c_id != I2C_DRIVERID_CX25840)
  497. return -EINVAL;
  498. if (!capable(CAP_SYS_ADMIN))
  499. return -EPERM;
  500. cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
  501. break;
  502. }
  503. #endif
  504. case VIDIOC_INT_DECODE_VBI_LINE:
  505. return cx25840_vbi(client, cmd, arg);
  506. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  507. return cx25840_audio(client, cmd, arg);
  508. case VIDIOC_STREAMON:
  509. v4l_dbg(1, client, "enable output\n");
  510. cx25840_write(client, 0x115, 0x8c);
  511. cx25840_write(client, 0x116, 0x07);
  512. break;
  513. case VIDIOC_STREAMOFF:
  514. v4l_dbg(1, client, "disable output\n");
  515. cx25840_write(client, 0x115, 0x00);
  516. cx25840_write(client, 0x116, 0x00);
  517. break;
  518. case VIDIOC_LOG_STATUS:
  519. log_status(client);
  520. break;
  521. case VIDIOC_G_CTRL:
  522. return get_v4lctrl(client, (struct v4l2_control *)arg);
  523. case VIDIOC_S_CTRL:
  524. return set_v4lctrl(client, (struct v4l2_control *)arg);
  525. case VIDIOC_G_STD:
  526. *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
  527. break;
  528. case VIDIOC_S_STD:
  529. state->radio = 0;
  530. return set_v4lstd(client, *(v4l2_std_id *)arg);
  531. case AUDC_SET_RADIO:
  532. state->radio = 1;
  533. break;
  534. case VIDIOC_G_INPUT:
  535. *(int *)arg = state->vid_input;
  536. break;
  537. case VIDIOC_S_INPUT:
  538. return set_input(client, *(enum cx25840_video_input *)arg, state->aud_input);
  539. case VIDIOC_S_AUDIO:
  540. {
  541. struct v4l2_audio *input = arg;
  542. return set_input(client, state->vid_input, input->index);
  543. }
  544. case VIDIOC_G_AUDIO:
  545. {
  546. struct v4l2_audio *input = arg;
  547. memset(input, 0, sizeof(*input));
  548. input->index = state->aud_input;
  549. break;
  550. }
  551. case VIDIOC_S_FREQUENCY:
  552. input_change(client);
  553. break;
  554. case VIDIOC_G_TUNER:
  555. {
  556. u8 mode = cx25840_read(client, 0x804);
  557. u8 pref = cx25840_read(client, 0x809) & 0xf;
  558. u8 vpres = cx25840_read(client, 0x80a) & 0x10;
  559. int val = 0;
  560. if (state->radio)
  561. break;
  562. vt->capability |=
  563. V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
  564. V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
  565. vt->signal = vpres ? 0xffff : 0x0;
  566. /* get rxsubchans and audmode */
  567. if ((mode & 0xf) == 1)
  568. val |= V4L2_TUNER_SUB_STEREO;
  569. else
  570. val |= V4L2_TUNER_SUB_MONO;
  571. if (mode == 2 || mode == 4)
  572. val |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  573. if (mode & 0x10)
  574. val |= V4L2_TUNER_SUB_SAP;
  575. vt->rxsubchans = val;
  576. switch (pref) {
  577. case 0:
  578. vt->audmode = V4L2_TUNER_MODE_MONO;
  579. break;
  580. case 1:
  581. case 2:
  582. vt->audmode = V4L2_TUNER_MODE_LANG2;
  583. break;
  584. case 4:
  585. default:
  586. vt->audmode = V4L2_TUNER_MODE_STEREO;
  587. }
  588. break;
  589. }
  590. case VIDIOC_S_TUNER:
  591. switch (vt->audmode) {
  592. case V4L2_TUNER_MODE_MONO:
  593. case V4L2_TUNER_MODE_LANG1:
  594. /* Force PREF_MODE to MONO */
  595. cx25840_and_or(client, 0x809, ~0xf, 0x00);
  596. break;
  597. case V4L2_TUNER_MODE_STEREO:
  598. /* Force PREF_MODE to STEREO */
  599. cx25840_and_or(client, 0x809, ~0xf, 0x04);
  600. break;
  601. case V4L2_TUNER_MODE_LANG2:
  602. /* Force PREF_MODE to LANG2 */
  603. cx25840_and_or(client, 0x809, ~0xf, 0x01);
  604. break;
  605. }
  606. break;
  607. case VIDIOC_G_FMT:
  608. return get_v4lfmt(client, (struct v4l2_format *)arg);
  609. case VIDIOC_S_FMT:
  610. return set_v4lfmt(client, (struct v4l2_format *)arg);
  611. case VIDIOC_INT_RESET:
  612. cx25840_initialize(client, 0);
  613. break;
  614. case VIDIOC_INT_G_CHIP_IDENT:
  615. *(enum v4l2_chip_ident *)arg =
  616. V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf);
  617. break;
  618. default:
  619. return -EINVAL;
  620. }
  621. return 0;
  622. }
  623. /* ----------------------------------------------------------------------- */
  624. static struct i2c_driver i2c_driver_cx25840;
  625. static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
  626. int kind)
  627. {
  628. struct i2c_client *client;
  629. struct cx25840_state *state;
  630. u16 device_id;
  631. /* Check if the adapter supports the needed features
  632. * Not until kernel version 2.6.11 did the bit-algo
  633. * correctly report that it would do an I2C-level xfer */
  634. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  635. return 0;
  636. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  637. if (client == 0)
  638. return -ENOMEM;
  639. memset(client, 0, sizeof(struct i2c_client));
  640. client->addr = address;
  641. client->adapter = adapter;
  642. client->driver = &i2c_driver_cx25840;
  643. snprintf(client->name, sizeof(client->name) - 1, "cx25840");
  644. v4l_dbg(1, client, "detecting cx25840 client on address 0x%x\n", address << 1);
  645. device_id = cx25840_read(client, 0x101) << 8;
  646. device_id |= cx25840_read(client, 0x100);
  647. /* The high byte of the device ID should be
  648. * 0x84 if chip is present */
  649. if ((device_id & 0xff00) != 0x8400) {
  650. v4l_dbg(1, client, "cx25840 not found\n");
  651. kfree(client);
  652. return 0;
  653. }
  654. v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
  655. (device_id & 0xfff0) >> 4,
  656. (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
  657. address << 1, adapter->name);
  658. state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
  659. if (state == NULL) {
  660. kfree(client);
  661. return -ENOMEM;
  662. }
  663. i2c_set_clientdata(client, state);
  664. memset(state, 0, sizeof(struct cx25840_state));
  665. state->vid_input = CX25840_COMPOSITE7;
  666. state->aud_input = CX25840_AUDIO8;
  667. state->audclk_freq = 48000;
  668. state->pvr150_workaround = 0;
  669. cx25840_initialize(client, 1);
  670. i2c_attach_client(client);
  671. return 0;
  672. }
  673. static int cx25840_attach_adapter(struct i2c_adapter *adapter)
  674. {
  675. if (adapter->class & I2C_CLASS_TV_ANALOG)
  676. return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
  677. return 0;
  678. }
  679. static int cx25840_detach_client(struct i2c_client *client)
  680. {
  681. struct cx25840_state *state = i2c_get_clientdata(client);
  682. int err;
  683. err = i2c_detach_client(client);
  684. if (err) {
  685. return err;
  686. }
  687. kfree(state);
  688. kfree(client);
  689. return 0;
  690. }
  691. /* ----------------------------------------------------------------------- */
  692. static struct i2c_driver i2c_driver_cx25840 = {
  693. .driver = {
  694. .name = "cx25840",
  695. },
  696. .id = I2C_DRIVERID_CX25840,
  697. .attach_adapter = cx25840_attach_adapter,
  698. .detach_client = cx25840_detach_client,
  699. .command = cx25840_command,
  700. };
  701. static int __init m__init(void)
  702. {
  703. return i2c_add_driver(&i2c_driver_cx25840);
  704. }
  705. static void __exit m__exit(void)
  706. {
  707. i2c_del_driver(&i2c_driver_cx25840);
  708. }
  709. module_init(m__init);
  710. module_exit(m__exit);
  711. /* ----------------------------------------------------------------------- */
  712. static void log_status(struct i2c_client *client)
  713. {
  714. static const char *const fmt_strs[] = {
  715. "0x0",
  716. "NTSC-M", "NTSC-J", "NTSC-4.43",
  717. "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
  718. "0x9", "0xA", "0xB",
  719. "SECAM",
  720. "0xD", "0xE", "0xF"
  721. };
  722. struct cx25840_state *state = i2c_get_clientdata(client);
  723. u8 microctrl_vidfmt = cx25840_read(client, 0x80a);
  724. u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
  725. u8 gen_stat1 = cx25840_read(client, 0x40d);
  726. u8 download_ctl = cx25840_read(client, 0x803);
  727. u8 mod_det_stat0 = cx25840_read(client, 0x804);
  728. u8 mod_det_stat1 = cx25840_read(client, 0x805);
  729. u8 audio_config = cx25840_read(client, 0x808);
  730. u8 pref_mode = cx25840_read(client, 0x809);
  731. u8 afc0 = cx25840_read(client, 0x80b);
  732. u8 mute_ctl = cx25840_read(client, 0x8d3);
  733. int vid_input = state->vid_input;
  734. int aud_input = state->aud_input;
  735. char *p;
  736. v4l_info(client, "Video signal: %spresent\n",
  737. (microctrl_vidfmt & 0x10) ? "" : "not ");
  738. v4l_info(client, "Detected format: %s\n",
  739. fmt_strs[gen_stat1 & 0xf]);
  740. switch (mod_det_stat0) {
  741. case 0x00: p = "mono"; break;
  742. case 0x01: p = "stereo"; break;
  743. case 0x02: p = "dual"; break;
  744. case 0x04: p = "tri"; break;
  745. case 0x10: p = "mono with SAP"; break;
  746. case 0x11: p = "stereo with SAP"; break;
  747. case 0x12: p = "dual with SAP"; break;
  748. case 0x14: p = "tri with SAP"; break;
  749. case 0xfe: p = "forced mode"; break;
  750. default: p = "not defined";
  751. }
  752. v4l_info(client, "Detected audio mode: %s\n", p);
  753. switch (mod_det_stat1) {
  754. case 0x00: p = "not defined"; break;
  755. case 0x01: p = "EIAJ"; break;
  756. case 0x02: p = "A2-M"; break;
  757. case 0x03: p = "A2-BG"; break;
  758. case 0x04: p = "A2-DK1"; break;
  759. case 0x05: p = "A2-DK2"; break;
  760. case 0x06: p = "A2-DK3"; break;
  761. case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
  762. case 0x08: p = "AM-L"; break;
  763. case 0x09: p = "NICAM-BG"; break;
  764. case 0x0a: p = "NICAM-DK"; break;
  765. case 0x0b: p = "NICAM-I"; break;
  766. case 0x0c: p = "NICAM-L"; break;
  767. case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
  768. case 0x0e: p = "IF FM Radio"; break;
  769. case 0x0f: p = "BTSC"; break;
  770. case 0x10: p = "high-deviation FM"; break;
  771. case 0x11: p = "very high-deviation FM"; break;
  772. case 0xfd: p = "unknown audio standard"; break;
  773. case 0xfe: p = "forced audio standard"; break;
  774. case 0xff: p = "no detected audio standard"; break;
  775. default: p = "not defined";
  776. }
  777. v4l_info(client, "Detected audio standard: %s\n", p);
  778. v4l_info(client, "Audio muted: %s\n",
  779. (mute_ctl & 0x2) ? "yes" : "no");
  780. v4l_info(client, "Audio microcontroller: %s\n",
  781. (download_ctl & 0x10) ? "running" : "stopped");
  782. switch (audio_config >> 4) {
  783. case 0x00: p = "undefined"; break;
  784. case 0x01: p = "BTSC"; break;
  785. case 0x02: p = "EIAJ"; break;
  786. case 0x03: p = "A2-M"; break;
  787. case 0x04: p = "A2-BG"; break;
  788. case 0x05: p = "A2-DK1"; break;
  789. case 0x06: p = "A2-DK2"; break;
  790. case 0x07: p = "A2-DK3"; break;
  791. case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
  792. case 0x09: p = "AM-L"; break;
  793. case 0x0a: p = "NICAM-BG"; break;
  794. case 0x0b: p = "NICAM-DK"; break;
  795. case 0x0c: p = "NICAM-I"; break;
  796. case 0x0d: p = "NICAM-L"; break;
  797. case 0x0e: p = "FM radio"; break;
  798. case 0x0f: p = "automatic detection"; break;
  799. default: p = "undefined";
  800. }
  801. v4l_info(client, "Configured audio standard: %s\n", p);
  802. if ((audio_config >> 4) < 0xF) {
  803. switch (audio_config & 0xF) {
  804. case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
  805. case 0x01: p = "MONO2 (LANGUAGE B)"; break;
  806. case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
  807. case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
  808. case 0x04: p = "STEREO"; break;
  809. case 0x05: p = "DUAL1 (AB)"; break;
  810. case 0x06: p = "DUAL2 (AC) (FM)"; break;
  811. case 0x07: p = "DUAL3 (BC) (FM)"; break;
  812. case 0x08: p = "DUAL4 (AC) (AM)"; break;
  813. case 0x09: p = "DUAL5 (BC) (AM)"; break;
  814. case 0x0a: p = "SAP"; break;
  815. default: p = "undefined";
  816. }
  817. v4l_info(client, "Configured audio mode: %s\n", p);
  818. } else {
  819. switch (audio_config & 0xF) {
  820. case 0x00: p = "BG"; break;
  821. case 0x01: p = "DK1"; break;
  822. case 0x02: p = "DK2"; break;
  823. case 0x03: p = "DK3"; break;
  824. case 0x04: p = "I"; break;
  825. case 0x05: p = "L"; break;
  826. case 0x06: p = "BTSC"; break;
  827. case 0x07: p = "EIAJ"; break;
  828. case 0x08: p = "A2-M"; break;
  829. case 0x09: p = "FM Radio"; break;
  830. case 0x0f: p = "automatic standard and mode detection"; break;
  831. default: p = "undefined";
  832. }
  833. v4l_info(client, "Configured audio system: %s\n", p);
  834. }
  835. v4l_info(client, "Specified standard: %s\n",
  836. vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
  837. if (vid_input >= CX25840_COMPOSITE1 &&
  838. vid_input <= CX25840_COMPOSITE8) {
  839. v4l_info(client, "Specified video input: Composite %d\n",
  840. vid_input - CX25840_COMPOSITE1 + 1);
  841. } else {
  842. v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
  843. (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
  844. }
  845. if (aud_input) {
  846. v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
  847. } else {
  848. v4l_info(client, "Specified audio input: External\n");
  849. }
  850. v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
  851. switch (pref_mode & 0xf) {
  852. case 0: p = "mono/language A"; break;
  853. case 1: p = "language B"; break;
  854. case 2: p = "language C"; break;
  855. case 3: p = "analog fallback"; break;
  856. case 4: p = "stereo"; break;
  857. case 5: p = "language AC"; break;
  858. case 6: p = "language BC"; break;
  859. case 7: p = "language AB"; break;
  860. default: p = "undefined";
  861. }
  862. v4l_info(client, "Preferred audio mode: %s\n", p);
  863. if ((audio_config & 0xf) == 0xf) {
  864. switch ((afc0 >> 3) & 0x3) {
  865. case 0: p = "system DK"; break;
  866. case 1: p = "system L"; break;
  867. case 2: p = "autodetect"; break;
  868. default: p = "undefined";
  869. }
  870. v4l_info(client, "Selected 65 MHz format: %s\n", p);
  871. switch (afc0 & 0x7) {
  872. case 0: p = "chroma"; break;
  873. case 1: p = "BTSC"; break;
  874. case 2: p = "EIAJ"; break;
  875. case 3: p = "A2-M"; break;
  876. case 4: p = "autodetect"; break;
  877. default: p = "undefined";
  878. }
  879. v4l_info(client, "Selected 45 MHz format: %s\n", p);
  880. }
  881. }