xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/max2175.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Maxim Integrated MAX2175 RF to Bits tuner driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This driver & most of the hard coded values are based on the reference
6*4882a593Smuzhiyun  * application delivered by Maxim for this device.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright (C) 2016 Maxim Integrated Products
9*4882a593Smuzhiyun  * Copyright (C) 2017 Renesas Electronics Corporation
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/clk.h>
13*4882a593Smuzhiyun #include <linux/delay.h>
14*4882a593Smuzhiyun #include <linux/errno.h>
15*4882a593Smuzhiyun #include <linux/i2c.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/math64.h>
18*4882a593Smuzhiyun #include <linux/max2175.h>
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/of.h>
21*4882a593Smuzhiyun #include <linux/regmap.h>
22*4882a593Smuzhiyun #include <linux/slab.h>
23*4882a593Smuzhiyun #include <media/v4l2-ctrls.h>
24*4882a593Smuzhiyun #include <media/v4l2-device.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include "max2175.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define DRIVER_NAME "max2175"
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define mxm_dbg(ctx, fmt, arg...) dev_dbg(&ctx->client->dev, fmt, ## arg)
31*4882a593Smuzhiyun #define mxm_err(ctx, fmt, arg...) dev_err(&ctx->client->dev, fmt, ## arg)
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /* Rx mode */
34*4882a593Smuzhiyun struct max2175_rxmode {
35*4882a593Smuzhiyun 	enum max2175_band band;		/* Associated band */
36*4882a593Smuzhiyun 	u32 freq;			/* Default freq in Hz */
37*4882a593Smuzhiyun 	u8 i2s_word_size;		/* Bit value */
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /* Register map to define preset values */
41*4882a593Smuzhiyun struct max2175_reg_map {
42*4882a593Smuzhiyun 	u8 idx;				/* Register index */
43*4882a593Smuzhiyun 	u8 val;				/* Register value */
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun static const struct max2175_rxmode eu_rx_modes[] = {
47*4882a593Smuzhiyun 	/* EU modes */
48*4882a593Smuzhiyun 	[MAX2175_EU_FM_1_2] = { MAX2175_BAND_FM, 98256000, 1 },
49*4882a593Smuzhiyun 	[MAX2175_DAB_1_2]   = { MAX2175_BAND_VHF, 182640000, 0 },
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun static const struct max2175_rxmode na_rx_modes[] = {
53*4882a593Smuzhiyun 	/* NA modes */
54*4882a593Smuzhiyun 	[MAX2175_NA_FM_1_0] = { MAX2175_BAND_FM, 98255520, 1 },
55*4882a593Smuzhiyun 	[MAX2175_NA_FM_2_0] = { MAX2175_BAND_FM, 98255520, 6 },
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun  * Preset values:
60*4882a593Smuzhiyun  * Based on Maxim MAX2175 Register Table revision: 130p10
61*4882a593Smuzhiyun  */
62*4882a593Smuzhiyun static const u8 full_fm_eu_1p0[] = {
63*4882a593Smuzhiyun 	0x15, 0x04, 0xb8, 0xe3, 0x35, 0x18, 0x7c, 0x00,
64*4882a593Smuzhiyun 	0x00, 0x7d, 0x40, 0x08, 0x70, 0x7a, 0x88, 0x91,
65*4882a593Smuzhiyun 	0x61, 0x61, 0x61, 0x61, 0x5a, 0x0f, 0x34, 0x1c,
66*4882a593Smuzhiyun 	0x14, 0x88, 0x33, 0x02, 0x00, 0x09, 0x00, 0x65,
67*4882a593Smuzhiyun 	0x9f, 0x2b, 0x80, 0x00, 0x95, 0x05, 0x2c, 0x00,
68*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
69*4882a593Smuzhiyun 	0x4a, 0x08, 0xa8, 0x0e, 0x0e, 0x2f, 0x7e, 0x00,
70*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0xab, 0x5e, 0xa9,
72*4882a593Smuzhiyun 	0xae, 0xbb, 0x57, 0x18, 0x3b, 0x03, 0x3b, 0x64,
73*4882a593Smuzhiyun 	0x40, 0x60, 0x00, 0x2a, 0xbf, 0x3f, 0xff, 0x9f,
74*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00,
75*4882a593Smuzhiyun 	0xff, 0xfc, 0xef, 0x1c, 0x40, 0x00, 0x00, 0x02,
76*4882a593Smuzhiyun 	0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
77*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0xac, 0x40, 0x00,
78*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00,
79*4882a593Smuzhiyun 	0x00, 0x47, 0x00, 0x00, 0x11, 0x3f, 0x22, 0x00,
80*4882a593Smuzhiyun 	0xf1, 0x00, 0x41, 0x03, 0xb0, 0x00, 0x00, 0x00,
81*4882a593Smuzhiyun 	0x1b,
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun static const u8 full_fm_na_1p0[] = {
85*4882a593Smuzhiyun 	0x13, 0x08, 0x8d, 0xc0, 0x35, 0x18, 0x7d, 0x3f,
86*4882a593Smuzhiyun 	0x7d, 0x75, 0x40, 0x08, 0x70, 0x7a, 0x88, 0x91,
87*4882a593Smuzhiyun 	0x61, 0x61, 0x61, 0x61, 0x5c, 0x0f, 0x34, 0x1c,
88*4882a593Smuzhiyun 	0x14, 0x88, 0x33, 0x02, 0x00, 0x01, 0x00, 0x65,
89*4882a593Smuzhiyun 	0x9f, 0x2b, 0x80, 0x00, 0x95, 0x05, 0x2c, 0x00,
90*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
91*4882a593Smuzhiyun 	0x4a, 0x08, 0xa8, 0x0e, 0x0e, 0xaf, 0x7e, 0x00,
92*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0xab, 0x5e, 0xa9,
94*4882a593Smuzhiyun 	0xae, 0xbb, 0x57, 0x18, 0x3b, 0x03, 0x3b, 0x64,
95*4882a593Smuzhiyun 	0x40, 0x60, 0x00, 0x2a, 0xbf, 0x3f, 0xff, 0x9f,
96*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00,
97*4882a593Smuzhiyun 	0xff, 0xfc, 0xef, 0x1c, 0x40, 0x00, 0x00, 0x02,
98*4882a593Smuzhiyun 	0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
99*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0xa6, 0x40, 0x00,
100*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00,
101*4882a593Smuzhiyun 	0x00, 0x35, 0x00, 0x00, 0x11, 0x3f, 0x22, 0x00,
102*4882a593Smuzhiyun 	0xf1, 0x00, 0x41, 0x03, 0xb0, 0x00, 0x00, 0x00,
103*4882a593Smuzhiyun 	0x1b,
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /* DAB1.2 settings */
107*4882a593Smuzhiyun static const struct max2175_reg_map dab12_map[] = {
108*4882a593Smuzhiyun 	{ 0x01, 0x13 }, { 0x02, 0x0d }, { 0x03, 0x15 }, { 0x04, 0x55 },
109*4882a593Smuzhiyun 	{ 0x05, 0x0a }, { 0x06, 0xa0 }, { 0x07, 0x40 }, { 0x08, 0x00 },
110*4882a593Smuzhiyun 	{ 0x09, 0x00 }, { 0x0a, 0x7d }, { 0x0b, 0x4a }, { 0x0c, 0x28 },
111*4882a593Smuzhiyun 	{ 0x0e, 0x43 }, { 0x0f, 0xb5 }, { 0x10, 0x31 }, { 0x11, 0x9e },
112*4882a593Smuzhiyun 	{ 0x12, 0x68 }, { 0x13, 0x9e }, { 0x14, 0x68 }, { 0x15, 0x58 },
113*4882a593Smuzhiyun 	{ 0x16, 0x2f }, { 0x17, 0x3f }, { 0x18, 0x40 }, { 0x1a, 0x88 },
114*4882a593Smuzhiyun 	{ 0x1b, 0xaa }, { 0x1c, 0x9a }, { 0x1d, 0x00 }, { 0x1e, 0x00 },
115*4882a593Smuzhiyun 	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x00 }, { 0x26, 0x00 },
116*4882a593Smuzhiyun 	{ 0x27, 0x00 }, { 0x32, 0x08 }, { 0x33, 0xf8 }, { 0x36, 0x2d },
117*4882a593Smuzhiyun 	{ 0x37, 0x7e }, { 0x55, 0xaf }, { 0x56, 0x3f }, { 0x57, 0xf8 },
118*4882a593Smuzhiyun 	{ 0x58, 0x99 }, { 0x76, 0x00 }, { 0x77, 0x00 }, { 0x78, 0x02 },
119*4882a593Smuzhiyun 	{ 0x79, 0x40 }, { 0x82, 0x00 }, { 0x83, 0x00 }, { 0x85, 0x00 },
120*4882a593Smuzhiyun 	{ 0x86, 0x20 },
121*4882a593Smuzhiyun };
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun /* EU FM 1.2 settings */
124*4882a593Smuzhiyun static const struct max2175_reg_map fmeu1p2_map[] = {
125*4882a593Smuzhiyun 	{ 0x01, 0x15 }, { 0x02, 0x04 }, { 0x03, 0xb8 }, { 0x04, 0xe3 },
126*4882a593Smuzhiyun 	{ 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7c }, { 0x08, 0x00 },
127*4882a593Smuzhiyun 	{ 0x09, 0x00 }, { 0x0a, 0x73 }, { 0x0b, 0x40 }, { 0x0c, 0x08 },
128*4882a593Smuzhiyun 	{ 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
129*4882a593Smuzhiyun 	{ 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5a },
130*4882a593Smuzhiyun 	{ 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
131*4882a593Smuzhiyun 	{ 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
132*4882a593Smuzhiyun 	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
133*4882a593Smuzhiyun 	{ 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0x2f },
134*4882a593Smuzhiyun 	{ 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
135*4882a593Smuzhiyun 	{ 0x58, 0x9f }, { 0x76, 0xac }, { 0x77, 0x40 }, { 0x78, 0x00 },
136*4882a593Smuzhiyun 	{ 0x79, 0x00 }, { 0x82, 0x47 }, { 0x83, 0x00 }, { 0x85, 0x11 },
137*4882a593Smuzhiyun 	{ 0x86, 0x3f },
138*4882a593Smuzhiyun };
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /* FM NA 1.0 settings */
141*4882a593Smuzhiyun static const struct max2175_reg_map fmna1p0_map[] = {
142*4882a593Smuzhiyun 	{ 0x01, 0x13 }, { 0x02, 0x08 }, { 0x03, 0x8d }, { 0x04, 0xc0 },
143*4882a593Smuzhiyun 	{ 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7d }, { 0x08, 0x3f },
144*4882a593Smuzhiyun 	{ 0x09, 0x7d }, { 0x0a, 0x75 }, { 0x0b, 0x40 }, { 0x0c, 0x08 },
145*4882a593Smuzhiyun 	{ 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
146*4882a593Smuzhiyun 	{ 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5c },
147*4882a593Smuzhiyun 	{ 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
148*4882a593Smuzhiyun 	{ 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
149*4882a593Smuzhiyun 	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
150*4882a593Smuzhiyun 	{ 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0xaf },
151*4882a593Smuzhiyun 	{ 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
152*4882a593Smuzhiyun 	{ 0x58, 0x9f }, { 0x76, 0xa6 }, { 0x77, 0x40 }, { 0x78, 0x00 },
153*4882a593Smuzhiyun 	{ 0x79, 0x00 }, { 0x82, 0x35 }, { 0x83, 0x00 }, { 0x85, 0x11 },
154*4882a593Smuzhiyun 	{ 0x86, 0x3f },
155*4882a593Smuzhiyun };
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /* FM NA 2.0 settings */
158*4882a593Smuzhiyun static const struct max2175_reg_map fmna2p0_map[] = {
159*4882a593Smuzhiyun 	{ 0x01, 0x13 }, { 0x02, 0x08 }, { 0x03, 0x8d }, { 0x04, 0xc0 },
160*4882a593Smuzhiyun 	{ 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7c }, { 0x08, 0x54 },
161*4882a593Smuzhiyun 	{ 0x09, 0xa7 }, { 0x0a, 0x55 }, { 0x0b, 0x42 }, { 0x0c, 0x48 },
162*4882a593Smuzhiyun 	{ 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
163*4882a593Smuzhiyun 	{ 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5c },
164*4882a593Smuzhiyun 	{ 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
165*4882a593Smuzhiyun 	{ 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
166*4882a593Smuzhiyun 	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
167*4882a593Smuzhiyun 	{ 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0xaf },
168*4882a593Smuzhiyun 	{ 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
169*4882a593Smuzhiyun 	{ 0x58, 0x9f }, { 0x76, 0xac }, { 0x77, 0xc0 }, { 0x78, 0x00 },
170*4882a593Smuzhiyun 	{ 0x79, 0x00 }, { 0x82, 0x6b }, { 0x83, 0x00 }, { 0x85, 0x11 },
171*4882a593Smuzhiyun 	{ 0x86, 0x3f },
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun static const u16 ch_coeff_dab1[] = {
175*4882a593Smuzhiyun 	0x001c, 0x0007, 0xffcd, 0x0056, 0xffa4, 0x0033, 0x0027, 0xff61,
176*4882a593Smuzhiyun 	0x010e, 0xfec0, 0x0106, 0xffb8, 0xff1c, 0x023c, 0xfcb2, 0x039b,
177*4882a593Smuzhiyun 	0xfd4e, 0x0055, 0x036a, 0xf7de, 0x0d21, 0xee72, 0x1499, 0x6a51,
178*4882a593Smuzhiyun };
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun static const u16 ch_coeff_fmeu[] = {
181*4882a593Smuzhiyun 	0x0000, 0xffff, 0x0001, 0x0002, 0xfffa, 0xffff, 0x0015, 0xffec,
182*4882a593Smuzhiyun 	0xffde, 0x0054, 0xfff9, 0xff52, 0x00b8, 0x00a2, 0xfe0a, 0x00af,
183*4882a593Smuzhiyun 	0x02e3, 0xfc14, 0xfe89, 0x089d, 0xfa2e, 0xf30f, 0x25be, 0x4eb6,
184*4882a593Smuzhiyun };
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun static const u16 eq_coeff_fmeu1_ra02_m6db[] = {
187*4882a593Smuzhiyun 	0x0040, 0xffc6, 0xfffa, 0x002c, 0x000d, 0xff90, 0x0037, 0x006e,
188*4882a593Smuzhiyun 	0xffc0, 0xff5b, 0x006a, 0x00f0, 0xff57, 0xfe94, 0x0112, 0x0252,
189*4882a593Smuzhiyun 	0xfe0c, 0xfc6a, 0x0385, 0x0553, 0xfa49, 0xf789, 0x0b91, 0x1a10,
190*4882a593Smuzhiyun };
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun static const u16 ch_coeff_fmna[] = {
193*4882a593Smuzhiyun 	0x0001, 0x0003, 0xfffe, 0xfff4, 0x0000, 0x001f, 0x000c, 0xffbc,
194*4882a593Smuzhiyun 	0xffd3, 0x007d, 0x0075, 0xff33, 0xff01, 0x0131, 0x01ef, 0xfe60,
195*4882a593Smuzhiyun 	0xfc7a, 0x020e, 0x0656, 0xfd94, 0xf395, 0x02ab, 0x2857, 0x3d3f,
196*4882a593Smuzhiyun };
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun static const u16 eq_coeff_fmna1_ra02_m6db[] = {
199*4882a593Smuzhiyun 	0xfff1, 0xffe1, 0xffef, 0x000e, 0x0030, 0x002f, 0xfff6, 0xffa7,
200*4882a593Smuzhiyun 	0xff9d, 0x000a, 0x00a2, 0x00b5, 0xffea, 0xfed9, 0xfec5, 0x003d,
201*4882a593Smuzhiyun 	0x0217, 0x021b, 0xff5a, 0xfc2b, 0xfcbd, 0x02c4, 0x0ac3, 0x0e85,
202*4882a593Smuzhiyun };
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun static const u8 adc_presets[2][23] = {
205*4882a593Smuzhiyun 	{
206*4882a593Smuzhiyun 		0x83, 0x00, 0xcf, 0xb4, 0x0f, 0x2c, 0x0c, 0x49,
207*4882a593Smuzhiyun 		0x00, 0x00, 0x00, 0x8c,	0x02, 0x02, 0x00, 0x04,
208*4882a593Smuzhiyun 		0xec, 0x82, 0x4b, 0xcc, 0x01, 0x88, 0x0c,
209*4882a593Smuzhiyun 	},
210*4882a593Smuzhiyun 	{
211*4882a593Smuzhiyun 		0x83, 0x00, 0xcf, 0xb4,	0x0f, 0x2c, 0x0c, 0x49,
212*4882a593Smuzhiyun 		0x00, 0x00, 0x00, 0x8c,	0x02, 0x20, 0x33, 0x8c,
213*4882a593Smuzhiyun 		0x57, 0xd7, 0x59, 0xb7,	0x65, 0x0e, 0x0c,
214*4882a593Smuzhiyun 	},
215*4882a593Smuzhiyun };
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun /* Tuner bands */
218*4882a593Smuzhiyun static const struct v4l2_frequency_band eu_bands_rf = {
219*4882a593Smuzhiyun 	.tuner = 0,
220*4882a593Smuzhiyun 	.type = V4L2_TUNER_RF,
221*4882a593Smuzhiyun 	.index = 0,
222*4882a593Smuzhiyun 	.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
223*4882a593Smuzhiyun 	.rangelow   = 65000000,
224*4882a593Smuzhiyun 	.rangehigh  = 240000000,
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun static const struct v4l2_frequency_band na_bands_rf = {
228*4882a593Smuzhiyun 	.tuner = 0,
229*4882a593Smuzhiyun 	.type = V4L2_TUNER_RF,
230*4882a593Smuzhiyun 	.index = 0,
231*4882a593Smuzhiyun 	.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
232*4882a593Smuzhiyun 	.rangelow   = 65000000,
233*4882a593Smuzhiyun 	.rangehigh  = 108000000,
234*4882a593Smuzhiyun };
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun /* Regmap settings */
237*4882a593Smuzhiyun static const struct regmap_range max2175_regmap_volatile_range[] = {
238*4882a593Smuzhiyun 	regmap_reg_range(0x30, 0x35),
239*4882a593Smuzhiyun 	regmap_reg_range(0x3a, 0x45),
240*4882a593Smuzhiyun 	regmap_reg_range(0x59, 0x5e),
241*4882a593Smuzhiyun 	regmap_reg_range(0x73, 0x75),
242*4882a593Smuzhiyun };
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun static const struct regmap_access_table max2175_volatile_regs = {
245*4882a593Smuzhiyun 	.yes_ranges = max2175_regmap_volatile_range,
246*4882a593Smuzhiyun 	.n_yes_ranges = ARRAY_SIZE(max2175_regmap_volatile_range),
247*4882a593Smuzhiyun };
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun static const struct reg_default max2175_reg_defaults[] = {
250*4882a593Smuzhiyun 	{ 0x00, 0x07},
251*4882a593Smuzhiyun };
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun static const struct regmap_config max2175_regmap_config = {
254*4882a593Smuzhiyun 	.reg_bits = 8,
255*4882a593Smuzhiyun 	.val_bits = 8,
256*4882a593Smuzhiyun 	.max_register = 0xff,
257*4882a593Smuzhiyun 	.reg_defaults = max2175_reg_defaults,
258*4882a593Smuzhiyun 	.num_reg_defaults = ARRAY_SIZE(max2175_reg_defaults),
259*4882a593Smuzhiyun 	.volatile_table = &max2175_volatile_regs,
260*4882a593Smuzhiyun 	.cache_type = REGCACHE_FLAT,
261*4882a593Smuzhiyun };
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun struct max2175 {
264*4882a593Smuzhiyun 	struct v4l2_subdev sd;		/* Sub-device */
265*4882a593Smuzhiyun 	struct i2c_client *client;	/* I2C client */
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	/* Controls */
268*4882a593Smuzhiyun 	struct v4l2_ctrl_handler ctrl_hdl;
269*4882a593Smuzhiyun 	struct v4l2_ctrl *lna_gain;	/* LNA gain value */
270*4882a593Smuzhiyun 	struct v4l2_ctrl *if_gain;	/* I/F gain value */
271*4882a593Smuzhiyun 	struct v4l2_ctrl *pll_lock;	/* PLL lock */
272*4882a593Smuzhiyun 	struct v4l2_ctrl *i2s_en;	/* I2S output enable */
273*4882a593Smuzhiyun 	struct v4l2_ctrl *hsls;		/* High-side/Low-side polarity */
274*4882a593Smuzhiyun 	struct v4l2_ctrl *rx_mode;	/* Receive mode */
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	/* Regmap */
277*4882a593Smuzhiyun 	struct regmap *regmap;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	/* Cached configuration */
280*4882a593Smuzhiyun 	u32 freq;			/* Tuned freq In Hz */
281*4882a593Smuzhiyun 	const struct max2175_rxmode *rx_modes;		/* EU or NA modes */
282*4882a593Smuzhiyun 	const struct v4l2_frequency_band *bands_rf;	/* EU or NA bands */
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	/* Device settings */
285*4882a593Smuzhiyun 	unsigned long xtal_freq;	/* Ref Oscillator freq in Hz */
286*4882a593Smuzhiyun 	u32 decim_ratio;
287*4882a593Smuzhiyun 	bool master;			/* Master/Slave */
288*4882a593Smuzhiyun 	bool am_hiz;			/* AM Hi-Z filter */
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	/* ROM values */
291*4882a593Smuzhiyun 	u8 rom_bbf_bw_am;
292*4882a593Smuzhiyun 	u8 rom_bbf_bw_fm;
293*4882a593Smuzhiyun 	u8 rom_bbf_bw_dab;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	/* Driver private variables */
296*4882a593Smuzhiyun 	bool mode_resolved;		/* Flag to sanity check settings */
297*4882a593Smuzhiyun };
298*4882a593Smuzhiyun 
max2175_from_sd(struct v4l2_subdev * sd)299*4882a593Smuzhiyun static inline struct max2175 *max2175_from_sd(struct v4l2_subdev *sd)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun 	return container_of(sd, struct max2175, sd);
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun 
max2175_from_ctrl_hdl(struct v4l2_ctrl_handler * h)304*4882a593Smuzhiyun static inline struct max2175 *max2175_from_ctrl_hdl(struct v4l2_ctrl_handler *h)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun 	return container_of(h, struct max2175, ctrl_hdl);
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun /* Get bitval of a given val */
max2175_get_bitval(u8 val,u8 msb,u8 lsb)310*4882a593Smuzhiyun static inline u8 max2175_get_bitval(u8 val, u8 msb, u8 lsb)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun 	return (val & GENMASK(msb, lsb)) >> lsb;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun /* Read/Write bit(s) on top of regmap */
max2175_read(struct max2175 * ctx,u8 idx,u8 * val)316*4882a593Smuzhiyun static int max2175_read(struct max2175 *ctx, u8 idx, u8 *val)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun 	u32 regval;
319*4882a593Smuzhiyun 	int ret;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	ret = regmap_read(ctx->regmap, idx, &regval);
322*4882a593Smuzhiyun 	if (ret)
323*4882a593Smuzhiyun 		mxm_err(ctx, "read ret(%d): idx 0x%02x\n", ret, idx);
324*4882a593Smuzhiyun 	else
325*4882a593Smuzhiyun 		*val = regval;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	return ret;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun 
max2175_write(struct max2175 * ctx,u8 idx,u8 val)330*4882a593Smuzhiyun static int max2175_write(struct max2175 *ctx, u8 idx, u8 val)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	int ret;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	ret = regmap_write(ctx->regmap, idx, val);
335*4882a593Smuzhiyun 	if (ret)
336*4882a593Smuzhiyun 		mxm_err(ctx, "write ret(%d): idx 0x%02x val 0x%02x\n",
337*4882a593Smuzhiyun 			ret, idx, val);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	return ret;
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun 
max2175_read_bits(struct max2175 * ctx,u8 idx,u8 msb,u8 lsb)342*4882a593Smuzhiyun static u8 max2175_read_bits(struct max2175 *ctx, u8 idx, u8 msb, u8 lsb)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun 	u8 val;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	if (max2175_read(ctx, idx, &val))
347*4882a593Smuzhiyun 		return 0;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	return max2175_get_bitval(val, msb, lsb);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
max2175_write_bits(struct max2175 * ctx,u8 idx,u8 msb,u8 lsb,u8 newval)352*4882a593Smuzhiyun static int max2175_write_bits(struct max2175 *ctx, u8 idx,
353*4882a593Smuzhiyun 			     u8 msb, u8 lsb, u8 newval)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	int ret = regmap_update_bits(ctx->regmap, idx, GENMASK(msb, lsb),
356*4882a593Smuzhiyun 				     newval << lsb);
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	if (ret)
359*4882a593Smuzhiyun 		mxm_err(ctx, "wbits ret(%d): idx 0x%02x\n", ret, idx);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	return ret;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun 
max2175_write_bit(struct max2175 * ctx,u8 idx,u8 bit,u8 newval)364*4882a593Smuzhiyun static int max2175_write_bit(struct max2175 *ctx, u8 idx, u8 bit, u8 newval)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun 	return max2175_write_bits(ctx, idx, bit, bit, newval);
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun /* Checks expected pattern every msec until timeout */
max2175_poll_timeout(struct max2175 * ctx,u8 idx,u8 msb,u8 lsb,u8 exp_bitval,u32 timeout_us)370*4882a593Smuzhiyun static int max2175_poll_timeout(struct max2175 *ctx, u8 idx, u8 msb, u8 lsb,
371*4882a593Smuzhiyun 				u8 exp_bitval, u32 timeout_us)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	unsigned int val;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	return regmap_read_poll_timeout(ctx->regmap, idx, val,
376*4882a593Smuzhiyun 			(max2175_get_bitval(val, msb, lsb) == exp_bitval),
377*4882a593Smuzhiyun 			1000, timeout_us);
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
max2175_poll_csm_ready(struct max2175 * ctx)380*4882a593Smuzhiyun static int max2175_poll_csm_ready(struct max2175 *ctx)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	int ret;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	ret = max2175_poll_timeout(ctx, 69, 1, 1, 0, 50000);
385*4882a593Smuzhiyun 	if (ret)
386*4882a593Smuzhiyun 		mxm_err(ctx, "csm not ready\n");
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	return ret;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun #define MAX2175_IS_BAND_AM(ctx)		\
392*4882a593Smuzhiyun 	(max2175_read_bits(ctx, 5, 1, 0) == MAX2175_BAND_AM)
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun #define MAX2175_IS_BAND_VHF(ctx)	\
395*4882a593Smuzhiyun 	(max2175_read_bits(ctx, 5, 1, 0) == MAX2175_BAND_VHF)
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun #define MAX2175_IS_FM_MODE(ctx)		\
398*4882a593Smuzhiyun 	(max2175_read_bits(ctx, 12, 5, 4) == 0)
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun #define MAX2175_IS_FMHD_MODE(ctx)	\
401*4882a593Smuzhiyun 	(max2175_read_bits(ctx, 12, 5, 4) == 1)
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun #define MAX2175_IS_DAB_MODE(ctx)	\
404*4882a593Smuzhiyun 	(max2175_read_bits(ctx, 12, 5, 4) == 2)
405*4882a593Smuzhiyun 
max2175_band_from_freq(u32 freq)406*4882a593Smuzhiyun static int max2175_band_from_freq(u32 freq)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun 	if (freq >= 144000 && freq <= 26100000)
409*4882a593Smuzhiyun 		return MAX2175_BAND_AM;
410*4882a593Smuzhiyun 	else if (freq >= 65000000 && freq <= 108000000)
411*4882a593Smuzhiyun 		return MAX2175_BAND_FM;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	return MAX2175_BAND_VHF;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun 
max2175_i2s_enable(struct max2175 * ctx,bool enable)416*4882a593Smuzhiyun static void max2175_i2s_enable(struct max2175 *ctx, bool enable)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun 	if (enable)
419*4882a593Smuzhiyun 		/* Stuff bits are zeroed */
420*4882a593Smuzhiyun 		max2175_write_bits(ctx, 104, 3, 0, 2);
421*4882a593Smuzhiyun 	else
422*4882a593Smuzhiyun 		/* Keep SCK alive */
423*4882a593Smuzhiyun 		max2175_write_bits(ctx, 104, 3, 0, 9);
424*4882a593Smuzhiyun 	mxm_dbg(ctx, "i2s %sabled\n", enable ? "en" : "dis");
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun 
max2175_set_filter_coeffs(struct max2175 * ctx,u8 m_sel,u8 bank,const u16 * coeffs)427*4882a593Smuzhiyun static void max2175_set_filter_coeffs(struct max2175 *ctx, u8 m_sel,
428*4882a593Smuzhiyun 				      u8 bank, const u16 *coeffs)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	unsigned int i;
431*4882a593Smuzhiyun 	u8 coeff_addr, upper_address = 24;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	mxm_dbg(ctx, "set_filter_coeffs: m_sel %d bank %d\n", m_sel, bank);
434*4882a593Smuzhiyun 	max2175_write_bits(ctx, 114, 5, 4, m_sel);
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	if (m_sel == 2)
437*4882a593Smuzhiyun 		upper_address = 12;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	for (i = 0; i < upper_address; i++) {
440*4882a593Smuzhiyun 		coeff_addr = i + bank * 24;
441*4882a593Smuzhiyun 		max2175_write(ctx, 115, coeffs[i] >> 8);
442*4882a593Smuzhiyun 		max2175_write(ctx, 116, coeffs[i]);
443*4882a593Smuzhiyun 		max2175_write(ctx, 117, coeff_addr | 1 << 7);
444*4882a593Smuzhiyun 	}
445*4882a593Smuzhiyun 	max2175_write_bit(ctx, 117, 7, 0);
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun 
max2175_load_fmeu_1p2(struct max2175 * ctx)448*4882a593Smuzhiyun static void max2175_load_fmeu_1p2(struct max2175 *ctx)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun 	unsigned int i;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(fmeu1p2_map); i++)
453*4882a593Smuzhiyun 		max2175_write(ctx, fmeu1p2_map[i].idx, fmeu1p2_map[i].val);
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	ctx->decim_ratio = 36;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	/* Load the Channel Filter Coefficients into channel filter bank #2 */
458*4882a593Smuzhiyun 	max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0, ch_coeff_fmeu);
459*4882a593Smuzhiyun 	max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
460*4882a593Smuzhiyun 				  eq_coeff_fmeu1_ra02_m6db);
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun 
max2175_load_dab_1p2(struct max2175 * ctx)463*4882a593Smuzhiyun static void max2175_load_dab_1p2(struct max2175 *ctx)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun 	unsigned int i;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(dab12_map); i++)
468*4882a593Smuzhiyun 		max2175_write(ctx, dab12_map[i].idx, dab12_map[i].val);
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	ctx->decim_ratio = 1;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	/* Load the Channel Filter Coefficients into channel filter bank #2 */
473*4882a593Smuzhiyun 	max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 2, ch_coeff_dab1);
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun 
max2175_load_fmna_1p0(struct max2175 * ctx)476*4882a593Smuzhiyun static void max2175_load_fmna_1p0(struct max2175 *ctx)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	unsigned int i;
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(fmna1p0_map); i++)
481*4882a593Smuzhiyun 		max2175_write(ctx, fmna1p0_map[i].idx, fmna1p0_map[i].val);
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun 
max2175_load_fmna_2p0(struct max2175 * ctx)484*4882a593Smuzhiyun static void max2175_load_fmna_2p0(struct max2175 *ctx)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun 	unsigned int i;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(fmna2p0_map); i++)
489*4882a593Smuzhiyun 		max2175_write(ctx, fmna2p0_map[i].idx, fmna2p0_map[i].val);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
max2175_set_bbfilter(struct max2175 * ctx)492*4882a593Smuzhiyun static void max2175_set_bbfilter(struct max2175 *ctx)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun 	if (MAX2175_IS_BAND_AM(ctx)) {
495*4882a593Smuzhiyun 		max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_am);
496*4882a593Smuzhiyun 		mxm_dbg(ctx, "set_bbfilter AM: rom %d\n", ctx->rom_bbf_bw_am);
497*4882a593Smuzhiyun 	} else if (MAX2175_IS_DAB_MODE(ctx)) {
498*4882a593Smuzhiyun 		max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_dab);
499*4882a593Smuzhiyun 		mxm_dbg(ctx, "set_bbfilter DAB: rom %d\n", ctx->rom_bbf_bw_dab);
500*4882a593Smuzhiyun 	} else {
501*4882a593Smuzhiyun 		max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_fm);
502*4882a593Smuzhiyun 		mxm_dbg(ctx, "set_bbfilter FM: rom %d\n", ctx->rom_bbf_bw_fm);
503*4882a593Smuzhiyun 	}
504*4882a593Smuzhiyun }
505*4882a593Smuzhiyun 
max2175_set_csm_mode(struct max2175 * ctx,enum max2175_csm_mode new_mode)506*4882a593Smuzhiyun static int max2175_set_csm_mode(struct max2175 *ctx,
507*4882a593Smuzhiyun 			  enum max2175_csm_mode new_mode)
508*4882a593Smuzhiyun {
509*4882a593Smuzhiyun 	int ret = max2175_poll_csm_ready(ctx);
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	if (ret)
512*4882a593Smuzhiyun 		return ret;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	max2175_write_bits(ctx, 0, 2, 0, new_mode);
515*4882a593Smuzhiyun 	mxm_dbg(ctx, "set csm new mode %d\n", new_mode);
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	/* Wait for a fixed settle down time depending on new mode */
518*4882a593Smuzhiyun 	switch (new_mode) {
519*4882a593Smuzhiyun 	case MAX2175_PRESET_TUNE:
520*4882a593Smuzhiyun 		usleep_range(51100, 51500);	/* 51.1ms */
521*4882a593Smuzhiyun 		break;
522*4882a593Smuzhiyun 	/*
523*4882a593Smuzhiyun 	 * Other mode switches need different sleep values depending on band &
524*4882a593Smuzhiyun 	 * mode
525*4882a593Smuzhiyun 	 */
526*4882a593Smuzhiyun 	default:
527*4882a593Smuzhiyun 		break;
528*4882a593Smuzhiyun 	}
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	return max2175_poll_csm_ready(ctx);
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun 
max2175_csm_action(struct max2175 * ctx,enum max2175_csm_mode action)533*4882a593Smuzhiyun static int max2175_csm_action(struct max2175 *ctx,
534*4882a593Smuzhiyun 			      enum max2175_csm_mode action)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun 	int ret;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	mxm_dbg(ctx, "csm_action: %d\n", action);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	/* Other actions can be added in future when needed */
541*4882a593Smuzhiyun 	ret = max2175_set_csm_mode(ctx, MAX2175_LOAD_TO_BUFFER);
542*4882a593Smuzhiyun 	if (ret)
543*4882a593Smuzhiyun 		return ret;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	return max2175_set_csm_mode(ctx, MAX2175_PRESET_TUNE);
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun 
max2175_set_lo_freq(struct max2175 * ctx,u32 lo_freq)548*4882a593Smuzhiyun static int max2175_set_lo_freq(struct max2175 *ctx, u32 lo_freq)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun 	u8 lo_mult, loband_bits = 0, vcodiv_bits = 0;
551*4882a593Smuzhiyun 	u32 int_desired, frac_desired;
552*4882a593Smuzhiyun 	enum max2175_band band;
553*4882a593Smuzhiyun 	int ret;
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	band = max2175_read_bits(ctx, 5, 1, 0);
556*4882a593Smuzhiyun 	switch (band) {
557*4882a593Smuzhiyun 	case MAX2175_BAND_AM:
558*4882a593Smuzhiyun 		lo_mult = 16;
559*4882a593Smuzhiyun 		break;
560*4882a593Smuzhiyun 	case MAX2175_BAND_FM:
561*4882a593Smuzhiyun 		if (lo_freq <= 74700000) {
562*4882a593Smuzhiyun 			lo_mult = 16;
563*4882a593Smuzhiyun 		} else if (lo_freq > 74700000 && lo_freq <= 110000000) {
564*4882a593Smuzhiyun 			loband_bits = 1;
565*4882a593Smuzhiyun 			lo_mult = 8;
566*4882a593Smuzhiyun 		} else {
567*4882a593Smuzhiyun 			loband_bits = 1;
568*4882a593Smuzhiyun 			vcodiv_bits = 3;
569*4882a593Smuzhiyun 			lo_mult = 8;
570*4882a593Smuzhiyun 		}
571*4882a593Smuzhiyun 		break;
572*4882a593Smuzhiyun 	case MAX2175_BAND_VHF:
573*4882a593Smuzhiyun 		if (lo_freq <= 210000000)
574*4882a593Smuzhiyun 			vcodiv_bits = 2;
575*4882a593Smuzhiyun 		else
576*4882a593Smuzhiyun 			vcodiv_bits = 1;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 		loband_bits = 2;
579*4882a593Smuzhiyun 		lo_mult = 4;
580*4882a593Smuzhiyun 		break;
581*4882a593Smuzhiyun 	default:
582*4882a593Smuzhiyun 		loband_bits = 3;
583*4882a593Smuzhiyun 		vcodiv_bits = 2;
584*4882a593Smuzhiyun 		lo_mult = 2;
585*4882a593Smuzhiyun 		break;
586*4882a593Smuzhiyun 	}
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	if (band == MAX2175_BAND_L)
589*4882a593Smuzhiyun 		lo_freq /= lo_mult;
590*4882a593Smuzhiyun 	else
591*4882a593Smuzhiyun 		lo_freq *= lo_mult;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	int_desired = lo_freq / ctx->xtal_freq;
594*4882a593Smuzhiyun 	frac_desired = div64_ul((u64)(lo_freq % ctx->xtal_freq) << 20,
595*4882a593Smuzhiyun 				ctx->xtal_freq);
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	/* Check CSM is not busy */
598*4882a593Smuzhiyun 	ret = max2175_poll_csm_ready(ctx);
599*4882a593Smuzhiyun 	if (ret)
600*4882a593Smuzhiyun 		return ret;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	mxm_dbg(ctx, "lo_mult %u int %u  frac %u\n",
603*4882a593Smuzhiyun 		lo_mult, int_desired, frac_desired);
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	/* Write the calculated values to the appropriate registers */
606*4882a593Smuzhiyun 	max2175_write(ctx, 1, int_desired);
607*4882a593Smuzhiyun 	max2175_write_bits(ctx, 2, 3, 0, (frac_desired >> 16) & 0xf);
608*4882a593Smuzhiyun 	max2175_write(ctx, 3, frac_desired >> 8);
609*4882a593Smuzhiyun 	max2175_write(ctx, 4, frac_desired);
610*4882a593Smuzhiyun 	max2175_write_bits(ctx, 5, 3, 2, loband_bits);
611*4882a593Smuzhiyun 	max2175_write_bits(ctx, 6, 7, 6, vcodiv_bits);
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	return ret;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun /*
617*4882a593Smuzhiyun  * Helper similar to DIV_ROUND_CLOSEST but an inline function that accepts s64
618*4882a593Smuzhiyun  * dividend and s32 divisor
619*4882a593Smuzhiyun  */
max2175_round_closest(s64 dividend,s32 divisor)620*4882a593Smuzhiyun static inline s64 max2175_round_closest(s64 dividend, s32 divisor)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun 	if ((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0))
623*4882a593Smuzhiyun 		return div_s64(dividend + divisor / 2, divisor);
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	return div_s64(dividend - divisor / 2, divisor);
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun 
max2175_set_nco_freq(struct max2175 * ctx,s32 nco_freq)628*4882a593Smuzhiyun static int max2175_set_nco_freq(struct max2175 *ctx, s32 nco_freq)
629*4882a593Smuzhiyun {
630*4882a593Smuzhiyun 	s32 clock_rate = ctx->xtal_freq / ctx->decim_ratio;
631*4882a593Smuzhiyun 	u32 nco_reg, abs_nco_freq = abs(nco_freq);
632*4882a593Smuzhiyun 	s64 nco_val_desired;
633*4882a593Smuzhiyun 	int ret;
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	if (abs_nco_freq < clock_rate / 2) {
636*4882a593Smuzhiyun 		nco_val_desired = 2 * nco_freq;
637*4882a593Smuzhiyun 	} else {
638*4882a593Smuzhiyun 		nco_val_desired = 2LL * (clock_rate - abs_nco_freq);
639*4882a593Smuzhiyun 		if (nco_freq < 0)
640*4882a593Smuzhiyun 			nco_val_desired = -nco_val_desired;
641*4882a593Smuzhiyun 	}
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	nco_reg = max2175_round_closest(nco_val_desired << 20, clock_rate);
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	if (nco_freq < 0)
646*4882a593Smuzhiyun 		nco_reg += 0x200000;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	/* Check CSM is not busy */
649*4882a593Smuzhiyun 	ret = max2175_poll_csm_ready(ctx);
650*4882a593Smuzhiyun 	if (ret)
651*4882a593Smuzhiyun 		return ret;
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	mxm_dbg(ctx, "freq %d desired %lld reg %u\n",
654*4882a593Smuzhiyun 		nco_freq, nco_val_desired, nco_reg);
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	/* Write the calculated values to the appropriate registers */
657*4882a593Smuzhiyun 	max2175_write_bits(ctx, 7, 4, 0, (nco_reg >> 16) & 0x1f);
658*4882a593Smuzhiyun 	max2175_write(ctx, 8, nco_reg >> 8);
659*4882a593Smuzhiyun 	max2175_write(ctx, 9, nco_reg);
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	return ret;
662*4882a593Smuzhiyun }
663*4882a593Smuzhiyun 
max2175_set_rf_freq_non_am_bands(struct max2175 * ctx,u64 freq,u32 lo_pos)664*4882a593Smuzhiyun static int max2175_set_rf_freq_non_am_bands(struct max2175 *ctx, u64 freq,
665*4882a593Smuzhiyun 					    u32 lo_pos)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun 	s64 adj_freq, low_if_freq;
668*4882a593Smuzhiyun 	int ret;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	mxm_dbg(ctx, "rf_freq: non AM bands\n");
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	if (MAX2175_IS_FM_MODE(ctx))
673*4882a593Smuzhiyun 		low_if_freq = 128000;
674*4882a593Smuzhiyun 	else if (MAX2175_IS_FMHD_MODE(ctx))
675*4882a593Smuzhiyun 		low_if_freq = 228000;
676*4882a593Smuzhiyun 	else
677*4882a593Smuzhiyun 		return max2175_set_lo_freq(ctx, freq);
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	if (MAX2175_IS_BAND_VHF(ctx) == (lo_pos == MAX2175_LO_ABOVE_DESIRED))
680*4882a593Smuzhiyun 		adj_freq = freq + low_if_freq;
681*4882a593Smuzhiyun 	else
682*4882a593Smuzhiyun 		adj_freq = freq - low_if_freq;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	ret = max2175_set_lo_freq(ctx, adj_freq);
685*4882a593Smuzhiyun 	if (ret)
686*4882a593Smuzhiyun 		return ret;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	return max2175_set_nco_freq(ctx, -low_if_freq);
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun 
max2175_set_rf_freq(struct max2175 * ctx,u64 freq,u32 lo_pos)691*4882a593Smuzhiyun static int max2175_set_rf_freq(struct max2175 *ctx, u64 freq, u32 lo_pos)
692*4882a593Smuzhiyun {
693*4882a593Smuzhiyun 	int ret;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	if (MAX2175_IS_BAND_AM(ctx))
696*4882a593Smuzhiyun 		ret = max2175_set_nco_freq(ctx, freq);
697*4882a593Smuzhiyun 	else
698*4882a593Smuzhiyun 		ret = max2175_set_rf_freq_non_am_bands(ctx, freq, lo_pos);
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	mxm_dbg(ctx, "set_rf_freq: ret %d freq %llu\n", ret, freq);
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	return ret;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun 
max2175_tune_rf_freq(struct max2175 * ctx,u64 freq,u32 hsls)705*4882a593Smuzhiyun static int max2175_tune_rf_freq(struct max2175 *ctx, u64 freq, u32 hsls)
706*4882a593Smuzhiyun {
707*4882a593Smuzhiyun 	int ret;
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	ret = max2175_set_rf_freq(ctx, freq, hsls);
710*4882a593Smuzhiyun 	if (ret)
711*4882a593Smuzhiyun 		return ret;
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	ret = max2175_csm_action(ctx, MAX2175_BUFFER_PLUS_PRESET_TUNE);
714*4882a593Smuzhiyun 	if (ret)
715*4882a593Smuzhiyun 		return ret;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	mxm_dbg(ctx, "tune_rf_freq: old %u new %llu\n", ctx->freq, freq);
718*4882a593Smuzhiyun 	ctx->freq = freq;
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	return ret;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun 
max2175_set_hsls(struct max2175 * ctx,u32 lo_pos)723*4882a593Smuzhiyun static void max2175_set_hsls(struct max2175 *ctx, u32 lo_pos)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun 	mxm_dbg(ctx, "set_hsls: lo_pos %u\n", lo_pos);
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	if ((lo_pos == MAX2175_LO_BELOW_DESIRED) == MAX2175_IS_BAND_VHF(ctx))
728*4882a593Smuzhiyun 		max2175_write_bit(ctx, 5, 4, 1);
729*4882a593Smuzhiyun 	else
730*4882a593Smuzhiyun 		max2175_write_bit(ctx, 5, 4, 0);
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun 
max2175_set_eu_rx_mode(struct max2175 * ctx,u32 rx_mode)733*4882a593Smuzhiyun static void max2175_set_eu_rx_mode(struct max2175 *ctx, u32 rx_mode)
734*4882a593Smuzhiyun {
735*4882a593Smuzhiyun 	switch (rx_mode) {
736*4882a593Smuzhiyun 	case MAX2175_EU_FM_1_2:
737*4882a593Smuzhiyun 		max2175_load_fmeu_1p2(ctx);
738*4882a593Smuzhiyun 		break;
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	case MAX2175_DAB_1_2:
741*4882a593Smuzhiyun 		max2175_load_dab_1p2(ctx);
742*4882a593Smuzhiyun 		break;
743*4882a593Smuzhiyun 	}
744*4882a593Smuzhiyun 	/* Master is the default setting */
745*4882a593Smuzhiyun 	if (!ctx->master)
746*4882a593Smuzhiyun 		max2175_write_bit(ctx, 30, 7, 1);
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun 
max2175_set_na_rx_mode(struct max2175 * ctx,u32 rx_mode)749*4882a593Smuzhiyun static void max2175_set_na_rx_mode(struct max2175 *ctx, u32 rx_mode)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun 	switch (rx_mode) {
752*4882a593Smuzhiyun 	case MAX2175_NA_FM_1_0:
753*4882a593Smuzhiyun 		max2175_load_fmna_1p0(ctx);
754*4882a593Smuzhiyun 		break;
755*4882a593Smuzhiyun 	case MAX2175_NA_FM_2_0:
756*4882a593Smuzhiyun 		max2175_load_fmna_2p0(ctx);
757*4882a593Smuzhiyun 		break;
758*4882a593Smuzhiyun 	}
759*4882a593Smuzhiyun 	/* Master is the default setting */
760*4882a593Smuzhiyun 	if (!ctx->master)
761*4882a593Smuzhiyun 		max2175_write_bit(ctx, 30, 7, 1);
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	ctx->decim_ratio = 27;
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	/* Load the Channel Filter Coefficients into channel filter bank #2 */
766*4882a593Smuzhiyun 	max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0, ch_coeff_fmna);
767*4882a593Smuzhiyun 	max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
768*4882a593Smuzhiyun 				  eq_coeff_fmna1_ra02_m6db);
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun 
max2175_set_rx_mode(struct max2175 * ctx,u32 rx_mode)771*4882a593Smuzhiyun static int max2175_set_rx_mode(struct max2175 *ctx, u32 rx_mode)
772*4882a593Smuzhiyun {
773*4882a593Smuzhiyun 	mxm_dbg(ctx, "set_rx_mode: %u am_hiz %u\n", rx_mode, ctx->am_hiz);
774*4882a593Smuzhiyun 	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ)
775*4882a593Smuzhiyun 		max2175_set_eu_rx_mode(ctx, rx_mode);
776*4882a593Smuzhiyun 	else
777*4882a593Smuzhiyun 		max2175_set_na_rx_mode(ctx, rx_mode);
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	if (ctx->am_hiz) {
780*4882a593Smuzhiyun 		mxm_dbg(ctx, "setting AM HiZ related config\n");
781*4882a593Smuzhiyun 		max2175_write_bit(ctx, 50, 5, 1);
782*4882a593Smuzhiyun 		max2175_write_bit(ctx, 90, 7, 1);
783*4882a593Smuzhiyun 		max2175_write_bits(ctx, 73, 1, 0, 2);
784*4882a593Smuzhiyun 		max2175_write_bits(ctx, 80, 5, 0, 33);
785*4882a593Smuzhiyun 	}
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	/* Load BB filter trim values saved in ROM */
788*4882a593Smuzhiyun 	max2175_set_bbfilter(ctx);
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	/* Set HSLS */
791*4882a593Smuzhiyun 	max2175_set_hsls(ctx, ctx->hsls->cur.val);
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	/* Use i2s enable settings */
794*4882a593Smuzhiyun 	max2175_i2s_enable(ctx, ctx->i2s_en->cur.val);
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	ctx->mode_resolved = true;
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	return 0;
799*4882a593Smuzhiyun }
800*4882a593Smuzhiyun 
max2175_rx_mode_from_freq(struct max2175 * ctx,u32 freq,u32 * mode)801*4882a593Smuzhiyun static int max2175_rx_mode_from_freq(struct max2175 *ctx, u32 freq, u32 *mode)
802*4882a593Smuzhiyun {
803*4882a593Smuzhiyun 	unsigned int i;
804*4882a593Smuzhiyun 	int band = max2175_band_from_freq(freq);
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	/* Pick the first match always */
807*4882a593Smuzhiyun 	for (i = 0; i <= ctx->rx_mode->maximum; i++) {
808*4882a593Smuzhiyun 		if (ctx->rx_modes[i].band == band) {
809*4882a593Smuzhiyun 			*mode = i;
810*4882a593Smuzhiyun 			mxm_dbg(ctx, "rx_mode_from_freq: freq %u mode %d\n",
811*4882a593Smuzhiyun 				freq, *mode);
812*4882a593Smuzhiyun 			return 0;
813*4882a593Smuzhiyun 		}
814*4882a593Smuzhiyun 	}
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	return -EINVAL;
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
max2175_freq_rx_mode_valid(struct max2175 * ctx,u32 mode,u32 freq)819*4882a593Smuzhiyun static bool max2175_freq_rx_mode_valid(struct max2175 *ctx,
820*4882a593Smuzhiyun 					 u32 mode, u32 freq)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun 	int band = max2175_band_from_freq(freq);
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	return (ctx->rx_modes[mode].band == band);
825*4882a593Smuzhiyun }
826*4882a593Smuzhiyun 
max2175_load_adc_presets(struct max2175 * ctx)827*4882a593Smuzhiyun static void max2175_load_adc_presets(struct max2175 *ctx)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun 	unsigned int i, j;
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(adc_presets); i++)
832*4882a593Smuzhiyun 		for (j = 0; j < ARRAY_SIZE(adc_presets[0]); j++)
833*4882a593Smuzhiyun 			max2175_write(ctx, 146 + j + i * 55, adc_presets[i][j]);
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun 
max2175_init_power_manager(struct max2175 * ctx)836*4882a593Smuzhiyun static int max2175_init_power_manager(struct max2175 *ctx)
837*4882a593Smuzhiyun {
838*4882a593Smuzhiyun 	int ret;
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	/* Execute on-chip power-up/calibration */
841*4882a593Smuzhiyun 	max2175_write_bit(ctx, 99, 2, 0);
842*4882a593Smuzhiyun 	usleep_range(1000, 1500);
843*4882a593Smuzhiyun 	max2175_write_bit(ctx, 99, 2, 1);
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	/* Wait for the power manager to finish. */
846*4882a593Smuzhiyun 	ret = max2175_poll_timeout(ctx, 69, 7, 7, 1, 50000);
847*4882a593Smuzhiyun 	if (ret)
848*4882a593Smuzhiyun 		mxm_err(ctx, "init pm failed\n");
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	return ret;
851*4882a593Smuzhiyun }
852*4882a593Smuzhiyun 
max2175_recalibrate_adc(struct max2175 * ctx)853*4882a593Smuzhiyun static int max2175_recalibrate_adc(struct max2175 *ctx)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun 	int ret;
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	/* ADC Re-calibration */
858*4882a593Smuzhiyun 	max2175_write(ctx, 150, 0xff);
859*4882a593Smuzhiyun 	max2175_write(ctx, 205, 0xff);
860*4882a593Smuzhiyun 	max2175_write(ctx, 147, 0x20);
861*4882a593Smuzhiyun 	max2175_write(ctx, 147, 0x00);
862*4882a593Smuzhiyun 	max2175_write(ctx, 202, 0x20);
863*4882a593Smuzhiyun 	max2175_write(ctx, 202, 0x00);
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	ret = max2175_poll_timeout(ctx, 69, 4, 3, 3, 50000);
866*4882a593Smuzhiyun 	if (ret)
867*4882a593Smuzhiyun 		mxm_err(ctx, "adc recalibration failed\n");
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	return ret;
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun 
max2175_read_rom(struct max2175 * ctx,u8 row)872*4882a593Smuzhiyun static u8 max2175_read_rom(struct max2175 *ctx, u8 row)
873*4882a593Smuzhiyun {
874*4882a593Smuzhiyun 	u8 data = 0;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	max2175_write_bit(ctx, 56, 4, 0);
877*4882a593Smuzhiyun 	max2175_write_bits(ctx, 56, 3, 0, row);
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 	usleep_range(2000, 2500);
880*4882a593Smuzhiyun 	max2175_read(ctx, 58, &data);
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	max2175_write_bits(ctx, 56, 3, 0, 0);
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	mxm_dbg(ctx, "read_rom: row %d data 0x%02x\n", row, data);
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	return data;
887*4882a593Smuzhiyun }
888*4882a593Smuzhiyun 
max2175_load_from_rom(struct max2175 * ctx)889*4882a593Smuzhiyun static void max2175_load_from_rom(struct max2175 *ctx)
890*4882a593Smuzhiyun {
891*4882a593Smuzhiyun 	u8 data = 0;
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	data = max2175_read_rom(ctx, 0);
894*4882a593Smuzhiyun 	ctx->rom_bbf_bw_am = data & 0x0f;
895*4882a593Smuzhiyun 	max2175_write_bits(ctx, 81, 3, 0, data >> 4);
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	data = max2175_read_rom(ctx, 1);
898*4882a593Smuzhiyun 	ctx->rom_bbf_bw_fm = data & 0x0f;
899*4882a593Smuzhiyun 	ctx->rom_bbf_bw_dab = data >> 4;
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	data = max2175_read_rom(ctx, 2);
902*4882a593Smuzhiyun 	max2175_write_bits(ctx, 82, 4, 0, data & 0x1f);
903*4882a593Smuzhiyun 	max2175_write_bits(ctx, 82, 7, 5, data >> 5);
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	data = max2175_read_rom(ctx, 3);
906*4882a593Smuzhiyun 	if (ctx->am_hiz) {
907*4882a593Smuzhiyun 		data &= 0x0f;
908*4882a593Smuzhiyun 		data |= (max2175_read_rom(ctx, 7) & 0x40) >> 2;
909*4882a593Smuzhiyun 		if (!data)
910*4882a593Smuzhiyun 			data |= 2;
911*4882a593Smuzhiyun 	} else {
912*4882a593Smuzhiyun 		data = (data & 0xf0) >> 4;
913*4882a593Smuzhiyun 		data |= (max2175_read_rom(ctx, 7) & 0x80) >> 3;
914*4882a593Smuzhiyun 		if (!data)
915*4882a593Smuzhiyun 			data |= 30;
916*4882a593Smuzhiyun 	}
917*4882a593Smuzhiyun 	max2175_write_bits(ctx, 80, 5, 0, data + 31);
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 	data = max2175_read_rom(ctx, 6);
920*4882a593Smuzhiyun 	max2175_write_bits(ctx, 81, 7, 6, data >> 6);
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun 
max2175_load_full_fm_eu_1p0(struct max2175 * ctx)923*4882a593Smuzhiyun static void max2175_load_full_fm_eu_1p0(struct max2175 *ctx)
924*4882a593Smuzhiyun {
925*4882a593Smuzhiyun 	unsigned int i;
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(full_fm_eu_1p0); i++)
928*4882a593Smuzhiyun 		max2175_write(ctx, i + 1, full_fm_eu_1p0[i]);
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	usleep_range(5000, 5500);
931*4882a593Smuzhiyun 	ctx->decim_ratio = 36;
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun 
max2175_load_full_fm_na_1p0(struct max2175 * ctx)934*4882a593Smuzhiyun static void max2175_load_full_fm_na_1p0(struct max2175 *ctx)
935*4882a593Smuzhiyun {
936*4882a593Smuzhiyun 	unsigned int i;
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(full_fm_na_1p0); i++)
939*4882a593Smuzhiyun 		max2175_write(ctx, i + 1, full_fm_na_1p0[i]);
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	usleep_range(5000, 5500);
942*4882a593Smuzhiyun 	ctx->decim_ratio = 27;
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun 
max2175_core_init(struct max2175 * ctx,u32 refout_bits)945*4882a593Smuzhiyun static int max2175_core_init(struct max2175 *ctx, u32 refout_bits)
946*4882a593Smuzhiyun {
947*4882a593Smuzhiyun 	int ret;
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	/* MAX2175 uses 36.864MHz clock for EU & 40.154MHz for NA region */
950*4882a593Smuzhiyun 	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ)
951*4882a593Smuzhiyun 		max2175_load_full_fm_eu_1p0(ctx);
952*4882a593Smuzhiyun 	else
953*4882a593Smuzhiyun 		max2175_load_full_fm_na_1p0(ctx);
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 	/* The default settings assume master */
956*4882a593Smuzhiyun 	if (!ctx->master)
957*4882a593Smuzhiyun 		max2175_write_bit(ctx, 30, 7, 1);
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 	mxm_dbg(ctx, "refout_bits %u\n", refout_bits);
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	/* Set REFOUT */
962*4882a593Smuzhiyun 	max2175_write_bits(ctx, 56, 7, 5, refout_bits);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	/* ADC Reset */
965*4882a593Smuzhiyun 	max2175_write_bit(ctx, 99, 1, 0);
966*4882a593Smuzhiyun 	usleep_range(1000, 1500);
967*4882a593Smuzhiyun 	max2175_write_bit(ctx, 99, 1, 1);
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 	/* Load ADC preset values */
970*4882a593Smuzhiyun 	max2175_load_adc_presets(ctx);
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun 	/* Initialize the power management state machine */
973*4882a593Smuzhiyun 	ret = max2175_init_power_manager(ctx);
974*4882a593Smuzhiyun 	if (ret)
975*4882a593Smuzhiyun 		return ret;
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	/* Recalibrate ADC */
978*4882a593Smuzhiyun 	ret = max2175_recalibrate_adc(ctx);
979*4882a593Smuzhiyun 	if (ret)
980*4882a593Smuzhiyun 		return ret;
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	/* Load ROM values to appropriate registers */
983*4882a593Smuzhiyun 	max2175_load_from_rom(ctx);
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ) {
986*4882a593Smuzhiyun 		/* Load FIR coefficients into bank 0 */
987*4882a593Smuzhiyun 		max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0,
988*4882a593Smuzhiyun 					  ch_coeff_fmeu);
989*4882a593Smuzhiyun 		max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
990*4882a593Smuzhiyun 					  eq_coeff_fmeu1_ra02_m6db);
991*4882a593Smuzhiyun 	} else {
992*4882a593Smuzhiyun 		/* Load FIR coefficients into bank 0 */
993*4882a593Smuzhiyun 		max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0,
994*4882a593Smuzhiyun 					  ch_coeff_fmna);
995*4882a593Smuzhiyun 		max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
996*4882a593Smuzhiyun 					  eq_coeff_fmna1_ra02_m6db);
997*4882a593Smuzhiyun 	}
998*4882a593Smuzhiyun 	mxm_dbg(ctx, "core initialized\n");
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	return 0;
1001*4882a593Smuzhiyun }
1002*4882a593Smuzhiyun 
max2175_s_ctrl_rx_mode(struct max2175 * ctx,u32 rx_mode)1003*4882a593Smuzhiyun static void max2175_s_ctrl_rx_mode(struct max2175 *ctx, u32 rx_mode)
1004*4882a593Smuzhiyun {
1005*4882a593Smuzhiyun 	/* Load mode. Range check already done */
1006*4882a593Smuzhiyun 	max2175_set_rx_mode(ctx, rx_mode);
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 	mxm_dbg(ctx, "s_ctrl_rx_mode: %u curr freq %u\n", rx_mode, ctx->freq);
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	/* Check if current freq valid for mode & update */
1011*4882a593Smuzhiyun 	if (max2175_freq_rx_mode_valid(ctx, rx_mode, ctx->freq))
1012*4882a593Smuzhiyun 		max2175_tune_rf_freq(ctx, ctx->freq, ctx->hsls->cur.val);
1013*4882a593Smuzhiyun 	else
1014*4882a593Smuzhiyun 		/* Use default freq of mode if current freq is not valid */
1015*4882a593Smuzhiyun 		max2175_tune_rf_freq(ctx, ctx->rx_modes[rx_mode].freq,
1016*4882a593Smuzhiyun 				     ctx->hsls->cur.val);
1017*4882a593Smuzhiyun }
1018*4882a593Smuzhiyun 
max2175_s_ctrl(struct v4l2_ctrl * ctrl)1019*4882a593Smuzhiyun static int max2175_s_ctrl(struct v4l2_ctrl *ctrl)
1020*4882a593Smuzhiyun {
1021*4882a593Smuzhiyun 	struct max2175 *ctx = max2175_from_ctrl_hdl(ctrl->handler);
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	mxm_dbg(ctx, "s_ctrl: id 0x%x, val %u\n", ctrl->id, ctrl->val);
1024*4882a593Smuzhiyun 	switch (ctrl->id) {
1025*4882a593Smuzhiyun 	case V4L2_CID_MAX2175_I2S_ENABLE:
1026*4882a593Smuzhiyun 		max2175_i2s_enable(ctx, ctrl->val);
1027*4882a593Smuzhiyun 		break;
1028*4882a593Smuzhiyun 	case V4L2_CID_MAX2175_HSLS:
1029*4882a593Smuzhiyun 		max2175_set_hsls(ctx, ctrl->val);
1030*4882a593Smuzhiyun 		break;
1031*4882a593Smuzhiyun 	case V4L2_CID_MAX2175_RX_MODE:
1032*4882a593Smuzhiyun 		max2175_s_ctrl_rx_mode(ctx, ctrl->val);
1033*4882a593Smuzhiyun 		break;
1034*4882a593Smuzhiyun 	}
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 	return 0;
1037*4882a593Smuzhiyun }
1038*4882a593Smuzhiyun 
max2175_get_lna_gain(struct max2175 * ctx)1039*4882a593Smuzhiyun static u32 max2175_get_lna_gain(struct max2175 *ctx)
1040*4882a593Smuzhiyun {
1041*4882a593Smuzhiyun 	enum max2175_band band = max2175_read_bits(ctx, 5, 1, 0);
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	switch (band) {
1044*4882a593Smuzhiyun 	case MAX2175_BAND_AM:
1045*4882a593Smuzhiyun 		return max2175_read_bits(ctx, 51, 3, 0);
1046*4882a593Smuzhiyun 	case MAX2175_BAND_FM:
1047*4882a593Smuzhiyun 		return max2175_read_bits(ctx, 50, 3, 0);
1048*4882a593Smuzhiyun 	case MAX2175_BAND_VHF:
1049*4882a593Smuzhiyun 		return max2175_read_bits(ctx, 52, 5, 0);
1050*4882a593Smuzhiyun 	default:
1051*4882a593Smuzhiyun 		return 0;
1052*4882a593Smuzhiyun 	}
1053*4882a593Smuzhiyun }
1054*4882a593Smuzhiyun 
max2175_g_volatile_ctrl(struct v4l2_ctrl * ctrl)1055*4882a593Smuzhiyun static int max2175_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1056*4882a593Smuzhiyun {
1057*4882a593Smuzhiyun 	struct max2175 *ctx = max2175_from_ctrl_hdl(ctrl->handler);
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun 	switch (ctrl->id) {
1060*4882a593Smuzhiyun 	case V4L2_CID_RF_TUNER_LNA_GAIN:
1061*4882a593Smuzhiyun 		ctrl->val = max2175_get_lna_gain(ctx);
1062*4882a593Smuzhiyun 		break;
1063*4882a593Smuzhiyun 	case V4L2_CID_RF_TUNER_IF_GAIN:
1064*4882a593Smuzhiyun 		ctrl->val = max2175_read_bits(ctx, 49, 4, 0);
1065*4882a593Smuzhiyun 		break;
1066*4882a593Smuzhiyun 	case V4L2_CID_RF_TUNER_PLL_LOCK:
1067*4882a593Smuzhiyun 		ctrl->val = (max2175_read_bits(ctx, 60, 7, 6) == 3);
1068*4882a593Smuzhiyun 		break;
1069*4882a593Smuzhiyun 	}
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 	return 0;
1072*4882a593Smuzhiyun };
1073*4882a593Smuzhiyun 
max2175_set_freq_and_mode(struct max2175 * ctx,u32 freq)1074*4882a593Smuzhiyun static int max2175_set_freq_and_mode(struct max2175 *ctx, u32 freq)
1075*4882a593Smuzhiyun {
1076*4882a593Smuzhiyun 	u32 rx_mode;
1077*4882a593Smuzhiyun 	int ret;
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	/* Get band from frequency */
1080*4882a593Smuzhiyun 	ret = max2175_rx_mode_from_freq(ctx, freq, &rx_mode);
1081*4882a593Smuzhiyun 	if (ret)
1082*4882a593Smuzhiyun 		return ret;
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun 	mxm_dbg(ctx, "set_freq_and_mode: freq %u rx_mode %d\n", freq, rx_mode);
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 	/* Load mode */
1087*4882a593Smuzhiyun 	max2175_set_rx_mode(ctx, rx_mode);
1088*4882a593Smuzhiyun 	ctx->rx_mode->cur.val = rx_mode;
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	/* Tune to the new freq given */
1091*4882a593Smuzhiyun 	return max2175_tune_rf_freq(ctx, freq, ctx->hsls->cur.val);
1092*4882a593Smuzhiyun }
1093*4882a593Smuzhiyun 
max2175_s_frequency(struct v4l2_subdev * sd,const struct v4l2_frequency * vf)1094*4882a593Smuzhiyun static int max2175_s_frequency(struct v4l2_subdev *sd,
1095*4882a593Smuzhiyun 			       const struct v4l2_frequency *vf)
1096*4882a593Smuzhiyun {
1097*4882a593Smuzhiyun 	struct max2175 *ctx = max2175_from_sd(sd);
1098*4882a593Smuzhiyun 	u32 freq;
1099*4882a593Smuzhiyun 	int ret = 0;
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun 	mxm_dbg(ctx, "s_freq: new %u curr %u, mode_resolved %d\n",
1102*4882a593Smuzhiyun 		vf->frequency, ctx->freq, ctx->mode_resolved);
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	if (vf->tuner != 0)
1105*4882a593Smuzhiyun 		return -EINVAL;
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	freq = clamp(vf->frequency, ctx->bands_rf->rangelow,
1108*4882a593Smuzhiyun 		     ctx->bands_rf->rangehigh);
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	/* Check new freq valid for rx_mode if already resolved */
1111*4882a593Smuzhiyun 	if (ctx->mode_resolved &&
1112*4882a593Smuzhiyun 	    max2175_freq_rx_mode_valid(ctx, ctx->rx_mode->cur.val, freq))
1113*4882a593Smuzhiyun 		ret = max2175_tune_rf_freq(ctx, freq, ctx->hsls->cur.val);
1114*4882a593Smuzhiyun 	else
1115*4882a593Smuzhiyun 		/* Find default rx_mode for freq and tune to it */
1116*4882a593Smuzhiyun 		ret = max2175_set_freq_and_mode(ctx, freq);
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	mxm_dbg(ctx, "s_freq: ret %d curr %u mode_resolved %d mode %u\n",
1119*4882a593Smuzhiyun 		ret, ctx->freq, ctx->mode_resolved, ctx->rx_mode->cur.val);
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	return ret;
1122*4882a593Smuzhiyun }
1123*4882a593Smuzhiyun 
max2175_g_frequency(struct v4l2_subdev * sd,struct v4l2_frequency * vf)1124*4882a593Smuzhiyun static int max2175_g_frequency(struct v4l2_subdev *sd,
1125*4882a593Smuzhiyun 			       struct v4l2_frequency *vf)
1126*4882a593Smuzhiyun {
1127*4882a593Smuzhiyun 	struct max2175 *ctx = max2175_from_sd(sd);
1128*4882a593Smuzhiyun 	int ret = 0;
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 	if (vf->tuner != 0)
1131*4882a593Smuzhiyun 		return -EINVAL;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	/* RF freq */
1134*4882a593Smuzhiyun 	vf->type = V4L2_TUNER_RF;
1135*4882a593Smuzhiyun 	vf->frequency = ctx->freq;
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	return ret;
1138*4882a593Smuzhiyun }
1139*4882a593Smuzhiyun 
max2175_enum_freq_bands(struct v4l2_subdev * sd,struct v4l2_frequency_band * band)1140*4882a593Smuzhiyun static int max2175_enum_freq_bands(struct v4l2_subdev *sd,
1141*4882a593Smuzhiyun 			    struct v4l2_frequency_band *band)
1142*4882a593Smuzhiyun {
1143*4882a593Smuzhiyun 	struct max2175 *ctx = max2175_from_sd(sd);
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	if (band->tuner != 0 || band->index != 0)
1146*4882a593Smuzhiyun 		return -EINVAL;
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	*band = *ctx->bands_rf;
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	return 0;
1151*4882a593Smuzhiyun }
1152*4882a593Smuzhiyun 
max2175_g_tuner(struct v4l2_subdev * sd,struct v4l2_tuner * vt)1153*4882a593Smuzhiyun static int max2175_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1154*4882a593Smuzhiyun {
1155*4882a593Smuzhiyun 	struct max2175 *ctx = max2175_from_sd(sd);
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	if (vt->index > 0)
1158*4882a593Smuzhiyun 		return -EINVAL;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	strscpy(vt->name, "RF", sizeof(vt->name));
1161*4882a593Smuzhiyun 	vt->type = V4L2_TUNER_RF;
1162*4882a593Smuzhiyun 	vt->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1163*4882a593Smuzhiyun 	vt->rangelow = ctx->bands_rf->rangelow;
1164*4882a593Smuzhiyun 	vt->rangehigh = ctx->bands_rf->rangehigh;
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 	return 0;
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun 
max2175_s_tuner(struct v4l2_subdev * sd,const struct v4l2_tuner * vt)1169*4882a593Smuzhiyun static int max2175_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
1170*4882a593Smuzhiyun {
1171*4882a593Smuzhiyun 	/* Check tuner index is valid */
1172*4882a593Smuzhiyun 	if (vt->index > 0)
1173*4882a593Smuzhiyun 		return -EINVAL;
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 	return 0;
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun static const struct v4l2_subdev_tuner_ops max2175_tuner_ops = {
1179*4882a593Smuzhiyun 	.s_frequency = max2175_s_frequency,
1180*4882a593Smuzhiyun 	.g_frequency = max2175_g_frequency,
1181*4882a593Smuzhiyun 	.enum_freq_bands = max2175_enum_freq_bands,
1182*4882a593Smuzhiyun 	.g_tuner = max2175_g_tuner,
1183*4882a593Smuzhiyun 	.s_tuner = max2175_s_tuner,
1184*4882a593Smuzhiyun };
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun static const struct v4l2_subdev_ops max2175_ops = {
1187*4882a593Smuzhiyun 	.tuner = &max2175_tuner_ops,
1188*4882a593Smuzhiyun };
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun static const struct v4l2_ctrl_ops max2175_ctrl_ops = {
1191*4882a593Smuzhiyun 	.s_ctrl = max2175_s_ctrl,
1192*4882a593Smuzhiyun 	.g_volatile_ctrl = max2175_g_volatile_ctrl,
1193*4882a593Smuzhiyun };
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun /*
1196*4882a593Smuzhiyun  * I2S output enable/disable configuration. This is a private control.
1197*4882a593Smuzhiyun  * Refer to Documentation/userspace-api/media/drivers/max2175.rst for more details.
1198*4882a593Smuzhiyun  */
1199*4882a593Smuzhiyun static const struct v4l2_ctrl_config max2175_i2s_en = {
1200*4882a593Smuzhiyun 	.ops = &max2175_ctrl_ops,
1201*4882a593Smuzhiyun 	.id = V4L2_CID_MAX2175_I2S_ENABLE,
1202*4882a593Smuzhiyun 	.name = "I2S Enable",
1203*4882a593Smuzhiyun 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1204*4882a593Smuzhiyun 	.min = 0,
1205*4882a593Smuzhiyun 	.max = 1,
1206*4882a593Smuzhiyun 	.step = 1,
1207*4882a593Smuzhiyun 	.def = 1,
1208*4882a593Smuzhiyun 	.is_private = 1,
1209*4882a593Smuzhiyun };
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun /*
1212*4882a593Smuzhiyun  * HSLS value control LO freq adjacent location configuration.
1213*4882a593Smuzhiyun  * Refer to Documentation/userspace-api/media/drivers/max2175.rst for more details.
1214*4882a593Smuzhiyun  */
1215*4882a593Smuzhiyun static const struct v4l2_ctrl_config max2175_hsls = {
1216*4882a593Smuzhiyun 	.ops = &max2175_ctrl_ops,
1217*4882a593Smuzhiyun 	.id = V4L2_CID_MAX2175_HSLS,
1218*4882a593Smuzhiyun 	.name = "HSLS Above/Below Desired",
1219*4882a593Smuzhiyun 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1220*4882a593Smuzhiyun 	.min = 0,
1221*4882a593Smuzhiyun 	.max = 1,
1222*4882a593Smuzhiyun 	.step = 1,
1223*4882a593Smuzhiyun 	.def = 1,
1224*4882a593Smuzhiyun };
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun /*
1227*4882a593Smuzhiyun  * Rx modes below are a set of preset configurations that decides the tuner's
1228*4882a593Smuzhiyun  * sck and sample rate of transmission. They are separate for EU & NA regions.
1229*4882a593Smuzhiyun  * Refer to Documentation/userspace-api/media/drivers/max2175.rst for more details.
1230*4882a593Smuzhiyun  */
1231*4882a593Smuzhiyun static const char * const max2175_ctrl_eu_rx_modes[] = {
1232*4882a593Smuzhiyun 	[MAX2175_EU_FM_1_2]	= "EU FM 1.2",
1233*4882a593Smuzhiyun 	[MAX2175_DAB_1_2]	= "DAB 1.2",
1234*4882a593Smuzhiyun };
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun static const char * const max2175_ctrl_na_rx_modes[] = {
1237*4882a593Smuzhiyun 	[MAX2175_NA_FM_1_0]	= "NA FM 1.0",
1238*4882a593Smuzhiyun 	[MAX2175_NA_FM_2_0]	= "NA FM 2.0",
1239*4882a593Smuzhiyun };
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun static const struct v4l2_ctrl_config max2175_eu_rx_mode = {
1242*4882a593Smuzhiyun 	.ops = &max2175_ctrl_ops,
1243*4882a593Smuzhiyun 	.id = V4L2_CID_MAX2175_RX_MODE,
1244*4882a593Smuzhiyun 	.name = "RX Mode",
1245*4882a593Smuzhiyun 	.type = V4L2_CTRL_TYPE_MENU,
1246*4882a593Smuzhiyun 	.max = ARRAY_SIZE(max2175_ctrl_eu_rx_modes) - 1,
1247*4882a593Smuzhiyun 	.def = 0,
1248*4882a593Smuzhiyun 	.qmenu = max2175_ctrl_eu_rx_modes,
1249*4882a593Smuzhiyun };
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun static const struct v4l2_ctrl_config max2175_na_rx_mode = {
1252*4882a593Smuzhiyun 	.ops = &max2175_ctrl_ops,
1253*4882a593Smuzhiyun 	.id = V4L2_CID_MAX2175_RX_MODE,
1254*4882a593Smuzhiyun 	.name = "RX Mode",
1255*4882a593Smuzhiyun 	.type = V4L2_CTRL_TYPE_MENU,
1256*4882a593Smuzhiyun 	.max = ARRAY_SIZE(max2175_ctrl_na_rx_modes) - 1,
1257*4882a593Smuzhiyun 	.def = 0,
1258*4882a593Smuzhiyun 	.qmenu = max2175_ctrl_na_rx_modes,
1259*4882a593Smuzhiyun };
1260*4882a593Smuzhiyun 
max2175_refout_load_to_bits(struct i2c_client * client,u32 load,u32 * bits)1261*4882a593Smuzhiyun static int max2175_refout_load_to_bits(struct i2c_client *client, u32 load,
1262*4882a593Smuzhiyun 				       u32 *bits)
1263*4882a593Smuzhiyun {
1264*4882a593Smuzhiyun 	if (load <= 40)
1265*4882a593Smuzhiyun 		*bits = load / 10;
1266*4882a593Smuzhiyun 	else if (load >= 60 && load <= 70)
1267*4882a593Smuzhiyun 		*bits = load / 10 - 1;
1268*4882a593Smuzhiyun 	else
1269*4882a593Smuzhiyun 		return -EINVAL;
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun 	return 0;
1272*4882a593Smuzhiyun }
1273*4882a593Smuzhiyun 
max2175_probe(struct i2c_client * client)1274*4882a593Smuzhiyun static int max2175_probe(struct i2c_client *client)
1275*4882a593Smuzhiyun {
1276*4882a593Smuzhiyun 	bool master = true, am_hiz = false;
1277*4882a593Smuzhiyun 	u32 refout_load, refout_bits = 0;	/* REFOUT disabled */
1278*4882a593Smuzhiyun 	struct v4l2_ctrl_handler *hdl;
1279*4882a593Smuzhiyun 	struct fwnode_handle *fwnode;
1280*4882a593Smuzhiyun 	struct device_node *np;
1281*4882a593Smuzhiyun 	struct v4l2_subdev *sd;
1282*4882a593Smuzhiyun 	struct regmap *regmap;
1283*4882a593Smuzhiyun 	struct max2175 *ctx;
1284*4882a593Smuzhiyun 	struct clk *clk;
1285*4882a593Smuzhiyun 	int ret;
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun 	/* Parse DT properties */
1288*4882a593Smuzhiyun 	np = of_parse_phandle(client->dev.of_node, "maxim,master", 0);
1289*4882a593Smuzhiyun 	if (np) {
1290*4882a593Smuzhiyun 		master = false;			/* Slave tuner */
1291*4882a593Smuzhiyun 		of_node_put(np);
1292*4882a593Smuzhiyun 	}
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	fwnode = of_fwnode_handle(client->dev.of_node);
1295*4882a593Smuzhiyun 	if (fwnode_property_present(fwnode, "maxim,am-hiz-filter"))
1296*4882a593Smuzhiyun 		am_hiz = true;
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	if (!fwnode_property_read_u32(fwnode, "maxim,refout-load",
1299*4882a593Smuzhiyun 				      &refout_load)) {
1300*4882a593Smuzhiyun 		ret = max2175_refout_load_to_bits(client, refout_load,
1301*4882a593Smuzhiyun 						  &refout_bits);
1302*4882a593Smuzhiyun 		if (ret) {
1303*4882a593Smuzhiyun 			dev_err(&client->dev, "invalid refout_load %u\n",
1304*4882a593Smuzhiyun 				refout_load);
1305*4882a593Smuzhiyun 			return -EINVAL;
1306*4882a593Smuzhiyun 		}
1307*4882a593Smuzhiyun 	}
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 	clk = devm_clk_get(&client->dev, NULL);
1310*4882a593Smuzhiyun 	if (IS_ERR(clk)) {
1311*4882a593Smuzhiyun 		ret = PTR_ERR(clk);
1312*4882a593Smuzhiyun 		dev_err(&client->dev, "cannot get clock %d\n", ret);
1313*4882a593Smuzhiyun 		return ret;
1314*4882a593Smuzhiyun 	}
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun 	regmap = devm_regmap_init_i2c(client, &max2175_regmap_config);
1317*4882a593Smuzhiyun 	if (IS_ERR(regmap)) {
1318*4882a593Smuzhiyun 		ret = PTR_ERR(regmap);
1319*4882a593Smuzhiyun 		dev_err(&client->dev, "regmap init failed %d\n", ret);
1320*4882a593Smuzhiyun 		return -ENODEV;
1321*4882a593Smuzhiyun 	}
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	/* Alloc tuner context */
1324*4882a593Smuzhiyun 	ctx = devm_kzalloc(&client->dev, sizeof(*ctx), GFP_KERNEL);
1325*4882a593Smuzhiyun 	if (ctx == NULL)
1326*4882a593Smuzhiyun 		return -ENOMEM;
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 	sd = &ctx->sd;
1329*4882a593Smuzhiyun 	ctx->master = master;
1330*4882a593Smuzhiyun 	ctx->am_hiz = am_hiz;
1331*4882a593Smuzhiyun 	ctx->mode_resolved = false;
1332*4882a593Smuzhiyun 	ctx->regmap = regmap;
1333*4882a593Smuzhiyun 	ctx->xtal_freq = clk_get_rate(clk);
1334*4882a593Smuzhiyun 	dev_info(&client->dev, "xtal freq %luHz\n", ctx->xtal_freq);
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun 	v4l2_i2c_subdev_init(sd, client, &max2175_ops);
1337*4882a593Smuzhiyun 	ctx->client = client;
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 	/* Controls */
1342*4882a593Smuzhiyun 	hdl = &ctx->ctrl_hdl;
1343*4882a593Smuzhiyun 	ret = v4l2_ctrl_handler_init(hdl, 7);
1344*4882a593Smuzhiyun 	if (ret)
1345*4882a593Smuzhiyun 		return ret;
1346*4882a593Smuzhiyun 
1347*4882a593Smuzhiyun 	ctx->lna_gain = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
1348*4882a593Smuzhiyun 					  V4L2_CID_RF_TUNER_LNA_GAIN,
1349*4882a593Smuzhiyun 					  0, 63, 1, 0);
1350*4882a593Smuzhiyun 	ctx->lna_gain->flags |= (V4L2_CTRL_FLAG_VOLATILE |
1351*4882a593Smuzhiyun 				 V4L2_CTRL_FLAG_READ_ONLY);
1352*4882a593Smuzhiyun 	ctx->if_gain = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
1353*4882a593Smuzhiyun 					 V4L2_CID_RF_TUNER_IF_GAIN,
1354*4882a593Smuzhiyun 					 0, 31, 1, 0);
1355*4882a593Smuzhiyun 	ctx->if_gain->flags |= (V4L2_CTRL_FLAG_VOLATILE |
1356*4882a593Smuzhiyun 				V4L2_CTRL_FLAG_READ_ONLY);
1357*4882a593Smuzhiyun 	ctx->pll_lock = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
1358*4882a593Smuzhiyun 					  V4L2_CID_RF_TUNER_PLL_LOCK,
1359*4882a593Smuzhiyun 					  0, 1, 1, 0);
1360*4882a593Smuzhiyun 	ctx->pll_lock->flags |= (V4L2_CTRL_FLAG_VOLATILE |
1361*4882a593Smuzhiyun 				 V4L2_CTRL_FLAG_READ_ONLY);
1362*4882a593Smuzhiyun 	ctx->i2s_en = v4l2_ctrl_new_custom(hdl, &max2175_i2s_en, NULL);
1363*4882a593Smuzhiyun 	ctx->hsls = v4l2_ctrl_new_custom(hdl, &max2175_hsls, NULL);
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ) {
1366*4882a593Smuzhiyun 		ctx->rx_mode = v4l2_ctrl_new_custom(hdl,
1367*4882a593Smuzhiyun 						    &max2175_eu_rx_mode, NULL);
1368*4882a593Smuzhiyun 		ctx->rx_modes = eu_rx_modes;
1369*4882a593Smuzhiyun 		ctx->bands_rf = &eu_bands_rf;
1370*4882a593Smuzhiyun 	} else {
1371*4882a593Smuzhiyun 		ctx->rx_mode = v4l2_ctrl_new_custom(hdl,
1372*4882a593Smuzhiyun 						    &max2175_na_rx_mode, NULL);
1373*4882a593Smuzhiyun 		ctx->rx_modes = na_rx_modes;
1374*4882a593Smuzhiyun 		ctx->bands_rf = &na_bands_rf;
1375*4882a593Smuzhiyun 	}
1376*4882a593Smuzhiyun 	ctx->sd.ctrl_handler = &ctx->ctrl_hdl;
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 	/* Set the defaults */
1379*4882a593Smuzhiyun 	ctx->freq = ctx->bands_rf->rangelow;
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	/* Register subdev */
1382*4882a593Smuzhiyun 	ret = v4l2_async_register_subdev(sd);
1383*4882a593Smuzhiyun 	if (ret) {
1384*4882a593Smuzhiyun 		dev_err(&client->dev, "register subdev failed\n");
1385*4882a593Smuzhiyun 		goto err_reg;
1386*4882a593Smuzhiyun 	}
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 	/* Initialize device */
1389*4882a593Smuzhiyun 	ret = max2175_core_init(ctx, refout_bits);
1390*4882a593Smuzhiyun 	if (ret)
1391*4882a593Smuzhiyun 		goto err_init;
1392*4882a593Smuzhiyun 
1393*4882a593Smuzhiyun 	ret = v4l2_ctrl_handler_setup(hdl);
1394*4882a593Smuzhiyun 	if (ret)
1395*4882a593Smuzhiyun 		goto err_init;
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun 	return 0;
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun err_init:
1400*4882a593Smuzhiyun 	v4l2_async_unregister_subdev(sd);
1401*4882a593Smuzhiyun err_reg:
1402*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 	return ret;
1405*4882a593Smuzhiyun }
1406*4882a593Smuzhiyun 
max2175_remove(struct i2c_client * client)1407*4882a593Smuzhiyun static int max2175_remove(struct i2c_client *client)
1408*4882a593Smuzhiyun {
1409*4882a593Smuzhiyun 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1410*4882a593Smuzhiyun 	struct max2175 *ctx = max2175_from_sd(sd);
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
1413*4882a593Smuzhiyun 	v4l2_async_unregister_subdev(sd);
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun 	return 0;
1416*4882a593Smuzhiyun }
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun static const struct i2c_device_id max2175_id[] = {
1419*4882a593Smuzhiyun 	{ DRIVER_NAME, 0},
1420*4882a593Smuzhiyun 	{},
1421*4882a593Smuzhiyun };
1422*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, max2175_id);
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun static const struct of_device_id max2175_of_ids[] = {
1425*4882a593Smuzhiyun 	{ .compatible = "maxim,max2175", },
1426*4882a593Smuzhiyun 	{ }
1427*4882a593Smuzhiyun };
1428*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, max2175_of_ids);
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun static struct i2c_driver max2175_driver = {
1431*4882a593Smuzhiyun 	.driver = {
1432*4882a593Smuzhiyun 		.name	= DRIVER_NAME,
1433*4882a593Smuzhiyun 		.of_match_table = max2175_of_ids,
1434*4882a593Smuzhiyun 	},
1435*4882a593Smuzhiyun 	.probe_new	= max2175_probe,
1436*4882a593Smuzhiyun 	.remove		= max2175_remove,
1437*4882a593Smuzhiyun 	.id_table	= max2175_id,
1438*4882a593Smuzhiyun };
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun module_i2c_driver(max2175_driver);
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun MODULE_DESCRIPTION("Maxim MAX2175 RF to Bits tuner driver");
1443*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
1444*4882a593Smuzhiyun MODULE_AUTHOR("Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>");
1445