gp8psk-fe.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Frontend driver for the GENPIX 8pks/qpsk/DCII USB2.0 DVB-S module
  4. *
  5. * Copyright (C) 2006,2007 Alan Nisota (alannisota@gmail.com)
  6. * Copyright (C) 2006,2007 Genpix Electronics (genpix@genpix-electronics.com)
  7. *
  8. * Thanks to GENPIX for the sample code used to implement this module.
  9. *
  10. * This module is based off the vp7045 and vp702x modules
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include "gp8psk-fe.h"
  14. #include <media/dvb_frontend.h>
  15. static int debug;
  16. module_param(debug, int, 0644);
  17. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  18. #define dprintk(fmt, arg...) do { \
  19. if (debug) \
  20. printk(KERN_DEBUG pr_fmt("%s: " fmt), \
  21. __func__, ##arg); \
  22. } while (0)
  23. struct gp8psk_fe_state {
  24. struct dvb_frontend fe;
  25. void *priv;
  26. const struct gp8psk_fe_ops *ops;
  27. bool is_rev1;
  28. u8 lock;
  29. u16 snr;
  30. unsigned long next_status_check;
  31. unsigned long status_check_interval;
  32. };
  33. static int gp8psk_tuned_to_DCII(struct dvb_frontend *fe)
  34. {
  35. struct gp8psk_fe_state *st = fe->demodulator_priv;
  36. u8 status;
  37. st->ops->in(st->priv, GET_8PSK_CONFIG, 0, 0, &status, 1);
  38. return status & bmDCtuned;
  39. }
  40. static int gp8psk_set_tuner_mode(struct dvb_frontend *fe, int mode)
  41. {
  42. struct gp8psk_fe_state *st = fe->demodulator_priv;
  43. return st->ops->out(st->priv, SET_8PSK_CONFIG, mode, 0, NULL, 0);
  44. }
  45. static int gp8psk_fe_update_status(struct gp8psk_fe_state *st)
  46. {
  47. u8 buf[6];
  48. if (time_after(jiffies,st->next_status_check)) {
  49. st->ops->in(st->priv, GET_SIGNAL_LOCK, 0, 0, &st->lock, 1);
  50. st->ops->in(st->priv, GET_SIGNAL_STRENGTH, 0, 0, buf, 6);
  51. st->snr = (buf[1]) << 8 | buf[0];
  52. st->next_status_check = jiffies + (st->status_check_interval*HZ)/1000;
  53. }
  54. return 0;
  55. }
  56. static int gp8psk_fe_read_status(struct dvb_frontend *fe,
  57. enum fe_status *status)
  58. {
  59. struct gp8psk_fe_state *st = fe->demodulator_priv;
  60. gp8psk_fe_update_status(st);
  61. if (st->lock)
  62. *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_SIGNAL | FE_HAS_CARRIER;
  63. else
  64. *status = 0;
  65. if (*status & FE_HAS_LOCK)
  66. st->status_check_interval = 1000;
  67. else
  68. st->status_check_interval = 100;
  69. return 0;
  70. }
  71. /* not supported by this Frontend */
  72. static int gp8psk_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
  73. {
  74. (void) fe;
  75. *ber = 0;
  76. return 0;
  77. }
  78. /* not supported by this Frontend */
  79. static int gp8psk_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
  80. {
  81. (void) fe;
  82. *unc = 0;
  83. return 0;
  84. }
  85. static int gp8psk_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
  86. {
  87. struct gp8psk_fe_state *st = fe->demodulator_priv;
  88. gp8psk_fe_update_status(st);
  89. /* snr is reported in dBu*256 */
  90. *snr = st->snr;
  91. return 0;
  92. }
  93. static int gp8psk_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
  94. {
  95. struct gp8psk_fe_state *st = fe->demodulator_priv;
  96. gp8psk_fe_update_status(st);
  97. /* snr is reported in dBu*256 */
  98. /* snr / 38.4 ~= 100% strength */
  99. /* snr * 17 returns 100% strength as 65535 */
  100. if (st->snr > 0xf00)
  101. *strength = 0xffff;
  102. else
  103. *strength = (st->snr << 4) + st->snr; /* snr*17 */
  104. return 0;
  105. }
  106. static int gp8psk_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  107. {
  108. tune->min_delay_ms = 800;
  109. return 0;
  110. }
  111. static int gp8psk_fe_set_frontend(struct dvb_frontend *fe)
  112. {
  113. struct gp8psk_fe_state *st = fe->demodulator_priv;
  114. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  115. u8 cmd[10];
  116. u32 freq = c->frequency * 1000;
  117. dprintk("%s()\n", __func__);
  118. cmd[4] = freq & 0xff;
  119. cmd[5] = (freq >> 8) & 0xff;
  120. cmd[6] = (freq >> 16) & 0xff;
  121. cmd[7] = (freq >> 24) & 0xff;
  122. /* backwards compatibility: DVB-S + 8-PSK were used for Turbo-FEC */
  123. if (c->delivery_system == SYS_DVBS && c->modulation == PSK_8)
  124. c->delivery_system = SYS_TURBO;
  125. switch (c->delivery_system) {
  126. case SYS_DVBS:
  127. if (c->modulation != QPSK) {
  128. dprintk("%s: unsupported modulation selected (%d)\n",
  129. __func__, c->modulation);
  130. return -EOPNOTSUPP;
  131. }
  132. c->fec_inner = FEC_AUTO;
  133. break;
  134. case SYS_DVBS2: /* kept for backwards compatibility */
  135. dprintk("%s: DVB-S2 delivery system selected\n", __func__);
  136. break;
  137. case SYS_TURBO:
  138. dprintk("%s: Turbo-FEC delivery system selected\n", __func__);
  139. break;
  140. default:
  141. dprintk("%s: unsupported delivery system selected (%d)\n",
  142. __func__, c->delivery_system);
  143. return -EOPNOTSUPP;
  144. }
  145. cmd[0] = c->symbol_rate & 0xff;
  146. cmd[1] = (c->symbol_rate >> 8) & 0xff;
  147. cmd[2] = (c->symbol_rate >> 16) & 0xff;
  148. cmd[3] = (c->symbol_rate >> 24) & 0xff;
  149. switch (c->modulation) {
  150. case QPSK:
  151. if (st->is_rev1)
  152. if (gp8psk_tuned_to_DCII(fe))
  153. st->ops->reload(st->priv);
  154. switch (c->fec_inner) {
  155. case FEC_1_2:
  156. cmd[9] = 0; break;
  157. case FEC_2_3:
  158. cmd[9] = 1; break;
  159. case FEC_3_4:
  160. cmd[9] = 2; break;
  161. case FEC_5_6:
  162. cmd[9] = 3; break;
  163. case FEC_7_8:
  164. cmd[9] = 4; break;
  165. case FEC_AUTO:
  166. cmd[9] = 5; break;
  167. default:
  168. cmd[9] = 5; break;
  169. }
  170. if (c->delivery_system == SYS_TURBO)
  171. cmd[8] = ADV_MOD_TURBO_QPSK;
  172. else
  173. cmd[8] = ADV_MOD_DVB_QPSK;
  174. break;
  175. case PSK_8: /* PSK_8 is for compatibility with DN */
  176. cmd[8] = ADV_MOD_TURBO_8PSK;
  177. switch (c->fec_inner) {
  178. case FEC_2_3:
  179. cmd[9] = 0; break;
  180. case FEC_3_4:
  181. cmd[9] = 1; break;
  182. case FEC_3_5:
  183. cmd[9] = 2; break;
  184. case FEC_5_6:
  185. cmd[9] = 3; break;
  186. case FEC_8_9:
  187. cmd[9] = 4; break;
  188. default:
  189. cmd[9] = 0; break;
  190. }
  191. break;
  192. case QAM_16: /* QAM_16 is for compatibility with DN */
  193. cmd[8] = ADV_MOD_TURBO_16QAM;
  194. cmd[9] = 0;
  195. break;
  196. default: /* Unknown modulation */
  197. dprintk("%s: unsupported modulation selected (%d)\n",
  198. __func__, c->modulation);
  199. return -EOPNOTSUPP;
  200. }
  201. if (st->is_rev1)
  202. gp8psk_set_tuner_mode(fe, 0);
  203. st->ops->out(st->priv, TUNE_8PSK, 0, 0, cmd, 10);
  204. st->lock = 0;
  205. st->next_status_check = jiffies;
  206. st->status_check_interval = 200;
  207. return 0;
  208. }
  209. static int gp8psk_fe_send_diseqc_msg (struct dvb_frontend* fe,
  210. struct dvb_diseqc_master_cmd *m)
  211. {
  212. struct gp8psk_fe_state *st = fe->demodulator_priv;
  213. dprintk("%s\n", __func__);
  214. if (st->ops->out(st->priv, SEND_DISEQC_COMMAND, m->msg[0], 0,
  215. m->msg, m->msg_len)) {
  216. return -EINVAL;
  217. }
  218. return 0;
  219. }
  220. static int gp8psk_fe_send_diseqc_burst(struct dvb_frontend *fe,
  221. enum fe_sec_mini_cmd burst)
  222. {
  223. struct gp8psk_fe_state *st = fe->demodulator_priv;
  224. u8 cmd;
  225. dprintk("%s\n", __func__);
  226. /* These commands are certainly wrong */
  227. cmd = (burst == SEC_MINI_A) ? 0x00 : 0x01;
  228. if (st->ops->out(st->priv, SEND_DISEQC_COMMAND, cmd, 0,
  229. &cmd, 0)) {
  230. return -EINVAL;
  231. }
  232. return 0;
  233. }
  234. static int gp8psk_fe_set_tone(struct dvb_frontend *fe,
  235. enum fe_sec_tone_mode tone)
  236. {
  237. struct gp8psk_fe_state *st = fe->demodulator_priv;
  238. if (st->ops->out(st->priv, SET_22KHZ_TONE,
  239. (tone == SEC_TONE_ON), 0, NULL, 0)) {
  240. return -EINVAL;
  241. }
  242. return 0;
  243. }
  244. static int gp8psk_fe_set_voltage(struct dvb_frontend *fe,
  245. enum fe_sec_voltage voltage)
  246. {
  247. struct gp8psk_fe_state *st = fe->demodulator_priv;
  248. if (st->ops->out(st->priv, SET_LNB_VOLTAGE,
  249. voltage == SEC_VOLTAGE_18, 0, NULL, 0)) {
  250. return -EINVAL;
  251. }
  252. return 0;
  253. }
  254. static int gp8psk_fe_enable_high_lnb_voltage(struct dvb_frontend* fe, long onoff)
  255. {
  256. struct gp8psk_fe_state *st = fe->demodulator_priv;
  257. return st->ops->out(st->priv, USE_EXTRA_VOLT, onoff, 0, NULL, 0);
  258. }
  259. static int gp8psk_fe_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long sw_cmd)
  260. {
  261. struct gp8psk_fe_state *st = fe->demodulator_priv;
  262. u8 cmd = sw_cmd & 0x7f;
  263. if (st->ops->out(st->priv, SET_DN_SWITCH, cmd, 0, NULL, 0))
  264. return -EINVAL;
  265. if (st->ops->out(st->priv, SET_LNB_VOLTAGE, !!(sw_cmd & 0x80),
  266. 0, NULL, 0))
  267. return -EINVAL;
  268. return 0;
  269. }
  270. static void gp8psk_fe_release(struct dvb_frontend* fe)
  271. {
  272. struct gp8psk_fe_state *st = fe->demodulator_priv;
  273. kfree(st);
  274. }
  275. static const struct dvb_frontend_ops gp8psk_fe_ops;
  276. struct dvb_frontend *gp8psk_fe_attach(const struct gp8psk_fe_ops *ops,
  277. void *priv, bool is_rev1)
  278. {
  279. struct gp8psk_fe_state *st;
  280. if (!ops || !ops->in || !ops->out || !ops->reload) {
  281. pr_err("Error! gp8psk-fe ops not defined.\n");
  282. return NULL;
  283. }
  284. st = kzalloc(sizeof(struct gp8psk_fe_state), GFP_KERNEL);
  285. if (!st)
  286. return NULL;
  287. memcpy(&st->fe.ops, &gp8psk_fe_ops, sizeof(struct dvb_frontend_ops));
  288. st->fe.demodulator_priv = st;
  289. st->ops = ops;
  290. st->priv = priv;
  291. st->is_rev1 = is_rev1;
  292. pr_info("Frontend %sattached\n", is_rev1 ? "revision 1 " : "");
  293. return &st->fe;
  294. }
  295. EXPORT_SYMBOL_GPL(gp8psk_fe_attach);
  296. static const struct dvb_frontend_ops gp8psk_fe_ops = {
  297. .delsys = { SYS_DVBS },
  298. .info = {
  299. .name = "Genpix DVB-S",
  300. .frequency_min_hz = 800 * MHz,
  301. .frequency_max_hz = 2250 * MHz,
  302. .frequency_stepsize_hz = 100 * kHz,
  303. .symbol_rate_min = 1000000,
  304. .symbol_rate_max = 45000000,
  305. .symbol_rate_tolerance = 500, /* ppm */
  306. .caps = FE_CAN_INVERSION_AUTO |
  307. 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. /*
  310. * FE_CAN_QAM_16 is for compatibility
  311. * (Myth incorrectly detects Turbo-QPSK as plain QAM-16)
  312. */
  313. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_TURBO_FEC
  314. },
  315. .release = gp8psk_fe_release,
  316. .init = NULL,
  317. .sleep = NULL,
  318. .set_frontend = gp8psk_fe_set_frontend,
  319. .get_tune_settings = gp8psk_fe_get_tune_settings,
  320. .read_status = gp8psk_fe_read_status,
  321. .read_ber = gp8psk_fe_read_ber,
  322. .read_signal_strength = gp8psk_fe_read_signal_strength,
  323. .read_snr = gp8psk_fe_read_snr,
  324. .read_ucblocks = gp8psk_fe_read_unc_blocks,
  325. .diseqc_send_master_cmd = gp8psk_fe_send_diseqc_msg,
  326. .diseqc_send_burst = gp8psk_fe_send_diseqc_burst,
  327. .set_tone = gp8psk_fe_set_tone,
  328. .set_voltage = gp8psk_fe_set_voltage,
  329. .dishnetwork_send_legacy_command = gp8psk_fe_send_legacy_dish_cmd,
  330. .enable_high_lnb_voltage = gp8psk_fe_enable_high_lnb_voltage
  331. };
  332. MODULE_AUTHOR("Alan Nisota <alannisota@gamil.com>");
  333. MODULE_DESCRIPTION("Frontend Driver for Genpix DVB-S");
  334. MODULE_VERSION("1.1");
  335. MODULE_LICENSE("GPL");