2 * Copyright 2013 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Authors: Ben Skeggs <bskeggs@redhat.com>
24 #define anx9805_pad(p) container_of((p), struct anx9805_pad, base)
25 #define anx9805_bus(p) container_of((p), struct anx9805_bus, base)
26 #define anx9805_aux(p) container_of((p), struct anx9805_aux, base)
31 struct nvkm_i2c_pad base
;
32 struct nvkm_i2c_bus
*bus
;
37 struct nvkm_i2c_bus base
;
38 struct anx9805_pad
*pad
;
43 anx9805_bus_xfer(struct nvkm_i2c_bus
*base
, struct i2c_msg
*msgs
, int num
)
45 struct anx9805_bus
*bus
= anx9805_bus(base
);
46 struct anx9805_pad
*pad
= bus
->pad
;
47 struct i2c_adapter
*adap
= &pad
->bus
->i2c
;
48 struct i2c_msg
*msg
= msgs
;
51 u8 seg
= 0x00, off
= 0x00, tmp
;
53 tmp
= nvkm_rdi2cr(adap
, pad
->addr
, 0x07) & ~0x10;
54 nvkm_wri2cr(adap
, pad
->addr
, 0x07, tmp
| 0x10);
55 nvkm_wri2cr(adap
, pad
->addr
, 0x07, tmp
);
56 nvkm_wri2cr(adap
, bus
->addr
, 0x43, 0x05);
60 if ( (msg
->flags
& I2C_M_RD
) && msg
->addr
== 0x50) {
61 nvkm_wri2cr(adap
, bus
->addr
, 0x40, msg
->addr
<< 1);
62 nvkm_wri2cr(adap
, bus
->addr
, 0x41, seg
);
63 nvkm_wri2cr(adap
, bus
->addr
, 0x42, off
);
64 nvkm_wri2cr(adap
, bus
->addr
, 0x44, msg
->len
);
65 nvkm_wri2cr(adap
, bus
->addr
, 0x45, 0x00);
66 nvkm_wri2cr(adap
, bus
->addr
, 0x43, 0x01);
67 for (i
= 0; i
< msg
->len
; i
++) {
69 while (nvkm_rdi2cr(adap
, bus
->addr
, 0x46) & 0x10) {
74 msg
->buf
[i
] = nvkm_rdi2cr(adap
, bus
->addr
, 0x47);
77 if (!(msg
->flags
& I2C_M_RD
)) {
78 if (msg
->addr
== 0x50 && msg
->len
== 0x01) {
81 if (msg
->addr
== 0x30 && msg
->len
== 0x01) {
93 nvkm_wri2cr(adap
, bus
->addr
, 0x43, 0x00);
97 static const struct nvkm_i2c_bus_func
99 .xfer
= anx9805_bus_xfer
,
103 anx9805_bus_new(struct nvkm_i2c_pad
*base
, int id
, u8 drive
,
104 struct nvkm_i2c_bus
**pbus
)
106 struct anx9805_pad
*pad
= anx9805_pad(base
);
107 struct anx9805_bus
*bus
;
110 if (!(bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
)))
115 ret
= nvkm_i2c_bus_ctor(&anx9805_bus_func
, &pad
->base
, id
, &bus
->base
);
120 case 0x39: bus
->addr
= 0x3d; break;
121 case 0x3b: bus
->addr
= 0x3f; break;
130 struct nvkm_i2c_aux base
;
131 struct anx9805_pad
*pad
;
136 anx9805_aux_xfer(struct nvkm_i2c_aux
*base
, bool retry
,
137 u8 type
, u32 addr
, u8
*data
, u8
*size
)
139 struct anx9805_aux
*aux
= anx9805_aux(base
);
140 struct anx9805_pad
*pad
= aux
->pad
;
141 struct i2c_adapter
*adap
= &pad
->bus
->i2c
;
142 int i
, ret
= -ETIMEDOUT
;
146 AUX_DBG(&aux
->base
, "%02x %05x %d", type
, addr
, *size
);
148 tmp
= nvkm_rdi2cr(adap
, pad
->addr
, 0x07) & ~0x04;
149 nvkm_wri2cr(adap
, pad
->addr
, 0x07, tmp
| 0x04);
150 nvkm_wri2cr(adap
, pad
->addr
, 0x07, tmp
);
151 nvkm_wri2cr(adap
, pad
->addr
, 0xf7, 0x01);
153 nvkm_wri2cr(adap
, aux
->addr
, 0xe4, 0x80);
155 memcpy(buf
, data
, *size
);
156 AUX_DBG(&aux
->base
, "%16ph", buf
);
157 for (i
= 0; i
< *size
; i
++)
158 nvkm_wri2cr(adap
, aux
->addr
, 0xf0 + i
, buf
[i
]);
160 nvkm_wri2cr(adap
, aux
->addr
, 0xe5, ((*size
- 1) << 4) | type
);
161 nvkm_wri2cr(adap
, aux
->addr
, 0xe6, (addr
& 0x000ff) >> 0);
162 nvkm_wri2cr(adap
, aux
->addr
, 0xe7, (addr
& 0x0ff00) >> 8);
163 nvkm_wri2cr(adap
, aux
->addr
, 0xe8, (addr
& 0xf0000) >> 16);
164 nvkm_wri2cr(adap
, aux
->addr
, 0xe9, 0x01);
167 while ((tmp
= nvkm_rdi2cr(adap
, aux
->addr
, 0xe9)) & 0x01) {
173 if ((tmp
= nvkm_rdi2cr(adap
, pad
->addr
, 0xf7)) & 0x01) {
179 for (i
= 0; i
< *size
; i
++)
180 buf
[i
] = nvkm_rdi2cr(adap
, aux
->addr
, 0xf0 + i
);
181 AUX_DBG(&aux
->base
, "%16ph", buf
);
182 memcpy(data
, buf
, *size
);
187 nvkm_wri2cr(adap
, pad
->addr
, 0xf7, 0x01);
192 anx9805_aux_lnk_ctl(struct nvkm_i2c_aux
*base
,
193 int link_nr
, int link_bw
, bool enh
)
195 struct anx9805_aux
*aux
= anx9805_aux(base
);
196 struct anx9805_pad
*pad
= aux
->pad
;
197 struct i2c_adapter
*adap
= &pad
->bus
->i2c
;
200 AUX_DBG(&aux
->base
, "ANX9805 train %d %02x %d",
201 link_nr
, link_bw
, enh
);
203 nvkm_wri2cr(adap
, aux
->addr
, 0xa0, link_bw
);
204 nvkm_wri2cr(adap
, aux
->addr
, 0xa1, link_nr
| (enh
? 0x80 : 0x00));
205 nvkm_wri2cr(adap
, aux
->addr
, 0xa2, 0x01);
206 nvkm_wri2cr(adap
, aux
->addr
, 0xa8, 0x01);
209 while ((tmp
= nvkm_rdi2cr(adap
, aux
->addr
, 0xa8)) & 0x01) {
212 AUX_ERR(&aux
->base
, "link training timeout");
218 AUX_ERR(&aux
->base
, "link training failed");
225 static const struct nvkm_i2c_aux_func
227 .xfer
= anx9805_aux_xfer
,
228 .lnk_ctl
= anx9805_aux_lnk_ctl
,
232 anx9805_aux_new(struct nvkm_i2c_pad
*base
, int id
, u8 drive
,
233 struct nvkm_i2c_aux
**pbus
)
235 struct anx9805_pad
*pad
= anx9805_pad(base
);
236 struct anx9805_aux
*aux
;
239 if (!(aux
= kzalloc(sizeof(*aux
), GFP_KERNEL
)))
244 ret
= nvkm_i2c_aux_ctor(&anx9805_aux_func
, &pad
->base
, id
, &aux
->base
);
249 case 0x39: aux
->addr
= 0x38; break;
250 case 0x3b: aux
->addr
= 0x3c; break;
258 static const struct nvkm_i2c_pad_func
260 .bus_new_4
= anx9805_bus_new
,
261 .aux_new_6
= anx9805_aux_new
,
265 anx9805_pad_new(struct nvkm_i2c_bus
*bus
, int id
, u8 addr
,
266 struct nvkm_i2c_pad
**ppad
)
268 struct anx9805_pad
*pad
;
270 if (!(pad
= kzalloc(sizeof(*pad
), GFP_KERNEL
)))
274 nvkm_i2c_pad_ctor(&anx9805_pad_func
, bus
->pad
->i2c
, id
, &pad
->base
);