s5h1432.c 10 KB

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