radio-si476x.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. /*
  2. * drivers/media/radio/radio-si476x.c -- V4L2 driver for SI476X chips
  3. *
  4. * Copyright (C) 2012 Innovative Converged Devices(ICD)
  5. * Copyright (C) 2013 Andrey Smirnov
  6. *
  7. * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/delay.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/slab.h>
  23. #include <linux/atomic.h>
  24. #include <linux/videodev2.h>
  25. #include <linux/mutex.h>
  26. #include <linux/debugfs.h>
  27. #include <media/v4l2-common.h>
  28. #include <media/v4l2-ioctl.h>
  29. #include <media/v4l2-ctrls.h>
  30. #include <media/v4l2-event.h>
  31. #include <media/v4l2-device.h>
  32. #include <media/si476x.h>
  33. #include <linux/mfd/si476x-core.h>
  34. #define FM_FREQ_RANGE_LOW 64000000
  35. #define FM_FREQ_RANGE_HIGH 108000000
  36. #define AM_FREQ_RANGE_LOW 520000
  37. #define AM_FREQ_RANGE_HIGH 30000000
  38. #define PWRLINEFLTR (1 << 8)
  39. #define FREQ_MUL (10000000 / 625)
  40. #define SI476X_PHDIV_STATUS_LINK_LOCKED(status) (0x80 & (status))
  41. #define DRIVER_NAME "si476x-radio"
  42. #define DRIVER_CARD "SI476x AM/FM Receiver"
  43. enum si476x_freq_bands {
  44. SI476X_BAND_FM,
  45. SI476X_BAND_AM,
  46. };
  47. static const struct v4l2_frequency_band si476x_bands[] = {
  48. [SI476X_BAND_FM] = {
  49. .type = V4L2_TUNER_RADIO,
  50. .index = SI476X_BAND_FM,
  51. .capability = V4L2_TUNER_CAP_LOW
  52. | V4L2_TUNER_CAP_STEREO
  53. | V4L2_TUNER_CAP_RDS
  54. | V4L2_TUNER_CAP_RDS_BLOCK_IO
  55. | V4L2_TUNER_CAP_FREQ_BANDS,
  56. .rangelow = 64 * FREQ_MUL,
  57. .rangehigh = 108 * FREQ_MUL,
  58. .modulation = V4L2_BAND_MODULATION_FM,
  59. },
  60. [SI476X_BAND_AM] = {
  61. .type = V4L2_TUNER_RADIO,
  62. .index = SI476X_BAND_AM,
  63. .capability = V4L2_TUNER_CAP_LOW
  64. | V4L2_TUNER_CAP_FREQ_BANDS,
  65. .rangelow = 0.52 * FREQ_MUL,
  66. .rangehigh = 30 * FREQ_MUL,
  67. .modulation = V4L2_BAND_MODULATION_AM,
  68. },
  69. };
  70. static inline bool si476x_radio_freq_is_inside_of_the_band(u32 freq, int band)
  71. {
  72. return freq >= si476x_bands[band].rangelow &&
  73. freq <= si476x_bands[band].rangehigh;
  74. }
  75. static inline bool si476x_radio_range_is_inside_of_the_band(u32 low, u32 high,
  76. int band)
  77. {
  78. return low >= si476x_bands[band].rangelow &&
  79. high <= si476x_bands[band].rangehigh;
  80. }
  81. static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl);
  82. static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
  83. enum phase_diversity_modes_idx {
  84. SI476X_IDX_PHDIV_DISABLED,
  85. SI476X_IDX_PHDIV_PRIMARY_COMBINING,
  86. SI476X_IDX_PHDIV_PRIMARY_ANTENNA,
  87. SI476X_IDX_PHDIV_SECONDARY_ANTENNA,
  88. SI476X_IDX_PHDIV_SECONDARY_COMBINING,
  89. };
  90. static const char * const phase_diversity_modes[] = {
  91. [SI476X_IDX_PHDIV_DISABLED] = "Disabled",
  92. [SI476X_IDX_PHDIV_PRIMARY_COMBINING] = "Primary with Secondary",
  93. [SI476X_IDX_PHDIV_PRIMARY_ANTENNA] = "Primary Antenna",
  94. [SI476X_IDX_PHDIV_SECONDARY_ANTENNA] = "Secondary Antenna",
  95. [SI476X_IDX_PHDIV_SECONDARY_COMBINING] = "Secondary with Primary",
  96. };
  97. static inline enum phase_diversity_modes_idx
  98. si476x_phase_diversity_mode_to_idx(enum si476x_phase_diversity_mode mode)
  99. {
  100. switch (mode) {
  101. default: /* FALLTHROUGH */
  102. case SI476X_PHDIV_DISABLED:
  103. return SI476X_IDX_PHDIV_DISABLED;
  104. case SI476X_PHDIV_PRIMARY_COMBINING:
  105. return SI476X_IDX_PHDIV_PRIMARY_COMBINING;
  106. case SI476X_PHDIV_PRIMARY_ANTENNA:
  107. return SI476X_IDX_PHDIV_PRIMARY_ANTENNA;
  108. case SI476X_PHDIV_SECONDARY_ANTENNA:
  109. return SI476X_IDX_PHDIV_SECONDARY_ANTENNA;
  110. case SI476X_PHDIV_SECONDARY_COMBINING:
  111. return SI476X_IDX_PHDIV_SECONDARY_COMBINING;
  112. }
  113. }
  114. static inline enum si476x_phase_diversity_mode
  115. si476x_phase_diversity_idx_to_mode(enum phase_diversity_modes_idx idx)
  116. {
  117. static const int idx_to_value[] = {
  118. [SI476X_IDX_PHDIV_DISABLED] = SI476X_PHDIV_DISABLED,
  119. [SI476X_IDX_PHDIV_PRIMARY_COMBINING] = SI476X_PHDIV_PRIMARY_COMBINING,
  120. [SI476X_IDX_PHDIV_PRIMARY_ANTENNA] = SI476X_PHDIV_PRIMARY_ANTENNA,
  121. [SI476X_IDX_PHDIV_SECONDARY_ANTENNA] = SI476X_PHDIV_SECONDARY_ANTENNA,
  122. [SI476X_IDX_PHDIV_SECONDARY_COMBINING] = SI476X_PHDIV_SECONDARY_COMBINING,
  123. };
  124. return idx_to_value[idx];
  125. }
  126. static const struct v4l2_ctrl_ops si476x_ctrl_ops = {
  127. .g_volatile_ctrl = si476x_radio_g_volatile_ctrl,
  128. .s_ctrl = si476x_radio_s_ctrl,
  129. };
  130. enum si476x_ctrl_idx {
  131. SI476X_IDX_RSSI_THRESHOLD,
  132. SI476X_IDX_SNR_THRESHOLD,
  133. SI476X_IDX_MAX_TUNE_ERROR,
  134. SI476X_IDX_HARMONICS_COUNT,
  135. SI476X_IDX_DIVERSITY_MODE,
  136. SI476X_IDX_INTERCHIP_LINK,
  137. };
  138. static struct v4l2_ctrl_config si476x_ctrls[] = {
  139. /**
  140. * SI476X during its station seeking(or tuning) process uses several
  141. * parameters to detrmine if "the station" is valid:
  142. *
  143. * - Signal's SNR(in dBuV) must be lower than
  144. * #V4L2_CID_SI476X_SNR_THRESHOLD
  145. * - Signal's RSSI(in dBuV) must be greater than
  146. * #V4L2_CID_SI476X_RSSI_THRESHOLD
  147. * - Signal's frequency deviation(in units of 2ppm) must not be
  148. * more than #V4L2_CID_SI476X_MAX_TUNE_ERROR
  149. */
  150. [SI476X_IDX_RSSI_THRESHOLD] = {
  151. .ops = &si476x_ctrl_ops,
  152. .id = V4L2_CID_SI476X_RSSI_THRESHOLD,
  153. .name = "Valid RSSI Threshold",
  154. .type = V4L2_CTRL_TYPE_INTEGER,
  155. .min = -128,
  156. .max = 127,
  157. .step = 1,
  158. },
  159. [SI476X_IDX_SNR_THRESHOLD] = {
  160. .ops = &si476x_ctrl_ops,
  161. .id = V4L2_CID_SI476X_SNR_THRESHOLD,
  162. .type = V4L2_CTRL_TYPE_INTEGER,
  163. .name = "Valid SNR Threshold",
  164. .min = -128,
  165. .max = 127,
  166. .step = 1,
  167. },
  168. [SI476X_IDX_MAX_TUNE_ERROR] = {
  169. .ops = &si476x_ctrl_ops,
  170. .id = V4L2_CID_SI476X_MAX_TUNE_ERROR,
  171. .type = V4L2_CTRL_TYPE_INTEGER,
  172. .name = "Max Tune Errors",
  173. .min = 0,
  174. .max = 126 * 2,
  175. .step = 2,
  176. },
  177. /**
  178. * #V4L2_CID_SI476X_HARMONICS_COUNT -- number of harmonics
  179. * built-in power-line noise supression filter is to reject
  180. * during AM-mode operation.
  181. */
  182. [SI476X_IDX_HARMONICS_COUNT] = {
  183. .ops = &si476x_ctrl_ops,
  184. .id = V4L2_CID_SI476X_HARMONICS_COUNT,
  185. .type = V4L2_CTRL_TYPE_INTEGER,
  186. .name = "Count of Harmonics to Reject",
  187. .min = 0,
  188. .max = 20,
  189. .step = 1,
  190. },
  191. /**
  192. * #V4L2_CID_SI476X_DIVERSITY_MODE -- configuration which
  193. * two tuners working in diversity mode are to work in.
  194. *
  195. * - #SI476X_IDX_PHDIV_DISABLED diversity mode disabled
  196. * - #SI476X_IDX_PHDIV_PRIMARY_COMBINING diversity mode is
  197. * on, primary tuner's antenna is the main one.
  198. * - #SI476X_IDX_PHDIV_PRIMARY_ANTENNA diversity mode is
  199. * off, primary tuner's antenna is the main one.
  200. * - #SI476X_IDX_PHDIV_SECONDARY_ANTENNA diversity mode is
  201. * off, secondary tuner's antenna is the main one.
  202. * - #SI476X_IDX_PHDIV_SECONDARY_COMBINING diversity mode is
  203. * on, secondary tuner's antenna is the main one.
  204. */
  205. [SI476X_IDX_DIVERSITY_MODE] = {
  206. .ops = &si476x_ctrl_ops,
  207. .id = V4L2_CID_SI476X_DIVERSITY_MODE,
  208. .type = V4L2_CTRL_TYPE_MENU,
  209. .name = "Phase Diversity Mode",
  210. .qmenu = phase_diversity_modes,
  211. .min = 0,
  212. .max = ARRAY_SIZE(phase_diversity_modes) - 1,
  213. },
  214. /**
  215. * #V4L2_CID_SI476X_INTERCHIP_LINK -- inter-chip link in
  216. * diversity mode indicator. Allows user to determine if two
  217. * chips working in diversity mode have established a link
  218. * between each other and if the system as a whole uses
  219. * signals from both antennas to receive FM radio.
  220. */
  221. [SI476X_IDX_INTERCHIP_LINK] = {
  222. .ops = &si476x_ctrl_ops,
  223. .id = V4L2_CID_SI476X_INTERCHIP_LINK,
  224. .type = V4L2_CTRL_TYPE_BOOLEAN,
  225. .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
  226. .name = "Inter-Chip Link",
  227. .min = 0,
  228. .max = 1,
  229. .step = 1,
  230. },
  231. };
  232. struct si476x_radio;
  233. /**
  234. * struct si476x_radio_ops - vtable of tuner functions
  235. *
  236. * This table holds pointers to functions implementing particular
  237. * operations depending on the mode in which the tuner chip was
  238. * configured to start in. If the function is not supported
  239. * corresponding element is set to #NULL.
  240. *
  241. * @tune_freq: Tune chip to a specific frequency
  242. * @seek_start: Star station seeking
  243. * @rsq_status: Get Received Signal Quality(RSQ) status
  244. * @rds_blckcnt: Get received RDS blocks count
  245. * @phase_diversity: Change phase diversity mode of the tuner
  246. * @phase_div_status: Get phase diversity mode status
  247. * @acf_status: Get the status of Automatically Controlled
  248. * Features(ACF)
  249. * @agc_status: Get Automatic Gain Control(AGC) status
  250. */
  251. struct si476x_radio_ops {
  252. int (*tune_freq)(struct si476x_core *, struct si476x_tune_freq_args *);
  253. int (*seek_start)(struct si476x_core *, bool, bool);
  254. int (*rsq_status)(struct si476x_core *, struct si476x_rsq_status_args *,
  255. struct si476x_rsq_status_report *);
  256. int (*rds_blckcnt)(struct si476x_core *, bool,
  257. struct si476x_rds_blockcount_report *);
  258. int (*phase_diversity)(struct si476x_core *,
  259. enum si476x_phase_diversity_mode);
  260. int (*phase_div_status)(struct si476x_core *);
  261. int (*acf_status)(struct si476x_core *,
  262. struct si476x_acf_status_report *);
  263. int (*agc_status)(struct si476x_core *,
  264. struct si476x_agc_status_report *);
  265. };
  266. /**
  267. * struct si476x_radio - radio device
  268. *
  269. * @core: Pointer to underlying core device
  270. * @videodev: Pointer to video device created by V4L2 subsystem
  271. * @ops: Vtable of functions. See struct si476x_radio_ops for details
  272. * @kref: Reference counter
  273. * @core_lock: An r/w semaphore to brebvent the deletion of underlying
  274. * core structure is the radio device is being used
  275. */
  276. struct si476x_radio {
  277. struct v4l2_device v4l2dev;
  278. struct video_device videodev;
  279. struct v4l2_ctrl_handler ctrl_handler;
  280. struct si476x_core *core;
  281. /* This field should not be accesses unless core lock is held */
  282. const struct si476x_radio_ops *ops;
  283. struct dentry *debugfs;
  284. u32 audmode;
  285. };
  286. static inline struct si476x_radio *
  287. v4l2_dev_to_radio(struct v4l2_device *d)
  288. {
  289. return container_of(d, struct si476x_radio, v4l2dev);
  290. }
  291. static inline struct si476x_radio *
  292. v4l2_ctrl_handler_to_radio(struct v4l2_ctrl_handler *d)
  293. {
  294. return container_of(d, struct si476x_radio, ctrl_handler);
  295. }
  296. /*
  297. * si476x_vidioc_querycap - query device capabilities
  298. */
  299. static int si476x_radio_querycap(struct file *file, void *priv,
  300. struct v4l2_capability *capability)
  301. {
  302. struct si476x_radio *radio = video_drvdata(file);
  303. strlcpy(capability->driver, radio->v4l2dev.name,
  304. sizeof(capability->driver));
  305. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  306. snprintf(capability->bus_info, sizeof(capability->bus_info),
  307. "platform:%s", radio->v4l2dev.name);
  308. capability->device_caps = V4L2_CAP_TUNER
  309. | V4L2_CAP_RADIO
  310. | V4L2_CAP_HW_FREQ_SEEK;
  311. si476x_core_lock(radio->core);
  312. if (!si476x_core_is_a_secondary_tuner(radio->core))
  313. capability->device_caps |= V4L2_CAP_RDS_CAPTURE
  314. | V4L2_CAP_READWRITE;
  315. si476x_core_unlock(radio->core);
  316. capability->capabilities = capability->device_caps
  317. | V4L2_CAP_DEVICE_CAPS;
  318. return 0;
  319. }
  320. static int si476x_radio_enum_freq_bands(struct file *file, void *priv,
  321. struct v4l2_frequency_band *band)
  322. {
  323. int err;
  324. struct si476x_radio *radio = video_drvdata(file);
  325. if (band->tuner != 0)
  326. return -EINVAL;
  327. switch (radio->core->chip_id) {
  328. /* AM/FM tuners -- all bands are supported */
  329. case SI476X_CHIP_SI4761:
  330. case SI476X_CHIP_SI4764:
  331. if (band->index < ARRAY_SIZE(si476x_bands)) {
  332. *band = si476x_bands[band->index];
  333. err = 0;
  334. } else {
  335. err = -EINVAL;
  336. }
  337. break;
  338. /* FM companion tuner chips -- only FM bands are
  339. * supported */
  340. case SI476X_CHIP_SI4768:
  341. if (band->index == SI476X_BAND_FM) {
  342. *band = si476x_bands[band->index];
  343. err = 0;
  344. } else {
  345. err = -EINVAL;
  346. }
  347. break;
  348. default:
  349. err = -EINVAL;
  350. }
  351. return err;
  352. }
  353. static int si476x_radio_g_tuner(struct file *file, void *priv,
  354. struct v4l2_tuner *tuner)
  355. {
  356. int err;
  357. struct si476x_rsq_status_report report;
  358. struct si476x_radio *radio = video_drvdata(file);
  359. struct si476x_rsq_status_args args = {
  360. .primary = false,
  361. .rsqack = false,
  362. .attune = false,
  363. .cancel = false,
  364. .stcack = false,
  365. };
  366. if (tuner->index != 0)
  367. return -EINVAL;
  368. tuner->type = V4L2_TUNER_RADIO;
  369. tuner->capability = V4L2_TUNER_CAP_LOW /* Measure frequencies
  370. * in multiples of
  371. * 62.5 Hz */
  372. | V4L2_TUNER_CAP_STEREO
  373. | V4L2_TUNER_CAP_HWSEEK_BOUNDED
  374. | V4L2_TUNER_CAP_HWSEEK_WRAP
  375. | V4L2_TUNER_CAP_HWSEEK_PROG_LIM;
  376. si476x_core_lock(radio->core);
  377. if (si476x_core_is_a_secondary_tuner(radio->core)) {
  378. strlcpy(tuner->name, "FM (secondary)", sizeof(tuner->name));
  379. tuner->rxsubchans = 0;
  380. tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
  381. } else if (si476x_core_has_am(radio->core)) {
  382. if (si476x_core_is_a_primary_tuner(radio->core))
  383. strlcpy(tuner->name, "AM/FM (primary)",
  384. sizeof(tuner->name));
  385. else
  386. strlcpy(tuner->name, "AM/FM", sizeof(tuner->name));
  387. tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO
  388. | V4L2_TUNER_SUB_RDS;
  389. tuner->capability |= V4L2_TUNER_CAP_RDS
  390. | V4L2_TUNER_CAP_RDS_BLOCK_IO
  391. | V4L2_TUNER_CAP_FREQ_BANDS;
  392. tuner->rangelow = si476x_bands[SI476X_BAND_AM].rangelow;
  393. } else {
  394. strlcpy(tuner->name, "FM", sizeof(tuner->name));
  395. tuner->rxsubchans = V4L2_TUNER_SUB_RDS;
  396. tuner->capability |= V4L2_TUNER_CAP_RDS
  397. | V4L2_TUNER_CAP_RDS_BLOCK_IO
  398. | V4L2_TUNER_CAP_FREQ_BANDS;
  399. tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
  400. }
  401. tuner->audmode = radio->audmode;
  402. tuner->afc = 1;
  403. tuner->rangehigh = si476x_bands[SI476X_BAND_FM].rangehigh;
  404. err = radio->ops->rsq_status(radio->core,
  405. &args, &report);
  406. if (err < 0) {
  407. tuner->signal = 0;
  408. } else {
  409. /*
  410. * tuner->signal value range: 0x0000 .. 0xFFFF,
  411. * report.rssi: -128 .. 127
  412. */
  413. tuner->signal = (report.rssi + 128) * 257;
  414. }
  415. si476x_core_unlock(radio->core);
  416. return err;
  417. }
  418. static int si476x_radio_s_tuner(struct file *file, void *priv,
  419. const struct v4l2_tuner *tuner)
  420. {
  421. struct si476x_radio *radio = video_drvdata(file);
  422. if (tuner->index != 0)
  423. return -EINVAL;
  424. if (tuner->audmode == V4L2_TUNER_MODE_MONO ||
  425. tuner->audmode == V4L2_TUNER_MODE_STEREO)
  426. radio->audmode = tuner->audmode;
  427. else
  428. radio->audmode = V4L2_TUNER_MODE_STEREO;
  429. return 0;
  430. }
  431. static int si476x_radio_init_vtable(struct si476x_radio *radio,
  432. enum si476x_func func)
  433. {
  434. static const struct si476x_radio_ops fm_ops = {
  435. .tune_freq = si476x_core_cmd_fm_tune_freq,
  436. .seek_start = si476x_core_cmd_fm_seek_start,
  437. .rsq_status = si476x_core_cmd_fm_rsq_status,
  438. .rds_blckcnt = si476x_core_cmd_fm_rds_blockcount,
  439. .phase_diversity = si476x_core_cmd_fm_phase_diversity,
  440. .phase_div_status = si476x_core_cmd_fm_phase_div_status,
  441. .acf_status = si476x_core_cmd_fm_acf_status,
  442. .agc_status = si476x_core_cmd_agc_status,
  443. };
  444. static const struct si476x_radio_ops am_ops = {
  445. .tune_freq = si476x_core_cmd_am_tune_freq,
  446. .seek_start = si476x_core_cmd_am_seek_start,
  447. .rsq_status = si476x_core_cmd_am_rsq_status,
  448. .rds_blckcnt = NULL,
  449. .phase_diversity = NULL,
  450. .phase_div_status = NULL,
  451. .acf_status = si476x_core_cmd_am_acf_status,
  452. .agc_status = NULL,
  453. };
  454. switch (func) {
  455. case SI476X_FUNC_FM_RECEIVER:
  456. radio->ops = &fm_ops;
  457. return 0;
  458. case SI476X_FUNC_AM_RECEIVER:
  459. radio->ops = &am_ops;
  460. return 0;
  461. default:
  462. WARN(1, "Unexpected tuner function value\n");
  463. return -EINVAL;
  464. }
  465. }
  466. static int si476x_radio_pretune(struct si476x_radio *radio,
  467. enum si476x_func func)
  468. {
  469. int retval;
  470. struct si476x_tune_freq_args args = {
  471. .zifsr = false,
  472. .hd = false,
  473. .injside = SI476X_INJSIDE_AUTO,
  474. .tunemode = SI476X_TM_VALIDATED_NORMAL_TUNE,
  475. .smoothmetrics = SI476X_SM_INITIALIZE_AUDIO,
  476. .antcap = 0,
  477. };
  478. switch (func) {
  479. case SI476X_FUNC_FM_RECEIVER:
  480. args.freq = v4l2_to_si476x(radio->core,
  481. 92 * FREQ_MUL);
  482. retval = radio->ops->tune_freq(radio->core, &args);
  483. break;
  484. case SI476X_FUNC_AM_RECEIVER:
  485. args.freq = v4l2_to_si476x(radio->core,
  486. 0.6 * FREQ_MUL);
  487. retval = radio->ops->tune_freq(radio->core, &args);
  488. break;
  489. default:
  490. WARN(1, "Unexpected tuner function value\n");
  491. retval = -EINVAL;
  492. }
  493. return retval;
  494. }
  495. static int si476x_radio_do_post_powerup_init(struct si476x_radio *radio,
  496. enum si476x_func func)
  497. {
  498. int err;
  499. /* regcache_mark_dirty(radio->core->regmap); */
  500. err = regcache_sync_region(radio->core->regmap,
  501. SI476X_PROP_DIGITAL_IO_INPUT_SAMPLE_RATE,
  502. SI476X_PROP_DIGITAL_IO_OUTPUT_FORMAT);
  503. if (err < 0)
  504. return err;
  505. err = regcache_sync_region(radio->core->regmap,
  506. SI476X_PROP_AUDIO_DEEMPHASIS,
  507. SI476X_PROP_AUDIO_PWR_LINE_FILTER);
  508. if (err < 0)
  509. return err;
  510. err = regcache_sync_region(radio->core->regmap,
  511. SI476X_PROP_INT_CTL_ENABLE,
  512. SI476X_PROP_INT_CTL_ENABLE);
  513. if (err < 0)
  514. return err;
  515. /*
  516. * Is there any point in restoring SNR and the like
  517. * when switching between AM/FM?
  518. */
  519. err = regcache_sync_region(radio->core->regmap,
  520. SI476X_PROP_VALID_MAX_TUNE_ERROR,
  521. SI476X_PROP_VALID_MAX_TUNE_ERROR);
  522. if (err < 0)
  523. return err;
  524. err = regcache_sync_region(radio->core->regmap,
  525. SI476X_PROP_VALID_SNR_THRESHOLD,
  526. SI476X_PROP_VALID_RSSI_THRESHOLD);
  527. if (err < 0)
  528. return err;
  529. if (func == SI476X_FUNC_FM_RECEIVER) {
  530. if (si476x_core_has_diversity(radio->core)) {
  531. err = si476x_core_cmd_fm_phase_diversity(radio->core,
  532. radio->core->diversity_mode);
  533. if (err < 0)
  534. return err;
  535. }
  536. err = regcache_sync_region(radio->core->regmap,
  537. SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
  538. SI476X_PROP_FM_RDS_CONFIG);
  539. if (err < 0)
  540. return err;
  541. }
  542. return si476x_radio_init_vtable(radio, func);
  543. }
  544. static int si476x_radio_change_func(struct si476x_radio *radio,
  545. enum si476x_func func)
  546. {
  547. int err;
  548. bool soft;
  549. /*
  550. * Since power/up down is a very time consuming operation,
  551. * try to avoid doing it if the requested mode matches the one
  552. * the tuner is in
  553. */
  554. if (func == radio->core->power_up_parameters.func)
  555. return 0;
  556. soft = true;
  557. err = si476x_core_stop(radio->core, soft);
  558. if (err < 0) {
  559. /*
  560. * OK, if the chip does not want to play nice let's
  561. * try to reset it in more brutal way
  562. */
  563. soft = false;
  564. err = si476x_core_stop(radio->core, soft);
  565. if (err < 0)
  566. return err;
  567. }
  568. /*
  569. Set the desired radio tuner function
  570. */
  571. radio->core->power_up_parameters.func = func;
  572. err = si476x_core_start(radio->core, soft);
  573. if (err < 0)
  574. return err;
  575. /*
  576. * No need to do the rest of manipulations for the bootlader
  577. * mode
  578. */
  579. if (func != SI476X_FUNC_FM_RECEIVER &&
  580. func != SI476X_FUNC_AM_RECEIVER)
  581. return err;
  582. return si476x_radio_do_post_powerup_init(radio, func);
  583. }
  584. static int si476x_radio_g_frequency(struct file *file, void *priv,
  585. struct v4l2_frequency *f)
  586. {
  587. int err;
  588. struct si476x_radio *radio = video_drvdata(file);
  589. if (f->tuner != 0 ||
  590. f->type != V4L2_TUNER_RADIO)
  591. return -EINVAL;
  592. si476x_core_lock(radio->core);
  593. if (radio->ops->rsq_status) {
  594. struct si476x_rsq_status_report report;
  595. struct si476x_rsq_status_args args = {
  596. .primary = false,
  597. .rsqack = false,
  598. .attune = true,
  599. .cancel = false,
  600. .stcack = false,
  601. };
  602. err = radio->ops->rsq_status(radio->core, &args, &report);
  603. if (!err)
  604. f->frequency = si476x_to_v4l2(radio->core,
  605. report.readfreq);
  606. } else {
  607. err = -EINVAL;
  608. }
  609. si476x_core_unlock(radio->core);
  610. return err;
  611. }
  612. static int si476x_radio_s_frequency(struct file *file, void *priv,
  613. const struct v4l2_frequency *f)
  614. {
  615. int err;
  616. u32 freq = f->frequency;
  617. struct si476x_tune_freq_args args;
  618. struct si476x_radio *radio = video_drvdata(file);
  619. const u32 midrange = (si476x_bands[SI476X_BAND_AM].rangehigh +
  620. si476x_bands[SI476X_BAND_FM].rangelow) / 2;
  621. const int band = (freq > midrange) ?
  622. SI476X_BAND_FM : SI476X_BAND_AM;
  623. const enum si476x_func func = (band == SI476X_BAND_AM) ?
  624. SI476X_FUNC_AM_RECEIVER : SI476X_FUNC_FM_RECEIVER;
  625. if (f->tuner != 0 ||
  626. f->type != V4L2_TUNER_RADIO)
  627. return -EINVAL;
  628. si476x_core_lock(radio->core);
  629. freq = clamp(freq,
  630. si476x_bands[band].rangelow,
  631. si476x_bands[band].rangehigh);
  632. if (si476x_radio_freq_is_inside_of_the_band(freq,
  633. SI476X_BAND_AM) &&
  634. (!si476x_core_has_am(radio->core) ||
  635. si476x_core_is_a_secondary_tuner(radio->core))) {
  636. err = -EINVAL;
  637. goto unlock;
  638. }
  639. err = si476x_radio_change_func(radio, func);
  640. if (err < 0)
  641. goto unlock;
  642. args.zifsr = false;
  643. args.hd = false;
  644. args.injside = SI476X_INJSIDE_AUTO;
  645. args.freq = v4l2_to_si476x(radio->core, freq);
  646. args.tunemode = SI476X_TM_VALIDATED_NORMAL_TUNE;
  647. args.smoothmetrics = SI476X_SM_INITIALIZE_AUDIO;
  648. args.antcap = 0;
  649. err = radio->ops->tune_freq(radio->core, &args);
  650. unlock:
  651. si476x_core_unlock(radio->core);
  652. return err;
  653. }
  654. static int si476x_radio_s_hw_freq_seek(struct file *file, void *priv,
  655. const struct v4l2_hw_freq_seek *seek)
  656. {
  657. int err;
  658. enum si476x_func func;
  659. u32 rangelow, rangehigh;
  660. struct si476x_radio *radio = video_drvdata(file);
  661. if (file->f_flags & O_NONBLOCK)
  662. return -EAGAIN;
  663. if (seek->tuner != 0 ||
  664. seek->type != V4L2_TUNER_RADIO)
  665. return -EINVAL;
  666. si476x_core_lock(radio->core);
  667. if (!seek->rangelow) {
  668. err = regmap_read(radio->core->regmap,
  669. SI476X_PROP_SEEK_BAND_BOTTOM,
  670. &rangelow);
  671. if (!err)
  672. rangelow = si476x_to_v4l2(radio->core, rangelow);
  673. else
  674. goto unlock;
  675. }
  676. if (!seek->rangehigh) {
  677. err = regmap_read(radio->core->regmap,
  678. SI476X_PROP_SEEK_BAND_TOP,
  679. &rangehigh);
  680. if (!err)
  681. rangehigh = si476x_to_v4l2(radio->core, rangehigh);
  682. else
  683. goto unlock;
  684. }
  685. if (rangelow > rangehigh) {
  686. err = -EINVAL;
  687. goto unlock;
  688. }
  689. if (si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
  690. SI476X_BAND_FM)) {
  691. func = SI476X_FUNC_FM_RECEIVER;
  692. } else if (si476x_core_has_am(radio->core) &&
  693. si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
  694. SI476X_BAND_AM)) {
  695. func = SI476X_FUNC_AM_RECEIVER;
  696. } else {
  697. err = -EINVAL;
  698. goto unlock;
  699. }
  700. err = si476x_radio_change_func(radio, func);
  701. if (err < 0)
  702. goto unlock;
  703. if (seek->rangehigh) {
  704. err = regmap_write(radio->core->regmap,
  705. SI476X_PROP_SEEK_BAND_TOP,
  706. v4l2_to_si476x(radio->core,
  707. seek->rangehigh));
  708. if (err)
  709. goto unlock;
  710. }
  711. if (seek->rangelow) {
  712. err = regmap_write(radio->core->regmap,
  713. SI476X_PROP_SEEK_BAND_BOTTOM,
  714. v4l2_to_si476x(radio->core,
  715. seek->rangelow));
  716. if (err)
  717. goto unlock;
  718. }
  719. if (seek->spacing) {
  720. err = regmap_write(radio->core->regmap,
  721. SI476X_PROP_SEEK_FREQUENCY_SPACING,
  722. v4l2_to_si476x(radio->core,
  723. seek->spacing));
  724. if (err)
  725. goto unlock;
  726. }
  727. err = radio->ops->seek_start(radio->core,
  728. seek->seek_upward,
  729. seek->wrap_around);
  730. unlock:
  731. si476x_core_unlock(radio->core);
  732. return err;
  733. }
  734. static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  735. {
  736. int retval;
  737. struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
  738. si476x_core_lock(radio->core);
  739. switch (ctrl->id) {
  740. case V4L2_CID_SI476X_INTERCHIP_LINK:
  741. if (si476x_core_has_diversity(radio->core)) {
  742. if (radio->ops->phase_diversity) {
  743. retval = radio->ops->phase_div_status(radio->core);
  744. if (retval < 0)
  745. break;
  746. ctrl->val = !!SI476X_PHDIV_STATUS_LINK_LOCKED(retval);
  747. retval = 0;
  748. break;
  749. } else {
  750. retval = -ENOTTY;
  751. break;
  752. }
  753. }
  754. retval = -EINVAL;
  755. break;
  756. default:
  757. retval = -EINVAL;
  758. break;
  759. }
  760. si476x_core_unlock(radio->core);
  761. return retval;
  762. }
  763. static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl)
  764. {
  765. int retval;
  766. enum si476x_phase_diversity_mode mode;
  767. struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
  768. si476x_core_lock(radio->core);
  769. switch (ctrl->id) {
  770. case V4L2_CID_SI476X_HARMONICS_COUNT:
  771. retval = regmap_update_bits(radio->core->regmap,
  772. SI476X_PROP_AUDIO_PWR_LINE_FILTER,
  773. SI476X_PROP_PWR_HARMONICS_MASK,
  774. ctrl->val);
  775. break;
  776. case V4L2_CID_POWER_LINE_FREQUENCY:
  777. switch (ctrl->val) {
  778. case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
  779. retval = regmap_update_bits(radio->core->regmap,
  780. SI476X_PROP_AUDIO_PWR_LINE_FILTER,
  781. SI476X_PROP_PWR_ENABLE_MASK,
  782. 0);
  783. break;
  784. case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
  785. retval = regmap_update_bits(radio->core->regmap,
  786. SI476X_PROP_AUDIO_PWR_LINE_FILTER,
  787. SI476X_PROP_PWR_GRID_MASK,
  788. SI476X_PROP_PWR_GRID_50HZ);
  789. break;
  790. case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
  791. retval = regmap_update_bits(radio->core->regmap,
  792. SI476X_PROP_AUDIO_PWR_LINE_FILTER,
  793. SI476X_PROP_PWR_GRID_MASK,
  794. SI476X_PROP_PWR_GRID_60HZ);
  795. break;
  796. default:
  797. retval = -EINVAL;
  798. break;
  799. }
  800. break;
  801. case V4L2_CID_SI476X_RSSI_THRESHOLD:
  802. retval = regmap_write(radio->core->regmap,
  803. SI476X_PROP_VALID_RSSI_THRESHOLD,
  804. ctrl->val);
  805. break;
  806. case V4L2_CID_SI476X_SNR_THRESHOLD:
  807. retval = regmap_write(radio->core->regmap,
  808. SI476X_PROP_VALID_SNR_THRESHOLD,
  809. ctrl->val);
  810. break;
  811. case V4L2_CID_SI476X_MAX_TUNE_ERROR:
  812. retval = regmap_write(radio->core->regmap,
  813. SI476X_PROP_VALID_MAX_TUNE_ERROR,
  814. ctrl->val);
  815. break;
  816. case V4L2_CID_RDS_RECEPTION:
  817. /*
  818. * It looks like RDS related properties are
  819. * inaccesable when tuner is in AM mode, so cache the
  820. * changes
  821. */
  822. if (si476x_core_is_in_am_receiver_mode(radio->core))
  823. regcache_cache_only(radio->core->regmap, true);
  824. if (ctrl->val) {
  825. retval = regmap_write(radio->core->regmap,
  826. SI476X_PROP_FM_RDS_INTERRUPT_FIFO_COUNT,
  827. radio->core->rds_fifo_depth);
  828. if (retval < 0)
  829. break;
  830. if (radio->core->client->irq) {
  831. retval = regmap_write(radio->core->regmap,
  832. SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
  833. SI476X_RDSRECV);
  834. if (retval < 0)
  835. break;
  836. }
  837. /* Drain RDS FIFO before enabling RDS processing */
  838. retval = si476x_core_cmd_fm_rds_status(radio->core,
  839. false,
  840. true,
  841. true,
  842. NULL);
  843. if (retval < 0)
  844. break;
  845. retval = regmap_update_bits(radio->core->regmap,
  846. SI476X_PROP_FM_RDS_CONFIG,
  847. SI476X_PROP_RDSEN_MASK,
  848. SI476X_PROP_RDSEN);
  849. } else {
  850. retval = regmap_update_bits(radio->core->regmap,
  851. SI476X_PROP_FM_RDS_CONFIG,
  852. SI476X_PROP_RDSEN_MASK,
  853. !SI476X_PROP_RDSEN);
  854. }
  855. if (si476x_core_is_in_am_receiver_mode(radio->core))
  856. regcache_cache_only(radio->core->regmap, false);
  857. break;
  858. case V4L2_CID_TUNE_DEEMPHASIS:
  859. retval = regmap_write(radio->core->regmap,
  860. SI476X_PROP_AUDIO_DEEMPHASIS,
  861. ctrl->val);
  862. break;
  863. case V4L2_CID_SI476X_DIVERSITY_MODE:
  864. mode = si476x_phase_diversity_idx_to_mode(ctrl->val);
  865. if (mode == radio->core->diversity_mode) {
  866. retval = 0;
  867. break;
  868. }
  869. if (si476x_core_is_in_am_receiver_mode(radio->core)) {
  870. /*
  871. * Diversity cannot be configured while tuner
  872. * is in AM mode so save the changes and carry on.
  873. */
  874. radio->core->diversity_mode = mode;
  875. retval = 0;
  876. } else {
  877. retval = radio->ops->phase_diversity(radio->core, mode);
  878. if (!retval)
  879. radio->core->diversity_mode = mode;
  880. }
  881. break;
  882. default:
  883. retval = -EINVAL;
  884. break;
  885. }
  886. si476x_core_unlock(radio->core);
  887. return retval;
  888. }
  889. #ifdef CONFIG_VIDEO_ADV_DEBUG
  890. static int si476x_radio_g_register(struct file *file, void *fh,
  891. struct v4l2_dbg_register *reg)
  892. {
  893. int err;
  894. unsigned int value;
  895. struct si476x_radio *radio = video_drvdata(file);
  896. si476x_core_lock(radio->core);
  897. reg->size = 2;
  898. err = regmap_read(radio->core->regmap,
  899. (unsigned int)reg->reg, &value);
  900. reg->val = value;
  901. si476x_core_unlock(radio->core);
  902. return err;
  903. }
  904. static int si476x_radio_s_register(struct file *file, void *fh,
  905. const struct v4l2_dbg_register *reg)
  906. {
  907. int err;
  908. struct si476x_radio *radio = video_drvdata(file);
  909. si476x_core_lock(radio->core);
  910. err = regmap_write(radio->core->regmap,
  911. (unsigned int)reg->reg,
  912. (unsigned int)reg->val);
  913. si476x_core_unlock(radio->core);
  914. return err;
  915. }
  916. #endif
  917. static int si476x_radio_fops_open(struct file *file)
  918. {
  919. struct si476x_radio *radio = video_drvdata(file);
  920. int err;
  921. err = v4l2_fh_open(file);
  922. if (err)
  923. return err;
  924. if (v4l2_fh_is_singular_file(file)) {
  925. si476x_core_lock(radio->core);
  926. err = si476x_core_set_power_state(radio->core,
  927. SI476X_POWER_UP_FULL);
  928. if (err < 0)
  929. goto done;
  930. err = si476x_radio_do_post_powerup_init(radio,
  931. radio->core->power_up_parameters.func);
  932. if (err < 0)
  933. goto power_down;
  934. err = si476x_radio_pretune(radio,
  935. radio->core->power_up_parameters.func);
  936. if (err < 0)
  937. goto power_down;
  938. si476x_core_unlock(radio->core);
  939. /*Must be done after si476x_core_unlock to prevent a deadlock*/
  940. v4l2_ctrl_handler_setup(&radio->ctrl_handler);
  941. }
  942. return err;
  943. power_down:
  944. si476x_core_set_power_state(radio->core,
  945. SI476X_POWER_DOWN);
  946. done:
  947. si476x_core_unlock(radio->core);
  948. v4l2_fh_release(file);
  949. return err;
  950. }
  951. static int si476x_radio_fops_release(struct file *file)
  952. {
  953. int err;
  954. struct si476x_radio *radio = video_drvdata(file);
  955. if (v4l2_fh_is_singular_file(file) &&
  956. atomic_read(&radio->core->is_alive))
  957. si476x_core_set_power_state(radio->core,
  958. SI476X_POWER_DOWN);
  959. err = v4l2_fh_release(file);
  960. return err;
  961. }
  962. static ssize_t si476x_radio_fops_read(struct file *file, char __user *buf,
  963. size_t count, loff_t *ppos)
  964. {
  965. ssize_t rval;
  966. size_t fifo_len;
  967. unsigned int copied;
  968. struct si476x_radio *radio = video_drvdata(file);
  969. /* block if no new data available */
  970. if (kfifo_is_empty(&radio->core->rds_fifo)) {
  971. if (file->f_flags & O_NONBLOCK)
  972. return -EWOULDBLOCK;
  973. rval = wait_event_interruptible(radio->core->rds_read_queue,
  974. (!kfifo_is_empty(&radio->core->rds_fifo) ||
  975. !atomic_read(&radio->core->is_alive)));
  976. if (rval < 0)
  977. return -EINTR;
  978. if (!atomic_read(&radio->core->is_alive))
  979. return -ENODEV;
  980. }
  981. fifo_len = kfifo_len(&radio->core->rds_fifo);
  982. if (kfifo_to_user(&radio->core->rds_fifo, buf,
  983. min(fifo_len, count),
  984. &copied) != 0) {
  985. dev_warn(&radio->videodev.dev,
  986. "Error during FIFO to userspace copy\n");
  987. rval = -EIO;
  988. } else {
  989. rval = (ssize_t)copied;
  990. }
  991. return rval;
  992. }
  993. static unsigned int si476x_radio_fops_poll(struct file *file,
  994. struct poll_table_struct *pts)
  995. {
  996. struct si476x_radio *radio = video_drvdata(file);
  997. unsigned long req_events = poll_requested_events(pts);
  998. unsigned int err = v4l2_ctrl_poll(file, pts);
  999. if (req_events & (POLLIN | POLLRDNORM)) {
  1000. if (atomic_read(&radio->core->is_alive))
  1001. poll_wait(file, &radio->core->rds_read_queue, pts);
  1002. if (!atomic_read(&radio->core->is_alive))
  1003. err = POLLHUP;
  1004. if (!kfifo_is_empty(&radio->core->rds_fifo))
  1005. err = POLLIN | POLLRDNORM;
  1006. }
  1007. return err;
  1008. }
  1009. static const struct v4l2_file_operations si476x_fops = {
  1010. .owner = THIS_MODULE,
  1011. .read = si476x_radio_fops_read,
  1012. .poll = si476x_radio_fops_poll,
  1013. .unlocked_ioctl = video_ioctl2,
  1014. .open = si476x_radio_fops_open,
  1015. .release = si476x_radio_fops_release,
  1016. };
  1017. static const struct v4l2_ioctl_ops si4761_ioctl_ops = {
  1018. .vidioc_querycap = si476x_radio_querycap,
  1019. .vidioc_g_tuner = si476x_radio_g_tuner,
  1020. .vidioc_s_tuner = si476x_radio_s_tuner,
  1021. .vidioc_g_frequency = si476x_radio_g_frequency,
  1022. .vidioc_s_frequency = si476x_radio_s_frequency,
  1023. .vidioc_s_hw_freq_seek = si476x_radio_s_hw_freq_seek,
  1024. .vidioc_enum_freq_bands = si476x_radio_enum_freq_bands,
  1025. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1026. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1027. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1028. .vidioc_g_register = si476x_radio_g_register,
  1029. .vidioc_s_register = si476x_radio_s_register,
  1030. #endif
  1031. };
  1032. static const struct video_device si476x_viddev_template = {
  1033. .fops = &si476x_fops,
  1034. .name = DRIVER_NAME,
  1035. .release = video_device_release_empty,
  1036. };
  1037. static ssize_t si476x_radio_read_acf_blob(struct file *file,
  1038. char __user *user_buf,
  1039. size_t count, loff_t *ppos)
  1040. {
  1041. int err;
  1042. struct si476x_radio *radio = file->private_data;
  1043. struct si476x_acf_status_report report;
  1044. si476x_core_lock(radio->core);
  1045. if (radio->ops->acf_status)
  1046. err = radio->ops->acf_status(radio->core, &report);
  1047. else
  1048. err = -ENOENT;
  1049. si476x_core_unlock(radio->core);
  1050. if (err < 0)
  1051. return err;
  1052. return simple_read_from_buffer(user_buf, count, ppos, &report,
  1053. sizeof(report));
  1054. }
  1055. static const struct file_operations radio_acf_fops = {
  1056. .open = simple_open,
  1057. .llseek = default_llseek,
  1058. .read = si476x_radio_read_acf_blob,
  1059. };
  1060. static ssize_t si476x_radio_read_rds_blckcnt_blob(struct file *file,
  1061. char __user *user_buf,
  1062. size_t count, loff_t *ppos)
  1063. {
  1064. int err;
  1065. struct si476x_radio *radio = file->private_data;
  1066. struct si476x_rds_blockcount_report report;
  1067. si476x_core_lock(radio->core);
  1068. if (radio->ops->rds_blckcnt)
  1069. err = radio->ops->rds_blckcnt(radio->core, true,
  1070. &report);
  1071. else
  1072. err = -ENOENT;
  1073. si476x_core_unlock(radio->core);
  1074. if (err < 0)
  1075. return err;
  1076. return simple_read_from_buffer(user_buf, count, ppos, &report,
  1077. sizeof(report));
  1078. }
  1079. static const struct file_operations radio_rds_blckcnt_fops = {
  1080. .open = simple_open,
  1081. .llseek = default_llseek,
  1082. .read = si476x_radio_read_rds_blckcnt_blob,
  1083. };
  1084. static ssize_t si476x_radio_read_agc_blob(struct file *file,
  1085. char __user *user_buf,
  1086. size_t count, loff_t *ppos)
  1087. {
  1088. int err;
  1089. struct si476x_radio *radio = file->private_data;
  1090. struct si476x_agc_status_report report;
  1091. si476x_core_lock(radio->core);
  1092. if (radio->ops->rds_blckcnt)
  1093. err = radio->ops->agc_status(radio->core, &report);
  1094. else
  1095. err = -ENOENT;
  1096. si476x_core_unlock(radio->core);
  1097. if (err < 0)
  1098. return err;
  1099. return simple_read_from_buffer(user_buf, count, ppos, &report,
  1100. sizeof(report));
  1101. }
  1102. static const struct file_operations radio_agc_fops = {
  1103. .open = simple_open,
  1104. .llseek = default_llseek,
  1105. .read = si476x_radio_read_agc_blob,
  1106. };
  1107. static ssize_t si476x_radio_read_rsq_blob(struct file *file,
  1108. char __user *user_buf,
  1109. size_t count, loff_t *ppos)
  1110. {
  1111. int err;
  1112. struct si476x_radio *radio = file->private_data;
  1113. struct si476x_rsq_status_report report;
  1114. struct si476x_rsq_status_args args = {
  1115. .primary = false,
  1116. .rsqack = false,
  1117. .attune = false,
  1118. .cancel = false,
  1119. .stcack = false,
  1120. };
  1121. si476x_core_lock(radio->core);
  1122. if (radio->ops->rds_blckcnt)
  1123. err = radio->ops->rsq_status(radio->core, &args, &report);
  1124. else
  1125. err = -ENOENT;
  1126. si476x_core_unlock(radio->core);
  1127. if (err < 0)
  1128. return err;
  1129. return simple_read_from_buffer(user_buf, count, ppos, &report,
  1130. sizeof(report));
  1131. }
  1132. static const struct file_operations radio_rsq_fops = {
  1133. .open = simple_open,
  1134. .llseek = default_llseek,
  1135. .read = si476x_radio_read_rsq_blob,
  1136. };
  1137. static ssize_t si476x_radio_read_rsq_primary_blob(struct file *file,
  1138. char __user *user_buf,
  1139. size_t count, loff_t *ppos)
  1140. {
  1141. int err;
  1142. struct si476x_radio *radio = file->private_data;
  1143. struct si476x_rsq_status_report report;
  1144. struct si476x_rsq_status_args args = {
  1145. .primary = true,
  1146. .rsqack = false,
  1147. .attune = false,
  1148. .cancel = false,
  1149. .stcack = false,
  1150. };
  1151. si476x_core_lock(radio->core);
  1152. if (radio->ops->rds_blckcnt)
  1153. err = radio->ops->rsq_status(radio->core, &args, &report);
  1154. else
  1155. err = -ENOENT;
  1156. si476x_core_unlock(radio->core);
  1157. if (err < 0)
  1158. return err;
  1159. return simple_read_from_buffer(user_buf, count, ppos, &report,
  1160. sizeof(report));
  1161. }
  1162. static const struct file_operations radio_rsq_primary_fops = {
  1163. .open = simple_open,
  1164. .llseek = default_llseek,
  1165. .read = si476x_radio_read_rsq_primary_blob,
  1166. };
  1167. static int si476x_radio_init_debugfs(struct si476x_radio *radio)
  1168. {
  1169. struct dentry *dentry;
  1170. int ret;
  1171. dentry = debugfs_create_dir(dev_name(radio->v4l2dev.dev), NULL);
  1172. if (IS_ERR(dentry)) {
  1173. ret = PTR_ERR(dentry);
  1174. goto exit;
  1175. }
  1176. radio->debugfs = dentry;
  1177. dentry = debugfs_create_file("acf", S_IRUGO,
  1178. radio->debugfs, radio, &radio_acf_fops);
  1179. if (IS_ERR(dentry)) {
  1180. ret = PTR_ERR(dentry);
  1181. goto cleanup;
  1182. }
  1183. dentry = debugfs_create_file("rds_blckcnt", S_IRUGO,
  1184. radio->debugfs, radio,
  1185. &radio_rds_blckcnt_fops);
  1186. if (IS_ERR(dentry)) {
  1187. ret = PTR_ERR(dentry);
  1188. goto cleanup;
  1189. }
  1190. dentry = debugfs_create_file("agc", S_IRUGO,
  1191. radio->debugfs, radio, &radio_agc_fops);
  1192. if (IS_ERR(dentry)) {
  1193. ret = PTR_ERR(dentry);
  1194. goto cleanup;
  1195. }
  1196. dentry = debugfs_create_file("rsq", S_IRUGO,
  1197. radio->debugfs, radio, &radio_rsq_fops);
  1198. if (IS_ERR(dentry)) {
  1199. ret = PTR_ERR(dentry);
  1200. goto cleanup;
  1201. }
  1202. dentry = debugfs_create_file("rsq_primary", S_IRUGO,
  1203. radio->debugfs, radio,
  1204. &radio_rsq_primary_fops);
  1205. if (IS_ERR(dentry)) {
  1206. ret = PTR_ERR(dentry);
  1207. goto cleanup;
  1208. }
  1209. return 0;
  1210. cleanup:
  1211. debugfs_remove_recursive(radio->debugfs);
  1212. exit:
  1213. return ret;
  1214. }
  1215. static int si476x_radio_add_new_custom(struct si476x_radio *radio,
  1216. enum si476x_ctrl_idx idx)
  1217. {
  1218. int rval;
  1219. struct v4l2_ctrl *ctrl;
  1220. ctrl = v4l2_ctrl_new_custom(&radio->ctrl_handler,
  1221. &si476x_ctrls[idx],
  1222. NULL);
  1223. rval = radio->ctrl_handler.error;
  1224. if (ctrl == NULL && rval)
  1225. dev_err(radio->v4l2dev.dev,
  1226. "Could not initialize '%s' control %d\n",
  1227. si476x_ctrls[idx].name, rval);
  1228. return rval;
  1229. }
  1230. static int si476x_radio_probe(struct platform_device *pdev)
  1231. {
  1232. int rval;
  1233. struct si476x_radio *radio;
  1234. struct v4l2_ctrl *ctrl;
  1235. static atomic_t instance = ATOMIC_INIT(0);
  1236. radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
  1237. if (!radio)
  1238. return -ENOMEM;
  1239. radio->core = i2c_mfd_cell_to_core(&pdev->dev);
  1240. v4l2_device_set_name(&radio->v4l2dev, DRIVER_NAME, &instance);
  1241. rval = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
  1242. if (rval) {
  1243. dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
  1244. return rval;
  1245. }
  1246. memcpy(&radio->videodev, &si476x_viddev_template,
  1247. sizeof(struct video_device));
  1248. radio->videodev.v4l2_dev = &radio->v4l2dev;
  1249. radio->videodev.ioctl_ops = &si4761_ioctl_ops;
  1250. video_set_drvdata(&radio->videodev, radio);
  1251. platform_set_drvdata(pdev, radio);
  1252. radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
  1253. v4l2_ctrl_handler_init(&radio->ctrl_handler,
  1254. 1 + ARRAY_SIZE(si476x_ctrls));
  1255. if (si476x_core_has_am(radio->core)) {
  1256. ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
  1257. &si476x_ctrl_ops,
  1258. V4L2_CID_POWER_LINE_FREQUENCY,
  1259. V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  1260. 0, 0);
  1261. rval = radio->ctrl_handler.error;
  1262. if (ctrl == NULL && rval) {
  1263. dev_err(&pdev->dev, "Could not initialize V4L2_CID_POWER_LINE_FREQUENCY control %d\n",
  1264. rval);
  1265. goto exit;
  1266. }
  1267. rval = si476x_radio_add_new_custom(radio,
  1268. SI476X_IDX_HARMONICS_COUNT);
  1269. if (rval < 0)
  1270. goto exit;
  1271. }
  1272. rval = si476x_radio_add_new_custom(radio, SI476X_IDX_RSSI_THRESHOLD);
  1273. if (rval < 0)
  1274. goto exit;
  1275. rval = si476x_radio_add_new_custom(radio, SI476X_IDX_SNR_THRESHOLD);
  1276. if (rval < 0)
  1277. goto exit;
  1278. rval = si476x_radio_add_new_custom(radio, SI476X_IDX_MAX_TUNE_ERROR);
  1279. if (rval < 0)
  1280. goto exit;
  1281. ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
  1282. &si476x_ctrl_ops,
  1283. V4L2_CID_TUNE_DEEMPHASIS,
  1284. V4L2_DEEMPHASIS_75_uS, 0, 0);
  1285. rval = radio->ctrl_handler.error;
  1286. if (ctrl == NULL && rval) {
  1287. dev_err(&pdev->dev, "Could not initialize V4L2_CID_TUNE_DEEMPHASIS control %d\n",
  1288. rval);
  1289. goto exit;
  1290. }
  1291. ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &si476x_ctrl_ops,
  1292. V4L2_CID_RDS_RECEPTION,
  1293. 0, 1, 1, 1);
  1294. rval = radio->ctrl_handler.error;
  1295. if (ctrl == NULL && rval) {
  1296. dev_err(&pdev->dev, "Could not initialize V4L2_CID_RDS_RECEPTION control %d\n",
  1297. rval);
  1298. goto exit;
  1299. }
  1300. if (si476x_core_has_diversity(radio->core)) {
  1301. si476x_ctrls[SI476X_IDX_DIVERSITY_MODE].def =
  1302. si476x_phase_diversity_mode_to_idx(radio->core->diversity_mode);
  1303. si476x_radio_add_new_custom(radio, SI476X_IDX_DIVERSITY_MODE);
  1304. if (rval < 0)
  1305. goto exit;
  1306. si476x_radio_add_new_custom(radio, SI476X_IDX_INTERCHIP_LINK);
  1307. if (rval < 0)
  1308. goto exit;
  1309. }
  1310. /* register video device */
  1311. rval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, -1);
  1312. if (rval < 0) {
  1313. dev_err(&pdev->dev, "Could not register video device\n");
  1314. goto exit;
  1315. }
  1316. rval = si476x_radio_init_debugfs(radio);
  1317. if (rval < 0) {
  1318. dev_err(&pdev->dev, "Could not creat debugfs interface\n");
  1319. goto exit;
  1320. }
  1321. return 0;
  1322. exit:
  1323. v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
  1324. return rval;
  1325. }
  1326. static int si476x_radio_remove(struct platform_device *pdev)
  1327. {
  1328. struct si476x_radio *radio = platform_get_drvdata(pdev);
  1329. v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
  1330. video_unregister_device(&radio->videodev);
  1331. v4l2_device_unregister(&radio->v4l2dev);
  1332. debugfs_remove_recursive(radio->debugfs);
  1333. return 0;
  1334. }
  1335. MODULE_ALIAS("platform:si476x-radio");
  1336. static struct platform_driver si476x_radio_driver = {
  1337. .driver = {
  1338. .name = DRIVER_NAME,
  1339. },
  1340. .probe = si476x_radio_probe,
  1341. .remove = si476x_radio_remove,
  1342. };
  1343. module_platform_driver(si476x_radio_driver);
  1344. MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
  1345. MODULE_DESCRIPTION("Driver for Si4761/64/68 AM/FM Radio MFD Cell");
  1346. MODULE_LICENSE("GPL");