18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * bsru6.h - ALPS BSRU6 tuner support (moved from budget-ci.c)
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * the project's page is at https://linuxtv.org
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef BSRU6_H
98c2ecf20Sopenharmony_ci#define BSRU6_H
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistatic u8 alps_bsru6_inittab[] = {
128c2ecf20Sopenharmony_ci	0x01, 0x15,
138c2ecf20Sopenharmony_ci	0x02, 0x30,
148c2ecf20Sopenharmony_ci	0x03, 0x00,
158c2ecf20Sopenharmony_ci	0x04, 0x7d,   /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
168c2ecf20Sopenharmony_ci	0x05, 0x35,   /* I2CT = 0, SCLT = 1, SDAT = 1 */
178c2ecf20Sopenharmony_ci	0x06, 0x40,   /* DAC not used, set to high impendance mode */
188c2ecf20Sopenharmony_ci	0x07, 0x00,   /* DAC LSB */
198c2ecf20Sopenharmony_ci	0x08, 0x40,   /* DiSEqC off, LNB power on OP2/LOCK pin on */
208c2ecf20Sopenharmony_ci	0x09, 0x00,   /* FIFO */
218c2ecf20Sopenharmony_ci	0x0c, 0x51,   /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
228c2ecf20Sopenharmony_ci	0x0d, 0x82,   /* DC offset compensation = ON, beta_agc1 = 2 */
238c2ecf20Sopenharmony_ci	0x0e, 0x23,   /* alpha_tmg = 2, beta_tmg = 3 */
248c2ecf20Sopenharmony_ci	0x10, 0x3f,   // AGC2  0x3d
258c2ecf20Sopenharmony_ci	0x11, 0x84,
268c2ecf20Sopenharmony_ci	0x12, 0xb9,
278c2ecf20Sopenharmony_ci	0x15, 0xc9,   // lock detector threshold
288c2ecf20Sopenharmony_ci	0x16, 0x00,
298c2ecf20Sopenharmony_ci	0x17, 0x00,
308c2ecf20Sopenharmony_ci	0x18, 0x00,
318c2ecf20Sopenharmony_ci	0x19, 0x00,
328c2ecf20Sopenharmony_ci	0x1a, 0x00,
338c2ecf20Sopenharmony_ci	0x1f, 0x50,
348c2ecf20Sopenharmony_ci	0x20, 0x00,
358c2ecf20Sopenharmony_ci	0x21, 0x00,
368c2ecf20Sopenharmony_ci	0x22, 0x00,
378c2ecf20Sopenharmony_ci	0x23, 0x00,
388c2ecf20Sopenharmony_ci	0x28, 0x00,  // out imp: normal  out type: parallel FEC mode:0
398c2ecf20Sopenharmony_ci	0x29, 0x1e,  // 1/2 threshold
408c2ecf20Sopenharmony_ci	0x2a, 0x14,  // 2/3 threshold
418c2ecf20Sopenharmony_ci	0x2b, 0x0f,  // 3/4 threshold
428c2ecf20Sopenharmony_ci	0x2c, 0x09,  // 5/6 threshold
438c2ecf20Sopenharmony_ci	0x2d, 0x05,  // 7/8 threshold
448c2ecf20Sopenharmony_ci	0x2e, 0x01,
458c2ecf20Sopenharmony_ci	0x31, 0x1f,  // test all FECs
468c2ecf20Sopenharmony_ci	0x32, 0x19,  // viterbi and synchro search
478c2ecf20Sopenharmony_ci	0x33, 0xfc,  // rs control
488c2ecf20Sopenharmony_ci	0x34, 0x93,  // error control
498c2ecf20Sopenharmony_ci	0x0f, 0x52,
508c2ecf20Sopenharmony_ci	0xff, 0xff
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic int alps_bsru6_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	u8 aclk = 0;
568c2ecf20Sopenharmony_ci	u8 bclk = 0;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if (srate < 1500000) {
598c2ecf20Sopenharmony_ci		aclk = 0xb7;
608c2ecf20Sopenharmony_ci		bclk = 0x47;
618c2ecf20Sopenharmony_ci	} else if (srate < 3000000) {
628c2ecf20Sopenharmony_ci		aclk = 0xb7;
638c2ecf20Sopenharmony_ci		bclk = 0x4b;
648c2ecf20Sopenharmony_ci	} else if (srate < 7000000) {
658c2ecf20Sopenharmony_ci		aclk = 0xb7;
668c2ecf20Sopenharmony_ci		bclk = 0x4f;
678c2ecf20Sopenharmony_ci	} else if (srate < 14000000) {
688c2ecf20Sopenharmony_ci		aclk = 0xb7;
698c2ecf20Sopenharmony_ci		bclk = 0x53;
708c2ecf20Sopenharmony_ci	} else if (srate < 30000000) {
718c2ecf20Sopenharmony_ci		aclk = 0xb6;
728c2ecf20Sopenharmony_ci		bclk = 0x53;
738c2ecf20Sopenharmony_ci	} else if (srate < 45000000) {
748c2ecf20Sopenharmony_ci		aclk = 0xb4;
758c2ecf20Sopenharmony_ci		bclk = 0x51;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x13, aclk);
798c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x14, bclk);
808c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
818c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
828c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x21, ratio & 0xf0);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	return 0;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int alps_bsru6_tuner_set_params(struct dvb_frontend *fe)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
908c2ecf20Sopenharmony_ci	u8 buf[4];
918c2ecf20Sopenharmony_ci	u32 div;
928c2ecf20Sopenharmony_ci	struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = buf, .len = sizeof(buf) };
938c2ecf20Sopenharmony_ci	struct i2c_adapter *i2c = fe->tuner_priv;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	if ((p->frequency < 950000) || (p->frequency > 2150000))
968c2ecf20Sopenharmony_ci		return -EINVAL;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	div = (p->frequency + (125 - 1)) / 125;	/* round correctly */
998c2ecf20Sopenharmony_ci	buf[0] = (div >> 8) & 0x7f;
1008c2ecf20Sopenharmony_ci	buf[1] = div & 0xff;
1018c2ecf20Sopenharmony_ci	buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1028c2ecf20Sopenharmony_ci	buf[3] = 0xC4;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (p->frequency > 1530000)
1058c2ecf20Sopenharmony_ci		buf[3] = 0xc0;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
1088c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 1);
1098c2ecf20Sopenharmony_ci	if (i2c_transfer(i2c, &msg, 1) != 1)
1108c2ecf20Sopenharmony_ci		return -EIO;
1118c2ecf20Sopenharmony_ci	return 0;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic struct stv0299_config alps_bsru6_config = {
1158c2ecf20Sopenharmony_ci	.demod_address = 0x68,
1168c2ecf20Sopenharmony_ci	.inittab = alps_bsru6_inittab,
1178c2ecf20Sopenharmony_ci	.mclk = 88000000UL,
1188c2ecf20Sopenharmony_ci	.invert = 1,
1198c2ecf20Sopenharmony_ci	.skip_reinit = 0,
1208c2ecf20Sopenharmony_ci	.lock_output = STV0299_LOCKOUTPUT_1,
1218c2ecf20Sopenharmony_ci	.volt13_op0_op1 = STV0299_VOLT13_OP1,
1228c2ecf20Sopenharmony_ci	.min_delay_ms = 100,
1238c2ecf20Sopenharmony_ci	.set_symbol_rate = alps_bsru6_set_symbol_rate,
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#endif
127