radio-si476x.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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/drv-intf/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. * @v4l2dev: Pointer to V4L2 device created by V4L2 subsystem
  270. * @videodev: Pointer to video device created by V4L2 subsystem
  271. * @ctrl_handler: V4L2 controls handler
  272. * @core: Pointer to underlying core device
  273. * @ops: Vtable of functions. See struct si476x_radio_ops for details
  274. * @debugfs: pointer to &strucd dentry for debugfs
  275. * @audmode: audio mode, as defined for the rxsubchans field
  276. * at videodev2.h
  277. *
  278. * core structure is the radio device is being used
  279. */
  280. struct si476x_radio {
  281. struct v4l2_device v4l2dev;
  282. struct video_device videodev;
  283. struct v4l2_ctrl_handler ctrl_handler;
  284. struct si476x_core *core;
  285. /* This field should not be accesses unless core lock is held */
  286. const struct si476x_radio_ops *ops;
  287. struct dentry *debugfs;
  288. u32 audmode;
  289. };
  290. static inline struct si476x_radio *
  291. v4l2_dev_to_radio(struct v4l2_device *d)
  292. {
  293. return container_of(d, struct si476x_radio, v4l2dev);
  294. }
  295. static inline struct si476x_radio *
  296. v4l2_ctrl_handler_to_radio(struct v4l2_ctrl_handler *d)
  297. {
  298. return container_of(d, struct si476x_radio, ctrl_handler);
  299. }
  300. /*
  301. * si476x_vidioc_querycap - query device capabilities
  302. */
  303. static int si476x_radio_querycap(struct file *file, void *priv,
  304. struct v4l2_capability *capability)
  305. {
  306. struct si476x_radio *radio = video_drvdata(file);
  307. strlcpy(capability->driver, radio->v4l2dev.name,
  308. sizeof(capability->driver));
  309. strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
  310. snprintf(capability->bus_info, sizeof(capability->bus_info),
  311. "platform:%s", radio->v4l2dev.name);
  312. capability->device_caps = V4L2_CAP_TUNER
  313. | V4L2_CAP_RADIO
  314. | V4L2_CAP_HW_FREQ_SEEK;
  315. si476x_core_lock(radio->core);
  316. if (!si476x_core_is_a_secondary_tuner(radio->core))
  317. capability->device_caps |= V4L2_CAP_RDS_CAPTURE
  318. | V4L2_CAP_READWRITE;
  319. si476x_core_unlock(radio->core);
  320. capability->capabilities = capability->device_caps
  321. | V4L2_CAP_DEVICE_CAPS;
  322. return 0;
  323. }
  324. static int si476x_radio_enum_freq_bands(struct file *file, void *priv,
  325. struct v4l2_frequency_band *band)
  326. {
  327. int err;
  328. struct si476x_radio *radio = video_drvdata(file);
  329. if (band->tuner != 0)
  330. return -EINVAL;
  331. switch (radio->core->chip_id) {
  332. /* AM/FM tuners -- all bands are supported */
  333. case SI476X_CHIP_SI4761:
  334. case SI476X_CHIP_SI4764:
  335. if (band->index < ARRAY_SIZE(si476x_bands)) {
  336. *band = si476x_bands[band->index];
  337. err = 0;
  338. } else {
  339. err = -EINVAL;
  340. }
  341. break;
  342. /* FM companion tuner chips -- only FM bands are
  343. * supported */
  344. case SI476X_CHIP_SI4768:
  345. if (band->index == SI476X_BAND_FM) {
  346. *band = si476x_bands[band->index];
  347. err = 0;
  348. } else {
  349. err = -EINVAL;
  350. }
  351. break;
  352. default:
  353. err = -EINVAL;
  354. }
  355. return err;
  356. }
  357. static int si476x_radio_g_tuner(struct file *file, void *priv,
  358. struct v4l2_tuner *tuner)
  359. {
  360. int err;
  361. struct si476x_rsq_status_report report;
  362. struct si476x_radio *radio = video_drvdata(file);
  363. struct si476x_rsq_status_args args = {
  364. .primary = false,
  365. .rsqack = false,
  366. .attune = false,
  367. .cancel = false,
  368. .stcack = false,
  369. };
  370. if (tuner->index != 0)
  371. return -EINVAL;
  372. tuner->type = V4L2_TUNER_RADIO;
  373. tuner->capability = V4L2_TUNER_CAP_LOW /* Measure frequencies
  374. * in multiples of
  375. * 62.5 Hz */
  376. | V4L2_TUNER_CAP_STEREO
  377. | V4L2_TUNER_CAP_HWSEEK_BOUNDED
  378. | V4L2_TUNER_CAP_HWSEEK_WRAP
  379. | V4L2_TUNER_CAP_HWSEEK_PROG_LIM;
  380. si476x_core_lock(radio->core);
  381. if (si476x_core_is_a_secondary_tuner(radio->core)) {
  382. strlcpy(tuner->name, "FM (secondary)", sizeof(tuner->name));
  383. tuner->rxsubchans = 0;
  384. tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
  385. } else if (si476x_core_has_am(radio->core)) {
  386. if (si476x_core_is_a_primary_tuner(radio->core))
  387. strlcpy(tuner->name, "AM/FM (primary)",
  388. sizeof(tuner->name));
  389. else
  390. strlcpy(tuner->name, "AM/FM", sizeof(tuner->name));
  391. tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO
  392. | V4L2_TUNER_SUB_RDS;
  393. tuner->capability |= V4L2_TUNER_CAP_RDS
  394. | V4L2_TUNER_CAP_RDS_BLOCK_IO
  395. | V4L2_TUNER_CAP_FREQ_BANDS;
  396. tuner->rangelow = si476x_bands[SI476X_BAND_AM].rangelow;
  397. } else {
  398. strlcpy(tuner->name, "FM", sizeof(tuner->name));
  399. tuner->rxsubchans = V4L2_TUNER_SUB_RDS;
  400. tuner->capability |= V4L2_TUNER_CAP_RDS
  401. | V4L2_TUNER_CAP_RDS_BLOCK_IO
  402. | V4L2_TUNER_CAP_FREQ_BANDS;
  403. tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
  404. }
  405. tuner->audmode = radio->audmode;
  406. tuner->afc = 1;
  407. tuner->rangehigh = si476x_bands[SI476X_BAND_FM].rangehigh;
  408. err = radio->ops->rsq_status(radio->core,
  409. &args, &report);
  410. if (err < 0) {
  411. tuner->signal = 0;
  412. } else {
  413. /*
  414. * tuner->signal value range: 0x0000 .. 0xFFFF,
  415. * report.rssi: -128 .. 127
  416. */
  417. tuner->signal = (report.rssi + 128) * 257;
  418. }
  419. si476x_core_unlock(radio->core);
  420. return err;
  421. }
  422. static int si476x_radio_s_tuner(struct file *file, void *priv,
  423. const struct v4l2_tuner *tuner)
  424. {
  425. struct si476x_radio *radio = video_drvdata(file);
  426. if (tuner->index != 0)
  427. return -EINVAL;
  428. if (tuner->audmode == V4L2_TUNER_MODE_MONO ||
  429. tuner->audmode == V4L2_TUNER_MODE_STEREO)
  430. radio->audmode = tuner->audmode;
  431. else
  432. radio->audmode = V4L2_TUNER_MODE_STEREO;
  433. return 0;
  434. }
  435. static int si476x_radio_init_vtable(struct si476x_radio *radio,
  436. enum si476x_func func)
  437. {
  438. static const struct si476x_radio_ops fm_ops = {
  439. .tune_freq = si476x_core_cmd_fm_tune_freq,
  440. .seek_start = si476x_core_cmd_fm_seek_start,
  441. .rsq_status = si476x_core_cmd_fm_rsq_status,
  442. .rds_blckcnt = si476x_core_cmd_fm_rds_blockcount,
  443. .phase_diversity = si476x_core_cmd_fm_phase_diversity,
  444. .phase_div_status = si476x_core_cmd_fm_phase_div_status,
  445. .acf_status = si476x_core_cmd_fm_acf_status,
  446. .agc_status = si476x_core_cmd_agc_status,
  447. };
  448. static const struct si476x_radio_ops am_ops = {
  449. .tune_freq = si476x_core_cmd_am_tune_freq,
  450. .seek_start = si476x_core_cmd_am_seek_start,
  451. .rsq_status = si476x_core_cmd_am_rsq_status,
  452. .rds_blckcnt = NULL,
  453. .phase_diversity = NULL,
  454. .phase_div_status = NULL,
  455. .acf_status = si476x_core_cmd_am_acf_status,
  456. .agc_status = NULL,
  457. };
  458. switch (func) {
  459. case SI476X_FUNC_FM_RECEIVER:
  460. radio->ops = &fm_ops;
  461. return 0;
  462. case SI476X_FUNC_AM_RECEIVER:
  463. radio->ops = &am_ops;
  464. return 0;
  465. default:
  466. WARN(1, "Unexpected tuner function value\n");
  467. return -EINVAL;
  468. }
  469. }
  470. static int si476x_radio_pretune(struct si476x_radio *radio,
  471. enum si476x_func func)
  472. {
  473. int retval;
  474. struct si476x_tune_freq_args args = {
  475. .zifsr = false,
  476. .hd = false,
  477. .injside = SI476X_INJSIDE_AUTO,
  478. .tunemode = SI476X_TM_VALIDATED_NORMAL_TUNE,
  479. .smoothmetrics = SI476X_SM_INITIALIZE_AUDIO,
  480. .antcap = 0,
  481. };
  482. switch (func) {
  483. case SI476X_FUNC_FM_RECEIVER:
  484. args.freq = v4l2_to_si476x(radio->core,
  485. 92 * FREQ_MUL);
  486. retval = radio->ops->tune_freq(radio->core, &args);
  487. break;
  488. case SI476X_FUNC_AM_RECEIVER:
  489. args.freq = v4l2_to_si476x(radio->core,
  490. 0.6 * FREQ_MUL);
  491. retval = radio->ops->tune_freq(radio->core, &args);
  492. break;
  493. default:
  494. WARN(1, "Unexpected tuner function value\n");
  495. retval = -EINVAL;
  496. }
  497. return retval;
  498. }
  499. static int si476x_radio_do_post_powerup_init(struct si476x_radio *radio,
  500. enum si476x_func func)
  501. {
  502. int err;
  503. /* regcache_mark_dirty(radio->core->regmap); */
  504. err = regcache_sync_region(radio->core->regmap,
  505. SI476X_PROP_DIGITAL_IO_INPUT_SAMPLE_RATE,
  506. SI476X_PROP_DIGITAL_IO_OUTPUT_FORMAT);
  507. if (err < 0)
  508. return err;
  509. err = regcache_sync_region(radio->core->regmap,
  510. SI476X_PROP_AUDIO_DEEMPHASIS,
  511. SI476X_PROP_AUDIO_PWR_LINE_FILTER);
  512. if (err < 0)
  513. return err;
  514. err = regcache_sync_region(radio->core->regmap,
  515. SI476X_PROP_INT_CTL_ENABLE,
  516. SI476X_PROP_INT_CTL_ENABLE);
  517. if (err < 0)
  518. return err;
  519. /*
  520. * Is there any point in restoring SNR and the like
  521. * when switching between AM/FM?
  522. */
  523. err = regcache_sync_region(radio->core->regmap,
  524. SI476X_PROP_VALID_MAX_TUNE_ERROR,
  525. SI476X_PROP_VALID_MAX_TUNE_ERROR);
  526. if (err < 0)
  527. return err;
  528. err = regcache_sync_region(radio->core->regmap,
  529. SI476X_PROP_VALID_SNR_THRESHOLD,
  530. SI476X_PROP_VALID_RSSI_THRESHOLD);
  531. if (err < 0)
  532. return err;
  533. if (func == SI476X_FUNC_FM_RECEIVER) {
  534. if (si476x_core_has_diversity(radio->core)) {
  535. err = si476x_core_cmd_fm_phase_diversity(radio->core,
  536. radio->core->diversity_mode);
  537. if (err < 0)
  538. return err;
  539. }
  540. err = regcache_sync_region(radio->core->regmap,
  541. SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
  542. SI476X_PROP_FM_RDS_CONFIG);
  543. if (err < 0)
  544. return err;
  545. }
  546. return si476x_radio_init_vtable(radio, func);
  547. }
  548. static int si476x_radio_change_func(struct si476x_radio *radio,
  549. enum si476x_func func)
  550. {
  551. int err;
  552. bool soft;
  553. /*
  554. * Since power/up down is a very time consuming operation,
  555. * try to avoid doing it if the requested mode matches the one
  556. * the tuner is in
  557. */
  558. if (func == radio->core->power_up_parameters.func)
  559. return 0;
  560. soft = true;
  561. err = si476x_core_stop(radio->core, soft);
  562. if (err < 0) {
  563. /*
  564. * OK, if the chip does not want to play nice let's
  565. * try to reset it in more brutal way
  566. */
  567. soft = false;
  568. err = si476x_core_stop(radio->core, soft);
  569. if (err < 0)
  570. return err;
  571. }
  572. /*
  573. Set the desired radio tuner function
  574. */
  575. radio->core->power_up_parameters.func = func;
  576. err = si476x_core_start(radio->core, soft);
  577. if (err < 0)
  578. return err;
  579. /*
  580. * No need to do the rest of manipulations for the bootlader
  581. * mode
  582. */
  583. if (func != SI476X_FUNC_FM_RECEIVER &&
  584. func != SI476X_FUNC_AM_RECEIVER)
  585. return err;
  586. return si476x_radio_do_post_powerup_init(radio, func);
  587. }
  588. static int si476x_radio_g_frequency(struct file *file, void *priv,
  589. struct v4l2_frequency *f)
  590. {
  591. int err;
  592. struct si476x_radio *radio = video_drvdata(file);
  593. if (f->tuner != 0 ||
  594. f->type != V4L2_TUNER_RADIO)
  595. return -EINVAL;
  596. si476x_core_lock(radio->core);
  597. if (radio->ops->rsq_status) {
  598. struct si476x_rsq_status_report report;
  599. struct si476x_rsq_status_args args = {
  600. .primary = false,
  601. .rsqack = false,
  602. .attune = true,
  603. .cancel = false,
  604. .stcack = false,
  605. };
  606. err = radio->ops->rsq_status(radio->core, &args, &report);
  607. if (!err)
  608. f->frequency = si476x_to_v4l2(radio->core,
  609. report.readfreq);
  610. } else {
  611. err = -EINVAL;
  612. }
  613. si476x_core_unlock(radio->core);
  614. return err;
  615. }
  616. static int si476x_radio_s_frequency(struct file *file, void *priv,
  617. const struct v4l2_frequency *f)
  618. {
  619. int err;
  620. u32 freq = f->frequency;
  621. struct si476x_tune_freq_args args;
  622. struct si476x_radio *radio = video_drvdata(file);
  623. const u32 midrange = (si476x_bands[SI476X_BAND_AM].rangehigh +
  624. si476x_bands[SI476X_BAND_FM].rangelow) / 2;
  625. const int band = (freq > midrange) ?
  626. SI476X_BAND_FM : SI476X_BAND_AM;
  627. const enum si476x_func func = (band == SI476X_BAND_AM) ?
  628. SI476X_FUNC_AM_RECEIVER : SI476X_FUNC_FM_RECEIVER;
  629. if (f->tuner != 0 ||
  630. f->type != V4L2_TUNER_RADIO)
  631. return -EINVAL;
  632. si476x_core_lock(radio->core);
  633. freq = clamp(freq,
  634. si476x_bands[band].rangelow,
  635. si476x_bands[band].rangehigh);
  636. if (si476x_radio_freq_is_inside_of_the_band(freq,
  637. SI476X_BAND_AM) &&
  638. (!si476x_core_has_am(radio->core) ||
  639. si476x_core_is_a_secondary_tuner(radio->core))) {
  640. err = -EINVAL;
  641. goto unlock;
  642. }
  643. err = si476x_radio_change_func(radio, func);
  644. if (err < 0)
  645. goto unlock;
  646. args.zifsr = false;
  647. args.hd = false;
  648. args.injside = SI476X_INJSIDE_AUTO;
  649. args.freq = v4l2_to_si476x(radio->core, freq);
  650. args.tunemode = SI476X_TM_VALIDATED_NORMAL_TUNE;
  651. args.smoothmetrics = SI476X_SM_INITIALIZE_AUDIO;
  652. args.antcap = 0;
  653. err = radio->ops->tune_freq(radio->core, &args);
  654. unlock:
  655. si476x_core_unlock(radio->core);
  656. return err;
  657. }
  658. static int si476x_radio_s_hw_freq_seek(struct file *file, void *priv,
  659. const struct v4l2_hw_freq_seek *seek)
  660. {
  661. int err;
  662. enum si476x_func func;
  663. u32 rangelow = seek->rangelow, rangehigh = seek->rangehigh;
  664. struct si476x_radio *radio = video_drvdata(file);
  665. if (file->f_flags & O_NONBLOCK)
  666. return -EAGAIN;
  667. if (seek->tuner != 0 ||
  668. seek->type != V4L2_TUNER_RADIO)
  669. return -EINVAL;
  670. si476x_core_lock(radio->core);
  671. if (!rangelow) {
  672. err = regmap_read(radio->core->regmap,
  673. SI476X_PROP_SEEK_BAND_BOTTOM,
  674. &rangelow);
  675. if (err)
  676. goto unlock;
  677. rangelow = si476x_to_v4l2(radio->core, rangelow);
  678. }
  679. if (!rangehigh) {
  680. err = regmap_read(radio->core->regmap,
  681. SI476X_PROP_SEEK_BAND_TOP,
  682. &rangehigh);
  683. if (err)
  684. goto unlock;
  685. rangehigh = si476x_to_v4l2(radio->core, rangehigh);
  686. }
  687. if (rangelow > rangehigh) {
  688. err = -EINVAL;
  689. goto unlock;
  690. }
  691. if (si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
  692. SI476X_BAND_FM)) {
  693. func = SI476X_FUNC_FM_RECEIVER;
  694. } else if (si476x_core_has_am(radio->core) &&
  695. si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
  696. SI476X_BAND_AM)) {
  697. func = SI476X_FUNC_AM_RECEIVER;
  698. } else {
  699. err = -EINVAL;
  700. goto unlock;
  701. }
  702. err = si476x_radio_change_func(radio, func);
  703. if (err < 0)
  704. goto unlock;
  705. if (seek->rangehigh) {
  706. err = regmap_write(radio->core->regmap,
  707. SI476X_PROP_SEEK_BAND_TOP,
  708. v4l2_to_si476x(radio->core,
  709. seek->rangehigh));
  710. if (err)
  711. goto unlock;
  712. }
  713. if (seek->rangelow) {
  714. err = regmap_write(radio->core->regmap,
  715. SI476X_PROP_SEEK_BAND_BOTTOM,
  716. v4l2_to_si476x(radio->core,
  717. seek->rangelow));
  718. if (err)
  719. goto unlock;
  720. }
  721. if (seek->spacing) {
  722. err = regmap_write(radio->core->regmap,
  723. SI476X_PROP_SEEK_FREQUENCY_SPACING,
  724. v4l2_to_si476x(radio->core,
  725. seek->spacing));
  726. if (err)
  727. goto unlock;
  728. }
  729. err = radio->ops->seek_start(radio->core,
  730. seek->seek_upward,
  731. seek->wrap_around);
  732. unlock:
  733. si476x_core_unlock(radio->core);
  734. return err;
  735. }
  736. static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  737. {
  738. int retval;
  739. struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
  740. si476x_core_lock(radio->core);
  741. switch (ctrl->id) {
  742. case V4L2_CID_SI476X_INTERCHIP_LINK:
  743. if (si476x_core_has_diversity(radio->core)) {
  744. if (radio->ops->phase_diversity) {
  745. retval = radio->ops->phase_div_status(radio->core);
  746. if (retval < 0)
  747. break;
  748. ctrl->val = !!SI476X_PHDIV_STATUS_LINK_LOCKED(retval);
  749. retval = 0;
  750. break;
  751. } else {
  752. retval = -ENOTTY;
  753. break;
  754. }
  755. }
  756. retval = -EINVAL;
  757. break;
  758. default:
  759. retval = -EINVAL;
  760. break;
  761. }
  762. si476x_core_unlock(radio->core);
  763. return retval;
  764. }
  765. static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl)
  766. {
  767. int retval;
  768. enum si476x_phase_diversity_mode mode;
  769. struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
  770. si476x_core_lock(radio->core);
  771. switch (ctrl->id) {
  772. case V4L2_CID_SI476X_HARMONICS_COUNT:
  773. retval = regmap_update_bits(radio->core->regmap,
  774. SI476X_PROP_AUDIO_PWR_LINE_FILTER,
  775. SI476X_PROP_PWR_HARMONICS_MASK,
  776. ctrl->val);
  777. break;
  778. case V4L2_CID_POWER_LINE_FREQUENCY:
  779. switch (ctrl->val) {
  780. case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
  781. retval = regmap_update_bits(radio->core->regmap,
  782. SI476X_PROP_AUDIO_PWR_LINE_FILTER,
  783. SI476X_PROP_PWR_ENABLE_MASK,
  784. 0);
  785. break;
  786. case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
  787. retval = regmap_update_bits(radio->core->regmap,
  788. SI476X_PROP_AUDIO_PWR_LINE_FILTER,
  789. SI476X_PROP_PWR_GRID_MASK,
  790. SI476X_PROP_PWR_GRID_50HZ);
  791. break;
  792. case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
  793. retval = regmap_update_bits(radio->core->regmap,
  794. SI476X_PROP_AUDIO_PWR_LINE_FILTER,
  795. SI476X_PROP_PWR_GRID_MASK,
  796. SI476X_PROP_PWR_GRID_60HZ);
  797. break;
  798. default:
  799. retval = -EINVAL;
  800. break;
  801. }
  802. break;
  803. case V4L2_CID_SI476X_RSSI_THRESHOLD:
  804. retval = regmap_write(radio->core->regmap,
  805. SI476X_PROP_VALID_RSSI_THRESHOLD,
  806. ctrl->val);
  807. break;
  808. case V4L2_CID_SI476X_SNR_THRESHOLD:
  809. retval = regmap_write(radio->core->regmap,
  810. SI476X_PROP_VALID_SNR_THRESHOLD,
  811. ctrl->val);
  812. break;
  813. case V4L2_CID_SI476X_MAX_TUNE_ERROR:
  814. retval = regmap_write(radio->core->regmap,
  815. SI476X_PROP_VALID_MAX_TUNE_ERROR,
  816. ctrl->val);
  817. break;
  818. case V4L2_CID_RDS_RECEPTION:
  819. /*
  820. * It looks like RDS related properties are
  821. * inaccesable when tuner is in AM mode, so cache the
  822. * changes
  823. */
  824. if (si476x_core_is_in_am_receiver_mode(radio->core))
  825. regcache_cache_only(radio->core->regmap, true);
  826. if (ctrl->val) {
  827. retval = regmap_write(radio->core->regmap,
  828. SI476X_PROP_FM_RDS_INTERRUPT_FIFO_COUNT,
  829. radio->core->rds_fifo_depth);
  830. if (retval < 0)
  831. break;
  832. if (radio->core->client->irq) {
  833. retval = regmap_write(radio->core->regmap,
  834. SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
  835. SI476X_RDSRECV);
  836. if (retval < 0)
  837. break;
  838. }
  839. /* Drain RDS FIFO before enabling RDS processing */
  840. retval = si476x_core_cmd_fm_rds_status(radio->core,
  841. false,
  842. true,
  843. true,
  844. NULL);
  845. if (retval < 0)
  846. break;
  847. retval = regmap_update_bits(radio->core->regmap,
  848. SI476X_PROP_FM_RDS_CONFIG,
  849. SI476X_PROP_RDSEN_MASK,
  850. SI476X_PROP_RDSEN);
  851. } else {
  852. retval = regmap_update_bits(radio->core->regmap,
  853. SI476X_PROP_FM_RDS_CONFIG,
  854. SI476X_PROP_RDSEN_MASK,
  855. !SI476X_PROP_RDSEN);
  856. }
  857. if (si476x_core_is_in_am_receiver_mode(radio->core))
  858. regcache_cache_only(radio->core->regmap, false);
  859. break;
  860. case V4L2_CID_TUNE_DEEMPHASIS:
  861. retval = regmap_write(radio->core->regmap,
  862. SI476X_PROP_AUDIO_DEEMPHASIS,
  863. ctrl->val);
  864. break;
  865. case V4L2_CID_SI476X_DIVERSITY_MODE:
  866. mode = si476x_phase_diversity_idx_to_mode(ctrl->val);
  867. if (mode == radio->core->diversity_mode) {
  868. retval = 0;
  869. break;
  870. }
  871. if (si476x_core_is_in_am_receiver_mode(radio->core)) {
  872. /*
  873. * Diversity cannot be configured while tuner
  874. * is in AM mode so save the changes and carry on.
  875. */
  876. radio->core->diversity_mode = mode;
  877. retval = 0;
  878. } else {
  879. retval = radio->ops->phase_diversity(radio->core, mode);
  880. if (!retval)
  881. radio->core->diversity_mode = mode;
  882. }
  883. break;
  884. default:
  885. retval = -EINVAL;
  886. break;
  887. }
  888. si476x_core_unlock(radio->core);
  889. return retval;
  890. }
  891. #ifdef CONFIG_VIDEO_ADV_DEBUG
  892. static int si476x_radio_g_register(struct file *file, void *fh,
  893. struct v4l2_dbg_register *reg)
  894. {
  895. int err;
  896. unsigned int value;
  897. struct si476x_radio *radio = video_drvdata(file);
  898. si476x_core_lock(radio->core);
  899. reg->size = 2;
  900. err = regmap_read(radio->core->regmap,
  901. (unsigned int)reg->reg, &value);
  902. reg->val = value;
  903. si476x_core_unlock(radio->core);
  904. return err;
  905. }
  906. static int si476x_radio_s_register(struct file *file, void *fh,
  907. const struct v4l2_dbg_register *reg)
  908. {
  909. int err;
  910. struct si476x_radio *radio = video_drvdata(file);
  911. si476x_core_lock(radio->core);
  912. err = regmap_write(radio->core->regmap,
  913. (unsigned int)reg->reg,
  914. (unsigned int)reg->val);
  915. si476x_core_unlock(radio->core);
  916. return err;
  917. }
  918. #endif
  919. static int si476x_radio_fops_open(struct file *file)
  920. {
  921. struct si476x_radio *radio = video_drvdata(file);
  922. int err;
  923. err = v4l2_fh_open(file);
  924. if (err)
  925. return err;
  926. if (v4l2_fh_is_singular_file(file)) {
  927. si476x_core_lock(radio->core);
  928. err = si476x_core_set_power_state(radio->core,
  929. SI476X_POWER_UP_FULL);
  930. if (err < 0)
  931. goto done;
  932. err = si476x_radio_do_post_powerup_init(radio,
  933. radio->core->power_up_parameters.func);
  934. if (err < 0)
  935. goto power_down;
  936. err = si476x_radio_pretune(radio,
  937. radio->core->power_up_parameters.func);
  938. if (err < 0)
  939. goto power_down;
  940. si476x_core_unlock(radio->core);
  941. /*Must be done after si476x_core_unlock to prevent a deadlock*/
  942. v4l2_ctrl_handler_setup(&radio->ctrl_handler);
  943. }
  944. return err;
  945. power_down:
  946. si476x_core_set_power_state(radio->core,
  947. SI476X_POWER_DOWN);
  948. done:
  949. si476x_core_unlock(radio->core);
  950. v4l2_fh_release(file);
  951. return err;
  952. }
  953. static int si476x_radio_fops_release(struct file *file)
  954. {
  955. int err;
  956. struct si476x_radio *radio = video_drvdata(file);
  957. if (v4l2_fh_is_singular_file(file) &&
  958. atomic_read(&radio->core->is_alive))
  959. si476x_core_set_power_state(radio->core,
  960. SI476X_POWER_DOWN);
  961. err = v4l2_fh_release(file);
  962. return err;
  963. }
  964. static ssize_t si476x_radio_fops_read(struct file *file, char __user *buf,
  965. size_t count, loff_t *ppos)
  966. {
  967. ssize_t rval;
  968. size_t fifo_len;
  969. unsigned int copied;
  970. struct si476x_radio *radio = video_drvdata(file);
  971. /* block if no new data available */
  972. if (kfifo_is_empty(&radio->core->rds_fifo)) {
  973. if (file->f_flags & O_NONBLOCK)
  974. return -EWOULDBLOCK;
  975. rval = wait_event_interruptible(radio->core->rds_read_queue,
  976. (!kfifo_is_empty(&radio->core->rds_fifo) ||
  977. !atomic_read(&radio->core->is_alive)));
  978. if (rval < 0)
  979. return -EINTR;
  980. if (!atomic_read(&radio->core->is_alive))
  981. return -ENODEV;
  982. }
  983. fifo_len = kfifo_len(&radio->core->rds_fifo);
  984. if (kfifo_to_user(&radio->core->rds_fifo, buf,
  985. min(fifo_len, count),
  986. &copied) != 0) {
  987. dev_warn(&radio->videodev.dev,
  988. "Error during FIFO to userspace copy\n");
  989. rval = -EIO;
  990. } else {
  991. rval = (ssize_t)copied;
  992. }
  993. return rval;
  994. }
  995. static __poll_t si476x_radio_fops_poll(struct file *file,
  996. struct poll_table_struct *pts)
  997. {
  998. struct si476x_radio *radio = video_drvdata(file);
  999. __poll_t req_events = poll_requested_events(pts);
  1000. __poll_t err = v4l2_ctrl_poll(file, pts);
  1001. if (req_events & (EPOLLIN | EPOLLRDNORM)) {
  1002. if (atomic_read(&radio->core->is_alive))
  1003. poll_wait(file, &radio->core->rds_read_queue, pts);
  1004. if (!atomic_read(&radio->core->is_alive))
  1005. err = EPOLLHUP;
  1006. if (!kfifo_is_empty(&radio->core->rds_fifo))
  1007. err = EPOLLIN | EPOLLRDNORM;
  1008. }
  1009. return err;
  1010. }
  1011. static const struct v4l2_file_operations si476x_fops = {
  1012. .owner = THIS_MODULE,
  1013. .read = si476x_radio_fops_read,
  1014. .poll = si476x_radio_fops_poll,
  1015. .unlocked_ioctl = video_ioctl2,
  1016. .open = si476x_radio_fops_open,
  1017. .release = si476x_radio_fops_release,
  1018. };
  1019. static const struct v4l2_ioctl_ops si4761_ioctl_ops = {
  1020. .vidioc_querycap = si476x_radio_querycap,
  1021. .vidioc_g_tuner = si476x_radio_g_tuner,
  1022. .vidioc_s_tuner = si476x_radio_s_tuner,
  1023. .vidioc_g_frequency = si476x_radio_g_frequency,
  1024. .vidioc_s_frequency = si476x_radio_s_frequency,
  1025. .vidioc_s_hw_freq_seek = si476x_radio_s_hw_freq_seek,
  1026. .vidioc_enum_freq_bands = si476x_radio_enum_freq_bands,
  1027. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1028. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1029. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1030. .vidioc_g_register = si476x_radio_g_register,
  1031. .vidioc_s_register = si476x_radio_s_register,
  1032. #endif
  1033. };
  1034. static const struct video_device si476x_viddev_template = {
  1035. .fops = &si476x_fops,
  1036. .name = DRIVER_NAME,
  1037. .release = video_device_release_empty,
  1038. };
  1039. static ssize_t si476x_radio_read_acf_blob(struct file *file,
  1040. char __user *user_buf,
  1041. size_t count, loff_t *ppos)
  1042. {
  1043. int err;
  1044. struct si476x_radio *radio = file->private_data;
  1045. struct si476x_acf_status_report report;
  1046. si476x_core_lock(radio->core);
  1047. if (radio->ops->acf_status)
  1048. err = radio->ops->acf_status(radio->core, &report);
  1049. else
  1050. err = -ENOENT;
  1051. si476x_core_unlock(radio->core);
  1052. if (err < 0)
  1053. return err;
  1054. return simple_read_from_buffer(user_buf, count, ppos, &report,
  1055. sizeof(report));
  1056. }
  1057. static const struct file_operations radio_acf_fops = {
  1058. .open = simple_open,
  1059. .llseek = default_llseek,
  1060. .read = si476x_radio_read_acf_blob,
  1061. };
  1062. static ssize_t si476x_radio_read_rds_blckcnt_blob(struct file *file,
  1063. char __user *user_buf,
  1064. size_t count, loff_t *ppos)
  1065. {
  1066. int err;
  1067. struct si476x_radio *radio = file->private_data;
  1068. struct si476x_rds_blockcount_report report;
  1069. si476x_core_lock(radio->core);
  1070. if (radio->ops->rds_blckcnt)
  1071. err = radio->ops->rds_blckcnt(radio->core, true,
  1072. &report);
  1073. else
  1074. err = -ENOENT;
  1075. si476x_core_unlock(radio->core);
  1076. if (err < 0)
  1077. return err;
  1078. return simple_read_from_buffer(user_buf, count, ppos, &report,
  1079. sizeof(report));
  1080. }
  1081. static const struct file_operations radio_rds_blckcnt_fops = {
  1082. .open = simple_open,
  1083. .llseek = default_llseek,
  1084. .read = si476x_radio_read_rds_blckcnt_blob,
  1085. };
  1086. static ssize_t si476x_radio_read_agc_blob(struct file *file,
  1087. char __user *user_buf,
  1088. size_t count, loff_t *ppos)
  1089. {
  1090. int err;
  1091. struct si476x_radio *radio = file->private_data;
  1092. struct si476x_agc_status_report report;
  1093. si476x_core_lock(radio->core);
  1094. if (radio->ops->rds_blckcnt)
  1095. err = radio->ops->agc_status(radio->core, &report);
  1096. else
  1097. err = -ENOENT;
  1098. si476x_core_unlock(radio->core);
  1099. if (err < 0)
  1100. return err;
  1101. return simple_read_from_buffer(user_buf, count, ppos, &report,
  1102. sizeof(report));
  1103. }
  1104. static const struct file_operations radio_agc_fops = {
  1105. .open = simple_open,
  1106. .llseek = default_llseek,
  1107. .read = si476x_radio_read_agc_blob,
  1108. };
  1109. static ssize_t si476x_radio_read_rsq_blob(struct file *file,
  1110. char __user *user_buf,
  1111. size_t count, loff_t *ppos)
  1112. {
  1113. int err;
  1114. struct si476x_radio *radio = file->private_data;
  1115. struct si476x_rsq_status_report report;
  1116. struct si476x_rsq_status_args args = {
  1117. .primary = false,
  1118. .rsqack = false,
  1119. .attune = false,
  1120. .cancel = false,
  1121. .stcack = false,
  1122. };
  1123. si476x_core_lock(radio->core);
  1124. if (radio->ops->rds_blckcnt)
  1125. err = radio->ops->rsq_status(radio->core, &args, &report);
  1126. else
  1127. err = -ENOENT;
  1128. si476x_core_unlock(radio->core);
  1129. if (err < 0)
  1130. return err;
  1131. return simple_read_from_buffer(user_buf, count, ppos, &report,
  1132. sizeof(report));
  1133. }
  1134. static const struct file_operations radio_rsq_fops = {
  1135. .open = simple_open,
  1136. .llseek = default_llseek,
  1137. .read = si476x_radio_read_rsq_blob,
  1138. };
  1139. static ssize_t si476x_radio_read_rsq_primary_blob(struct file *file,
  1140. char __user *user_buf,
  1141. size_t count, loff_t *ppos)
  1142. {
  1143. int err;
  1144. struct si476x_radio *radio = file->private_data;
  1145. struct si476x_rsq_status_report report;
  1146. struct si476x_rsq_status_args args = {
  1147. .primary = true,
  1148. .rsqack = false,
  1149. .attune = false,
  1150. .cancel = false,
  1151. .stcack = false,
  1152. };
  1153. si476x_core_lock(radio->core);
  1154. if (radio->ops->rds_blckcnt)
  1155. err = radio->ops->rsq_status(radio->core, &args, &report);
  1156. else
  1157. err = -ENOENT;
  1158. si476x_core_unlock(radio->core);
  1159. if (err < 0)
  1160. return err;
  1161. return simple_read_from_buffer(user_buf, count, ppos, &report,
  1162. sizeof(report));
  1163. }
  1164. static const struct file_operations radio_rsq_primary_fops = {
  1165. .open = simple_open,
  1166. .llseek = default_llseek,
  1167. .read = si476x_radio_read_rsq_primary_blob,
  1168. };
  1169. static int si476x_radio_init_debugfs(struct si476x_radio *radio)
  1170. {
  1171. struct dentry *dentry;
  1172. int ret;
  1173. dentry = debugfs_create_dir(dev_name(radio->v4l2dev.dev), NULL);
  1174. if (IS_ERR(dentry)) {
  1175. ret = PTR_ERR(dentry);
  1176. goto exit;
  1177. }
  1178. radio->debugfs = dentry;
  1179. dentry = debugfs_create_file("acf", S_IRUGO,
  1180. radio->debugfs, radio, &radio_acf_fops);
  1181. if (IS_ERR(dentry)) {
  1182. ret = PTR_ERR(dentry);
  1183. goto cleanup;
  1184. }
  1185. dentry = debugfs_create_file("rds_blckcnt", S_IRUGO,
  1186. radio->debugfs, radio,
  1187. &radio_rds_blckcnt_fops);
  1188. if (IS_ERR(dentry)) {
  1189. ret = PTR_ERR(dentry);
  1190. goto cleanup;
  1191. }
  1192. dentry = debugfs_create_file("agc", S_IRUGO,
  1193. radio->debugfs, radio, &radio_agc_fops);
  1194. if (IS_ERR(dentry)) {
  1195. ret = PTR_ERR(dentry);
  1196. goto cleanup;
  1197. }
  1198. dentry = debugfs_create_file("rsq", S_IRUGO,
  1199. radio->debugfs, radio, &radio_rsq_fops);
  1200. if (IS_ERR(dentry)) {
  1201. ret = PTR_ERR(dentry);
  1202. goto cleanup;
  1203. }
  1204. dentry = debugfs_create_file("rsq_primary", S_IRUGO,
  1205. radio->debugfs, radio,
  1206. &radio_rsq_primary_fops);
  1207. if (IS_ERR(dentry)) {
  1208. ret = PTR_ERR(dentry);
  1209. goto cleanup;
  1210. }
  1211. return 0;
  1212. cleanup:
  1213. debugfs_remove_recursive(radio->debugfs);
  1214. exit:
  1215. return ret;
  1216. }
  1217. static int si476x_radio_add_new_custom(struct si476x_radio *radio,
  1218. enum si476x_ctrl_idx idx)
  1219. {
  1220. int rval;
  1221. struct v4l2_ctrl *ctrl;
  1222. ctrl = v4l2_ctrl_new_custom(&radio->ctrl_handler,
  1223. &si476x_ctrls[idx],
  1224. NULL);
  1225. rval = radio->ctrl_handler.error;
  1226. if (ctrl == NULL && rval)
  1227. dev_err(radio->v4l2dev.dev,
  1228. "Could not initialize '%s' control %d\n",
  1229. si476x_ctrls[idx].name, rval);
  1230. return rval;
  1231. }
  1232. static int si476x_radio_probe(struct platform_device *pdev)
  1233. {
  1234. int rval;
  1235. struct si476x_radio *radio;
  1236. struct v4l2_ctrl *ctrl;
  1237. static atomic_t instance = ATOMIC_INIT(0);
  1238. radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
  1239. if (!radio)
  1240. return -ENOMEM;
  1241. radio->core = i2c_mfd_cell_to_core(&pdev->dev);
  1242. v4l2_device_set_name(&radio->v4l2dev, DRIVER_NAME, &instance);
  1243. rval = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
  1244. if (rval) {
  1245. dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
  1246. return rval;
  1247. }
  1248. memcpy(&radio->videodev, &si476x_viddev_template,
  1249. sizeof(struct video_device));
  1250. radio->videodev.v4l2_dev = &radio->v4l2dev;
  1251. radio->videodev.ioctl_ops = &si4761_ioctl_ops;
  1252. video_set_drvdata(&radio->videodev, radio);
  1253. platform_set_drvdata(pdev, radio);
  1254. radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
  1255. v4l2_ctrl_handler_init(&radio->ctrl_handler,
  1256. 1 + ARRAY_SIZE(si476x_ctrls));
  1257. if (si476x_core_has_am(radio->core)) {
  1258. ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
  1259. &si476x_ctrl_ops,
  1260. V4L2_CID_POWER_LINE_FREQUENCY,
  1261. V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  1262. 0, 0);
  1263. rval = radio->ctrl_handler.error;
  1264. if (ctrl == NULL && rval) {
  1265. dev_err(&pdev->dev, "Could not initialize V4L2_CID_POWER_LINE_FREQUENCY control %d\n",
  1266. rval);
  1267. goto exit;
  1268. }
  1269. rval = si476x_radio_add_new_custom(radio,
  1270. SI476X_IDX_HARMONICS_COUNT);
  1271. if (rval < 0)
  1272. goto exit;
  1273. }
  1274. rval = si476x_radio_add_new_custom(radio, SI476X_IDX_RSSI_THRESHOLD);
  1275. if (rval < 0)
  1276. goto exit;
  1277. rval = si476x_radio_add_new_custom(radio, SI476X_IDX_SNR_THRESHOLD);
  1278. if (rval < 0)
  1279. goto exit;
  1280. rval = si476x_radio_add_new_custom(radio, SI476X_IDX_MAX_TUNE_ERROR);
  1281. if (rval < 0)
  1282. goto exit;
  1283. ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
  1284. &si476x_ctrl_ops,
  1285. V4L2_CID_TUNE_DEEMPHASIS,
  1286. V4L2_DEEMPHASIS_75_uS, 0, 0);
  1287. rval = radio->ctrl_handler.error;
  1288. if (ctrl == NULL && rval) {
  1289. dev_err(&pdev->dev, "Could not initialize V4L2_CID_TUNE_DEEMPHASIS control %d\n",
  1290. rval);
  1291. goto exit;
  1292. }
  1293. ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &si476x_ctrl_ops,
  1294. V4L2_CID_RDS_RECEPTION,
  1295. 0, 1, 1, 1);
  1296. rval = radio->ctrl_handler.error;
  1297. if (ctrl == NULL && rval) {
  1298. dev_err(&pdev->dev, "Could not initialize V4L2_CID_RDS_RECEPTION control %d\n",
  1299. rval);
  1300. goto exit;
  1301. }
  1302. if (si476x_core_has_diversity(radio->core)) {
  1303. si476x_ctrls[SI476X_IDX_DIVERSITY_MODE].def =
  1304. si476x_phase_diversity_mode_to_idx(radio->core->diversity_mode);
  1305. rval = si476x_radio_add_new_custom(radio, SI476X_IDX_DIVERSITY_MODE);
  1306. if (rval < 0)
  1307. goto exit;
  1308. rval = si476x_radio_add_new_custom(radio, SI476X_IDX_INTERCHIP_LINK);
  1309. if (rval < 0)
  1310. goto exit;
  1311. }
  1312. /* register video device */
  1313. rval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, -1);
  1314. if (rval < 0) {
  1315. dev_err(&pdev->dev, "Could not register video device\n");
  1316. goto exit;
  1317. }
  1318. rval = si476x_radio_init_debugfs(radio);
  1319. if (rval < 0) {
  1320. dev_err(&pdev->dev, "Could not creat debugfs interface\n");
  1321. goto exit;
  1322. }
  1323. return 0;
  1324. exit:
  1325. v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
  1326. return rval;
  1327. }
  1328. static int si476x_radio_remove(struct platform_device *pdev)
  1329. {
  1330. struct si476x_radio *radio = platform_get_drvdata(pdev);
  1331. v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
  1332. video_unregister_device(&radio->videodev);
  1333. v4l2_device_unregister(&radio->v4l2dev);
  1334. debugfs_remove_recursive(radio->debugfs);
  1335. return 0;
  1336. }
  1337. MODULE_ALIAS("platform:si476x-radio");
  1338. static struct platform_driver si476x_radio_driver = {
  1339. .driver = {
  1340. .name = DRIVER_NAME,
  1341. },
  1342. .probe = si476x_radio_probe,
  1343. .remove = si476x_radio_remove,
  1344. };
  1345. module_platform_driver(si476x_radio_driver);
  1346. MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
  1347. MODULE_DESCRIPTION("Driver for Si4761/64/68 AM/FM Radio MFD Cell");
  1348. MODULE_LICENSE("GPL");