si476x-cmd.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/mfd/si476x-cmd.c -- Subroutines implementing command
  4. * protocol of si476x series of chips
  5. *
  6. * Copyright (C) 2012 Innovative Converged Devices(ICD)
  7. * Copyright (C) 2013 Andrey Smirnov
  8. *
  9. * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/completion.h>
  13. #include <linux/delay.h>
  14. #include <linux/atomic.h>
  15. #include <linux/i2c.h>
  16. #include <linux/device.h>
  17. #include <linux/gpio.h>
  18. #include <linux/videodev2.h>
  19. #include <linux/mfd/si476x-core.h>
  20. #include <asm/unaligned.h>
  21. #define msb(x) ((u8)((u16) x >> 8))
  22. #define lsb(x) ((u8)((u16) x & 0x00FF))
  23. #define CMD_POWER_UP 0x01
  24. #define CMD_POWER_UP_A10_NRESP 1
  25. #define CMD_POWER_UP_A10_NARGS 5
  26. #define CMD_POWER_UP_A20_NRESP 1
  27. #define CMD_POWER_UP_A20_NARGS 5
  28. #define POWER_UP_DELAY_MS 110
  29. #define CMD_POWER_DOWN 0x11
  30. #define CMD_POWER_DOWN_A10_NRESP 1
  31. #define CMD_POWER_DOWN_A20_NRESP 1
  32. #define CMD_POWER_DOWN_A20_NARGS 1
  33. #define CMD_FUNC_INFO 0x12
  34. #define CMD_FUNC_INFO_NRESP 7
  35. #define CMD_SET_PROPERTY 0x13
  36. #define CMD_SET_PROPERTY_NARGS 5
  37. #define CMD_SET_PROPERTY_NRESP 1
  38. #define CMD_GET_PROPERTY 0x14
  39. #define CMD_GET_PROPERTY_NARGS 3
  40. #define CMD_GET_PROPERTY_NRESP 4
  41. #define CMD_AGC_STATUS 0x17
  42. #define CMD_AGC_STATUS_NRESP_A10 2
  43. #define CMD_AGC_STATUS_NRESP_A20 6
  44. #define PIN_CFG_BYTE(x) (0x7F & (x))
  45. #define CMD_DIG_AUDIO_PIN_CFG 0x18
  46. #define CMD_DIG_AUDIO_PIN_CFG_NARGS 4
  47. #define CMD_DIG_AUDIO_PIN_CFG_NRESP 5
  48. #define CMD_ZIF_PIN_CFG 0x19
  49. #define CMD_ZIF_PIN_CFG_NARGS 4
  50. #define CMD_ZIF_PIN_CFG_NRESP 5
  51. #define CMD_IC_LINK_GPO_CTL_PIN_CFG 0x1A
  52. #define CMD_IC_LINK_GPO_CTL_PIN_CFG_NARGS 4
  53. #define CMD_IC_LINK_GPO_CTL_PIN_CFG_NRESP 5
  54. #define CMD_ANA_AUDIO_PIN_CFG 0x1B
  55. #define CMD_ANA_AUDIO_PIN_CFG_NARGS 1
  56. #define CMD_ANA_AUDIO_PIN_CFG_NRESP 2
  57. #define CMD_INTB_PIN_CFG 0x1C
  58. #define CMD_INTB_PIN_CFG_NARGS 2
  59. #define CMD_INTB_PIN_CFG_A10_NRESP 6
  60. #define CMD_INTB_PIN_CFG_A20_NRESP 3
  61. #define CMD_FM_TUNE_FREQ 0x30
  62. #define CMD_FM_TUNE_FREQ_A10_NARGS 5
  63. #define CMD_FM_TUNE_FREQ_A20_NARGS 3
  64. #define CMD_FM_TUNE_FREQ_NRESP 1
  65. #define CMD_FM_RSQ_STATUS 0x32
  66. #define CMD_FM_RSQ_STATUS_A10_NARGS 1
  67. #define CMD_FM_RSQ_STATUS_A10_NRESP 17
  68. #define CMD_FM_RSQ_STATUS_A30_NARGS 1
  69. #define CMD_FM_RSQ_STATUS_A30_NRESP 23
  70. #define CMD_FM_SEEK_START 0x31
  71. #define CMD_FM_SEEK_START_NARGS 1
  72. #define CMD_FM_SEEK_START_NRESP 1
  73. #define CMD_FM_RDS_STATUS 0x36
  74. #define CMD_FM_RDS_STATUS_NARGS 1
  75. #define CMD_FM_RDS_STATUS_NRESP 16
  76. #define CMD_FM_RDS_BLOCKCOUNT 0x37
  77. #define CMD_FM_RDS_BLOCKCOUNT_NARGS 1
  78. #define CMD_FM_RDS_BLOCKCOUNT_NRESP 8
  79. #define CMD_FM_PHASE_DIVERSITY 0x38
  80. #define CMD_FM_PHASE_DIVERSITY_NARGS 1
  81. #define CMD_FM_PHASE_DIVERSITY_NRESP 1
  82. #define CMD_FM_PHASE_DIV_STATUS 0x39
  83. #define CMD_FM_PHASE_DIV_STATUS_NRESP 2
  84. #define CMD_AM_TUNE_FREQ 0x40
  85. #define CMD_AM_TUNE_FREQ_NARGS 3
  86. #define CMD_AM_TUNE_FREQ_NRESP 1
  87. #define CMD_AM_RSQ_STATUS 0x42
  88. #define CMD_AM_RSQ_STATUS_NARGS 1
  89. #define CMD_AM_RSQ_STATUS_NRESP 13
  90. #define CMD_AM_SEEK_START 0x41
  91. #define CMD_AM_SEEK_START_NARGS 1
  92. #define CMD_AM_SEEK_START_NRESP 1
  93. #define CMD_AM_ACF_STATUS 0x45
  94. #define CMD_AM_ACF_STATUS_NRESP 6
  95. #define CMD_AM_ACF_STATUS_NARGS 1
  96. #define CMD_FM_ACF_STATUS 0x35
  97. #define CMD_FM_ACF_STATUS_NRESP 8
  98. #define CMD_FM_ACF_STATUS_NARGS 1
  99. #define CMD_MAX_ARGS_COUNT (10)
  100. enum si476x_acf_status_report_bits {
  101. SI476X_ACF_BLEND_INT = (1 << 4),
  102. SI476X_ACF_HIBLEND_INT = (1 << 3),
  103. SI476X_ACF_HICUT_INT = (1 << 2),
  104. SI476X_ACF_CHBW_INT = (1 << 1),
  105. SI476X_ACF_SOFTMUTE_INT = (1 << 0),
  106. SI476X_ACF_SMUTE = (1 << 0),
  107. SI476X_ACF_SMATTN = 0x1f,
  108. SI476X_ACF_PILOT = (1 << 7),
  109. SI476X_ACF_STBLEND = ~SI476X_ACF_PILOT,
  110. };
  111. enum si476x_agc_status_report_bits {
  112. SI476X_AGC_MXHI = (1 << 5),
  113. SI476X_AGC_MXLO = (1 << 4),
  114. SI476X_AGC_LNAHI = (1 << 3),
  115. SI476X_AGC_LNALO = (1 << 2),
  116. };
  117. enum si476x_errors {
  118. SI476X_ERR_BAD_COMMAND = 0x10,
  119. SI476X_ERR_BAD_ARG1 = 0x11,
  120. SI476X_ERR_BAD_ARG2 = 0x12,
  121. SI476X_ERR_BAD_ARG3 = 0x13,
  122. SI476X_ERR_BAD_ARG4 = 0x14,
  123. SI476X_ERR_BUSY = 0x18,
  124. SI476X_ERR_BAD_INTERNAL_MEMORY = 0x20,
  125. SI476X_ERR_BAD_PATCH = 0x30,
  126. SI476X_ERR_BAD_BOOT_MODE = 0x31,
  127. SI476X_ERR_BAD_PROPERTY = 0x40,
  128. };
  129. static int si476x_core_parse_and_nag_about_error(struct si476x_core *core)
  130. {
  131. int err;
  132. char *cause;
  133. u8 buffer[2];
  134. if (core->revision != SI476X_REVISION_A10) {
  135. err = si476x_core_i2c_xfer(core, SI476X_I2C_RECV,
  136. buffer, sizeof(buffer));
  137. if (err == sizeof(buffer)) {
  138. switch (buffer[1]) {
  139. case SI476X_ERR_BAD_COMMAND:
  140. cause = "Bad command";
  141. err = -EINVAL;
  142. break;
  143. case SI476X_ERR_BAD_ARG1:
  144. cause = "Bad argument #1";
  145. err = -EINVAL;
  146. break;
  147. case SI476X_ERR_BAD_ARG2:
  148. cause = "Bad argument #2";
  149. err = -EINVAL;
  150. break;
  151. case SI476X_ERR_BAD_ARG3:
  152. cause = "Bad argument #3";
  153. err = -EINVAL;
  154. break;
  155. case SI476X_ERR_BAD_ARG4:
  156. cause = "Bad argument #4";
  157. err = -EINVAL;
  158. break;
  159. case SI476X_ERR_BUSY:
  160. cause = "Chip is busy";
  161. err = -EBUSY;
  162. break;
  163. case SI476X_ERR_BAD_INTERNAL_MEMORY:
  164. cause = "Bad internal memory";
  165. err = -EIO;
  166. break;
  167. case SI476X_ERR_BAD_PATCH:
  168. cause = "Bad patch";
  169. err = -EINVAL;
  170. break;
  171. case SI476X_ERR_BAD_BOOT_MODE:
  172. cause = "Bad boot mode";
  173. err = -EINVAL;
  174. break;
  175. case SI476X_ERR_BAD_PROPERTY:
  176. cause = "Bad property";
  177. err = -EINVAL;
  178. break;
  179. default:
  180. cause = "Unknown";
  181. err = -EIO;
  182. }
  183. dev_err(&core->client->dev,
  184. "[Chip error status]: %s\n", cause);
  185. } else {
  186. dev_err(&core->client->dev,
  187. "Failed to fetch error code\n");
  188. err = (err >= 0) ? -EIO : err;
  189. }
  190. } else {
  191. err = -EIO;
  192. }
  193. return err;
  194. }
  195. /**
  196. * si476x_core_send_command() - sends a command to si476x and waits its
  197. * response
  198. * @core: si476x_device structure for the device we are
  199. * communicating with
  200. * @command: command id
  201. * @args: command arguments we are sending
  202. * @argn: actual size of @args
  203. * @resp: buffer to place the expected response from the device
  204. * @respn: actual size of @resp
  205. * @usecs: amount of time to wait before reading the response (in
  206. * usecs)
  207. *
  208. * Function returns 0 on succsess and negative error code on
  209. * failure
  210. */
  211. static int si476x_core_send_command(struct si476x_core *core,
  212. const u8 command,
  213. const u8 args[],
  214. const int argn,
  215. u8 resp[],
  216. const int respn,
  217. const int usecs)
  218. {
  219. struct i2c_client *client = core->client;
  220. int err;
  221. u8 data[CMD_MAX_ARGS_COUNT + 1];
  222. if (argn > CMD_MAX_ARGS_COUNT) {
  223. err = -ENOMEM;
  224. goto exit;
  225. }
  226. if (!client->adapter) {
  227. err = -ENODEV;
  228. goto exit;
  229. }
  230. /* First send the command and its arguments */
  231. data[0] = command;
  232. memcpy(&data[1], args, argn);
  233. dev_dbg(&client->dev, "Command:\n %*ph\n", argn + 1, data);
  234. err = si476x_core_i2c_xfer(core, SI476X_I2C_SEND,
  235. (char *) data, argn + 1);
  236. if (err != argn + 1) {
  237. dev_err(&core->client->dev,
  238. "Error while sending command 0x%02x\n",
  239. command);
  240. err = (err >= 0) ? -EIO : err;
  241. goto exit;
  242. }
  243. /* Set CTS to zero only after the command is send to avoid
  244. * possible racing conditions when working in polling mode */
  245. atomic_set(&core->cts, 0);
  246. /* if (unlikely(command == CMD_POWER_DOWN) */
  247. if (!wait_event_timeout(core->command,
  248. atomic_read(&core->cts),
  249. usecs_to_jiffies(usecs) + 1))
  250. dev_warn(&core->client->dev,
  251. "(%s) [CMD 0x%02x] Answer timeout.\n",
  252. __func__, command);
  253. /*
  254. When working in polling mode, for some reason the tuner will
  255. report CTS bit as being set in the first status byte read,
  256. but all the consequtive ones will return zeros until the
  257. tuner is actually completed the POWER_UP command. To
  258. workaround that we wait for second CTS to be reported
  259. */
  260. if (unlikely(!core->client->irq && command == CMD_POWER_UP)) {
  261. if (!wait_event_timeout(core->command,
  262. atomic_read(&core->cts),
  263. usecs_to_jiffies(usecs) + 1))
  264. dev_warn(&core->client->dev,
  265. "(%s) Power up took too much time.\n",
  266. __func__);
  267. }
  268. /* Then get the response */
  269. err = si476x_core_i2c_xfer(core, SI476X_I2C_RECV, resp, respn);
  270. if (err != respn) {
  271. dev_err(&core->client->dev,
  272. "Error while reading response for command 0x%02x\n",
  273. command);
  274. err = (err >= 0) ? -EIO : err;
  275. goto exit;
  276. }
  277. dev_dbg(&client->dev, "Response:\n %*ph\n", respn, resp);
  278. err = 0;
  279. if (resp[0] & SI476X_ERR) {
  280. dev_err(&core->client->dev,
  281. "[CMD 0x%02x] Chip set error flag\n", command);
  282. err = si476x_core_parse_and_nag_about_error(core);
  283. goto exit;
  284. }
  285. if (!(resp[0] & SI476X_CTS))
  286. err = -EBUSY;
  287. exit:
  288. return err;
  289. }
  290. static int si476x_cmd_clear_stc(struct si476x_core *core)
  291. {
  292. int err;
  293. struct si476x_rsq_status_args args = {
  294. .primary = false,
  295. .rsqack = false,
  296. .attune = false,
  297. .cancel = false,
  298. .stcack = true,
  299. };
  300. switch (core->power_up_parameters.func) {
  301. case SI476X_FUNC_FM_RECEIVER:
  302. err = si476x_core_cmd_fm_rsq_status(core, &args, NULL);
  303. break;
  304. case SI476X_FUNC_AM_RECEIVER:
  305. err = si476x_core_cmd_am_rsq_status(core, &args, NULL);
  306. break;
  307. default:
  308. err = -EINVAL;
  309. }
  310. return err;
  311. }
  312. static int si476x_cmd_tune_seek_freq(struct si476x_core *core,
  313. uint8_t cmd,
  314. const uint8_t args[], size_t argn,
  315. uint8_t *resp, size_t respn)
  316. {
  317. int err;
  318. atomic_set(&core->stc, 0);
  319. err = si476x_core_send_command(core, cmd, args, argn, resp, respn,
  320. SI476X_TIMEOUT_TUNE);
  321. if (!err) {
  322. wait_event_killable(core->tuning,
  323. atomic_read(&core->stc));
  324. si476x_cmd_clear_stc(core);
  325. }
  326. return err;
  327. }
  328. /**
  329. * si476x_cmd_func_info() - send 'FUNC_INFO' command to the device
  330. * @core: device to send the command to
  331. * @info: struct si476x_func_info to fill all the information
  332. * returned by the command
  333. *
  334. * The command requests the firmware and patch version for currently
  335. * loaded firmware (dependent on the function of the device FM/AM/WB)
  336. *
  337. * Function returns 0 on succsess and negative error code on
  338. * failure
  339. */
  340. int si476x_core_cmd_func_info(struct si476x_core *core,
  341. struct si476x_func_info *info)
  342. {
  343. int err;
  344. u8 resp[CMD_FUNC_INFO_NRESP];
  345. err = si476x_core_send_command(core, CMD_FUNC_INFO,
  346. NULL, 0,
  347. resp, ARRAY_SIZE(resp),
  348. SI476X_DEFAULT_TIMEOUT);
  349. info->firmware.major = resp[1];
  350. info->firmware.minor[0] = resp[2];
  351. info->firmware.minor[1] = resp[3];
  352. info->patch_id = ((u16) resp[4] << 8) | resp[5];
  353. info->func = resp[6];
  354. return err;
  355. }
  356. EXPORT_SYMBOL_GPL(si476x_core_cmd_func_info);
  357. /**
  358. * si476x_cmd_set_property() - send 'SET_PROPERTY' command to the device
  359. * @core: device to send the command to
  360. * @property: property address
  361. * @value: property value
  362. *
  363. * Function returns 0 on succsess and negative error code on
  364. * failure
  365. */
  366. int si476x_core_cmd_set_property(struct si476x_core *core,
  367. u16 property, u16 value)
  368. {
  369. u8 resp[CMD_SET_PROPERTY_NRESP];
  370. const u8 args[CMD_SET_PROPERTY_NARGS] = {
  371. 0x00,
  372. msb(property),
  373. lsb(property),
  374. msb(value),
  375. lsb(value),
  376. };
  377. return si476x_core_send_command(core, CMD_SET_PROPERTY,
  378. args, ARRAY_SIZE(args),
  379. resp, ARRAY_SIZE(resp),
  380. SI476X_DEFAULT_TIMEOUT);
  381. }
  382. EXPORT_SYMBOL_GPL(si476x_core_cmd_set_property);
  383. /**
  384. * si476x_cmd_get_property() - send 'GET_PROPERTY' command to the device
  385. * @core: device to send the command to
  386. * @property: property address
  387. *
  388. * Function return the value of property as u16 on success or a
  389. * negative error on failure
  390. */
  391. int si476x_core_cmd_get_property(struct si476x_core *core, u16 property)
  392. {
  393. int err;
  394. u8 resp[CMD_GET_PROPERTY_NRESP];
  395. const u8 args[CMD_GET_PROPERTY_NARGS] = {
  396. 0x00,
  397. msb(property),
  398. lsb(property),
  399. };
  400. err = si476x_core_send_command(core, CMD_GET_PROPERTY,
  401. args, ARRAY_SIZE(args),
  402. resp, ARRAY_SIZE(resp),
  403. SI476X_DEFAULT_TIMEOUT);
  404. if (err < 0)
  405. return err;
  406. else
  407. return get_unaligned_be16(resp + 2);
  408. }
  409. EXPORT_SYMBOL_GPL(si476x_core_cmd_get_property);
  410. /**
  411. * si476x_cmd_dig_audio_pin_cfg() - send 'DIG_AUDIO_PIN_CFG' command to
  412. * the device
  413. * @core: device to send the command to
  414. * @dclk: DCLK pin function configuration:
  415. * #SI476X_DCLK_NOOP - do not modify the behaviour
  416. * #SI476X_DCLK_TRISTATE - put the pin in tristate condition,
  417. * enable 1MOhm pulldown
  418. * #SI476X_DCLK_DAUDIO - set the pin to be a part of digital
  419. * audio interface
  420. * @dfs: DFS pin function configuration:
  421. * #SI476X_DFS_NOOP - do not modify the behaviour
  422. * #SI476X_DFS_TRISTATE - put the pin in tristate condition,
  423. * enable 1MOhm pulldown
  424. * SI476X_DFS_DAUDIO - set the pin to be a part of digital
  425. * audio interface
  426. * @dout: - DOUT pin function configuration:
  427. * SI476X_DOUT_NOOP - do not modify the behaviour
  428. * SI476X_DOUT_TRISTATE - put the pin in tristate condition,
  429. * enable 1MOhm pulldown
  430. * SI476X_DOUT_I2S_OUTPUT - set this pin to be digital out on I2S
  431. * port 1
  432. * SI476X_DOUT_I2S_INPUT - set this pin to be digital in on I2S
  433. * port 1
  434. * @xout: - XOUT pin function configuration:
  435. * SI476X_XOUT_NOOP - do not modify the behaviour
  436. * SI476X_XOUT_TRISTATE - put the pin in tristate condition,
  437. * enable 1MOhm pulldown
  438. * SI476X_XOUT_I2S_INPUT - set this pin to be digital in on I2S
  439. * port 1
  440. * SI476X_XOUT_MODE_SELECT - set this pin to be the input that
  441. * selects the mode of the I2S audio
  442. * combiner (analog or HD)
  443. * [SI4761/63/65/67 Only]
  444. *
  445. * Function returns 0 on success and negative error code on failure
  446. */
  447. int si476x_core_cmd_dig_audio_pin_cfg(struct si476x_core *core,
  448. enum si476x_dclk_config dclk,
  449. enum si476x_dfs_config dfs,
  450. enum si476x_dout_config dout,
  451. enum si476x_xout_config xout)
  452. {
  453. u8 resp[CMD_DIG_AUDIO_PIN_CFG_NRESP];
  454. const u8 args[CMD_DIG_AUDIO_PIN_CFG_NARGS] = {
  455. PIN_CFG_BYTE(dclk),
  456. PIN_CFG_BYTE(dfs),
  457. PIN_CFG_BYTE(dout),
  458. PIN_CFG_BYTE(xout),
  459. };
  460. return si476x_core_send_command(core, CMD_DIG_AUDIO_PIN_CFG,
  461. args, ARRAY_SIZE(args),
  462. resp, ARRAY_SIZE(resp),
  463. SI476X_DEFAULT_TIMEOUT);
  464. }
  465. EXPORT_SYMBOL_GPL(si476x_core_cmd_dig_audio_pin_cfg);
  466. /**
  467. * si476x_cmd_zif_pin_cfg - send 'ZIF_PIN_CFG_COMMAND'
  468. * @core: - device to send the command to
  469. * @iqclk: - IQCL pin function configuration:
  470. * SI476X_IQCLK_NOOP - do not modify the behaviour
  471. * SI476X_IQCLK_TRISTATE - put the pin in tristate condition,
  472. * enable 1MOhm pulldown
  473. * SI476X_IQCLK_IQ - set pin to be a part of I/Q interace
  474. * in master mode
  475. * @iqfs: - IQFS pin function configuration:
  476. * SI476X_IQFS_NOOP - do not modify the behaviour
  477. * SI476X_IQFS_TRISTATE - put the pin in tristate condition,
  478. * enable 1MOhm pulldown
  479. * SI476X_IQFS_IQ - set pin to be a part of I/Q interace
  480. * in master mode
  481. * @iout: - IOUT pin function configuration:
  482. * SI476X_IOUT_NOOP - do not modify the behaviour
  483. * SI476X_IOUT_TRISTATE - put the pin in tristate condition,
  484. * enable 1MOhm pulldown
  485. * SI476X_IOUT_OUTPUT - set pin to be I out
  486. * @qout: - QOUT pin function configuration:
  487. * SI476X_QOUT_NOOP - do not modify the behaviour
  488. * SI476X_QOUT_TRISTATE - put the pin in tristate condition,
  489. * enable 1MOhm pulldown
  490. * SI476X_QOUT_OUTPUT - set pin to be Q out
  491. *
  492. * Function returns 0 on success and negative error code on failure
  493. */
  494. int si476x_core_cmd_zif_pin_cfg(struct si476x_core *core,
  495. enum si476x_iqclk_config iqclk,
  496. enum si476x_iqfs_config iqfs,
  497. enum si476x_iout_config iout,
  498. enum si476x_qout_config qout)
  499. {
  500. u8 resp[CMD_ZIF_PIN_CFG_NRESP];
  501. const u8 args[CMD_ZIF_PIN_CFG_NARGS] = {
  502. PIN_CFG_BYTE(iqclk),
  503. PIN_CFG_BYTE(iqfs),
  504. PIN_CFG_BYTE(iout),
  505. PIN_CFG_BYTE(qout),
  506. };
  507. return si476x_core_send_command(core, CMD_ZIF_PIN_CFG,
  508. args, ARRAY_SIZE(args),
  509. resp, ARRAY_SIZE(resp),
  510. SI476X_DEFAULT_TIMEOUT);
  511. }
  512. EXPORT_SYMBOL_GPL(si476x_core_cmd_zif_pin_cfg);
  513. /**
  514. * si476x_cmd_ic_link_gpo_ctl_pin_cfg - send
  515. * 'IC_LINK_GPIO_CTL_PIN_CFG' comand to the device
  516. * @core: - device to send the command to
  517. * @icin: - ICIN pin function configuration:
  518. * SI476X_ICIN_NOOP - do not modify the behaviour
  519. * SI476X_ICIN_TRISTATE - put the pin in tristate condition,
  520. * enable 1MOhm pulldown
  521. * SI476X_ICIN_GPO1_HIGH - set pin to be an output, drive it high
  522. * SI476X_ICIN_GPO1_LOW - set pin to be an output, drive it low
  523. * SI476X_ICIN_IC_LINK - set the pin to be a part of Inter-Chip link
  524. * @icip: - ICIP pin function configuration:
  525. * SI476X_ICIP_NOOP - do not modify the behaviour
  526. * SI476X_ICIP_TRISTATE - put the pin in tristate condition,
  527. * enable 1MOhm pulldown
  528. * SI476X_ICIP_GPO1_HIGH - set pin to be an output, drive it high
  529. * SI476X_ICIP_GPO1_LOW - set pin to be an output, drive it low
  530. * SI476X_ICIP_IC_LINK - set the pin to be a part of Inter-Chip link
  531. * @icon: - ICON pin function configuration:
  532. * SI476X_ICON_NOOP - do not modify the behaviour
  533. * SI476X_ICON_TRISTATE - put the pin in tristate condition,
  534. * enable 1MOhm pulldown
  535. * SI476X_ICON_I2S - set the pin to be a part of audio
  536. * interface in slave mode (DCLK)
  537. * SI476X_ICON_IC_LINK - set the pin to be a part of Inter-Chip link
  538. * @icop: - ICOP pin function configuration:
  539. * SI476X_ICOP_NOOP - do not modify the behaviour
  540. * SI476X_ICOP_TRISTATE - put the pin in tristate condition,
  541. * enable 1MOhm pulldown
  542. * SI476X_ICOP_I2S - set the pin to be a part of audio
  543. * interface in slave mode (DOUT)
  544. * [Si4761/63/65/67 Only]
  545. * SI476X_ICOP_IC_LINK - set the pin to be a part of Inter-Chip link
  546. *
  547. * Function returns 0 on success and negative error code on failure
  548. */
  549. int si476x_core_cmd_ic_link_gpo_ctl_pin_cfg(struct si476x_core *core,
  550. enum si476x_icin_config icin,
  551. enum si476x_icip_config icip,
  552. enum si476x_icon_config icon,
  553. enum si476x_icop_config icop)
  554. {
  555. u8 resp[CMD_IC_LINK_GPO_CTL_PIN_CFG_NRESP];
  556. const u8 args[CMD_IC_LINK_GPO_CTL_PIN_CFG_NARGS] = {
  557. PIN_CFG_BYTE(icin),
  558. PIN_CFG_BYTE(icip),
  559. PIN_CFG_BYTE(icon),
  560. PIN_CFG_BYTE(icop),
  561. };
  562. return si476x_core_send_command(core, CMD_IC_LINK_GPO_CTL_PIN_CFG,
  563. args, ARRAY_SIZE(args),
  564. resp, ARRAY_SIZE(resp),
  565. SI476X_DEFAULT_TIMEOUT);
  566. }
  567. EXPORT_SYMBOL_GPL(si476x_core_cmd_ic_link_gpo_ctl_pin_cfg);
  568. /**
  569. * si476x_cmd_ana_audio_pin_cfg - send 'ANA_AUDIO_PIN_CFG' to the
  570. * device
  571. * @core: - device to send the command to
  572. * @lrout: - LROUT pin function configuration:
  573. * SI476X_LROUT_NOOP - do not modify the behaviour
  574. * SI476X_LROUT_TRISTATE - put the pin in tristate condition,
  575. * enable 1MOhm pulldown
  576. * SI476X_LROUT_AUDIO - set pin to be audio output
  577. * SI476X_LROUT_MPX - set pin to be MPX output
  578. *
  579. * Function returns 0 on success and negative error code on failure
  580. */
  581. int si476x_core_cmd_ana_audio_pin_cfg(struct si476x_core *core,
  582. enum si476x_lrout_config lrout)
  583. {
  584. u8 resp[CMD_ANA_AUDIO_PIN_CFG_NRESP];
  585. const u8 args[CMD_ANA_AUDIO_PIN_CFG_NARGS] = {
  586. PIN_CFG_BYTE(lrout),
  587. };
  588. return si476x_core_send_command(core, CMD_ANA_AUDIO_PIN_CFG,
  589. args, ARRAY_SIZE(args),
  590. resp, ARRAY_SIZE(resp),
  591. SI476X_DEFAULT_TIMEOUT);
  592. }
  593. EXPORT_SYMBOL_GPL(si476x_core_cmd_ana_audio_pin_cfg);
  594. /**
  595. * si476x_cmd_intb_pin_cfg - send 'INTB_PIN_CFG' command to the device
  596. * @core: - device to send the command to
  597. * @intb: - INTB pin function configuration:
  598. * SI476X_INTB_NOOP - do not modify the behaviour
  599. * SI476X_INTB_TRISTATE - put the pin in tristate condition,
  600. * enable 1MOhm pulldown
  601. * SI476X_INTB_DAUDIO - set pin to be a part of digital
  602. * audio interface in slave mode
  603. * SI476X_INTB_IRQ - set pin to be an interrupt request line
  604. * @a1: - A1 pin function configuration:
  605. * SI476X_A1_NOOP - do not modify the behaviour
  606. * SI476X_A1_TRISTATE - put the pin in tristate condition,
  607. * enable 1MOhm pulldown
  608. * SI476X_A1_IRQ - set pin to be an interrupt request line
  609. *
  610. * Function returns 0 on success and negative error code on failure
  611. */
  612. static int si476x_core_cmd_intb_pin_cfg_a10(struct si476x_core *core,
  613. enum si476x_intb_config intb,
  614. enum si476x_a1_config a1)
  615. {
  616. u8 resp[CMD_INTB_PIN_CFG_A10_NRESP];
  617. const u8 args[CMD_INTB_PIN_CFG_NARGS] = {
  618. PIN_CFG_BYTE(intb),
  619. PIN_CFG_BYTE(a1),
  620. };
  621. return si476x_core_send_command(core, CMD_INTB_PIN_CFG,
  622. args, ARRAY_SIZE(args),
  623. resp, ARRAY_SIZE(resp),
  624. SI476X_DEFAULT_TIMEOUT);
  625. }
  626. static int si476x_core_cmd_intb_pin_cfg_a20(struct si476x_core *core,
  627. enum si476x_intb_config intb,
  628. enum si476x_a1_config a1)
  629. {
  630. u8 resp[CMD_INTB_PIN_CFG_A20_NRESP];
  631. const u8 args[CMD_INTB_PIN_CFG_NARGS] = {
  632. PIN_CFG_BYTE(intb),
  633. PIN_CFG_BYTE(a1),
  634. };
  635. return si476x_core_send_command(core, CMD_INTB_PIN_CFG,
  636. args, ARRAY_SIZE(args),
  637. resp, ARRAY_SIZE(resp),
  638. SI476X_DEFAULT_TIMEOUT);
  639. }
  640. /**
  641. * si476x_cmd_am_rsq_status - send 'AM_RSQ_STATUS' command to the
  642. * device
  643. * @core: - device to send the command to
  644. * @rsqargs: - pointer to a structure containing a group of sub-args
  645. * relevant to sending the RSQ status command
  646. * @report: - all signal quality information retured by the command
  647. * (if NULL then the output of the command is ignored)
  648. *
  649. * Function returns 0 on success and negative error code on failure
  650. */
  651. int si476x_core_cmd_am_rsq_status(struct si476x_core *core,
  652. struct si476x_rsq_status_args *rsqargs,
  653. struct si476x_rsq_status_report *report)
  654. {
  655. int err;
  656. u8 resp[CMD_AM_RSQ_STATUS_NRESP];
  657. const u8 args[CMD_AM_RSQ_STATUS_NARGS] = {
  658. rsqargs->rsqack << 3 | rsqargs->attune << 2 |
  659. rsqargs->cancel << 1 | rsqargs->stcack,
  660. };
  661. err = si476x_core_send_command(core, CMD_AM_RSQ_STATUS,
  662. args, ARRAY_SIZE(args),
  663. resp, ARRAY_SIZE(resp),
  664. SI476X_DEFAULT_TIMEOUT);
  665. /*
  666. * Besides getting received signal quality information this
  667. * command can be used to just acknowledge different interrupt
  668. * flags in those cases it is useless to copy and parse
  669. * received data so user can pass NULL, and thus avoid
  670. * unnecessary copying.
  671. */
  672. if (!report)
  673. return err;
  674. report->snrhint = 0x08 & resp[1];
  675. report->snrlint = 0x04 & resp[1];
  676. report->rssihint = 0x02 & resp[1];
  677. report->rssilint = 0x01 & resp[1];
  678. report->bltf = 0x80 & resp[2];
  679. report->snr_ready = 0x20 & resp[2];
  680. report->rssiready = 0x08 & resp[2];
  681. report->afcrl = 0x02 & resp[2];
  682. report->valid = 0x01 & resp[2];
  683. report->readfreq = get_unaligned_be16(resp + 3);
  684. report->freqoff = resp[5];
  685. report->rssi = resp[6];
  686. report->snr = resp[7];
  687. report->lassi = resp[9];
  688. report->hassi = resp[10];
  689. report->mult = resp[11];
  690. report->dev = resp[12];
  691. return err;
  692. }
  693. EXPORT_SYMBOL_GPL(si476x_core_cmd_am_rsq_status);
  694. int si476x_core_cmd_fm_acf_status(struct si476x_core *core,
  695. struct si476x_acf_status_report *report)
  696. {
  697. int err;
  698. u8 resp[CMD_FM_ACF_STATUS_NRESP];
  699. const u8 args[CMD_FM_ACF_STATUS_NARGS] = {
  700. 0x0,
  701. };
  702. if (!report)
  703. return -EINVAL;
  704. err = si476x_core_send_command(core, CMD_FM_ACF_STATUS,
  705. args, ARRAY_SIZE(args),
  706. resp, ARRAY_SIZE(resp),
  707. SI476X_DEFAULT_TIMEOUT);
  708. if (err < 0)
  709. return err;
  710. report->blend_int = resp[1] & SI476X_ACF_BLEND_INT;
  711. report->hblend_int = resp[1] & SI476X_ACF_HIBLEND_INT;
  712. report->hicut_int = resp[1] & SI476X_ACF_HICUT_INT;
  713. report->chbw_int = resp[1] & SI476X_ACF_CHBW_INT;
  714. report->softmute_int = resp[1] & SI476X_ACF_SOFTMUTE_INT;
  715. report->smute = resp[2] & SI476X_ACF_SMUTE;
  716. report->smattn = resp[3] & SI476X_ACF_SMATTN;
  717. report->chbw = resp[4];
  718. report->hicut = resp[5];
  719. report->hiblend = resp[6];
  720. report->pilot = resp[7] & SI476X_ACF_PILOT;
  721. report->stblend = resp[7] & SI476X_ACF_STBLEND;
  722. return err;
  723. }
  724. EXPORT_SYMBOL_GPL(si476x_core_cmd_fm_acf_status);
  725. int si476x_core_cmd_am_acf_status(struct si476x_core *core,
  726. struct si476x_acf_status_report *report)
  727. {
  728. int err;
  729. u8 resp[CMD_AM_ACF_STATUS_NRESP];
  730. const u8 args[CMD_AM_ACF_STATUS_NARGS] = {
  731. 0x0,
  732. };
  733. if (!report)
  734. return -EINVAL;
  735. err = si476x_core_send_command(core, CMD_AM_ACF_STATUS,
  736. args, ARRAY_SIZE(args),
  737. resp, ARRAY_SIZE(resp),
  738. SI476X_DEFAULT_TIMEOUT);
  739. if (err < 0)
  740. return err;
  741. report->blend_int = resp[1] & SI476X_ACF_BLEND_INT;
  742. report->hblend_int = resp[1] & SI476X_ACF_HIBLEND_INT;
  743. report->hicut_int = resp[1] & SI476X_ACF_HICUT_INT;
  744. report->chbw_int = resp[1] & SI476X_ACF_CHBW_INT;
  745. report->softmute_int = resp[1] & SI476X_ACF_SOFTMUTE_INT;
  746. report->smute = resp[2] & SI476X_ACF_SMUTE;
  747. report->smattn = resp[3] & SI476X_ACF_SMATTN;
  748. report->chbw = resp[4];
  749. report->hicut = resp[5];
  750. return err;
  751. }
  752. EXPORT_SYMBOL_GPL(si476x_core_cmd_am_acf_status);
  753. /**
  754. * si476x_cmd_fm_seek_start - send 'FM_SEEK_START' command to the
  755. * device
  756. * @core: - device to send the command to
  757. * @seekup: - if set the direction of the search is 'up'
  758. * @wrap: - if set seek wraps when hitting band limit
  759. *
  760. * This function begins search for a valid station. The station is
  761. * considered valid when 'FM_VALID_SNR_THRESHOLD' and
  762. * 'FM_VALID_RSSI_THRESHOLD' and 'FM_VALID_MAX_TUNE_ERROR' criteria
  763. * are met.
  764. } *
  765. * Function returns 0 on success and negative error code on failure
  766. */
  767. int si476x_core_cmd_fm_seek_start(struct si476x_core *core,
  768. bool seekup, bool wrap)
  769. {
  770. u8 resp[CMD_FM_SEEK_START_NRESP];
  771. const u8 args[CMD_FM_SEEK_START_NARGS] = {
  772. seekup << 3 | wrap << 2,
  773. };
  774. return si476x_cmd_tune_seek_freq(core, CMD_FM_SEEK_START,
  775. args, sizeof(args),
  776. resp, sizeof(resp));
  777. }
  778. EXPORT_SYMBOL_GPL(si476x_core_cmd_fm_seek_start);
  779. /**
  780. * si476x_cmd_fm_rds_status - send 'FM_RDS_STATUS' command to the
  781. * device
  782. * @core: - device to send the command to
  783. * @status_only: - if set the data is not removed from RDSFIFO,
  784. * RDSFIFOUSED is not decremented and data in all the
  785. * rest RDS data contains the last valid info received
  786. * @mtfifo: if set the command clears RDS receive FIFO
  787. * @intack: if set the command clards the RDSINT bit.
  788. * @report: - all signal quality information retured by the command
  789. * (if NULL then the output of the command is ignored)
  790. *
  791. * Function returns 0 on success and negative error code on failure
  792. */
  793. int si476x_core_cmd_fm_rds_status(struct si476x_core *core,
  794. bool status_only,
  795. bool mtfifo,
  796. bool intack,
  797. struct si476x_rds_status_report *report)
  798. {
  799. int err;
  800. u8 resp[CMD_FM_RDS_STATUS_NRESP];
  801. const u8 args[CMD_FM_RDS_STATUS_NARGS] = {
  802. status_only << 2 | mtfifo << 1 | intack,
  803. };
  804. err = si476x_core_send_command(core, CMD_FM_RDS_STATUS,
  805. args, ARRAY_SIZE(args),
  806. resp, ARRAY_SIZE(resp),
  807. SI476X_DEFAULT_TIMEOUT);
  808. /*
  809. * Besides getting RDS status information this command can be
  810. * used to just acknowledge different interrupt flags in those
  811. * cases it is useless to copy and parse received data so user
  812. * can pass NULL, and thus avoid unnecessary copying.
  813. */
  814. if (err < 0 || report == NULL)
  815. return err;
  816. report->rdstpptyint = 0x10 & resp[1];
  817. report->rdspiint = 0x08 & resp[1];
  818. report->rdssyncint = 0x02 & resp[1];
  819. report->rdsfifoint = 0x01 & resp[1];
  820. report->tpptyvalid = 0x10 & resp[2];
  821. report->pivalid = 0x08 & resp[2];
  822. report->rdssync = 0x02 & resp[2];
  823. report->rdsfifolost = 0x01 & resp[2];
  824. report->tp = 0x20 & resp[3];
  825. report->pty = 0x1f & resp[3];
  826. report->pi = get_unaligned_be16(resp + 4);
  827. report->rdsfifoused = resp[6];
  828. report->ble[V4L2_RDS_BLOCK_A] = 0xc0 & resp[7];
  829. report->ble[V4L2_RDS_BLOCK_B] = 0x30 & resp[7];
  830. report->ble[V4L2_RDS_BLOCK_C] = 0x0c & resp[7];
  831. report->ble[V4L2_RDS_BLOCK_D] = 0x03 & resp[7];
  832. report->rds[V4L2_RDS_BLOCK_A].block = V4L2_RDS_BLOCK_A;
  833. report->rds[V4L2_RDS_BLOCK_A].msb = resp[8];
  834. report->rds[V4L2_RDS_BLOCK_A].lsb = resp[9];
  835. report->rds[V4L2_RDS_BLOCK_B].block = V4L2_RDS_BLOCK_B;
  836. report->rds[V4L2_RDS_BLOCK_B].msb = resp[10];
  837. report->rds[V4L2_RDS_BLOCK_B].lsb = resp[11];
  838. report->rds[V4L2_RDS_BLOCK_C].block = V4L2_RDS_BLOCK_C;
  839. report->rds[V4L2_RDS_BLOCK_C].msb = resp[12];
  840. report->rds[V4L2_RDS_BLOCK_C].lsb = resp[13];
  841. report->rds[V4L2_RDS_BLOCK_D].block = V4L2_RDS_BLOCK_D;
  842. report->rds[V4L2_RDS_BLOCK_D].msb = resp[14];
  843. report->rds[V4L2_RDS_BLOCK_D].lsb = resp[15];
  844. return err;
  845. }
  846. EXPORT_SYMBOL_GPL(si476x_core_cmd_fm_rds_status);
  847. int si476x_core_cmd_fm_rds_blockcount(struct si476x_core *core,
  848. bool clear,
  849. struct si476x_rds_blockcount_report *report)
  850. {
  851. int err;
  852. u8 resp[CMD_FM_RDS_BLOCKCOUNT_NRESP];
  853. const u8 args[CMD_FM_RDS_BLOCKCOUNT_NARGS] = {
  854. clear,
  855. };
  856. if (!report)
  857. return -EINVAL;
  858. err = si476x_core_send_command(core, CMD_FM_RDS_BLOCKCOUNT,
  859. args, ARRAY_SIZE(args),
  860. resp, ARRAY_SIZE(resp),
  861. SI476X_DEFAULT_TIMEOUT);
  862. if (!err) {
  863. report->expected = get_unaligned_be16(resp + 2);
  864. report->received = get_unaligned_be16(resp + 4);
  865. report->uncorrectable = get_unaligned_be16(resp + 6);
  866. }
  867. return err;
  868. }
  869. EXPORT_SYMBOL_GPL(si476x_core_cmd_fm_rds_blockcount);
  870. int si476x_core_cmd_fm_phase_diversity(struct si476x_core *core,
  871. enum si476x_phase_diversity_mode mode)
  872. {
  873. u8 resp[CMD_FM_PHASE_DIVERSITY_NRESP];
  874. const u8 args[CMD_FM_PHASE_DIVERSITY_NARGS] = {
  875. mode & 0x07,
  876. };
  877. return si476x_core_send_command(core, CMD_FM_PHASE_DIVERSITY,
  878. args, ARRAY_SIZE(args),
  879. resp, ARRAY_SIZE(resp),
  880. SI476X_DEFAULT_TIMEOUT);
  881. }
  882. EXPORT_SYMBOL_GPL(si476x_core_cmd_fm_phase_diversity);
  883. /**
  884. * si476x_core_cmd_fm_phase_div_status() - get the phase diversity
  885. * status
  886. *
  887. * @core: si476x device
  888. *
  889. * NOTE caller must hold core lock
  890. *
  891. * Function returns the value of the status bit in case of success and
  892. * negative error code in case of failre.
  893. */
  894. int si476x_core_cmd_fm_phase_div_status(struct si476x_core *core)
  895. {
  896. int err;
  897. u8 resp[CMD_FM_PHASE_DIV_STATUS_NRESP];
  898. err = si476x_core_send_command(core, CMD_FM_PHASE_DIV_STATUS,
  899. NULL, 0,
  900. resp, ARRAY_SIZE(resp),
  901. SI476X_DEFAULT_TIMEOUT);
  902. return (err < 0) ? err : resp[1];
  903. }
  904. EXPORT_SYMBOL_GPL(si476x_core_cmd_fm_phase_div_status);
  905. /**
  906. * si476x_cmd_am_seek_start - send 'FM_SEEK_START' command to the
  907. * device
  908. * @core: - device to send the command to
  909. * @seekup: - if set the direction of the search is 'up'
  910. * @wrap: - if set seek wraps when hitting band limit
  911. *
  912. * This function begins search for a valid station. The station is
  913. * considered valid when 'FM_VALID_SNR_THRESHOLD' and
  914. * 'FM_VALID_RSSI_THRESHOLD' and 'FM_VALID_MAX_TUNE_ERROR' criteria
  915. * are met.
  916. *
  917. * Function returns 0 on success and negative error code on failure
  918. */
  919. int si476x_core_cmd_am_seek_start(struct si476x_core *core,
  920. bool seekup, bool wrap)
  921. {
  922. u8 resp[CMD_AM_SEEK_START_NRESP];
  923. const u8 args[CMD_AM_SEEK_START_NARGS] = {
  924. seekup << 3 | wrap << 2,
  925. };
  926. return si476x_cmd_tune_seek_freq(core, CMD_AM_SEEK_START,
  927. args, sizeof(args),
  928. resp, sizeof(resp));
  929. }
  930. EXPORT_SYMBOL_GPL(si476x_core_cmd_am_seek_start);
  931. static int si476x_core_cmd_power_up_a10(struct si476x_core *core,
  932. struct si476x_power_up_args *puargs)
  933. {
  934. u8 resp[CMD_POWER_UP_A10_NRESP];
  935. const bool intsel = (core->pinmux.a1 == SI476X_A1_IRQ);
  936. const bool ctsen = (core->client->irq != 0);
  937. const u8 args[CMD_POWER_UP_A10_NARGS] = {
  938. 0xF7, /* Reserved, always 0xF7 */
  939. 0x3F & puargs->xcload, /* First two bits are reserved to be
  940. * zeros */
  941. ctsen << 7 | intsel << 6 | 0x07, /* Last five bits
  942. * are reserved to
  943. * be written as 0x7 */
  944. puargs->func << 4 | puargs->freq,
  945. 0x11, /* Reserved, always 0x11 */
  946. };
  947. return si476x_core_send_command(core, CMD_POWER_UP,
  948. args, ARRAY_SIZE(args),
  949. resp, ARRAY_SIZE(resp),
  950. SI476X_TIMEOUT_POWER_UP);
  951. }
  952. static int si476x_core_cmd_power_up_a20(struct si476x_core *core,
  953. struct si476x_power_up_args *puargs)
  954. {
  955. u8 resp[CMD_POWER_UP_A20_NRESP];
  956. const bool intsel = (core->pinmux.a1 == SI476X_A1_IRQ);
  957. const bool ctsen = (core->client->irq != 0);
  958. const u8 args[CMD_POWER_UP_A20_NARGS] = {
  959. puargs->ibias6x << 7 | puargs->xstart,
  960. 0x3F & puargs->xcload, /* First two bits are reserved to be
  961. * zeros */
  962. ctsen << 7 | intsel << 6 | puargs->fastboot << 5 |
  963. puargs->xbiashc << 3 | puargs->xbias,
  964. puargs->func << 4 | puargs->freq,
  965. 0x10 | puargs->xmode,
  966. };
  967. return si476x_core_send_command(core, CMD_POWER_UP,
  968. args, ARRAY_SIZE(args),
  969. resp, ARRAY_SIZE(resp),
  970. SI476X_TIMEOUT_POWER_UP);
  971. }
  972. static int si476x_core_cmd_power_down_a10(struct si476x_core *core,
  973. struct si476x_power_down_args *pdargs)
  974. {
  975. u8 resp[CMD_POWER_DOWN_A10_NRESP];
  976. return si476x_core_send_command(core, CMD_POWER_DOWN,
  977. NULL, 0,
  978. resp, ARRAY_SIZE(resp),
  979. SI476X_DEFAULT_TIMEOUT);
  980. }
  981. static int si476x_core_cmd_power_down_a20(struct si476x_core *core,
  982. struct si476x_power_down_args *pdargs)
  983. {
  984. u8 resp[CMD_POWER_DOWN_A20_NRESP];
  985. const u8 args[CMD_POWER_DOWN_A20_NARGS] = {
  986. pdargs->xosc,
  987. };
  988. return si476x_core_send_command(core, CMD_POWER_DOWN,
  989. args, ARRAY_SIZE(args),
  990. resp, ARRAY_SIZE(resp),
  991. SI476X_DEFAULT_TIMEOUT);
  992. }
  993. static int si476x_core_cmd_am_tune_freq_a10(struct si476x_core *core,
  994. struct si476x_tune_freq_args *tuneargs)
  995. {
  996. const int am_freq = tuneargs->freq;
  997. u8 resp[CMD_AM_TUNE_FREQ_NRESP];
  998. const u8 args[CMD_AM_TUNE_FREQ_NARGS] = {
  999. (tuneargs->hd << 6),
  1000. msb(am_freq),
  1001. lsb(am_freq),
  1002. };
  1003. return si476x_cmd_tune_seek_freq(core, CMD_AM_TUNE_FREQ, args,
  1004. sizeof(args),
  1005. resp, sizeof(resp));
  1006. }
  1007. static int si476x_core_cmd_am_tune_freq_a20(struct si476x_core *core,
  1008. struct si476x_tune_freq_args *tuneargs)
  1009. {
  1010. const int am_freq = tuneargs->freq;
  1011. u8 resp[CMD_AM_TUNE_FREQ_NRESP];
  1012. const u8 args[CMD_AM_TUNE_FREQ_NARGS] = {
  1013. (tuneargs->zifsr << 6) | (tuneargs->injside & 0x03),
  1014. msb(am_freq),
  1015. lsb(am_freq),
  1016. };
  1017. return si476x_cmd_tune_seek_freq(core, CMD_AM_TUNE_FREQ,
  1018. args, sizeof(args),
  1019. resp, sizeof(resp));
  1020. }
  1021. static int si476x_core_cmd_fm_rsq_status_a10(struct si476x_core *core,
  1022. struct si476x_rsq_status_args *rsqargs,
  1023. struct si476x_rsq_status_report *report)
  1024. {
  1025. int err;
  1026. u8 resp[CMD_FM_RSQ_STATUS_A10_NRESP];
  1027. const u8 args[CMD_FM_RSQ_STATUS_A10_NARGS] = {
  1028. rsqargs->rsqack << 3 | rsqargs->attune << 2 |
  1029. rsqargs->cancel << 1 | rsqargs->stcack,
  1030. };
  1031. err = si476x_core_send_command(core, CMD_FM_RSQ_STATUS,
  1032. args, ARRAY_SIZE(args),
  1033. resp, ARRAY_SIZE(resp),
  1034. SI476X_DEFAULT_TIMEOUT);
  1035. /*
  1036. * Besides getting received signal quality information this
  1037. * command can be used to just acknowledge different interrupt
  1038. * flags in those cases it is useless to copy and parse
  1039. * received data so user can pass NULL, and thus avoid
  1040. * unnecessary copying.
  1041. */
  1042. if (err < 0 || report == NULL)
  1043. return err;
  1044. report->multhint = 0x80 & resp[1];
  1045. report->multlint = 0x40 & resp[1];
  1046. report->snrhint = 0x08 & resp[1];
  1047. report->snrlint = 0x04 & resp[1];
  1048. report->rssihint = 0x02 & resp[1];
  1049. report->rssilint = 0x01 & resp[1];
  1050. report->bltf = 0x80 & resp[2];
  1051. report->snr_ready = 0x20 & resp[2];
  1052. report->rssiready = 0x08 & resp[2];
  1053. report->afcrl = 0x02 & resp[2];
  1054. report->valid = 0x01 & resp[2];
  1055. report->readfreq = get_unaligned_be16(resp + 3);
  1056. report->freqoff = resp[5];
  1057. report->rssi = resp[6];
  1058. report->snr = resp[7];
  1059. report->lassi = resp[9];
  1060. report->hassi = resp[10];
  1061. report->mult = resp[11];
  1062. report->dev = resp[12];
  1063. report->readantcap = get_unaligned_be16(resp + 13);
  1064. report->assi = resp[15];
  1065. report->usn = resp[16];
  1066. return err;
  1067. }
  1068. static int si476x_core_cmd_fm_rsq_status_a20(struct si476x_core *core,
  1069. struct si476x_rsq_status_args *rsqargs,
  1070. struct si476x_rsq_status_report *report)
  1071. {
  1072. int err;
  1073. u8 resp[CMD_FM_RSQ_STATUS_A10_NRESP];
  1074. const u8 args[CMD_FM_RSQ_STATUS_A30_NARGS] = {
  1075. rsqargs->primary << 4 | rsqargs->rsqack << 3 |
  1076. rsqargs->attune << 2 | rsqargs->cancel << 1 |
  1077. rsqargs->stcack,
  1078. };
  1079. err = si476x_core_send_command(core, CMD_FM_RSQ_STATUS,
  1080. args, ARRAY_SIZE(args),
  1081. resp, ARRAY_SIZE(resp),
  1082. SI476X_DEFAULT_TIMEOUT);
  1083. /*
  1084. * Besides getting received signal quality information this
  1085. * command can be used to just acknowledge different interrupt
  1086. * flags in those cases it is useless to copy and parse
  1087. * received data so user can pass NULL, and thus avoid
  1088. * unnecessary copying.
  1089. */
  1090. if (err < 0 || report == NULL)
  1091. return err;
  1092. report->multhint = 0x80 & resp[1];
  1093. report->multlint = 0x40 & resp[1];
  1094. report->snrhint = 0x08 & resp[1];
  1095. report->snrlint = 0x04 & resp[1];
  1096. report->rssihint = 0x02 & resp[1];
  1097. report->rssilint = 0x01 & resp[1];
  1098. report->bltf = 0x80 & resp[2];
  1099. report->snr_ready = 0x20 & resp[2];
  1100. report->rssiready = 0x08 & resp[2];
  1101. report->afcrl = 0x02 & resp[2];
  1102. report->valid = 0x01 & resp[2];
  1103. report->readfreq = get_unaligned_be16(resp + 3);
  1104. report->freqoff = resp[5];
  1105. report->rssi = resp[6];
  1106. report->snr = resp[7];
  1107. report->lassi = resp[9];
  1108. report->hassi = resp[10];
  1109. report->mult = resp[11];
  1110. report->dev = resp[12];
  1111. report->readantcap = get_unaligned_be16(resp + 13);
  1112. report->assi = resp[15];
  1113. report->usn = resp[16];
  1114. return err;
  1115. }
  1116. static int si476x_core_cmd_fm_rsq_status_a30(struct si476x_core *core,
  1117. struct si476x_rsq_status_args *rsqargs,
  1118. struct si476x_rsq_status_report *report)
  1119. {
  1120. int err;
  1121. u8 resp[CMD_FM_RSQ_STATUS_A30_NRESP];
  1122. const u8 args[CMD_FM_RSQ_STATUS_A30_NARGS] = {
  1123. rsqargs->primary << 4 | rsqargs->rsqack << 3 |
  1124. rsqargs->attune << 2 | rsqargs->cancel << 1 |
  1125. rsqargs->stcack,
  1126. };
  1127. err = si476x_core_send_command(core, CMD_FM_RSQ_STATUS,
  1128. args, ARRAY_SIZE(args),
  1129. resp, ARRAY_SIZE(resp),
  1130. SI476X_DEFAULT_TIMEOUT);
  1131. /*
  1132. * Besides getting received signal quality information this
  1133. * command can be used to just acknowledge different interrupt
  1134. * flags in those cases it is useless to copy and parse
  1135. * received data so user can pass NULL, and thus avoid
  1136. * unnecessary copying.
  1137. */
  1138. if (err < 0 || report == NULL)
  1139. return err;
  1140. report->multhint = 0x80 & resp[1];
  1141. report->multlint = 0x40 & resp[1];
  1142. report->snrhint = 0x08 & resp[1];
  1143. report->snrlint = 0x04 & resp[1];
  1144. report->rssihint = 0x02 & resp[1];
  1145. report->rssilint = 0x01 & resp[1];
  1146. report->bltf = 0x80 & resp[2];
  1147. report->snr_ready = 0x20 & resp[2];
  1148. report->rssiready = 0x08 & resp[2];
  1149. report->injside = 0x04 & resp[2];
  1150. report->afcrl = 0x02 & resp[2];
  1151. report->valid = 0x01 & resp[2];
  1152. report->readfreq = get_unaligned_be16(resp + 3);
  1153. report->freqoff = resp[5];
  1154. report->rssi = resp[6];
  1155. report->snr = resp[7];
  1156. report->issi = resp[8];
  1157. report->lassi = resp[9];
  1158. report->hassi = resp[10];
  1159. report->mult = resp[11];
  1160. report->dev = resp[12];
  1161. report->readantcap = get_unaligned_be16(resp + 13);
  1162. report->assi = resp[15];
  1163. report->usn = resp[16];
  1164. report->pilotdev = resp[17];
  1165. report->rdsdev = resp[18];
  1166. report->assidev = resp[19];
  1167. report->strongdev = resp[20];
  1168. report->rdspi = get_unaligned_be16(resp + 21);
  1169. return err;
  1170. }
  1171. static int si476x_core_cmd_fm_tune_freq_a10(struct si476x_core *core,
  1172. struct si476x_tune_freq_args *tuneargs)
  1173. {
  1174. u8 resp[CMD_FM_TUNE_FREQ_NRESP];
  1175. const u8 args[CMD_FM_TUNE_FREQ_A10_NARGS] = {
  1176. (tuneargs->hd << 6) | (tuneargs->tunemode << 4)
  1177. | (tuneargs->smoothmetrics << 2),
  1178. msb(tuneargs->freq),
  1179. lsb(tuneargs->freq),
  1180. msb(tuneargs->antcap),
  1181. lsb(tuneargs->antcap)
  1182. };
  1183. return si476x_cmd_tune_seek_freq(core, CMD_FM_TUNE_FREQ,
  1184. args, sizeof(args),
  1185. resp, sizeof(resp));
  1186. }
  1187. static int si476x_core_cmd_fm_tune_freq_a20(struct si476x_core *core,
  1188. struct si476x_tune_freq_args *tuneargs)
  1189. {
  1190. u8 resp[CMD_FM_TUNE_FREQ_NRESP];
  1191. const u8 args[CMD_FM_TUNE_FREQ_A20_NARGS] = {
  1192. (tuneargs->hd << 6) | (tuneargs->tunemode << 4)
  1193. | (tuneargs->smoothmetrics << 2) | (tuneargs->injside),
  1194. msb(tuneargs->freq),
  1195. lsb(tuneargs->freq),
  1196. };
  1197. return si476x_cmd_tune_seek_freq(core, CMD_FM_TUNE_FREQ,
  1198. args, sizeof(args),
  1199. resp, sizeof(resp));
  1200. }
  1201. static int si476x_core_cmd_agc_status_a20(struct si476x_core *core,
  1202. struct si476x_agc_status_report *report)
  1203. {
  1204. int err;
  1205. u8 resp[CMD_AGC_STATUS_NRESP_A20];
  1206. if (!report)
  1207. return -EINVAL;
  1208. err = si476x_core_send_command(core, CMD_AGC_STATUS,
  1209. NULL, 0,
  1210. resp, ARRAY_SIZE(resp),
  1211. SI476X_DEFAULT_TIMEOUT);
  1212. if (err < 0)
  1213. return err;
  1214. report->mxhi = resp[1] & SI476X_AGC_MXHI;
  1215. report->mxlo = resp[1] & SI476X_AGC_MXLO;
  1216. report->lnahi = resp[1] & SI476X_AGC_LNAHI;
  1217. report->lnalo = resp[1] & SI476X_AGC_LNALO;
  1218. report->fmagc1 = resp[2];
  1219. report->fmagc2 = resp[3];
  1220. report->pgagain = resp[4];
  1221. report->fmwblang = resp[5];
  1222. return err;
  1223. }
  1224. static int si476x_core_cmd_agc_status_a10(struct si476x_core *core,
  1225. struct si476x_agc_status_report *report)
  1226. {
  1227. int err;
  1228. u8 resp[CMD_AGC_STATUS_NRESP_A10];
  1229. if (!report)
  1230. return -EINVAL;
  1231. err = si476x_core_send_command(core, CMD_AGC_STATUS,
  1232. NULL, 0,
  1233. resp, ARRAY_SIZE(resp),
  1234. SI476X_DEFAULT_TIMEOUT);
  1235. if (err < 0)
  1236. return err;
  1237. report->mxhi = resp[1] & SI476X_AGC_MXHI;
  1238. report->mxlo = resp[1] & SI476X_AGC_MXLO;
  1239. report->lnahi = resp[1] & SI476X_AGC_LNAHI;
  1240. report->lnalo = resp[1] & SI476X_AGC_LNALO;
  1241. return err;
  1242. }
  1243. typedef int (*tune_freq_func_t) (struct si476x_core *core,
  1244. struct si476x_tune_freq_args *tuneargs);
  1245. static struct {
  1246. int (*power_up)(struct si476x_core *,
  1247. struct si476x_power_up_args *);
  1248. int (*power_down)(struct si476x_core *,
  1249. struct si476x_power_down_args *);
  1250. tune_freq_func_t fm_tune_freq;
  1251. tune_freq_func_t am_tune_freq;
  1252. int (*fm_rsq_status)(struct si476x_core *,
  1253. struct si476x_rsq_status_args *,
  1254. struct si476x_rsq_status_report *);
  1255. int (*agc_status)(struct si476x_core *,
  1256. struct si476x_agc_status_report *);
  1257. int (*intb_pin_cfg)(struct si476x_core *core,
  1258. enum si476x_intb_config intb,
  1259. enum si476x_a1_config a1);
  1260. } si476x_cmds_vtable[] = {
  1261. [SI476X_REVISION_A10] = {
  1262. .power_up = si476x_core_cmd_power_up_a10,
  1263. .power_down = si476x_core_cmd_power_down_a10,
  1264. .fm_tune_freq = si476x_core_cmd_fm_tune_freq_a10,
  1265. .am_tune_freq = si476x_core_cmd_am_tune_freq_a10,
  1266. .fm_rsq_status = si476x_core_cmd_fm_rsq_status_a10,
  1267. .agc_status = si476x_core_cmd_agc_status_a10,
  1268. .intb_pin_cfg = si476x_core_cmd_intb_pin_cfg_a10,
  1269. },
  1270. [SI476X_REVISION_A20] = {
  1271. .power_up = si476x_core_cmd_power_up_a20,
  1272. .power_down = si476x_core_cmd_power_down_a20,
  1273. .fm_tune_freq = si476x_core_cmd_fm_tune_freq_a20,
  1274. .am_tune_freq = si476x_core_cmd_am_tune_freq_a20,
  1275. .fm_rsq_status = si476x_core_cmd_fm_rsq_status_a20,
  1276. .agc_status = si476x_core_cmd_agc_status_a20,
  1277. .intb_pin_cfg = si476x_core_cmd_intb_pin_cfg_a20,
  1278. },
  1279. [SI476X_REVISION_A30] = {
  1280. .power_up = si476x_core_cmd_power_up_a20,
  1281. .power_down = si476x_core_cmd_power_down_a20,
  1282. .fm_tune_freq = si476x_core_cmd_fm_tune_freq_a20,
  1283. .am_tune_freq = si476x_core_cmd_am_tune_freq_a20,
  1284. .fm_rsq_status = si476x_core_cmd_fm_rsq_status_a30,
  1285. .agc_status = si476x_core_cmd_agc_status_a20,
  1286. .intb_pin_cfg = si476x_core_cmd_intb_pin_cfg_a20,
  1287. },
  1288. };
  1289. int si476x_core_cmd_power_up(struct si476x_core *core,
  1290. struct si476x_power_up_args *args)
  1291. {
  1292. BUG_ON(core->revision > SI476X_REVISION_A30 ||
  1293. core->revision == -1);
  1294. return si476x_cmds_vtable[core->revision].power_up(core, args);
  1295. }
  1296. EXPORT_SYMBOL_GPL(si476x_core_cmd_power_up);
  1297. int si476x_core_cmd_power_down(struct si476x_core *core,
  1298. struct si476x_power_down_args *args)
  1299. {
  1300. BUG_ON(core->revision > SI476X_REVISION_A30 ||
  1301. core->revision == -1);
  1302. return si476x_cmds_vtable[core->revision].power_down(core, args);
  1303. }
  1304. EXPORT_SYMBOL_GPL(si476x_core_cmd_power_down);
  1305. int si476x_core_cmd_fm_tune_freq(struct si476x_core *core,
  1306. struct si476x_tune_freq_args *args)
  1307. {
  1308. BUG_ON(core->revision > SI476X_REVISION_A30 ||
  1309. core->revision == -1);
  1310. return si476x_cmds_vtable[core->revision].fm_tune_freq(core, args);
  1311. }
  1312. EXPORT_SYMBOL_GPL(si476x_core_cmd_fm_tune_freq);
  1313. int si476x_core_cmd_am_tune_freq(struct si476x_core *core,
  1314. struct si476x_tune_freq_args *args)
  1315. {
  1316. BUG_ON(core->revision > SI476X_REVISION_A30 ||
  1317. core->revision == -1);
  1318. return si476x_cmds_vtable[core->revision].am_tune_freq(core, args);
  1319. }
  1320. EXPORT_SYMBOL_GPL(si476x_core_cmd_am_tune_freq);
  1321. int si476x_core_cmd_fm_rsq_status(struct si476x_core *core,
  1322. struct si476x_rsq_status_args *args,
  1323. struct si476x_rsq_status_report *report)
  1324. {
  1325. BUG_ON(core->revision > SI476X_REVISION_A30 ||
  1326. core->revision == -1);
  1327. return si476x_cmds_vtable[core->revision].fm_rsq_status(core, args,
  1328. report);
  1329. }
  1330. EXPORT_SYMBOL_GPL(si476x_core_cmd_fm_rsq_status);
  1331. int si476x_core_cmd_agc_status(struct si476x_core *core,
  1332. struct si476x_agc_status_report *report)
  1333. {
  1334. BUG_ON(core->revision > SI476X_REVISION_A30 ||
  1335. core->revision == -1);
  1336. return si476x_cmds_vtable[core->revision].agc_status(core, report);
  1337. }
  1338. EXPORT_SYMBOL_GPL(si476x_core_cmd_agc_status);
  1339. int si476x_core_cmd_intb_pin_cfg(struct si476x_core *core,
  1340. enum si476x_intb_config intb,
  1341. enum si476x_a1_config a1)
  1342. {
  1343. BUG_ON(core->revision > SI476X_REVISION_A30 ||
  1344. core->revision == -1);
  1345. return si476x_cmds_vtable[core->revision].intb_pin_cfg(core, intb, a1);
  1346. }
  1347. EXPORT_SYMBOL_GPL(si476x_core_cmd_intb_pin_cfg);
  1348. MODULE_LICENSE("GPL");
  1349. MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
  1350. MODULE_DESCRIPTION("API for command exchange for si476x");