mb86a20s.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /*
  2. * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
  3. *
  4. * Copyright (C) 2010-2013 Mauro Carvalho Chehab <mchehab@redhat.com>
  5. * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  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 GNU
  14. * General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <asm/div64.h>
  18. #include "dvb_frontend.h"
  19. #include "mb86a20s.h"
  20. static int debug = 1;
  21. module_param(debug, int, 0644);
  22. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  23. struct mb86a20s_state {
  24. struct i2c_adapter *i2c;
  25. const struct mb86a20s_config *config;
  26. u32 last_frequency;
  27. struct dvb_frontend frontend;
  28. bool need_init;
  29. };
  30. struct regdata {
  31. u8 reg;
  32. u8 data;
  33. };
  34. /*
  35. * Initialization sequence: Use whatevere default values that PV SBTVD
  36. * does on its initialisation, obtained via USB snoop
  37. */
  38. static struct regdata mb86a20s_init[] = {
  39. { 0x70, 0x0f },
  40. { 0x70, 0xff },
  41. { 0x08, 0x01 },
  42. { 0x09, 0x3e },
  43. { 0x50, 0xd1 }, { 0x51, 0x22 },
  44. { 0x39, 0x01 },
  45. { 0x71, 0x00 },
  46. { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
  47. { 0x28, 0x20 }, { 0x29, 0x33 }, { 0x2a, 0xdf }, { 0x2b, 0xa9 },
  48. { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
  49. { 0x3b, 0x21 },
  50. { 0x3c, 0x3a },
  51. { 0x01, 0x0d },
  52. { 0x04, 0x08 }, { 0x05, 0x05 },
  53. { 0x04, 0x0e }, { 0x05, 0x00 },
  54. { 0x04, 0x0f }, { 0x05, 0x14 },
  55. { 0x04, 0x0b }, { 0x05, 0x8c },
  56. { 0x04, 0x00 }, { 0x05, 0x00 },
  57. { 0x04, 0x01 }, { 0x05, 0x07 },
  58. { 0x04, 0x02 }, { 0x05, 0x0f },
  59. { 0x04, 0x03 }, { 0x05, 0xa0 },
  60. { 0x04, 0x09 }, { 0x05, 0x00 },
  61. { 0x04, 0x0a }, { 0x05, 0xff },
  62. { 0x04, 0x27 }, { 0x05, 0x64 },
  63. { 0x04, 0x28 }, { 0x05, 0x00 },
  64. { 0x04, 0x1e }, { 0x05, 0xff },
  65. { 0x04, 0x29 }, { 0x05, 0x0a },
  66. { 0x04, 0x32 }, { 0x05, 0x0a },
  67. { 0x04, 0x14 }, { 0x05, 0x02 },
  68. { 0x04, 0x04 }, { 0x05, 0x00 },
  69. { 0x04, 0x05 }, { 0x05, 0x22 },
  70. { 0x04, 0x06 }, { 0x05, 0x0e },
  71. { 0x04, 0x07 }, { 0x05, 0xd8 },
  72. { 0x04, 0x12 }, { 0x05, 0x00 },
  73. { 0x04, 0x13 }, { 0x05, 0xff },
  74. { 0x04, 0x15 }, { 0x05, 0x4e },
  75. { 0x04, 0x16 }, { 0x05, 0x20 },
  76. /*
  77. * On this demod, when the bit count reaches the count below,
  78. * it collects the bit error count. The bit counters are initialized
  79. * to 65535 here. This warrants that all of them will be quickly
  80. * calculated when device gets locked. As TMCC is parsed, the values
  81. * can be adjusted later in the driver's code.
  82. */
  83. { 0x52, 0x01 }, /* Turn on BER before Viterbi */
  84. { 0x50, 0xa7 }, { 0x51, 0x00 },
  85. { 0x50, 0xa8 }, { 0x51, 0xff },
  86. { 0x50, 0xa9 }, { 0x51, 0xff },
  87. { 0x50, 0xaa }, { 0x51, 0x00 },
  88. { 0x50, 0xab }, { 0x51, 0xff },
  89. { 0x50, 0xac }, { 0x51, 0xff },
  90. { 0x50, 0xad }, { 0x51, 0x00 },
  91. { 0x50, 0xae }, { 0x51, 0xff },
  92. { 0x50, 0xaf }, { 0x51, 0xff },
  93. { 0x5e, 0x00 }, /* Turn off BER after Viterbi */
  94. { 0x50, 0xdc }, { 0x51, 0x01 },
  95. { 0x50, 0xdd }, { 0x51, 0xf4 },
  96. { 0x50, 0xde }, { 0x51, 0x01 },
  97. { 0x50, 0xdf }, { 0x51, 0xf4 },
  98. { 0x50, 0xe0 }, { 0x51, 0x01 },
  99. { 0x50, 0xe1 }, { 0x51, 0xf4 },
  100. { 0x50, 0xb0 }, { 0x51, 0x07 },
  101. { 0x50, 0xb2 }, { 0x51, 0xff },
  102. { 0x50, 0xb3 }, { 0x51, 0xff },
  103. { 0x50, 0xb4 }, { 0x51, 0xff },
  104. { 0x50, 0xb5 }, { 0x51, 0xff },
  105. { 0x50, 0xb6 }, { 0x51, 0xff },
  106. { 0x50, 0xb7 }, { 0x51, 0xff },
  107. { 0x50, 0x50 }, { 0x51, 0x02 },
  108. { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
  109. { 0x45, 0x04 }, /* CN symbol 4 */
  110. { 0x48, 0x04 },
  111. { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
  112. { 0x50, 0xd6 }, { 0x51, 0x1f },
  113. { 0x50, 0xd2 }, { 0x51, 0x03 },
  114. { 0x50, 0xd7 }, { 0x51, 0x3f },
  115. { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
  116. { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
  117. { 0x04, 0x40 }, { 0x05, 0x00 },
  118. { 0x28, 0x00 }, { 0x29, 0x10 },
  119. { 0x28, 0x05 }, { 0x29, 0x02 },
  120. { 0x1c, 0x01 },
  121. { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
  122. { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
  123. { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
  124. { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
  125. { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
  126. { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
  127. { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
  128. { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
  129. { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
  130. { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
  131. { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
  132. { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
  133. { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
  134. { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
  135. { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
  136. { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
  137. { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
  138. { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
  139. { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
  140. { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
  141. { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
  142. { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
  143. { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
  144. { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
  145. { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
  146. { 0x50, 0x1e }, { 0x51, 0x5d },
  147. { 0x50, 0x22 }, { 0x51, 0x00 },
  148. { 0x50, 0x23 }, { 0x51, 0xc8 },
  149. { 0x50, 0x24 }, { 0x51, 0x00 },
  150. { 0x50, 0x25 }, { 0x51, 0xf0 },
  151. { 0x50, 0x26 }, { 0x51, 0x00 },
  152. { 0x50, 0x27 }, { 0x51, 0xc3 },
  153. { 0x50, 0x39 }, { 0x51, 0x02 },
  154. { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
  155. { 0xd0, 0x00 },
  156. };
  157. static struct regdata mb86a20s_reset_reception[] = {
  158. { 0x70, 0xf0 },
  159. { 0x70, 0xff },
  160. { 0x08, 0x01 },
  161. { 0x08, 0x00 },
  162. };
  163. static struct regdata mb86a20s_vber_reset[] = {
  164. { 0x53, 0x00 }, /* VBER Counter reset */
  165. { 0x53, 0x07 },
  166. };
  167. static struct regdata mb86a20s_per_reset[] = {
  168. { 0x50, 0xb1 }, /* PER Counter reset */
  169. { 0x51, 0x07 },
  170. { 0x51, 0x00 },
  171. };
  172. /*
  173. * I2C read/write functions and macros
  174. */
  175. static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
  176. u8 i2c_addr, u8 reg, u8 data)
  177. {
  178. u8 buf[] = { reg, data };
  179. struct i2c_msg msg = {
  180. .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
  181. };
  182. int rc;
  183. rc = i2c_transfer(state->i2c, &msg, 1);
  184. if (rc != 1) {
  185. dev_err(&state->i2c->dev,
  186. "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
  187. __func__, rc, reg, data);
  188. return rc;
  189. }
  190. return 0;
  191. }
  192. static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
  193. u8 i2c_addr, struct regdata *rd, int size)
  194. {
  195. int i, rc;
  196. for (i = 0; i < size; i++) {
  197. rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
  198. rd[i].data);
  199. if (rc < 0)
  200. return rc;
  201. }
  202. return 0;
  203. }
  204. static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
  205. u8 i2c_addr, u8 reg)
  206. {
  207. u8 val;
  208. int rc;
  209. struct i2c_msg msg[] = {
  210. { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
  211. { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
  212. };
  213. rc = i2c_transfer(state->i2c, msg, 2);
  214. if (rc != 2) {
  215. dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
  216. __func__, reg, rc);
  217. return (rc < 0) ? rc : -EIO;
  218. }
  219. return val;
  220. }
  221. #define mb86a20s_readreg(state, reg) \
  222. mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
  223. #define mb86a20s_writereg(state, reg, val) \
  224. mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
  225. #define mb86a20s_writeregdata(state, regdata) \
  226. mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
  227. regdata, ARRAY_SIZE(regdata))
  228. /*
  229. * Ancillary internal routines (likely compiled inlined)
  230. *
  231. * The functions below assume that gateway lock has already obtained
  232. */
  233. static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
  234. {
  235. struct mb86a20s_state *state = fe->demodulator_priv;
  236. int val;
  237. *status = 0;
  238. val = mb86a20s_readreg(state, 0x0a) & 0xf;
  239. if (val < 0)
  240. return val;
  241. if (val >= 2)
  242. *status |= FE_HAS_SIGNAL;
  243. if (val >= 4)
  244. *status |= FE_HAS_CARRIER;
  245. if (val >= 5)
  246. *status |= FE_HAS_VITERBI;
  247. if (val >= 7)
  248. *status |= FE_HAS_SYNC;
  249. if (val >= 8) /* Maybe 9? */
  250. *status |= FE_HAS_LOCK;
  251. dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
  252. __func__, *status, val);
  253. return 0;
  254. }
  255. static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
  256. {
  257. struct mb86a20s_state *state = fe->demodulator_priv;
  258. int rc;
  259. unsigned rf_max, rf_min, rf;
  260. /* Does a binary search to get RF strength */
  261. rf_max = 0xfff;
  262. rf_min = 0;
  263. do {
  264. rf = (rf_max + rf_min) / 2;
  265. rc = mb86a20s_writereg(state, 0x04, 0x1f);
  266. if (rc < 0)
  267. return rc;
  268. rc = mb86a20s_writereg(state, 0x05, rf >> 8);
  269. if (rc < 0)
  270. return rc;
  271. rc = mb86a20s_writereg(state, 0x04, 0x20);
  272. if (rc < 0)
  273. return rc;
  274. rc = mb86a20s_writereg(state, 0x04, rf);
  275. if (rc < 0)
  276. return rc;
  277. rc = mb86a20s_readreg(state, 0x02);
  278. if (rc < 0)
  279. return rc;
  280. if (rc & 0x08)
  281. rf_min = (rf_max + rf_min) / 2;
  282. else
  283. rf_max = (rf_max + rf_min) / 2;
  284. if (rf_max - rf_min < 4) {
  285. rf = (rf_max + rf_min) / 2;
  286. /* Rescale it from 2^12 (4096) to 2^16 */
  287. rf <<= (16 - 12);
  288. dev_dbg(&state->i2c->dev,
  289. "%s: signal strength = %d (%d < RF=%d < %d)\n",
  290. __func__, rf, rf_min, rf >> 4, rf_max);
  291. return rf;
  292. }
  293. } while (1);
  294. return 0;
  295. }
  296. static int mb86a20s_get_modulation(struct mb86a20s_state *state,
  297. unsigned layer)
  298. {
  299. int rc;
  300. static unsigned char reg[] = {
  301. [0] = 0x86, /* Layer A */
  302. [1] = 0x8a, /* Layer B */
  303. [2] = 0x8e, /* Layer C */
  304. };
  305. if (layer >= ARRAY_SIZE(reg))
  306. return -EINVAL;
  307. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  308. if (rc < 0)
  309. return rc;
  310. rc = mb86a20s_readreg(state, 0x6e);
  311. if (rc < 0)
  312. return rc;
  313. switch ((rc >> 4) & 0x07) {
  314. case 0:
  315. return DQPSK;
  316. case 1:
  317. return QPSK;
  318. case 2:
  319. return QAM_16;
  320. case 3:
  321. return QAM_64;
  322. default:
  323. return QAM_AUTO;
  324. }
  325. }
  326. static int mb86a20s_get_fec(struct mb86a20s_state *state,
  327. unsigned layer)
  328. {
  329. int rc;
  330. static unsigned char reg[] = {
  331. [0] = 0x87, /* Layer A */
  332. [1] = 0x8b, /* Layer B */
  333. [2] = 0x8f, /* Layer C */
  334. };
  335. if (layer >= ARRAY_SIZE(reg))
  336. return -EINVAL;
  337. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  338. if (rc < 0)
  339. return rc;
  340. rc = mb86a20s_readreg(state, 0x6e);
  341. if (rc < 0)
  342. return rc;
  343. switch ((rc >> 4) & 0x07) {
  344. case 0:
  345. return FEC_1_2;
  346. case 1:
  347. return FEC_2_3;
  348. case 2:
  349. return FEC_3_4;
  350. case 3:
  351. return FEC_5_6;
  352. case 4:
  353. return FEC_7_8;
  354. default:
  355. return FEC_AUTO;
  356. }
  357. }
  358. static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
  359. unsigned layer)
  360. {
  361. int rc;
  362. static unsigned char reg[] = {
  363. [0] = 0x88, /* Layer A */
  364. [1] = 0x8c, /* Layer B */
  365. [2] = 0x90, /* Layer C */
  366. };
  367. if (layer >= ARRAY_SIZE(reg))
  368. return -EINVAL;
  369. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  370. if (rc < 0)
  371. return rc;
  372. rc = mb86a20s_readreg(state, 0x6e);
  373. if (rc < 0)
  374. return rc;
  375. switch ((rc >> 4) & 0x07) {
  376. case 1:
  377. return GUARD_INTERVAL_1_4;
  378. case 2:
  379. return GUARD_INTERVAL_1_8;
  380. case 3:
  381. return GUARD_INTERVAL_1_16;
  382. case 4:
  383. return GUARD_INTERVAL_1_32;
  384. default:
  385. case 0:
  386. return GUARD_INTERVAL_AUTO;
  387. }
  388. }
  389. static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
  390. unsigned layer)
  391. {
  392. int rc, count;
  393. static unsigned char reg[] = {
  394. [0] = 0x89, /* Layer A */
  395. [1] = 0x8d, /* Layer B */
  396. [2] = 0x91, /* Layer C */
  397. };
  398. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  399. if (layer >= ARRAY_SIZE(reg))
  400. return -EINVAL;
  401. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  402. if (rc < 0)
  403. return rc;
  404. rc = mb86a20s_readreg(state, 0x6e);
  405. if (rc < 0)
  406. return rc;
  407. count = (rc >> 4) & 0x0f;
  408. dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
  409. return count;
  410. }
  411. static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
  412. {
  413. struct mb86a20s_state *state = fe->demodulator_priv;
  414. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  415. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  416. /* Fixed parameters */
  417. c->delivery_system = SYS_ISDBT;
  418. c->bandwidth_hz = 6000000;
  419. /* Initialize values that will be later autodetected */
  420. c->isdbt_layer_enabled = 0;
  421. c->transmission_mode = TRANSMISSION_MODE_AUTO;
  422. c->guard_interval = GUARD_INTERVAL_AUTO;
  423. c->isdbt_sb_mode = 0;
  424. c->isdbt_sb_segment_count = 0;
  425. }
  426. static int mb86a20s_get_frontend(struct dvb_frontend *fe)
  427. {
  428. struct mb86a20s_state *state = fe->demodulator_priv;
  429. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  430. int i, rc;
  431. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  432. /* Reset frontend cache to default values */
  433. mb86a20s_reset_frontend_cache(fe);
  434. /* Check for partial reception */
  435. rc = mb86a20s_writereg(state, 0x6d, 0x85);
  436. if (rc < 0)
  437. return rc;
  438. rc = mb86a20s_readreg(state, 0x6e);
  439. if (rc < 0)
  440. return rc;
  441. c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
  442. /* Get per-layer data */
  443. for (i = 0; i < 3; i++) {
  444. dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
  445. __func__, 'A' + i);
  446. rc = mb86a20s_get_segment_count(state, i);
  447. if (rc < 0)
  448. goto noperlayer_error;
  449. if (rc >= 0 && rc < 14)
  450. c->layer[i].segment_count = rc;
  451. else {
  452. c->layer[i].segment_count = 0;
  453. continue;
  454. }
  455. c->isdbt_layer_enabled |= 1 << i;
  456. rc = mb86a20s_get_modulation(state, i);
  457. if (rc < 0)
  458. goto noperlayer_error;
  459. dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
  460. __func__, rc);
  461. c->layer[i].modulation = rc;
  462. rc = mb86a20s_get_fec(state, i);
  463. if (rc < 0)
  464. goto noperlayer_error;
  465. dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
  466. __func__, rc);
  467. c->layer[i].fec = rc;
  468. rc = mb86a20s_get_interleaving(state, i);
  469. if (rc < 0)
  470. goto noperlayer_error;
  471. dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
  472. __func__, rc);
  473. c->layer[i].interleaving = rc;
  474. }
  475. rc = mb86a20s_writereg(state, 0x6d, 0x84);
  476. if (rc < 0)
  477. return rc;
  478. if ((rc & 0x60) == 0x20) {
  479. c->isdbt_sb_mode = 1;
  480. /* At least, one segment should exist */
  481. if (!c->isdbt_sb_segment_count)
  482. c->isdbt_sb_segment_count = 1;
  483. }
  484. /* Get transmission mode and guard interval */
  485. rc = mb86a20s_readreg(state, 0x07);
  486. if (rc < 0)
  487. return rc;
  488. if ((rc & 0x60) == 0x20) {
  489. switch (rc & 0x0c >> 2) {
  490. case 0:
  491. c->transmission_mode = TRANSMISSION_MODE_2K;
  492. break;
  493. case 1:
  494. c->transmission_mode = TRANSMISSION_MODE_4K;
  495. break;
  496. case 2:
  497. c->transmission_mode = TRANSMISSION_MODE_8K;
  498. break;
  499. }
  500. }
  501. if (!(rc & 0x10)) {
  502. switch (rc & 0x3) {
  503. case 0:
  504. c->guard_interval = GUARD_INTERVAL_1_4;
  505. break;
  506. case 1:
  507. c->guard_interval = GUARD_INTERVAL_1_8;
  508. break;
  509. case 2:
  510. c->guard_interval = GUARD_INTERVAL_1_16;
  511. break;
  512. }
  513. }
  514. return 0;
  515. noperlayer_error:
  516. /* per-layer info is incomplete; discard all per-layer */
  517. c->isdbt_layer_enabled = 0;
  518. return rc;
  519. }
  520. static int mb86a20s_reset_counters(struct dvb_frontend *fe)
  521. {
  522. struct mb86a20s_state *state = fe->demodulator_priv;
  523. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  524. int rc, val;
  525. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  526. /* Reset the counters, if the channel changed */
  527. if (state->last_frequency != c->frequency) {
  528. memset(&c->strength, 0, sizeof(c->strength));
  529. memset(&c->cnr, 0, sizeof(c->cnr));
  530. memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
  531. memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
  532. memset(&c->block_error, 0, sizeof(c->block_error));
  533. memset(&c->block_count, 0, sizeof(c->block_count));
  534. state->last_frequency = c->frequency;
  535. }
  536. /* Clear status for most stats */
  537. /* BER counter reset */
  538. rc = mb86a20s_writeregdata(state, mb86a20s_vber_reset);
  539. if (rc < 0)
  540. goto err;
  541. /* MER, PER counter reset */
  542. rc = mb86a20s_writeregdata(state, mb86a20s_per_reset);
  543. if (rc < 0)
  544. goto err;
  545. /* CNR counter reset */
  546. rc = mb86a20s_readreg(state, 0x45);
  547. if (rc < 0)
  548. goto err;
  549. val = rc;
  550. rc = mb86a20s_writereg(state, 0x45, val | 0x10);
  551. if (rc < 0)
  552. goto err;
  553. rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
  554. if (rc < 0)
  555. goto err;
  556. /* MER counter reset */
  557. rc = mb86a20s_writereg(state, 0x50, 0x50);
  558. if (rc < 0)
  559. goto err;
  560. rc = mb86a20s_readreg(state, 0x51);
  561. if (rc < 0)
  562. goto err;
  563. val = rc;
  564. rc = mb86a20s_writereg(state, 0x51, val | 0x01);
  565. if (rc < 0)
  566. goto err;
  567. rc = mb86a20s_writereg(state, 0x51, val & 0x06);
  568. if (rc < 0)
  569. goto err;
  570. goto ok;
  571. err:
  572. dev_err(&state->i2c->dev,
  573. "%s: Can't reset FE statistics (error %d).\n",
  574. __func__, rc);
  575. ok:
  576. return rc;
  577. }
  578. static int mb86a20s_get_ber_before_vterbi(struct dvb_frontend *fe,
  579. unsigned layer,
  580. u32 *error, u32 *count)
  581. {
  582. struct mb86a20s_state *state = fe->demodulator_priv;
  583. int rc;
  584. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  585. if (layer >= 3)
  586. return -EINVAL;
  587. /* Check if the BER measures are already available */
  588. rc = mb86a20s_readreg(state, 0x54);
  589. if (rc < 0)
  590. return rc;
  591. /* Check if data is available for that layer */
  592. if (!(rc & (1 << layer))) {
  593. dev_dbg(&state->i2c->dev,
  594. "%s: BER for layer %c is not available yet.\n",
  595. __func__, 'A' + layer);
  596. return -EBUSY;
  597. }
  598. /* Read Bit Error Count */
  599. rc = mb86a20s_readreg(state, 0x55 + layer * 3);
  600. if (rc < 0)
  601. return rc;
  602. *error = rc << 16;
  603. rc = mb86a20s_readreg(state, 0x56 + layer * 3);
  604. if (rc < 0)
  605. return rc;
  606. *error |= rc << 8;
  607. rc = mb86a20s_readreg(state, 0x57 + layer * 3);
  608. if (rc < 0)
  609. return rc;
  610. *error |= rc;
  611. dev_dbg(&state->i2c->dev,
  612. "%s: bit error before Viterbi for layer %c: %d.\n",
  613. __func__, 'A' + layer, *error);
  614. /* Read Bit Count */
  615. rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
  616. if (rc < 0)
  617. return rc;
  618. rc = mb86a20s_readreg(state, 0x51);
  619. if (rc < 0)
  620. return rc;
  621. *count = rc << 16;
  622. rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
  623. if (rc < 0)
  624. return rc;
  625. rc = mb86a20s_readreg(state, 0x51);
  626. if (rc < 0)
  627. return rc;
  628. *count |= rc << 8;
  629. rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
  630. if (rc < 0)
  631. return rc;
  632. rc = mb86a20s_readreg(state, 0x51);
  633. if (rc < 0)
  634. return rc;
  635. *count |= rc;
  636. dev_dbg(&state->i2c->dev,
  637. "%s: bit count before Viterbi for layer %c: %d.\n",
  638. __func__, 'A' + layer, *count);
  639. /* Reset counter to collect new data */
  640. rc = mb86a20s_writereg(state, 0x53, 0x07 & ~(1 << layer));
  641. if (rc < 0)
  642. return rc;
  643. rc = mb86a20s_writereg(state, 0x53, 0x07);
  644. return 0;
  645. }
  646. static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
  647. {
  648. struct mb86a20s_state *state = fe->demodulator_priv;
  649. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  650. int i;
  651. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  652. /* Fill the length of each status counter */
  653. /* Only global stats */
  654. c->strength.len = 1;
  655. /* Per-layer stats - 3 layers + global */
  656. c->cnr.len = 4;
  657. c->pre_bit_error.len = 4;
  658. c->pre_bit_count.len = 4;
  659. c->block_error.len = 4;
  660. c->block_count.len = 4;
  661. /* Signal is always available */
  662. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  663. c->strength.stat[0].uvalue = 0;
  664. /* Put all of them at FE_SCALE_NOT_AVAILABLE */
  665. for (i = 0; i < 4; i++) {
  666. c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  667. c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  668. c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  669. c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  670. c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  671. }
  672. }
  673. static int mb86a20s_get_stats(struct dvb_frontend *fe)
  674. {
  675. struct mb86a20s_state *state = fe->demodulator_priv;
  676. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  677. int rc = 0, i;
  678. u32 bit_error = 0, bit_count = 0;
  679. u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
  680. int active_layers = 0, ber_layers = 0;
  681. /* Get per-layer stats */
  682. for (i = 0; i < 3; i++) {
  683. if (c->isdbt_layer_enabled & (1 << i)) {
  684. /* Layer is active and has rc segments */
  685. active_layers++;
  686. /* Read per-layer BER */
  687. /* Handle BER before vterbi */
  688. rc = mb86a20s_get_ber_before_vterbi(fe, i,
  689. &bit_error,
  690. &bit_count);
  691. if (rc >= 0) {
  692. c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
  693. c->pre_bit_error.stat[1 + i].uvalue += bit_error;
  694. c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
  695. c->pre_bit_count.stat[1 + i].uvalue += bit_count;
  696. } else if (rc != -EBUSY) {
  697. /*
  698. * If an I/O error happened,
  699. * measures are now unavailable
  700. */
  701. c->pre_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  702. c->pre_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  703. dev_err(&state->i2c->dev,
  704. "%s: Can't get BER for layer %c (error %d).\n",
  705. __func__, 'A' + i, rc);
  706. }
  707. if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
  708. ber_layers++;
  709. /* Update total BER */
  710. t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue;
  711. t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue;
  712. }
  713. }
  714. /*
  715. * Start showing global count if at least one error count is
  716. * available.
  717. */
  718. if (ber_layers) {
  719. /*
  720. * At least one per-layer BER measure was read. We can now
  721. * calculate the total BER
  722. *
  723. * Total Bit Error/Count is calculated as the sum of the
  724. * bit errors on all active layers.
  725. */
  726. c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  727. c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
  728. c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  729. c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
  730. }
  731. return rc;
  732. }
  733. /*
  734. * The functions below are called via DVB callbacks, so they need to
  735. * properly use the I2C gate control
  736. */
  737. static int mb86a20s_initfe(struct dvb_frontend *fe)
  738. {
  739. struct mb86a20s_state *state = fe->demodulator_priv;
  740. int rc;
  741. u8 regD5 = 1;
  742. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  743. if (fe->ops.i2c_gate_ctrl)
  744. fe->ops.i2c_gate_ctrl(fe, 0);
  745. /* Initialize the frontend */
  746. rc = mb86a20s_writeregdata(state, mb86a20s_init);
  747. if (rc < 0)
  748. goto err;
  749. if (!state->config->is_serial) {
  750. regD5 &= ~1;
  751. rc = mb86a20s_writereg(state, 0x50, 0xd5);
  752. if (rc < 0)
  753. goto err;
  754. rc = mb86a20s_writereg(state, 0x51, regD5);
  755. if (rc < 0)
  756. goto err;
  757. }
  758. err:
  759. if (fe->ops.i2c_gate_ctrl)
  760. fe->ops.i2c_gate_ctrl(fe, 1);
  761. if (rc < 0) {
  762. state->need_init = true;
  763. dev_info(&state->i2c->dev,
  764. "mb86a20s: Init failed. Will try again later\n");
  765. } else {
  766. state->need_init = false;
  767. dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
  768. }
  769. return rc;
  770. }
  771. static int mb86a20s_set_frontend(struct dvb_frontend *fe)
  772. {
  773. struct mb86a20s_state *state = fe->demodulator_priv;
  774. int rc;
  775. #if 0
  776. /*
  777. * FIXME: Properly implement the set frontend properties
  778. */
  779. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  780. #endif
  781. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  782. /*
  783. * Gate should already be opened, but it doesn't hurt to
  784. * double-check
  785. */
  786. if (fe->ops.i2c_gate_ctrl)
  787. fe->ops.i2c_gate_ctrl(fe, 1);
  788. fe->ops.tuner_ops.set_params(fe);
  789. /*
  790. * Make it more reliable: if, for some reason, the initial
  791. * device initialization doesn't happen, initialize it when
  792. * a SBTVD parameters are adjusted.
  793. *
  794. * Unfortunately, due to a hard to track bug at tda829x/tda18271,
  795. * the agc callback logic is not called during DVB attach time,
  796. * causing mb86a20s to not be initialized with Kworld SBTVD.
  797. * So, this hack is needed, in order to make Kworld SBTVD to work.
  798. */
  799. if (state->need_init)
  800. mb86a20s_initfe(fe);
  801. if (fe->ops.i2c_gate_ctrl)
  802. fe->ops.i2c_gate_ctrl(fe, 0);
  803. rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
  804. mb86a20s_reset_counters(fe);
  805. if (fe->ops.i2c_gate_ctrl)
  806. fe->ops.i2c_gate_ctrl(fe, 1);
  807. return rc;
  808. }
  809. static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
  810. fe_status_t *status)
  811. {
  812. struct mb86a20s_state *state = fe->demodulator_priv;
  813. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  814. int rc;
  815. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  816. if (fe->ops.i2c_gate_ctrl)
  817. fe->ops.i2c_gate_ctrl(fe, 0);
  818. /* Get lock */
  819. rc = mb86a20s_read_status(fe, status);
  820. if (!(*status & FE_HAS_LOCK)) {
  821. mb86a20s_stats_not_ready(fe);
  822. mb86a20s_reset_frontend_cache(fe);
  823. }
  824. if (rc < 0) {
  825. dev_err(&state->i2c->dev,
  826. "%s: Can't read frontend lock status\n", __func__);
  827. goto error;
  828. }
  829. /* Get signal strength */
  830. rc = mb86a20s_read_signal_strength(fe);
  831. if (rc < 0) {
  832. dev_err(&state->i2c->dev,
  833. "%s: Can't reset VBER registers.\n", __func__);
  834. mb86a20s_stats_not_ready(fe);
  835. mb86a20s_reset_frontend_cache(fe);
  836. rc = 0; /* Status is OK */
  837. goto error;
  838. }
  839. /* Fill signal strength */
  840. c->strength.stat[0].uvalue = rc;
  841. if (*status & FE_HAS_LOCK) {
  842. /* Get TMCC info*/
  843. rc = mb86a20s_get_frontend(fe);
  844. if (rc < 0) {
  845. dev_err(&state->i2c->dev,
  846. "%s: Can't get FE TMCC data.\n", __func__);
  847. rc = 0; /* Status is OK */
  848. goto error;
  849. }
  850. /* Get statistics */
  851. rc = mb86a20s_get_stats(fe);
  852. if (rc < 0 && rc != -EBUSY) {
  853. dev_err(&state->i2c->dev,
  854. "%s: Can't get FE statistics.\n", __func__);
  855. rc = 0;
  856. goto error;
  857. }
  858. rc = 0; /* Don't return EBUSY to userspace */
  859. }
  860. goto ok;
  861. error:
  862. mb86a20s_stats_not_ready(fe);
  863. ok:
  864. if (fe->ops.i2c_gate_ctrl)
  865. fe->ops.i2c_gate_ctrl(fe, 1);
  866. return rc;
  867. }
  868. static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
  869. u16 *strength)
  870. {
  871. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  872. *strength = c->strength.stat[0].uvalue;
  873. return 0;
  874. }
  875. static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
  876. {
  877. /*
  878. * get_frontend is now handled together with other stats
  879. * retrival, when read_status() is called, as some statistics
  880. * will depend on the layers detection.
  881. */
  882. return 0;
  883. };
  884. static int mb86a20s_tune(struct dvb_frontend *fe,
  885. bool re_tune,
  886. unsigned int mode_flags,
  887. unsigned int *delay,
  888. fe_status_t *status)
  889. {
  890. struct mb86a20s_state *state = fe->demodulator_priv;
  891. int rc = 0;
  892. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  893. if (re_tune)
  894. rc = mb86a20s_set_frontend(fe);
  895. if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
  896. mb86a20s_read_status_and_stats(fe, status);
  897. return rc;
  898. }
  899. static void mb86a20s_release(struct dvb_frontend *fe)
  900. {
  901. struct mb86a20s_state *state = fe->demodulator_priv;
  902. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  903. kfree(state);
  904. }
  905. static struct dvb_frontend_ops mb86a20s_ops;
  906. struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
  907. struct i2c_adapter *i2c)
  908. {
  909. struct mb86a20s_state *state;
  910. u8 rev;
  911. dev_dbg(&i2c->dev, "%s called.\n", __func__);
  912. /* allocate memory for the internal state */
  913. state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
  914. if (state == NULL) {
  915. dev_err(&i2c->dev,
  916. "%s: unable to allocate memory for state\n", __func__);
  917. goto error;
  918. }
  919. /* setup the state */
  920. state->config = config;
  921. state->i2c = i2c;
  922. /* create dvb_frontend */
  923. memcpy(&state->frontend.ops, &mb86a20s_ops,
  924. sizeof(struct dvb_frontend_ops));
  925. state->frontend.demodulator_priv = state;
  926. /* Check if it is a mb86a20s frontend */
  927. rev = mb86a20s_readreg(state, 0);
  928. if (rev == 0x13) {
  929. dev_info(&i2c->dev,
  930. "Detected a Fujitsu mb86a20s frontend\n");
  931. } else {
  932. dev_dbg(&i2c->dev,
  933. "Frontend revision %d is unknown - aborting.\n",
  934. rev);
  935. goto error;
  936. }
  937. return &state->frontend;
  938. error:
  939. kfree(state);
  940. return NULL;
  941. }
  942. EXPORT_SYMBOL(mb86a20s_attach);
  943. static struct dvb_frontend_ops mb86a20s_ops = {
  944. .delsys = { SYS_ISDBT },
  945. /* Use dib8000 values per default */
  946. .info = {
  947. .name = "Fujitsu mb86A20s",
  948. .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
  949. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  950. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  951. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  952. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
  953. FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
  954. /* Actually, those values depend on the used tuner */
  955. .frequency_min = 45000000,
  956. .frequency_max = 864000000,
  957. .frequency_stepsize = 62500,
  958. },
  959. .release = mb86a20s_release,
  960. .init = mb86a20s_initfe,
  961. .set_frontend = mb86a20s_set_frontend,
  962. .get_frontend = mb86a20s_get_frontend_dummy,
  963. .read_status = mb86a20s_read_status_and_stats,
  964. .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
  965. .tune = mb86a20s_tune,
  966. };
  967. MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
  968. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  969. MODULE_LICENSE("GPL");