18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * budget-av.c: driver for the SAA7146 based Budget DVB cards
48c2ecf20Sopenharmony_ci *              with analog video in
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Compiled from various sources by Michael Hunold <michael@mihu.de>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
98c2ecf20Sopenharmony_ci *                               Andrew de Quincey <adq_dvb@lidskialf.net>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Copyright (C) 1999-2002 Ralph  Metzler
148c2ecf20Sopenharmony_ci *                       & Marcus Metzler for convergence integrated media GmbH
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * the project's page is at https://linuxtv.org
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "budget.h"
228c2ecf20Sopenharmony_ci#include "stv0299.h"
238c2ecf20Sopenharmony_ci#include "stb0899_drv.h"
248c2ecf20Sopenharmony_ci#include "stb0899_reg.h"
258c2ecf20Sopenharmony_ci#include "stb0899_cfg.h"
268c2ecf20Sopenharmony_ci#include "tda8261.h"
278c2ecf20Sopenharmony_ci#include "tda8261_cfg.h"
288c2ecf20Sopenharmony_ci#include "tda1002x.h"
298c2ecf20Sopenharmony_ci#include "tda1004x.h"
308c2ecf20Sopenharmony_ci#include "tua6100.h"
318c2ecf20Sopenharmony_ci#include "dvb-pll.h"
328c2ecf20Sopenharmony_ci#include <media/drv-intf/saa7146_vv.h>
338c2ecf20Sopenharmony_ci#include <linux/module.h>
348c2ecf20Sopenharmony_ci#include <linux/errno.h>
358c2ecf20Sopenharmony_ci#include <linux/slab.h>
368c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
378c2ecf20Sopenharmony_ci#include <linux/input.h>
388c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#include <media/dvb_ca_en50221.h>
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define DEBICICAM		0x02420000
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define SLOTSTATUS_NONE         1
458c2ecf20Sopenharmony_ci#define SLOTSTATUS_PRESENT      2
468c2ecf20Sopenharmony_ci#define SLOTSTATUS_RESET        4
478c2ecf20Sopenharmony_ci#define SLOTSTATUS_READY        8
488c2ecf20Sopenharmony_ci#define SLOTSTATUS_OCCUPIED     (SLOTSTATUS_PRESENT|SLOTSTATUS_RESET|SLOTSTATUS_READY)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ciDVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistruct budget_av {
538c2ecf20Sopenharmony_ci	struct budget budget;
548c2ecf20Sopenharmony_ci	struct video_device vd;
558c2ecf20Sopenharmony_ci	int cur_input;
568c2ecf20Sopenharmony_ci	int has_saa7113;
578c2ecf20Sopenharmony_ci	struct tasklet_struct ciintf_irq_tasklet;
588c2ecf20Sopenharmony_ci	int slot_status;
598c2ecf20Sopenharmony_ci	struct dvb_ca_en50221 ca;
608c2ecf20Sopenharmony_ci	u8 reinitialise_demod:1;
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* GPIO Connections:
678c2ecf20Sopenharmony_ci * 0 - Vcc/Reset (Reset is controlled by capacitor). Resets the frontend *AS WELL*!
688c2ecf20Sopenharmony_ci * 1 - CI memory select 0=>IO memory, 1=>Attribute Memory
698c2ecf20Sopenharmony_ci * 2 - CI Card Enable (Active Low)
708c2ecf20Sopenharmony_ci * 3 - CI Card Detect
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/****************************************************************************
748c2ecf20Sopenharmony_ci * INITIALIZATION
758c2ecf20Sopenharmony_ci ****************************************************************************/
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic u8 i2c_readreg(struct i2c_adapter *i2c, u8 id, u8 reg)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	u8 mm1[] = { 0x00 };
808c2ecf20Sopenharmony_ci	u8 mm2[] = { 0x00 };
818c2ecf20Sopenharmony_ci	struct i2c_msg msgs[2];
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	msgs[0].flags = 0;
848c2ecf20Sopenharmony_ci	msgs[1].flags = I2C_M_RD;
858c2ecf20Sopenharmony_ci	msgs[0].addr = msgs[1].addr = id / 2;
868c2ecf20Sopenharmony_ci	mm1[0] = reg;
878c2ecf20Sopenharmony_ci	msgs[0].len = 1;
888c2ecf20Sopenharmony_ci	msgs[1].len = 1;
898c2ecf20Sopenharmony_ci	msgs[0].buf = mm1;
908c2ecf20Sopenharmony_ci	msgs[1].buf = mm2;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	i2c_transfer(i2c, msgs, 2);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	return mm2[0];
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic int i2c_readregs(struct i2c_adapter *i2c, u8 id, u8 reg, u8 * buf, u8 len)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	u8 mm1[] = { reg };
1008c2ecf20Sopenharmony_ci	struct i2c_msg msgs[2] = {
1018c2ecf20Sopenharmony_ci		{.addr = id / 2,.flags = 0,.buf = mm1,.len = 1},
1028c2ecf20Sopenharmony_ci		{.addr = id / 2,.flags = I2C_M_RD,.buf = buf,.len = len}
1038c2ecf20Sopenharmony_ci	};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (i2c_transfer(i2c, msgs, 2) != 2)
1068c2ecf20Sopenharmony_ci		return -EIO;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return 0;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int i2c_writereg(struct i2c_adapter *i2c, u8 id, u8 reg, u8 val)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	u8 msg[2] = { reg, val };
1148c2ecf20Sopenharmony_ci	struct i2c_msg msgs;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	msgs.flags = 0;
1178c2ecf20Sopenharmony_ci	msgs.addr = id / 2;
1188c2ecf20Sopenharmony_ci	msgs.len = 2;
1198c2ecf20Sopenharmony_ci	msgs.buf = msg;
1208c2ecf20Sopenharmony_ci	return i2c_transfer(i2c, &msgs, 1);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic int ciintf_read_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *) ca->data;
1268c2ecf20Sopenharmony_ci	int result;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (slot != 0)
1298c2ecf20Sopenharmony_ci		return -EINVAL;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
1328c2ecf20Sopenharmony_ci	udelay(1);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 0xfff, 1, 0, 1);
1358c2ecf20Sopenharmony_ci	if (result == -ETIMEDOUT) {
1368c2ecf20Sopenharmony_ci		ciintf_slot_shutdown(ca, slot);
1378c2ecf20Sopenharmony_ci		pr_info("cam ejected 1\n");
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci	return result;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic int ciintf_write_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address, u8 value)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *) ca->data;
1458c2ecf20Sopenharmony_ci	int result;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	if (slot != 0)
1488c2ecf20Sopenharmony_ci		return -EINVAL;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
1518c2ecf20Sopenharmony_ci	udelay(1);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 0xfff, 1, value, 0, 1);
1548c2ecf20Sopenharmony_ci	if (result == -ETIMEDOUT) {
1558c2ecf20Sopenharmony_ci		ciintf_slot_shutdown(ca, slot);
1568c2ecf20Sopenharmony_ci		pr_info("cam ejected 2\n");
1578c2ecf20Sopenharmony_ci	}
1588c2ecf20Sopenharmony_ci	return result;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic int ciintf_read_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *) ca->data;
1648c2ecf20Sopenharmony_ci	int result;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	if (slot != 0)
1678c2ecf20Sopenharmony_ci		return -EINVAL;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
1708c2ecf20Sopenharmony_ci	udelay(1);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 3, 1, 0, 0);
1738c2ecf20Sopenharmony_ci	if (result == -ETIMEDOUT) {
1748c2ecf20Sopenharmony_ci		ciintf_slot_shutdown(ca, slot);
1758c2ecf20Sopenharmony_ci		pr_info("cam ejected 3\n");
1768c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci	return result;
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic int ciintf_write_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address, u8 value)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *) ca->data;
1848c2ecf20Sopenharmony_ci	int result;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	if (slot != 0)
1878c2ecf20Sopenharmony_ci		return -EINVAL;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
1908c2ecf20Sopenharmony_ci	udelay(1);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 3, 1, value, 0, 0);
1938c2ecf20Sopenharmony_ci	if (result == -ETIMEDOUT) {
1948c2ecf20Sopenharmony_ci		ciintf_slot_shutdown(ca, slot);
1958c2ecf20Sopenharmony_ci		pr_info("cam ejected 5\n");
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci	return result;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic int ciintf_slot_reset(struct dvb_ca_en50221 *ca, int slot)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *) ca->data;
2038c2ecf20Sopenharmony_ci	struct saa7146_dev *saa = budget_av->budget.dev;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	if (slot != 0)
2068c2ecf20Sopenharmony_ci		return -EINVAL;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	dprintk(1, "ciintf_slot_reset\n");
2098c2ecf20Sopenharmony_ci	budget_av->slot_status = SLOTSTATUS_RESET;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
2148c2ecf20Sopenharmony_ci	msleep(2);
2158c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO); /* Vcc on */
2168c2ecf20Sopenharmony_ci	msleep(20); /* 20 ms Vcc settling time */
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO); /* enable card */
2198c2ecf20Sopenharmony_ci	ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
2208c2ecf20Sopenharmony_ci	msleep(20);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	/* reinitialise the frontend if necessary */
2238c2ecf20Sopenharmony_ci	if (budget_av->reinitialise_demod)
2248c2ecf20Sopenharmony_ci		dvb_frontend_reinitialise(budget_av->budget.dvb_frontend);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	return 0;
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistatic int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *) ca->data;
2328c2ecf20Sopenharmony_ci	struct saa7146_dev *saa = budget_av->budget.dev;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	if (slot != 0)
2358c2ecf20Sopenharmony_ci		return -EINVAL;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	dprintk(1, "ciintf_slot_shutdown\n");
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
2408c2ecf20Sopenharmony_ci	budget_av->slot_status = SLOTSTATUS_NONE;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	return 0;
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic int ciintf_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *) ca->data;
2488c2ecf20Sopenharmony_ci	struct saa7146_dev *saa = budget_av->budget.dev;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (slot != 0)
2518c2ecf20Sopenharmony_ci		return -EINVAL;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	dprintk(1, "ciintf_slot_ts_enable: %d\n", budget_av->slot_status);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	return 0;
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic int ciintf_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *) ca->data;
2638c2ecf20Sopenharmony_ci	struct saa7146_dev *saa = budget_av->budget.dev;
2648c2ecf20Sopenharmony_ci	int result;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	if (slot != 0)
2678c2ecf20Sopenharmony_ci		return -EINVAL;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	/* test the card detect line - needs to be done carefully
2708c2ecf20Sopenharmony_ci	 * since it never goes high for some CAMs on this interface (e.g. topuptv) */
2718c2ecf20Sopenharmony_ci	if (budget_av->slot_status == SLOTSTATUS_NONE) {
2728c2ecf20Sopenharmony_ci		saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
2738c2ecf20Sopenharmony_ci		udelay(1);
2748c2ecf20Sopenharmony_ci		if (saa7146_read(saa, PSR) & MASK_06) {
2758c2ecf20Sopenharmony_ci			if (budget_av->slot_status == SLOTSTATUS_NONE) {
2768c2ecf20Sopenharmony_ci				budget_av->slot_status = SLOTSTATUS_PRESENT;
2778c2ecf20Sopenharmony_ci				pr_info("cam inserted A\n");
2788c2ecf20Sopenharmony_ci			}
2798c2ecf20Sopenharmony_ci		}
2808c2ecf20Sopenharmony_ci		saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	/* We also try and read from IO memory to work round the above detection bug. If
2848c2ecf20Sopenharmony_ci	 * there is no CAM, we will get a timeout. Only done if there is no cam
2858c2ecf20Sopenharmony_ci	 * present, since this test actually breaks some cams :(
2868c2ecf20Sopenharmony_ci	 *
2878c2ecf20Sopenharmony_ci	 * if the CI interface is not open, we also do the above test since we
2888c2ecf20Sopenharmony_ci	 * don't care if the cam has problems - we'll be resetting it on open() anyway */
2898c2ecf20Sopenharmony_ci	if ((budget_av->slot_status == SLOTSTATUS_NONE) || (!open)) {
2908c2ecf20Sopenharmony_ci		saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
2918c2ecf20Sopenharmony_ci		result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1);
2928c2ecf20Sopenharmony_ci		if ((result >= 0) && (budget_av->slot_status == SLOTSTATUS_NONE)) {
2938c2ecf20Sopenharmony_ci			budget_av->slot_status = SLOTSTATUS_PRESENT;
2948c2ecf20Sopenharmony_ci			pr_info("cam inserted B\n");
2958c2ecf20Sopenharmony_ci		} else if (result < 0) {
2968c2ecf20Sopenharmony_ci			if (budget_av->slot_status != SLOTSTATUS_NONE) {
2978c2ecf20Sopenharmony_ci				ciintf_slot_shutdown(ca, slot);
2988c2ecf20Sopenharmony_ci				pr_info("cam ejected 5\n");
2998c2ecf20Sopenharmony_ci				return 0;
3008c2ecf20Sopenharmony_ci			}
3018c2ecf20Sopenharmony_ci		}
3028c2ecf20Sopenharmony_ci	}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	/* read from attribute memory in reset/ready state to know when the CAM is ready */
3058c2ecf20Sopenharmony_ci	if (budget_av->slot_status == SLOTSTATUS_RESET) {
3068c2ecf20Sopenharmony_ci		result = ciintf_read_attribute_mem(ca, slot, 0);
3078c2ecf20Sopenharmony_ci		if (result == 0x1d) {
3088c2ecf20Sopenharmony_ci			budget_av->slot_status = SLOTSTATUS_READY;
3098c2ecf20Sopenharmony_ci		}
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/* work out correct return code */
3138c2ecf20Sopenharmony_ci	if (budget_av->slot_status != SLOTSTATUS_NONE) {
3148c2ecf20Sopenharmony_ci		if (budget_av->slot_status & SLOTSTATUS_READY) {
3158c2ecf20Sopenharmony_ci			return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
3168c2ecf20Sopenharmony_ci		}
3178c2ecf20Sopenharmony_ci		return DVB_CA_EN50221_POLL_CAM_PRESENT;
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci	return 0;
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic int ciintf_init(struct budget_av *budget_av)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	struct saa7146_dev *saa = budget_av->budget.dev;
3258c2ecf20Sopenharmony_ci	int result;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	memset(&budget_av->ca, 0, sizeof(struct dvb_ca_en50221));
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
3308c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
3318c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO);
3328c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	/* Enable DEBI pins */
3358c2ecf20Sopenharmony_ci	saa7146_write(saa, MC1, MASK_27 | MASK_11);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/* register CI interface */
3388c2ecf20Sopenharmony_ci	budget_av->ca.owner = THIS_MODULE;
3398c2ecf20Sopenharmony_ci	budget_av->ca.read_attribute_mem = ciintf_read_attribute_mem;
3408c2ecf20Sopenharmony_ci	budget_av->ca.write_attribute_mem = ciintf_write_attribute_mem;
3418c2ecf20Sopenharmony_ci	budget_av->ca.read_cam_control = ciintf_read_cam_control;
3428c2ecf20Sopenharmony_ci	budget_av->ca.write_cam_control = ciintf_write_cam_control;
3438c2ecf20Sopenharmony_ci	budget_av->ca.slot_reset = ciintf_slot_reset;
3448c2ecf20Sopenharmony_ci	budget_av->ca.slot_shutdown = ciintf_slot_shutdown;
3458c2ecf20Sopenharmony_ci	budget_av->ca.slot_ts_enable = ciintf_slot_ts_enable;
3468c2ecf20Sopenharmony_ci	budget_av->ca.poll_slot_status = ciintf_poll_slot_status;
3478c2ecf20Sopenharmony_ci	budget_av->ca.data = budget_av;
3488c2ecf20Sopenharmony_ci	budget_av->budget.ci_present = 1;
3498c2ecf20Sopenharmony_ci	budget_av->slot_status = SLOTSTATUS_NONE;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	if ((result = dvb_ca_en50221_init(&budget_av->budget.dvb_adapter,
3528c2ecf20Sopenharmony_ci					  &budget_av->ca, 0, 1)) != 0) {
3538c2ecf20Sopenharmony_ci		pr_err("ci initialisation failed\n");
3548c2ecf20Sopenharmony_ci		goto error;
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	pr_info("ci interface initialised\n");
3588c2ecf20Sopenharmony_ci	return 0;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cierror:
3618c2ecf20Sopenharmony_ci	saa7146_write(saa, MC1, MASK_27);
3628c2ecf20Sopenharmony_ci	return result;
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic void ciintf_deinit(struct budget_av *budget_av)
3668c2ecf20Sopenharmony_ci{
3678c2ecf20Sopenharmony_ci	struct saa7146_dev *saa = budget_av->budget.dev;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
3708c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 1, SAA7146_GPIO_INPUT);
3718c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
3728c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	/* release the CA device */
3758c2ecf20Sopenharmony_ci	dvb_ca_en50221_release(&budget_av->ca);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	/* disable DEBI pins */
3788c2ecf20Sopenharmony_ci	saa7146_write(saa, MC1, MASK_27);
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic const u8 saa7113_tab[] = {
3838c2ecf20Sopenharmony_ci	0x01, 0x08,
3848c2ecf20Sopenharmony_ci	0x02, 0xc0,
3858c2ecf20Sopenharmony_ci	0x03, 0x33,
3868c2ecf20Sopenharmony_ci	0x04, 0x00,
3878c2ecf20Sopenharmony_ci	0x05, 0x00,
3888c2ecf20Sopenharmony_ci	0x06, 0xeb,
3898c2ecf20Sopenharmony_ci	0x07, 0xe0,
3908c2ecf20Sopenharmony_ci	0x08, 0x28,
3918c2ecf20Sopenharmony_ci	0x09, 0x00,
3928c2ecf20Sopenharmony_ci	0x0a, 0x80,
3938c2ecf20Sopenharmony_ci	0x0b, 0x47,
3948c2ecf20Sopenharmony_ci	0x0c, 0x40,
3958c2ecf20Sopenharmony_ci	0x0d, 0x00,
3968c2ecf20Sopenharmony_ci	0x0e, 0x01,
3978c2ecf20Sopenharmony_ci	0x0f, 0x44,
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	0x10, 0x08,
4008c2ecf20Sopenharmony_ci	0x11, 0x0c,
4018c2ecf20Sopenharmony_ci	0x12, 0x7b,
4028c2ecf20Sopenharmony_ci	0x13, 0x00,
4038c2ecf20Sopenharmony_ci	0x15, 0x00, 0x16, 0x00, 0x17, 0x00,
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	0x57, 0xff,
4068c2ecf20Sopenharmony_ci	0x40, 0x82, 0x58, 0x00, 0x59, 0x54, 0x5a, 0x07,
4078c2ecf20Sopenharmony_ci	0x5b, 0x83, 0x5e, 0x00,
4088c2ecf20Sopenharmony_ci	0xff
4098c2ecf20Sopenharmony_ci};
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic int saa7113_init(struct budget_av *budget_av)
4128c2ecf20Sopenharmony_ci{
4138c2ecf20Sopenharmony_ci	struct budget *budget = &budget_av->budget;
4148c2ecf20Sopenharmony_ci	struct saa7146_dev *saa = budget->dev;
4158c2ecf20Sopenharmony_ci	const u8 *data = saa7113_tab;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI);
4188c2ecf20Sopenharmony_ci	msleep(200);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	if (i2c_writereg(&budget->i2c_adap, 0x4a, 0x01, 0x08) != 1) {
4218c2ecf20Sopenharmony_ci		dprintk(1, "saa7113 not found on KNC card\n");
4228c2ecf20Sopenharmony_ci		return -ENODEV;
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	dprintk(1, "saa7113 detected and initializing\n");
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	while (*data != 0xff) {
4288c2ecf20Sopenharmony_ci		i2c_writereg(&budget->i2c_adap, 0x4a, *data, *(data + 1));
4298c2ecf20Sopenharmony_ci		data += 2;
4308c2ecf20Sopenharmony_ci	}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	dprintk(1, "saa7113  status=%02x\n", i2c_readreg(&budget->i2c_adap, 0x4a, 0x1f));
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	return 0;
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_cistatic int saa7113_setinput(struct budget_av *budget_av, int input)
4388c2ecf20Sopenharmony_ci{
4398c2ecf20Sopenharmony_ci	struct budget *budget = &budget_av->budget;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	if (1 != budget_av->has_saa7113)
4428c2ecf20Sopenharmony_ci		return -ENODEV;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	if (input == 1) {
4458c2ecf20Sopenharmony_ci		i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc7);
4468c2ecf20Sopenharmony_ci		i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x80);
4478c2ecf20Sopenharmony_ci	} else if (input == 0) {
4488c2ecf20Sopenharmony_ci		i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc0);
4498c2ecf20Sopenharmony_ci		i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x00);
4508c2ecf20Sopenharmony_ci	} else
4518c2ecf20Sopenharmony_ci		return -EINVAL;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	budget_av->cur_input = input;
4548c2ecf20Sopenharmony_ci	return 0;
4558c2ecf20Sopenharmony_ci}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_cistatic int philips_su1278_ty_ci_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	u8 aclk = 0;
4618c2ecf20Sopenharmony_ci	u8 bclk = 0;
4628c2ecf20Sopenharmony_ci	u8 m1;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	aclk = 0xb5;
4658c2ecf20Sopenharmony_ci	if (srate < 2000000)
4668c2ecf20Sopenharmony_ci		bclk = 0x86;
4678c2ecf20Sopenharmony_ci	else if (srate < 5000000)
4688c2ecf20Sopenharmony_ci		bclk = 0x89;
4698c2ecf20Sopenharmony_ci	else if (srate < 15000000)
4708c2ecf20Sopenharmony_ci		bclk = 0x8f;
4718c2ecf20Sopenharmony_ci	else if (srate < 45000000)
4728c2ecf20Sopenharmony_ci		bclk = 0x95;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	m1 = 0x14;
4758c2ecf20Sopenharmony_ci	if (srate < 4000000)
4768c2ecf20Sopenharmony_ci		m1 = 0x10;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x13, aclk);
4798c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x14, bclk);
4808c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
4818c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
4828c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
4838c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x0f, 0x80 | m1);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	return 0;
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_cistatic int philips_su1278_ty_ci_tuner_set_params(struct dvb_frontend *fe)
4898c2ecf20Sopenharmony_ci{
4908c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
4918c2ecf20Sopenharmony_ci	u32 div;
4928c2ecf20Sopenharmony_ci	u8 buf[4];
4938c2ecf20Sopenharmony_ci	struct budget *budget = (struct budget *) fe->dvb->priv;
4948c2ecf20Sopenharmony_ci	struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	if ((c->frequency < 950000) || (c->frequency > 2150000))
4978c2ecf20Sopenharmony_ci		return -EINVAL;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	div = (c->frequency + (125 - 1)) / 125;	/* round correctly */
5008c2ecf20Sopenharmony_ci	buf[0] = (div >> 8) & 0x7f;
5018c2ecf20Sopenharmony_ci	buf[1] = div & 0xff;
5028c2ecf20Sopenharmony_ci	buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
5038c2ecf20Sopenharmony_ci	buf[3] = 0x20;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	if (c->symbol_rate < 4000000)
5068c2ecf20Sopenharmony_ci		buf[3] |= 1;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	if (c->frequency < 1250000)
5098c2ecf20Sopenharmony_ci		buf[3] |= 0;
5108c2ecf20Sopenharmony_ci	else if (c->frequency < 1550000)
5118c2ecf20Sopenharmony_ci		buf[3] |= 0x40;
5128c2ecf20Sopenharmony_ci	else if (c->frequency < 2050000)
5138c2ecf20Sopenharmony_ci		buf[3] |= 0x80;
5148c2ecf20Sopenharmony_ci	else if (c->frequency < 2150000)
5158c2ecf20Sopenharmony_ci		buf[3] |= 0xC0;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
5188c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 1);
5198c2ecf20Sopenharmony_ci	if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
5208c2ecf20Sopenharmony_ci		return -EIO;
5218c2ecf20Sopenharmony_ci	return 0;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic u8 typhoon_cinergy1200s_inittab[] = {
5258c2ecf20Sopenharmony_ci	0x01, 0x15,
5268c2ecf20Sopenharmony_ci	0x02, 0x30,
5278c2ecf20Sopenharmony_ci	0x03, 0x00,
5288c2ecf20Sopenharmony_ci	0x04, 0x7d,		/* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
5298c2ecf20Sopenharmony_ci	0x05, 0x35,		/* I2CT = 0, SCLT = 1, SDAT = 1 */
5308c2ecf20Sopenharmony_ci	0x06, 0x40,		/* DAC not used, set to high impendance mode */
5318c2ecf20Sopenharmony_ci	0x07, 0x00,		/* DAC LSB */
5328c2ecf20Sopenharmony_ci	0x08, 0x40,		/* DiSEqC off */
5338c2ecf20Sopenharmony_ci	0x09, 0x00,		/* FIFO */
5348c2ecf20Sopenharmony_ci	0x0c, 0x51,		/* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
5358c2ecf20Sopenharmony_ci	0x0d, 0x82,		/* DC offset compensation = ON, beta_agc1 = 2 */
5368c2ecf20Sopenharmony_ci	0x0e, 0x23,		/* alpha_tmg = 2, beta_tmg = 3 */
5378c2ecf20Sopenharmony_ci	0x10, 0x3f,		// AGC2  0x3d
5388c2ecf20Sopenharmony_ci	0x11, 0x84,
5398c2ecf20Sopenharmony_ci	0x12, 0xb9,
5408c2ecf20Sopenharmony_ci	0x15, 0xc9,		// lock detector threshold
5418c2ecf20Sopenharmony_ci	0x16, 0x00,
5428c2ecf20Sopenharmony_ci	0x17, 0x00,
5438c2ecf20Sopenharmony_ci	0x18, 0x00,
5448c2ecf20Sopenharmony_ci	0x19, 0x00,
5458c2ecf20Sopenharmony_ci	0x1a, 0x00,
5468c2ecf20Sopenharmony_ci	0x1f, 0x50,
5478c2ecf20Sopenharmony_ci	0x20, 0x00,
5488c2ecf20Sopenharmony_ci	0x21, 0x00,
5498c2ecf20Sopenharmony_ci	0x22, 0x00,
5508c2ecf20Sopenharmony_ci	0x23, 0x00,
5518c2ecf20Sopenharmony_ci	0x28, 0x00,		// out imp: normal  out type: parallel FEC mode:0
5528c2ecf20Sopenharmony_ci	0x29, 0x1e,		// 1/2 threshold
5538c2ecf20Sopenharmony_ci	0x2a, 0x14,		// 2/3 threshold
5548c2ecf20Sopenharmony_ci	0x2b, 0x0f,		// 3/4 threshold
5558c2ecf20Sopenharmony_ci	0x2c, 0x09,		// 5/6 threshold
5568c2ecf20Sopenharmony_ci	0x2d, 0x05,		// 7/8 threshold
5578c2ecf20Sopenharmony_ci	0x2e, 0x01,
5588c2ecf20Sopenharmony_ci	0x31, 0x1f,		// test all FECs
5598c2ecf20Sopenharmony_ci	0x32, 0x19,		// viterbi and synchro search
5608c2ecf20Sopenharmony_ci	0x33, 0xfc,		// rs control
5618c2ecf20Sopenharmony_ci	0x34, 0x93,		// error control
5628c2ecf20Sopenharmony_ci	0x0f, 0x92,
5638c2ecf20Sopenharmony_ci	0xff, 0xff
5648c2ecf20Sopenharmony_ci};
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_cistatic const struct stv0299_config typhoon_config = {
5678c2ecf20Sopenharmony_ci	.demod_address = 0x68,
5688c2ecf20Sopenharmony_ci	.inittab = typhoon_cinergy1200s_inittab,
5698c2ecf20Sopenharmony_ci	.mclk = 88000000UL,
5708c2ecf20Sopenharmony_ci	.invert = 0,
5718c2ecf20Sopenharmony_ci	.skip_reinit = 0,
5728c2ecf20Sopenharmony_ci	.lock_output = STV0299_LOCKOUTPUT_1,
5738c2ecf20Sopenharmony_ci	.volt13_op0_op1 = STV0299_VOLT13_OP0,
5748c2ecf20Sopenharmony_ci	.min_delay_ms = 100,
5758c2ecf20Sopenharmony_ci	.set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
5768c2ecf20Sopenharmony_ci};
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_cistatic const struct stv0299_config cinergy_1200s_config = {
5808c2ecf20Sopenharmony_ci	.demod_address = 0x68,
5818c2ecf20Sopenharmony_ci	.inittab = typhoon_cinergy1200s_inittab,
5828c2ecf20Sopenharmony_ci	.mclk = 88000000UL,
5838c2ecf20Sopenharmony_ci	.invert = 0,
5848c2ecf20Sopenharmony_ci	.skip_reinit = 0,
5858c2ecf20Sopenharmony_ci	.lock_output = STV0299_LOCKOUTPUT_0,
5868c2ecf20Sopenharmony_ci	.volt13_op0_op1 = STV0299_VOLT13_OP0,
5878c2ecf20Sopenharmony_ci	.min_delay_ms = 100,
5888c2ecf20Sopenharmony_ci	.set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
5898c2ecf20Sopenharmony_ci};
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_cistatic const struct stv0299_config cinergy_1200s_1894_0010_config = {
5928c2ecf20Sopenharmony_ci	.demod_address = 0x68,
5938c2ecf20Sopenharmony_ci	.inittab = typhoon_cinergy1200s_inittab,
5948c2ecf20Sopenharmony_ci	.mclk = 88000000UL,
5958c2ecf20Sopenharmony_ci	.invert = 1,
5968c2ecf20Sopenharmony_ci	.skip_reinit = 0,
5978c2ecf20Sopenharmony_ci	.lock_output = STV0299_LOCKOUTPUT_1,
5988c2ecf20Sopenharmony_ci	.volt13_op0_op1 = STV0299_VOLT13_OP0,
5998c2ecf20Sopenharmony_ci	.min_delay_ms = 100,
6008c2ecf20Sopenharmony_ci	.set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
6018c2ecf20Sopenharmony_ci};
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_cistatic int philips_cu1216_tuner_set_params(struct dvb_frontend *fe)
6048c2ecf20Sopenharmony_ci{
6058c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
6068c2ecf20Sopenharmony_ci	struct budget *budget = (struct budget *) fe->dvb->priv;
6078c2ecf20Sopenharmony_ci	u8 buf[6];
6088c2ecf20Sopenharmony_ci	struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = buf,.len = sizeof(buf) };
6098c2ecf20Sopenharmony_ci	int i;
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci#define CU1216_IF 36125000
6128c2ecf20Sopenharmony_ci#define TUNER_MUL 62500
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	u32 div = (c->frequency + CU1216_IF + TUNER_MUL / 2) / TUNER_MUL;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	buf[0] = (div >> 8) & 0x7f;
6178c2ecf20Sopenharmony_ci	buf[1] = div & 0xff;
6188c2ecf20Sopenharmony_ci	buf[2] = 0xce;
6198c2ecf20Sopenharmony_ci	buf[3] = (c->frequency < 150000000 ? 0x01 :
6208c2ecf20Sopenharmony_ci		  c->frequency < 445000000 ? 0x02 : 0x04);
6218c2ecf20Sopenharmony_ci	buf[4] = 0xde;
6228c2ecf20Sopenharmony_ci	buf[5] = 0x20;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
6258c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 1);
6268c2ecf20Sopenharmony_ci	if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
6278c2ecf20Sopenharmony_ci		return -EIO;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	/* wait for the pll lock */
6308c2ecf20Sopenharmony_ci	msg.flags = I2C_M_RD;
6318c2ecf20Sopenharmony_ci	msg.len = 1;
6328c2ecf20Sopenharmony_ci	for (i = 0; i < 20; i++) {
6338c2ecf20Sopenharmony_ci		if (fe->ops.i2c_gate_ctrl)
6348c2ecf20Sopenharmony_ci			fe->ops.i2c_gate_ctrl(fe, 1);
6358c2ecf20Sopenharmony_ci		if (i2c_transfer(&budget->i2c_adap, &msg, 1) == 1 && (buf[0] & 0x40))
6368c2ecf20Sopenharmony_ci			break;
6378c2ecf20Sopenharmony_ci		msleep(10);
6388c2ecf20Sopenharmony_ci	}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	/* switch the charge pump to the lower current */
6418c2ecf20Sopenharmony_ci	msg.flags = 0;
6428c2ecf20Sopenharmony_ci	msg.len = 2;
6438c2ecf20Sopenharmony_ci	msg.buf = &buf[2];
6448c2ecf20Sopenharmony_ci	buf[2] &= ~0x40;
6458c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
6468c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 1);
6478c2ecf20Sopenharmony_ci	if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
6488c2ecf20Sopenharmony_ci		return -EIO;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	return 0;
6518c2ecf20Sopenharmony_ci}
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_cistatic struct tda1002x_config philips_cu1216_config = {
6548c2ecf20Sopenharmony_ci	.demod_address = 0x0c,
6558c2ecf20Sopenharmony_ci	.invert = 1,
6568c2ecf20Sopenharmony_ci};
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_cistatic struct tda1002x_config philips_cu1216_config_altaddress = {
6598c2ecf20Sopenharmony_ci	.demod_address = 0x0d,
6608c2ecf20Sopenharmony_ci	.invert = 0,
6618c2ecf20Sopenharmony_ci};
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_cistatic struct tda10023_config philips_cu1216_tda10023_config = {
6648c2ecf20Sopenharmony_ci	.demod_address = 0x0c,
6658c2ecf20Sopenharmony_ci	.invert = 1,
6668c2ecf20Sopenharmony_ci};
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic int philips_tu1216_tuner_init(struct dvb_frontend *fe)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	struct budget *budget = (struct budget *) fe->dvb->priv;
6718c2ecf20Sopenharmony_ci	static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
6728c2ecf20Sopenharmony_ci	struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	// setup PLL configuration
6758c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
6768c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 1);
6778c2ecf20Sopenharmony_ci	if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
6788c2ecf20Sopenharmony_ci		return -EIO;
6798c2ecf20Sopenharmony_ci	msleep(1);
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	return 0;
6828c2ecf20Sopenharmony_ci}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_cistatic int philips_tu1216_tuner_set_params(struct dvb_frontend *fe)
6858c2ecf20Sopenharmony_ci{
6868c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
6878c2ecf20Sopenharmony_ci	struct budget *budget = (struct budget *) fe->dvb->priv;
6888c2ecf20Sopenharmony_ci	u8 tuner_buf[4];
6898c2ecf20Sopenharmony_ci	struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,.len =
6908c2ecf20Sopenharmony_ci			sizeof(tuner_buf) };
6918c2ecf20Sopenharmony_ci	int tuner_frequency = 0;
6928c2ecf20Sopenharmony_ci	u8 band, cp, filter;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	// determine charge pump
6958c2ecf20Sopenharmony_ci	tuner_frequency = c->frequency + 36166000;
6968c2ecf20Sopenharmony_ci	if (tuner_frequency < 87000000)
6978c2ecf20Sopenharmony_ci		return -EINVAL;
6988c2ecf20Sopenharmony_ci	else if (tuner_frequency < 130000000)
6998c2ecf20Sopenharmony_ci		cp = 3;
7008c2ecf20Sopenharmony_ci	else if (tuner_frequency < 160000000)
7018c2ecf20Sopenharmony_ci		cp = 5;
7028c2ecf20Sopenharmony_ci	else if (tuner_frequency < 200000000)
7038c2ecf20Sopenharmony_ci		cp = 6;
7048c2ecf20Sopenharmony_ci	else if (tuner_frequency < 290000000)
7058c2ecf20Sopenharmony_ci		cp = 3;
7068c2ecf20Sopenharmony_ci	else if (tuner_frequency < 420000000)
7078c2ecf20Sopenharmony_ci		cp = 5;
7088c2ecf20Sopenharmony_ci	else if (tuner_frequency < 480000000)
7098c2ecf20Sopenharmony_ci		cp = 6;
7108c2ecf20Sopenharmony_ci	else if (tuner_frequency < 620000000)
7118c2ecf20Sopenharmony_ci		cp = 3;
7128c2ecf20Sopenharmony_ci	else if (tuner_frequency < 830000000)
7138c2ecf20Sopenharmony_ci		cp = 5;
7148c2ecf20Sopenharmony_ci	else if (tuner_frequency < 895000000)
7158c2ecf20Sopenharmony_ci		cp = 7;
7168c2ecf20Sopenharmony_ci	else
7178c2ecf20Sopenharmony_ci		return -EINVAL;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	// determine band
7208c2ecf20Sopenharmony_ci	if (c->frequency < 49000000)
7218c2ecf20Sopenharmony_ci		return -EINVAL;
7228c2ecf20Sopenharmony_ci	else if (c->frequency < 161000000)
7238c2ecf20Sopenharmony_ci		band = 1;
7248c2ecf20Sopenharmony_ci	else if (c->frequency < 444000000)
7258c2ecf20Sopenharmony_ci		band = 2;
7268c2ecf20Sopenharmony_ci	else if (c->frequency < 861000000)
7278c2ecf20Sopenharmony_ci		band = 4;
7288c2ecf20Sopenharmony_ci	else
7298c2ecf20Sopenharmony_ci		return -EINVAL;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	// setup PLL filter
7328c2ecf20Sopenharmony_ci	switch (c->bandwidth_hz) {
7338c2ecf20Sopenharmony_ci	case 6000000:
7348c2ecf20Sopenharmony_ci		filter = 0;
7358c2ecf20Sopenharmony_ci		break;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	case 7000000:
7388c2ecf20Sopenharmony_ci		filter = 0;
7398c2ecf20Sopenharmony_ci		break;
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	case 8000000:
7428c2ecf20Sopenharmony_ci		filter = 1;
7438c2ecf20Sopenharmony_ci		break;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	default:
7468c2ecf20Sopenharmony_ci		return -EINVAL;
7478c2ecf20Sopenharmony_ci	}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	// calculate divisor
7508c2ecf20Sopenharmony_ci	// ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
7518c2ecf20Sopenharmony_ci	tuner_frequency = (((c->frequency / 1000) * 6) + 217496) / 1000;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	// setup tuner buffer
7548c2ecf20Sopenharmony_ci	tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
7558c2ecf20Sopenharmony_ci	tuner_buf[1] = tuner_frequency & 0xff;
7568c2ecf20Sopenharmony_ci	tuner_buf[2] = 0xca;
7578c2ecf20Sopenharmony_ci	tuner_buf[3] = (cp << 5) | (filter << 3) | band;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
7608c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 1);
7618c2ecf20Sopenharmony_ci	if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
7628c2ecf20Sopenharmony_ci		return -EIO;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	msleep(1);
7658c2ecf20Sopenharmony_ci	return 0;
7668c2ecf20Sopenharmony_ci}
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_cistatic int philips_tu1216_request_firmware(struct dvb_frontend *fe,
7698c2ecf20Sopenharmony_ci					   const struct firmware **fw, char *name)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	struct budget *budget = (struct budget *) fe->dvb->priv;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	return request_firmware(fw, name, &budget->dev->pci->dev);
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_cistatic struct tda1004x_config philips_tu1216_config = {
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	.demod_address = 0x8,
7798c2ecf20Sopenharmony_ci	.invert = 1,
7808c2ecf20Sopenharmony_ci	.invert_oclk = 1,
7818c2ecf20Sopenharmony_ci	.xtal_freq = TDA10046_XTAL_4M,
7828c2ecf20Sopenharmony_ci	.agc_config = TDA10046_AGC_DEFAULT,
7838c2ecf20Sopenharmony_ci	.if_freq = TDA10046_FREQ_3617,
7848c2ecf20Sopenharmony_ci	.request_firmware = philips_tu1216_request_firmware,
7858c2ecf20Sopenharmony_ci};
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_cistatic u8 philips_sd1878_inittab[] = {
7888c2ecf20Sopenharmony_ci	0x01, 0x15,
7898c2ecf20Sopenharmony_ci	0x02, 0x30,
7908c2ecf20Sopenharmony_ci	0x03, 0x00,
7918c2ecf20Sopenharmony_ci	0x04, 0x7d,
7928c2ecf20Sopenharmony_ci	0x05, 0x35,
7938c2ecf20Sopenharmony_ci	0x06, 0x40,
7948c2ecf20Sopenharmony_ci	0x07, 0x00,
7958c2ecf20Sopenharmony_ci	0x08, 0x43,
7968c2ecf20Sopenharmony_ci	0x09, 0x02,
7978c2ecf20Sopenharmony_ci	0x0C, 0x51,
7988c2ecf20Sopenharmony_ci	0x0D, 0x82,
7998c2ecf20Sopenharmony_ci	0x0E, 0x23,
8008c2ecf20Sopenharmony_ci	0x10, 0x3f,
8018c2ecf20Sopenharmony_ci	0x11, 0x84,
8028c2ecf20Sopenharmony_ci	0x12, 0xb9,
8038c2ecf20Sopenharmony_ci	0x15, 0xc9,
8048c2ecf20Sopenharmony_ci	0x16, 0x19,
8058c2ecf20Sopenharmony_ci	0x17, 0x8c,
8068c2ecf20Sopenharmony_ci	0x18, 0x59,
8078c2ecf20Sopenharmony_ci	0x19, 0xf8,
8088c2ecf20Sopenharmony_ci	0x1a, 0xfe,
8098c2ecf20Sopenharmony_ci	0x1c, 0x7f,
8108c2ecf20Sopenharmony_ci	0x1d, 0x00,
8118c2ecf20Sopenharmony_ci	0x1e, 0x00,
8128c2ecf20Sopenharmony_ci	0x1f, 0x50,
8138c2ecf20Sopenharmony_ci	0x20, 0x00,
8148c2ecf20Sopenharmony_ci	0x21, 0x00,
8158c2ecf20Sopenharmony_ci	0x22, 0x00,
8168c2ecf20Sopenharmony_ci	0x23, 0x00,
8178c2ecf20Sopenharmony_ci	0x28, 0x00,
8188c2ecf20Sopenharmony_ci	0x29, 0x28,
8198c2ecf20Sopenharmony_ci	0x2a, 0x14,
8208c2ecf20Sopenharmony_ci	0x2b, 0x0f,
8218c2ecf20Sopenharmony_ci	0x2c, 0x09,
8228c2ecf20Sopenharmony_ci	0x2d, 0x09,
8238c2ecf20Sopenharmony_ci	0x31, 0x1f,
8248c2ecf20Sopenharmony_ci	0x32, 0x19,
8258c2ecf20Sopenharmony_ci	0x33, 0xfc,
8268c2ecf20Sopenharmony_ci	0x34, 0x93,
8278c2ecf20Sopenharmony_ci	0xff, 0xff
8288c2ecf20Sopenharmony_ci};
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_cistatic int philips_sd1878_ci_set_symbol_rate(struct dvb_frontend *fe,
8318c2ecf20Sopenharmony_ci		u32 srate, u32 ratio)
8328c2ecf20Sopenharmony_ci{
8338c2ecf20Sopenharmony_ci	u8 aclk = 0;
8348c2ecf20Sopenharmony_ci	u8 bclk = 0;
8358c2ecf20Sopenharmony_ci	u8 m1;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	aclk = 0xb5;
8388c2ecf20Sopenharmony_ci	if (srate < 2000000)
8398c2ecf20Sopenharmony_ci		bclk = 0x86;
8408c2ecf20Sopenharmony_ci	else if (srate < 5000000)
8418c2ecf20Sopenharmony_ci		bclk = 0x89;
8428c2ecf20Sopenharmony_ci	else if (srate < 15000000)
8438c2ecf20Sopenharmony_ci		bclk = 0x8f;
8448c2ecf20Sopenharmony_ci	else if (srate < 45000000)
8458c2ecf20Sopenharmony_ci		bclk = 0x95;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	m1 = 0x14;
8488c2ecf20Sopenharmony_ci	if (srate < 4000000)
8498c2ecf20Sopenharmony_ci		m1 = 0x10;
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x0e, 0x23);
8528c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x0f, 0x94);
8538c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x10, 0x39);
8548c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x13, aclk);
8558c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x14, bclk);
8568c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x15, 0xc9);
8578c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
8588c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
8598c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
8608c2ecf20Sopenharmony_ci	stv0299_writereg(fe, 0x0f, 0x80 | m1);
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	return 0;
8638c2ecf20Sopenharmony_ci}
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_cistatic const struct stv0299_config philips_sd1878_config = {
8668c2ecf20Sopenharmony_ci	.demod_address = 0x68,
8678c2ecf20Sopenharmony_ci     .inittab = philips_sd1878_inittab,
8688c2ecf20Sopenharmony_ci	.mclk = 88000000UL,
8698c2ecf20Sopenharmony_ci	.invert = 0,
8708c2ecf20Sopenharmony_ci	.skip_reinit = 0,
8718c2ecf20Sopenharmony_ci	.lock_output = STV0299_LOCKOUTPUT_1,
8728c2ecf20Sopenharmony_ci	.volt13_op0_op1 = STV0299_VOLT13_OP0,
8738c2ecf20Sopenharmony_ci	.min_delay_ms = 100,
8748c2ecf20Sopenharmony_ci	.set_symbol_rate = philips_sd1878_ci_set_symbol_rate,
8758c2ecf20Sopenharmony_ci};
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci/* KNC1 DVB-S (STB0899) Inittab	*/
8788c2ecf20Sopenharmony_cistatic const struct stb0899_s1_reg knc1_stb0899_s1_init_1[] = {
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	{ STB0899_DEV_ID		, 0x81 },
8818c2ecf20Sopenharmony_ci	{ STB0899_DISCNTRL1		, 0x32 },
8828c2ecf20Sopenharmony_ci	{ STB0899_DISCNTRL2		, 0x80 },
8838c2ecf20Sopenharmony_ci	{ STB0899_DISRX_ST0		, 0x04 },
8848c2ecf20Sopenharmony_ci	{ STB0899_DISRX_ST1		, 0x00 },
8858c2ecf20Sopenharmony_ci	{ STB0899_DISPARITY		, 0x00 },
8868c2ecf20Sopenharmony_ci	{ STB0899_DISSTATUS		, 0x20 },
8878c2ecf20Sopenharmony_ci	{ STB0899_DISF22		, 0x8c },
8888c2ecf20Sopenharmony_ci	{ STB0899_DISF22RX		, 0x9a },
8898c2ecf20Sopenharmony_ci	{ STB0899_SYSREG		, 0x0b },
8908c2ecf20Sopenharmony_ci	{ STB0899_ACRPRESC		, 0x11 },
8918c2ecf20Sopenharmony_ci	{ STB0899_ACRDIV1		, 0x0a },
8928c2ecf20Sopenharmony_ci	{ STB0899_ACRDIV2		, 0x05 },
8938c2ecf20Sopenharmony_ci	{ STB0899_DACR1			, 0x00 },
8948c2ecf20Sopenharmony_ci	{ STB0899_DACR2			, 0x00 },
8958c2ecf20Sopenharmony_ci	{ STB0899_OUTCFG		, 0x00 },
8968c2ecf20Sopenharmony_ci	{ STB0899_MODECFG		, 0x00 },
8978c2ecf20Sopenharmony_ci	{ STB0899_IRQSTATUS_3		, 0x30 },
8988c2ecf20Sopenharmony_ci	{ STB0899_IRQSTATUS_2		, 0x00 },
8998c2ecf20Sopenharmony_ci	{ STB0899_IRQSTATUS_1		, 0x00 },
9008c2ecf20Sopenharmony_ci	{ STB0899_IRQSTATUS_0		, 0x00 },
9018c2ecf20Sopenharmony_ci	{ STB0899_IRQMSK_3		, 0xf3 },
9028c2ecf20Sopenharmony_ci	{ STB0899_IRQMSK_2		, 0xfc },
9038c2ecf20Sopenharmony_ci	{ STB0899_IRQMSK_1		, 0xff },
9048c2ecf20Sopenharmony_ci	{ STB0899_IRQMSK_0		, 0xff },
9058c2ecf20Sopenharmony_ci	{ STB0899_IRQCFG		, 0x00 },
9068c2ecf20Sopenharmony_ci	{ STB0899_I2CCFG		, 0x88 },
9078c2ecf20Sopenharmony_ci	{ STB0899_I2CRPT		, 0x58 }, /* Repeater=8, Stop=disabled */
9088c2ecf20Sopenharmony_ci	{ STB0899_IOPVALUE5		, 0x00 },
9098c2ecf20Sopenharmony_ci	{ STB0899_IOPVALUE4		, 0x20 },
9108c2ecf20Sopenharmony_ci	{ STB0899_IOPVALUE3		, 0xc9 },
9118c2ecf20Sopenharmony_ci	{ STB0899_IOPVALUE2		, 0x90 },
9128c2ecf20Sopenharmony_ci	{ STB0899_IOPVALUE1		, 0x40 },
9138c2ecf20Sopenharmony_ci	{ STB0899_IOPVALUE0		, 0x00 },
9148c2ecf20Sopenharmony_ci	{ STB0899_GPIO00CFG		, 0x82 },
9158c2ecf20Sopenharmony_ci	{ STB0899_GPIO01CFG		, 0x82 },
9168c2ecf20Sopenharmony_ci	{ STB0899_GPIO02CFG		, 0x82 },
9178c2ecf20Sopenharmony_ci	{ STB0899_GPIO03CFG		, 0x82 },
9188c2ecf20Sopenharmony_ci	{ STB0899_GPIO04CFG		, 0x82 },
9198c2ecf20Sopenharmony_ci	{ STB0899_GPIO05CFG		, 0x82 },
9208c2ecf20Sopenharmony_ci	{ STB0899_GPIO06CFG		, 0x82 },
9218c2ecf20Sopenharmony_ci	{ STB0899_GPIO07CFG		, 0x82 },
9228c2ecf20Sopenharmony_ci	{ STB0899_GPIO08CFG		, 0x82 },
9238c2ecf20Sopenharmony_ci	{ STB0899_GPIO09CFG		, 0x82 },
9248c2ecf20Sopenharmony_ci	{ STB0899_GPIO10CFG		, 0x82 },
9258c2ecf20Sopenharmony_ci	{ STB0899_GPIO11CFG		, 0x82 },
9268c2ecf20Sopenharmony_ci	{ STB0899_GPIO12CFG		, 0x82 },
9278c2ecf20Sopenharmony_ci	{ STB0899_GPIO13CFG		, 0x82 },
9288c2ecf20Sopenharmony_ci	{ STB0899_GPIO14CFG		, 0x82 },
9298c2ecf20Sopenharmony_ci	{ STB0899_GPIO15CFG		, 0x82 },
9308c2ecf20Sopenharmony_ci	{ STB0899_GPIO16CFG		, 0x82 },
9318c2ecf20Sopenharmony_ci	{ STB0899_GPIO17CFG		, 0x82 },
9328c2ecf20Sopenharmony_ci	{ STB0899_GPIO18CFG		, 0x82 },
9338c2ecf20Sopenharmony_ci	{ STB0899_GPIO19CFG		, 0x82 },
9348c2ecf20Sopenharmony_ci	{ STB0899_GPIO20CFG		, 0x82 },
9358c2ecf20Sopenharmony_ci	{ STB0899_SDATCFG		, 0xb8 },
9368c2ecf20Sopenharmony_ci	{ STB0899_SCLTCFG		, 0xba },
9378c2ecf20Sopenharmony_ci	{ STB0899_AGCRFCFG		, 0x08 }, /* 0x1c */
9388c2ecf20Sopenharmony_ci	{ STB0899_GPIO22		, 0x82 }, /* AGCBB2CFG */
9398c2ecf20Sopenharmony_ci	{ STB0899_GPIO21		, 0x91 }, /* AGCBB1CFG */
9408c2ecf20Sopenharmony_ci	{ STB0899_DIRCLKCFG		, 0x82 },
9418c2ecf20Sopenharmony_ci	{ STB0899_CLKOUT27CFG		, 0x7e },
9428c2ecf20Sopenharmony_ci	{ STB0899_STDBYCFG		, 0x82 },
9438c2ecf20Sopenharmony_ci	{ STB0899_CS0CFG		, 0x82 },
9448c2ecf20Sopenharmony_ci	{ STB0899_CS1CFG		, 0x82 },
9458c2ecf20Sopenharmony_ci	{ STB0899_DISEQCOCFG		, 0x20 },
9468c2ecf20Sopenharmony_ci	{ STB0899_GPIO32CFG		, 0x82 },
9478c2ecf20Sopenharmony_ci	{ STB0899_GPIO33CFG		, 0x82 },
9488c2ecf20Sopenharmony_ci	{ STB0899_GPIO34CFG		, 0x82 },
9498c2ecf20Sopenharmony_ci	{ STB0899_GPIO35CFG		, 0x82 },
9508c2ecf20Sopenharmony_ci	{ STB0899_GPIO36CFG		, 0x82 },
9518c2ecf20Sopenharmony_ci	{ STB0899_GPIO37CFG		, 0x82 },
9528c2ecf20Sopenharmony_ci	{ STB0899_GPIO38CFG		, 0x82 },
9538c2ecf20Sopenharmony_ci	{ STB0899_GPIO39CFG		, 0x82 },
9548c2ecf20Sopenharmony_ci	{ STB0899_NCOARSE		, 0x15 }, /* 0x15 = 27 Mhz Clock, F/3 = 198MHz, F/6 = 99MHz */
9558c2ecf20Sopenharmony_ci	{ STB0899_SYNTCTRL		, 0x02 }, /* 0x00 = CLK from CLKI, 0x02 = CLK from XTALI */
9568c2ecf20Sopenharmony_ci	{ STB0899_FILTCTRL		, 0x00 },
9578c2ecf20Sopenharmony_ci	{ STB0899_SYSCTRL		, 0x00 },
9588c2ecf20Sopenharmony_ci	{ STB0899_STOPCLK1		, 0x20 },
9598c2ecf20Sopenharmony_ci	{ STB0899_STOPCLK2		, 0x00 },
9608c2ecf20Sopenharmony_ci	{ STB0899_INTBUFSTATUS		, 0x00 },
9618c2ecf20Sopenharmony_ci	{ STB0899_INTBUFCTRL		, 0x0a },
9628c2ecf20Sopenharmony_ci	{ 0xffff			, 0xff },
9638c2ecf20Sopenharmony_ci};
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_cistatic const struct stb0899_s1_reg knc1_stb0899_s1_init_3[] = {
9668c2ecf20Sopenharmony_ci	{ STB0899_DEMOD			, 0x00 },
9678c2ecf20Sopenharmony_ci	{ STB0899_RCOMPC		, 0xc9 },
9688c2ecf20Sopenharmony_ci	{ STB0899_AGC1CN		, 0x41 },
9698c2ecf20Sopenharmony_ci	{ STB0899_AGC1REF		, 0x08 },
9708c2ecf20Sopenharmony_ci	{ STB0899_RTC			, 0x7a },
9718c2ecf20Sopenharmony_ci	{ STB0899_TMGCFG		, 0x4e },
9728c2ecf20Sopenharmony_ci	{ STB0899_AGC2REF		, 0x33 },
9738c2ecf20Sopenharmony_ci	{ STB0899_TLSR			, 0x84 },
9748c2ecf20Sopenharmony_ci	{ STB0899_CFD			, 0xee },
9758c2ecf20Sopenharmony_ci	{ STB0899_ACLC			, 0x87 },
9768c2ecf20Sopenharmony_ci	{ STB0899_BCLC			, 0x94 },
9778c2ecf20Sopenharmony_ci	{ STB0899_EQON			, 0x41 },
9788c2ecf20Sopenharmony_ci	{ STB0899_LDT			, 0xdd },
9798c2ecf20Sopenharmony_ci	{ STB0899_LDT2			, 0xc9 },
9808c2ecf20Sopenharmony_ci	{ STB0899_EQUALREF		, 0xb4 },
9818c2ecf20Sopenharmony_ci	{ STB0899_TMGRAMP		, 0x10 },
9828c2ecf20Sopenharmony_ci	{ STB0899_TMGTHD		, 0x30 },
9838c2ecf20Sopenharmony_ci	{ STB0899_IDCCOMP		, 0xfb },
9848c2ecf20Sopenharmony_ci	{ STB0899_QDCCOMP		, 0x03 },
9858c2ecf20Sopenharmony_ci	{ STB0899_POWERI		, 0x3b },
9868c2ecf20Sopenharmony_ci	{ STB0899_POWERQ		, 0x3d },
9878c2ecf20Sopenharmony_ci	{ STB0899_RCOMP			, 0x81 },
9888c2ecf20Sopenharmony_ci	{ STB0899_AGCIQIN		, 0x80 },
9898c2ecf20Sopenharmony_ci	{ STB0899_AGC2I1		, 0x04 },
9908c2ecf20Sopenharmony_ci	{ STB0899_AGC2I2		, 0xf5 },
9918c2ecf20Sopenharmony_ci	{ STB0899_TLIR			, 0x25 },
9928c2ecf20Sopenharmony_ci	{ STB0899_RTF			, 0x80 },
9938c2ecf20Sopenharmony_ci	{ STB0899_DSTATUS		, 0x00 },
9948c2ecf20Sopenharmony_ci	{ STB0899_LDI			, 0xca },
9958c2ecf20Sopenharmony_ci	{ STB0899_CFRM			, 0xf1 },
9968c2ecf20Sopenharmony_ci	{ STB0899_CFRL			, 0xf3 },
9978c2ecf20Sopenharmony_ci	{ STB0899_NIRM			, 0x2a },
9988c2ecf20Sopenharmony_ci	{ STB0899_NIRL			, 0x05 },
9998c2ecf20Sopenharmony_ci	{ STB0899_ISYMB			, 0x17 },
10008c2ecf20Sopenharmony_ci	{ STB0899_QSYMB			, 0xfa },
10018c2ecf20Sopenharmony_ci	{ STB0899_SFRH			, 0x2f },
10028c2ecf20Sopenharmony_ci	{ STB0899_SFRM			, 0x68 },
10038c2ecf20Sopenharmony_ci	{ STB0899_SFRL			, 0x40 },
10048c2ecf20Sopenharmony_ci	{ STB0899_SFRUPH		, 0x2f },
10058c2ecf20Sopenharmony_ci	{ STB0899_SFRUPM		, 0x68 },
10068c2ecf20Sopenharmony_ci	{ STB0899_SFRUPL		, 0x40 },
10078c2ecf20Sopenharmony_ci	{ STB0899_EQUAI1		, 0xfd },
10088c2ecf20Sopenharmony_ci	{ STB0899_EQUAQ1		, 0x04 },
10098c2ecf20Sopenharmony_ci	{ STB0899_EQUAI2		, 0x0f },
10108c2ecf20Sopenharmony_ci	{ STB0899_EQUAQ2		, 0xff },
10118c2ecf20Sopenharmony_ci	{ STB0899_EQUAI3		, 0xdf },
10128c2ecf20Sopenharmony_ci	{ STB0899_EQUAQ3		, 0xfa },
10138c2ecf20Sopenharmony_ci	{ STB0899_EQUAI4		, 0x37 },
10148c2ecf20Sopenharmony_ci	{ STB0899_EQUAQ4		, 0x0d },
10158c2ecf20Sopenharmony_ci	{ STB0899_EQUAI5		, 0xbd },
10168c2ecf20Sopenharmony_ci	{ STB0899_EQUAQ5		, 0xf7 },
10178c2ecf20Sopenharmony_ci	{ STB0899_DSTATUS2		, 0x00 },
10188c2ecf20Sopenharmony_ci	{ STB0899_VSTATUS		, 0x00 },
10198c2ecf20Sopenharmony_ci	{ STB0899_VERROR		, 0xff },
10208c2ecf20Sopenharmony_ci	{ STB0899_IQSWAP		, 0x2a },
10218c2ecf20Sopenharmony_ci	{ STB0899_ECNT1M		, 0x00 },
10228c2ecf20Sopenharmony_ci	{ STB0899_ECNT1L		, 0x00 },
10238c2ecf20Sopenharmony_ci	{ STB0899_ECNT2M		, 0x00 },
10248c2ecf20Sopenharmony_ci	{ STB0899_ECNT2L		, 0x00 },
10258c2ecf20Sopenharmony_ci	{ STB0899_ECNT3M		, 0x00 },
10268c2ecf20Sopenharmony_ci	{ STB0899_ECNT3L		, 0x00 },
10278c2ecf20Sopenharmony_ci	{ STB0899_FECAUTO1		, 0x06 },
10288c2ecf20Sopenharmony_ci	{ STB0899_FECM			, 0x01 },
10298c2ecf20Sopenharmony_ci	{ STB0899_VTH12			, 0xf0 },
10308c2ecf20Sopenharmony_ci	{ STB0899_VTH23			, 0xa0 },
10318c2ecf20Sopenharmony_ci	{ STB0899_VTH34			, 0x78 },
10328c2ecf20Sopenharmony_ci	{ STB0899_VTH56			, 0x4e },
10338c2ecf20Sopenharmony_ci	{ STB0899_VTH67			, 0x48 },
10348c2ecf20Sopenharmony_ci	{ STB0899_VTH78			, 0x38 },
10358c2ecf20Sopenharmony_ci	{ STB0899_PRVIT			, 0xff },
10368c2ecf20Sopenharmony_ci	{ STB0899_VITSYNC		, 0x19 },
10378c2ecf20Sopenharmony_ci	{ STB0899_RSULC			, 0xb1 }, /* DVB = 0xb1, DSS = 0xa1 */
10388c2ecf20Sopenharmony_ci	{ STB0899_TSULC			, 0x42 },
10398c2ecf20Sopenharmony_ci	{ STB0899_RSLLC			, 0x40 },
10408c2ecf20Sopenharmony_ci	{ STB0899_TSLPL			, 0x12 },
10418c2ecf20Sopenharmony_ci	{ STB0899_TSCFGH		, 0x0c },
10428c2ecf20Sopenharmony_ci	{ STB0899_TSCFGM		, 0x00 },
10438c2ecf20Sopenharmony_ci	{ STB0899_TSCFGL		, 0x0c },
10448c2ecf20Sopenharmony_ci	{ STB0899_TSOUT			, 0x4d }, /* 0x0d for CAM */
10458c2ecf20Sopenharmony_ci	{ STB0899_RSSYNCDEL		, 0x00 },
10468c2ecf20Sopenharmony_ci	{ STB0899_TSINHDELH		, 0x02 },
10478c2ecf20Sopenharmony_ci	{ STB0899_TSINHDELM		, 0x00 },
10488c2ecf20Sopenharmony_ci	{ STB0899_TSINHDELL		, 0x00 },
10498c2ecf20Sopenharmony_ci	{ STB0899_TSLLSTKM		, 0x00 },
10508c2ecf20Sopenharmony_ci	{ STB0899_TSLLSTKL		, 0x00 },
10518c2ecf20Sopenharmony_ci	{ STB0899_TSULSTKM		, 0x00 },
10528c2ecf20Sopenharmony_ci	{ STB0899_TSULSTKL		, 0xab },
10538c2ecf20Sopenharmony_ci	{ STB0899_PCKLENUL		, 0x00 },
10548c2ecf20Sopenharmony_ci	{ STB0899_PCKLENLL		, 0xcc },
10558c2ecf20Sopenharmony_ci	{ STB0899_RSPCKLEN		, 0xcc },
10568c2ecf20Sopenharmony_ci	{ STB0899_TSSTATUS		, 0x80 },
10578c2ecf20Sopenharmony_ci	{ STB0899_ERRCTRL1		, 0xb6 },
10588c2ecf20Sopenharmony_ci	{ STB0899_ERRCTRL2		, 0x96 },
10598c2ecf20Sopenharmony_ci	{ STB0899_ERRCTRL3		, 0x89 },
10608c2ecf20Sopenharmony_ci	{ STB0899_DMONMSK1		, 0x27 },
10618c2ecf20Sopenharmony_ci	{ STB0899_DMONMSK0		, 0x03 },
10628c2ecf20Sopenharmony_ci	{ STB0899_DEMAPVIT		, 0x5c },
10638c2ecf20Sopenharmony_ci	{ STB0899_PLPARM		, 0x1f },
10648c2ecf20Sopenharmony_ci	{ STB0899_PDELCTRL		, 0x48 },
10658c2ecf20Sopenharmony_ci	{ STB0899_PDELCTRL2		, 0x00 },
10668c2ecf20Sopenharmony_ci	{ STB0899_BBHCTRL1		, 0x00 },
10678c2ecf20Sopenharmony_ci	{ STB0899_BBHCTRL2		, 0x00 },
10688c2ecf20Sopenharmony_ci	{ STB0899_HYSTTHRESH		, 0x77 },
10698c2ecf20Sopenharmony_ci	{ STB0899_MATCSTM		, 0x00 },
10708c2ecf20Sopenharmony_ci	{ STB0899_MATCSTL		, 0x00 },
10718c2ecf20Sopenharmony_ci	{ STB0899_UPLCSTM		, 0x00 },
10728c2ecf20Sopenharmony_ci	{ STB0899_UPLCSTL		, 0x00 },
10738c2ecf20Sopenharmony_ci	{ STB0899_DFLCSTM		, 0x00 },
10748c2ecf20Sopenharmony_ci	{ STB0899_DFLCSTL		, 0x00 },
10758c2ecf20Sopenharmony_ci	{ STB0899_SYNCCST		, 0x00 },
10768c2ecf20Sopenharmony_ci	{ STB0899_SYNCDCSTM		, 0x00 },
10778c2ecf20Sopenharmony_ci	{ STB0899_SYNCDCSTL		, 0x00 },
10788c2ecf20Sopenharmony_ci	{ STB0899_ISI_ENTRY		, 0x00 },
10798c2ecf20Sopenharmony_ci	{ STB0899_ISI_BIT_EN		, 0x00 },
10808c2ecf20Sopenharmony_ci	{ STB0899_MATSTRM		, 0x00 },
10818c2ecf20Sopenharmony_ci	{ STB0899_MATSTRL		, 0x00 },
10828c2ecf20Sopenharmony_ci	{ STB0899_UPLSTRM		, 0x00 },
10838c2ecf20Sopenharmony_ci	{ STB0899_UPLSTRL		, 0x00 },
10848c2ecf20Sopenharmony_ci	{ STB0899_DFLSTRM		, 0x00 },
10858c2ecf20Sopenharmony_ci	{ STB0899_DFLSTRL		, 0x00 },
10868c2ecf20Sopenharmony_ci	{ STB0899_SYNCSTR		, 0x00 },
10878c2ecf20Sopenharmony_ci	{ STB0899_SYNCDSTRM		, 0x00 },
10888c2ecf20Sopenharmony_ci	{ STB0899_SYNCDSTRL		, 0x00 },
10898c2ecf20Sopenharmony_ci	{ STB0899_CFGPDELSTATUS1	, 0x10 },
10908c2ecf20Sopenharmony_ci	{ STB0899_CFGPDELSTATUS2	, 0x00 },
10918c2ecf20Sopenharmony_ci	{ STB0899_BBFERRORM		, 0x00 },
10928c2ecf20Sopenharmony_ci	{ STB0899_BBFERRORL		, 0x00 },
10938c2ecf20Sopenharmony_ci	{ STB0899_UPKTERRORM		, 0x00 },
10948c2ecf20Sopenharmony_ci	{ STB0899_UPKTERRORL		, 0x00 },
10958c2ecf20Sopenharmony_ci	{ 0xffff			, 0xff },
10968c2ecf20Sopenharmony_ci};
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci/* STB0899 demodulator config for the KNC1 and clones */
10998c2ecf20Sopenharmony_cistatic struct stb0899_config knc1_dvbs2_config = {
11008c2ecf20Sopenharmony_ci	.init_dev		= knc1_stb0899_s1_init_1,
11018c2ecf20Sopenharmony_ci	.init_s2_demod		= stb0899_s2_init_2,
11028c2ecf20Sopenharmony_ci	.init_s1_demod		= knc1_stb0899_s1_init_3,
11038c2ecf20Sopenharmony_ci	.init_s2_fec		= stb0899_s2_init_4,
11048c2ecf20Sopenharmony_ci	.init_tst		= stb0899_s1_init_5,
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ci	.postproc		= NULL,
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci	.demod_address		= 0x68,
11098c2ecf20Sopenharmony_ci//	.ts_output_mode		= STB0899_OUT_PARALLEL,	/* types = SERIAL/PARALLEL	*/
11108c2ecf20Sopenharmony_ci	.block_sync_mode	= STB0899_SYNC_FORCED,	/* DSS, SYNC_FORCED/UNSYNCED	*/
11118c2ecf20Sopenharmony_ci//	.ts_pfbit_toggle	= STB0899_MPEG_NORMAL,	/* DirecTV, MPEG toggling seq	*/
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	.xtal_freq		= 27000000,
11148c2ecf20Sopenharmony_ci	.inversion		= IQ_SWAP_OFF,
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci	.lo_clk			= 76500000,
11178c2ecf20Sopenharmony_ci	.hi_clk			= 90000000,
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	.esno_ave		= STB0899_DVBS2_ESNO_AVE,
11208c2ecf20Sopenharmony_ci	.esno_quant		= STB0899_DVBS2_ESNO_QUANT,
11218c2ecf20Sopenharmony_ci	.avframes_coarse	= STB0899_DVBS2_AVFRAMES_COARSE,
11228c2ecf20Sopenharmony_ci	.avframes_fine		= STB0899_DVBS2_AVFRAMES_FINE,
11238c2ecf20Sopenharmony_ci	.miss_threshold		= STB0899_DVBS2_MISS_THRESHOLD,
11248c2ecf20Sopenharmony_ci	.uwp_threshold_acq	= STB0899_DVBS2_UWP_THRESHOLD_ACQ,
11258c2ecf20Sopenharmony_ci	.uwp_threshold_track	= STB0899_DVBS2_UWP_THRESHOLD_TRACK,
11268c2ecf20Sopenharmony_ci	.uwp_threshold_sof	= STB0899_DVBS2_UWP_THRESHOLD_SOF,
11278c2ecf20Sopenharmony_ci	.sof_search_timeout	= STB0899_DVBS2_SOF_SEARCH_TIMEOUT,
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	.btr_nco_bits		= STB0899_DVBS2_BTR_NCO_BITS,
11308c2ecf20Sopenharmony_ci	.btr_gain_shift_offset	= STB0899_DVBS2_BTR_GAIN_SHIFT_OFFSET,
11318c2ecf20Sopenharmony_ci	.crl_nco_bits		= STB0899_DVBS2_CRL_NCO_BITS,
11328c2ecf20Sopenharmony_ci	.ldpc_max_iter		= STB0899_DVBS2_LDPC_MAX_ITER,
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	.tuner_get_frequency	= tda8261_get_frequency,
11358c2ecf20Sopenharmony_ci	.tuner_set_frequency	= tda8261_set_frequency,
11368c2ecf20Sopenharmony_ci	.tuner_set_bandwidth	= NULL,
11378c2ecf20Sopenharmony_ci	.tuner_get_bandwidth	= tda8261_get_bandwidth,
11388c2ecf20Sopenharmony_ci	.tuner_set_rfsiggain	= NULL
11398c2ecf20Sopenharmony_ci};
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci/*
11428c2ecf20Sopenharmony_ci * SD1878/SHA tuner config
11438c2ecf20Sopenharmony_ci * 1F, Single I/P, Horizontal mount, High Sensitivity
11448c2ecf20Sopenharmony_ci */
11458c2ecf20Sopenharmony_cistatic const struct tda8261_config sd1878c_config = {
11468c2ecf20Sopenharmony_ci//	.name		= "SD1878/SHA",
11478c2ecf20Sopenharmony_ci	.addr		= 0x60,
11488c2ecf20Sopenharmony_ci	.step_size	= TDA8261_STEP_1000 /* kHz */
11498c2ecf20Sopenharmony_ci};
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_cistatic u8 read_pwm(struct budget_av *budget_av)
11528c2ecf20Sopenharmony_ci{
11538c2ecf20Sopenharmony_ci	u8 b = 0xff;
11548c2ecf20Sopenharmony_ci	u8 pwm;
11558c2ecf20Sopenharmony_ci	struct i2c_msg msg[] = { {.addr = 0x50,.flags = 0,.buf = &b,.len = 1},
11568c2ecf20Sopenharmony_ci	{.addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1}
11578c2ecf20Sopenharmony_ci	};
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci	if ((i2c_transfer(&budget_av->budget.i2c_adap, msg, 2) != 2)
11608c2ecf20Sopenharmony_ci	    || (pwm == 0xff))
11618c2ecf20Sopenharmony_ci		pwm = 0x48;
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	return pwm;
11648c2ecf20Sopenharmony_ci}
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci#define SUBID_DVBS_KNC1			0x0010
11678c2ecf20Sopenharmony_ci#define SUBID_DVBS_KNC1_PLUS		0x0011
11688c2ecf20Sopenharmony_ci#define SUBID_DVBS_TYPHOON		0x4f56
11698c2ecf20Sopenharmony_ci#define SUBID_DVBS_CINERGY1200		0x1154
11708c2ecf20Sopenharmony_ci#define SUBID_DVBS_CYNERGY1200N		0x1155
11718c2ecf20Sopenharmony_ci#define SUBID_DVBS_TV_STAR		0x0014
11728c2ecf20Sopenharmony_ci#define SUBID_DVBS_TV_STAR_PLUS_X4	0x0015
11738c2ecf20Sopenharmony_ci#define SUBID_DVBS_TV_STAR_CI		0x0016
11748c2ecf20Sopenharmony_ci#define SUBID_DVBS2_KNC1		0x0018
11758c2ecf20Sopenharmony_ci#define SUBID_DVBS2_KNC1_OEM		0x0019
11768c2ecf20Sopenharmony_ci#define SUBID_DVBS_EASYWATCH_1		0x001a
11778c2ecf20Sopenharmony_ci#define SUBID_DVBS_EASYWATCH_2		0x001b
11788c2ecf20Sopenharmony_ci#define SUBID_DVBS2_EASYWATCH		0x001d
11798c2ecf20Sopenharmony_ci#define SUBID_DVBS_EASYWATCH		0x001e
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci#define SUBID_DVBC_EASYWATCH		0x002a
11828c2ecf20Sopenharmony_ci#define SUBID_DVBC_EASYWATCH_MK3	0x002c
11838c2ecf20Sopenharmony_ci#define SUBID_DVBC_KNC1			0x0020
11848c2ecf20Sopenharmony_ci#define SUBID_DVBC_KNC1_PLUS		0x0021
11858c2ecf20Sopenharmony_ci#define SUBID_DVBC_KNC1_MK3		0x0022
11868c2ecf20Sopenharmony_ci#define SUBID_DVBC_KNC1_TDA10024	0x0028
11878c2ecf20Sopenharmony_ci#define SUBID_DVBC_KNC1_PLUS_MK3	0x0023
11888c2ecf20Sopenharmony_ci#define SUBID_DVBC_CINERGY1200		0x1156
11898c2ecf20Sopenharmony_ci#define SUBID_DVBC_CINERGY1200_MK3	0x1176
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci#define SUBID_DVBT_EASYWATCH		0x003a
11928c2ecf20Sopenharmony_ci#define SUBID_DVBT_KNC1_PLUS		0x0031
11938c2ecf20Sopenharmony_ci#define SUBID_DVBT_KNC1			0x0030
11948c2ecf20Sopenharmony_ci#define SUBID_DVBT_CINERGY1200		0x1157
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_cistatic void frontend_init(struct budget_av *budget_av)
11978c2ecf20Sopenharmony_ci{
11988c2ecf20Sopenharmony_ci	struct saa7146_dev * saa = budget_av->budget.dev;
11998c2ecf20Sopenharmony_ci	struct dvb_frontend * fe = NULL;
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci	/* Enable / PowerON Frontend */
12028c2ecf20Sopenharmony_ci	saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci	/* Wait for PowerON */
12058c2ecf20Sopenharmony_ci	msleep(100);
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	/* additional setup necessary for the PLUS cards */
12088c2ecf20Sopenharmony_ci	switch (saa->pci->subsystem_device) {
12098c2ecf20Sopenharmony_ci		case SUBID_DVBS_KNC1_PLUS:
12108c2ecf20Sopenharmony_ci		case SUBID_DVBC_KNC1_PLUS:
12118c2ecf20Sopenharmony_ci		case SUBID_DVBT_KNC1_PLUS:
12128c2ecf20Sopenharmony_ci		case SUBID_DVBC_EASYWATCH:
12138c2ecf20Sopenharmony_ci		case SUBID_DVBC_KNC1_PLUS_MK3:
12148c2ecf20Sopenharmony_ci		case SUBID_DVBS2_KNC1:
12158c2ecf20Sopenharmony_ci		case SUBID_DVBS2_KNC1_OEM:
12168c2ecf20Sopenharmony_ci		case SUBID_DVBS2_EASYWATCH:
12178c2ecf20Sopenharmony_ci			saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTHI);
12188c2ecf20Sopenharmony_ci			break;
12198c2ecf20Sopenharmony_ci	}
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci	switch (saa->pci->subsystem_device) {
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	case SUBID_DVBS_KNC1:
12248c2ecf20Sopenharmony_ci		/*
12258c2ecf20Sopenharmony_ci		 * maybe that setting is needed for other dvb-s cards as well,
12268c2ecf20Sopenharmony_ci		 * but so far it has been only confirmed for this type
12278c2ecf20Sopenharmony_ci		 */
12288c2ecf20Sopenharmony_ci		budget_av->reinitialise_demod = 1;
12298c2ecf20Sopenharmony_ci		fallthrough;
12308c2ecf20Sopenharmony_ci	case SUBID_DVBS_KNC1_PLUS:
12318c2ecf20Sopenharmony_ci	case SUBID_DVBS_EASYWATCH_1:
12328c2ecf20Sopenharmony_ci		if (saa->pci->subsystem_vendor == 0x1894) {
12338c2ecf20Sopenharmony_ci			fe = dvb_attach(stv0299_attach, &cinergy_1200s_1894_0010_config,
12348c2ecf20Sopenharmony_ci					     &budget_av->budget.i2c_adap);
12358c2ecf20Sopenharmony_ci			if (fe) {
12368c2ecf20Sopenharmony_ci				dvb_attach(tua6100_attach, fe, 0x60, &budget_av->budget.i2c_adap);
12378c2ecf20Sopenharmony_ci			}
12388c2ecf20Sopenharmony_ci		} else {
12398c2ecf20Sopenharmony_ci			fe = dvb_attach(stv0299_attach, &typhoon_config,
12408c2ecf20Sopenharmony_ci					     &budget_av->budget.i2c_adap);
12418c2ecf20Sopenharmony_ci			if (fe) {
12428c2ecf20Sopenharmony_ci				fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
12438c2ecf20Sopenharmony_ci			}
12448c2ecf20Sopenharmony_ci		}
12458c2ecf20Sopenharmony_ci		break;
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	case SUBID_DVBS_TV_STAR:
12488c2ecf20Sopenharmony_ci	case SUBID_DVBS_TV_STAR_PLUS_X4:
12498c2ecf20Sopenharmony_ci	case SUBID_DVBS_TV_STAR_CI:
12508c2ecf20Sopenharmony_ci	case SUBID_DVBS_CYNERGY1200N:
12518c2ecf20Sopenharmony_ci	case SUBID_DVBS_EASYWATCH:
12528c2ecf20Sopenharmony_ci	case SUBID_DVBS_EASYWATCH_2:
12538c2ecf20Sopenharmony_ci		fe = dvb_attach(stv0299_attach, &philips_sd1878_config,
12548c2ecf20Sopenharmony_ci				&budget_av->budget.i2c_adap);
12558c2ecf20Sopenharmony_ci		if (fe) {
12568c2ecf20Sopenharmony_ci			dvb_attach(dvb_pll_attach, fe, 0x60,
12578c2ecf20Sopenharmony_ci				   &budget_av->budget.i2c_adap,
12588c2ecf20Sopenharmony_ci				   DVB_PLL_PHILIPS_SD1878_TDA8261);
12598c2ecf20Sopenharmony_ci		}
12608c2ecf20Sopenharmony_ci		break;
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	case SUBID_DVBS_TYPHOON:
12638c2ecf20Sopenharmony_ci		fe = dvb_attach(stv0299_attach, &typhoon_config,
12648c2ecf20Sopenharmony_ci				    &budget_av->budget.i2c_adap);
12658c2ecf20Sopenharmony_ci		if (fe) {
12668c2ecf20Sopenharmony_ci			fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
12678c2ecf20Sopenharmony_ci		}
12688c2ecf20Sopenharmony_ci		break;
12698c2ecf20Sopenharmony_ci	case SUBID_DVBS2_KNC1:
12708c2ecf20Sopenharmony_ci	case SUBID_DVBS2_KNC1_OEM:
12718c2ecf20Sopenharmony_ci	case SUBID_DVBS2_EASYWATCH:
12728c2ecf20Sopenharmony_ci		budget_av->reinitialise_demod = 1;
12738c2ecf20Sopenharmony_ci		if ((fe = dvb_attach(stb0899_attach, &knc1_dvbs2_config, &budget_av->budget.i2c_adap)))
12748c2ecf20Sopenharmony_ci			dvb_attach(tda8261_attach, fe, &sd1878c_config, &budget_av->budget.i2c_adap);
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci		break;
12778c2ecf20Sopenharmony_ci	case SUBID_DVBS_CINERGY1200:
12788c2ecf20Sopenharmony_ci		fe = dvb_attach(stv0299_attach, &cinergy_1200s_config,
12798c2ecf20Sopenharmony_ci				    &budget_av->budget.i2c_adap);
12808c2ecf20Sopenharmony_ci		if (fe) {
12818c2ecf20Sopenharmony_ci			fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
12828c2ecf20Sopenharmony_ci		}
12838c2ecf20Sopenharmony_ci		break;
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci	case SUBID_DVBC_KNC1:
12868c2ecf20Sopenharmony_ci	case SUBID_DVBC_KNC1_PLUS:
12878c2ecf20Sopenharmony_ci	case SUBID_DVBC_CINERGY1200:
12888c2ecf20Sopenharmony_ci	case SUBID_DVBC_EASYWATCH:
12898c2ecf20Sopenharmony_ci		budget_av->reinitialise_demod = 1;
12908c2ecf20Sopenharmony_ci		budget_av->budget.dev->i2c_bitrate = SAA7146_I2C_BUS_BIT_RATE_240;
12918c2ecf20Sopenharmony_ci		fe = dvb_attach(tda10021_attach, &philips_cu1216_config,
12928c2ecf20Sopenharmony_ci				     &budget_av->budget.i2c_adap,
12938c2ecf20Sopenharmony_ci				     read_pwm(budget_av));
12948c2ecf20Sopenharmony_ci		if (fe == NULL)
12958c2ecf20Sopenharmony_ci			fe = dvb_attach(tda10021_attach, &philips_cu1216_config_altaddress,
12968c2ecf20Sopenharmony_ci					     &budget_av->budget.i2c_adap,
12978c2ecf20Sopenharmony_ci					     read_pwm(budget_av));
12988c2ecf20Sopenharmony_ci		if (fe) {
12998c2ecf20Sopenharmony_ci			fe->ops.tuner_ops.set_params = philips_cu1216_tuner_set_params;
13008c2ecf20Sopenharmony_ci		}
13018c2ecf20Sopenharmony_ci		break;
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci	case SUBID_DVBC_EASYWATCH_MK3:
13048c2ecf20Sopenharmony_ci	case SUBID_DVBC_CINERGY1200_MK3:
13058c2ecf20Sopenharmony_ci	case SUBID_DVBC_KNC1_MK3:
13068c2ecf20Sopenharmony_ci	case SUBID_DVBC_KNC1_TDA10024:
13078c2ecf20Sopenharmony_ci	case SUBID_DVBC_KNC1_PLUS_MK3:
13088c2ecf20Sopenharmony_ci		budget_av->reinitialise_demod = 1;
13098c2ecf20Sopenharmony_ci		budget_av->budget.dev->i2c_bitrate = SAA7146_I2C_BUS_BIT_RATE_240;
13108c2ecf20Sopenharmony_ci		fe = dvb_attach(tda10023_attach,
13118c2ecf20Sopenharmony_ci			&philips_cu1216_tda10023_config,
13128c2ecf20Sopenharmony_ci			&budget_av->budget.i2c_adap,
13138c2ecf20Sopenharmony_ci			read_pwm(budget_av));
13148c2ecf20Sopenharmony_ci		if (fe) {
13158c2ecf20Sopenharmony_ci			fe->ops.tuner_ops.set_params = philips_cu1216_tuner_set_params;
13168c2ecf20Sopenharmony_ci		}
13178c2ecf20Sopenharmony_ci		break;
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	case SUBID_DVBT_EASYWATCH:
13208c2ecf20Sopenharmony_ci	case SUBID_DVBT_KNC1:
13218c2ecf20Sopenharmony_ci	case SUBID_DVBT_KNC1_PLUS:
13228c2ecf20Sopenharmony_ci	case SUBID_DVBT_CINERGY1200:
13238c2ecf20Sopenharmony_ci		budget_av->reinitialise_demod = 1;
13248c2ecf20Sopenharmony_ci		fe = dvb_attach(tda10046_attach, &philips_tu1216_config,
13258c2ecf20Sopenharmony_ci				     &budget_av->budget.i2c_adap);
13268c2ecf20Sopenharmony_ci		if (fe) {
13278c2ecf20Sopenharmony_ci			fe->ops.tuner_ops.init = philips_tu1216_tuner_init;
13288c2ecf20Sopenharmony_ci			fe->ops.tuner_ops.set_params = philips_tu1216_tuner_set_params;
13298c2ecf20Sopenharmony_ci		}
13308c2ecf20Sopenharmony_ci		break;
13318c2ecf20Sopenharmony_ci	}
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci	if (fe == NULL) {
13348c2ecf20Sopenharmony_ci		pr_err("A frontend driver was not found for device [%04x:%04x] subsystem [%04x:%04x]\n",
13358c2ecf20Sopenharmony_ci		       saa->pci->vendor,
13368c2ecf20Sopenharmony_ci		       saa->pci->device,
13378c2ecf20Sopenharmony_ci		       saa->pci->subsystem_vendor,
13388c2ecf20Sopenharmony_ci		       saa->pci->subsystem_device);
13398c2ecf20Sopenharmony_ci		return;
13408c2ecf20Sopenharmony_ci	}
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	budget_av->budget.dvb_frontend = fe;
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	if (dvb_register_frontend(&budget_av->budget.dvb_adapter,
13458c2ecf20Sopenharmony_ci				  budget_av->budget.dvb_frontend)) {
13468c2ecf20Sopenharmony_ci		pr_err("Frontend registration failed!\n");
13478c2ecf20Sopenharmony_ci		dvb_frontend_detach(budget_av->budget.dvb_frontend);
13488c2ecf20Sopenharmony_ci		budget_av->budget.dvb_frontend = NULL;
13498c2ecf20Sopenharmony_ci	}
13508c2ecf20Sopenharmony_ci}
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_cistatic void budget_av_irq(struct saa7146_dev *dev, u32 * isr)
13548c2ecf20Sopenharmony_ci{
13558c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_ci	dprintk(8, "dev: %p, budget_av: %p\n", dev, budget_av);
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	if (*isr & MASK_10)
13608c2ecf20Sopenharmony_ci		ttpci_budget_irq10_handler(dev, isr);
13618c2ecf20Sopenharmony_ci}
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_cistatic int budget_av_detach(struct saa7146_dev *dev)
13648c2ecf20Sopenharmony_ci{
13658c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
13668c2ecf20Sopenharmony_ci	int err;
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci	dprintk(2, "dev: %p\n", dev);
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	if (1 == budget_av->has_saa7113) {
13718c2ecf20Sopenharmony_ci		saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTLO);
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_ci		msleep(200);
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci		saa7146_unregister_device(&budget_av->vd, dev);
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci		saa7146_vv_release(dev);
13788c2ecf20Sopenharmony_ci	}
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci	if (budget_av->budget.ci_present)
13818c2ecf20Sopenharmony_ci		ciintf_deinit(budget_av);
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci	if (budget_av->budget.dvb_frontend != NULL) {
13848c2ecf20Sopenharmony_ci		dvb_unregister_frontend(budget_av->budget.dvb_frontend);
13858c2ecf20Sopenharmony_ci		dvb_frontend_detach(budget_av->budget.dvb_frontend);
13868c2ecf20Sopenharmony_ci	}
13878c2ecf20Sopenharmony_ci	err = ttpci_budget_deinit(&budget_av->budget);
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	kfree(budget_av);
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci	return err;
13928c2ecf20Sopenharmony_ci}
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci#define KNC1_INPUTS 2
13958c2ecf20Sopenharmony_cistatic struct v4l2_input knc1_inputs[KNC1_INPUTS] = {
13968c2ecf20Sopenharmony_ci	{ 0, "Composite", V4L2_INPUT_TYPE_TUNER, 1, 0,
13978c2ecf20Sopenharmony_ci		V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
13988c2ecf20Sopenharmony_ci	{ 1, "S-Video", V4L2_INPUT_TYPE_CAMERA, 2, 0,
13998c2ecf20Sopenharmony_ci		V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
14008c2ecf20Sopenharmony_ci};
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_cistatic int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
14038c2ecf20Sopenharmony_ci{
14048c2ecf20Sopenharmony_ci	dprintk(1, "VIDIOC_ENUMINPUT %d\n", i->index);
14058c2ecf20Sopenharmony_ci	if (i->index >= KNC1_INPUTS)
14068c2ecf20Sopenharmony_ci		return -EINVAL;
14078c2ecf20Sopenharmony_ci	memcpy(i, &knc1_inputs[i->index], sizeof(struct v4l2_input));
14088c2ecf20Sopenharmony_ci	return 0;
14098c2ecf20Sopenharmony_ci}
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_cistatic int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
14128c2ecf20Sopenharmony_ci{
14138c2ecf20Sopenharmony_ci	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
14148c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *)dev->ext_priv;
14158c2ecf20Sopenharmony_ci
14168c2ecf20Sopenharmony_ci	*i = budget_av->cur_input;
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci	dprintk(1, "VIDIOC_G_INPUT %d\n", *i);
14198c2ecf20Sopenharmony_ci	return 0;
14208c2ecf20Sopenharmony_ci}
14218c2ecf20Sopenharmony_ci
14228c2ecf20Sopenharmony_cistatic int vidioc_s_input(struct file *file, void *fh, unsigned int input)
14238c2ecf20Sopenharmony_ci{
14248c2ecf20Sopenharmony_ci	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
14258c2ecf20Sopenharmony_ci	struct budget_av *budget_av = (struct budget_av *)dev->ext_priv;
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci	dprintk(1, "VIDIOC_S_INPUT %d\n", input);
14288c2ecf20Sopenharmony_ci	return saa7113_setinput(budget_av, input);
14298c2ecf20Sopenharmony_ci}
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_cistatic struct saa7146_ext_vv vv_data;
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_cistatic int budget_av_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
14348c2ecf20Sopenharmony_ci{
14358c2ecf20Sopenharmony_ci	struct budget_av *budget_av;
14368c2ecf20Sopenharmony_ci	u8 *mac;
14378c2ecf20Sopenharmony_ci	int err;
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_ci	dprintk(2, "dev: %p\n", dev);
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	if (!(budget_av = kzalloc(sizeof(struct budget_av), GFP_KERNEL)))
14428c2ecf20Sopenharmony_ci		return -ENOMEM;
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci	budget_av->has_saa7113 = 0;
14458c2ecf20Sopenharmony_ci	budget_av->budget.ci_present = 0;
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ci	dev->ext_priv = budget_av;
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci	err = ttpci_budget_init(&budget_av->budget, dev, info, THIS_MODULE,
14508c2ecf20Sopenharmony_ci				adapter_nr);
14518c2ecf20Sopenharmony_ci	if (err) {
14528c2ecf20Sopenharmony_ci		kfree(budget_av);
14538c2ecf20Sopenharmony_ci		return err;
14548c2ecf20Sopenharmony_ci	}
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci	/* knc1 initialization */
14578c2ecf20Sopenharmony_ci	saa7146_write(dev, DD1_STREAM_B, 0x04000000);
14588c2ecf20Sopenharmony_ci	saa7146_write(dev, DD1_INIT, 0x07000600);
14598c2ecf20Sopenharmony_ci	saa7146_write(dev, MC2, MASK_09 | MASK_25 | MASK_10 | MASK_26);
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci	if (saa7113_init(budget_av) == 0) {
14628c2ecf20Sopenharmony_ci		budget_av->has_saa7113 = 1;
14638c2ecf20Sopenharmony_ci		err = saa7146_vv_init(dev, &vv_data);
14648c2ecf20Sopenharmony_ci		if (err != 0) {
14658c2ecf20Sopenharmony_ci			/* fixme: proper cleanup here */
14668c2ecf20Sopenharmony_ci			ERR("cannot init vv subsystem\n");
14678c2ecf20Sopenharmony_ci			return err;
14688c2ecf20Sopenharmony_ci		}
14698c2ecf20Sopenharmony_ci		vv_data.vid_ops.vidioc_enum_input = vidioc_enum_input;
14708c2ecf20Sopenharmony_ci		vv_data.vid_ops.vidioc_g_input = vidioc_g_input;
14718c2ecf20Sopenharmony_ci		vv_data.vid_ops.vidioc_s_input = vidioc_s_input;
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_ci		if ((err = saa7146_register_device(&budget_av->vd, dev, "knc1", VFL_TYPE_VIDEO))) {
14748c2ecf20Sopenharmony_ci			/* fixme: proper cleanup here */
14758c2ecf20Sopenharmony_ci			ERR("cannot register capture v4l2 device\n");
14768c2ecf20Sopenharmony_ci			saa7146_vv_release(dev);
14778c2ecf20Sopenharmony_ci			return err;
14788c2ecf20Sopenharmony_ci		}
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci		/* beware: this modifies dev->vv ... */
14818c2ecf20Sopenharmony_ci		saa7146_set_hps_source_and_sync(dev, SAA7146_HPS_SOURCE_PORT_A,
14828c2ecf20Sopenharmony_ci						SAA7146_HPS_SYNC_PORT_A);
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci		saa7113_setinput(budget_av, 0);
14858c2ecf20Sopenharmony_ci	}
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci	/* fixme: find some sane values here... */
14888c2ecf20Sopenharmony_ci	saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_ci	mac = budget_av->budget.dvb_adapter.proposed_mac;
14918c2ecf20Sopenharmony_ci	if (i2c_readregs(&budget_av->budget.i2c_adap, 0xa0, 0x30, mac, 6)) {
14928c2ecf20Sopenharmony_ci		pr_err("KNC1-%d: Could not read MAC from KNC1 card\n",
14938c2ecf20Sopenharmony_ci		       budget_av->budget.dvb_adapter.num);
14948c2ecf20Sopenharmony_ci		eth_zero_addr(mac);
14958c2ecf20Sopenharmony_ci	} else {
14968c2ecf20Sopenharmony_ci		pr_info("KNC1-%d: MAC addr = %pM\n",
14978c2ecf20Sopenharmony_ci			budget_av->budget.dvb_adapter.num, mac);
14988c2ecf20Sopenharmony_ci	}
14998c2ecf20Sopenharmony_ci
15008c2ecf20Sopenharmony_ci	budget_av->budget.dvb_adapter.priv = budget_av;
15018c2ecf20Sopenharmony_ci	frontend_init(budget_av);
15028c2ecf20Sopenharmony_ci	ciintf_init(budget_av);
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_ci	ttpci_budget_init_hooks(&budget_av->budget);
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci	return 0;
15078c2ecf20Sopenharmony_ci}
15088c2ecf20Sopenharmony_ci
15098c2ecf20Sopenharmony_cistatic struct saa7146_standard standard[] = {
15108c2ecf20Sopenharmony_ci	{.name = "PAL",.id = V4L2_STD_PAL,
15118c2ecf20Sopenharmony_ci	 .v_offset = 0x17,.v_field = 288,
15128c2ecf20Sopenharmony_ci	 .h_offset = 0x14,.h_pixels = 680,
15138c2ecf20Sopenharmony_ci	 .v_max_out = 576,.h_max_out = 768 },
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci	{.name = "NTSC",.id = V4L2_STD_NTSC,
15168c2ecf20Sopenharmony_ci	 .v_offset = 0x16,.v_field = 240,
15178c2ecf20Sopenharmony_ci	 .h_offset = 0x06,.h_pixels = 708,
15188c2ecf20Sopenharmony_ci	 .v_max_out = 480,.h_max_out = 640, },
15198c2ecf20Sopenharmony_ci};
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_cistatic struct saa7146_ext_vv vv_data = {
15228c2ecf20Sopenharmony_ci	.inputs = 2,
15238c2ecf20Sopenharmony_ci	.capabilities = 0,	// perhaps later: V4L2_CAP_VBI_CAPTURE, but that need tweaking with the saa7113
15248c2ecf20Sopenharmony_ci	.flags = 0,
15258c2ecf20Sopenharmony_ci	.stds = &standard[0],
15268c2ecf20Sopenharmony_ci	.num_stds = ARRAY_SIZE(standard),
15278c2ecf20Sopenharmony_ci};
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_cistatic struct saa7146_extension budget_extension;
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(knc1s, "KNC1 DVB-S", BUDGET_KNC1S);
15328c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(knc1s2,"KNC1 DVB-S2", BUDGET_KNC1S2);
15338c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(sates2,"Satelco EasyWatch DVB-S2", BUDGET_KNC1S2);
15348c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(knc1c, "KNC1 DVB-C", BUDGET_KNC1C);
15358c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(knc1t, "KNC1 DVB-T", BUDGET_KNC1T);
15368c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(kncxs, "KNC TV STAR DVB-S", BUDGET_TVSTAR);
15378c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(satewpls, "Satelco EasyWatch DVB-S light", BUDGET_TVSTAR);
15388c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(satewpls1, "Satelco EasyWatch DVB-S light", BUDGET_KNC1S);
15398c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(satewps, "Satelco EasyWatch DVB-S", BUDGET_KNC1S);
15408c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(satewplc, "Satelco EasyWatch DVB-C", BUDGET_KNC1CP);
15418c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(satewcmk3, "Satelco EasyWatch DVB-C MK3", BUDGET_KNC1C_MK3);
15428c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(satewt, "Satelco EasyWatch DVB-T", BUDGET_KNC1T);
15438c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(knc1sp, "KNC1 DVB-S Plus", BUDGET_KNC1SP);
15448c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(knc1spx4, "KNC1 DVB-S Plus X4", BUDGET_KNC1SP);
15458c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(knc1cp, "KNC1 DVB-C Plus", BUDGET_KNC1CP);
15468c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(knc1cmk3, "KNC1 DVB-C MK3", BUDGET_KNC1C_MK3);
15478c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(knc1ctda10024, "KNC1 DVB-C TDA10024", BUDGET_KNC1C_TDA10024);
15488c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(knc1cpmk3, "KNC1 DVB-C Plus MK3", BUDGET_KNC1CP_MK3);
15498c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(knc1tp, "KNC1 DVB-T Plus", BUDGET_KNC1TP);
15508c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(cin1200s, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
15518c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(cin1200sn, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
15528c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(cin1200c, "Terratec Cinergy 1200 DVB-C", BUDGET_CIN1200C);
15538c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(cin1200cmk3, "Terratec Cinergy 1200 DVB-C MK3", BUDGET_CIN1200C_MK3);
15548c2ecf20Sopenharmony_ciMAKE_BUDGET_INFO(cin1200t, "Terratec Cinergy 1200 DVB-T", BUDGET_CIN1200T);
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_cistatic const struct pci_device_id pci_tbl[] = {
15578c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x4f56),
15588c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x0010),
15598c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1s, 0x1894, 0x0010),
15608c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1sp, 0x1131, 0x0011),
15618c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1sp, 0x1894, 0x0011),
15628c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0014),
15638c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1spx4, 0x1894, 0x0015),
15648c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0016),
15658c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1s2, 0x1894, 0x0018),
15668c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1s2, 0x1894, 0x0019),
15678c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(sates2, 0x1894, 0x001d),
15688c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(satewpls, 0x1894, 0x001e),
15698c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(satewpls1, 0x1894, 0x001a),
15708c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(satewps, 0x1894, 0x001b),
15718c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(satewplc, 0x1894, 0x002a),
15728c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(satewcmk3, 0x1894, 0x002c),
15738c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(satewt, 0x1894, 0x003a),
15748c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1c, 0x1894, 0x0020),
15758c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1cp, 0x1894, 0x0021),
15768c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1cmk3, 0x1894, 0x0022),
15778c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1ctda10024, 0x1894, 0x0028),
15788c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1cpmk3, 0x1894, 0x0023),
15798c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1t, 0x1894, 0x0030),
15808c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(knc1tp, 0x1894, 0x0031),
15818c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(cin1200s, 0x153b, 0x1154),
15828c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(cin1200sn, 0x153b, 0x1155),
15838c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(cin1200c, 0x153b, 0x1156),
15848c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(cin1200cmk3, 0x153b, 0x1176),
15858c2ecf20Sopenharmony_ci	MAKE_EXTENSION_PCI(cin1200t, 0x153b, 0x1157),
15868c2ecf20Sopenharmony_ci	{
15878c2ecf20Sopenharmony_ci	 .vendor = 0,
15888c2ecf20Sopenharmony_ci	}
15898c2ecf20Sopenharmony_ci};
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, pci_tbl);
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_cistatic struct saa7146_extension budget_extension = {
15948c2ecf20Sopenharmony_ci	.name = "budget_av",
15958c2ecf20Sopenharmony_ci	.flags = SAA7146_USE_I2C_IRQ,
15968c2ecf20Sopenharmony_ci
15978c2ecf20Sopenharmony_ci	.pci_tbl = pci_tbl,
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci	.module = THIS_MODULE,
16008c2ecf20Sopenharmony_ci	.attach = budget_av_attach,
16018c2ecf20Sopenharmony_ci	.detach = budget_av_detach,
16028c2ecf20Sopenharmony_ci
16038c2ecf20Sopenharmony_ci	.irq_mask = MASK_10,
16048c2ecf20Sopenharmony_ci	.irq_func = budget_av_irq,
16058c2ecf20Sopenharmony_ci};
16068c2ecf20Sopenharmony_ci
16078c2ecf20Sopenharmony_cistatic int __init budget_av_init(void)
16088c2ecf20Sopenharmony_ci{
16098c2ecf20Sopenharmony_ci	return saa7146_register_extension(&budget_extension);
16108c2ecf20Sopenharmony_ci}
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_cistatic void __exit budget_av_exit(void)
16138c2ecf20Sopenharmony_ci{
16148c2ecf20Sopenharmony_ci	saa7146_unregister_extension(&budget_extension);
16158c2ecf20Sopenharmony_ci}
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_cimodule_init(budget_av_init);
16188c2ecf20Sopenharmony_cimodule_exit(budget_av_exit);
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
16218c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
16228c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("driver for the SAA7146 based so-called budget PCI DVB w/ analog input and CI-module (e.g. the KNC cards)");
1623