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