radio-si476x.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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");