4 * Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
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.
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.
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/i2c.h>
20 #include <linux/pci.h>
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)
29 /* masks for I2C status register */
30 #define STAT_SEQ_RUNNING 0x1
31 #define STAT_SEQ_ERROR 0x6
32 #define STAT_NO_SEQ 0x8
34 #define PT3_I2C_RUN (1 << 16)
35 #define PT3_I2C_RESET (1 << 17)
54 static void cmdbuf_add(struct pt3_i2cbuf
*cbuf
, enum ctl_cmd cmd
)
58 if ((cbuf
->num_cmds
% 2) == 0)
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
;
69 static void put_end(struct pt3_i2cbuf
*cbuf
)
71 cmdbuf_add(cbuf
, I_END
);
72 if (cbuf
->num_cmds
% 2)
73 cmdbuf_add(cbuf
, I_END
);
76 static void put_start(struct pt3_i2cbuf
*cbuf
)
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
);
84 static void put_byte_write(struct pt3_i2cbuf
*cbuf
, u8 val
)
89 for (mask
= 0x80; mask
> 0; mask
>>= 1)
90 cmdbuf_add(cbuf
, (val
& mask
) ? I_DATA_H_NOP
: I_DATA_L_NOP
);
91 cmdbuf_add(cbuf
, I_DATA_H_ACK0
);
94 static void put_byte_read(struct pt3_i2cbuf
*cbuf
, u32 size
)
98 for (i
= 0; i
< size
; i
++) {
99 for (j
= 0; j
< 8; j
++)
100 cmdbuf_add(cbuf
, I_DATA_H_READ
);
101 cmdbuf_add(cbuf
, (i
== size
- 1) ? I_DATA_H_NOP
: I_DATA_L_NOP
);
105 static void put_stop(struct pt3_i2cbuf
*cbuf
)
107 cmdbuf_add(cbuf
, I_DATA_L
);
108 cmdbuf_add(cbuf
, I_CLOCK_H
);
109 cmdbuf_add(cbuf
, I_DATA_H
);
113 /* translates msgs to internal commands for bit-banging */
114 static void translate(struct pt3_i2cbuf
*cbuf
, struct i2c_msg
*msgs
, int num
)
120 for (i
= 0; i
< num
; i
++) {
121 rd
= !!(msgs
[i
].flags
& I2C_M_RD
);
123 put_byte_write(cbuf
, msgs
[i
].addr
<< 1 | rd
);
125 put_byte_read(cbuf
, msgs
[i
].len
);
127 for (j
= 0; j
< msgs
[i
].len
; j
++)
128 put_byte_write(cbuf
, msgs
[i
].buf
[j
]);
136 static int wait_i2c_result(struct pt3_board
*pt3
, u32
*result
, int max_wait
)
141 for (i
= 0; i
< max_wait
; i
++) {
142 v
= ioread32(pt3
->regs
[0] + REG_I2C_R
);
143 if (!(v
& STAT_SEQ_RUNNING
))
145 usleep_range(500, 750);
154 /* send [pre-]translated i2c msgs stored at addr */
155 static int send_i2c_cmd(struct pt3_board
*pt3
, u32 addr
)
159 /* make sure that previous transactions had finished */
160 if (wait_i2c_result(pt3
, NULL
, 50)) {
161 dev_warn(&pt3
->pdev
->dev
, "(%s) prev. transaction stalled\n",
166 iowrite32(PT3_I2C_RUN
| addr
, pt3
->regs
[0] + REG_I2C_W
);
167 usleep_range(200, 300);
168 /* wait for the current transaction to finish */
169 if (wait_i2c_result(pt3
, &ret
, 500) || (ret
& STAT_SEQ_ERROR
)) {
170 dev_warn(&pt3
->pdev
->dev
, "(%s) failed.\n", __func__
);
177 /* init commands for each demod are combined into one transaction
178 * and hidden in ROM with the address PT3_CMD_ADDR_INIT_DEMOD.
180 int pt3_init_all_demods(struct pt3_board
*pt3
)
182 ioread32(pt3
->regs
[0] + REG_I2C_R
);
183 return send_i2c_cmd(pt3
, PT3_CMD_ADDR_INIT_DEMOD
);
186 /* init commands for two ISDB-T tuners are hidden in ROM. */
187 int pt3_init_all_mxl301rf(struct pt3_board
*pt3
)
189 usleep_range(1000, 2000);
190 return send_i2c_cmd(pt3
, PT3_CMD_ADDR_INIT_TUNER
);
193 void pt3_i2c_reset(struct pt3_board
*pt3
)
195 iowrite32(PT3_I2C_RESET
, pt3
->regs
[0] + REG_I2C_W
);
202 pt3_i2c_master_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
204 struct pt3_board
*pt3
;
205 struct pt3_i2cbuf
*cbuf
;
209 pt3
= i2c_get_adapdata(adap
);
212 for (i
= 0; i
< num
; i
++)
213 if (msgs
[i
].flags
& I2C_M_RECV_LEN
) {
214 dev_warn(&pt3
->pdev
->dev
,
215 "(%s) I2C_M_RECV_LEN not supported.\n",
220 translate(cbuf
, msgs
, num
);
221 memcpy_toio(pt3
->regs
[1] + PT3_I2C_BASE
+ PT3_CMD_ADDR_NORMAL
/ 2,
222 cbuf
->data
, cbuf
->num_cmds
);
224 if (send_i2c_cmd(pt3
, PT3_CMD_ADDR_NORMAL
) < 0)
227 p
= pt3
->regs
[1] + PT3_I2C_BASE
;
228 for (i
= 0; i
< num
; i
++)
229 if ((msgs
[i
].flags
& I2C_M_RD
) && msgs
[i
].len
> 0) {
230 memcpy_fromio(msgs
[i
].buf
, p
, msgs
[i
].len
);
237 u32
pt3_i2c_functionality(struct i2c_adapter
*adap
)