mn88443x.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Socionext MN88443x series demodulator driver for ISDB-S/ISDB-T.
  4. //
  5. // Copyright (c) 2018 Socionext Inc.
  6. #include <linux/bitfield.h>
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/of_device.h>
  11. #include <linux/regmap.h>
  12. #include <media/dvb_math.h>
  13. #include "mn88443x.h"
  14. /* ISDB-S registers */
  15. #define ATSIDU_S 0x2f
  16. #define ATSIDL_S 0x30
  17. #define TSSET_S 0x31
  18. #define AGCREAD_S 0x5a
  19. #define CPMON1_S 0x5e
  20. #define CPMON1_S_FSYNC BIT(5)
  21. #define CPMON1_S_ERRMON BIT(4)
  22. #define CPMON1_S_SIGOFF BIT(3)
  23. #define CPMON1_S_W2LOCK BIT(2)
  24. #define CPMON1_S_W1LOCK BIT(1)
  25. #define CPMON1_S_DW1LOCK BIT(0)
  26. #define TRMON_S 0x60
  27. #define BERCNFLG_S 0x68
  28. #define BERCNFLG_S_BERVRDY BIT(5)
  29. #define BERCNFLG_S_BERVCHK BIT(4)
  30. #define BERCNFLG_S_BERDRDY BIT(3)
  31. #define BERCNFLG_S_BERDCHK BIT(2)
  32. #define CNRDXU_S 0x69
  33. #define CNRDXL_S 0x6a
  34. #define CNRDYU_S 0x6b
  35. #define CNRDYL_S 0x6c
  36. #define BERVRDU_S 0x71
  37. #define BERVRDL_S 0x72
  38. #define DOSET1_S 0x73
  39. /* Primary ISDB-T */
  40. #define PLLASET1 0x00
  41. #define PLLASET2 0x01
  42. #define PLLBSET1 0x02
  43. #define PLLBSET2 0x03
  44. #define PLLSET 0x04
  45. #define OUTCSET 0x08
  46. #define OUTCSET_CHDRV_8MA 0xff
  47. #define OUTCSET_CHDRV_4MA 0x00
  48. #define PLDWSET 0x09
  49. #define PLDWSET_NORMAL 0x00
  50. #define PLDWSET_PULLDOWN 0xff
  51. #define HIZSET1 0x0a
  52. #define HIZSET2 0x0b
  53. /* Secondary ISDB-T (for MN884434 only) */
  54. #define RCVSET 0x00
  55. #define TSSET1_M 0x01
  56. #define TSSET2_M 0x02
  57. #define TSSET3_M 0x03
  58. #define INTACSET 0x08
  59. #define HIZSET3 0x0b
  60. /* ISDB-T registers */
  61. #define TSSET1 0x05
  62. #define TSSET1_TSASEL_MASK GENMASK(4, 3)
  63. #define TSSET1_TSASEL_ISDBT (0x0 << 3)
  64. #define TSSET1_TSASEL_ISDBS (0x1 << 3)
  65. #define TSSET1_TSASEL_NONE (0x2 << 3)
  66. #define TSSET1_TSBSEL_MASK GENMASK(2, 1)
  67. #define TSSET1_TSBSEL_ISDBS (0x0 << 1)
  68. #define TSSET1_TSBSEL_ISDBT (0x1 << 1)
  69. #define TSSET1_TSBSEL_NONE (0x2 << 1)
  70. #define TSSET2 0x06
  71. #define TSSET3 0x07
  72. #define TSSET3_INTASEL_MASK GENMASK(7, 6)
  73. #define TSSET3_INTASEL_T (0x0 << 6)
  74. #define TSSET3_INTASEL_S (0x1 << 6)
  75. #define TSSET3_INTASEL_NONE (0x2 << 6)
  76. #define TSSET3_INTBSEL_MASK GENMASK(5, 4)
  77. #define TSSET3_INTBSEL_S (0x0 << 4)
  78. #define TSSET3_INTBSEL_T (0x1 << 4)
  79. #define TSSET3_INTBSEL_NONE (0x2 << 4)
  80. #define OUTSET2 0x0d
  81. #define PWDSET 0x0f
  82. #define PWDSET_OFDMPD_MASK GENMASK(3, 2)
  83. #define PWDSET_OFDMPD_DOWN BIT(3)
  84. #define PWDSET_PSKPD_MASK GENMASK(1, 0)
  85. #define PWDSET_PSKPD_DOWN BIT(1)
  86. #define CLKSET1_T 0x11
  87. #define MDSET_T 0x13
  88. #define MDSET_T_MDAUTO_MASK GENMASK(7, 4)
  89. #define MDSET_T_MDAUTO_AUTO (0xf << 4)
  90. #define MDSET_T_MDAUTO_MANUAL (0x0 << 4)
  91. #define MDSET_T_FFTS_MASK GENMASK(3, 2)
  92. #define MDSET_T_FFTS_MODE1 (0x0 << 2)
  93. #define MDSET_T_FFTS_MODE2 (0x1 << 2)
  94. #define MDSET_T_FFTS_MODE3 (0x2 << 2)
  95. #define MDSET_T_GI_MASK GENMASK(1, 0)
  96. #define MDSET_T_GI_1_32 (0x0 << 0)
  97. #define MDSET_T_GI_1_16 (0x1 << 0)
  98. #define MDSET_T_GI_1_8 (0x2 << 0)
  99. #define MDSET_T_GI_1_4 (0x3 << 0)
  100. #define MDASET_T 0x14
  101. #define ADCSET1_T 0x20
  102. #define ADCSET1_T_REFSEL_MASK GENMASK(1, 0)
  103. #define ADCSET1_T_REFSEL_2V (0x3 << 0)
  104. #define ADCSET1_T_REFSEL_1_5V (0x2 << 0)
  105. #define ADCSET1_T_REFSEL_1V (0x1 << 0)
  106. #define NCOFREQU_T 0x24
  107. #define NCOFREQM_T 0x25
  108. #define NCOFREQL_T 0x26
  109. #define FADU_T 0x27
  110. #define FADM_T 0x28
  111. #define FADL_T 0x29
  112. #define AGCSET2_T 0x2c
  113. #define AGCSET2_T_IFPOLINV_INC BIT(0)
  114. #define AGCSET2_T_RFPOLINV_INC BIT(1)
  115. #define AGCV3_T 0x3e
  116. #define MDRD_T 0xa2
  117. #define MDRD_T_SEGID_MASK GENMASK(5, 4)
  118. #define MDRD_T_SEGID_13 (0x0 << 4)
  119. #define MDRD_T_SEGID_1 (0x1 << 4)
  120. #define MDRD_T_SEGID_3 (0x2 << 4)
  121. #define MDRD_T_FFTS_MASK GENMASK(3, 2)
  122. #define MDRD_T_FFTS_MODE1 (0x0 << 2)
  123. #define MDRD_T_FFTS_MODE2 (0x1 << 2)
  124. #define MDRD_T_FFTS_MODE3 (0x2 << 2)
  125. #define MDRD_T_GI_MASK GENMASK(1, 0)
  126. #define MDRD_T_GI_1_32 (0x0 << 0)
  127. #define MDRD_T_GI_1_16 (0x1 << 0)
  128. #define MDRD_T_GI_1_8 (0x2 << 0)
  129. #define MDRD_T_GI_1_4 (0x3 << 0)
  130. #define SSEQRD_T 0xa3
  131. #define SSEQRD_T_SSEQSTRD_MASK GENMASK(3, 0)
  132. #define SSEQRD_T_SSEQSTRD_RESET (0x0 << 0)
  133. #define SSEQRD_T_SSEQSTRD_TUNING (0x1 << 0)
  134. #define SSEQRD_T_SSEQSTRD_AGC (0x2 << 0)
  135. #define SSEQRD_T_SSEQSTRD_SEARCH (0x3 << 0)
  136. #define SSEQRD_T_SSEQSTRD_CLOCK_SYNC (0x4 << 0)
  137. #define SSEQRD_T_SSEQSTRD_FREQ_SYNC (0x8 << 0)
  138. #define SSEQRD_T_SSEQSTRD_FRAME_SYNC (0x9 << 0)
  139. #define SSEQRD_T_SSEQSTRD_SYNC (0xa << 0)
  140. #define SSEQRD_T_SSEQSTRD_LOCK (0xb << 0)
  141. #define AGCRDU_T 0xa8
  142. #define AGCRDL_T 0xa9
  143. #define CNRDU_T 0xbe
  144. #define CNRDL_T 0xbf
  145. #define BERFLG_T 0xc0
  146. #define BERFLG_T_BERDRDY BIT(7)
  147. #define BERFLG_T_BERDCHK BIT(6)
  148. #define BERFLG_T_BERVRDYA BIT(5)
  149. #define BERFLG_T_BERVCHKA BIT(4)
  150. #define BERFLG_T_BERVRDYB BIT(3)
  151. #define BERFLG_T_BERVCHKB BIT(2)
  152. #define BERFLG_T_BERVRDYC BIT(1)
  153. #define BERFLG_T_BERVCHKC BIT(0)
  154. #define BERRDU_T 0xc1
  155. #define BERRDM_T 0xc2
  156. #define BERRDL_T 0xc3
  157. #define BERLENRDU_T 0xc4
  158. #define BERLENRDL_T 0xc5
  159. #define ERRFLG_T 0xc6
  160. #define ERRFLG_T_BERDOVF BIT(7)
  161. #define ERRFLG_T_BERVOVFA BIT(6)
  162. #define ERRFLG_T_BERVOVFB BIT(5)
  163. #define ERRFLG_T_BERVOVFC BIT(4)
  164. #define ERRFLG_T_NERRFA BIT(3)
  165. #define ERRFLG_T_NERRFB BIT(2)
  166. #define ERRFLG_T_NERRFC BIT(1)
  167. #define ERRFLG_T_NERRF BIT(0)
  168. #define DOSET1_T 0xcf
  169. #define CLK_LOW 4000000
  170. #define CLK_DIRECT 20200000
  171. #define CLK_MAX 25410000
  172. #define S_T_FREQ 8126984 /* 512 / 63 MHz */
  173. struct mn88443x_spec {
  174. bool primary;
  175. };
  176. struct mn88443x_priv {
  177. const struct mn88443x_spec *spec;
  178. struct dvb_frontend fe;
  179. struct clk *mclk;
  180. struct gpio_desc *reset_gpio;
  181. u32 clk_freq;
  182. u32 if_freq;
  183. /* Common */
  184. bool use_clkbuf;
  185. /* ISDB-S */
  186. struct i2c_client *client_s;
  187. struct regmap *regmap_s;
  188. /* ISDB-T */
  189. struct i2c_client *client_t;
  190. struct regmap *regmap_t;
  191. };
  192. static void mn88443x_cmn_power_on(struct mn88443x_priv *chip)
  193. {
  194. struct regmap *r_t = chip->regmap_t;
  195. clk_prepare_enable(chip->mclk);
  196. gpiod_set_value_cansleep(chip->reset_gpio, 1);
  197. usleep_range(100, 1000);
  198. gpiod_set_value_cansleep(chip->reset_gpio, 0);
  199. if (chip->spec->primary) {
  200. regmap_write(r_t, OUTCSET, OUTCSET_CHDRV_8MA);
  201. regmap_write(r_t, PLDWSET, PLDWSET_NORMAL);
  202. regmap_write(r_t, HIZSET1, 0x80);
  203. regmap_write(r_t, HIZSET2, 0xe0);
  204. } else {
  205. regmap_write(r_t, HIZSET3, 0x8f);
  206. }
  207. }
  208. static void mn88443x_cmn_power_off(struct mn88443x_priv *chip)
  209. {
  210. gpiod_set_value_cansleep(chip->reset_gpio, 1);
  211. clk_disable_unprepare(chip->mclk);
  212. }
  213. static void mn88443x_s_sleep(struct mn88443x_priv *chip)
  214. {
  215. struct regmap *r_t = chip->regmap_t;
  216. regmap_update_bits(r_t, PWDSET, PWDSET_PSKPD_MASK,
  217. PWDSET_PSKPD_DOWN);
  218. }
  219. static void mn88443x_s_wake(struct mn88443x_priv *chip)
  220. {
  221. struct regmap *r_t = chip->regmap_t;
  222. regmap_update_bits(r_t, PWDSET, PWDSET_PSKPD_MASK, 0);
  223. }
  224. static void mn88443x_s_tune(struct mn88443x_priv *chip,
  225. struct dtv_frontend_properties *c)
  226. {
  227. struct regmap *r_s = chip->regmap_s;
  228. regmap_write(r_s, ATSIDU_S, c->stream_id >> 8);
  229. regmap_write(r_s, ATSIDL_S, c->stream_id);
  230. regmap_write(r_s, TSSET_S, 0);
  231. }
  232. static int mn88443x_s_read_status(struct mn88443x_priv *chip,
  233. struct dtv_frontend_properties *c,
  234. enum fe_status *status)
  235. {
  236. struct regmap *r_s = chip->regmap_s;
  237. u32 cpmon, tmpu, tmpl, flg;
  238. u64 tmp;
  239. /* Sync detection */
  240. regmap_read(r_s, CPMON1_S, &cpmon);
  241. *status = 0;
  242. if (cpmon & CPMON1_S_FSYNC)
  243. *status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  244. if (cpmon & CPMON1_S_W2LOCK)
  245. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
  246. /* Signal strength */
  247. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  248. if (*status & FE_HAS_SIGNAL) {
  249. u32 agc;
  250. regmap_read(r_s, AGCREAD_S, &tmpu);
  251. agc = tmpu << 8;
  252. c->strength.len = 1;
  253. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  254. c->strength.stat[0].uvalue = agc;
  255. }
  256. /* C/N rate */
  257. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  258. if (*status & FE_HAS_VITERBI) {
  259. u32 cnr = 0, x, y, d;
  260. u64 d_3 = 0;
  261. regmap_read(r_s, CNRDXU_S, &tmpu);
  262. regmap_read(r_s, CNRDXL_S, &tmpl);
  263. x = (tmpu << 8) | tmpl;
  264. regmap_read(r_s, CNRDYU_S, &tmpu);
  265. regmap_read(r_s, CNRDYL_S, &tmpl);
  266. y = (tmpu << 8) | tmpl;
  267. /* CNR[dB]: 10 * log10(D) - 30.74 / D^3 - 3 */
  268. /* D = x^2 / (2^15 * y - x^2) */
  269. d = (y << 15) - x * x;
  270. if (d > 0) {
  271. /* (2^4 * D)^3 = 2^12 * D^3 */
  272. /* 3.074 * 2^(12 + 24) = 211243671486 */
  273. d_3 = div_u64(16 * x * x, d);
  274. d_3 = d_3 * d_3 * d_3;
  275. if (d_3)
  276. d_3 = div_u64(211243671486ULL, d_3);
  277. }
  278. if (d_3) {
  279. /* 0.3 * 2^24 = 5033164 */
  280. tmp = (s64)2 * intlog10(x) - intlog10(abs(d)) - d_3
  281. - 5033164;
  282. cnr = div_u64(tmp * 10000, 1 << 24);
  283. }
  284. if (cnr) {
  285. c->cnr.len = 1;
  286. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  287. c->cnr.stat[0].uvalue = cnr;
  288. }
  289. }
  290. /* BER */
  291. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  292. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  293. regmap_read(r_s, BERCNFLG_S, &flg);
  294. if ((*status & FE_HAS_VITERBI) && (flg & BERCNFLG_S_BERVRDY)) {
  295. u32 bit_err, bit_cnt;
  296. regmap_read(r_s, BERVRDU_S, &tmpu);
  297. regmap_read(r_s, BERVRDL_S, &tmpl);
  298. bit_err = (tmpu << 8) | tmpl;
  299. bit_cnt = (1 << 13) * 204;
  300. if (bit_cnt) {
  301. c->post_bit_error.len = 1;
  302. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  303. c->post_bit_error.stat[0].uvalue = bit_err;
  304. c->post_bit_count.len = 1;
  305. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  306. c->post_bit_count.stat[0].uvalue = bit_cnt;
  307. }
  308. }
  309. return 0;
  310. }
  311. static void mn88443x_t_sleep(struct mn88443x_priv *chip)
  312. {
  313. struct regmap *r_t = chip->regmap_t;
  314. regmap_update_bits(r_t, PWDSET, PWDSET_OFDMPD_MASK,
  315. PWDSET_OFDMPD_DOWN);
  316. }
  317. static void mn88443x_t_wake(struct mn88443x_priv *chip)
  318. {
  319. struct regmap *r_t = chip->regmap_t;
  320. regmap_update_bits(r_t, PWDSET, PWDSET_OFDMPD_MASK, 0);
  321. }
  322. static bool mn88443x_t_is_valid_clk(u32 adckt, u32 if_freq)
  323. {
  324. if (if_freq == DIRECT_IF_57MHZ) {
  325. if (adckt >= CLK_DIRECT && adckt <= 21000000)
  326. return true;
  327. if (adckt >= 25300000 && adckt <= CLK_MAX)
  328. return true;
  329. } else if (if_freq == DIRECT_IF_44MHZ) {
  330. if (adckt >= 25000000 && adckt <= CLK_MAX)
  331. return true;
  332. } else if (if_freq >= LOW_IF_4MHZ && if_freq < DIRECT_IF_44MHZ) {
  333. if (adckt >= CLK_DIRECT && adckt <= CLK_MAX)
  334. return true;
  335. }
  336. return false;
  337. }
  338. static int mn88443x_t_set_freq(struct mn88443x_priv *chip)
  339. {
  340. struct device *dev = &chip->client_s->dev;
  341. struct regmap *r_t = chip->regmap_t;
  342. s64 adckt, nco, ad_t;
  343. u32 m, v;
  344. /* Clock buffer (but not supported) or XTAL */
  345. if (chip->clk_freq >= CLK_LOW && chip->clk_freq < CLK_DIRECT) {
  346. chip->use_clkbuf = true;
  347. regmap_write(r_t, CLKSET1_T, 0x07);
  348. adckt = 0;
  349. } else {
  350. chip->use_clkbuf = false;
  351. regmap_write(r_t, CLKSET1_T, 0x00);
  352. adckt = chip->clk_freq;
  353. }
  354. if (!mn88443x_t_is_valid_clk(adckt, chip->if_freq)) {
  355. dev_err(dev, "Invalid clock, CLK:%d, ADCKT:%lld, IF:%d\n",
  356. chip->clk_freq, adckt, chip->if_freq);
  357. return -EINVAL;
  358. }
  359. /* Direct IF or Low IF */
  360. if (chip->if_freq == DIRECT_IF_57MHZ ||
  361. chip->if_freq == DIRECT_IF_44MHZ)
  362. nco = adckt * 2 - chip->if_freq;
  363. else
  364. nco = -((s64)chip->if_freq);
  365. nco = div_s64(nco << 24, adckt);
  366. ad_t = div_s64(adckt << 22, S_T_FREQ);
  367. regmap_write(r_t, NCOFREQU_T, nco >> 16);
  368. regmap_write(r_t, NCOFREQM_T, nco >> 8);
  369. regmap_write(r_t, NCOFREQL_T, nco);
  370. regmap_write(r_t, FADU_T, ad_t >> 16);
  371. regmap_write(r_t, FADM_T, ad_t >> 8);
  372. regmap_write(r_t, FADL_T, ad_t);
  373. /* Level of IF */
  374. m = ADCSET1_T_REFSEL_MASK;
  375. v = ADCSET1_T_REFSEL_1_5V;
  376. regmap_update_bits(r_t, ADCSET1_T, m, v);
  377. /* Polarity of AGC */
  378. v = AGCSET2_T_IFPOLINV_INC | AGCSET2_T_RFPOLINV_INC;
  379. regmap_update_bits(r_t, AGCSET2_T, v, v);
  380. /* Lower output level of AGC */
  381. regmap_write(r_t, AGCV3_T, 0x00);
  382. regmap_write(r_t, MDSET_T, 0xfa);
  383. return 0;
  384. }
  385. static void mn88443x_t_tune(struct mn88443x_priv *chip,
  386. struct dtv_frontend_properties *c)
  387. {
  388. struct regmap *r_t = chip->regmap_t;
  389. u32 m, v;
  390. m = MDSET_T_MDAUTO_MASK | MDSET_T_FFTS_MASK | MDSET_T_GI_MASK;
  391. v = MDSET_T_MDAUTO_AUTO | MDSET_T_FFTS_MODE3 | MDSET_T_GI_1_8;
  392. regmap_update_bits(r_t, MDSET_T, m, v);
  393. regmap_write(r_t, MDASET_T, 0);
  394. }
  395. static int mn88443x_t_read_status(struct mn88443x_priv *chip,
  396. struct dtv_frontend_properties *c,
  397. enum fe_status *status)
  398. {
  399. struct regmap *r_t = chip->regmap_t;
  400. u32 seqrd, st, flg, tmpu, tmpm, tmpl;
  401. u64 tmp;
  402. /* Sync detection */
  403. regmap_read(r_t, SSEQRD_T, &seqrd);
  404. st = seqrd & SSEQRD_T_SSEQSTRD_MASK;
  405. *status = 0;
  406. if (st >= SSEQRD_T_SSEQSTRD_SYNC)
  407. *status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  408. if (st >= SSEQRD_T_SSEQSTRD_FRAME_SYNC)
  409. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
  410. /* Signal strength */
  411. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  412. if (*status & FE_HAS_SIGNAL) {
  413. u32 agc;
  414. regmap_read(r_t, AGCRDU_T, &tmpu);
  415. regmap_read(r_t, AGCRDL_T, &tmpl);
  416. agc = (tmpu << 8) | tmpl;
  417. c->strength.len = 1;
  418. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  419. c->strength.stat[0].uvalue = agc;
  420. }
  421. /* C/N rate */
  422. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  423. if (*status & FE_HAS_VITERBI) {
  424. u32 cnr;
  425. regmap_read(r_t, CNRDU_T, &tmpu);
  426. regmap_read(r_t, CNRDL_T, &tmpl);
  427. if (tmpu || tmpl) {
  428. /* CNR[dB]: 10 * (log10(65536 / value) + 0.2) */
  429. /* intlog10(65536) = 80807124, 0.2 * 2^24 = 3355443 */
  430. tmp = (u64)80807124 - intlog10((tmpu << 8) | tmpl)
  431. + 3355443;
  432. cnr = div_u64(tmp * 10000, 1 << 24);
  433. } else {
  434. cnr = 0;
  435. }
  436. c->cnr.len = 1;
  437. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  438. c->cnr.stat[0].uvalue = cnr;
  439. }
  440. /* BER */
  441. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  442. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  443. regmap_read(r_t, BERFLG_T, &flg);
  444. if ((*status & FE_HAS_VITERBI) && (flg & BERFLG_T_BERVRDYA)) {
  445. u32 bit_err, bit_cnt;
  446. regmap_read(r_t, BERRDU_T, &tmpu);
  447. regmap_read(r_t, BERRDM_T, &tmpm);
  448. regmap_read(r_t, BERRDL_T, &tmpl);
  449. bit_err = (tmpu << 16) | (tmpm << 8) | tmpl;
  450. regmap_read(r_t, BERLENRDU_T, &tmpu);
  451. regmap_read(r_t, BERLENRDL_T, &tmpl);
  452. bit_cnt = ((tmpu << 8) | tmpl) * 203 * 8;
  453. if (bit_cnt) {
  454. c->post_bit_error.len = 1;
  455. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  456. c->post_bit_error.stat[0].uvalue = bit_err;
  457. c->post_bit_count.len = 1;
  458. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  459. c->post_bit_count.stat[0].uvalue = bit_cnt;
  460. }
  461. }
  462. return 0;
  463. }
  464. static int mn88443x_sleep(struct dvb_frontend *fe)
  465. {
  466. struct mn88443x_priv *chip = fe->demodulator_priv;
  467. mn88443x_s_sleep(chip);
  468. mn88443x_t_sleep(chip);
  469. return 0;
  470. }
  471. static int mn88443x_set_frontend(struct dvb_frontend *fe)
  472. {
  473. struct mn88443x_priv *chip = fe->demodulator_priv;
  474. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  475. struct regmap *r_s = chip->regmap_s;
  476. struct regmap *r_t = chip->regmap_t;
  477. u8 tssel = 0, intsel = 0;
  478. if (c->delivery_system == SYS_ISDBS) {
  479. mn88443x_s_wake(chip);
  480. mn88443x_t_sleep(chip);
  481. tssel = TSSET1_TSASEL_ISDBS;
  482. intsel = TSSET3_INTASEL_S;
  483. } else if (c->delivery_system == SYS_ISDBT) {
  484. mn88443x_s_sleep(chip);
  485. mn88443x_t_wake(chip);
  486. mn88443x_t_set_freq(chip);
  487. tssel = TSSET1_TSASEL_ISDBT;
  488. intsel = TSSET3_INTASEL_T;
  489. }
  490. regmap_update_bits(r_t, TSSET1,
  491. TSSET1_TSASEL_MASK | TSSET1_TSBSEL_MASK,
  492. tssel | TSSET1_TSBSEL_NONE);
  493. regmap_write(r_t, TSSET2, 0);
  494. regmap_update_bits(r_t, TSSET3,
  495. TSSET3_INTASEL_MASK | TSSET3_INTBSEL_MASK,
  496. intsel | TSSET3_INTBSEL_NONE);
  497. regmap_write(r_t, DOSET1_T, 0x95);
  498. regmap_write(r_s, DOSET1_S, 0x80);
  499. if (c->delivery_system == SYS_ISDBS)
  500. mn88443x_s_tune(chip, c);
  501. else if (c->delivery_system == SYS_ISDBT)
  502. mn88443x_t_tune(chip, c);
  503. if (fe->ops.tuner_ops.set_params) {
  504. if (fe->ops.i2c_gate_ctrl)
  505. fe->ops.i2c_gate_ctrl(fe, 1);
  506. fe->ops.tuner_ops.set_params(fe);
  507. if (fe->ops.i2c_gate_ctrl)
  508. fe->ops.i2c_gate_ctrl(fe, 0);
  509. }
  510. return 0;
  511. }
  512. static int mn88443x_get_tune_settings(struct dvb_frontend *fe,
  513. struct dvb_frontend_tune_settings *s)
  514. {
  515. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  516. s->min_delay_ms = 850;
  517. if (c->delivery_system == SYS_ISDBS) {
  518. s->max_drift = 30000 * 2 + 1;
  519. s->step_size = 30000;
  520. } else if (c->delivery_system == SYS_ISDBT) {
  521. s->max_drift = 142857 * 2 + 1;
  522. s->step_size = 142857 * 2;
  523. }
  524. return 0;
  525. }
  526. static int mn88443x_read_status(struct dvb_frontend *fe, enum fe_status *status)
  527. {
  528. struct mn88443x_priv *chip = fe->demodulator_priv;
  529. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  530. if (c->delivery_system == SYS_ISDBS)
  531. return mn88443x_s_read_status(chip, c, status);
  532. if (c->delivery_system == SYS_ISDBT)
  533. return mn88443x_t_read_status(chip, c, status);
  534. return -EINVAL;
  535. }
  536. static const struct dvb_frontend_ops mn88443x_ops = {
  537. .delsys = { SYS_ISDBS, SYS_ISDBT },
  538. .info = {
  539. .name = "Socionext MN88443x",
  540. .frequency_min_hz = 470 * MHz,
  541. .frequency_max_hz = 2071 * MHz,
  542. .symbol_rate_min = 28860000,
  543. .symbol_rate_max = 28860000,
  544. .caps = FE_CAN_INVERSION_AUTO | FE_CAN_FEC_AUTO |
  545. FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
  546. FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
  547. },
  548. .sleep = mn88443x_sleep,
  549. .set_frontend = mn88443x_set_frontend,
  550. .get_tune_settings = mn88443x_get_tune_settings,
  551. .read_status = mn88443x_read_status,
  552. };
  553. static const struct regmap_config regmap_config = {
  554. .reg_bits = 8,
  555. .val_bits = 8,
  556. .cache_type = REGCACHE_NONE,
  557. };
  558. static int mn88443x_probe(struct i2c_client *client,
  559. const struct i2c_device_id *id)
  560. {
  561. struct mn88443x_config *conf = client->dev.platform_data;
  562. struct mn88443x_priv *chip;
  563. struct device *dev = &client->dev;
  564. int ret;
  565. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  566. if (!chip)
  567. return -ENOMEM;
  568. if (dev->of_node)
  569. chip->spec = of_device_get_match_data(dev);
  570. else
  571. chip->spec = (struct mn88443x_spec *)id->driver_data;
  572. if (!chip->spec)
  573. return -EINVAL;
  574. chip->mclk = devm_clk_get(dev, "mclk");
  575. if (IS_ERR(chip->mclk) && !conf) {
  576. dev_err(dev, "Failed to request mclk: %ld\n",
  577. PTR_ERR(chip->mclk));
  578. return PTR_ERR(chip->mclk);
  579. }
  580. ret = of_property_read_u32(dev->of_node, "if-frequency",
  581. &chip->if_freq);
  582. if (ret && !conf) {
  583. dev_err(dev, "Failed to load IF frequency: %d.\n", ret);
  584. return ret;
  585. }
  586. chip->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  587. GPIOD_OUT_HIGH);
  588. if (IS_ERR(chip->reset_gpio)) {
  589. dev_err(dev, "Failed to request reset_gpio: %ld\n",
  590. PTR_ERR(chip->reset_gpio));
  591. return PTR_ERR(chip->reset_gpio);
  592. }
  593. if (conf) {
  594. chip->mclk = conf->mclk;
  595. chip->if_freq = conf->if_freq;
  596. chip->reset_gpio = conf->reset_gpio;
  597. *conf->fe = &chip->fe;
  598. }
  599. chip->client_s = client;
  600. chip->regmap_s = devm_regmap_init_i2c(chip->client_s, &regmap_config);
  601. if (IS_ERR(chip->regmap_s))
  602. return PTR_ERR(chip->regmap_s);
  603. /*
  604. * Chip has two I2C addresses for each satellite/terrestrial system.
  605. * ISDB-T uses address ISDB-S + 4, so we register a dummy client.
  606. */
  607. chip->client_t = i2c_new_dummy_device(client->adapter, client->addr + 4);
  608. if (IS_ERR(chip->client_t))
  609. return PTR_ERR(chip->client_t);
  610. chip->regmap_t = devm_regmap_init_i2c(chip->client_t, &regmap_config);
  611. if (IS_ERR(chip->regmap_t)) {
  612. ret = PTR_ERR(chip->regmap_t);
  613. goto err_i2c_t;
  614. }
  615. chip->clk_freq = clk_get_rate(chip->mclk);
  616. memcpy(&chip->fe.ops, &mn88443x_ops, sizeof(mn88443x_ops));
  617. chip->fe.demodulator_priv = chip;
  618. i2c_set_clientdata(client, chip);
  619. mn88443x_cmn_power_on(chip);
  620. mn88443x_s_sleep(chip);
  621. mn88443x_t_sleep(chip);
  622. return 0;
  623. err_i2c_t:
  624. i2c_unregister_device(chip->client_t);
  625. return ret;
  626. }
  627. static int mn88443x_remove(struct i2c_client *client)
  628. {
  629. struct mn88443x_priv *chip = i2c_get_clientdata(client);
  630. mn88443x_cmn_power_off(chip);
  631. i2c_unregister_device(chip->client_t);
  632. return 0;
  633. }
  634. static const struct mn88443x_spec mn88443x_spec_pri = {
  635. .primary = true,
  636. };
  637. static const struct mn88443x_spec mn88443x_spec_sec = {
  638. .primary = false,
  639. };
  640. static const struct of_device_id mn88443x_of_match[] = {
  641. { .compatible = "socionext,mn884433", .data = &mn88443x_spec_pri, },
  642. { .compatible = "socionext,mn884434-0", .data = &mn88443x_spec_pri, },
  643. { .compatible = "socionext,mn884434-1", .data = &mn88443x_spec_sec, },
  644. {}
  645. };
  646. MODULE_DEVICE_TABLE(of, mn88443x_of_match);
  647. static const struct i2c_device_id mn88443x_i2c_id[] = {
  648. { "mn884433", (kernel_ulong_t)&mn88443x_spec_pri },
  649. { "mn884434-0", (kernel_ulong_t)&mn88443x_spec_pri },
  650. { "mn884434-1", (kernel_ulong_t)&mn88443x_spec_sec },
  651. {}
  652. };
  653. MODULE_DEVICE_TABLE(i2c, mn88443x_i2c_id);
  654. static struct i2c_driver mn88443x_driver = {
  655. .driver = {
  656. .name = "mn88443x",
  657. .of_match_table = of_match_ptr(mn88443x_of_match),
  658. },
  659. .probe = mn88443x_probe,
  660. .remove = mn88443x_remove,
  661. .id_table = mn88443x_i2c_id,
  662. };
  663. module_i2c_driver(mn88443x_driver);
  664. MODULE_AUTHOR("Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>");
  665. MODULE_DESCRIPTION("Socionext MN88443x series demodulator driver.");
  666. MODULE_LICENSE("GPL v2");