1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * netup-init.c
4  *
5  * NetUP Dual DVB-S2 CI driver
6  *
7  * Copyright (C) 2009 NetUP Inc.
8  * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
9  * Copyright (C) 2009 Abylay Ospan <aospan@netup.ru>
10  */
11 
12 #include "cx23885.h"
13 #include "netup-init.h"
14 
15 static void i2c_av_write(struct i2c_adapter *i2c, u16 reg, u8 val)
16 {
17 	int ret;
18 	u8 buf[3];
19 	struct i2c_msg msg = {
20 		.addr	= 0x88 >> 1,
21 		.flags	= 0,
22 		.buf	= buf,
23 		.len	= 3
24 	};
25 
26 	buf[0] = reg >> 8;
27 	buf[1] = reg & 0xff;
28 	buf[2] = val;
29 
30 	ret = i2c_transfer(i2c, &msg, 1);
31 
32 	if (ret != 1)
33 		pr_err("%s: i2c write error!\n", __func__);
34 }
35 
36 static void i2c_av_write4(struct i2c_adapter *i2c, u16 reg, u32 val)
37 {
38 	int ret;
39 	u8 buf[6];
40 	struct i2c_msg msg = {
41 		.addr	= 0x88 >> 1,
42 		.flags	= 0,
43 		.buf	= buf,
44 		.len	= 6
45 	};
46 
47 	buf[0] = reg >> 8;
48 	buf[1] = reg & 0xff;
49 	buf[2] = val & 0xff;
50 	buf[3] = (val >> 8) & 0xff;
51 	buf[4] = (val >> 16) & 0xff;
52 	buf[5] = val >> 24;
53 
54 	ret = i2c_transfer(i2c, &msg, 1);
55 
56 	if (ret != 1)
57 		pr_err("%s: i2c write error!\n", __func__);
58 }
59 
60 static u8 i2c_av_read(struct i2c_adapter *i2c, u16 reg)
61 {
62 	int ret;
63 	u8 buf[2];
64 	struct i2c_msg msg = {
65 		.addr	= 0x88 >> 1,
66 		.flags	= 0,
67 		.buf	= buf,
68 		.len	= 2
69 	};
70 
71 	buf[0] = reg >> 8;
72 	buf[1] = reg & 0xff;
73 
74 	ret = i2c_transfer(i2c, &msg, 1);
75 
76 	if (ret != 1)
77 		pr_err("%s: i2c write error!\n", __func__);
78 
79 	msg.flags = I2C_M_RD;
80 	msg.len = 1;
81 
82 	ret = i2c_transfer(i2c, &msg, 1);
83 
84 	if (ret != 1)
85 		pr_err("%s: i2c read error!\n", __func__);
86 
87 	return buf[0];
88 }
89 
90 static void i2c_av_and_or(struct i2c_adapter *i2c, u16 reg, unsigned and_mask,
91 								u8 or_value)
92 {
93 	i2c_av_write(i2c, reg, (i2c_av_read(i2c, reg) & and_mask) | or_value);
94 }
95 /* set 27MHz on AUX_CLK */
96 void netup_initialize(struct cx23885_dev *dev)
97 {
98 	struct cx23885_i2c *i2c_bus = &dev->i2c_bus[2];
99 	struct i2c_adapter *i2c = &i2c_bus->i2c_adap;
100 
101 	/* Stop microcontroller */
102 	i2c_av_and_or(i2c, 0x803, ~0x10, 0x00);
103 
104 	/* Aux PLL frac for 27 MHz */
105 	i2c_av_write4(i2c, 0x114, 0xea0eb3);
106 
107 	/* Aux PLL int for 27 MHz */
108 	i2c_av_write4(i2c, 0x110, 0x090319);
109 
110 	/* start microcontroller */
111 	i2c_av_and_or(i2c, 0x803, ~0x10, 0x10);
112 }
113