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
)
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
);
93 static void put_byte_read(struct pt3_i2cbuf
*cbuf
, u32 size
)
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
);
104 static void put_stop(struct pt3_i2cbuf
*cbuf
)
106 cmdbuf_add(cbuf
, I_DATA_L
);
107 cmdbuf_add(cbuf
, I_CLOCK_H
);
108 cmdbuf_add(cbuf
, I_DATA_H
);
112 /* translates msgs to internal commands for bit-banging */
113 static void translate(struct pt3_i2cbuf
*cbuf
, struct i2c_msg
*msgs
, int num
)
119 for (i
= 0; i
< num
; i
++) {
120 rd
= !!(msgs
[i
].flags
& I2C_M_RD
);
122 put_byte_write(cbuf
, msgs
[i
].addr
<< 1 | rd
);
124 put_byte_read(cbuf
, msgs
[i
].len
);
126 for (j
= 0; j
< msgs
[i
].len
; j
++)
127 put_byte_write(cbuf
, msgs
[i
].buf
[j
]);
135 static int wait_i2c_result(struct pt3_board
*pt3
, u32
*result
, int max_wait
)
140 for (i
= 0; i
< max_wait
; i
++) {
141 v
= ioread32(pt3
->regs
[0] + REG_I2C_R
);
142 if (!(v
& STAT_SEQ_RUNNING
))
144 usleep_range(500, 750);
153 /* send [pre-]translated i2c msgs stored at addr */
154 static int send_i2c_cmd(struct pt3_board
*pt3
, u32 addr
)
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",
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__
);
176 /* init commands for each demod are combined into one transaction
177 * and hidden in ROM with the address PT3_CMD_ADDR_INIT_DEMOD.
179 int pt3_init_all_demods(struct pt3_board
*pt3
)
181 ioread32(pt3
->regs
[0] + REG_I2C_R
);
182 return send_i2c_cmd(pt3
, PT3_CMD_ADDR_INIT_DEMOD
);
185 /* init commands for two ISDB-T tuners are hidden in ROM. */
186 int pt3_init_all_mxl301rf(struct pt3_board
*pt3
)
188 usleep_range(1000, 2000);
189 return send_i2c_cmd(pt3
, PT3_CMD_ADDR_INIT_TUNER
);
192 void pt3_i2c_reset(struct pt3_board
*pt3
)
194 iowrite32(PT3_I2C_RESET
, pt3
->regs
[0] + REG_I2C_W
);
201 pt3_i2c_master_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
203 struct pt3_board
*pt3
;
204 struct pt3_i2cbuf
*cbuf
;
208 pt3
= i2c_get_adapdata(adap
);
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",
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
);
223 if (send_i2c_cmd(pt3
, PT3_CMD_ADDR_NORMAL
) < 0)
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
);
236 u32
pt3_i2c_functionality(struct i2c_adapter
*adap
)