xc5000.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. /*
  2. * Driver for Xceive XC5000 "QAM/8VSB single chip tuner"
  3. *
  4. * Copyright (c) 2007 Xceive Corporation
  5. * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. *
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/videodev2.h>
  25. #include <linux/delay.h>
  26. #include <linux/dvb/frontend.h>
  27. #include <linux/i2c.h>
  28. #include "dvb_frontend.h"
  29. #include "xc5000.h"
  30. #include "tuner-i2c.h"
  31. static int debug;
  32. module_param(debug, int, 0644);
  33. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  34. static int xc5000_load_fw_on_attach;
  35. module_param_named(init_fw, xc5000_load_fw_on_attach, int, 0644);
  36. MODULE_PARM_DESC(init_fw, "Load firmware during driver initialization.");
  37. static DEFINE_MUTEX(xc5000_list_mutex);
  38. static LIST_HEAD(hybrid_tuner_instance_list);
  39. #define dprintk(level, fmt, arg...) if (debug >= level) \
  40. printk(KERN_INFO "%s: " fmt, "xc5000", ## arg)
  41. #define XC5000_DEFAULT_FIRMWARE "dvb-fe-xc5000-1.1.fw"
  42. #define XC5000_DEFAULT_FIRMWARE_SIZE 12332
  43. struct xc5000_priv {
  44. struct tuner_i2c_props i2c_props;
  45. struct list_head hybrid_tuner_instance_list;
  46. u32 if_khz;
  47. u32 freq_hz;
  48. u32 bandwidth;
  49. u8 video_standard;
  50. u8 rf_mode;
  51. };
  52. /* Misc Defines */
  53. #define MAX_TV_STANDARD 23
  54. #define XC_MAX_I2C_WRITE_LENGTH 64
  55. /* Signal Types */
  56. #define XC_RF_MODE_AIR 0
  57. #define XC_RF_MODE_CABLE 1
  58. /* Result codes */
  59. #define XC_RESULT_SUCCESS 0
  60. #define XC_RESULT_RESET_FAILURE 1
  61. #define XC_RESULT_I2C_WRITE_FAILURE 2
  62. #define XC_RESULT_I2C_READ_FAILURE 3
  63. #define XC_RESULT_OUT_OF_RANGE 5
  64. /* Product id */
  65. #define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
  66. #define XC_PRODUCT_ID_FW_LOADED 0x1388
  67. /* Registers */
  68. #define XREG_INIT 0x00
  69. #define XREG_VIDEO_MODE 0x01
  70. #define XREG_AUDIO_MODE 0x02
  71. #define XREG_RF_FREQ 0x03
  72. #define XREG_D_CODE 0x04
  73. #define XREG_IF_OUT 0x05
  74. #define XREG_SEEK_MODE 0x07
  75. #define XREG_POWER_DOWN 0x0A
  76. #define XREG_SIGNALSOURCE 0x0D /* 0=Air, 1=Cable */
  77. #define XREG_SMOOTHEDCVBS 0x0E
  78. #define XREG_XTALFREQ 0x0F
  79. #define XREG_FINERFFREQ 0x10
  80. #define XREG_DDIMODE 0x11
  81. #define XREG_ADC_ENV 0x00
  82. #define XREG_QUALITY 0x01
  83. #define XREG_FRAME_LINES 0x02
  84. #define XREG_HSYNC_FREQ 0x03
  85. #define XREG_LOCK 0x04
  86. #define XREG_FREQ_ERROR 0x05
  87. #define XREG_SNR 0x06
  88. #define XREG_VERSION 0x07
  89. #define XREG_PRODUCT_ID 0x08
  90. #define XREG_BUSY 0x09
  91. /*
  92. Basic firmware description. This will remain with
  93. the driver for documentation purposes.
  94. This represents an I2C firmware file encoded as a
  95. string of unsigned char. Format is as follows:
  96. char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
  97. char[1 ]=len0_LSB -> length of first write transaction
  98. char[2 ]=data0 -> first byte to be sent
  99. char[3 ]=data1
  100. char[4 ]=data2
  101. char[ ]=...
  102. char[M ]=dataN -> last byte to be sent
  103. char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
  104. char[M+2]=len1_LSB -> length of second write transaction
  105. char[M+3]=data0
  106. char[M+4]=data1
  107. ...
  108. etc.
  109. The [len] value should be interpreted as follows:
  110. len= len_MSB _ len_LSB
  111. len=1111_1111_1111_1111 : End of I2C_SEQUENCE
  112. len=0000_0000_0000_0000 : Reset command: Do hardware reset
  113. len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
  114. len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
  115. For the RESET and WAIT commands, the two following bytes will contain
  116. immediately the length of the following transaction.
  117. */
  118. struct XC_TV_STANDARD {
  119. char *Name;
  120. u16 AudioMode;
  121. u16 VideoMode;
  122. };
  123. /* Tuner standards */
  124. #define MN_NTSC_PAL_BTSC 0
  125. #define MN_NTSC_PAL_A2 1
  126. #define MN_NTSC_PAL_EIAJ 2
  127. #define MN_NTSC_PAL_Mono 3
  128. #define BG_PAL_A2 4
  129. #define BG_PAL_NICAM 5
  130. #define BG_PAL_MONO 6
  131. #define I_PAL_NICAM 7
  132. #define I_PAL_NICAM_MONO 8
  133. #define DK_PAL_A2 9
  134. #define DK_PAL_NICAM 10
  135. #define DK_PAL_MONO 11
  136. #define DK_SECAM_A2DK1 12
  137. #define DK_SECAM_A2LDK3 13
  138. #define DK_SECAM_A2MONO 14
  139. #define L_SECAM_NICAM 15
  140. #define LC_SECAM_NICAM 16
  141. #define DTV6 17
  142. #define DTV8 18
  143. #define DTV7_8 19
  144. #define DTV7 20
  145. #define FM_Radio_INPUT2 21
  146. #define FM_Radio_INPUT1 22
  147. static struct XC_TV_STANDARD XC5000_Standard[MAX_TV_STANDARD] = {
  148. {"M/N-NTSC/PAL-BTSC", 0x0400, 0x8020},
  149. {"M/N-NTSC/PAL-A2", 0x0600, 0x8020},
  150. {"M/N-NTSC/PAL-EIAJ", 0x0440, 0x8020},
  151. {"M/N-NTSC/PAL-Mono", 0x0478, 0x8020},
  152. {"B/G-PAL-A2", 0x0A00, 0x8049},
  153. {"B/G-PAL-NICAM", 0x0C04, 0x8049},
  154. {"B/G-PAL-MONO", 0x0878, 0x8059},
  155. {"I-PAL-NICAM", 0x1080, 0x8009},
  156. {"I-PAL-NICAM-MONO", 0x0E78, 0x8009},
  157. {"D/K-PAL-A2", 0x1600, 0x8009},
  158. {"D/K-PAL-NICAM", 0x0E80, 0x8009},
  159. {"D/K-PAL-MONO", 0x1478, 0x8009},
  160. {"D/K-SECAM-A2 DK1", 0x1200, 0x8009},
  161. {"D/K-SECAM-A2 L/DK3", 0x0E00, 0x8009},
  162. {"D/K-SECAM-A2 MONO", 0x1478, 0x8009},
  163. {"L-SECAM-NICAM", 0x8E82, 0x0009},
  164. {"L'-SECAM-NICAM", 0x8E82, 0x4009},
  165. {"DTV6", 0x00C0, 0x8002},
  166. {"DTV8", 0x00C0, 0x800B},
  167. {"DTV7/8", 0x00C0, 0x801B},
  168. {"DTV7", 0x00C0, 0x8007},
  169. {"FM Radio-INPUT2", 0x9802, 0x9002},
  170. {"FM Radio-INPUT1", 0x0208, 0x9002}
  171. };
  172. static int xc5000_is_firmware_loaded(struct dvb_frontend *fe);
  173. static int xc5000_writeregs(struct xc5000_priv *priv, u8 *buf, u8 len);
  174. static int xc5000_readregs(struct xc5000_priv *priv, u8 *buf, u8 len);
  175. static void xc5000_TunerReset(struct dvb_frontend *fe);
  176. static int xc_send_i2c_data(struct xc5000_priv *priv, u8 *buf, int len)
  177. {
  178. return xc5000_writeregs(priv, buf, len)
  179. ? XC_RESULT_I2C_WRITE_FAILURE : XC_RESULT_SUCCESS;
  180. }
  181. static int xc_read_i2c_data(struct xc5000_priv *priv, u8 *buf, int len)
  182. {
  183. return xc5000_readregs(priv, buf, len)
  184. ? XC_RESULT_I2C_READ_FAILURE : XC_RESULT_SUCCESS;
  185. }
  186. static int xc_reset(struct dvb_frontend *fe)
  187. {
  188. xc5000_TunerReset(fe);
  189. return XC_RESULT_SUCCESS;
  190. }
  191. static void xc_wait(int wait_ms)
  192. {
  193. msleep(wait_ms);
  194. }
  195. static void xc5000_TunerReset(struct dvb_frontend *fe)
  196. {
  197. struct xc5000_priv *priv = fe->tuner_priv;
  198. int ret;
  199. dprintk(1, "%s()\n", __func__);
  200. if (fe->callback) {
  201. ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
  202. fe->dvb->priv :
  203. priv->i2c_props.adap->algo_data,
  204. DVB_FRONTEND_COMPONENT_TUNER,
  205. XC5000_TUNER_RESET, 0);
  206. if (ret)
  207. printk(KERN_ERR "xc5000: reset failed\n");
  208. } else
  209. printk(KERN_ERR "xc5000: no tuner reset callback function, fatal\n");
  210. }
  211. static int xc_write_reg(struct xc5000_priv *priv, u16 regAddr, u16 i2cData)
  212. {
  213. u8 buf[4];
  214. int WatchDogTimer = 5;
  215. int result;
  216. buf[0] = (regAddr >> 8) & 0xFF;
  217. buf[1] = regAddr & 0xFF;
  218. buf[2] = (i2cData >> 8) & 0xFF;
  219. buf[3] = i2cData & 0xFF;
  220. result = xc_send_i2c_data(priv, buf, 4);
  221. if (result == XC_RESULT_SUCCESS) {
  222. /* wait for busy flag to clear */
  223. while ((WatchDogTimer > 0) && (result == XC_RESULT_SUCCESS)) {
  224. buf[0] = 0;
  225. buf[1] = XREG_BUSY;
  226. result = xc_send_i2c_data(priv, buf, 2);
  227. if (result == XC_RESULT_SUCCESS) {
  228. result = xc_read_i2c_data(priv, buf, 2);
  229. if (result == XC_RESULT_SUCCESS) {
  230. if ((buf[0] == 0) && (buf[1] == 0)) {
  231. /* busy flag cleared */
  232. break;
  233. } else {
  234. xc_wait(100); /* wait 5 ms */
  235. WatchDogTimer--;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. if (WatchDogTimer < 0)
  242. result = XC_RESULT_I2C_WRITE_FAILURE;
  243. return result;
  244. }
  245. static int xc_read_reg(struct xc5000_priv *priv, u16 regAddr, u16 *i2cData)
  246. {
  247. u8 buf[2];
  248. int result;
  249. buf[0] = (regAddr >> 8) & 0xFF;
  250. buf[1] = regAddr & 0xFF;
  251. result = xc_send_i2c_data(priv, buf, 2);
  252. if (result != XC_RESULT_SUCCESS)
  253. return result;
  254. result = xc_read_i2c_data(priv, buf, 2);
  255. if (result != XC_RESULT_SUCCESS)
  256. return result;
  257. *i2cData = buf[0] * 256 + buf[1];
  258. return result;
  259. }
  260. static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
  261. {
  262. struct xc5000_priv *priv = fe->tuner_priv;
  263. int i, nbytes_to_send, result;
  264. unsigned int len, pos, index;
  265. u8 buf[XC_MAX_I2C_WRITE_LENGTH];
  266. index = 0;
  267. while ((i2c_sequence[index] != 0xFF) ||
  268. (i2c_sequence[index + 1] != 0xFF)) {
  269. len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
  270. if (len == 0x0000) {
  271. /* RESET command */
  272. result = xc_reset(fe);
  273. index += 2;
  274. if (result != XC_RESULT_SUCCESS)
  275. return result;
  276. } else if (len & 0x8000) {
  277. /* WAIT command */
  278. xc_wait(len & 0x7FFF);
  279. index += 2;
  280. } else {
  281. /* Send i2c data whilst ensuring individual transactions
  282. * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
  283. */
  284. index += 2;
  285. buf[0] = i2c_sequence[index];
  286. buf[1] = i2c_sequence[index + 1];
  287. pos = 2;
  288. while (pos < len) {
  289. if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
  290. nbytes_to_send =
  291. XC_MAX_I2C_WRITE_LENGTH;
  292. else
  293. nbytes_to_send = (len - pos + 2);
  294. for (i = 2; i < nbytes_to_send; i++) {
  295. buf[i] = i2c_sequence[index + pos +
  296. i - 2];
  297. }
  298. result = xc_send_i2c_data(priv, buf,
  299. nbytes_to_send);
  300. if (result != XC_RESULT_SUCCESS)
  301. return result;
  302. pos += nbytes_to_send - 2;
  303. }
  304. index += len;
  305. }
  306. }
  307. return XC_RESULT_SUCCESS;
  308. }
  309. static int xc_initialize(struct xc5000_priv *priv)
  310. {
  311. dprintk(1, "%s()\n", __func__);
  312. return xc_write_reg(priv, XREG_INIT, 0);
  313. }
  314. static int xc_SetTVStandard(struct xc5000_priv *priv,
  315. u16 VideoMode, u16 AudioMode)
  316. {
  317. int ret;
  318. dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
  319. dprintk(1, "%s() Standard = %s\n",
  320. __func__,
  321. XC5000_Standard[priv->video_standard].Name);
  322. ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
  323. if (ret == XC_RESULT_SUCCESS)
  324. ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
  325. return ret;
  326. }
  327. static int xc_shutdown(struct xc5000_priv *priv)
  328. {
  329. return XC_RESULT_SUCCESS;
  330. /* Fixme: cannot bring tuner back alive once shutdown
  331. * without reloading the driver modules.
  332. * return xc_write_reg(priv, XREG_POWER_DOWN, 0);
  333. */
  334. }
  335. static int xc_SetSignalSource(struct xc5000_priv *priv, u16 rf_mode)
  336. {
  337. dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
  338. rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
  339. if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
  340. rf_mode = XC_RF_MODE_CABLE;
  341. printk(KERN_ERR
  342. "%s(), Invalid mode, defaulting to CABLE",
  343. __func__);
  344. }
  345. return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
  346. }
  347. static const struct dvb_tuner_ops xc5000_tuner_ops;
  348. static int xc_set_RF_frequency(struct xc5000_priv *priv, u32 freq_hz)
  349. {
  350. u16 freq_code;
  351. dprintk(1, "%s(%u)\n", __func__, freq_hz);
  352. if ((freq_hz > xc5000_tuner_ops.info.frequency_max) ||
  353. (freq_hz < xc5000_tuner_ops.info.frequency_min))
  354. return XC_RESULT_OUT_OF_RANGE;
  355. freq_code = (u16)(freq_hz / 15625);
  356. return xc_write_reg(priv, XREG_RF_FREQ, freq_code);
  357. }
  358. static int xc_set_IF_frequency(struct xc5000_priv *priv, u32 freq_khz)
  359. {
  360. u32 freq_code = (freq_khz * 1024)/1000;
  361. dprintk(1, "%s(freq_khz = %d) freq_code = 0x%x\n",
  362. __func__, freq_khz, freq_code);
  363. return xc_write_reg(priv, XREG_IF_OUT, freq_code);
  364. }
  365. static int xc_get_ADC_Envelope(struct xc5000_priv *priv, u16 *adc_envelope)
  366. {
  367. return xc_read_reg(priv, XREG_ADC_ENV, adc_envelope);
  368. }
  369. static int xc_get_frequency_error(struct xc5000_priv *priv, u32 *freq_error_hz)
  370. {
  371. int result;
  372. u16 regData;
  373. u32 tmp;
  374. result = xc_read_reg(priv, XREG_FREQ_ERROR, &regData);
  375. if (result)
  376. return result;
  377. tmp = (u32)regData;
  378. (*freq_error_hz) = (tmp * 15625) / 1000;
  379. return result;
  380. }
  381. static int xc_get_lock_status(struct xc5000_priv *priv, u16 *lock_status)
  382. {
  383. return xc_read_reg(priv, XREG_LOCK, lock_status);
  384. }
  385. static int xc_get_version(struct xc5000_priv *priv,
  386. u8 *hw_majorversion, u8 *hw_minorversion,
  387. u8 *fw_majorversion, u8 *fw_minorversion)
  388. {
  389. u16 data;
  390. int result;
  391. result = xc_read_reg(priv, XREG_VERSION, &data);
  392. if (result)
  393. return result;
  394. (*hw_majorversion) = (data >> 12) & 0x0F;
  395. (*hw_minorversion) = (data >> 8) & 0x0F;
  396. (*fw_majorversion) = (data >> 4) & 0x0F;
  397. (*fw_minorversion) = data & 0x0F;
  398. return 0;
  399. }
  400. static int xc_get_hsync_freq(struct xc5000_priv *priv, u32 *hsync_freq_hz)
  401. {
  402. u16 regData;
  403. int result;
  404. result = xc_read_reg(priv, XREG_HSYNC_FREQ, &regData);
  405. if (result)
  406. return result;
  407. (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
  408. return result;
  409. }
  410. static int xc_get_frame_lines(struct xc5000_priv *priv, u16 *frame_lines)
  411. {
  412. return xc_read_reg(priv, XREG_FRAME_LINES, frame_lines);
  413. }
  414. static int xc_get_quality(struct xc5000_priv *priv, u16 *quality)
  415. {
  416. return xc_read_reg(priv, XREG_QUALITY, quality);
  417. }
  418. static u16 WaitForLock(struct xc5000_priv *priv)
  419. {
  420. u16 lockState = 0;
  421. int watchDogCount = 40;
  422. while ((lockState == 0) && (watchDogCount > 0)) {
  423. xc_get_lock_status(priv, &lockState);
  424. if (lockState != 1) {
  425. xc_wait(5);
  426. watchDogCount--;
  427. }
  428. }
  429. return lockState;
  430. }
  431. static int xc_tune_channel(struct xc5000_priv *priv, u32 freq_hz)
  432. {
  433. int found = 0;
  434. dprintk(1, "%s(%u)\n", __func__, freq_hz);
  435. if (xc_set_RF_frequency(priv, freq_hz) != XC_RESULT_SUCCESS)
  436. return 0;
  437. if (WaitForLock(priv) == 1)
  438. found = 1;
  439. return found;
  440. }
  441. static int xc5000_readreg(struct xc5000_priv *priv, u16 reg, u16 *val)
  442. {
  443. u8 buf[2] = { reg >> 8, reg & 0xff };
  444. u8 bval[2] = { 0, 0 };
  445. struct i2c_msg msg[2] = {
  446. { .addr = priv->i2c_props.addr,
  447. .flags = 0, .buf = &buf[0], .len = 2 },
  448. { .addr = priv->i2c_props.addr,
  449. .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
  450. };
  451. if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
  452. printk(KERN_WARNING "xc5000: I2C read failed\n");
  453. return -EREMOTEIO;
  454. }
  455. *val = (bval[0] << 8) | bval[1];
  456. return 0;
  457. }
  458. static int xc5000_writeregs(struct xc5000_priv *priv, u8 *buf, u8 len)
  459. {
  460. struct i2c_msg msg = { .addr = priv->i2c_props.addr,
  461. .flags = 0, .buf = buf, .len = len };
  462. if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
  463. printk(KERN_ERR "xc5000: I2C write failed (len=%i)\n",
  464. (int)len);
  465. return -EREMOTEIO;
  466. }
  467. return 0;
  468. }
  469. static int xc5000_readregs(struct xc5000_priv *priv, u8 *buf, u8 len)
  470. {
  471. struct i2c_msg msg = { .addr = priv->i2c_props.addr,
  472. .flags = I2C_M_RD, .buf = buf, .len = len };
  473. if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
  474. printk(KERN_ERR "xc5000 I2C read failed (len=%i)\n", (int)len);
  475. return -EREMOTEIO;
  476. }
  477. return 0;
  478. }
  479. static int xc5000_fwupload(struct dvb_frontend *fe)
  480. {
  481. struct xc5000_priv *priv = fe->tuner_priv;
  482. const struct firmware *fw;
  483. int ret;
  484. /* request the firmware, this will block and timeout */
  485. printk(KERN_INFO "xc5000: waiting for firmware upload (%s)...\n",
  486. XC5000_DEFAULT_FIRMWARE);
  487. ret = request_firmware(&fw, XC5000_DEFAULT_FIRMWARE,
  488. &priv->i2c_props.adap->dev);
  489. if (ret) {
  490. printk(KERN_ERR "xc5000: Upload failed. (file not found?)\n");
  491. ret = XC_RESULT_RESET_FAILURE;
  492. goto out;
  493. } else {
  494. printk(KERN_INFO "xc5000: firmware read %Zu bytes.\n",
  495. fw->size);
  496. ret = XC_RESULT_SUCCESS;
  497. }
  498. if (fw->size != XC5000_DEFAULT_FIRMWARE_SIZE) {
  499. printk(KERN_ERR "xc5000: firmware incorrect size\n");
  500. ret = XC_RESULT_RESET_FAILURE;
  501. } else {
  502. printk(KERN_INFO "xc5000: firmware upload\n");
  503. ret = xc_load_i2c_sequence(fe, fw->data);
  504. }
  505. out:
  506. release_firmware(fw);
  507. return ret;
  508. }
  509. static void xc_debug_dump(struct xc5000_priv *priv)
  510. {
  511. u16 adc_envelope;
  512. u32 freq_error_hz = 0;
  513. u16 lock_status;
  514. u32 hsync_freq_hz = 0;
  515. u16 frame_lines;
  516. u16 quality;
  517. u8 hw_majorversion = 0, hw_minorversion = 0;
  518. u8 fw_majorversion = 0, fw_minorversion = 0;
  519. /* Wait for stats to stabilize.
  520. * Frame Lines needs two frame times after initial lock
  521. * before it is valid.
  522. */
  523. xc_wait(100);
  524. xc_get_ADC_Envelope(priv, &adc_envelope);
  525. dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
  526. xc_get_frequency_error(priv, &freq_error_hz);
  527. dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
  528. xc_get_lock_status(priv, &lock_status);
  529. dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
  530. lock_status);
  531. xc_get_version(priv, &hw_majorversion, &hw_minorversion,
  532. &fw_majorversion, &fw_minorversion);
  533. dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
  534. hw_majorversion, hw_minorversion,
  535. fw_majorversion, fw_minorversion);
  536. xc_get_hsync_freq(priv, &hsync_freq_hz);
  537. dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
  538. xc_get_frame_lines(priv, &frame_lines);
  539. dprintk(1, "*** Frame lines = %d\n", frame_lines);
  540. xc_get_quality(priv, &quality);
  541. dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
  542. }
  543. static int xc5000_set_params(struct dvb_frontend *fe,
  544. struct dvb_frontend_parameters *params)
  545. {
  546. struct xc5000_priv *priv = fe->tuner_priv;
  547. int ret;
  548. dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
  549. switch (params->u.vsb.modulation) {
  550. case VSB_8:
  551. case VSB_16:
  552. dprintk(1, "%s() VSB modulation\n", __func__);
  553. priv->rf_mode = XC_RF_MODE_AIR;
  554. priv->freq_hz = params->frequency - 1750000;
  555. priv->bandwidth = BANDWIDTH_6_MHZ;
  556. priv->video_standard = DTV6;
  557. break;
  558. case QAM_64:
  559. case QAM_256:
  560. case QAM_AUTO:
  561. dprintk(1, "%s() QAM modulation\n", __func__);
  562. priv->rf_mode = XC_RF_MODE_CABLE;
  563. priv->freq_hz = params->frequency - 1750000;
  564. priv->bandwidth = BANDWIDTH_6_MHZ;
  565. priv->video_standard = DTV6;
  566. break;
  567. default:
  568. return -EINVAL;
  569. }
  570. dprintk(1, "%s() frequency=%d (compensated)\n",
  571. __func__, priv->freq_hz);
  572. ret = xc_SetSignalSource(priv, priv->rf_mode);
  573. if (ret != XC_RESULT_SUCCESS) {
  574. printk(KERN_ERR
  575. "xc5000: xc_SetSignalSource(%d) failed\n",
  576. priv->rf_mode);
  577. return -EREMOTEIO;
  578. }
  579. ret = xc_SetTVStandard(priv,
  580. XC5000_Standard[priv->video_standard].VideoMode,
  581. XC5000_Standard[priv->video_standard].AudioMode);
  582. if (ret != XC_RESULT_SUCCESS) {
  583. printk(KERN_ERR "xc5000: xc_SetTVStandard failed\n");
  584. return -EREMOTEIO;
  585. }
  586. ret = xc_set_IF_frequency(priv, priv->if_khz);
  587. if (ret != XC_RESULT_SUCCESS) {
  588. printk(KERN_ERR "xc5000: xc_Set_IF_frequency(%d) failed\n",
  589. priv->if_khz);
  590. return -EIO;
  591. }
  592. xc_tune_channel(priv, priv->freq_hz);
  593. if (debug)
  594. xc_debug_dump(priv);
  595. return 0;
  596. }
  597. static int xc5000_is_firmware_loaded(struct dvb_frontend *fe)
  598. {
  599. struct xc5000_priv *priv = fe->tuner_priv;
  600. int ret;
  601. u16 id;
  602. ret = xc5000_readreg(priv, XREG_PRODUCT_ID, &id);
  603. if (ret == XC_RESULT_SUCCESS) {
  604. if (id == XC_PRODUCT_ID_FW_NOT_LOADED)
  605. ret = XC_RESULT_RESET_FAILURE;
  606. else
  607. ret = XC_RESULT_SUCCESS;
  608. }
  609. dprintk(1, "%s() returns %s id = 0x%x\n", __func__,
  610. ret == XC_RESULT_SUCCESS ? "True" : "False", id);
  611. return ret;
  612. }
  613. static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe);
  614. static int xc5000_set_analog_params(struct dvb_frontend *fe,
  615. struct analog_parameters *params)
  616. {
  617. struct xc5000_priv *priv = fe->tuner_priv;
  618. int ret;
  619. if (xc5000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS)
  620. xc_load_fw_and_init_tuner(fe);
  621. dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
  622. __func__, params->frequency);
  623. priv->rf_mode = XC_RF_MODE_CABLE; /* Fix me: it could be air. */
  624. /* params->frequency is in units of 62.5khz */
  625. priv->freq_hz = params->frequency * 62500;
  626. /* FIX ME: Some video standards may have several possible audio
  627. standards. We simply default to one of them here.
  628. */
  629. if (params->std & V4L2_STD_MN) {
  630. /* default to BTSC audio standard */
  631. priv->video_standard = MN_NTSC_PAL_BTSC;
  632. goto tune_channel;
  633. }
  634. if (params->std & V4L2_STD_PAL_BG) {
  635. /* default to NICAM audio standard */
  636. priv->video_standard = BG_PAL_NICAM;
  637. goto tune_channel;
  638. }
  639. if (params->std & V4L2_STD_PAL_I) {
  640. /* default to NICAM audio standard */
  641. priv->video_standard = I_PAL_NICAM;
  642. goto tune_channel;
  643. }
  644. if (params->std & V4L2_STD_PAL_DK) {
  645. /* default to NICAM audio standard */
  646. priv->video_standard = DK_PAL_NICAM;
  647. goto tune_channel;
  648. }
  649. if (params->std & V4L2_STD_SECAM_DK) {
  650. /* default to A2 DK1 audio standard */
  651. priv->video_standard = DK_SECAM_A2DK1;
  652. goto tune_channel;
  653. }
  654. if (params->std & V4L2_STD_SECAM_L) {
  655. priv->video_standard = L_SECAM_NICAM;
  656. goto tune_channel;
  657. }
  658. if (params->std & V4L2_STD_SECAM_LC) {
  659. priv->video_standard = LC_SECAM_NICAM;
  660. goto tune_channel;
  661. }
  662. tune_channel:
  663. ret = xc_SetSignalSource(priv, priv->rf_mode);
  664. if (ret != XC_RESULT_SUCCESS) {
  665. printk(KERN_ERR
  666. "xc5000: xc_SetSignalSource(%d) failed\n",
  667. priv->rf_mode);
  668. return -EREMOTEIO;
  669. }
  670. ret = xc_SetTVStandard(priv,
  671. XC5000_Standard[priv->video_standard].VideoMode,
  672. XC5000_Standard[priv->video_standard].AudioMode);
  673. if (ret != XC_RESULT_SUCCESS) {
  674. printk(KERN_ERR "xc5000: xc_SetTVStandard failed\n");
  675. return -EREMOTEIO;
  676. }
  677. xc_tune_channel(priv, priv->freq_hz);
  678. if (debug)
  679. xc_debug_dump(priv);
  680. return 0;
  681. }
  682. static int xc5000_get_frequency(struct dvb_frontend *fe, u32 *freq)
  683. {
  684. struct xc5000_priv *priv = fe->tuner_priv;
  685. dprintk(1, "%s()\n", __func__);
  686. *freq = priv->freq_hz;
  687. return 0;
  688. }
  689. static int xc5000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
  690. {
  691. struct xc5000_priv *priv = fe->tuner_priv;
  692. dprintk(1, "%s()\n", __func__);
  693. *bw = priv->bandwidth;
  694. return 0;
  695. }
  696. static int xc5000_get_status(struct dvb_frontend *fe, u32 *status)
  697. {
  698. struct xc5000_priv *priv = fe->tuner_priv;
  699. u16 lock_status = 0;
  700. xc_get_lock_status(priv, &lock_status);
  701. dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
  702. *status = lock_status;
  703. return 0;
  704. }
  705. static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe)
  706. {
  707. struct xc5000_priv *priv = fe->tuner_priv;
  708. int ret = 0;
  709. if (xc5000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS) {
  710. ret = xc5000_fwupload(fe);
  711. if (ret != XC_RESULT_SUCCESS)
  712. return ret;
  713. }
  714. /* Start the tuner self-calibration process */
  715. ret |= xc_initialize(priv);
  716. /* Wait for calibration to complete.
  717. * We could continue but XC5000 will clock stretch subsequent
  718. * I2C transactions until calibration is complete. This way we
  719. * don't have to rely on clock stretching working.
  720. */
  721. xc_wait(100);
  722. /* Default to "CABLE" mode */
  723. ret |= xc_write_reg(priv, XREG_SIGNALSOURCE, XC_RF_MODE_CABLE);
  724. return ret;
  725. }
  726. static int xc5000_sleep(struct dvb_frontend *fe)
  727. {
  728. struct xc5000_priv *priv = fe->tuner_priv;
  729. int ret;
  730. dprintk(1, "%s()\n", __func__);
  731. /* On Pinnacle PCTV HD 800i, the tuner cannot be reinitialized
  732. * once shutdown without reloading the driver. Maybe I am not
  733. * doing something right.
  734. *
  735. */
  736. ret = xc_shutdown(priv);
  737. if (ret != XC_RESULT_SUCCESS) {
  738. printk(KERN_ERR
  739. "xc5000: %s() unable to shutdown tuner\n",
  740. __func__);
  741. return -EREMOTEIO;
  742. } else
  743. return XC_RESULT_SUCCESS;
  744. }
  745. static int xc5000_init(struct dvb_frontend *fe)
  746. {
  747. struct xc5000_priv *priv = fe->tuner_priv;
  748. dprintk(1, "%s()\n", __func__);
  749. if (xc_load_fw_and_init_tuner(fe) != XC_RESULT_SUCCESS) {
  750. printk(KERN_ERR "xc5000: Unable to initialise tuner\n");
  751. return -EREMOTEIO;
  752. }
  753. if (debug)
  754. xc_debug_dump(priv);
  755. return 0;
  756. }
  757. static int xc5000_release(struct dvb_frontend *fe)
  758. {
  759. struct xc5000_priv *priv = fe->tuner_priv;
  760. dprintk(1, "%s()\n", __func__);
  761. mutex_lock(&xc5000_list_mutex);
  762. if (priv)
  763. hybrid_tuner_release_state(priv);
  764. mutex_unlock(&xc5000_list_mutex);
  765. fe->tuner_priv = NULL;
  766. return 0;
  767. }
  768. static const struct dvb_tuner_ops xc5000_tuner_ops = {
  769. .info = {
  770. .name = "Xceive XC5000",
  771. .frequency_min = 1000000,
  772. .frequency_max = 1023000000,
  773. .frequency_step = 50000,
  774. },
  775. .release = xc5000_release,
  776. .init = xc5000_init,
  777. .sleep = xc5000_sleep,
  778. .set_params = xc5000_set_params,
  779. .set_analog_params = xc5000_set_analog_params,
  780. .get_frequency = xc5000_get_frequency,
  781. .get_bandwidth = xc5000_get_bandwidth,
  782. .get_status = xc5000_get_status
  783. };
  784. struct dvb_frontend *xc5000_attach(struct dvb_frontend *fe,
  785. struct i2c_adapter *i2c,
  786. struct xc5000_config *cfg)
  787. {
  788. struct xc5000_priv *priv = NULL;
  789. int instance;
  790. u16 id = 0;
  791. dprintk(1, "%s(%d-%04x)\n", __func__,
  792. i2c ? i2c_adapter_id(i2c) : -1,
  793. cfg ? cfg->i2c_address : -1);
  794. mutex_lock(&xc5000_list_mutex);
  795. instance = hybrid_tuner_request_state(struct xc5000_priv, priv,
  796. hybrid_tuner_instance_list,
  797. i2c, cfg->i2c_address, "xc5000");
  798. switch (instance) {
  799. case 0:
  800. goto fail;
  801. break;
  802. case 1:
  803. /* new tuner instance */
  804. priv->bandwidth = BANDWIDTH_6_MHZ;
  805. priv->if_khz = cfg->if_khz;
  806. fe->tuner_priv = priv;
  807. break;
  808. default:
  809. /* existing tuner instance */
  810. fe->tuner_priv = priv;
  811. break;
  812. }
  813. /* Check if firmware has been loaded. It is possible that another
  814. instance of the driver has loaded the firmware.
  815. */
  816. if (xc5000_readreg(priv, XREG_PRODUCT_ID, &id) != 0)
  817. goto fail;
  818. switch (id) {
  819. case XC_PRODUCT_ID_FW_LOADED:
  820. printk(KERN_INFO
  821. "xc5000: Successfully identified at address 0x%02x\n",
  822. cfg->i2c_address);
  823. printk(KERN_INFO
  824. "xc5000: Firmware has been loaded previously\n");
  825. break;
  826. case XC_PRODUCT_ID_FW_NOT_LOADED:
  827. printk(KERN_INFO
  828. "xc5000: Successfully identified at address 0x%02x\n",
  829. cfg->i2c_address);
  830. printk(KERN_INFO
  831. "xc5000: Firmware has not been loaded previously\n");
  832. break;
  833. default:
  834. printk(KERN_ERR
  835. "xc5000: Device not found at addr 0x%02x (0x%x)\n",
  836. cfg->i2c_address, id);
  837. goto fail;
  838. }
  839. mutex_unlock(&xc5000_list_mutex);
  840. memcpy(&fe->ops.tuner_ops, &xc5000_tuner_ops,
  841. sizeof(struct dvb_tuner_ops));
  842. if (xc5000_load_fw_on_attach)
  843. xc5000_init(fe);
  844. return fe;
  845. fail:
  846. mutex_unlock(&xc5000_list_mutex);
  847. xc5000_release(fe);
  848. return NULL;
  849. }
  850. EXPORT_SYMBOL(xc5000_attach);
  851. MODULE_AUTHOR("Steven Toth");
  852. MODULE_DESCRIPTION("Xceive xc5000 silicon tuner driver");
  853. MODULE_LICENSE("GPL");