xref: /linux/drivers/media/dvb-frontends/cxd2880/cxd2880_tnrdmd_mon.c (revision c13aca79ff3c4af5fd31a5b2743a90eba6e36a26)
1*e5835488SYasunari Takiguchi // SPDX-License-Identifier: GPL-2.0
2*e5835488SYasunari Takiguchi /*
3*e5835488SYasunari Takiguchi  * cxd2880_tnrdmd_mon.c
4*e5835488SYasunari Takiguchi  * Sony CXD2880 DVB-T2/T tuner + demodulator driver
5*e5835488SYasunari Takiguchi  * common monitor functions
6*e5835488SYasunari Takiguchi  *
7*e5835488SYasunari Takiguchi  * Copyright (C) 2016, 2017, 2018 Sony Semiconductor Solutions Corporation
8*e5835488SYasunari Takiguchi  */
9*e5835488SYasunari Takiguchi 
10*e5835488SYasunari Takiguchi #include "cxd2880_common.h"
11*e5835488SYasunari Takiguchi #include "cxd2880_tnrdmd_mon.h"
12*e5835488SYasunari Takiguchi 
13*e5835488SYasunari Takiguchi static const u8 rf_lvl_seq[2] = {
14*e5835488SYasunari Takiguchi 	0x80, 0x00,
15*e5835488SYasunari Takiguchi };
16*e5835488SYasunari Takiguchi 
cxd2880_tnrdmd_mon_rf_lvl(struct cxd2880_tnrdmd * tnr_dmd,int * rf_lvl_db)17*e5835488SYasunari Takiguchi int cxd2880_tnrdmd_mon_rf_lvl(struct cxd2880_tnrdmd *tnr_dmd,
18*e5835488SYasunari Takiguchi 			      int *rf_lvl_db)
19*e5835488SYasunari Takiguchi {
20*e5835488SYasunari Takiguchi 	u8 rdata[2];
21*e5835488SYasunari Takiguchi 	int ret;
22*e5835488SYasunari Takiguchi 
23*e5835488SYasunari Takiguchi 	if (!tnr_dmd || !rf_lvl_db)
24*e5835488SYasunari Takiguchi 		return -EINVAL;
25*e5835488SYasunari Takiguchi 
26*e5835488SYasunari Takiguchi 	if (tnr_dmd->state != CXD2880_TNRDMD_STATE_ACTIVE)
27*e5835488SYasunari Takiguchi 		return -EINVAL;
28*e5835488SYasunari Takiguchi 
29*e5835488SYasunari Takiguchi 	ret = tnr_dmd->io->write_reg(tnr_dmd->io,
30*e5835488SYasunari Takiguchi 				     CXD2880_IO_TGT_DMD,
31*e5835488SYasunari Takiguchi 				     0x00, 0x00);
32*e5835488SYasunari Takiguchi 	if (ret)
33*e5835488SYasunari Takiguchi 		return ret;
34*e5835488SYasunari Takiguchi 
35*e5835488SYasunari Takiguchi 	ret = tnr_dmd->io->write_reg(tnr_dmd->io,
36*e5835488SYasunari Takiguchi 				     CXD2880_IO_TGT_DMD,
37*e5835488SYasunari Takiguchi 				     0x10, 0x01);
38*e5835488SYasunari Takiguchi 	if (ret)
39*e5835488SYasunari Takiguchi 		return ret;
40*e5835488SYasunari Takiguchi 
41*e5835488SYasunari Takiguchi 	ret = tnr_dmd->io->write_reg(tnr_dmd->io,
42*e5835488SYasunari Takiguchi 				     CXD2880_IO_TGT_SYS,
43*e5835488SYasunari Takiguchi 				     0x00, 0x10);
44*e5835488SYasunari Takiguchi 	if (ret)
45*e5835488SYasunari Takiguchi 		return ret;
46*e5835488SYasunari Takiguchi 
47*e5835488SYasunari Takiguchi 	ret = tnr_dmd->io->write_regs(tnr_dmd->io,
48*e5835488SYasunari Takiguchi 				      CXD2880_IO_TGT_SYS,
49*e5835488SYasunari Takiguchi 				      0x5b, rf_lvl_seq, 2);
50*e5835488SYasunari Takiguchi 	if (ret)
51*e5835488SYasunari Takiguchi 		return ret;
52*e5835488SYasunari Takiguchi 
53*e5835488SYasunari Takiguchi 	usleep_range(2000, 3000);
54*e5835488SYasunari Takiguchi 
55*e5835488SYasunari Takiguchi 	ret = tnr_dmd->io->write_reg(tnr_dmd->io,
56*e5835488SYasunari Takiguchi 				     CXD2880_IO_TGT_SYS,
57*e5835488SYasunari Takiguchi 				     0x00, 0x1a);
58*e5835488SYasunari Takiguchi 	if (ret)
59*e5835488SYasunari Takiguchi 		return ret;
60*e5835488SYasunari Takiguchi 
61*e5835488SYasunari Takiguchi 	ret = tnr_dmd->io->read_regs(tnr_dmd->io,
62*e5835488SYasunari Takiguchi 				     CXD2880_IO_TGT_SYS,
63*e5835488SYasunari Takiguchi 				     0x15, rdata, 2);
64*e5835488SYasunari Takiguchi 	if (ret)
65*e5835488SYasunari Takiguchi 		return ret;
66*e5835488SYasunari Takiguchi 
67*e5835488SYasunari Takiguchi 	if (rdata[0] || rdata[1])
68*e5835488SYasunari Takiguchi 		return -EINVAL;
69*e5835488SYasunari Takiguchi 
70*e5835488SYasunari Takiguchi 	ret = tnr_dmd->io->read_regs(tnr_dmd->io,
71*e5835488SYasunari Takiguchi 				     CXD2880_IO_TGT_SYS,
72*e5835488SYasunari Takiguchi 				     0x11, rdata, 2);
73*e5835488SYasunari Takiguchi 	if (ret)
74*e5835488SYasunari Takiguchi 		return ret;
75*e5835488SYasunari Takiguchi 
76*e5835488SYasunari Takiguchi 	*rf_lvl_db =
77*e5835488SYasunari Takiguchi 	    cxd2880_convert2s_complement((rdata[0] << 3) |
78*e5835488SYasunari Takiguchi 					 ((rdata[1] & 0xe0) >> 5), 11);
79*e5835488SYasunari Takiguchi 
80*e5835488SYasunari Takiguchi 	*rf_lvl_db *= 125;
81*e5835488SYasunari Takiguchi 
82*e5835488SYasunari Takiguchi 	ret = tnr_dmd->io->write_reg(tnr_dmd->io,
83*e5835488SYasunari Takiguchi 				     CXD2880_IO_TGT_DMD,
84*e5835488SYasunari Takiguchi 				     0x00, 0x00);
85*e5835488SYasunari Takiguchi 	if (ret)
86*e5835488SYasunari Takiguchi 		return ret;
87*e5835488SYasunari Takiguchi 
88*e5835488SYasunari Takiguchi 	ret = tnr_dmd->io->write_reg(tnr_dmd->io,
89*e5835488SYasunari Takiguchi 				     CXD2880_IO_TGT_DMD,
90*e5835488SYasunari Takiguchi 				     0x10, 0x00);
91*e5835488SYasunari Takiguchi 	if (ret)
92*e5835488SYasunari Takiguchi 		return ret;
93*e5835488SYasunari Takiguchi 
94*e5835488SYasunari Takiguchi 	if (tnr_dmd->rf_lvl_cmpstn)
95*e5835488SYasunari Takiguchi 		ret = tnr_dmd->rf_lvl_cmpstn(tnr_dmd, rf_lvl_db);
96*e5835488SYasunari Takiguchi 
97*e5835488SYasunari Takiguchi 	return ret;
98*e5835488SYasunari Takiguchi }
99*e5835488SYasunari Takiguchi 
cxd2880_tnrdmd_mon_rf_lvl_sub(struct cxd2880_tnrdmd * tnr_dmd,int * rf_lvl_db)100*e5835488SYasunari Takiguchi int cxd2880_tnrdmd_mon_rf_lvl_sub(struct cxd2880_tnrdmd *tnr_dmd,
101*e5835488SYasunari Takiguchi 				  int *rf_lvl_db)
102*e5835488SYasunari Takiguchi {
103*e5835488SYasunari Takiguchi 	if (!tnr_dmd || !rf_lvl_db)
104*e5835488SYasunari Takiguchi 		return -EINVAL;
105*e5835488SYasunari Takiguchi 
106*e5835488SYasunari Takiguchi 	if (tnr_dmd->diver_mode != CXD2880_TNRDMD_DIVERMODE_MAIN)
107*e5835488SYasunari Takiguchi 		return -EINVAL;
108*e5835488SYasunari Takiguchi 
109*e5835488SYasunari Takiguchi 	return cxd2880_tnrdmd_mon_rf_lvl(tnr_dmd->diver_sub, rf_lvl_db);
110*e5835488SYasunari Takiguchi }
111*e5835488SYasunari Takiguchi 
cxd2880_tnrdmd_mon_internal_cpu_status(struct cxd2880_tnrdmd * tnr_dmd,u16 * status)112*e5835488SYasunari Takiguchi int cxd2880_tnrdmd_mon_internal_cpu_status(struct cxd2880_tnrdmd
113*e5835488SYasunari Takiguchi 					   *tnr_dmd, u16 *status)
114*e5835488SYasunari Takiguchi {
115*e5835488SYasunari Takiguchi 	u8 data[2] = { 0 };
116*e5835488SYasunari Takiguchi 	int ret;
117*e5835488SYasunari Takiguchi 
118*e5835488SYasunari Takiguchi 	if (!tnr_dmd || !status)
119*e5835488SYasunari Takiguchi 		return -EINVAL;
120*e5835488SYasunari Takiguchi 
121*e5835488SYasunari Takiguchi 	ret = tnr_dmd->io->write_reg(tnr_dmd->io,
122*e5835488SYasunari Takiguchi 				     CXD2880_IO_TGT_SYS,
123*e5835488SYasunari Takiguchi 				     0x00, 0x1a);
124*e5835488SYasunari Takiguchi 	if (ret)
125*e5835488SYasunari Takiguchi 		return ret;
126*e5835488SYasunari Takiguchi 	ret = tnr_dmd->io->read_regs(tnr_dmd->io,
127*e5835488SYasunari Takiguchi 				     CXD2880_IO_TGT_SYS,
128*e5835488SYasunari Takiguchi 				     0x15, data, 2);
129*e5835488SYasunari Takiguchi 	if (ret)
130*e5835488SYasunari Takiguchi 		return ret;
131*e5835488SYasunari Takiguchi 
132*e5835488SYasunari Takiguchi 	*status = (data[0] << 8) | data[1];
133*e5835488SYasunari Takiguchi 
134*e5835488SYasunari Takiguchi 	return 0;
135*e5835488SYasunari Takiguchi }
136*e5835488SYasunari Takiguchi 
cxd2880_tnrdmd_mon_internal_cpu_status_sub(struct cxd2880_tnrdmd * tnr_dmd,u16 * status)137*e5835488SYasunari Takiguchi int cxd2880_tnrdmd_mon_internal_cpu_status_sub(struct
138*e5835488SYasunari Takiguchi 					       cxd2880_tnrdmd
139*e5835488SYasunari Takiguchi 					       *tnr_dmd,
140*e5835488SYasunari Takiguchi 					       u16 *status)
141*e5835488SYasunari Takiguchi {
142*e5835488SYasunari Takiguchi 	if (!tnr_dmd || !status)
143*e5835488SYasunari Takiguchi 		return -EINVAL;
144*e5835488SYasunari Takiguchi 
145*e5835488SYasunari Takiguchi 	if (tnr_dmd->diver_mode != CXD2880_TNRDMD_DIVERMODE_MAIN)
146*e5835488SYasunari Takiguchi 		return -EINVAL;
147*e5835488SYasunari Takiguchi 
148*e5835488SYasunari Takiguchi 	return cxd2880_tnrdmd_mon_internal_cpu_status(tnr_dmd->diver_sub,
149*e5835488SYasunari Takiguchi 						      status);
150*e5835488SYasunari Takiguchi }
151