xref: /linux/drivers/media/pci/pt3/pt3_i2c.c (revision d4da404f36b24b089d20db1fb27d8f99cb66f409)
1 /*
2  * Earthsoft PT3 driver
3  *
4  * Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation version 2.
9  *
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/i2c.h>
19 #include <linux/io.h>
20 #include <linux/pci.h>
21 
22 #include "pt3.h"
23 
24 #define PT3_I2C_BASE  2048
25 #define PT3_CMD_ADDR_NORMAL 0
26 #define PT3_CMD_ADDR_INIT_DEMOD  4096
27 #define PT3_CMD_ADDR_INIT_TUNER  (4096 + 2042)
28 
29 /* masks for I2C status register */
30 #define STAT_SEQ_RUNNING 0x1
31 #define STAT_SEQ_ERROR   0x6
32 #define STAT_NO_SEQ      0x8
33 
34 #define PT3_I2C_RUN   (1 << 16)
35 #define PT3_I2C_RESET (1 << 17)
36 
37 enum ctl_cmd {
38 	I_END,
39 	I_ADDRESS,
40 	I_CLOCK_L,
41 	I_CLOCK_H,
42 	I_DATA_L,
43 	I_DATA_H,
44 	I_RESET,
45 	I_SLEEP,
46 	I_DATA_L_NOP  = 0x08,
47 	I_DATA_H_NOP  = 0x0c,
48 	I_DATA_H_READ = 0x0d,
49 	I_DATA_H_ACK0 = 0x0e,
50 	I_DATA_H_ACK1 = 0x0f,
51 };
52 
53 
54 static void cmdbuf_add(struct pt3_i2cbuf *cbuf, enum ctl_cmd cmd)
55 {
56 	int buf_idx;
57 
58 	if ((cbuf->num_cmds % 2) == 0)
59 		cbuf->tmp = cmd;
60 	else {
61 		cbuf->tmp |= cmd << 4;
62 		buf_idx = cbuf->num_cmds / 2;
63 		if (buf_idx < ARRAY_SIZE(cbuf->data))
64 			cbuf->data[buf_idx] = cbuf->tmp;
65 	}
66 	cbuf->num_cmds++;
67 }
68 
69 static void put_end(struct pt3_i2cbuf *cbuf)
70 {
71 	cmdbuf_add(cbuf, I_END);
72 	if (cbuf->num_cmds % 2)
73 		cmdbuf_add(cbuf, I_END);
74 }
75 
76 static void put_start(struct pt3_i2cbuf *cbuf)
77 {
78 	cmdbuf_add(cbuf, I_DATA_H);
79 	cmdbuf_add(cbuf, I_CLOCK_H);
80 	cmdbuf_add(cbuf, I_DATA_L);
81 	cmdbuf_add(cbuf, I_CLOCK_L);
82 }
83 
84 static void put_byte_write(struct pt3_i2cbuf *cbuf, u8 val)
85 {
86 	u8 mask;
87 
88 	for (mask = 0x80; mask > 0; mask >>= 1)
89 		cmdbuf_add(cbuf, (val & mask) ? I_DATA_H_NOP : I_DATA_L_NOP);
90 	cmdbuf_add(cbuf, I_DATA_H_ACK0);
91 }
92 
93 static void put_byte_read(struct pt3_i2cbuf *cbuf, u32 size)
94 {
95 	int i, j;
96 
97 	for (i = 0; i < size; i++) {
98 		for (j = 0; j < 8; j++)
99 			cmdbuf_add(cbuf, I_DATA_H_READ);
100 		cmdbuf_add(cbuf, (i == size - 1) ? I_DATA_H_NOP : I_DATA_L_NOP);
101 	}
102 }
103 
104 static void put_stop(struct pt3_i2cbuf *cbuf)
105 {
106 	cmdbuf_add(cbuf, I_DATA_L);
107 	cmdbuf_add(cbuf, I_CLOCK_H);
108 	cmdbuf_add(cbuf, I_DATA_H);
109 }
110 
111 
112 /* translates msgs to internal commands for bit-banging */
113 static void translate(struct pt3_i2cbuf *cbuf, struct i2c_msg *msgs, int num)
114 {
115 	int i, j;
116 	bool rd;
117 
118 	cbuf->num_cmds = 0;
119 	for (i = 0; i < num; i++) {
120 		rd = !!(msgs[i].flags & I2C_M_RD);
121 		put_start(cbuf);
122 		put_byte_write(cbuf, msgs[i].addr << 1 | rd);
123 		if (rd)
124 			put_byte_read(cbuf, msgs[i].len);
125 		else
126 			for (j = 0; j < msgs[i].len; j++)
127 				put_byte_write(cbuf, msgs[i].buf[j]);
128 	}
129 	if (num > 0) {
130 		put_stop(cbuf);
131 		put_end(cbuf);
132 	}
133 }
134 
135 static int wait_i2c_result(struct pt3_board *pt3, u32 *result, int max_wait)
136 {
137 	int i;
138 	u32 v;
139 
140 	for (i = 0; i < max_wait; i++) {
141 		v = ioread32(pt3->regs[0] + REG_I2C_R);
142 		if (!(v & STAT_SEQ_RUNNING))
143 			break;
144 		usleep_range(500, 750);
145 	}
146 	if (i >= max_wait)
147 		return -EIO;
148 	if (result)
149 		*result = v;
150 	return 0;
151 }
152 
153 /* send [pre-]translated i2c msgs stored at addr */
154 static int send_i2c_cmd(struct pt3_board *pt3, u32 addr)
155 {
156 	u32 ret;
157 
158 	/* make sure that previous transactions had finished */
159 	if (wait_i2c_result(pt3, NULL, 50)) {
160 		dev_warn(&pt3->pdev->dev, "(%s) prev. transaction stalled\n",
161 				__func__);
162 		return -EIO;
163 	}
164 
165 	iowrite32(PT3_I2C_RUN | addr, pt3->regs[0] + REG_I2C_W);
166 	usleep_range(200, 300);
167 	/* wait for the current transaction to finish */
168 	if (wait_i2c_result(pt3, &ret, 500) || (ret & STAT_SEQ_ERROR)) {
169 		dev_warn(&pt3->pdev->dev, "(%s) failed.\n", __func__);
170 		return -EIO;
171 	}
172 	return 0;
173 }
174 
175 
176 /* init commands for each demod are combined into one transaction
177  *  and hidden in ROM with the address PT3_CMD_ADDR_INIT_DEMOD.
178  */
179 int  pt3_init_all_demods(struct pt3_board *pt3)
180 {
181 	ioread32(pt3->regs[0] + REG_I2C_R);
182 	return send_i2c_cmd(pt3, PT3_CMD_ADDR_INIT_DEMOD);
183 }
184 
185 /* init commands for two ISDB-T tuners are hidden in ROM. */
186 int  pt3_init_all_mxl301rf(struct pt3_board *pt3)
187 {
188 	usleep_range(1000, 2000);
189 	return send_i2c_cmd(pt3, PT3_CMD_ADDR_INIT_TUNER);
190 }
191 
192 void pt3_i2c_reset(struct pt3_board *pt3)
193 {
194 	iowrite32(PT3_I2C_RESET, pt3->regs[0] + REG_I2C_W);
195 }
196 
197 /*
198  * I2C algorithm
199  */
200 int
201 pt3_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
202 {
203 	struct pt3_board *pt3;
204 	struct pt3_i2cbuf *cbuf;
205 	int i;
206 	void __iomem *p;
207 
208 	pt3 = i2c_get_adapdata(adap);
209 	cbuf = pt3->i2c_buf;
210 
211 	for (i = 0; i < num; i++)
212 		if (msgs[i].flags & I2C_M_RECV_LEN) {
213 			dev_warn(&pt3->pdev->dev,
214 				"(%s) I2C_M_RECV_LEN not supported.\n",
215 				__func__);
216 			return -EINVAL;
217 		}
218 
219 	translate(cbuf, msgs, num);
220 	memcpy_toio(pt3->regs[1] + PT3_I2C_BASE + PT3_CMD_ADDR_NORMAL / 2,
221 			cbuf->data, cbuf->num_cmds);
222 
223 	if (send_i2c_cmd(pt3, PT3_CMD_ADDR_NORMAL) < 0)
224 		return -EIO;
225 
226 	p = pt3->regs[1] + PT3_I2C_BASE;
227 	for (i = 0; i < num; i++)
228 		if ((msgs[i].flags & I2C_M_RD) && msgs[i].len > 0) {
229 			memcpy_fromio(msgs[i].buf, p, msgs[i].len);
230 			p += msgs[i].len;
231 		}
232 
233 	return num;
234 }
235 
236 u32 pt3_i2c_functionality(struct i2c_adapter *adap)
237 {
238 	return I2C_FUNC_I2C;
239 }
240