<commit_before>/*
	STB0899 Multistandard Frontend driver
	Copyright (C) <NAME> (<EMAIL>)

	Copyright (C) ST Microelectronics

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "stb0899_drv.h"
#include "stb0899_priv.h"
#include "stb0899_reg.h"

/*
 * BinaryFloatDiv
 * float division with integer
 */
static long BinaryFloatDiv(long n1, long n2, int precision)
{
	int i = 0;
	long result = 0;

	while (i <= precision) {
		if (n1 < n2) {
			result *= 2;
			n1 *= 2;
		} else {
			result = result * 2 + 1;
			n1 = (n1 - n2) * 2;
		}
		i++;
	}

	return result;
}

/*
 * stb0899_calc_srate
 * Compute symbol rate
 */
static u32 stb0899_calc_srate(u32 master_clk, u8 *sfr)
{
	u32 tmp, tmp2, mclk;

	mclk  =  master_clk / 4096L; /* MasterClock * 10 / 2^20	*/
	tmp  = (((u32) sfr[0] << 12) + ((u32) sfr[1] << 4)) / 16;

	tmp *= mclk;
	tmp /= 16;
	tmp2 = ((u32) sfr[2] * mclk) / 256;
	tmp += tmp2;

	return tmp;
}

/*
 * stb0899_get_srate
 * Get the current symbol rate
 */
u32 stb0899_get_srate(struct stb0899_state *state)
{
	struct stb0899_internal *internal = &state->internal;
	u8 sfr[4];

	stb0899_read_regs(state, STB0899_SFRH, sfr, 3);

	return stb0899_calc_srate(internal->master_clk, sfr);
}

/*
 * stb0899_set_srate
 * Set symbol frequency
 * MasterClock: master clock frequency (hz)
 * SymbolRate: symbol rate (bauds)
 * return symbol frequency
 */
static u32 stb0899_set_srate(struct stb0899_state *state, u32 master_clk, u32 srate)
{
	u32 tmp, tmp_up, srate_up;
	u8 sfr_up[3], sfr[3];

//	srate_up = srate;
	dprintk(state->verbose, FE_DEBUG, 1, "-->");
	/*
	 * in order to have the maximum precision, the symbol rate entered into
	 * the chip is computed as the closest value of the "true value".
	 * In this purpose, the symbol rate value is rounded (1 is added on the bit
	 * below the LSB )
	 */
//	srate_up += (srate_up * 3) / 100;

	tmp = BinaryFloatDiv(srate, master_clk, 20);
//	tmp_up = BinaryFloatDiv(srate_up, master_clk, 20);

//	sfr_up[0] = (tmp_up >> 12) & 0xff;
//	sfr_up[1] = (tmp_up >>  4) & 0xff;
//	sfr_up[2] =  tmp_up & 0x0f;

	sfr[0] = (tmp >> 12) & 0xff;
	sfr[1] = (tmp >>  4) & 0xff;
	sfr[2] = (tmp <<  4) & 0xf0;

//	stb0899_write_regs(state, STB0899_SFRUPH, sfr_up, 3);
	stb0899_write_regs(state, STB0899_SFRH, sfr, 3);

	return srate;
}

/*
 * stb0899_calc_loop_time
 * Compute the amount of time needed by the timing loop to lock
 * SymbolRate: Symbol rate
 * return: timing loop time constant (ms)
 */
static long stb0899_calc_loop_time(long srate)
{
	if (srate > 0)
		return (100000 / (srate / 1000));
	else
		return 0;
}

/*
 * stb0899_calc_derot_time
 * Compute the amount of time needed by the derotator to lock<commit_msg>V4L/DVB (9449): Code Simplification: use do_div() instead<commit_after>/*
	STB0899 Multistandard Frontend driver
	Copyright (C) <NAME> (<EMAIL>)

	Copyright (C) ST Microelectronics

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "stb0899_drv.h"
#include "stb0899_priv.h"
#include "stb0899_reg.h"

inline u32 stb0899_do_div(u64 n, u32 d)
{
	/* wrap do_div() for ease of use */

	do_div(n, d);
	return n;
}

/*
 * stb0899_calc_srate
 * Compute symbol rate
 */
static u32 stb0899_calc_srate(u32 master_clk, u8 *sfr)
{
	u64 tmp;

	/* srate = (SFR * master_clk) >> 20 */

	/* sfr is of size 20 bit, stored with an offset of 4 bit */
	tmp = (((u32)sfr[0]) << 16) | (((u32)sfr[1]) << 8) | sfr[2];
	tmp &= ~0xf;
	tmp *= master_clk;
	tmp >>= 24;

	return tmp;
}

/*
 * stb0899_get_srate
 * Get the current symbol rate
 */
u32 stb0899_get_srate(struct stb0899_state *state)
{
	struct stb0899_internal *internal = &state->internal;
	u8 sfr[3];

	stb0899_read_regs(state, STB0899_SFRH, sfr, 3);

	return stb0899_calc_srate(internal->master_clk, sfr);
}

/*
 * stb0899_set_srate
 * Set symbol frequency
 * MasterClock: master clock frequency (hz)
 * SymbolRate: symbol rate (bauds)
 * return symbol frequency
 */
static u32 stb0899_set_srate(struct stb0899_state *state, u32 master_clk, u32 srate)
{
	u32 tmp, tmp_up, srate_up;
	u8 sfr_up[3], sfr[3];

//	srate_up = srate;
	dprintk(state->verbose, FE_DEBUG, 1, "-->");
	/*
	 * in order to have the maximum precision, the symbol rate entered into
	 * the chip is computed as the closest value of the "true value".
	 * In this purpose, the symbol rate value is rounded (1 is added on the bit
	 * below the LSB )
	 */
//	srate_up += (srate_up * 3) / 100;

	/*
	 * srate = (SFR * master_clk) >> 20
	 *      <=>
	 *   SFR = srate << 20 / master_clk
	 *
	 * rounded:
	 *   SFR = (srate << 21 + master_clk) / (2 * master_clk)
	 *
	 * stored as 20 bit number with an offset of 4 bit:
	 *   sfr = SFR << 4;
	 */
//	tmp_up = stb0899_do_div((((u64)srate_up) << 21) + master_clk, 2 * master_clk);
//	tmp_up <<= 4;

	tmp = stb0899_do_div((((u64)srate) << 21) + master_clk, 2 * master_clk);
	tmp <<= 4;

//	sfr_up[0] = tmp_up >> 16;
//	sfr_up[1] = tmp_up >>  8;
//	sfr_up[2] = tmp_up;

	sfr[0] = tmp >> 16;
	sfr[1] = tmp >>  8;
	sfr[2] = tmp;

//	stb0899_write_regs(state, STB0899_SFRUPH, sfr_up, 3);
	stb0899_write_regs(state, STB0899_SFRH, sfr, 3);

	return srate;
}

/*
 * stb0899_calc_loop_time
 * Compute the amount of time needed by the timing loop to lock
 * SymbolRate: Symbol rate
 * return: timing loop time constant (ms)
 */
static long stb0899_calc_loop_time(long srate)
{
	if (srate > 0)
		return (100000 / (srate / 1000));
	else
		return 0;
}

/*
 * stb0899_calc_derot_time
 * Compute the amount of time needed by the derotator to lock
{'fraction_non_alphanumeric': 0.11862130707251567, 'fraction_numerical': 0.06490599820948971, 'mean_word_length': 3.6516308119361556, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7546581', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2550, 'n_words': 826}