budget-av.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /*
  2. * budget-av.c: driver for the SAA7146 based Budget DVB cards
  3. * with analog video in
  4. *
  5. * Compiled from various sources by Michael Hunold <michael@mihu.de>
  6. *
  7. * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
  8. * Andrew de Quincey <adq_dvb@lidskialf.net>
  9. *
  10. * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
  11. *
  12. * Copyright (C) 1999-2002 Ralph Metzler
  13. * & Marcus Metzler for convergence integrated media GmbH
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version 2
  18. * of the License, or (at your option) any later version.
  19. *
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  30. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  31. *
  32. *
  33. * the project's page is at http://www.linuxtv.org/dvb/
  34. */
  35. #include "budget.h"
  36. #include "stv0299.h"
  37. #include "tda10021.h"
  38. #include "tda1004x.h"
  39. #include <media/saa7146_vv.h>
  40. #include <linux/module.h>
  41. #include <linux/errno.h>
  42. #include <linux/slab.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/input.h>
  45. #include <linux/spinlock.h>
  46. #include "dvb_ca_en50221.h"
  47. #define DEBICICAM 0x02420000
  48. struct budget_av {
  49. struct budget budget;
  50. struct video_device *vd;
  51. int cur_input;
  52. int has_saa7113;
  53. struct tasklet_struct ciintf_irq_tasklet;
  54. int slot_status;
  55. struct dvb_ca_en50221 ca;
  56. };
  57. /* GPIO CI Connections:
  58. * 0 - Vcc/Reset (Reset is controlled by capacitor)
  59. * 1 - Attribute Memory
  60. * 2 - Card Enable (Active Low)
  61. * 3 - Card Detect
  62. */
  63. /****************************************************************************
  64. * INITIALIZATION
  65. ****************************************************************************/
  66. static u8 i2c_readreg(struct i2c_adapter *i2c, u8 id, u8 reg)
  67. {
  68. u8 mm1[] = { 0x00 };
  69. u8 mm2[] = { 0x00 };
  70. struct i2c_msg msgs[2];
  71. msgs[0].flags = 0;
  72. msgs[1].flags = I2C_M_RD;
  73. msgs[0].addr = msgs[1].addr = id / 2;
  74. mm1[0] = reg;
  75. msgs[0].len = 1;
  76. msgs[1].len = 1;
  77. msgs[0].buf = mm1;
  78. msgs[1].buf = mm2;
  79. i2c_transfer(i2c, msgs, 2);
  80. return mm2[0];
  81. }
  82. static int i2c_readregs(struct i2c_adapter *i2c, u8 id, u8 reg, u8 * buf, u8 len)
  83. {
  84. u8 mm1[] = { reg };
  85. struct i2c_msg msgs[2] = {
  86. {.addr = id / 2,.flags = 0,.buf = mm1,.len = 1},
  87. {.addr = id / 2,.flags = I2C_M_RD,.buf = buf,.len = len}
  88. };
  89. if (i2c_transfer(i2c, msgs, 2) != 2)
  90. return -EIO;
  91. return 0;
  92. }
  93. static int i2c_writereg(struct i2c_adapter *i2c, u8 id, u8 reg, u8 val)
  94. {
  95. u8 msg[2] = { reg, val };
  96. struct i2c_msg msgs;
  97. msgs.flags = 0;
  98. msgs.addr = id / 2;
  99. msgs.len = 2;
  100. msgs.buf = msg;
  101. return i2c_transfer(i2c, &msgs, 1);
  102. }
  103. static int ciintf_read_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address)
  104. {
  105. struct budget_av *budget_av = (struct budget_av *) ca->data;
  106. int result;
  107. if (slot != 0)
  108. return -EINVAL;
  109. saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
  110. udelay(1);
  111. result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 0xfff, 1, 0, 0);
  112. if (result == -ETIMEDOUT)
  113. budget_av->slot_status = 0;
  114. return result;
  115. }
  116. static int ciintf_write_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address, u8 value)
  117. {
  118. struct budget_av *budget_av = (struct budget_av *) ca->data;
  119. int result;
  120. if (slot != 0)
  121. return -EINVAL;
  122. saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
  123. udelay(1);
  124. result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 0xfff, 1, value, 0, 0);
  125. if (result == -ETIMEDOUT)
  126. budget_av->slot_status = 0;
  127. return result;
  128. }
  129. static int ciintf_read_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address)
  130. {
  131. struct budget_av *budget_av = (struct budget_av *) ca->data;
  132. int result;
  133. if (slot != 0)
  134. return -EINVAL;
  135. saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
  136. udelay(1);
  137. result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 3, 1, 0, 0);
  138. if (result == -ETIMEDOUT)
  139. budget_av->slot_status = 0;
  140. return result;
  141. }
  142. static int ciintf_write_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address, u8 value)
  143. {
  144. struct budget_av *budget_av = (struct budget_av *) ca->data;
  145. int result;
  146. if (slot != 0)
  147. return -EINVAL;
  148. saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
  149. udelay(1);
  150. result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 3, 1, value, 0, 0);
  151. if (result == -ETIMEDOUT)
  152. budget_av->slot_status = 0;
  153. return result;
  154. }
  155. static int ciintf_slot_reset(struct dvb_ca_en50221 *ca, int slot)
  156. {
  157. struct budget_av *budget_av = (struct budget_av *) ca->data;
  158. struct saa7146_dev *saa = budget_av->budget.dev;
  159. int timeout = 50; // 5 seconds (4.4.6 Ready)
  160. if (slot != 0)
  161. return -EINVAL;
  162. dprintk(1, "ciintf_slot_reset\n");
  163. saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
  164. saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
  165. msleep(2);
  166. saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO); /* Vcc on */
  167. msleep(20); /* 20 ms Vcc settling time */
  168. saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO); /* enable card */
  169. /* This should have been based on pin 16 READY of the pcmcia port,
  170. * but AFAICS it is not routed to the saa7146 */
  171. while (--timeout > 0 && ciintf_read_attribute_mem(ca, slot, 0) != 0x1d)
  172. msleep(100);
  173. if (timeout <= 0)
  174. {
  175. printk(KERN_ERR "budget-av: cam reset failed (timeout).\n");
  176. saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
  177. saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
  178. return -ETIMEDOUT;
  179. }
  180. return 0;
  181. }
  182. static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
  183. {
  184. struct budget_av *budget_av = (struct budget_av *) ca->data;
  185. struct saa7146_dev *saa = budget_av->budget.dev;
  186. if (slot != 0)
  187. return -EINVAL;
  188. dprintk(1, "ciintf_slot_shutdown\n");
  189. ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
  190. budget_av->slot_status = 0;
  191. return 0;
  192. }
  193. static int ciintf_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
  194. {
  195. struct budget_av *budget_av = (struct budget_av *) ca->data;
  196. struct saa7146_dev *saa = budget_av->budget.dev;
  197. if (slot != 0)
  198. return -EINVAL;
  199. dprintk(1, "ciintf_slot_ts_enable: %d\n", budget_av->slot_status);
  200. ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
  201. return 0;
  202. }
  203. static int ciintf_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
  204. {
  205. struct budget_av *budget_av = (struct budget_av *) ca->data;
  206. struct saa7146_dev *saa = budget_av->budget.dev;
  207. if (slot != 0)
  208. return -EINVAL;
  209. if (!budget_av->slot_status) {
  210. saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
  211. udelay(1);
  212. if (saa7146_read(saa, PSR) & MASK_06)
  213. {
  214. printk(KERN_INFO "budget-av: cam inserted\n");
  215. budget_av->slot_status = 1;
  216. }
  217. saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
  218. } else if (!open) {
  219. saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
  220. if (ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1) == -ETIMEDOUT)
  221. {
  222. printk(KERN_INFO "budget-av: cam ejected\n");
  223. saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
  224. saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
  225. budget_av->slot_status = 0;
  226. }
  227. }
  228. if (budget_av->slot_status == 1)
  229. return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
  230. return 0;
  231. }
  232. static int ciintf_init(struct budget_av *budget_av)
  233. {
  234. struct saa7146_dev *saa = budget_av->budget.dev;
  235. int result;
  236. memset(&budget_av->ca, 0, sizeof(struct dvb_ca_en50221));
  237. saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
  238. saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
  239. saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO);
  240. saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
  241. /* Enable DEBI pins */
  242. saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16) | 0x800);
  243. /* register CI interface */
  244. budget_av->ca.owner = THIS_MODULE;
  245. budget_av->ca.read_attribute_mem = ciintf_read_attribute_mem;
  246. budget_av->ca.write_attribute_mem = ciintf_write_attribute_mem;
  247. budget_av->ca.read_cam_control = ciintf_read_cam_control;
  248. budget_av->ca.write_cam_control = ciintf_write_cam_control;
  249. budget_av->ca.slot_reset = ciintf_slot_reset;
  250. budget_av->ca.slot_shutdown = ciintf_slot_shutdown;
  251. budget_av->ca.slot_ts_enable = ciintf_slot_ts_enable;
  252. budget_av->ca.poll_slot_status = ciintf_poll_slot_status;
  253. budget_av->ca.data = budget_av;
  254. if ((result = dvb_ca_en50221_init(&budget_av->budget.dvb_adapter,
  255. &budget_av->ca, 0, 1)) != 0) {
  256. printk(KERN_ERR "budget-av: ci initialisation failed.\n");
  257. goto error;
  258. }
  259. printk(KERN_INFO "budget-av: ci interface initialised.\n");
  260. budget_av->budget.ci_present = 1;
  261. return 0;
  262. error:
  263. saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
  264. return result;
  265. }
  266. static void ciintf_deinit(struct budget_av *budget_av)
  267. {
  268. struct saa7146_dev *saa = budget_av->budget.dev;
  269. saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
  270. saa7146_setgpio(saa, 1, SAA7146_GPIO_INPUT);
  271. saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
  272. saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
  273. /* release the CA device */
  274. dvb_ca_en50221_release(&budget_av->ca);
  275. /* disable DEBI pins */
  276. saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
  277. }
  278. static const u8 saa7113_tab[] = {
  279. 0x01, 0x08,
  280. 0x02, 0xc0,
  281. 0x03, 0x33,
  282. 0x04, 0x00,
  283. 0x05, 0x00,
  284. 0x06, 0xeb,
  285. 0x07, 0xe0,
  286. 0x08, 0x28,
  287. 0x09, 0x00,
  288. 0x0a, 0x80,
  289. 0x0b, 0x47,
  290. 0x0c, 0x40,
  291. 0x0d, 0x00,
  292. 0x0e, 0x01,
  293. 0x0f, 0x44,
  294. 0x10, 0x08,
  295. 0x11, 0x0c,
  296. 0x12, 0x7b,
  297. 0x13, 0x00,
  298. 0x15, 0x00, 0x16, 0x00, 0x17, 0x00,
  299. 0x57, 0xff,
  300. 0x40, 0x82, 0x58, 0x00, 0x59, 0x54, 0x5a, 0x07,
  301. 0x5b, 0x83, 0x5e, 0x00,
  302. 0xff
  303. };
  304. static int saa7113_init(struct budget_av *budget_av)
  305. {
  306. struct budget *budget = &budget_av->budget;
  307. struct saa7146_dev *saa = budget->dev;
  308. const u8 *data = saa7113_tab;
  309. saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI);
  310. msleep(200);
  311. if (i2c_writereg(&budget->i2c_adap, 0x4a, 0x01, 0x08) != 1) {
  312. dprintk(1, "saa7113 not found on KNC card\n");
  313. return -ENODEV;
  314. }
  315. dprintk(1, "saa7113 detected and initializing\n");
  316. while (*data != 0xff) {
  317. i2c_writereg(&budget->i2c_adap, 0x4a, *data, *(data + 1));
  318. data += 2;
  319. }
  320. dprintk(1, "saa7113 status=%02x\n", i2c_readreg(&budget->i2c_adap, 0x4a, 0x1f));
  321. return 0;
  322. }
  323. static int saa7113_setinput(struct budget_av *budget_av, int input)
  324. {
  325. struct budget *budget = &budget_av->budget;
  326. if (1 != budget_av->has_saa7113)
  327. return -ENODEV;
  328. if (input == 1) {
  329. i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc7);
  330. i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x80);
  331. } else if (input == 0) {
  332. i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc0);
  333. i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x00);
  334. } else
  335. return -EINVAL;
  336. budget_av->cur_input = input;
  337. return 0;
  338. }
  339. static int philips_su1278_ty_ci_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
  340. {
  341. u8 aclk = 0;
  342. u8 bclk = 0;
  343. u8 m1;
  344. aclk = 0xb5;
  345. if (srate < 2000000)
  346. bclk = 0x86;
  347. else if (srate < 5000000)
  348. bclk = 0x89;
  349. else if (srate < 15000000)
  350. bclk = 0x8f;
  351. else if (srate < 45000000)
  352. bclk = 0x95;
  353. m1 = 0x14;
  354. if (srate < 4000000)
  355. m1 = 0x10;
  356. stv0299_writereg(fe, 0x13, aclk);
  357. stv0299_writereg(fe, 0x14, bclk);
  358. stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
  359. stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
  360. stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
  361. stv0299_writereg(fe, 0x0f, 0x80 | m1);
  362. return 0;
  363. }
  364. static int philips_su1278_ty_ci_pll_set(struct dvb_frontend *fe,
  365. struct dvb_frontend_parameters *params)
  366. {
  367. struct budget_av *budget_av = (struct budget_av *) fe->dvb->priv;
  368. u32 div;
  369. u8 buf[4];
  370. struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
  371. if ((params->frequency < 950000) || (params->frequency > 2150000))
  372. return -EINVAL;
  373. div = (params->frequency + (125 - 1)) / 125; // round correctly
  374. buf[0] = (div >> 8) & 0x7f;
  375. buf[1] = div & 0xff;
  376. buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
  377. buf[3] = 0x20;
  378. if (params->u.qpsk.symbol_rate < 4000000)
  379. buf[3] |= 1;
  380. if (params->frequency < 1250000)
  381. buf[3] |= 0;
  382. else if (params->frequency < 1550000)
  383. buf[3] |= 0x40;
  384. else if (params->frequency < 2050000)
  385. buf[3] |= 0x80;
  386. else if (params->frequency < 2150000)
  387. buf[3] |= 0xC0;
  388. if (i2c_transfer(&budget_av->budget.i2c_adap, &msg, 1) != 1)
  389. return -EIO;
  390. return 0;
  391. }
  392. static u8 typhoon_cinergy1200s_inittab[] = {
  393. 0x01, 0x15,
  394. 0x02, 0x30,
  395. 0x03, 0x00,
  396. 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
  397. 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
  398. 0x06, 0x40, /* DAC not used, set to high impendance mode */
  399. 0x07, 0x00, /* DAC LSB */
  400. 0x08, 0x40, /* DiSEqC off */
  401. 0x09, 0x00, /* FIFO */
  402. 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
  403. 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
  404. 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
  405. 0x10, 0x3f, // AGC2 0x3d
  406. 0x11, 0x84,
  407. 0x12, 0xb5, // Lock detect: -64 Carrier freq detect:on
  408. 0x15, 0xc9, // lock detector threshold
  409. 0x16, 0x00,
  410. 0x17, 0x00,
  411. 0x18, 0x00,
  412. 0x19, 0x00,
  413. 0x1a, 0x00,
  414. 0x1f, 0x50,
  415. 0x20, 0x00,
  416. 0x21, 0x00,
  417. 0x22, 0x00,
  418. 0x23, 0x00,
  419. 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
  420. 0x29, 0x1e, // 1/2 threshold
  421. 0x2a, 0x14, // 2/3 threshold
  422. 0x2b, 0x0f, // 3/4 threshold
  423. 0x2c, 0x09, // 5/6 threshold
  424. 0x2d, 0x05, // 7/8 threshold
  425. 0x2e, 0x01,
  426. 0x31, 0x1f, // test all FECs
  427. 0x32, 0x19, // viterbi and synchro search
  428. 0x33, 0xfc, // rs control
  429. 0x34, 0x93, // error control
  430. 0x0f, 0x92,
  431. 0xff, 0xff
  432. };
  433. static struct stv0299_config typhoon_config = {
  434. .demod_address = 0x68,
  435. .inittab = typhoon_cinergy1200s_inittab,
  436. .mclk = 88000000UL,
  437. .invert = 0,
  438. .enhanced_tuning = 0,
  439. .skip_reinit = 0,
  440. .lock_output = STV0229_LOCKOUTPUT_1,
  441. .volt13_op0_op1 = STV0299_VOLT13_OP0,
  442. .min_delay_ms = 100,
  443. .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
  444. .pll_set = philips_su1278_ty_ci_pll_set,
  445. };
  446. static struct stv0299_config cinergy_1200s_config = {
  447. .demod_address = 0x68,
  448. .inittab = typhoon_cinergy1200s_inittab,
  449. .mclk = 88000000UL,
  450. .invert = 0,
  451. .enhanced_tuning = 0,
  452. .skip_reinit = 0,
  453. .lock_output = STV0229_LOCKOUTPUT_0,
  454. .volt13_op0_op1 = STV0299_VOLT13_OP0,
  455. .min_delay_ms = 100,
  456. .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
  457. .pll_set = philips_su1278_ty_ci_pll_set,
  458. };
  459. static int philips_cu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
  460. {
  461. struct budget *budget = (struct budget *) fe->dvb->priv;
  462. u8 buf[4];
  463. struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = buf,.len = sizeof(buf) };
  464. #define TUNER_MUL 62500
  465. u32 div = (params->frequency + 36125000 + TUNER_MUL / 2) / TUNER_MUL;
  466. buf[0] = (div >> 8) & 0x7f;
  467. buf[1] = div & 0xff;
  468. buf[2] = 0x86;
  469. buf[3] = (params->frequency < 150000000 ? 0x01 :
  470. params->frequency < 445000000 ? 0x02 : 0x04);
  471. if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
  472. return -EIO;
  473. return 0;
  474. }
  475. static struct tda10021_config philips_cu1216_config = {
  476. .demod_address = 0x0c,
  477. .pll_set = philips_cu1216_pll_set,
  478. };
  479. static int philips_tu1216_pll_init(struct dvb_frontend *fe)
  480. {
  481. struct budget *budget = (struct budget *) fe->dvb->priv;
  482. static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
  483. struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
  484. // setup PLL configuration
  485. if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
  486. return -EIO;
  487. msleep(1);
  488. return 0;
  489. }
  490. static int philips_tu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
  491. {
  492. struct budget *budget = (struct budget *) fe->dvb->priv;
  493. u8 tuner_buf[4];
  494. struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,.len =
  495. sizeof(tuner_buf) };
  496. int tuner_frequency = 0;
  497. u8 band, cp, filter;
  498. // determine charge pump
  499. tuner_frequency = params->frequency + 36166000;
  500. if (tuner_frequency < 87000000)
  501. return -EINVAL;
  502. else if (tuner_frequency < 130000000)
  503. cp = 3;
  504. else if (tuner_frequency < 160000000)
  505. cp = 5;
  506. else if (tuner_frequency < 200000000)
  507. cp = 6;
  508. else if (tuner_frequency < 290000000)
  509. cp = 3;
  510. else if (tuner_frequency < 420000000)
  511. cp = 5;
  512. else if (tuner_frequency < 480000000)
  513. cp = 6;
  514. else if (tuner_frequency < 620000000)
  515. cp = 3;
  516. else if (tuner_frequency < 830000000)
  517. cp = 5;
  518. else if (tuner_frequency < 895000000)
  519. cp = 7;
  520. else
  521. return -EINVAL;
  522. // determine band
  523. if (params->frequency < 49000000)
  524. return -EINVAL;
  525. else if (params->frequency < 161000000)
  526. band = 1;
  527. else if (params->frequency < 444000000)
  528. band = 2;
  529. else if (params->frequency < 861000000)
  530. band = 4;
  531. else
  532. return -EINVAL;
  533. // setup PLL filter
  534. switch (params->u.ofdm.bandwidth) {
  535. case BANDWIDTH_6_MHZ:
  536. filter = 0;
  537. break;
  538. case BANDWIDTH_7_MHZ:
  539. filter = 0;
  540. break;
  541. case BANDWIDTH_8_MHZ:
  542. filter = 1;
  543. break;
  544. default:
  545. return -EINVAL;
  546. }
  547. // calculate divisor
  548. // ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
  549. tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
  550. // setup tuner buffer
  551. tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
  552. tuner_buf[1] = tuner_frequency & 0xff;
  553. tuner_buf[2] = 0xca;
  554. tuner_buf[3] = (cp << 5) | (filter << 3) | band;
  555. if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
  556. return -EIO;
  557. msleep(1);
  558. return 0;
  559. }
  560. static int philips_tu1216_request_firmware(struct dvb_frontend *fe,
  561. const struct firmware **fw, char *name)
  562. {
  563. struct budget *budget = (struct budget *) fe->dvb->priv;
  564. return request_firmware(fw, name, &budget->dev->pci->dev);
  565. }
  566. static struct tda1004x_config philips_tu1216_config = {
  567. .demod_address = 0x8,
  568. .invert = 1,
  569. .invert_oclk = 1,
  570. .xtal_freq = TDA10046_XTAL_4M,
  571. .agc_config = TDA10046_AGC_DEFAULT,
  572. .if_freq = TDA10046_FREQ_3617,
  573. .pll_init = philips_tu1216_pll_init,
  574. .pll_set = philips_tu1216_pll_set,
  575. .pll_sleep = NULL,
  576. .request_firmware = philips_tu1216_request_firmware,
  577. };
  578. static u8 read_pwm(struct budget_av *budget_av)
  579. {
  580. u8 b = 0xff;
  581. u8 pwm;
  582. struct i2c_msg msg[] = { {.addr = 0x50,.flags = 0,.buf = &b,.len = 1},
  583. {.addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1}
  584. };
  585. if ((i2c_transfer(&budget_av->budget.i2c_adap, msg, 2) != 2)
  586. || (pwm == 0xff))
  587. pwm = 0x48;
  588. return pwm;
  589. }
  590. #define SUBID_DVBS_KNC1 0x0010
  591. #define SUBID_DVBS_KNC1_PLUS 0x0011
  592. #define SUBID_DVBS_TYPHOON 0x4f56
  593. #define SUBID_DVBS_CINERGY1200 0x1154
  594. #define SUBID_DVBC_KNC1 0x0020
  595. #define SUBID_DVBC_KNC1_PLUS 0x0021
  596. #define SUBID_DVBC_CINERGY1200 0x1156
  597. #define SUBID_DVBT_KNC1_PLUS 0x0031
  598. #define SUBID_DVBT_KNC1 0x0030
  599. #define SUBID_DVBT_CINERGY1200 0x1157
  600. static void frontend_init(struct budget_av *budget_av)
  601. {
  602. struct saa7146_dev * saa = budget_av->budget.dev;
  603. struct dvb_frontend * fe = NULL;
  604. switch (saa->pci->subsystem_device) {
  605. case SUBID_DVBS_KNC1_PLUS:
  606. case SUBID_DVBC_KNC1_PLUS:
  607. case SUBID_DVBT_KNC1_PLUS:
  608. // Enable / PowerON Frontend
  609. saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTHI);
  610. break;
  611. }
  612. switch (saa->pci->subsystem_device) {
  613. case SUBID_DVBS_KNC1:
  614. case SUBID_DVBS_KNC1_PLUS:
  615. case SUBID_DVBS_TYPHOON:
  616. fe = stv0299_attach(&typhoon_config,
  617. &budget_av->budget.i2c_adap);
  618. break;
  619. case SUBID_DVBS_CINERGY1200:
  620. fe = stv0299_attach(&cinergy_1200s_config,
  621. &budget_av->budget.i2c_adap);
  622. break;
  623. case SUBID_DVBC_KNC1:
  624. case SUBID_DVBC_KNC1_PLUS:
  625. fe = tda10021_attach(&philips_cu1216_config,
  626. &budget_av->budget.i2c_adap,
  627. read_pwm(budget_av));
  628. break;
  629. case SUBID_DVBT_KNC1:
  630. case SUBID_DVBT_KNC1_PLUS:
  631. fe = tda10046_attach(&philips_tu1216_config,
  632. &budget_av->budget.i2c_adap);
  633. break;
  634. case SUBID_DVBC_CINERGY1200:
  635. fe = tda10021_attach(&philips_cu1216_config,
  636. &budget_av->budget.i2c_adap,
  637. read_pwm(budget_av));
  638. break;
  639. case SUBID_DVBT_CINERGY1200:
  640. fe = tda10046_attach(&philips_tu1216_config,
  641. &budget_av->budget.i2c_adap);
  642. break;
  643. }
  644. if (fe == NULL) {
  645. printk(KERN_ERR "budget-av: A frontend driver was not found "
  646. "for device %04x/%04x subsystem %04x/%04x\n",
  647. saa->pci->vendor,
  648. saa->pci->device,
  649. saa->pci->subsystem_vendor,
  650. saa->pci->subsystem_device);
  651. return;
  652. }
  653. budget_av->budget.dvb_frontend = fe;
  654. if (dvb_register_frontend(&budget_av->budget.dvb_adapter,
  655. budget_av->budget.dvb_frontend)) {
  656. printk(KERN_ERR "budget-av: Frontend registration failed!\n");
  657. if (budget_av->budget.dvb_frontend->ops->release)
  658. budget_av->budget.dvb_frontend->ops->release(budget_av->budget.dvb_frontend);
  659. budget_av->budget.dvb_frontend = NULL;
  660. }
  661. }
  662. static void budget_av_irq(struct saa7146_dev *dev, u32 * isr)
  663. {
  664. struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
  665. dprintk(8, "dev: %p, budget_av: %p\n", dev, budget_av);
  666. if (*isr & MASK_10)
  667. ttpci_budget_irq10_handler(dev, isr);
  668. }
  669. static int budget_av_detach(struct saa7146_dev *dev)
  670. {
  671. struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
  672. int err;
  673. dprintk(2, "dev: %p\n", dev);
  674. if (1 == budget_av->has_saa7113) {
  675. saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTLO);
  676. msleep(200);
  677. saa7146_unregister_device(&budget_av->vd, dev);
  678. }
  679. if (budget_av->budget.ci_present)
  680. ciintf_deinit(budget_av);
  681. if (budget_av->budget.dvb_frontend != NULL)
  682. dvb_unregister_frontend(budget_av->budget.dvb_frontend);
  683. err = ttpci_budget_deinit(&budget_av->budget);
  684. kfree(budget_av);
  685. return err;
  686. }
  687. static struct saa7146_ext_vv vv_data;
  688. static int budget_av_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
  689. {
  690. struct budget_av *budget_av;
  691. u8 *mac;
  692. int err;
  693. dprintk(2, "dev: %p\n", dev);
  694. if (!(budget_av = kmalloc(sizeof(struct budget_av), GFP_KERNEL)))
  695. return -ENOMEM;
  696. memset(budget_av, 0, sizeof(struct budget_av));
  697. budget_av->has_saa7113 = 0;
  698. budget_av->budget.ci_present = 0;
  699. dev->ext_priv = budget_av;
  700. if ((err = ttpci_budget_init(&budget_av->budget, dev, info, THIS_MODULE))) {
  701. kfree(budget_av);
  702. return err;
  703. }
  704. /* knc1 initialization */
  705. saa7146_write(dev, DD1_STREAM_B, 0x04000000);
  706. saa7146_write(dev, DD1_INIT, 0x07000600);
  707. saa7146_write(dev, MC2, MASK_09 | MASK_25 | MASK_10 | MASK_26);
  708. if (saa7113_init(budget_av) == 0) {
  709. budget_av->has_saa7113 = 1;
  710. if (0 != saa7146_vv_init(dev, &vv_data)) {
  711. /* fixme: proper cleanup here */
  712. ERR(("cannot init vv subsystem.\n"));
  713. return err;
  714. }
  715. if ((err = saa7146_register_device(&budget_av->vd, dev, "knc1", VFL_TYPE_GRABBER))) {
  716. /* fixme: proper cleanup here */
  717. ERR(("cannot register capture v4l2 device.\n"));
  718. return err;
  719. }
  720. /* beware: this modifies dev->vv ... */
  721. saa7146_set_hps_source_and_sync(dev, SAA7146_HPS_SOURCE_PORT_A,
  722. SAA7146_HPS_SYNC_PORT_A);
  723. saa7113_setinput(budget_av, 0);
  724. } else {
  725. ciintf_init(budget_av);
  726. }
  727. /* fixme: find some sane values here... */
  728. saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
  729. mac = budget_av->budget.dvb_adapter.proposed_mac;
  730. if (i2c_readregs(&budget_av->budget.i2c_adap, 0xa0, 0x30, mac, 6)) {
  731. printk(KERN_ERR "KNC1-%d: Could not read MAC from KNC1 card\n",
  732. budget_av->budget.dvb_adapter.num);
  733. memset(mac, 0, 6);
  734. } else {
  735. printk(KERN_INFO "KNC1-%d: MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
  736. budget_av->budget.dvb_adapter.num,
  737. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  738. }
  739. budget_av->budget.dvb_adapter.priv = budget_av;
  740. frontend_init(budget_av);
  741. return 0;
  742. }
  743. #define KNC1_INPUTS 2
  744. static struct v4l2_input knc1_inputs[KNC1_INPUTS] = {
  745. {0, "Composite", V4L2_INPUT_TYPE_TUNER, 1, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
  746. {1, "S-Video", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
  747. };
  748. static struct saa7146_extension_ioctls ioctls[] = {
  749. {VIDIOC_ENUMINPUT, SAA7146_EXCLUSIVE},
  750. {VIDIOC_G_INPUT, SAA7146_EXCLUSIVE},
  751. {VIDIOC_S_INPUT, SAA7146_EXCLUSIVE},
  752. {0, 0}
  753. };
  754. static int av_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
  755. {
  756. struct saa7146_dev *dev = fh->dev;
  757. struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
  758. switch (cmd) {
  759. case VIDIOC_ENUMINPUT:{
  760. struct v4l2_input *i = arg;
  761. dprintk(1, "VIDIOC_ENUMINPUT %d.\n", i->index);
  762. if (i->index < 0 || i->index >= KNC1_INPUTS) {
  763. return -EINVAL;
  764. }
  765. memcpy(i, &knc1_inputs[i->index], sizeof(struct v4l2_input));
  766. return 0;
  767. }
  768. case VIDIOC_G_INPUT:{
  769. int *input = (int *) arg;
  770. *input = budget_av->cur_input;
  771. dprintk(1, "VIDIOC_G_INPUT %d.\n", *input);
  772. return 0;
  773. }
  774. case VIDIOC_S_INPUT:{
  775. int input = *(int *) arg;
  776. dprintk(1, "VIDIOC_S_INPUT %d.\n", input);
  777. return saa7113_setinput(budget_av, input);
  778. }
  779. default:
  780. return -ENOIOCTLCMD;
  781. }
  782. return 0;
  783. }
  784. static struct saa7146_standard standard[] = {
  785. {.name = "PAL",.id = V4L2_STD_PAL,
  786. .v_offset = 0x17,.v_field = 288,
  787. .h_offset = 0x14,.h_pixels = 680,
  788. .v_max_out = 576,.h_max_out = 768 },
  789. {.name = "NTSC",.id = V4L2_STD_NTSC,
  790. .v_offset = 0x16,.v_field = 240,
  791. .h_offset = 0x06,.h_pixels = 708,
  792. .v_max_out = 480,.h_max_out = 640, },
  793. };
  794. static struct saa7146_ext_vv vv_data = {
  795. .inputs = 2,
  796. .capabilities = 0, // perhaps later: V4L2_CAP_VBI_CAPTURE, but that need tweaking with the saa7113
  797. .flags = 0,
  798. .stds = &standard[0],
  799. .num_stds = sizeof(standard) / sizeof(struct saa7146_standard),
  800. .ioctls = &ioctls[0],
  801. .ioctl = av_ioctl,
  802. };
  803. static struct saa7146_extension budget_extension;
  804. MAKE_BUDGET_INFO(knc1s, "KNC1 DVB-S", BUDGET_KNC1S);
  805. MAKE_BUDGET_INFO(knc1c, "KNC1 DVB-C", BUDGET_KNC1C);
  806. MAKE_BUDGET_INFO(knc1t, "KNC1 DVB-T", BUDGET_KNC1T);
  807. MAKE_BUDGET_INFO(knc1sp, "KNC1 DVB-S Plus", BUDGET_KNC1SP);
  808. MAKE_BUDGET_INFO(knc1cp, "KNC1 DVB-C Plus", BUDGET_KNC1CP);
  809. MAKE_BUDGET_INFO(knc1tp, "KNC1 DVB-T Plus", BUDGET_KNC1TP);
  810. MAKE_BUDGET_INFO(cin1200s, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
  811. MAKE_BUDGET_INFO(cin1200c, "Terratec Cinergy 1200 DVB-C", BUDGET_CIN1200C);
  812. MAKE_BUDGET_INFO(cin1200t, "Terratec Cinergy 1200 DVB-T", BUDGET_CIN1200T);
  813. static struct pci_device_id pci_tbl[] = {
  814. MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x4f56),
  815. MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x0010),
  816. MAKE_EXTENSION_PCI(knc1sp, 0x1131, 0x0011),
  817. MAKE_EXTENSION_PCI(knc1c, 0x1894, 0x0020),
  818. MAKE_EXTENSION_PCI(knc1cp, 0x1894, 0x0021),
  819. MAKE_EXTENSION_PCI(knc1t, 0x1894, 0x0030),
  820. MAKE_EXTENSION_PCI(knc1tp, 0x1894, 0x0031),
  821. MAKE_EXTENSION_PCI(cin1200s, 0x153b, 0x1154),
  822. MAKE_EXTENSION_PCI(cin1200c, 0x153b, 0x1156),
  823. MAKE_EXTENSION_PCI(cin1200t, 0x153b, 0x1157),
  824. {
  825. .vendor = 0,
  826. }
  827. };
  828. MODULE_DEVICE_TABLE(pci, pci_tbl);
  829. static struct saa7146_extension budget_extension = {
  830. .name = "budget_av",
  831. .pci_tbl = pci_tbl,
  832. .module = THIS_MODULE,
  833. .attach = budget_av_attach,
  834. .detach = budget_av_detach,
  835. .irq_mask = MASK_10,
  836. .irq_func = budget_av_irq,
  837. };
  838. static int __init budget_av_init(void)
  839. {
  840. return saa7146_register_extension(&budget_extension);
  841. }
  842. static void __exit budget_av_exit(void)
  843. {
  844. saa7146_unregister_extension(&budget_extension);
  845. }
  846. module_init(budget_av_init);
  847. module_exit(budget_av_exit);
  848. MODULE_LICENSE("GPL");
  849. MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
  850. MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
  851. "budget PCI DVB w/ analog input and CI-module (e.g. the KNC cards)");