mxl5007t.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*
  2. * mxl5007t.c - driver for the MaxLinear MxL5007T silicon tuner
  3. *
  4. * Copyright (C) 2008, 2009 Michael Krufky <mkrufky@linuxtv.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/i2c.h>
  21. #include <linux/types.h>
  22. #include <linux/videodev2.h>
  23. #include "tuner-i2c.h"
  24. #include "mxl5007t.h"
  25. static DEFINE_MUTEX(mxl5007t_list_mutex);
  26. static LIST_HEAD(hybrid_tuner_instance_list);
  27. static int mxl5007t_debug;
  28. module_param_named(debug, mxl5007t_debug, int, 0644);
  29. MODULE_PARM_DESC(debug, "set debug level");
  30. /* ------------------------------------------------------------------------- */
  31. #define mxl_printk(kern, fmt, arg...) \
  32. printk(kern "%s: " fmt "\n", __func__, ##arg)
  33. #define mxl_err(fmt, arg...) \
  34. mxl_printk(KERN_ERR, "%d: " fmt, __LINE__, ##arg)
  35. #define mxl_warn(fmt, arg...) \
  36. mxl_printk(KERN_WARNING, fmt, ##arg)
  37. #define mxl_info(fmt, arg...) \
  38. mxl_printk(KERN_INFO, fmt, ##arg)
  39. #define mxl_debug(fmt, arg...) \
  40. ({ \
  41. if (mxl5007t_debug) \
  42. mxl_printk(KERN_DEBUG, fmt, ##arg); \
  43. })
  44. #define mxl_fail(ret) \
  45. ({ \
  46. int __ret; \
  47. __ret = (ret < 0); \
  48. if (__ret) \
  49. mxl_printk(KERN_ERR, "error %d on line %d", \
  50. ret, __LINE__); \
  51. __ret; \
  52. })
  53. /* ------------------------------------------------------------------------- */
  54. #define MHz 1000000
  55. enum mxl5007t_mode {
  56. MxL_MODE_ISDBT = 0,
  57. MxL_MODE_DVBT = 1,
  58. MxL_MODE_ATSC = 2,
  59. MxL_MODE_CABLE = 0x10,
  60. };
  61. enum mxl5007t_chip_version {
  62. MxL_UNKNOWN_ID = 0x00,
  63. MxL_5007_V1_F1 = 0x11,
  64. MxL_5007_V1_F2 = 0x12,
  65. MxL_5007_V4 = 0x14,
  66. MxL_5007_V2_100_F1 = 0x21,
  67. MxL_5007_V2_100_F2 = 0x22,
  68. MxL_5007_V2_200_F1 = 0x23,
  69. MxL_5007_V2_200_F2 = 0x24,
  70. };
  71. struct reg_pair_t {
  72. u8 reg;
  73. u8 val;
  74. };
  75. /* ------------------------------------------------------------------------- */
  76. static struct reg_pair_t init_tab[] = {
  77. { 0x02, 0x06 },
  78. { 0x03, 0x48 },
  79. { 0x05, 0x04 },
  80. { 0x06, 0x10 },
  81. { 0x2e, 0x15 }, /* OVERRIDE */
  82. { 0x30, 0x10 }, /* OVERRIDE */
  83. { 0x45, 0x58 }, /* OVERRIDE */
  84. { 0x48, 0x19 }, /* OVERRIDE */
  85. { 0x52, 0x03 }, /* OVERRIDE */
  86. { 0x53, 0x44 }, /* OVERRIDE */
  87. { 0x6a, 0x4b }, /* OVERRIDE */
  88. { 0x76, 0x00 }, /* OVERRIDE */
  89. { 0x78, 0x18 }, /* OVERRIDE */
  90. { 0x7a, 0x17 }, /* OVERRIDE */
  91. { 0x85, 0x06 }, /* OVERRIDE */
  92. { 0x01, 0x01 }, /* TOP_MASTER_ENABLE */
  93. { 0, 0 }
  94. };
  95. static struct reg_pair_t init_tab_cable[] = {
  96. { 0x02, 0x06 },
  97. { 0x03, 0x48 },
  98. { 0x05, 0x04 },
  99. { 0x06, 0x10 },
  100. { 0x09, 0x3f },
  101. { 0x0a, 0x3f },
  102. { 0x0b, 0x3f },
  103. { 0x2e, 0x15 }, /* OVERRIDE */
  104. { 0x30, 0x10 }, /* OVERRIDE */
  105. { 0x45, 0x58 }, /* OVERRIDE */
  106. { 0x48, 0x19 }, /* OVERRIDE */
  107. { 0x52, 0x03 }, /* OVERRIDE */
  108. { 0x53, 0x44 }, /* OVERRIDE */
  109. { 0x6a, 0x4b }, /* OVERRIDE */
  110. { 0x76, 0x00 }, /* OVERRIDE */
  111. { 0x78, 0x18 }, /* OVERRIDE */
  112. { 0x7a, 0x17 }, /* OVERRIDE */
  113. { 0x85, 0x06 }, /* OVERRIDE */
  114. { 0x01, 0x01 }, /* TOP_MASTER_ENABLE */
  115. { 0, 0 }
  116. };
  117. /* ------------------------------------------------------------------------- */
  118. static struct reg_pair_t reg_pair_rftune[] = {
  119. { 0x0f, 0x00 }, /* abort tune */
  120. { 0x0c, 0x15 },
  121. { 0x0d, 0x40 },
  122. { 0x0e, 0x0e },
  123. { 0x1f, 0x87 }, /* OVERRIDE */
  124. { 0x20, 0x1f }, /* OVERRIDE */
  125. { 0x21, 0x87 }, /* OVERRIDE */
  126. { 0x22, 0x1f }, /* OVERRIDE */
  127. { 0x80, 0x01 }, /* freq dependent */
  128. { 0x0f, 0x01 }, /* start tune */
  129. { 0, 0 }
  130. };
  131. /* ------------------------------------------------------------------------- */
  132. struct mxl5007t_state {
  133. struct list_head hybrid_tuner_instance_list;
  134. struct tuner_i2c_props i2c_props;
  135. struct mutex lock;
  136. struct mxl5007t_config *config;
  137. enum mxl5007t_chip_version chip_id;
  138. struct reg_pair_t tab_init[ARRAY_SIZE(init_tab)];
  139. struct reg_pair_t tab_init_cable[ARRAY_SIZE(init_tab_cable)];
  140. struct reg_pair_t tab_rftune[ARRAY_SIZE(reg_pair_rftune)];
  141. enum mxl5007t_if_freq if_freq;
  142. u32 frequency;
  143. u32 bandwidth;
  144. };
  145. /* ------------------------------------------------------------------------- */
  146. /* called by _init and _rftun to manipulate the register arrays */
  147. static void set_reg_bits(struct reg_pair_t *reg_pair, u8 reg, u8 mask, u8 val)
  148. {
  149. unsigned int i = 0;
  150. while (reg_pair[i].reg || reg_pair[i].val) {
  151. if (reg_pair[i].reg == reg) {
  152. reg_pair[i].val &= ~mask;
  153. reg_pair[i].val |= val;
  154. }
  155. i++;
  156. }
  157. return;
  158. }
  159. static void copy_reg_bits(struct reg_pair_t *reg_pair1,
  160. struct reg_pair_t *reg_pair2)
  161. {
  162. unsigned int i, j;
  163. i = j = 0;
  164. while (reg_pair1[i].reg || reg_pair1[i].val) {
  165. while (reg_pair2[j].reg || reg_pair2[j].val) {
  166. if (reg_pair1[i].reg != reg_pair2[j].reg) {
  167. j++;
  168. continue;
  169. }
  170. reg_pair2[j].val = reg_pair1[i].val;
  171. break;
  172. }
  173. i++;
  174. }
  175. return;
  176. }
  177. /* ------------------------------------------------------------------------- */
  178. static void mxl5007t_set_mode_bits(struct mxl5007t_state *state,
  179. enum mxl5007t_mode mode,
  180. s32 if_diff_out_level)
  181. {
  182. switch (mode) {
  183. case MxL_MODE_ATSC:
  184. set_reg_bits(state->tab_init, 0x06, 0x1f, 0x12);
  185. break;
  186. case MxL_MODE_DVBT:
  187. set_reg_bits(state->tab_init, 0x06, 0x1f, 0x11);
  188. break;
  189. case MxL_MODE_ISDBT:
  190. set_reg_bits(state->tab_init, 0x06, 0x1f, 0x10);
  191. break;
  192. case MxL_MODE_CABLE:
  193. set_reg_bits(state->tab_init_cable, 0x09, 0xff, 0xc1);
  194. set_reg_bits(state->tab_init_cable, 0x0a, 0xff,
  195. 8 - if_diff_out_level);
  196. set_reg_bits(state->tab_init_cable, 0x0b, 0xff, 0x17);
  197. break;
  198. default:
  199. mxl_fail(-EINVAL);
  200. }
  201. return;
  202. }
  203. static void mxl5007t_set_if_freq_bits(struct mxl5007t_state *state,
  204. enum mxl5007t_if_freq if_freq,
  205. int invert_if)
  206. {
  207. u8 val;
  208. switch (if_freq) {
  209. case MxL_IF_4_MHZ:
  210. val = 0x00;
  211. break;
  212. case MxL_IF_4_5_MHZ:
  213. val = 0x02;
  214. break;
  215. case MxL_IF_4_57_MHZ:
  216. val = 0x03;
  217. break;
  218. case MxL_IF_5_MHZ:
  219. val = 0x04;
  220. break;
  221. case MxL_IF_5_38_MHZ:
  222. val = 0x05;
  223. break;
  224. case MxL_IF_6_MHZ:
  225. val = 0x06;
  226. break;
  227. case MxL_IF_6_28_MHZ:
  228. val = 0x07;
  229. break;
  230. case MxL_IF_9_1915_MHZ:
  231. val = 0x08;
  232. break;
  233. case MxL_IF_35_25_MHZ:
  234. val = 0x09;
  235. break;
  236. case MxL_IF_36_15_MHZ:
  237. val = 0x0a;
  238. break;
  239. case MxL_IF_44_MHZ:
  240. val = 0x0b;
  241. break;
  242. default:
  243. mxl_fail(-EINVAL);
  244. return;
  245. }
  246. set_reg_bits(state->tab_init, 0x02, 0x0f, val);
  247. /* set inverted IF or normal IF */
  248. set_reg_bits(state->tab_init, 0x02, 0x10, invert_if ? 0x10 : 0x00);
  249. state->if_freq = if_freq;
  250. return;
  251. }
  252. static void mxl5007t_set_xtal_freq_bits(struct mxl5007t_state *state,
  253. enum mxl5007t_xtal_freq xtal_freq)
  254. {
  255. switch (xtal_freq) {
  256. case MxL_XTAL_16_MHZ:
  257. /* select xtal freq & ref freq */
  258. set_reg_bits(state->tab_init, 0x03, 0xf0, 0x00);
  259. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x00);
  260. break;
  261. case MxL_XTAL_20_MHZ:
  262. set_reg_bits(state->tab_init, 0x03, 0xf0, 0x10);
  263. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x01);
  264. break;
  265. case MxL_XTAL_20_25_MHZ:
  266. set_reg_bits(state->tab_init, 0x03, 0xf0, 0x20);
  267. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x02);
  268. break;
  269. case MxL_XTAL_20_48_MHZ:
  270. set_reg_bits(state->tab_init, 0x03, 0xf0, 0x30);
  271. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x03);
  272. break;
  273. case MxL_XTAL_24_MHZ:
  274. set_reg_bits(state->tab_init, 0x03, 0xf0, 0x40);
  275. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x04);
  276. break;
  277. case MxL_XTAL_25_MHZ:
  278. set_reg_bits(state->tab_init, 0x03, 0xf0, 0x50);
  279. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x05);
  280. break;
  281. case MxL_XTAL_25_14_MHZ:
  282. set_reg_bits(state->tab_init, 0x03, 0xf0, 0x60);
  283. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x06);
  284. break;
  285. case MxL_XTAL_27_MHZ:
  286. set_reg_bits(state->tab_init, 0x03, 0xf0, 0x70);
  287. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x07);
  288. break;
  289. case MxL_XTAL_28_8_MHZ:
  290. set_reg_bits(state->tab_init, 0x03, 0xf0, 0x80);
  291. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x08);
  292. break;
  293. case MxL_XTAL_32_MHZ:
  294. set_reg_bits(state->tab_init, 0x03, 0xf0, 0x90);
  295. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x09);
  296. break;
  297. case MxL_XTAL_40_MHZ:
  298. set_reg_bits(state->tab_init, 0x03, 0xf0, 0xa0);
  299. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0a);
  300. break;
  301. case MxL_XTAL_44_MHZ:
  302. set_reg_bits(state->tab_init, 0x03, 0xf0, 0xb0);
  303. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0b);
  304. break;
  305. case MxL_XTAL_48_MHZ:
  306. set_reg_bits(state->tab_init, 0x03, 0xf0, 0xc0);
  307. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0c);
  308. break;
  309. case MxL_XTAL_49_3811_MHZ:
  310. set_reg_bits(state->tab_init, 0x03, 0xf0, 0xd0);
  311. set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0d);
  312. break;
  313. default:
  314. mxl_fail(-EINVAL);
  315. return;
  316. }
  317. return;
  318. }
  319. static struct reg_pair_t *mxl5007t_calc_init_regs(struct mxl5007t_state *state,
  320. enum mxl5007t_mode mode)
  321. {
  322. struct mxl5007t_config *cfg = state->config;
  323. memcpy(&state->tab_init, &init_tab, sizeof(init_tab));
  324. memcpy(&state->tab_init_cable, &init_tab_cable, sizeof(init_tab_cable));
  325. mxl5007t_set_mode_bits(state, mode, cfg->if_diff_out_level);
  326. mxl5007t_set_if_freq_bits(state, cfg->if_freq_hz, cfg->invert_if);
  327. mxl5007t_set_xtal_freq_bits(state, cfg->xtal_freq_hz);
  328. set_reg_bits(state->tab_init, 0x03, 0x08, cfg->clk_out_enable << 3);
  329. set_reg_bits(state->tab_init, 0x03, 0x07, cfg->clk_out_amp);
  330. if (mode >= MxL_MODE_CABLE) {
  331. copy_reg_bits(state->tab_init, state->tab_init_cable);
  332. return state->tab_init_cable;
  333. } else
  334. return state->tab_init;
  335. }
  336. /* ------------------------------------------------------------------------- */
  337. enum mxl5007t_bw_mhz {
  338. MxL_BW_6MHz = 6,
  339. MxL_BW_7MHz = 7,
  340. MxL_BW_8MHz = 8,
  341. };
  342. static void mxl5007t_set_bw_bits(struct mxl5007t_state *state,
  343. enum mxl5007t_bw_mhz bw)
  344. {
  345. u8 val;
  346. switch (bw) {
  347. case MxL_BW_6MHz:
  348. val = 0x15; /* set DIG_MODEINDEX, DIG_MODEINDEX_A,
  349. * and DIG_MODEINDEX_CSF */
  350. break;
  351. case MxL_BW_7MHz:
  352. val = 0x2a;
  353. break;
  354. case MxL_BW_8MHz:
  355. val = 0x3f;
  356. break;
  357. default:
  358. mxl_fail(-EINVAL);
  359. return;
  360. }
  361. set_reg_bits(state->tab_rftune, 0x0c, 0x3f, val);
  362. return;
  363. }
  364. static struct
  365. reg_pair_t *mxl5007t_calc_rf_tune_regs(struct mxl5007t_state *state,
  366. u32 rf_freq, enum mxl5007t_bw_mhz bw)
  367. {
  368. u32 dig_rf_freq = 0;
  369. u32 temp;
  370. u32 frac_divider = 1000000;
  371. unsigned int i;
  372. memcpy(&state->tab_rftune, &reg_pair_rftune, sizeof(reg_pair_rftune));
  373. mxl5007t_set_bw_bits(state, bw);
  374. /* Convert RF frequency into 16 bits =>
  375. * 10 bit integer (MHz) + 6 bit fraction */
  376. dig_rf_freq = rf_freq / MHz;
  377. temp = rf_freq % MHz;
  378. for (i = 0; i < 6; i++) {
  379. dig_rf_freq <<= 1;
  380. frac_divider /= 2;
  381. if (temp > frac_divider) {
  382. temp -= frac_divider;
  383. dig_rf_freq++;
  384. }
  385. }
  386. /* add to have shift center point by 7.8124 kHz */
  387. if (temp > 7812)
  388. dig_rf_freq++;
  389. set_reg_bits(state->tab_rftune, 0x0d, 0xff, (u8) dig_rf_freq);
  390. set_reg_bits(state->tab_rftune, 0x0e, 0xff, (u8) (dig_rf_freq >> 8));
  391. if (rf_freq >= 333000000)
  392. set_reg_bits(state->tab_rftune, 0x80, 0x40, 0x40);
  393. return state->tab_rftune;
  394. }
  395. /* ------------------------------------------------------------------------- */
  396. static int mxl5007t_write_reg(struct mxl5007t_state *state, u8 reg, u8 val)
  397. {
  398. u8 buf[] = { reg, val };
  399. struct i2c_msg msg = { .addr = state->i2c_props.addr, .flags = 0,
  400. .buf = buf, .len = 2 };
  401. int ret;
  402. ret = i2c_transfer(state->i2c_props.adap, &msg, 1);
  403. if (ret != 1) {
  404. mxl_err("failed!");
  405. return -EREMOTEIO;
  406. }
  407. return 0;
  408. }
  409. static int mxl5007t_write_regs(struct mxl5007t_state *state,
  410. struct reg_pair_t *reg_pair)
  411. {
  412. unsigned int i = 0;
  413. int ret = 0;
  414. while ((ret == 0) && (reg_pair[i].reg || reg_pair[i].val)) {
  415. ret = mxl5007t_write_reg(state,
  416. reg_pair[i].reg, reg_pair[i].val);
  417. i++;
  418. }
  419. return ret;
  420. }
  421. static int mxl5007t_read_reg(struct mxl5007t_state *state, u8 reg, u8 *val)
  422. {
  423. u8 buf[2] = { 0xfb, reg };
  424. struct i2c_msg msg[] = {
  425. { .addr = state->i2c_props.addr, .flags = 0,
  426. .buf = buf, .len = 2 },
  427. { .addr = state->i2c_props.addr, .flags = I2C_M_RD,
  428. .buf = val, .len = 1 },
  429. };
  430. int ret;
  431. ret = i2c_transfer(state->i2c_props.adap, msg, 2);
  432. if (ret != 2) {
  433. mxl_err("failed!");
  434. return -EREMOTEIO;
  435. }
  436. return 0;
  437. }
  438. static int mxl5007t_soft_reset(struct mxl5007t_state *state)
  439. {
  440. u8 d = 0xff;
  441. struct i2c_msg msg = {
  442. .addr = state->i2c_props.addr, .flags = 0,
  443. .buf = &d, .len = 1
  444. };
  445. int ret = i2c_transfer(state->i2c_props.adap, &msg, 1);
  446. if (ret != 1) {
  447. mxl_err("failed!");
  448. return -EREMOTEIO;
  449. }
  450. return 0;
  451. }
  452. static int mxl5007t_tuner_init(struct mxl5007t_state *state,
  453. enum mxl5007t_mode mode)
  454. {
  455. struct reg_pair_t *init_regs;
  456. int ret;
  457. /* calculate initialization reg array */
  458. init_regs = mxl5007t_calc_init_regs(state, mode);
  459. ret = mxl5007t_write_regs(state, init_regs);
  460. if (mxl_fail(ret))
  461. goto fail;
  462. mdelay(1);
  463. fail:
  464. return ret;
  465. }
  466. static int mxl5007t_tuner_rf_tune(struct mxl5007t_state *state, u32 rf_freq_hz,
  467. enum mxl5007t_bw_mhz bw)
  468. {
  469. struct reg_pair_t *rf_tune_regs;
  470. int ret;
  471. /* calculate channel change reg array */
  472. rf_tune_regs = mxl5007t_calc_rf_tune_regs(state, rf_freq_hz, bw);
  473. ret = mxl5007t_write_regs(state, rf_tune_regs);
  474. if (mxl_fail(ret))
  475. goto fail;
  476. msleep(3);
  477. fail:
  478. return ret;
  479. }
  480. /* ------------------------------------------------------------------------- */
  481. static int mxl5007t_synth_lock_status(struct mxl5007t_state *state,
  482. int *rf_locked, int *ref_locked)
  483. {
  484. u8 d;
  485. int ret;
  486. *rf_locked = 0;
  487. *ref_locked = 0;
  488. ret = mxl5007t_read_reg(state, 0xd8, &d);
  489. if (mxl_fail(ret))
  490. goto fail;
  491. if ((d & 0x0c) == 0x0c)
  492. *rf_locked = 1;
  493. if ((d & 0x03) == 0x03)
  494. *ref_locked = 1;
  495. fail:
  496. return ret;
  497. }
  498. /* ------------------------------------------------------------------------- */
  499. static int mxl5007t_get_status(struct dvb_frontend *fe, u32 *status)
  500. {
  501. struct mxl5007t_state *state = fe->tuner_priv;
  502. int rf_locked, ref_locked, ret;
  503. *status = 0;
  504. if (fe->ops.i2c_gate_ctrl)
  505. fe->ops.i2c_gate_ctrl(fe, 1);
  506. ret = mxl5007t_synth_lock_status(state, &rf_locked, &ref_locked);
  507. if (mxl_fail(ret))
  508. goto fail;
  509. mxl_debug("%s%s", rf_locked ? "rf locked " : "",
  510. ref_locked ? "ref locked" : "");
  511. if ((rf_locked) || (ref_locked))
  512. *status |= TUNER_STATUS_LOCKED;
  513. fail:
  514. if (fe->ops.i2c_gate_ctrl)
  515. fe->ops.i2c_gate_ctrl(fe, 0);
  516. return ret;
  517. }
  518. /* ------------------------------------------------------------------------- */
  519. static int mxl5007t_set_params(struct dvb_frontend *fe)
  520. {
  521. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  522. u32 delsys = c->delivery_system;
  523. struct mxl5007t_state *state = fe->tuner_priv;
  524. enum mxl5007t_bw_mhz bw;
  525. enum mxl5007t_mode mode;
  526. int ret;
  527. u32 freq = c->frequency;
  528. switch (delsys) {
  529. case SYS_ATSC:
  530. mode = MxL_MODE_ATSC;
  531. bw = MxL_BW_6MHz;
  532. break;
  533. case SYS_DVBC_ANNEX_B:
  534. mode = MxL_MODE_CABLE;
  535. bw = MxL_BW_6MHz;
  536. break;
  537. case SYS_DVBT:
  538. case SYS_DVBT2:
  539. mode = MxL_MODE_DVBT;
  540. switch (c->bandwidth_hz) {
  541. case 6000000:
  542. bw = MxL_BW_6MHz;
  543. break;
  544. case 7000000:
  545. bw = MxL_BW_7MHz;
  546. break;
  547. case 8000000:
  548. bw = MxL_BW_8MHz;
  549. break;
  550. default:
  551. return -EINVAL;
  552. }
  553. break;
  554. default:
  555. mxl_err("modulation type not supported!");
  556. return -EINVAL;
  557. }
  558. if (fe->ops.i2c_gate_ctrl)
  559. fe->ops.i2c_gate_ctrl(fe, 1);
  560. mutex_lock(&state->lock);
  561. ret = mxl5007t_tuner_init(state, mode);
  562. if (mxl_fail(ret))
  563. goto fail;
  564. ret = mxl5007t_tuner_rf_tune(state, freq, bw);
  565. if (mxl_fail(ret))
  566. goto fail;
  567. state->frequency = freq;
  568. state->bandwidth = c->bandwidth_hz;
  569. fail:
  570. mutex_unlock(&state->lock);
  571. if (fe->ops.i2c_gate_ctrl)
  572. fe->ops.i2c_gate_ctrl(fe, 0);
  573. return ret;
  574. }
  575. /* ------------------------------------------------------------------------- */
  576. static int mxl5007t_init(struct dvb_frontend *fe)
  577. {
  578. struct mxl5007t_state *state = fe->tuner_priv;
  579. int ret;
  580. if (fe->ops.i2c_gate_ctrl)
  581. fe->ops.i2c_gate_ctrl(fe, 1);
  582. /* wake from standby */
  583. ret = mxl5007t_write_reg(state, 0x01, 0x01);
  584. mxl_fail(ret);
  585. if (fe->ops.i2c_gate_ctrl)
  586. fe->ops.i2c_gate_ctrl(fe, 0);
  587. return ret;
  588. }
  589. static int mxl5007t_sleep(struct dvb_frontend *fe)
  590. {
  591. struct mxl5007t_state *state = fe->tuner_priv;
  592. int ret;
  593. if (fe->ops.i2c_gate_ctrl)
  594. fe->ops.i2c_gate_ctrl(fe, 1);
  595. /* enter standby mode */
  596. ret = mxl5007t_write_reg(state, 0x01, 0x00);
  597. mxl_fail(ret);
  598. ret = mxl5007t_write_reg(state, 0x0f, 0x00);
  599. mxl_fail(ret);
  600. if (fe->ops.i2c_gate_ctrl)
  601. fe->ops.i2c_gate_ctrl(fe, 0);
  602. return ret;
  603. }
  604. /* ------------------------------------------------------------------------- */
  605. static int mxl5007t_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  606. {
  607. struct mxl5007t_state *state = fe->tuner_priv;
  608. *frequency = state->frequency;
  609. return 0;
  610. }
  611. static int mxl5007t_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  612. {
  613. struct mxl5007t_state *state = fe->tuner_priv;
  614. *bandwidth = state->bandwidth;
  615. return 0;
  616. }
  617. static int mxl5007t_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  618. {
  619. struct mxl5007t_state *state = fe->tuner_priv;
  620. *frequency = 0;
  621. switch (state->if_freq) {
  622. case MxL_IF_4_MHZ:
  623. *frequency = 4000000;
  624. break;
  625. case MxL_IF_4_5_MHZ:
  626. *frequency = 4500000;
  627. break;
  628. case MxL_IF_4_57_MHZ:
  629. *frequency = 4570000;
  630. break;
  631. case MxL_IF_5_MHZ:
  632. *frequency = 5000000;
  633. break;
  634. case MxL_IF_5_38_MHZ:
  635. *frequency = 5380000;
  636. break;
  637. case MxL_IF_6_MHZ:
  638. *frequency = 6000000;
  639. break;
  640. case MxL_IF_6_28_MHZ:
  641. *frequency = 6280000;
  642. break;
  643. case MxL_IF_9_1915_MHZ:
  644. *frequency = 9191500;
  645. break;
  646. case MxL_IF_35_25_MHZ:
  647. *frequency = 35250000;
  648. break;
  649. case MxL_IF_36_15_MHZ:
  650. *frequency = 36150000;
  651. break;
  652. case MxL_IF_44_MHZ:
  653. *frequency = 44000000;
  654. break;
  655. }
  656. return 0;
  657. }
  658. static int mxl5007t_release(struct dvb_frontend *fe)
  659. {
  660. struct mxl5007t_state *state = fe->tuner_priv;
  661. mutex_lock(&mxl5007t_list_mutex);
  662. if (state)
  663. hybrid_tuner_release_state(state);
  664. mutex_unlock(&mxl5007t_list_mutex);
  665. fe->tuner_priv = NULL;
  666. return 0;
  667. }
  668. /* ------------------------------------------------------------------------- */
  669. static struct dvb_tuner_ops mxl5007t_tuner_ops = {
  670. .info = {
  671. .name = "MaxLinear MxL5007T",
  672. },
  673. .init = mxl5007t_init,
  674. .sleep = mxl5007t_sleep,
  675. .set_params = mxl5007t_set_params,
  676. .get_status = mxl5007t_get_status,
  677. .get_frequency = mxl5007t_get_frequency,
  678. .get_bandwidth = mxl5007t_get_bandwidth,
  679. .release = mxl5007t_release,
  680. .get_if_frequency = mxl5007t_get_if_frequency,
  681. };
  682. static int mxl5007t_get_chip_id(struct mxl5007t_state *state)
  683. {
  684. char *name;
  685. int ret;
  686. u8 id;
  687. ret = mxl5007t_read_reg(state, 0xd9, &id);
  688. if (mxl_fail(ret))
  689. goto fail;
  690. switch (id) {
  691. case MxL_5007_V1_F1:
  692. name = "MxL5007.v1.f1";
  693. break;
  694. case MxL_5007_V1_F2:
  695. name = "MxL5007.v1.f2";
  696. break;
  697. case MxL_5007_V2_100_F1:
  698. name = "MxL5007.v2.100.f1";
  699. break;
  700. case MxL_5007_V2_100_F2:
  701. name = "MxL5007.v2.100.f2";
  702. break;
  703. case MxL_5007_V2_200_F1:
  704. name = "MxL5007.v2.200.f1";
  705. break;
  706. case MxL_5007_V2_200_F2:
  707. name = "MxL5007.v2.200.f2";
  708. break;
  709. case MxL_5007_V4:
  710. name = "MxL5007T.v4";
  711. break;
  712. default:
  713. name = "MxL5007T";
  714. printk(KERN_WARNING "%s: unknown rev (%02x)\n", __func__, id);
  715. id = MxL_UNKNOWN_ID;
  716. }
  717. state->chip_id = id;
  718. mxl_info("%s detected @ %d-%04x", name,
  719. i2c_adapter_id(state->i2c_props.adap),
  720. state->i2c_props.addr);
  721. return 0;
  722. fail:
  723. mxl_warn("unable to identify device @ %d-%04x",
  724. i2c_adapter_id(state->i2c_props.adap),
  725. state->i2c_props.addr);
  726. state->chip_id = MxL_UNKNOWN_ID;
  727. return ret;
  728. }
  729. struct dvb_frontend *mxl5007t_attach(struct dvb_frontend *fe,
  730. struct i2c_adapter *i2c, u8 addr,
  731. struct mxl5007t_config *cfg)
  732. {
  733. struct mxl5007t_state *state = NULL;
  734. int instance, ret;
  735. mutex_lock(&mxl5007t_list_mutex);
  736. instance = hybrid_tuner_request_state(struct mxl5007t_state, state,
  737. hybrid_tuner_instance_list,
  738. i2c, addr, "mxl5007t");
  739. switch (instance) {
  740. case 0:
  741. goto fail;
  742. case 1:
  743. /* new tuner instance */
  744. state->config = cfg;
  745. mutex_init(&state->lock);
  746. if (fe->ops.i2c_gate_ctrl)
  747. fe->ops.i2c_gate_ctrl(fe, 1);
  748. ret = mxl5007t_get_chip_id(state);
  749. if (fe->ops.i2c_gate_ctrl)
  750. fe->ops.i2c_gate_ctrl(fe, 0);
  751. /* check return value of mxl5007t_get_chip_id */
  752. if (mxl_fail(ret))
  753. goto fail;
  754. break;
  755. default:
  756. /* existing tuner instance */
  757. break;
  758. }
  759. if (fe->ops.i2c_gate_ctrl)
  760. fe->ops.i2c_gate_ctrl(fe, 1);
  761. ret = mxl5007t_soft_reset(state);
  762. if (fe->ops.i2c_gate_ctrl)
  763. fe->ops.i2c_gate_ctrl(fe, 0);
  764. if (mxl_fail(ret))
  765. goto fail;
  766. if (fe->ops.i2c_gate_ctrl)
  767. fe->ops.i2c_gate_ctrl(fe, 1);
  768. ret = mxl5007t_write_reg(state, 0x04,
  769. state->config->loop_thru_enable);
  770. if (fe->ops.i2c_gate_ctrl)
  771. fe->ops.i2c_gate_ctrl(fe, 0);
  772. if (mxl_fail(ret))
  773. goto fail;
  774. fe->tuner_priv = state;
  775. mutex_unlock(&mxl5007t_list_mutex);
  776. memcpy(&fe->ops.tuner_ops, &mxl5007t_tuner_ops,
  777. sizeof(struct dvb_tuner_ops));
  778. return fe;
  779. fail:
  780. mutex_unlock(&mxl5007t_list_mutex);
  781. mxl5007t_release(fe);
  782. return NULL;
  783. }
  784. EXPORT_SYMBOL_GPL(mxl5007t_attach);
  785. MODULE_DESCRIPTION("MaxLinear MxL5007T Silicon IC tuner driver");
  786. MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
  787. MODULE_LICENSE("GPL");
  788. MODULE_VERSION("0.2");