s5h1432.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Samsung s5h1432 DVB-T demodulator driver
  3. *
  4. * Copyright (C) 2009 Bill Liu <Bill.Liu@Conexant.com>
  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/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/string.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include "dvb_frontend.h"
  27. #include "s5h1432.h"
  28. struct s5h1432_state {
  29. struct i2c_adapter *i2c;
  30. /* configuration settings */
  31. const struct s5h1432_config *config;
  32. struct dvb_frontend frontend;
  33. fe_modulation_t current_modulation;
  34. unsigned int first_tune:1;
  35. u32 current_frequency;
  36. int if_freq;
  37. u8 inversion;
  38. };
  39. static int debug;
  40. #define dprintk(arg...) do { \
  41. if (debug) \
  42. printk(arg); \
  43. } while (0)
  44. static int s5h1432_writereg(struct s5h1432_state *state,
  45. u8 addr, u8 reg, u8 data)
  46. {
  47. int ret;
  48. u8 buf[] = { reg, data };
  49. struct i2c_msg msg = {.addr = addr, .flags = 0, .buf = buf, .len = 2 };
  50. ret = i2c_transfer(state->i2c, &msg, 1);
  51. if (ret != 1)
  52. printk(KERN_ERR "%s: writereg error 0x%02x 0x%02x 0x%04x, "
  53. "ret == %i)\n", __func__, addr, reg, data, ret);
  54. return (ret != 1) ? -1 : 0;
  55. }
  56. static u8 s5h1432_readreg(struct s5h1432_state *state, u8 addr, u8 reg)
  57. {
  58. int ret;
  59. u8 b0[] = { reg };
  60. u8 b1[] = { 0 };
  61. struct i2c_msg msg[] = {
  62. {.addr = addr, .flags = 0, .buf = b0, .len = 1},
  63. {.addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 1}
  64. };
  65. ret = i2c_transfer(state->i2c, msg, 2);
  66. if (ret != 2)
  67. printk(KERN_ERR "%s: readreg error (ret == %i)\n",
  68. __func__, ret);
  69. return b1[0];
  70. }
  71. static int s5h1432_sleep(struct dvb_frontend *fe)
  72. {
  73. return 0;
  74. }
  75. static int s5h1432_set_channel_bandwidth(struct dvb_frontend *fe,
  76. u32 bandwidth)
  77. {
  78. struct s5h1432_state *state = fe->demodulator_priv;
  79. u8 reg = 0;
  80. /* Register [0x2E] bit 3:2 : 8MHz = 0; 7MHz = 1; 6MHz = 2 */
  81. reg = s5h1432_readreg(state, S5H1432_I2C_TOP_ADDR, 0x2E);
  82. reg &= ~(0x0C);
  83. switch (bandwidth) {
  84. case 6:
  85. reg |= 0x08;
  86. break;
  87. case 7:
  88. reg |= 0x04;
  89. break;
  90. case 8:
  91. reg |= 0x00;
  92. break;
  93. default:
  94. return 0;
  95. }
  96. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x2E, reg);
  97. return 1;
  98. }
  99. static int s5h1432_set_IF(struct dvb_frontend *fe, u32 ifFreqHz)
  100. {
  101. struct s5h1432_state *state = fe->demodulator_priv;
  102. switch (ifFreqHz) {
  103. case TAIWAN_HI_IF_FREQ_44_MHZ:
  104. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x55);
  105. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x55);
  106. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0x15);
  107. break;
  108. case EUROPE_HI_IF_FREQ_36_MHZ:
  109. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x00);
  110. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x00);
  111. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0x40);
  112. break;
  113. case IF_FREQ_6_MHZ:
  114. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x00);
  115. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x00);
  116. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0xe0);
  117. break;
  118. case IF_FREQ_3point3_MHZ:
  119. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x66);
  120. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x66);
  121. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0xEE);
  122. break;
  123. case IF_FREQ_3point5_MHZ:
  124. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x55);
  125. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x55);
  126. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0xED);
  127. break;
  128. case IF_FREQ_4_MHZ:
  129. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0xAA);
  130. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0xAA);
  131. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0xEA);
  132. break;
  133. default:
  134. {
  135. u32 value = 0;
  136. value = (u32) (((48000 - (ifFreqHz / 1000)) * 512 *
  137. (u32) 32768) / (48 * 1000));
  138. printk(KERN_INFO
  139. "Default IFFreq %d :reg value = 0x%x\n",
  140. ifFreqHz, value);
  141. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4,
  142. (u8) value & 0xFF);
  143. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5,
  144. (u8) (value >> 8) & 0xFF);
  145. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7,
  146. (u8) (value >> 16) & 0xFF);
  147. break;
  148. }
  149. }
  150. return 1;
  151. }
  152. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  153. static int s5h1432_set_frontend(struct dvb_frontend *fe,
  154. struct dvb_frontend_parameters *p)
  155. {
  156. u32 dvb_bandwidth = 8;
  157. struct s5h1432_state *state = fe->demodulator_priv;
  158. if (p->frequency == state->current_frequency) {
  159. /*current_frequency = p->frequency; */
  160. /*state->current_frequency = p->frequency; */
  161. } else {
  162. fe->ops.tuner_ops.set_params(fe, p);
  163. msleep(300);
  164. s5h1432_set_channel_bandwidth(fe, dvb_bandwidth);
  165. switch (p->u.ofdm.bandwidth) {
  166. case BANDWIDTH_6_MHZ:
  167. dvb_bandwidth = 6;
  168. s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
  169. break;
  170. case BANDWIDTH_7_MHZ:
  171. dvb_bandwidth = 7;
  172. s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
  173. break;
  174. case BANDWIDTH_8_MHZ:
  175. dvb_bandwidth = 8;
  176. s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
  177. break;
  178. default:
  179. return 0;
  180. }
  181. /*fe->ops.tuner_ops.set_params(fe, p); */
  182. /*Soft Reset chip*/
  183. msleep(30);
  184. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1a);
  185. msleep(30);
  186. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1b);
  187. s5h1432_set_channel_bandwidth(fe, dvb_bandwidth);
  188. switch (p->u.ofdm.bandwidth) {
  189. case BANDWIDTH_6_MHZ:
  190. dvb_bandwidth = 6;
  191. s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
  192. break;
  193. case BANDWIDTH_7_MHZ:
  194. dvb_bandwidth = 7;
  195. s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
  196. break;
  197. case BANDWIDTH_8_MHZ:
  198. dvb_bandwidth = 8;
  199. s5h1432_set_IF(fe, IF_FREQ_4_MHZ);
  200. break;
  201. default:
  202. return 0;
  203. }
  204. /*fe->ops.tuner_ops.set_params(fe,p); */
  205. /*Soft Reset chip*/
  206. msleep(30);
  207. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1a);
  208. msleep(30);
  209. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1b);
  210. }
  211. state->current_frequency = p->frequency;
  212. return 0;
  213. }
  214. static int s5h1432_init(struct dvb_frontend *fe)
  215. {
  216. struct s5h1432_state *state = fe->demodulator_priv;
  217. u8 reg = 0;
  218. state->current_frequency = 0;
  219. printk(KERN_INFO " s5h1432_init().\n");
  220. /*Set VSB mode as default, this also does a soft reset */
  221. /*Initialize registers */
  222. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x04, 0xa8);
  223. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x05, 0x01);
  224. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x07, 0x70);
  225. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x19, 0x80);
  226. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x1b, 0x9D);
  227. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x1c, 0x30);
  228. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x1d, 0x20);
  229. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x1e, 0x1B);
  230. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x2e, 0x40);
  231. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x42, 0x84);
  232. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x50, 0x5a);
  233. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x5a, 0xd3);
  234. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x68, 0x50);
  235. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xb8, 0x3c);
  236. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xc4, 0x10);
  237. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xcc, 0x9c);
  238. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xDA, 0x00);
  239. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe1, 0x94);
  240. /* s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xf4, 0xa1); */
  241. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xf9, 0x00);
  242. /*For NXP tuner*/
  243. /*Set 3.3MHz as default IF frequency */
  244. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe4, 0x66);
  245. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe5, 0x66);
  246. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0xe7, 0xEE);
  247. /* Set reg 0x1E to get the full dynamic range */
  248. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x1e, 0x31);
  249. /* Mode setting in demod */
  250. reg = s5h1432_readreg(state, S5H1432_I2C_TOP_ADDR, 0x42);
  251. reg |= 0x80;
  252. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x42, reg);
  253. /* Serial mode */
  254. /* Soft Reset chip */
  255. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1a);
  256. msleep(30);
  257. s5h1432_writereg(state, S5H1432_I2C_TOP_ADDR, 0x09, 0x1b);
  258. return 0;
  259. }
  260. static int s5h1432_read_status(struct dvb_frontend *fe, fe_status_t *status)
  261. {
  262. return 0;
  263. }
  264. static int s5h1432_read_signal_strength(struct dvb_frontend *fe,
  265. u16 *signal_strength)
  266. {
  267. return 0;
  268. }
  269. static int s5h1432_read_snr(struct dvb_frontend *fe, u16 *snr)
  270. {
  271. return 0;
  272. }
  273. static int s5h1432_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  274. {
  275. return 0;
  276. }
  277. static int s5h1432_read_ber(struct dvb_frontend *fe, u32 *ber)
  278. {
  279. return 0;
  280. }
  281. static int s5h1432_get_frontend(struct dvb_frontend *fe,
  282. struct dvb_frontend_parameters *p)
  283. {
  284. return 0;
  285. }
  286. static int s5h1432_get_tune_settings(struct dvb_frontend *fe,
  287. struct dvb_frontend_tune_settings *tune)
  288. {
  289. return 0;
  290. }
  291. static void s5h1432_release(struct dvb_frontend *fe)
  292. {
  293. struct s5h1432_state *state = fe->demodulator_priv;
  294. kfree(state);
  295. }
  296. static struct dvb_frontend_ops s5h1432_ops;
  297. struct dvb_frontend *s5h1432_attach(const struct s5h1432_config *config,
  298. struct i2c_adapter *i2c)
  299. {
  300. struct s5h1432_state *state = NULL;
  301. printk(KERN_INFO " Enter s5h1432_attach(). attach success!\n");
  302. /* allocate memory for the internal state */
  303. state = kmalloc(sizeof(struct s5h1432_state), GFP_KERNEL);
  304. if (state == NULL)
  305. goto error;
  306. /* setup the state */
  307. state->config = config;
  308. state->i2c = i2c;
  309. state->current_modulation = QAM_16;
  310. state->inversion = state->config->inversion;
  311. /* create dvb_frontend */
  312. memcpy(&state->frontend.ops, &s5h1432_ops,
  313. sizeof(struct dvb_frontend_ops));
  314. state->frontend.demodulator_priv = state;
  315. return &state->frontend;
  316. error:
  317. kfree(state);
  318. return NULL;
  319. }
  320. EXPORT_SYMBOL(s5h1432_attach);
  321. static struct dvb_frontend_ops s5h1432_ops = {
  322. .info = {
  323. .name = "Samsung s5h1432 DVB-T Frontend",
  324. .type = FE_OFDM,
  325. .frequency_min = 177000000,
  326. .frequency_max = 858000000,
  327. .frequency_stepsize = 166666,
  328. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  329. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  330. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  331. FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  332. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER},
  333. .init = s5h1432_init,
  334. .sleep = s5h1432_sleep,
  335. .set_frontend = s5h1432_set_frontend,
  336. .get_frontend = s5h1432_get_frontend,
  337. .get_tune_settings = s5h1432_get_tune_settings,
  338. .read_status = s5h1432_read_status,
  339. .read_ber = s5h1432_read_ber,
  340. .read_signal_strength = s5h1432_read_signal_strength,
  341. .read_snr = s5h1432_read_snr,
  342. .read_ucblocks = s5h1432_read_ucblocks,
  343. .release = s5h1432_release,
  344. };
  345. module_param(debug, int, 0644);
  346. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  347. MODULE_DESCRIPTION("Samsung s5h1432 DVB-T Demodulator driver");
  348. MODULE_AUTHOR("Bill Liu");
  349. MODULE_LICENSE("GPL");