18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Maxim Integrated MAX2175 RF to Bits tuner driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This driver & most of the hard coded values are based on the reference
68c2ecf20Sopenharmony_ci * application delivered by Maxim for this device.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (C) 2016 Maxim Integrated Products
98c2ecf20Sopenharmony_ci * Copyright (C) 2017 Renesas Electronics Corporation
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/errno.h>
158c2ecf20Sopenharmony_ci#include <linux/i2c.h>
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/math64.h>
188c2ecf20Sopenharmony_ci#include <linux/max2175.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/of.h>
218c2ecf20Sopenharmony_ci#include <linux/regmap.h>
228c2ecf20Sopenharmony_ci#include <linux/slab.h>
238c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
248c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include "max2175.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define DRIVER_NAME "max2175"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define mxm_dbg(ctx, fmt, arg...) dev_dbg(&ctx->client->dev, fmt, ## arg)
318c2ecf20Sopenharmony_ci#define mxm_err(ctx, fmt, arg...) dev_err(&ctx->client->dev, fmt, ## arg)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* Rx mode */
348c2ecf20Sopenharmony_cistruct max2175_rxmode {
358c2ecf20Sopenharmony_ci	enum max2175_band band;		/* Associated band */
368c2ecf20Sopenharmony_ci	u32 freq;			/* Default freq in Hz */
378c2ecf20Sopenharmony_ci	u8 i2s_word_size;		/* Bit value */
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* Register map to define preset values */
418c2ecf20Sopenharmony_cistruct max2175_reg_map {
428c2ecf20Sopenharmony_ci	u8 idx;				/* Register index */
438c2ecf20Sopenharmony_ci	u8 val;				/* Register value */
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic const struct max2175_rxmode eu_rx_modes[] = {
478c2ecf20Sopenharmony_ci	/* EU modes */
488c2ecf20Sopenharmony_ci	[MAX2175_EU_FM_1_2] = { MAX2175_BAND_FM, 98256000, 1 },
498c2ecf20Sopenharmony_ci	[MAX2175_DAB_1_2]   = { MAX2175_BAND_VHF, 182640000, 0 },
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic const struct max2175_rxmode na_rx_modes[] = {
538c2ecf20Sopenharmony_ci	/* NA modes */
548c2ecf20Sopenharmony_ci	[MAX2175_NA_FM_1_0] = { MAX2175_BAND_FM, 98255520, 1 },
558c2ecf20Sopenharmony_ci	[MAX2175_NA_FM_2_0] = { MAX2175_BAND_FM, 98255520, 6 },
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/*
598c2ecf20Sopenharmony_ci * Preset values:
608c2ecf20Sopenharmony_ci * Based on Maxim MAX2175 Register Table revision: 130p10
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_cistatic const u8 full_fm_eu_1p0[] = {
638c2ecf20Sopenharmony_ci	0x15, 0x04, 0xb8, 0xe3, 0x35, 0x18, 0x7c, 0x00,
648c2ecf20Sopenharmony_ci	0x00, 0x7d, 0x40, 0x08, 0x70, 0x7a, 0x88, 0x91,
658c2ecf20Sopenharmony_ci	0x61, 0x61, 0x61, 0x61, 0x5a, 0x0f, 0x34, 0x1c,
668c2ecf20Sopenharmony_ci	0x14, 0x88, 0x33, 0x02, 0x00, 0x09, 0x00, 0x65,
678c2ecf20Sopenharmony_ci	0x9f, 0x2b, 0x80, 0x00, 0x95, 0x05, 0x2c, 0x00,
688c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
698c2ecf20Sopenharmony_ci	0x4a, 0x08, 0xa8, 0x0e, 0x0e, 0x2f, 0x7e, 0x00,
708c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
718c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0xab, 0x5e, 0xa9,
728c2ecf20Sopenharmony_ci	0xae, 0xbb, 0x57, 0x18, 0x3b, 0x03, 0x3b, 0x64,
738c2ecf20Sopenharmony_ci	0x40, 0x60, 0x00, 0x2a, 0xbf, 0x3f, 0xff, 0x9f,
748c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00,
758c2ecf20Sopenharmony_ci	0xff, 0xfc, 0xef, 0x1c, 0x40, 0x00, 0x00, 0x02,
768c2ecf20Sopenharmony_ci	0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
778c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0xac, 0x40, 0x00,
788c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00,
798c2ecf20Sopenharmony_ci	0x00, 0x47, 0x00, 0x00, 0x11, 0x3f, 0x22, 0x00,
808c2ecf20Sopenharmony_ci	0xf1, 0x00, 0x41, 0x03, 0xb0, 0x00, 0x00, 0x00,
818c2ecf20Sopenharmony_ci	0x1b,
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic const u8 full_fm_na_1p0[] = {
858c2ecf20Sopenharmony_ci	0x13, 0x08, 0x8d, 0xc0, 0x35, 0x18, 0x7d, 0x3f,
868c2ecf20Sopenharmony_ci	0x7d, 0x75, 0x40, 0x08, 0x70, 0x7a, 0x88, 0x91,
878c2ecf20Sopenharmony_ci	0x61, 0x61, 0x61, 0x61, 0x5c, 0x0f, 0x34, 0x1c,
888c2ecf20Sopenharmony_ci	0x14, 0x88, 0x33, 0x02, 0x00, 0x01, 0x00, 0x65,
898c2ecf20Sopenharmony_ci	0x9f, 0x2b, 0x80, 0x00, 0x95, 0x05, 0x2c, 0x00,
908c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
918c2ecf20Sopenharmony_ci	0x4a, 0x08, 0xa8, 0x0e, 0x0e, 0xaf, 0x7e, 0x00,
928c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
938c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0xab, 0x5e, 0xa9,
948c2ecf20Sopenharmony_ci	0xae, 0xbb, 0x57, 0x18, 0x3b, 0x03, 0x3b, 0x64,
958c2ecf20Sopenharmony_ci	0x40, 0x60, 0x00, 0x2a, 0xbf, 0x3f, 0xff, 0x9f,
968c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00,
978c2ecf20Sopenharmony_ci	0xff, 0xfc, 0xef, 0x1c, 0x40, 0x00, 0x00, 0x02,
988c2ecf20Sopenharmony_ci	0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
998c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0xa6, 0x40, 0x00,
1008c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00,
1018c2ecf20Sopenharmony_ci	0x00, 0x35, 0x00, 0x00, 0x11, 0x3f, 0x22, 0x00,
1028c2ecf20Sopenharmony_ci	0xf1, 0x00, 0x41, 0x03, 0xb0, 0x00, 0x00, 0x00,
1038c2ecf20Sopenharmony_ci	0x1b,
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/* DAB1.2 settings */
1078c2ecf20Sopenharmony_cistatic const struct max2175_reg_map dab12_map[] = {
1088c2ecf20Sopenharmony_ci	{ 0x01, 0x13 }, { 0x02, 0x0d }, { 0x03, 0x15 }, { 0x04, 0x55 },
1098c2ecf20Sopenharmony_ci	{ 0x05, 0x0a }, { 0x06, 0xa0 }, { 0x07, 0x40 }, { 0x08, 0x00 },
1108c2ecf20Sopenharmony_ci	{ 0x09, 0x00 }, { 0x0a, 0x7d }, { 0x0b, 0x4a }, { 0x0c, 0x28 },
1118c2ecf20Sopenharmony_ci	{ 0x0e, 0x43 }, { 0x0f, 0xb5 }, { 0x10, 0x31 }, { 0x11, 0x9e },
1128c2ecf20Sopenharmony_ci	{ 0x12, 0x68 }, { 0x13, 0x9e }, { 0x14, 0x68 }, { 0x15, 0x58 },
1138c2ecf20Sopenharmony_ci	{ 0x16, 0x2f }, { 0x17, 0x3f }, { 0x18, 0x40 }, { 0x1a, 0x88 },
1148c2ecf20Sopenharmony_ci	{ 0x1b, 0xaa }, { 0x1c, 0x9a }, { 0x1d, 0x00 }, { 0x1e, 0x00 },
1158c2ecf20Sopenharmony_ci	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x00 }, { 0x26, 0x00 },
1168c2ecf20Sopenharmony_ci	{ 0x27, 0x00 }, { 0x32, 0x08 }, { 0x33, 0xf8 }, { 0x36, 0x2d },
1178c2ecf20Sopenharmony_ci	{ 0x37, 0x7e }, { 0x55, 0xaf }, { 0x56, 0x3f }, { 0x57, 0xf8 },
1188c2ecf20Sopenharmony_ci	{ 0x58, 0x99 }, { 0x76, 0x00 }, { 0x77, 0x00 }, { 0x78, 0x02 },
1198c2ecf20Sopenharmony_ci	{ 0x79, 0x40 }, { 0x82, 0x00 }, { 0x83, 0x00 }, { 0x85, 0x00 },
1208c2ecf20Sopenharmony_ci	{ 0x86, 0x20 },
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* EU FM 1.2 settings */
1248c2ecf20Sopenharmony_cistatic const struct max2175_reg_map fmeu1p2_map[] = {
1258c2ecf20Sopenharmony_ci	{ 0x01, 0x15 }, { 0x02, 0x04 }, { 0x03, 0xb8 }, { 0x04, 0xe3 },
1268c2ecf20Sopenharmony_ci	{ 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7c }, { 0x08, 0x00 },
1278c2ecf20Sopenharmony_ci	{ 0x09, 0x00 }, { 0x0a, 0x73 }, { 0x0b, 0x40 }, { 0x0c, 0x08 },
1288c2ecf20Sopenharmony_ci	{ 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
1298c2ecf20Sopenharmony_ci	{ 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5a },
1308c2ecf20Sopenharmony_ci	{ 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
1318c2ecf20Sopenharmony_ci	{ 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
1328c2ecf20Sopenharmony_ci	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
1338c2ecf20Sopenharmony_ci	{ 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0x2f },
1348c2ecf20Sopenharmony_ci	{ 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
1358c2ecf20Sopenharmony_ci	{ 0x58, 0x9f }, { 0x76, 0xac }, { 0x77, 0x40 }, { 0x78, 0x00 },
1368c2ecf20Sopenharmony_ci	{ 0x79, 0x00 }, { 0x82, 0x47 }, { 0x83, 0x00 }, { 0x85, 0x11 },
1378c2ecf20Sopenharmony_ci	{ 0x86, 0x3f },
1388c2ecf20Sopenharmony_ci};
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/* FM NA 1.0 settings */
1418c2ecf20Sopenharmony_cistatic const struct max2175_reg_map fmna1p0_map[] = {
1428c2ecf20Sopenharmony_ci	{ 0x01, 0x13 }, { 0x02, 0x08 }, { 0x03, 0x8d }, { 0x04, 0xc0 },
1438c2ecf20Sopenharmony_ci	{ 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7d }, { 0x08, 0x3f },
1448c2ecf20Sopenharmony_ci	{ 0x09, 0x7d }, { 0x0a, 0x75 }, { 0x0b, 0x40 }, { 0x0c, 0x08 },
1458c2ecf20Sopenharmony_ci	{ 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
1468c2ecf20Sopenharmony_ci	{ 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5c },
1478c2ecf20Sopenharmony_ci	{ 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
1488c2ecf20Sopenharmony_ci	{ 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
1498c2ecf20Sopenharmony_ci	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
1508c2ecf20Sopenharmony_ci	{ 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0xaf },
1518c2ecf20Sopenharmony_ci	{ 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
1528c2ecf20Sopenharmony_ci	{ 0x58, 0x9f }, { 0x76, 0xa6 }, { 0x77, 0x40 }, { 0x78, 0x00 },
1538c2ecf20Sopenharmony_ci	{ 0x79, 0x00 }, { 0x82, 0x35 }, { 0x83, 0x00 }, { 0x85, 0x11 },
1548c2ecf20Sopenharmony_ci	{ 0x86, 0x3f },
1558c2ecf20Sopenharmony_ci};
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/* FM NA 2.0 settings */
1588c2ecf20Sopenharmony_cistatic const struct max2175_reg_map fmna2p0_map[] = {
1598c2ecf20Sopenharmony_ci	{ 0x01, 0x13 }, { 0x02, 0x08 }, { 0x03, 0x8d }, { 0x04, 0xc0 },
1608c2ecf20Sopenharmony_ci	{ 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7c }, { 0x08, 0x54 },
1618c2ecf20Sopenharmony_ci	{ 0x09, 0xa7 }, { 0x0a, 0x55 }, { 0x0b, 0x42 }, { 0x0c, 0x48 },
1628c2ecf20Sopenharmony_ci	{ 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
1638c2ecf20Sopenharmony_ci	{ 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5c },
1648c2ecf20Sopenharmony_ci	{ 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
1658c2ecf20Sopenharmony_ci	{ 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
1668c2ecf20Sopenharmony_ci	{ 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
1678c2ecf20Sopenharmony_ci	{ 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0xaf },
1688c2ecf20Sopenharmony_ci	{ 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
1698c2ecf20Sopenharmony_ci	{ 0x58, 0x9f }, { 0x76, 0xac }, { 0x77, 0xc0 }, { 0x78, 0x00 },
1708c2ecf20Sopenharmony_ci	{ 0x79, 0x00 }, { 0x82, 0x6b }, { 0x83, 0x00 }, { 0x85, 0x11 },
1718c2ecf20Sopenharmony_ci	{ 0x86, 0x3f },
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic const u16 ch_coeff_dab1[] = {
1758c2ecf20Sopenharmony_ci	0x001c, 0x0007, 0xffcd, 0x0056, 0xffa4, 0x0033, 0x0027, 0xff61,
1768c2ecf20Sopenharmony_ci	0x010e, 0xfec0, 0x0106, 0xffb8, 0xff1c, 0x023c, 0xfcb2, 0x039b,
1778c2ecf20Sopenharmony_ci	0xfd4e, 0x0055, 0x036a, 0xf7de, 0x0d21, 0xee72, 0x1499, 0x6a51,
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic const u16 ch_coeff_fmeu[] = {
1818c2ecf20Sopenharmony_ci	0x0000, 0xffff, 0x0001, 0x0002, 0xfffa, 0xffff, 0x0015, 0xffec,
1828c2ecf20Sopenharmony_ci	0xffde, 0x0054, 0xfff9, 0xff52, 0x00b8, 0x00a2, 0xfe0a, 0x00af,
1838c2ecf20Sopenharmony_ci	0x02e3, 0xfc14, 0xfe89, 0x089d, 0xfa2e, 0xf30f, 0x25be, 0x4eb6,
1848c2ecf20Sopenharmony_ci};
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic const u16 eq_coeff_fmeu1_ra02_m6db[] = {
1878c2ecf20Sopenharmony_ci	0x0040, 0xffc6, 0xfffa, 0x002c, 0x000d, 0xff90, 0x0037, 0x006e,
1888c2ecf20Sopenharmony_ci	0xffc0, 0xff5b, 0x006a, 0x00f0, 0xff57, 0xfe94, 0x0112, 0x0252,
1898c2ecf20Sopenharmony_ci	0xfe0c, 0xfc6a, 0x0385, 0x0553, 0xfa49, 0xf789, 0x0b91, 0x1a10,
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic const u16 ch_coeff_fmna[] = {
1938c2ecf20Sopenharmony_ci	0x0001, 0x0003, 0xfffe, 0xfff4, 0x0000, 0x001f, 0x000c, 0xffbc,
1948c2ecf20Sopenharmony_ci	0xffd3, 0x007d, 0x0075, 0xff33, 0xff01, 0x0131, 0x01ef, 0xfe60,
1958c2ecf20Sopenharmony_ci	0xfc7a, 0x020e, 0x0656, 0xfd94, 0xf395, 0x02ab, 0x2857, 0x3d3f,
1968c2ecf20Sopenharmony_ci};
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic const u16 eq_coeff_fmna1_ra02_m6db[] = {
1998c2ecf20Sopenharmony_ci	0xfff1, 0xffe1, 0xffef, 0x000e, 0x0030, 0x002f, 0xfff6, 0xffa7,
2008c2ecf20Sopenharmony_ci	0xff9d, 0x000a, 0x00a2, 0x00b5, 0xffea, 0xfed9, 0xfec5, 0x003d,
2018c2ecf20Sopenharmony_ci	0x0217, 0x021b, 0xff5a, 0xfc2b, 0xfcbd, 0x02c4, 0x0ac3, 0x0e85,
2028c2ecf20Sopenharmony_ci};
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic const u8 adc_presets[2][23] = {
2058c2ecf20Sopenharmony_ci	{
2068c2ecf20Sopenharmony_ci		0x83, 0x00, 0xcf, 0xb4, 0x0f, 0x2c, 0x0c, 0x49,
2078c2ecf20Sopenharmony_ci		0x00, 0x00, 0x00, 0x8c,	0x02, 0x02, 0x00, 0x04,
2088c2ecf20Sopenharmony_ci		0xec, 0x82, 0x4b, 0xcc, 0x01, 0x88, 0x0c,
2098c2ecf20Sopenharmony_ci	},
2108c2ecf20Sopenharmony_ci	{
2118c2ecf20Sopenharmony_ci		0x83, 0x00, 0xcf, 0xb4,	0x0f, 0x2c, 0x0c, 0x49,
2128c2ecf20Sopenharmony_ci		0x00, 0x00, 0x00, 0x8c,	0x02, 0x20, 0x33, 0x8c,
2138c2ecf20Sopenharmony_ci		0x57, 0xd7, 0x59, 0xb7,	0x65, 0x0e, 0x0c,
2148c2ecf20Sopenharmony_ci	},
2158c2ecf20Sopenharmony_ci};
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/* Tuner bands */
2188c2ecf20Sopenharmony_cistatic const struct v4l2_frequency_band eu_bands_rf = {
2198c2ecf20Sopenharmony_ci	.tuner = 0,
2208c2ecf20Sopenharmony_ci	.type = V4L2_TUNER_RF,
2218c2ecf20Sopenharmony_ci	.index = 0,
2228c2ecf20Sopenharmony_ci	.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
2238c2ecf20Sopenharmony_ci	.rangelow   = 65000000,
2248c2ecf20Sopenharmony_ci	.rangehigh  = 240000000,
2258c2ecf20Sopenharmony_ci};
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic const struct v4l2_frequency_band na_bands_rf = {
2288c2ecf20Sopenharmony_ci	.tuner = 0,
2298c2ecf20Sopenharmony_ci	.type = V4L2_TUNER_RF,
2308c2ecf20Sopenharmony_ci	.index = 0,
2318c2ecf20Sopenharmony_ci	.capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
2328c2ecf20Sopenharmony_ci	.rangelow   = 65000000,
2338c2ecf20Sopenharmony_ci	.rangehigh  = 108000000,
2348c2ecf20Sopenharmony_ci};
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci/* Regmap settings */
2378c2ecf20Sopenharmony_cistatic const struct regmap_range max2175_regmap_volatile_range[] = {
2388c2ecf20Sopenharmony_ci	regmap_reg_range(0x30, 0x35),
2398c2ecf20Sopenharmony_ci	regmap_reg_range(0x3a, 0x45),
2408c2ecf20Sopenharmony_ci	regmap_reg_range(0x59, 0x5e),
2418c2ecf20Sopenharmony_ci	regmap_reg_range(0x73, 0x75),
2428c2ecf20Sopenharmony_ci};
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic const struct regmap_access_table max2175_volatile_regs = {
2458c2ecf20Sopenharmony_ci	.yes_ranges = max2175_regmap_volatile_range,
2468c2ecf20Sopenharmony_ci	.n_yes_ranges = ARRAY_SIZE(max2175_regmap_volatile_range),
2478c2ecf20Sopenharmony_ci};
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic const struct reg_default max2175_reg_defaults[] = {
2508c2ecf20Sopenharmony_ci	{ 0x00, 0x07},
2518c2ecf20Sopenharmony_ci};
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic const struct regmap_config max2175_regmap_config = {
2548c2ecf20Sopenharmony_ci	.reg_bits = 8,
2558c2ecf20Sopenharmony_ci	.val_bits = 8,
2568c2ecf20Sopenharmony_ci	.max_register = 0xff,
2578c2ecf20Sopenharmony_ci	.reg_defaults = max2175_reg_defaults,
2588c2ecf20Sopenharmony_ci	.num_reg_defaults = ARRAY_SIZE(max2175_reg_defaults),
2598c2ecf20Sopenharmony_ci	.volatile_table = &max2175_volatile_regs,
2608c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_FLAT,
2618c2ecf20Sopenharmony_ci};
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistruct max2175 {
2648c2ecf20Sopenharmony_ci	struct v4l2_subdev sd;		/* Sub-device */
2658c2ecf20Sopenharmony_ci	struct i2c_client *client;	/* I2C client */
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	/* Controls */
2688c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler ctrl_hdl;
2698c2ecf20Sopenharmony_ci	struct v4l2_ctrl *lna_gain;	/* LNA gain value */
2708c2ecf20Sopenharmony_ci	struct v4l2_ctrl *if_gain;	/* I/F gain value */
2718c2ecf20Sopenharmony_ci	struct v4l2_ctrl *pll_lock;	/* PLL lock */
2728c2ecf20Sopenharmony_ci	struct v4l2_ctrl *i2s_en;	/* I2S output enable */
2738c2ecf20Sopenharmony_ci	struct v4l2_ctrl *hsls;		/* High-side/Low-side polarity */
2748c2ecf20Sopenharmony_ci	struct v4l2_ctrl *rx_mode;	/* Receive mode */
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	/* Regmap */
2778c2ecf20Sopenharmony_ci	struct regmap *regmap;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	/* Cached configuration */
2808c2ecf20Sopenharmony_ci	u32 freq;			/* Tuned freq In Hz */
2818c2ecf20Sopenharmony_ci	const struct max2175_rxmode *rx_modes;		/* EU or NA modes */
2828c2ecf20Sopenharmony_ci	const struct v4l2_frequency_band *bands_rf;	/* EU or NA bands */
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* Device settings */
2858c2ecf20Sopenharmony_ci	unsigned long xtal_freq;	/* Ref Oscillator freq in Hz */
2868c2ecf20Sopenharmony_ci	u32 decim_ratio;
2878c2ecf20Sopenharmony_ci	bool master;			/* Master/Slave */
2888c2ecf20Sopenharmony_ci	bool am_hiz;			/* AM Hi-Z filter */
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	/* ROM values */
2918c2ecf20Sopenharmony_ci	u8 rom_bbf_bw_am;
2928c2ecf20Sopenharmony_ci	u8 rom_bbf_bw_fm;
2938c2ecf20Sopenharmony_ci	u8 rom_bbf_bw_dab;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	/* Driver private variables */
2968c2ecf20Sopenharmony_ci	bool mode_resolved;		/* Flag to sanity check settings */
2978c2ecf20Sopenharmony_ci};
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic inline struct max2175 *max2175_from_sd(struct v4l2_subdev *sd)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	return container_of(sd, struct max2175, sd);
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic inline struct max2175 *max2175_from_ctrl_hdl(struct v4l2_ctrl_handler *h)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	return container_of(h, struct max2175, ctrl_hdl);
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci/* Get bitval of a given val */
3108c2ecf20Sopenharmony_cistatic inline u8 max2175_get_bitval(u8 val, u8 msb, u8 lsb)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	return (val & GENMASK(msb, lsb)) >> lsb;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci/* Read/Write bit(s) on top of regmap */
3168c2ecf20Sopenharmony_cistatic int max2175_read(struct max2175 *ctx, u8 idx, u8 *val)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	u32 regval;
3198c2ecf20Sopenharmony_ci	int ret;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	ret = regmap_read(ctx->regmap, idx, &regval);
3228c2ecf20Sopenharmony_ci	if (ret)
3238c2ecf20Sopenharmony_ci		mxm_err(ctx, "read ret(%d): idx 0x%02x\n", ret, idx);
3248c2ecf20Sopenharmony_ci	else
3258c2ecf20Sopenharmony_ci		*val = regval;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	return ret;
3288c2ecf20Sopenharmony_ci}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistatic int max2175_write(struct max2175 *ctx, u8 idx, u8 val)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	int ret;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	ret = regmap_write(ctx->regmap, idx, val);
3358c2ecf20Sopenharmony_ci	if (ret)
3368c2ecf20Sopenharmony_ci		mxm_err(ctx, "write ret(%d): idx 0x%02x val 0x%02x\n",
3378c2ecf20Sopenharmony_ci			ret, idx, val);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	return ret;
3408c2ecf20Sopenharmony_ci}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_cistatic u8 max2175_read_bits(struct max2175 *ctx, u8 idx, u8 msb, u8 lsb)
3438c2ecf20Sopenharmony_ci{
3448c2ecf20Sopenharmony_ci	u8 val;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	if (max2175_read(ctx, idx, &val))
3478c2ecf20Sopenharmony_ci		return 0;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	return max2175_get_bitval(val, msb, lsb);
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic int max2175_write_bits(struct max2175 *ctx, u8 idx,
3538c2ecf20Sopenharmony_ci			     u8 msb, u8 lsb, u8 newval)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	int ret = regmap_update_bits(ctx->regmap, idx, GENMASK(msb, lsb),
3568c2ecf20Sopenharmony_ci				     newval << lsb);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	if (ret)
3598c2ecf20Sopenharmony_ci		mxm_err(ctx, "wbits ret(%d): idx 0x%02x\n", ret, idx);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	return ret;
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cistatic int max2175_write_bit(struct max2175 *ctx, u8 idx, u8 bit, u8 newval)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	return max2175_write_bits(ctx, idx, bit, bit, newval);
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci/* Checks expected pattern every msec until timeout */
3708c2ecf20Sopenharmony_cistatic int max2175_poll_timeout(struct max2175 *ctx, u8 idx, u8 msb, u8 lsb,
3718c2ecf20Sopenharmony_ci				u8 exp_bitval, u32 timeout_us)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	unsigned int val;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	return regmap_read_poll_timeout(ctx->regmap, idx, val,
3768c2ecf20Sopenharmony_ci			(max2175_get_bitval(val, msb, lsb) == exp_bitval),
3778c2ecf20Sopenharmony_ci			1000, timeout_us);
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic int max2175_poll_csm_ready(struct max2175 *ctx)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	int ret;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	ret = max2175_poll_timeout(ctx, 69, 1, 1, 0, 50000);
3858c2ecf20Sopenharmony_ci	if (ret)
3868c2ecf20Sopenharmony_ci		mxm_err(ctx, "csm not ready\n");
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	return ret;
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci#define MAX2175_IS_BAND_AM(ctx)		\
3928c2ecf20Sopenharmony_ci	(max2175_read_bits(ctx, 5, 1, 0) == MAX2175_BAND_AM)
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci#define MAX2175_IS_BAND_VHF(ctx)	\
3958c2ecf20Sopenharmony_ci	(max2175_read_bits(ctx, 5, 1, 0) == MAX2175_BAND_VHF)
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci#define MAX2175_IS_FM_MODE(ctx)		\
3988c2ecf20Sopenharmony_ci	(max2175_read_bits(ctx, 12, 5, 4) == 0)
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci#define MAX2175_IS_FMHD_MODE(ctx)	\
4018c2ecf20Sopenharmony_ci	(max2175_read_bits(ctx, 12, 5, 4) == 1)
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci#define MAX2175_IS_DAB_MODE(ctx)	\
4048c2ecf20Sopenharmony_ci	(max2175_read_bits(ctx, 12, 5, 4) == 2)
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic int max2175_band_from_freq(u32 freq)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	if (freq >= 144000 && freq <= 26100000)
4098c2ecf20Sopenharmony_ci		return MAX2175_BAND_AM;
4108c2ecf20Sopenharmony_ci	else if (freq >= 65000000 && freq <= 108000000)
4118c2ecf20Sopenharmony_ci		return MAX2175_BAND_FM;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	return MAX2175_BAND_VHF;
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistatic void max2175_i2s_enable(struct max2175 *ctx, bool enable)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	if (enable)
4198c2ecf20Sopenharmony_ci		/* Stuff bits are zeroed */
4208c2ecf20Sopenharmony_ci		max2175_write_bits(ctx, 104, 3, 0, 2);
4218c2ecf20Sopenharmony_ci	else
4228c2ecf20Sopenharmony_ci		/* Keep SCK alive */
4238c2ecf20Sopenharmony_ci		max2175_write_bits(ctx, 104, 3, 0, 9);
4248c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "i2s %sabled\n", enable ? "en" : "dis");
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic void max2175_set_filter_coeffs(struct max2175 *ctx, u8 m_sel,
4288c2ecf20Sopenharmony_ci				      u8 bank, const u16 *coeffs)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	unsigned int i;
4318c2ecf20Sopenharmony_ci	u8 coeff_addr, upper_address = 24;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "set_filter_coeffs: m_sel %d bank %d\n", m_sel, bank);
4348c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 114, 5, 4, m_sel);
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	if (m_sel == 2)
4378c2ecf20Sopenharmony_ci		upper_address = 12;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	for (i = 0; i < upper_address; i++) {
4408c2ecf20Sopenharmony_ci		coeff_addr = i + bank * 24;
4418c2ecf20Sopenharmony_ci		max2175_write(ctx, 115, coeffs[i] >> 8);
4428c2ecf20Sopenharmony_ci		max2175_write(ctx, 116, coeffs[i]);
4438c2ecf20Sopenharmony_ci		max2175_write(ctx, 117, coeff_addr | 1 << 7);
4448c2ecf20Sopenharmony_ci	}
4458c2ecf20Sopenharmony_ci	max2175_write_bit(ctx, 117, 7, 0);
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cistatic void max2175_load_fmeu_1p2(struct max2175 *ctx)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	unsigned int i;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(fmeu1p2_map); i++)
4538c2ecf20Sopenharmony_ci		max2175_write(ctx, fmeu1p2_map[i].idx, fmeu1p2_map[i].val);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	ctx->decim_ratio = 36;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	/* Load the Channel Filter Coefficients into channel filter bank #2 */
4588c2ecf20Sopenharmony_ci	max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0, ch_coeff_fmeu);
4598c2ecf20Sopenharmony_ci	max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
4608c2ecf20Sopenharmony_ci				  eq_coeff_fmeu1_ra02_m6db);
4618c2ecf20Sopenharmony_ci}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_cistatic void max2175_load_dab_1p2(struct max2175 *ctx)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	unsigned int i;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(dab12_map); i++)
4688c2ecf20Sopenharmony_ci		max2175_write(ctx, dab12_map[i].idx, dab12_map[i].val);
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	ctx->decim_ratio = 1;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	/* Load the Channel Filter Coefficients into channel filter bank #2 */
4738c2ecf20Sopenharmony_ci	max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 2, ch_coeff_dab1);
4748c2ecf20Sopenharmony_ci}
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_cistatic void max2175_load_fmna_1p0(struct max2175 *ctx)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	unsigned int i;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(fmna1p0_map); i++)
4818c2ecf20Sopenharmony_ci		max2175_write(ctx, fmna1p0_map[i].idx, fmna1p0_map[i].val);
4828c2ecf20Sopenharmony_ci}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_cistatic void max2175_load_fmna_2p0(struct max2175 *ctx)
4858c2ecf20Sopenharmony_ci{
4868c2ecf20Sopenharmony_ci	unsigned int i;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(fmna2p0_map); i++)
4898c2ecf20Sopenharmony_ci		max2175_write(ctx, fmna2p0_map[i].idx, fmna2p0_map[i].val);
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic void max2175_set_bbfilter(struct max2175 *ctx)
4938c2ecf20Sopenharmony_ci{
4948c2ecf20Sopenharmony_ci	if (MAX2175_IS_BAND_AM(ctx)) {
4958c2ecf20Sopenharmony_ci		max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_am);
4968c2ecf20Sopenharmony_ci		mxm_dbg(ctx, "set_bbfilter AM: rom %d\n", ctx->rom_bbf_bw_am);
4978c2ecf20Sopenharmony_ci	} else if (MAX2175_IS_DAB_MODE(ctx)) {
4988c2ecf20Sopenharmony_ci		max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_dab);
4998c2ecf20Sopenharmony_ci		mxm_dbg(ctx, "set_bbfilter DAB: rom %d\n", ctx->rom_bbf_bw_dab);
5008c2ecf20Sopenharmony_ci	} else {
5018c2ecf20Sopenharmony_ci		max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_fm);
5028c2ecf20Sopenharmony_ci		mxm_dbg(ctx, "set_bbfilter FM: rom %d\n", ctx->rom_bbf_bw_fm);
5038c2ecf20Sopenharmony_ci	}
5048c2ecf20Sopenharmony_ci}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_cistatic int max2175_set_csm_mode(struct max2175 *ctx,
5078c2ecf20Sopenharmony_ci			  enum max2175_csm_mode new_mode)
5088c2ecf20Sopenharmony_ci{
5098c2ecf20Sopenharmony_ci	int ret = max2175_poll_csm_ready(ctx);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	if (ret)
5128c2ecf20Sopenharmony_ci		return ret;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 0, 2, 0, new_mode);
5158c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "set csm new mode %d\n", new_mode);
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	/* Wait for a fixed settle down time depending on new mode */
5188c2ecf20Sopenharmony_ci	switch (new_mode) {
5198c2ecf20Sopenharmony_ci	case MAX2175_PRESET_TUNE:
5208c2ecf20Sopenharmony_ci		usleep_range(51100, 51500);	/* 51.1ms */
5218c2ecf20Sopenharmony_ci		break;
5228c2ecf20Sopenharmony_ci	/*
5238c2ecf20Sopenharmony_ci	 * Other mode switches need different sleep values depending on band &
5248c2ecf20Sopenharmony_ci	 * mode
5258c2ecf20Sopenharmony_ci	 */
5268c2ecf20Sopenharmony_ci	default:
5278c2ecf20Sopenharmony_ci		break;
5288c2ecf20Sopenharmony_ci	}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	return max2175_poll_csm_ready(ctx);
5318c2ecf20Sopenharmony_ci}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistatic int max2175_csm_action(struct max2175 *ctx,
5348c2ecf20Sopenharmony_ci			      enum max2175_csm_mode action)
5358c2ecf20Sopenharmony_ci{
5368c2ecf20Sopenharmony_ci	int ret;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "csm_action: %d\n", action);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	/* Other actions can be added in future when needed */
5418c2ecf20Sopenharmony_ci	ret = max2175_set_csm_mode(ctx, MAX2175_LOAD_TO_BUFFER);
5428c2ecf20Sopenharmony_ci	if (ret)
5438c2ecf20Sopenharmony_ci		return ret;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	return max2175_set_csm_mode(ctx, MAX2175_PRESET_TUNE);
5468c2ecf20Sopenharmony_ci}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_cistatic int max2175_set_lo_freq(struct max2175 *ctx, u32 lo_freq)
5498c2ecf20Sopenharmony_ci{
5508c2ecf20Sopenharmony_ci	u8 lo_mult, loband_bits = 0, vcodiv_bits = 0;
5518c2ecf20Sopenharmony_ci	u32 int_desired, frac_desired;
5528c2ecf20Sopenharmony_ci	enum max2175_band band;
5538c2ecf20Sopenharmony_ci	int ret;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	band = max2175_read_bits(ctx, 5, 1, 0);
5568c2ecf20Sopenharmony_ci	switch (band) {
5578c2ecf20Sopenharmony_ci	case MAX2175_BAND_AM:
5588c2ecf20Sopenharmony_ci		lo_mult = 16;
5598c2ecf20Sopenharmony_ci		break;
5608c2ecf20Sopenharmony_ci	case MAX2175_BAND_FM:
5618c2ecf20Sopenharmony_ci		if (lo_freq <= 74700000) {
5628c2ecf20Sopenharmony_ci			lo_mult = 16;
5638c2ecf20Sopenharmony_ci		} else if (lo_freq > 74700000 && lo_freq <= 110000000) {
5648c2ecf20Sopenharmony_ci			loband_bits = 1;
5658c2ecf20Sopenharmony_ci			lo_mult = 8;
5668c2ecf20Sopenharmony_ci		} else {
5678c2ecf20Sopenharmony_ci			loband_bits = 1;
5688c2ecf20Sopenharmony_ci			vcodiv_bits = 3;
5698c2ecf20Sopenharmony_ci			lo_mult = 8;
5708c2ecf20Sopenharmony_ci		}
5718c2ecf20Sopenharmony_ci		break;
5728c2ecf20Sopenharmony_ci	case MAX2175_BAND_VHF:
5738c2ecf20Sopenharmony_ci		if (lo_freq <= 210000000)
5748c2ecf20Sopenharmony_ci			vcodiv_bits = 2;
5758c2ecf20Sopenharmony_ci		else
5768c2ecf20Sopenharmony_ci			vcodiv_bits = 1;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci		loband_bits = 2;
5798c2ecf20Sopenharmony_ci		lo_mult = 4;
5808c2ecf20Sopenharmony_ci		break;
5818c2ecf20Sopenharmony_ci	default:
5828c2ecf20Sopenharmony_ci		loband_bits = 3;
5838c2ecf20Sopenharmony_ci		vcodiv_bits = 2;
5848c2ecf20Sopenharmony_ci		lo_mult = 2;
5858c2ecf20Sopenharmony_ci		break;
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	if (band == MAX2175_BAND_L)
5898c2ecf20Sopenharmony_ci		lo_freq /= lo_mult;
5908c2ecf20Sopenharmony_ci	else
5918c2ecf20Sopenharmony_ci		lo_freq *= lo_mult;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	int_desired = lo_freq / ctx->xtal_freq;
5948c2ecf20Sopenharmony_ci	frac_desired = div64_ul((u64)(lo_freq % ctx->xtal_freq) << 20,
5958c2ecf20Sopenharmony_ci				ctx->xtal_freq);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	/* Check CSM is not busy */
5988c2ecf20Sopenharmony_ci	ret = max2175_poll_csm_ready(ctx);
5998c2ecf20Sopenharmony_ci	if (ret)
6008c2ecf20Sopenharmony_ci		return ret;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "lo_mult %u int %u  frac %u\n",
6038c2ecf20Sopenharmony_ci		lo_mult, int_desired, frac_desired);
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	/* Write the calculated values to the appropriate registers */
6068c2ecf20Sopenharmony_ci	max2175_write(ctx, 1, int_desired);
6078c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 2, 3, 0, (frac_desired >> 16) & 0xf);
6088c2ecf20Sopenharmony_ci	max2175_write(ctx, 3, frac_desired >> 8);
6098c2ecf20Sopenharmony_ci	max2175_write(ctx, 4, frac_desired);
6108c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 5, 3, 2, loband_bits);
6118c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 6, 7, 6, vcodiv_bits);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	return ret;
6148c2ecf20Sopenharmony_ci}
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci/*
6178c2ecf20Sopenharmony_ci * Helper similar to DIV_ROUND_CLOSEST but an inline function that accepts s64
6188c2ecf20Sopenharmony_ci * dividend and s32 divisor
6198c2ecf20Sopenharmony_ci */
6208c2ecf20Sopenharmony_cistatic inline s64 max2175_round_closest(s64 dividend, s32 divisor)
6218c2ecf20Sopenharmony_ci{
6228c2ecf20Sopenharmony_ci	if ((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0))
6238c2ecf20Sopenharmony_ci		return div_s64(dividend + divisor / 2, divisor);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	return div_s64(dividend - divisor / 2, divisor);
6268c2ecf20Sopenharmony_ci}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_cistatic int max2175_set_nco_freq(struct max2175 *ctx, s32 nco_freq)
6298c2ecf20Sopenharmony_ci{
6308c2ecf20Sopenharmony_ci	s32 clock_rate = ctx->xtal_freq / ctx->decim_ratio;
6318c2ecf20Sopenharmony_ci	u32 nco_reg, abs_nco_freq = abs(nco_freq);
6328c2ecf20Sopenharmony_ci	s64 nco_val_desired;
6338c2ecf20Sopenharmony_ci	int ret;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	if (abs_nco_freq < clock_rate / 2) {
6368c2ecf20Sopenharmony_ci		nco_val_desired = 2 * nco_freq;
6378c2ecf20Sopenharmony_ci	} else {
6388c2ecf20Sopenharmony_ci		nco_val_desired = 2LL * (clock_rate - abs_nco_freq);
6398c2ecf20Sopenharmony_ci		if (nco_freq < 0)
6408c2ecf20Sopenharmony_ci			nco_val_desired = -nco_val_desired;
6418c2ecf20Sopenharmony_ci	}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	nco_reg = max2175_round_closest(nco_val_desired << 20, clock_rate);
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	if (nco_freq < 0)
6468c2ecf20Sopenharmony_ci		nco_reg += 0x200000;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	/* Check CSM is not busy */
6498c2ecf20Sopenharmony_ci	ret = max2175_poll_csm_ready(ctx);
6508c2ecf20Sopenharmony_ci	if (ret)
6518c2ecf20Sopenharmony_ci		return ret;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "freq %d desired %lld reg %u\n",
6548c2ecf20Sopenharmony_ci		nco_freq, nco_val_desired, nco_reg);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	/* Write the calculated values to the appropriate registers */
6578c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 7, 4, 0, (nco_reg >> 16) & 0x1f);
6588c2ecf20Sopenharmony_ci	max2175_write(ctx, 8, nco_reg >> 8);
6598c2ecf20Sopenharmony_ci	max2175_write(ctx, 9, nco_reg);
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	return ret;
6628c2ecf20Sopenharmony_ci}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_cistatic int max2175_set_rf_freq_non_am_bands(struct max2175 *ctx, u64 freq,
6658c2ecf20Sopenharmony_ci					    u32 lo_pos)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	s64 adj_freq, low_if_freq;
6688c2ecf20Sopenharmony_ci	int ret;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "rf_freq: non AM bands\n");
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	if (MAX2175_IS_FM_MODE(ctx))
6738c2ecf20Sopenharmony_ci		low_if_freq = 128000;
6748c2ecf20Sopenharmony_ci	else if (MAX2175_IS_FMHD_MODE(ctx))
6758c2ecf20Sopenharmony_ci		low_if_freq = 228000;
6768c2ecf20Sopenharmony_ci	else
6778c2ecf20Sopenharmony_ci		return max2175_set_lo_freq(ctx, freq);
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	if (MAX2175_IS_BAND_VHF(ctx) == (lo_pos == MAX2175_LO_ABOVE_DESIRED))
6808c2ecf20Sopenharmony_ci		adj_freq = freq + low_if_freq;
6818c2ecf20Sopenharmony_ci	else
6828c2ecf20Sopenharmony_ci		adj_freq = freq - low_if_freq;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	ret = max2175_set_lo_freq(ctx, adj_freq);
6858c2ecf20Sopenharmony_ci	if (ret)
6868c2ecf20Sopenharmony_ci		return ret;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	return max2175_set_nco_freq(ctx, -low_if_freq);
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_cistatic int max2175_set_rf_freq(struct max2175 *ctx, u64 freq, u32 lo_pos)
6928c2ecf20Sopenharmony_ci{
6938c2ecf20Sopenharmony_ci	int ret;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	if (MAX2175_IS_BAND_AM(ctx))
6968c2ecf20Sopenharmony_ci		ret = max2175_set_nco_freq(ctx, freq);
6978c2ecf20Sopenharmony_ci	else
6988c2ecf20Sopenharmony_ci		ret = max2175_set_rf_freq_non_am_bands(ctx, freq, lo_pos);
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "set_rf_freq: ret %d freq %llu\n", ret, freq);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	return ret;
7038c2ecf20Sopenharmony_ci}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_cistatic int max2175_tune_rf_freq(struct max2175 *ctx, u64 freq, u32 hsls)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci	int ret;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	ret = max2175_set_rf_freq(ctx, freq, hsls);
7108c2ecf20Sopenharmony_ci	if (ret)
7118c2ecf20Sopenharmony_ci		return ret;
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	ret = max2175_csm_action(ctx, MAX2175_BUFFER_PLUS_PRESET_TUNE);
7148c2ecf20Sopenharmony_ci	if (ret)
7158c2ecf20Sopenharmony_ci		return ret;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "tune_rf_freq: old %u new %llu\n", ctx->freq, freq);
7188c2ecf20Sopenharmony_ci	ctx->freq = freq;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	return ret;
7218c2ecf20Sopenharmony_ci}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_cistatic void max2175_set_hsls(struct max2175 *ctx, u32 lo_pos)
7248c2ecf20Sopenharmony_ci{
7258c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "set_hsls: lo_pos %u\n", lo_pos);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	if ((lo_pos == MAX2175_LO_BELOW_DESIRED) == MAX2175_IS_BAND_VHF(ctx))
7288c2ecf20Sopenharmony_ci		max2175_write_bit(ctx, 5, 4, 1);
7298c2ecf20Sopenharmony_ci	else
7308c2ecf20Sopenharmony_ci		max2175_write_bit(ctx, 5, 4, 0);
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_cistatic void max2175_set_eu_rx_mode(struct max2175 *ctx, u32 rx_mode)
7348c2ecf20Sopenharmony_ci{
7358c2ecf20Sopenharmony_ci	switch (rx_mode) {
7368c2ecf20Sopenharmony_ci	case MAX2175_EU_FM_1_2:
7378c2ecf20Sopenharmony_ci		max2175_load_fmeu_1p2(ctx);
7388c2ecf20Sopenharmony_ci		break;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	case MAX2175_DAB_1_2:
7418c2ecf20Sopenharmony_ci		max2175_load_dab_1p2(ctx);
7428c2ecf20Sopenharmony_ci		break;
7438c2ecf20Sopenharmony_ci	}
7448c2ecf20Sopenharmony_ci	/* Master is the default setting */
7458c2ecf20Sopenharmony_ci	if (!ctx->master)
7468c2ecf20Sopenharmony_ci		max2175_write_bit(ctx, 30, 7, 1);
7478c2ecf20Sopenharmony_ci}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_cistatic void max2175_set_na_rx_mode(struct max2175 *ctx, u32 rx_mode)
7508c2ecf20Sopenharmony_ci{
7518c2ecf20Sopenharmony_ci	switch (rx_mode) {
7528c2ecf20Sopenharmony_ci	case MAX2175_NA_FM_1_0:
7538c2ecf20Sopenharmony_ci		max2175_load_fmna_1p0(ctx);
7548c2ecf20Sopenharmony_ci		break;
7558c2ecf20Sopenharmony_ci	case MAX2175_NA_FM_2_0:
7568c2ecf20Sopenharmony_ci		max2175_load_fmna_2p0(ctx);
7578c2ecf20Sopenharmony_ci		break;
7588c2ecf20Sopenharmony_ci	}
7598c2ecf20Sopenharmony_ci	/* Master is the default setting */
7608c2ecf20Sopenharmony_ci	if (!ctx->master)
7618c2ecf20Sopenharmony_ci		max2175_write_bit(ctx, 30, 7, 1);
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	ctx->decim_ratio = 27;
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	/* Load the Channel Filter Coefficients into channel filter bank #2 */
7668c2ecf20Sopenharmony_ci	max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0, ch_coeff_fmna);
7678c2ecf20Sopenharmony_ci	max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
7688c2ecf20Sopenharmony_ci				  eq_coeff_fmna1_ra02_m6db);
7698c2ecf20Sopenharmony_ci}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_cistatic int max2175_set_rx_mode(struct max2175 *ctx, u32 rx_mode)
7728c2ecf20Sopenharmony_ci{
7738c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "set_rx_mode: %u am_hiz %u\n", rx_mode, ctx->am_hiz);
7748c2ecf20Sopenharmony_ci	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ)
7758c2ecf20Sopenharmony_ci		max2175_set_eu_rx_mode(ctx, rx_mode);
7768c2ecf20Sopenharmony_ci	else
7778c2ecf20Sopenharmony_ci		max2175_set_na_rx_mode(ctx, rx_mode);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	if (ctx->am_hiz) {
7808c2ecf20Sopenharmony_ci		mxm_dbg(ctx, "setting AM HiZ related config\n");
7818c2ecf20Sopenharmony_ci		max2175_write_bit(ctx, 50, 5, 1);
7828c2ecf20Sopenharmony_ci		max2175_write_bit(ctx, 90, 7, 1);
7838c2ecf20Sopenharmony_ci		max2175_write_bits(ctx, 73, 1, 0, 2);
7848c2ecf20Sopenharmony_ci		max2175_write_bits(ctx, 80, 5, 0, 33);
7858c2ecf20Sopenharmony_ci	}
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	/* Load BB filter trim values saved in ROM */
7888c2ecf20Sopenharmony_ci	max2175_set_bbfilter(ctx);
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	/* Set HSLS */
7918c2ecf20Sopenharmony_ci	max2175_set_hsls(ctx, ctx->hsls->cur.val);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	/* Use i2s enable settings */
7948c2ecf20Sopenharmony_ci	max2175_i2s_enable(ctx, ctx->i2s_en->cur.val);
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	ctx->mode_resolved = true;
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	return 0;
7998c2ecf20Sopenharmony_ci}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_cistatic int max2175_rx_mode_from_freq(struct max2175 *ctx, u32 freq, u32 *mode)
8028c2ecf20Sopenharmony_ci{
8038c2ecf20Sopenharmony_ci	unsigned int i;
8048c2ecf20Sopenharmony_ci	int band = max2175_band_from_freq(freq);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	/* Pick the first match always */
8078c2ecf20Sopenharmony_ci	for (i = 0; i <= ctx->rx_mode->maximum; i++) {
8088c2ecf20Sopenharmony_ci		if (ctx->rx_modes[i].band == band) {
8098c2ecf20Sopenharmony_ci			*mode = i;
8108c2ecf20Sopenharmony_ci			mxm_dbg(ctx, "rx_mode_from_freq: freq %u mode %d\n",
8118c2ecf20Sopenharmony_ci				freq, *mode);
8128c2ecf20Sopenharmony_ci			return 0;
8138c2ecf20Sopenharmony_ci		}
8148c2ecf20Sopenharmony_ci	}
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	return -EINVAL;
8178c2ecf20Sopenharmony_ci}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_cistatic bool max2175_freq_rx_mode_valid(struct max2175 *ctx,
8208c2ecf20Sopenharmony_ci					 u32 mode, u32 freq)
8218c2ecf20Sopenharmony_ci{
8228c2ecf20Sopenharmony_ci	int band = max2175_band_from_freq(freq);
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	return (ctx->rx_modes[mode].band == band);
8258c2ecf20Sopenharmony_ci}
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_cistatic void max2175_load_adc_presets(struct max2175 *ctx)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	unsigned int i, j;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(adc_presets); i++)
8328c2ecf20Sopenharmony_ci		for (j = 0; j < ARRAY_SIZE(adc_presets[0]); j++)
8338c2ecf20Sopenharmony_ci			max2175_write(ctx, 146 + j + i * 55, adc_presets[i][j]);
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_cistatic int max2175_init_power_manager(struct max2175 *ctx)
8378c2ecf20Sopenharmony_ci{
8388c2ecf20Sopenharmony_ci	int ret;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	/* Execute on-chip power-up/calibration */
8418c2ecf20Sopenharmony_ci	max2175_write_bit(ctx, 99, 2, 0);
8428c2ecf20Sopenharmony_ci	usleep_range(1000, 1500);
8438c2ecf20Sopenharmony_ci	max2175_write_bit(ctx, 99, 2, 1);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	/* Wait for the power manager to finish. */
8468c2ecf20Sopenharmony_ci	ret = max2175_poll_timeout(ctx, 69, 7, 7, 1, 50000);
8478c2ecf20Sopenharmony_ci	if (ret)
8488c2ecf20Sopenharmony_ci		mxm_err(ctx, "init pm failed\n");
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	return ret;
8518c2ecf20Sopenharmony_ci}
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_cistatic int max2175_recalibrate_adc(struct max2175 *ctx)
8548c2ecf20Sopenharmony_ci{
8558c2ecf20Sopenharmony_ci	int ret;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	/* ADC Re-calibration */
8588c2ecf20Sopenharmony_ci	max2175_write(ctx, 150, 0xff);
8598c2ecf20Sopenharmony_ci	max2175_write(ctx, 205, 0xff);
8608c2ecf20Sopenharmony_ci	max2175_write(ctx, 147, 0x20);
8618c2ecf20Sopenharmony_ci	max2175_write(ctx, 147, 0x00);
8628c2ecf20Sopenharmony_ci	max2175_write(ctx, 202, 0x20);
8638c2ecf20Sopenharmony_ci	max2175_write(ctx, 202, 0x00);
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	ret = max2175_poll_timeout(ctx, 69, 4, 3, 3, 50000);
8668c2ecf20Sopenharmony_ci	if (ret)
8678c2ecf20Sopenharmony_ci		mxm_err(ctx, "adc recalibration failed\n");
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	return ret;
8708c2ecf20Sopenharmony_ci}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_cistatic u8 max2175_read_rom(struct max2175 *ctx, u8 row)
8738c2ecf20Sopenharmony_ci{
8748c2ecf20Sopenharmony_ci	u8 data = 0;
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	max2175_write_bit(ctx, 56, 4, 0);
8778c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 56, 3, 0, row);
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	usleep_range(2000, 2500);
8808c2ecf20Sopenharmony_ci	max2175_read(ctx, 58, &data);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 56, 3, 0, 0);
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "read_rom: row %d data 0x%02x\n", row, data);
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	return data;
8878c2ecf20Sopenharmony_ci}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_cistatic void max2175_load_from_rom(struct max2175 *ctx)
8908c2ecf20Sopenharmony_ci{
8918c2ecf20Sopenharmony_ci	u8 data = 0;
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	data = max2175_read_rom(ctx, 0);
8948c2ecf20Sopenharmony_ci	ctx->rom_bbf_bw_am = data & 0x0f;
8958c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 81, 3, 0, data >> 4);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	data = max2175_read_rom(ctx, 1);
8988c2ecf20Sopenharmony_ci	ctx->rom_bbf_bw_fm = data & 0x0f;
8998c2ecf20Sopenharmony_ci	ctx->rom_bbf_bw_dab = data >> 4;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	data = max2175_read_rom(ctx, 2);
9028c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 82, 4, 0, data & 0x1f);
9038c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 82, 7, 5, data >> 5);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	data = max2175_read_rom(ctx, 3);
9068c2ecf20Sopenharmony_ci	if (ctx->am_hiz) {
9078c2ecf20Sopenharmony_ci		data &= 0x0f;
9088c2ecf20Sopenharmony_ci		data |= (max2175_read_rom(ctx, 7) & 0x40) >> 2;
9098c2ecf20Sopenharmony_ci		if (!data)
9108c2ecf20Sopenharmony_ci			data |= 2;
9118c2ecf20Sopenharmony_ci	} else {
9128c2ecf20Sopenharmony_ci		data = (data & 0xf0) >> 4;
9138c2ecf20Sopenharmony_ci		data |= (max2175_read_rom(ctx, 7) & 0x80) >> 3;
9148c2ecf20Sopenharmony_ci		if (!data)
9158c2ecf20Sopenharmony_ci			data |= 30;
9168c2ecf20Sopenharmony_ci	}
9178c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 80, 5, 0, data + 31);
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	data = max2175_read_rom(ctx, 6);
9208c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 81, 7, 6, data >> 6);
9218c2ecf20Sopenharmony_ci}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_cistatic void max2175_load_full_fm_eu_1p0(struct max2175 *ctx)
9248c2ecf20Sopenharmony_ci{
9258c2ecf20Sopenharmony_ci	unsigned int i;
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(full_fm_eu_1p0); i++)
9288c2ecf20Sopenharmony_ci		max2175_write(ctx, i + 1, full_fm_eu_1p0[i]);
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	usleep_range(5000, 5500);
9318c2ecf20Sopenharmony_ci	ctx->decim_ratio = 36;
9328c2ecf20Sopenharmony_ci}
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_cistatic void max2175_load_full_fm_na_1p0(struct max2175 *ctx)
9358c2ecf20Sopenharmony_ci{
9368c2ecf20Sopenharmony_ci	unsigned int i;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(full_fm_na_1p0); i++)
9398c2ecf20Sopenharmony_ci		max2175_write(ctx, i + 1, full_fm_na_1p0[i]);
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	usleep_range(5000, 5500);
9428c2ecf20Sopenharmony_ci	ctx->decim_ratio = 27;
9438c2ecf20Sopenharmony_ci}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_cistatic int max2175_core_init(struct max2175 *ctx, u32 refout_bits)
9468c2ecf20Sopenharmony_ci{
9478c2ecf20Sopenharmony_ci	int ret;
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	/* MAX2175 uses 36.864MHz clock for EU & 40.154MHz for NA region */
9508c2ecf20Sopenharmony_ci	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ)
9518c2ecf20Sopenharmony_ci		max2175_load_full_fm_eu_1p0(ctx);
9528c2ecf20Sopenharmony_ci	else
9538c2ecf20Sopenharmony_ci		max2175_load_full_fm_na_1p0(ctx);
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	/* The default settings assume master */
9568c2ecf20Sopenharmony_ci	if (!ctx->master)
9578c2ecf20Sopenharmony_ci		max2175_write_bit(ctx, 30, 7, 1);
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "refout_bits %u\n", refout_bits);
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	/* Set REFOUT */
9628c2ecf20Sopenharmony_ci	max2175_write_bits(ctx, 56, 7, 5, refout_bits);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	/* ADC Reset */
9658c2ecf20Sopenharmony_ci	max2175_write_bit(ctx, 99, 1, 0);
9668c2ecf20Sopenharmony_ci	usleep_range(1000, 1500);
9678c2ecf20Sopenharmony_ci	max2175_write_bit(ctx, 99, 1, 1);
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	/* Load ADC preset values */
9708c2ecf20Sopenharmony_ci	max2175_load_adc_presets(ctx);
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	/* Initialize the power management state machine */
9738c2ecf20Sopenharmony_ci	ret = max2175_init_power_manager(ctx);
9748c2ecf20Sopenharmony_ci	if (ret)
9758c2ecf20Sopenharmony_ci		return ret;
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci	/* Recalibrate ADC */
9788c2ecf20Sopenharmony_ci	ret = max2175_recalibrate_adc(ctx);
9798c2ecf20Sopenharmony_ci	if (ret)
9808c2ecf20Sopenharmony_ci		return ret;
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	/* Load ROM values to appropriate registers */
9838c2ecf20Sopenharmony_ci	max2175_load_from_rom(ctx);
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ) {
9868c2ecf20Sopenharmony_ci		/* Load FIR coefficients into bank 0 */
9878c2ecf20Sopenharmony_ci		max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0,
9888c2ecf20Sopenharmony_ci					  ch_coeff_fmeu);
9898c2ecf20Sopenharmony_ci		max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
9908c2ecf20Sopenharmony_ci					  eq_coeff_fmeu1_ra02_m6db);
9918c2ecf20Sopenharmony_ci	} else {
9928c2ecf20Sopenharmony_ci		/* Load FIR coefficients into bank 0 */
9938c2ecf20Sopenharmony_ci		max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0,
9948c2ecf20Sopenharmony_ci					  ch_coeff_fmna);
9958c2ecf20Sopenharmony_ci		max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
9968c2ecf20Sopenharmony_ci					  eq_coeff_fmna1_ra02_m6db);
9978c2ecf20Sopenharmony_ci	}
9988c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "core initialized\n");
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	return 0;
10018c2ecf20Sopenharmony_ci}
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_cistatic void max2175_s_ctrl_rx_mode(struct max2175 *ctx, u32 rx_mode)
10048c2ecf20Sopenharmony_ci{
10058c2ecf20Sopenharmony_ci	/* Load mode. Range check already done */
10068c2ecf20Sopenharmony_ci	max2175_set_rx_mode(ctx, rx_mode);
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "s_ctrl_rx_mode: %u curr freq %u\n", rx_mode, ctx->freq);
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	/* Check if current freq valid for mode & update */
10118c2ecf20Sopenharmony_ci	if (max2175_freq_rx_mode_valid(ctx, rx_mode, ctx->freq))
10128c2ecf20Sopenharmony_ci		max2175_tune_rf_freq(ctx, ctx->freq, ctx->hsls->cur.val);
10138c2ecf20Sopenharmony_ci	else
10148c2ecf20Sopenharmony_ci		/* Use default freq of mode if current freq is not valid */
10158c2ecf20Sopenharmony_ci		max2175_tune_rf_freq(ctx, ctx->rx_modes[rx_mode].freq,
10168c2ecf20Sopenharmony_ci				     ctx->hsls->cur.val);
10178c2ecf20Sopenharmony_ci}
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_cistatic int max2175_s_ctrl(struct v4l2_ctrl *ctrl)
10208c2ecf20Sopenharmony_ci{
10218c2ecf20Sopenharmony_ci	struct max2175 *ctx = max2175_from_ctrl_hdl(ctrl->handler);
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "s_ctrl: id 0x%x, val %u\n", ctrl->id, ctrl->val);
10248c2ecf20Sopenharmony_ci	switch (ctrl->id) {
10258c2ecf20Sopenharmony_ci	case V4L2_CID_MAX2175_I2S_ENABLE:
10268c2ecf20Sopenharmony_ci		max2175_i2s_enable(ctx, ctrl->val);
10278c2ecf20Sopenharmony_ci		break;
10288c2ecf20Sopenharmony_ci	case V4L2_CID_MAX2175_HSLS:
10298c2ecf20Sopenharmony_ci		max2175_set_hsls(ctx, ctrl->val);
10308c2ecf20Sopenharmony_ci		break;
10318c2ecf20Sopenharmony_ci	case V4L2_CID_MAX2175_RX_MODE:
10328c2ecf20Sopenharmony_ci		max2175_s_ctrl_rx_mode(ctx, ctrl->val);
10338c2ecf20Sopenharmony_ci		break;
10348c2ecf20Sopenharmony_ci	}
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	return 0;
10378c2ecf20Sopenharmony_ci}
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_cistatic u32 max2175_get_lna_gain(struct max2175 *ctx)
10408c2ecf20Sopenharmony_ci{
10418c2ecf20Sopenharmony_ci	enum max2175_band band = max2175_read_bits(ctx, 5, 1, 0);
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	switch (band) {
10448c2ecf20Sopenharmony_ci	case MAX2175_BAND_AM:
10458c2ecf20Sopenharmony_ci		return max2175_read_bits(ctx, 51, 3, 0);
10468c2ecf20Sopenharmony_ci	case MAX2175_BAND_FM:
10478c2ecf20Sopenharmony_ci		return max2175_read_bits(ctx, 50, 3, 0);
10488c2ecf20Sopenharmony_ci	case MAX2175_BAND_VHF:
10498c2ecf20Sopenharmony_ci		return max2175_read_bits(ctx, 52, 5, 0);
10508c2ecf20Sopenharmony_ci	default:
10518c2ecf20Sopenharmony_ci		return 0;
10528c2ecf20Sopenharmony_ci	}
10538c2ecf20Sopenharmony_ci}
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_cistatic int max2175_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
10568c2ecf20Sopenharmony_ci{
10578c2ecf20Sopenharmony_ci	struct max2175 *ctx = max2175_from_ctrl_hdl(ctrl->handler);
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	switch (ctrl->id) {
10608c2ecf20Sopenharmony_ci	case V4L2_CID_RF_TUNER_LNA_GAIN:
10618c2ecf20Sopenharmony_ci		ctrl->val = max2175_get_lna_gain(ctx);
10628c2ecf20Sopenharmony_ci		break;
10638c2ecf20Sopenharmony_ci	case V4L2_CID_RF_TUNER_IF_GAIN:
10648c2ecf20Sopenharmony_ci		ctrl->val = max2175_read_bits(ctx, 49, 4, 0);
10658c2ecf20Sopenharmony_ci		break;
10668c2ecf20Sopenharmony_ci	case V4L2_CID_RF_TUNER_PLL_LOCK:
10678c2ecf20Sopenharmony_ci		ctrl->val = (max2175_read_bits(ctx, 60, 7, 6) == 3);
10688c2ecf20Sopenharmony_ci		break;
10698c2ecf20Sopenharmony_ci	}
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	return 0;
10728c2ecf20Sopenharmony_ci};
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_cistatic int max2175_set_freq_and_mode(struct max2175 *ctx, u32 freq)
10758c2ecf20Sopenharmony_ci{
10768c2ecf20Sopenharmony_ci	u32 rx_mode;
10778c2ecf20Sopenharmony_ci	int ret;
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci	/* Get band from frequency */
10808c2ecf20Sopenharmony_ci	ret = max2175_rx_mode_from_freq(ctx, freq, &rx_mode);
10818c2ecf20Sopenharmony_ci	if (ret)
10828c2ecf20Sopenharmony_ci		return ret;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "set_freq_and_mode: freq %u rx_mode %d\n", freq, rx_mode);
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	/* Load mode */
10878c2ecf20Sopenharmony_ci	max2175_set_rx_mode(ctx, rx_mode);
10888c2ecf20Sopenharmony_ci	ctx->rx_mode->cur.val = rx_mode;
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	/* Tune to the new freq given */
10918c2ecf20Sopenharmony_ci	return max2175_tune_rf_freq(ctx, freq, ctx->hsls->cur.val);
10928c2ecf20Sopenharmony_ci}
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_cistatic int max2175_s_frequency(struct v4l2_subdev *sd,
10958c2ecf20Sopenharmony_ci			       const struct v4l2_frequency *vf)
10968c2ecf20Sopenharmony_ci{
10978c2ecf20Sopenharmony_ci	struct max2175 *ctx = max2175_from_sd(sd);
10988c2ecf20Sopenharmony_ci	u32 freq;
10998c2ecf20Sopenharmony_ci	int ret = 0;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "s_freq: new %u curr %u, mode_resolved %d\n",
11028c2ecf20Sopenharmony_ci		vf->frequency, ctx->freq, ctx->mode_resolved);
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	if (vf->tuner != 0)
11058c2ecf20Sopenharmony_ci		return -EINVAL;
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	freq = clamp(vf->frequency, ctx->bands_rf->rangelow,
11088c2ecf20Sopenharmony_ci		     ctx->bands_rf->rangehigh);
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	/* Check new freq valid for rx_mode if already resolved */
11118c2ecf20Sopenharmony_ci	if (ctx->mode_resolved &&
11128c2ecf20Sopenharmony_ci	    max2175_freq_rx_mode_valid(ctx, ctx->rx_mode->cur.val, freq))
11138c2ecf20Sopenharmony_ci		ret = max2175_tune_rf_freq(ctx, freq, ctx->hsls->cur.val);
11148c2ecf20Sopenharmony_ci	else
11158c2ecf20Sopenharmony_ci		/* Find default rx_mode for freq and tune to it */
11168c2ecf20Sopenharmony_ci		ret = max2175_set_freq_and_mode(ctx, freq);
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	mxm_dbg(ctx, "s_freq: ret %d curr %u mode_resolved %d mode %u\n",
11198c2ecf20Sopenharmony_ci		ret, ctx->freq, ctx->mode_resolved, ctx->rx_mode->cur.val);
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci	return ret;
11228c2ecf20Sopenharmony_ci}
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_cistatic int max2175_g_frequency(struct v4l2_subdev *sd,
11258c2ecf20Sopenharmony_ci			       struct v4l2_frequency *vf)
11268c2ecf20Sopenharmony_ci{
11278c2ecf20Sopenharmony_ci	struct max2175 *ctx = max2175_from_sd(sd);
11288c2ecf20Sopenharmony_ci	int ret = 0;
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci	if (vf->tuner != 0)
11318c2ecf20Sopenharmony_ci		return -EINVAL;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	/* RF freq */
11348c2ecf20Sopenharmony_ci	vf->type = V4L2_TUNER_RF;
11358c2ecf20Sopenharmony_ci	vf->frequency = ctx->freq;
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	return ret;
11388c2ecf20Sopenharmony_ci}
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_cistatic int max2175_enum_freq_bands(struct v4l2_subdev *sd,
11418c2ecf20Sopenharmony_ci			    struct v4l2_frequency_band *band)
11428c2ecf20Sopenharmony_ci{
11438c2ecf20Sopenharmony_ci	struct max2175 *ctx = max2175_from_sd(sd);
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	if (band->tuner != 0 || band->index != 0)
11468c2ecf20Sopenharmony_ci		return -EINVAL;
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	*band = *ctx->bands_rf;
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	return 0;
11518c2ecf20Sopenharmony_ci}
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_cistatic int max2175_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
11548c2ecf20Sopenharmony_ci{
11558c2ecf20Sopenharmony_ci	struct max2175 *ctx = max2175_from_sd(sd);
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	if (vt->index > 0)
11588c2ecf20Sopenharmony_ci		return -EINVAL;
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci	strscpy(vt->name, "RF", sizeof(vt->name));
11618c2ecf20Sopenharmony_ci	vt->type = V4L2_TUNER_RF;
11628c2ecf20Sopenharmony_ci	vt->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
11638c2ecf20Sopenharmony_ci	vt->rangelow = ctx->bands_rf->rangelow;
11648c2ecf20Sopenharmony_ci	vt->rangehigh = ctx->bands_rf->rangehigh;
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	return 0;
11678c2ecf20Sopenharmony_ci}
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_cistatic int max2175_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
11708c2ecf20Sopenharmony_ci{
11718c2ecf20Sopenharmony_ci	/* Check tuner index is valid */
11728c2ecf20Sopenharmony_ci	if (vt->index > 0)
11738c2ecf20Sopenharmony_ci		return -EINVAL;
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	return 0;
11768c2ecf20Sopenharmony_ci}
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_tuner_ops max2175_tuner_ops = {
11798c2ecf20Sopenharmony_ci	.s_frequency = max2175_s_frequency,
11808c2ecf20Sopenharmony_ci	.g_frequency = max2175_g_frequency,
11818c2ecf20Sopenharmony_ci	.enum_freq_bands = max2175_enum_freq_bands,
11828c2ecf20Sopenharmony_ci	.g_tuner = max2175_g_tuner,
11838c2ecf20Sopenharmony_ci	.s_tuner = max2175_s_tuner,
11848c2ecf20Sopenharmony_ci};
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops max2175_ops = {
11878c2ecf20Sopenharmony_ci	.tuner = &max2175_tuner_ops,
11888c2ecf20Sopenharmony_ci};
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops max2175_ctrl_ops = {
11918c2ecf20Sopenharmony_ci	.s_ctrl = max2175_s_ctrl,
11928c2ecf20Sopenharmony_ci	.g_volatile_ctrl = max2175_g_volatile_ctrl,
11938c2ecf20Sopenharmony_ci};
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci/*
11968c2ecf20Sopenharmony_ci * I2S output enable/disable configuration. This is a private control.
11978c2ecf20Sopenharmony_ci * Refer to Documentation/userspace-api/media/drivers/max2175.rst for more details.
11988c2ecf20Sopenharmony_ci */
11998c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_config max2175_i2s_en = {
12008c2ecf20Sopenharmony_ci	.ops = &max2175_ctrl_ops,
12018c2ecf20Sopenharmony_ci	.id = V4L2_CID_MAX2175_I2S_ENABLE,
12028c2ecf20Sopenharmony_ci	.name = "I2S Enable",
12038c2ecf20Sopenharmony_ci	.type = V4L2_CTRL_TYPE_BOOLEAN,
12048c2ecf20Sopenharmony_ci	.min = 0,
12058c2ecf20Sopenharmony_ci	.max = 1,
12068c2ecf20Sopenharmony_ci	.step = 1,
12078c2ecf20Sopenharmony_ci	.def = 1,
12088c2ecf20Sopenharmony_ci	.is_private = 1,
12098c2ecf20Sopenharmony_ci};
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci/*
12128c2ecf20Sopenharmony_ci * HSLS value control LO freq adjacent location configuration.
12138c2ecf20Sopenharmony_ci * Refer to Documentation/userspace-api/media/drivers/max2175.rst for more details.
12148c2ecf20Sopenharmony_ci */
12158c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_config max2175_hsls = {
12168c2ecf20Sopenharmony_ci	.ops = &max2175_ctrl_ops,
12178c2ecf20Sopenharmony_ci	.id = V4L2_CID_MAX2175_HSLS,
12188c2ecf20Sopenharmony_ci	.name = "HSLS Above/Below Desired",
12198c2ecf20Sopenharmony_ci	.type = V4L2_CTRL_TYPE_BOOLEAN,
12208c2ecf20Sopenharmony_ci	.min = 0,
12218c2ecf20Sopenharmony_ci	.max = 1,
12228c2ecf20Sopenharmony_ci	.step = 1,
12238c2ecf20Sopenharmony_ci	.def = 1,
12248c2ecf20Sopenharmony_ci};
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci/*
12278c2ecf20Sopenharmony_ci * Rx modes below are a set of preset configurations that decides the tuner's
12288c2ecf20Sopenharmony_ci * sck and sample rate of transmission. They are separate for EU & NA regions.
12298c2ecf20Sopenharmony_ci * Refer to Documentation/userspace-api/media/drivers/max2175.rst for more details.
12308c2ecf20Sopenharmony_ci */
12318c2ecf20Sopenharmony_cistatic const char * const max2175_ctrl_eu_rx_modes[] = {
12328c2ecf20Sopenharmony_ci	[MAX2175_EU_FM_1_2]	= "EU FM 1.2",
12338c2ecf20Sopenharmony_ci	[MAX2175_DAB_1_2]	= "DAB 1.2",
12348c2ecf20Sopenharmony_ci};
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_cistatic const char * const max2175_ctrl_na_rx_modes[] = {
12378c2ecf20Sopenharmony_ci	[MAX2175_NA_FM_1_0]	= "NA FM 1.0",
12388c2ecf20Sopenharmony_ci	[MAX2175_NA_FM_2_0]	= "NA FM 2.0",
12398c2ecf20Sopenharmony_ci};
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_config max2175_eu_rx_mode = {
12428c2ecf20Sopenharmony_ci	.ops = &max2175_ctrl_ops,
12438c2ecf20Sopenharmony_ci	.id = V4L2_CID_MAX2175_RX_MODE,
12448c2ecf20Sopenharmony_ci	.name = "RX Mode",
12458c2ecf20Sopenharmony_ci	.type = V4L2_CTRL_TYPE_MENU,
12468c2ecf20Sopenharmony_ci	.max = ARRAY_SIZE(max2175_ctrl_eu_rx_modes) - 1,
12478c2ecf20Sopenharmony_ci	.def = 0,
12488c2ecf20Sopenharmony_ci	.qmenu = max2175_ctrl_eu_rx_modes,
12498c2ecf20Sopenharmony_ci};
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_config max2175_na_rx_mode = {
12528c2ecf20Sopenharmony_ci	.ops = &max2175_ctrl_ops,
12538c2ecf20Sopenharmony_ci	.id = V4L2_CID_MAX2175_RX_MODE,
12548c2ecf20Sopenharmony_ci	.name = "RX Mode",
12558c2ecf20Sopenharmony_ci	.type = V4L2_CTRL_TYPE_MENU,
12568c2ecf20Sopenharmony_ci	.max = ARRAY_SIZE(max2175_ctrl_na_rx_modes) - 1,
12578c2ecf20Sopenharmony_ci	.def = 0,
12588c2ecf20Sopenharmony_ci	.qmenu = max2175_ctrl_na_rx_modes,
12598c2ecf20Sopenharmony_ci};
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_cistatic int max2175_refout_load_to_bits(struct i2c_client *client, u32 load,
12628c2ecf20Sopenharmony_ci				       u32 *bits)
12638c2ecf20Sopenharmony_ci{
12648c2ecf20Sopenharmony_ci	if (load <= 40)
12658c2ecf20Sopenharmony_ci		*bits = load / 10;
12668c2ecf20Sopenharmony_ci	else if (load >= 60 && load <= 70)
12678c2ecf20Sopenharmony_ci		*bits = load / 10 - 1;
12688c2ecf20Sopenharmony_ci	else
12698c2ecf20Sopenharmony_ci		return -EINVAL;
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci	return 0;
12728c2ecf20Sopenharmony_ci}
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_cistatic int max2175_probe(struct i2c_client *client)
12758c2ecf20Sopenharmony_ci{
12768c2ecf20Sopenharmony_ci	bool master = true, am_hiz = false;
12778c2ecf20Sopenharmony_ci	u32 refout_load, refout_bits = 0;	/* REFOUT disabled */
12788c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *hdl;
12798c2ecf20Sopenharmony_ci	struct fwnode_handle *fwnode;
12808c2ecf20Sopenharmony_ci	struct device_node *np;
12818c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
12828c2ecf20Sopenharmony_ci	struct regmap *regmap;
12838c2ecf20Sopenharmony_ci	struct max2175 *ctx;
12848c2ecf20Sopenharmony_ci	struct clk *clk;
12858c2ecf20Sopenharmony_ci	int ret;
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci	/* Parse DT properties */
12888c2ecf20Sopenharmony_ci	np = of_parse_phandle(client->dev.of_node, "maxim,master", 0);
12898c2ecf20Sopenharmony_ci	if (np) {
12908c2ecf20Sopenharmony_ci		master = false;			/* Slave tuner */
12918c2ecf20Sopenharmony_ci		of_node_put(np);
12928c2ecf20Sopenharmony_ci	}
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ci	fwnode = of_fwnode_handle(client->dev.of_node);
12958c2ecf20Sopenharmony_ci	if (fwnode_property_present(fwnode, "maxim,am-hiz-filter"))
12968c2ecf20Sopenharmony_ci		am_hiz = true;
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci	if (!fwnode_property_read_u32(fwnode, "maxim,refout-load",
12998c2ecf20Sopenharmony_ci				      &refout_load)) {
13008c2ecf20Sopenharmony_ci		ret = max2175_refout_load_to_bits(client, refout_load,
13018c2ecf20Sopenharmony_ci						  &refout_bits);
13028c2ecf20Sopenharmony_ci		if (ret) {
13038c2ecf20Sopenharmony_ci			dev_err(&client->dev, "invalid refout_load %u\n",
13048c2ecf20Sopenharmony_ci				refout_load);
13058c2ecf20Sopenharmony_ci			return -EINVAL;
13068c2ecf20Sopenharmony_ci		}
13078c2ecf20Sopenharmony_ci	}
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	clk = devm_clk_get(&client->dev, NULL);
13108c2ecf20Sopenharmony_ci	if (IS_ERR(clk)) {
13118c2ecf20Sopenharmony_ci		ret = PTR_ERR(clk);
13128c2ecf20Sopenharmony_ci		dev_err(&client->dev, "cannot get clock %d\n", ret);
13138c2ecf20Sopenharmony_ci		return ret;
13148c2ecf20Sopenharmony_ci	}
13158c2ecf20Sopenharmony_ci
13168c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_i2c(client, &max2175_regmap_config);
13178c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
13188c2ecf20Sopenharmony_ci		ret = PTR_ERR(regmap);
13198c2ecf20Sopenharmony_ci		dev_err(&client->dev, "regmap init failed %d\n", ret);
13208c2ecf20Sopenharmony_ci		return -ENODEV;
13218c2ecf20Sopenharmony_ci	}
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_ci	/* Alloc tuner context */
13248c2ecf20Sopenharmony_ci	ctx = devm_kzalloc(&client->dev, sizeof(*ctx), GFP_KERNEL);
13258c2ecf20Sopenharmony_ci	if (ctx == NULL)
13268c2ecf20Sopenharmony_ci		return -ENOMEM;
13278c2ecf20Sopenharmony_ci
13288c2ecf20Sopenharmony_ci	sd = &ctx->sd;
13298c2ecf20Sopenharmony_ci	ctx->master = master;
13308c2ecf20Sopenharmony_ci	ctx->am_hiz = am_hiz;
13318c2ecf20Sopenharmony_ci	ctx->mode_resolved = false;
13328c2ecf20Sopenharmony_ci	ctx->regmap = regmap;
13338c2ecf20Sopenharmony_ci	ctx->xtal_freq = clk_get_rate(clk);
13348c2ecf20Sopenharmony_ci	dev_info(&client->dev, "xtal freq %luHz\n", ctx->xtal_freq);
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &max2175_ops);
13378c2ecf20Sopenharmony_ci	ctx->client = client;
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	/* Controls */
13428c2ecf20Sopenharmony_ci	hdl = &ctx->ctrl_hdl;
13438c2ecf20Sopenharmony_ci	ret = v4l2_ctrl_handler_init(hdl, 7);
13448c2ecf20Sopenharmony_ci	if (ret)
13458c2ecf20Sopenharmony_ci		return ret;
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci	ctx->lna_gain = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
13488c2ecf20Sopenharmony_ci					  V4L2_CID_RF_TUNER_LNA_GAIN,
13498c2ecf20Sopenharmony_ci					  0, 63, 1, 0);
13508c2ecf20Sopenharmony_ci	ctx->lna_gain->flags |= (V4L2_CTRL_FLAG_VOLATILE |
13518c2ecf20Sopenharmony_ci				 V4L2_CTRL_FLAG_READ_ONLY);
13528c2ecf20Sopenharmony_ci	ctx->if_gain = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
13538c2ecf20Sopenharmony_ci					 V4L2_CID_RF_TUNER_IF_GAIN,
13548c2ecf20Sopenharmony_ci					 0, 31, 1, 0);
13558c2ecf20Sopenharmony_ci	ctx->if_gain->flags |= (V4L2_CTRL_FLAG_VOLATILE |
13568c2ecf20Sopenharmony_ci				V4L2_CTRL_FLAG_READ_ONLY);
13578c2ecf20Sopenharmony_ci	ctx->pll_lock = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
13588c2ecf20Sopenharmony_ci					  V4L2_CID_RF_TUNER_PLL_LOCK,
13598c2ecf20Sopenharmony_ci					  0, 1, 1, 0);
13608c2ecf20Sopenharmony_ci	ctx->pll_lock->flags |= (V4L2_CTRL_FLAG_VOLATILE |
13618c2ecf20Sopenharmony_ci				 V4L2_CTRL_FLAG_READ_ONLY);
13628c2ecf20Sopenharmony_ci	ctx->i2s_en = v4l2_ctrl_new_custom(hdl, &max2175_i2s_en, NULL);
13638c2ecf20Sopenharmony_ci	ctx->hsls = v4l2_ctrl_new_custom(hdl, &max2175_hsls, NULL);
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ) {
13668c2ecf20Sopenharmony_ci		ctx->rx_mode = v4l2_ctrl_new_custom(hdl,
13678c2ecf20Sopenharmony_ci						    &max2175_eu_rx_mode, NULL);
13688c2ecf20Sopenharmony_ci		ctx->rx_modes = eu_rx_modes;
13698c2ecf20Sopenharmony_ci		ctx->bands_rf = &eu_bands_rf;
13708c2ecf20Sopenharmony_ci	} else {
13718c2ecf20Sopenharmony_ci		ctx->rx_mode = v4l2_ctrl_new_custom(hdl,
13728c2ecf20Sopenharmony_ci						    &max2175_na_rx_mode, NULL);
13738c2ecf20Sopenharmony_ci		ctx->rx_modes = na_rx_modes;
13748c2ecf20Sopenharmony_ci		ctx->bands_rf = &na_bands_rf;
13758c2ecf20Sopenharmony_ci	}
13768c2ecf20Sopenharmony_ci	ctx->sd.ctrl_handler = &ctx->ctrl_hdl;
13778c2ecf20Sopenharmony_ci
13788c2ecf20Sopenharmony_ci	/* Set the defaults */
13798c2ecf20Sopenharmony_ci	ctx->freq = ctx->bands_rf->rangelow;
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	/* Register subdev */
13828c2ecf20Sopenharmony_ci	ret = v4l2_async_register_subdev(sd);
13838c2ecf20Sopenharmony_ci	if (ret) {
13848c2ecf20Sopenharmony_ci		dev_err(&client->dev, "register subdev failed\n");
13858c2ecf20Sopenharmony_ci		goto err_reg;
13868c2ecf20Sopenharmony_ci	}
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci	/* Initialize device */
13898c2ecf20Sopenharmony_ci	ret = max2175_core_init(ctx, refout_bits);
13908c2ecf20Sopenharmony_ci	if (ret)
13918c2ecf20Sopenharmony_ci		goto err_init;
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	ret = v4l2_ctrl_handler_setup(hdl);
13948c2ecf20Sopenharmony_ci	if (ret)
13958c2ecf20Sopenharmony_ci		goto err_init;
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci	return 0;
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_cierr_init:
14008c2ecf20Sopenharmony_ci	v4l2_async_unregister_subdev(sd);
14018c2ecf20Sopenharmony_cierr_reg:
14028c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci	return ret;
14058c2ecf20Sopenharmony_ci}
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_cistatic int max2175_remove(struct i2c_client *client)
14088c2ecf20Sopenharmony_ci{
14098c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
14108c2ecf20Sopenharmony_ci	struct max2175 *ctx = max2175_from_sd(sd);
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
14138c2ecf20Sopenharmony_ci	v4l2_async_unregister_subdev(sd);
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ci	return 0;
14168c2ecf20Sopenharmony_ci}
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_cistatic const struct i2c_device_id max2175_id[] = {
14198c2ecf20Sopenharmony_ci	{ DRIVER_NAME, 0},
14208c2ecf20Sopenharmony_ci	{},
14218c2ecf20Sopenharmony_ci};
14228c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max2175_id);
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_cistatic const struct of_device_id max2175_of_ids[] = {
14258c2ecf20Sopenharmony_ci	{ .compatible = "maxim,max2175", },
14268c2ecf20Sopenharmony_ci	{ }
14278c2ecf20Sopenharmony_ci};
14288c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, max2175_of_ids);
14298c2ecf20Sopenharmony_ci
14308c2ecf20Sopenharmony_cistatic struct i2c_driver max2175_driver = {
14318c2ecf20Sopenharmony_ci	.driver = {
14328c2ecf20Sopenharmony_ci		.name	= DRIVER_NAME,
14338c2ecf20Sopenharmony_ci		.of_match_table = max2175_of_ids,
14348c2ecf20Sopenharmony_ci	},
14358c2ecf20Sopenharmony_ci	.probe_new	= max2175_probe,
14368c2ecf20Sopenharmony_ci	.remove		= max2175_remove,
14378c2ecf20Sopenharmony_ci	.id_table	= max2175_id,
14388c2ecf20Sopenharmony_ci};
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_cimodule_i2c_driver(max2175_driver);
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Maxim MAX2175 RF to Bits tuner driver");
14438c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
14448c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>");
1445