1 /*
2  * Driver for Silicon Labs C8051F300 microcontroller.
3  *
4  * It is used for LNB power control in TeVii S470,
5  * TBS 6920 PCIe DVB-S2 cards.
6  *
7  * Microcontroller connected to cx23885 GPIO pins:
8  * GPIO0 - data		- P0.3 F300
9  * GPIO1 - reset	- P0.2 F300
10  * GPIO2 - clk		- P0.1 F300
11  * GPIO3 - busy		- P0.0 F300
12  *
13  * Copyright (C) 2009 Igor M. Liplianin <liplianin@me.by>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  */
30 
31 #include "cx23885.h"
32 #include "cx23885-f300.h"
33 
34 #define F300_DATA	GPIO_0
35 #define F300_RESET	GPIO_1
36 #define F300_CLK	GPIO_2
37 #define F300_BUSY	GPIO_3
38 
39 static void f300_set_line(struct cx23885_dev *dev, u32 line, u8 lvl)
40 {
41 	cx23885_gpio_enable(dev, line, 1);
42 	if (lvl == 1)
43 		cx23885_gpio_set(dev, line);
44 	else
45 		cx23885_gpio_clear(dev, line);
46 }
47 
48 static u8 f300_get_line(struct cx23885_dev *dev, u32 line)
49 {
50 	cx23885_gpio_enable(dev, line, 0);
51 
52 	return cx23885_gpio_get(dev, line);
53 }
54 
55 static void f300_send_byte(struct cx23885_dev *dev, u8 dta)
56 {
57 	u8 i;
58 
59 	for (i = 0; i < 8; i++) {
60 		f300_set_line(dev, F300_CLK, 0);
61 		udelay(30);
62 		f300_set_line(dev, F300_DATA, (dta & 0x80) >> 7);/* msb first */
63 		udelay(30);
64 		dta <<= 1;
65 		f300_set_line(dev, F300_CLK, 1);
66 		udelay(30);
67 	}
68 }
69 
70 static u8 f300_get_byte(struct cx23885_dev *dev)
71 {
72 	u8 i, dta = 0;
73 
74 	for (i = 0; i < 8; i++) {
75 		f300_set_line(dev, F300_CLK, 0);
76 		udelay(30);
77 		dta <<= 1;
78 		f300_set_line(dev, F300_CLK, 1);
79 		udelay(30);
80 		dta |= f300_get_line(dev, F300_DATA);/* msb first */
81 
82 	}
83 
84 	return dta;
85 }
86 
87 static u8 f300_xfer(struct dvb_frontend *fe, u8 *buf)
88 {
89 	struct cx23885_tsport *port = fe->dvb->priv;
90 	struct cx23885_dev *dev = port->dev;
91 	u8 i, temp, ret = 0;
92 
93 	temp = buf[0];
94 	for (i = 0; i < buf[0]; i++)
95 		temp += buf[i + 1];
96 	temp = (~temp + 1);/* get check sum */
97 	buf[1 + buf[0]] = temp;
98 
99 	f300_set_line(dev, F300_RESET, 1);
100 	f300_set_line(dev, F300_CLK, 1);
101 	udelay(30);
102 	f300_set_line(dev, F300_DATA, 1);
103 	msleep(1);
104 
105 	/* question: */
106 	f300_set_line(dev, F300_RESET, 0);/* begin to send data */
107 	msleep(1);
108 
109 	f300_send_byte(dev, 0xe0);/* the slave address is 0xe0, write */
110 	msleep(1);
111 
112 	temp = buf[0];
113 	temp += 2;
114 	for (i = 0; i < temp; i++)
115 		f300_send_byte(dev, buf[i]);
116 
117 	f300_set_line(dev, F300_RESET, 1);/* sent data over */
118 	f300_set_line(dev, F300_DATA, 1);
119 
120 	/* answer: */
121 	temp = 0;
122 	for (i = 0; ((i < 8) & (temp == 0)); i++) {
123 		msleep(1);
124 		if (f300_get_line(dev, F300_BUSY) == 0)
125 			temp = 1;
126 	}
127 
128 	if (i > 7) {
129 		printk(KERN_ERR "%s: timeout, the slave no response\n",
130 								__func__);
131 		ret = 1; /* timeout, the slave no response */
132 	} else { /* the slave not busy, prepare for getting data */
133 		f300_set_line(dev, F300_RESET, 0);/*ready...*/
134 		msleep(1);
135 		f300_send_byte(dev, 0xe1);/* 0xe1 is Read */
136 		msleep(1);
137 		temp = f300_get_byte(dev);/*get the data length */
138 		if (temp > 14)
139 			temp = 14;
140 
141 		for (i = 0; i < (temp + 1); i++)
142 			f300_get_byte(dev);/* get data to empty buffer */
143 
144 		f300_set_line(dev, F300_RESET, 1);/* received data over */
145 		f300_set_line(dev, F300_DATA, 1);
146 	}
147 
148 	return ret;
149 }
150 
151 int f300_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
152 {
153 	u8 buf[16];
154 
155 	buf[0] = 0x05;
156 	buf[1] = 0x38;/* write port */
157 	buf[2] = 0x01;/* A port, lnb power */
158 
159 	switch (voltage) {
160 	case SEC_VOLTAGE_13:
161 		buf[3] = 0x01;/* power on */
162 		buf[4] = 0x02;/* B port, H/V */
163 		buf[5] = 0x00;/*13V v*/
164 		break;
165 	case SEC_VOLTAGE_18:
166 		buf[3] = 0x01;
167 		buf[4] = 0x02;
168 		buf[5] = 0x01;/* 18V h*/
169 		break;
170 	case SEC_VOLTAGE_OFF:
171 		buf[3] = 0x00;/* power off */
172 		buf[4] = 0x00;
173 		buf[5] = 0x00;
174 		break;
175 	}
176 
177 	return f300_xfer(fe, buf);
178 }
179