Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * include/media/si476x-core.h -- Common definitions for si476x core
4 * device
5 *
6 * Copyright (C) 2012 Innovative Converged Devices(ICD)
7 * Copyright (C) 2013 Andrey Smirnov
8 *
9 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
10 */
11
12#ifndef SI476X_CORE_H
13#define SI476X_CORE_H
14
15#include <linux/kfifo.h>
16#include <linux/atomic.h>
17#include <linux/i2c.h>
18#include <linux/regmap.h>
19#include <linux/mutex.h>
20#include <linux/mfd/core.h>
21#include <linux/videodev2.h>
22#include <linux/regulator/consumer.h>
23
24#include <linux/mfd/si476x-platform.h>
25#include <linux/mfd/si476x-reports.h>
26
27/* Command Timeouts */
28#define SI476X_DEFAULT_TIMEOUT 100000
29#define SI476X_TIMEOUT_TUNE 700000
30#define SI476X_TIMEOUT_POWER_UP 330000
31#define SI476X_STATUS_POLL_US 0
32
33/* -------------------- si476x-i2c.c ----------------------- */
34
35enum si476x_freq_supported_chips {
36 SI476X_CHIP_SI4761 = 1,
37 SI476X_CHIP_SI4764,
38 SI476X_CHIP_SI4768,
39};
40
41enum si476x_part_revisions {
42 SI476X_REVISION_A10 = 0,
43 SI476X_REVISION_A20 = 1,
44 SI476X_REVISION_A30 = 2,
45};
46
47enum si476x_mfd_cells {
48 SI476X_RADIO_CELL = 0,
49 SI476X_CODEC_CELL,
50 SI476X_MFD_CELLS,
51};
52
53/**
54 * enum si476x_power_state - possible power state of the si476x
55 * device.
56 *
57 * @SI476X_POWER_DOWN: In this state all regulators are turned off
58 * and the reset line is pulled low. The device is completely
59 * inactive.
60 * @SI476X_POWER_UP_FULL: In this state all the power regulators are
61 * turned on, reset line pulled high, IRQ line is enabled(polling is
62 * active for polling use scenario) and device is turned on with
63 * POWER_UP command. The device is ready to be used.
64 * @SI476X_POWER_INCONSISTENT: This state indicates that previous
65 * power down was inconsistent, meaning some of the regulators were
66 * not turned down and thus use of the device, without power-cycling
67 * is impossible.
68 */
69enum si476x_power_state {
70 SI476X_POWER_DOWN = 0,
71 SI476X_POWER_UP_FULL = 1,
72 SI476X_POWER_INCONSISTENT = 2,
73};
74
75/**
76 * struct si476x_core - internal data structure representing the
77 * underlying "core" device which all the MFD cell-devices use.
78 *
79 * @client: Actual I2C client used to transfer commands to the chip.
80 * @regmap: Regmap for accessing the device registers
81 * @chip_id: Last digit of the chip model(E.g. "1" for SI4761)
82 * @cells: MFD cell devices created by this driver.
83 * @cmd_lock: Mutex used to serialize all the requests to the core
84 * device. This filed should not be used directly. Instead
85 * si476x_core_lock()/si476x_core_unlock() should be used to get
86 * exclusive access to the "core" device.
87 * @users: Active users counter(Used by the radio cell)
88 * @rds_read_queue: Wait queue used to wait for RDS data.
89 * @rds_fifo: FIFO in which all the RDS data received from the chip is
90 * placed.
91 * @rds_fifo_drainer: Worker that drains on-chip RDS FIFO.
92 * @rds_drainer_is_working: Flag used for launching only one instance
93 * of the @rds_fifo_drainer.
94 * @rds_drainer_status_lock: Lock used to guard access to the
95 * @rds_drainer_is_working variable.
96 * @command: Wait queue for wainting on the command comapletion.
97 * @cts: Clear To Send flag set upon receiving first status with CTS
98 * set.
99 * @tuning: Wait queue used for wainting for tune/seek comand
100 * completion.
101 * @stc: Similar to @cts, but for the STC bit of the status value.
102 * @power_up_parameters: Parameters used as argument for POWER_UP
103 * command when the device is started.
104 * @power_state: Current power state of the device.
105 * @supplies: Structure containing handles to all power supplies used
106 * by the device (NULL ones are ignored).
107 * @gpio_reset: GPIO pin connectet to the RSTB pin of the chip.
108 * @pinmux: Chip's configurable pins configuration.
109 * @diversity_mode: Chips role when functioning in diversity mode.
110 * @is_alive: Chip is initialized and active.
111 * @status_monitor: Polling worker used in polling use case scenarion
112 * (when IRQ is not avalible).
113 * @revision: Chip's running firmware revision number(Used for correct
114 * command set support).
115 * @rds_fifo_depth: RDS FIFO size: 20 for IRQ mode or 5 for polling mode.
116 */
117
118struct si476x_core {
119 struct i2c_client *client;
120 struct regmap *regmap;
121 int chip_id;
122 struct mfd_cell cells[SI476X_MFD_CELLS];
123
124 struct mutex cmd_lock; /* for serializing fm radio operations */
125 atomic_t users;
126
127 wait_queue_head_t rds_read_queue;
128 struct kfifo rds_fifo;
129 struct work_struct rds_fifo_drainer;
130 bool rds_drainer_is_working;
131 struct mutex rds_drainer_status_lock;
132
133 wait_queue_head_t command;
134 atomic_t cts;
135
136 wait_queue_head_t tuning;
137 atomic_t stc;
138
139 struct si476x_power_up_args power_up_parameters;
140
141 enum si476x_power_state power_state;
142
143 struct regulator_bulk_data supplies[4];
144
145 int gpio_reset;
146
147 struct si476x_pinmux pinmux;
148 enum si476x_phase_diversity_mode diversity_mode;
149
150 atomic_t is_alive;
151
152 struct delayed_work status_monitor;
153#define SI476X_WORK_TO_CORE(w) container_of(to_delayed_work(w), \
154 struct si476x_core, \
155 status_monitor)
156
157 int revision;
158
159 int rds_fifo_depth;
160};
161
162static inline struct si476x_core *i2c_mfd_cell_to_core(struct device *dev)
163{
164 struct i2c_client *client = to_i2c_client(dev->parent);
165 return i2c_get_clientdata(client);
166}
167
168
169/**
170 * si476x_core_lock() - lock the core device to get an exclusive access
171 * to it.
172 * @core: Core device structure
173 */
174static inline void si476x_core_lock(struct si476x_core *core)
175{
176 mutex_lock(&core->cmd_lock);
177}
178
179/**
180 * si476x_core_unlock() - unlock the core device to relinquish an
181 * exclusive access to it.
182 * @core: Core device structure
183 */
184static inline void si476x_core_unlock(struct si476x_core *core)
185{
186 mutex_unlock(&core->cmd_lock);
187}
188
189/* *_TUNE_FREQ family of commands accept frequency in multiples of
190 10kHz */
191static inline u16 hz_to_si476x(struct si476x_core *core, int freq)
192{
193 u16 result;
194
195 switch (core->power_up_parameters.func) {
196 default:
197 case SI476X_FUNC_FM_RECEIVER:
198 result = freq / 10000;
199 break;
200 case SI476X_FUNC_AM_RECEIVER:
201 result = freq / 1000;
202 break;
203 }
204
205 return result;
206}
207
208static inline int si476x_to_hz(struct si476x_core *core, u16 freq)
209{
210 int result;
211
212 switch (core->power_up_parameters.func) {
213 default:
214 case SI476X_FUNC_FM_RECEIVER:
215 result = freq * 10000;
216 break;
217 case SI476X_FUNC_AM_RECEIVER:
218 result = freq * 1000;
219 break;
220 }
221
222 return result;
223}
224
225/* Since the V4L2_TUNER_CAP_LOW flag is supplied, V4L2 subsystem
226 * mesures frequency in 62.5 Hz units */
227
228static inline int hz_to_v4l2(int freq)
229{
230 return (freq * 10) / 625;
231}
232
233static inline int v4l2_to_hz(int freq)
234{
235 return (freq * 625) / 10;
236}
237
238static inline u16 v4l2_to_si476x(struct si476x_core *core, int freq)
239{
240 return hz_to_si476x(core, v4l2_to_hz(freq));
241}
242
243static inline int si476x_to_v4l2(struct si476x_core *core, u16 freq)
244{
245 return hz_to_v4l2(si476x_to_hz(core, freq));
246}
247
248
249
250/**
251 * struct si476x_func_info - structure containing result of the
252 * FUNC_INFO command.
253 *
254 * @firmware: Firmware version numbers.
255 * @firmware.major: Firmware major number.
256 * @firmware.minor[...]: Firmware minor numbers.
257 * @patch_id: Firmware patch level.
258 * @func: Mode tuner is working in.
259 */
260struct si476x_func_info {
261 struct {
262 u8 major, minor[2];
263 } firmware;
264 u16 patch_id;
265 enum si476x_func func;
266};
267
268/**
269 * struct si476x_power_down_args - structure used to pass parameters
270 * to POWER_DOWN command
271 *
272 * @xosc: true - Power down, but leav oscillator running.
273 * false - Full power down.
274 */
275struct si476x_power_down_args {
276 bool xosc;
277};
278
279/**
280 * enum si476x_tunemode - enum representing possible tune modes for
281 * the chip.
282 * @SI476X_TM_VALIDATED_NORMAL_TUNE: Unconditionally stay on the new
283 * channel after tune, tune status is valid.
284 * @SI476X_TM_INVALIDATED_FAST_TUNE: Unconditionally stay in the new
285 * channel after tune, tune status invalid.
286 * @SI476X_TM_VALIDATED_AF_TUNE: Jump back to previous channel if
287 * metric thresholds are not met.
288 * @SI476X_TM_VALIDATED_AF_CHECK: Unconditionally jump back to the
289 * previous channel.
290 */
291enum si476x_tunemode {
292 SI476X_TM_VALIDATED_NORMAL_TUNE = 0,
293 SI476X_TM_INVALIDATED_FAST_TUNE = 1,
294 SI476X_TM_VALIDATED_AF_TUNE = 2,
295 SI476X_TM_VALIDATED_AF_CHECK = 3,
296};
297
298/**
299 * enum si476x_smoothmetrics - enum containing the possible setting fo
300 * audio transitioning of the chip
301 * @SI476X_SM_INITIALIZE_AUDIO: Initialize audio state to match this
302 * new channel
303 * @SI476X_SM_TRANSITION_AUDIO: Transition audio state from previous
304 * channel values to the new values
305 */
306enum si476x_smoothmetrics {
307 SI476X_SM_INITIALIZE_AUDIO = 0,
308 SI476X_SM_TRANSITION_AUDIO = 1,
309};
310
311/**
312 * struct si476x_rds_status_report - the structure representing the
313 * response to 'FM_RD_STATUS' command
314 * @rdstpptyint: Traffic program flag(TP) and/or program type(PTY)
315 * code has changed.
316 * @rdspiint: Program identification(PI) code has changed.
317 * @rdssyncint: RDS synchronization has changed.
318 * @rdsfifoint: RDS was received and the RDS FIFO has at least
319 * 'FM_RDS_INTERRUPT_FIFO_COUNT' elements in it.
320 * @tpptyvalid: TP flag and PTY code are valid falg.
321 * @pivalid: PI code is valid flag.
322 * @rdssync: RDS is currently synchronized.
323 * @rdsfifolost: On or more RDS groups have been lost/discarded flag.
324 * @tp: Current channel's TP flag.
325 * @pty: Current channel's PTY code.
326 * @pi: Current channel's PI code.
327 * @rdsfifoused: Number of blocks remaining in the RDS FIFO (0 if empty).
328 * @ble:
329 * @rds: RDS data descriptor
330 */
331struct si476x_rds_status_report {
332 bool rdstpptyint, rdspiint, rdssyncint, rdsfifoint;
333 bool tpptyvalid, pivalid, rdssync, rdsfifolost;
334 bool tp;
335
336 u8 pty;
337 u16 pi;
338
339 u8 rdsfifoused;
340 u8 ble[4];
341
342 struct v4l2_rds_data rds[4];
343};
344
345struct si476x_rsq_status_args {
346 bool primary;
347 bool rsqack;
348 bool attune;
349 bool cancel;
350 bool stcack;
351};
352
353enum si476x_injside {
354 SI476X_INJSIDE_AUTO = 0,
355 SI476X_INJSIDE_LOW = 1,
356 SI476X_INJSIDE_HIGH = 2,
357};
358
359struct si476x_tune_freq_args {
360 bool zifsr;
361 bool hd;
362 enum si476x_injside injside;
363 int freq;
364 enum si476x_tunemode tunemode;
365 enum si476x_smoothmetrics smoothmetrics;
366 int antcap;
367};
368
369int si476x_core_stop(struct si476x_core *, bool);
370int si476x_core_start(struct si476x_core *, bool);
371int si476x_core_set_power_state(struct si476x_core *, enum si476x_power_state);
372bool si476x_core_has_am(struct si476x_core *);
373bool si476x_core_has_diversity(struct si476x_core *);
374bool si476x_core_is_a_secondary_tuner(struct si476x_core *);
375bool si476x_core_is_a_primary_tuner(struct si476x_core *);
376bool si476x_core_is_in_am_receiver_mode(struct si476x_core *core);
377bool si476x_core_is_powered_up(struct si476x_core *core);
378
379enum si476x_i2c_type {
380 SI476X_I2C_SEND,
381 SI476X_I2C_RECV
382};
383
384int si476x_core_i2c_xfer(struct si476x_core *,
385 enum si476x_i2c_type,
386 char *, int);
387
388
389/* -------------------- si476x-cmd.c ----------------------- */
390
391int si476x_core_cmd_func_info(struct si476x_core *, struct si476x_func_info *);
392int si476x_core_cmd_set_property(struct si476x_core *, u16, u16);
393int si476x_core_cmd_get_property(struct si476x_core *, u16);
394int si476x_core_cmd_dig_audio_pin_cfg(struct si476x_core *,
395 enum si476x_dclk_config,
396 enum si476x_dfs_config,
397 enum si476x_dout_config,
398 enum si476x_xout_config);
399int si476x_core_cmd_zif_pin_cfg(struct si476x_core *,
400 enum si476x_iqclk_config,
401 enum si476x_iqfs_config,
402 enum si476x_iout_config,
403 enum si476x_qout_config);
404int si476x_core_cmd_ic_link_gpo_ctl_pin_cfg(struct si476x_core *,
405 enum si476x_icin_config,
406 enum si476x_icip_config,
407 enum si476x_icon_config,
408 enum si476x_icop_config);
409int si476x_core_cmd_ana_audio_pin_cfg(struct si476x_core *,
410 enum si476x_lrout_config);
411int si476x_core_cmd_intb_pin_cfg(struct si476x_core *, enum si476x_intb_config,
412 enum si476x_a1_config);
413int si476x_core_cmd_fm_seek_start(struct si476x_core *, bool, bool);
414int si476x_core_cmd_am_seek_start(struct si476x_core *, bool, bool);
415int si476x_core_cmd_fm_rds_status(struct si476x_core *, bool, bool, bool,
416 struct si476x_rds_status_report *);
417int si476x_core_cmd_fm_rds_blockcount(struct si476x_core *, bool,
418 struct si476x_rds_blockcount_report *);
419int si476x_core_cmd_fm_tune_freq(struct si476x_core *,
420 struct si476x_tune_freq_args *);
421int si476x_core_cmd_am_tune_freq(struct si476x_core *,
422 struct si476x_tune_freq_args *);
423int si476x_core_cmd_am_rsq_status(struct si476x_core *,
424 struct si476x_rsq_status_args *,
425 struct si476x_rsq_status_report *);
426int si476x_core_cmd_fm_rsq_status(struct si476x_core *,
427 struct si476x_rsq_status_args *,
428 struct si476x_rsq_status_report *);
429int si476x_core_cmd_power_up(struct si476x_core *,
430 struct si476x_power_up_args *);
431int si476x_core_cmd_power_down(struct si476x_core *,
432 struct si476x_power_down_args *);
433int si476x_core_cmd_fm_phase_div_status(struct si476x_core *);
434int si476x_core_cmd_fm_phase_diversity(struct si476x_core *,
435 enum si476x_phase_diversity_mode);
436
437int si476x_core_cmd_fm_acf_status(struct si476x_core *,
438 struct si476x_acf_status_report *);
439int si476x_core_cmd_am_acf_status(struct si476x_core *,
440 struct si476x_acf_status_report *);
441int si476x_core_cmd_agc_status(struct si476x_core *,
442 struct si476x_agc_status_report *);
443
444enum si476x_power_grid_type {
445 SI476X_POWER_GRID_50HZ = 0,
446 SI476X_POWER_GRID_60HZ,
447};
448
449/* Properties */
450
451enum si476x_interrupt_flags {
452 SI476X_STCIEN = (1 << 0),
453 SI476X_ACFIEN = (1 << 1),
454 SI476X_RDSIEN = (1 << 2),
455 SI476X_RSQIEN = (1 << 3),
456
457 SI476X_ERRIEN = (1 << 6),
458 SI476X_CTSIEN = (1 << 7),
459
460 SI476X_STCREP = (1 << 8),
461 SI476X_ACFREP = (1 << 9),
462 SI476X_RDSREP = (1 << 10),
463 SI476X_RSQREP = (1 << 11),
464};
465
466enum si476x_rdsint_sources {
467 SI476X_RDSTPPTY = (1 << 4),
468 SI476X_RDSPI = (1 << 3),
469 SI476X_RDSSYNC = (1 << 1),
470 SI476X_RDSRECV = (1 << 0),
471};
472
473enum si476x_status_response_bits {
474 SI476X_CTS = (1 << 7),
475 SI476X_ERR = (1 << 6),
476 /* Status response for WB receiver */
477 SI476X_WB_ASQ_INT = (1 << 4),
478 SI476X_RSQ_INT = (1 << 3),
479 /* Status response for FM receiver */
480 SI476X_FM_RDS_INT = (1 << 2),
481 SI476X_ACF_INT = (1 << 1),
482 SI476X_STC_INT = (1 << 0),
483};
484
485/* -------------------- si476x-prop.c ----------------------- */
486
487enum si476x_common_receiver_properties {
488 SI476X_PROP_INT_CTL_ENABLE = 0x0000,
489 SI476X_PROP_DIGITAL_IO_INPUT_SAMPLE_RATE = 0x0200,
490 SI476X_PROP_DIGITAL_IO_INPUT_FORMAT = 0x0201,
491 SI476X_PROP_DIGITAL_IO_OUTPUT_SAMPLE_RATE = 0x0202,
492 SI476X_PROP_DIGITAL_IO_OUTPUT_FORMAT = 0x0203,
493
494 SI476X_PROP_SEEK_BAND_BOTTOM = 0x1100,
495 SI476X_PROP_SEEK_BAND_TOP = 0x1101,
496 SI476X_PROP_SEEK_FREQUENCY_SPACING = 0x1102,
497
498 SI476X_PROP_VALID_MAX_TUNE_ERROR = 0x2000,
499 SI476X_PROP_VALID_SNR_THRESHOLD = 0x2003,
500 SI476X_PROP_VALID_RSSI_THRESHOLD = 0x2004,
501};
502
503enum si476x_am_receiver_properties {
504 SI476X_PROP_AUDIO_PWR_LINE_FILTER = 0x0303,
505};
506
507enum si476x_fm_receiver_properties {
508 SI476X_PROP_AUDIO_DEEMPHASIS = 0x0302,
509
510 SI476X_PROP_FM_RDS_INTERRUPT_SOURCE = 0x4000,
511 SI476X_PROP_FM_RDS_INTERRUPT_FIFO_COUNT = 0x4001,
512 SI476X_PROP_FM_RDS_CONFIG = 0x4002,
513};
514
515enum si476x_prop_audio_pwr_line_filter_bits {
516 SI476X_PROP_PWR_HARMONICS_MASK = 0x001f,
517 SI476X_PROP_PWR_GRID_MASK = 0x0100,
518 SI476X_PROP_PWR_ENABLE_MASK = 0x0200,
519 SI476X_PROP_PWR_GRID_50HZ = 0x0000,
520 SI476X_PROP_PWR_GRID_60HZ = 0x0100,
521};
522
523enum si476x_prop_fm_rds_config_bits {
524 SI476X_PROP_RDSEN_MASK = 0x1,
525 SI476X_PROP_RDSEN = 0x1,
526};
527
528
529struct regmap *devm_regmap_init_si476x(struct si476x_core *);
530
531#endif /* SI476X_CORE_H */