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.
25 #include <core/option.h>
27 #include <subdev/bios.h>
28 #include <subdev/bios/dcb.h>
29 #include <subdev/bios/i2c.h>
30 #include <subdev/i2c.h>
31 #include <subdev/vga.h>
33 /******************************************************************************
34 * interface to linux i2c bit-banging algorithm
35 *****************************************************************************/
37 #ifdef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT
44 nouveau_i2c_pre_xfer(struct i2c_adapter
*adap
)
46 struct i2c_algo_bit_data
*bit
= adap
->algo_data
;
47 struct nouveau_i2c_port
*port
= bit
->data
;
48 if (port
->func
->acquire
)
49 port
->func
->acquire(port
);
54 nouveau_i2c_setscl(void *data
, int state
)
56 struct nouveau_i2c_port
*port
= data
;
57 port
->func
->drive_scl(port
, state
);
61 nouveau_i2c_setsda(void *data
, int state
)
63 struct nouveau_i2c_port
*port
= data
;
64 port
->func
->drive_sda(port
, state
);
68 nouveau_i2c_getscl(void *data
)
70 struct nouveau_i2c_port
*port
= data
;
71 return port
->func
->sense_scl(port
);
75 nouveau_i2c_getsda(void *data
)
77 struct nouveau_i2c_port
*port
= data
;
78 return port
->func
->sense_sda(port
);
81 /******************************************************************************
82 * base i2c "port" class implementation
83 *****************************************************************************/
86 _nouveau_i2c_port_dtor(struct nouveau_object
*object
)
88 struct nouveau_i2c_port
*port
= (void *)object
;
89 i2c_del_adapter(&port
->adapter
);
90 nouveau_object_destroy(&port
->base
);
94 nouveau_i2c_port_create_(struct nouveau_object
*parent
,
95 struct nouveau_object
*engine
,
96 struct nouveau_oclass
*oclass
, u8 index
,
97 const struct i2c_algorithm
*algo
,
98 int size
, void **pobject
)
100 struct nouveau_device
*device
= nv_device(parent
);
101 struct nouveau_i2c
*i2c
= (void *)engine
;
102 struct nouveau_i2c_port
*port
;
105 ret
= nouveau_object_create_(parent
, engine
, oclass
, 0, size
, pobject
);
110 snprintf(port
->adapter
.name
, sizeof(port
->adapter
.name
),
111 "nouveau-%s-%d", device
->name
, index
);
112 port
->adapter
.owner
= THIS_MODULE
;
113 port
->adapter
.dev
.parent
= &device
->pdev
->dev
;
115 i2c_set_adapdata(&port
->adapter
, i2c
);
117 if ( algo
== &nouveau_i2c_bit_algo
&&
118 !nouveau_boolopt(device
->cfgopt
, "NvI2C", CSTMSEL
)) {
119 struct i2c_algo_bit_data
*bit
;
121 bit
= kzalloc(sizeof(*bit
), GFP_KERNEL
);
126 bit
->timeout
= usecs_to_jiffies(2200);
128 bit
->pre_xfer
= nouveau_i2c_pre_xfer
;
129 bit
->setsda
= nouveau_i2c_setsda
;
130 bit
->setscl
= nouveau_i2c_setscl
;
131 bit
->getsda
= nouveau_i2c_getsda
;
132 bit
->getscl
= nouveau_i2c_getscl
;
134 port
->adapter
.algo_data
= bit
;
135 ret
= i2c_bit_add_bus(&port
->adapter
);
137 port
->adapter
.algo_data
= port
;
138 port
->adapter
.algo
= algo
;
139 ret
= i2c_add_adapter(&port
->adapter
);
142 /* drop port's i2c subdev refcount, i2c handles this itself */
144 list_add_tail(&port
->head
, &i2c
->ports
);
145 atomic_dec(&engine
->refcount
);
151 /******************************************************************************
152 * base i2c subdev class implementation
153 *****************************************************************************/
155 static struct nouveau_i2c_port
*
156 nouveau_i2c_find(struct nouveau_i2c
*i2c
, u8 index
)
158 struct nouveau_bios
*bios
= nouveau_bios(i2c
);
159 struct nouveau_i2c_port
*port
;
161 if (index
== NV_I2C_DEFAULT(0) ||
162 index
== NV_I2C_DEFAULT(1)) {
163 u8 ver
, hdr
, cnt
, len
;
164 u16 i2c
= dcb_i2c_table(bios
, &ver
, &hdr
, &cnt
, &len
);
165 if (i2c
&& ver
>= 0x30) {
166 u8 auxidx
= nv_ro08(bios
, i2c
+ 4);
167 if (index
== NV_I2C_DEFAULT(0))
168 index
= (auxidx
& 0x0f) >> 0;
170 index
= (auxidx
& 0xf0) >> 4;
176 list_for_each_entry(port
, &i2c
->ports
, head
) {
177 if (port
->index
== index
)
184 static struct nouveau_i2c_port
*
185 nouveau_i2c_find_type(struct nouveau_i2c
*i2c
, u16 type
)
187 struct nouveau_i2c_port
*port
;
189 list_for_each_entry(port
, &i2c
->ports
, head
) {
190 if (nv_hclass(port
) == type
)
198 nouveau_i2c_identify(struct nouveau_i2c
*i2c
, int index
, const char *what
,
199 struct i2c_board_info
*info
,
200 bool (*match
)(struct nouveau_i2c_port
*,
201 struct i2c_board_info
*))
203 struct nouveau_i2c_port
*port
= nouveau_i2c_find(i2c
, index
);
207 nv_debug(i2c
, "no bus when probing %s on %d\n", what
, index
);
211 nv_debug(i2c
, "probing %ss on bus: %d\n", what
, port
->index
);
212 for (i
= 0; info
[i
].addr
; i
++) {
213 if (nv_probe_i2c(port
, info
[i
].addr
) &&
214 (!match
|| match(port
, &info
[i
]))) {
215 nv_info(i2c
, "detected %s: %s\n", what
, info
[i
].type
);
220 nv_debug(i2c
, "no devices found.\n");
225 _nouveau_i2c_fini(struct nouveau_object
*object
, bool suspend
)
227 struct nouveau_i2c
*i2c
= (void *)object
;
228 struct nouveau_i2c_port
*port
;
231 list_for_each_entry(port
, &i2c
->ports
, head
) {
232 ret
= nv_ofuncs(port
)->fini(nv_object(port
), suspend
);
237 return nouveau_subdev_fini(&i2c
->base
, suspend
);
239 list_for_each_entry_continue_reverse(port
, &i2c
->ports
, head
) {
240 nv_ofuncs(port
)->init(nv_object(port
));
247 _nouveau_i2c_init(struct nouveau_object
*object
)
249 struct nouveau_i2c
*i2c
= (void *)object
;
250 struct nouveau_i2c_port
*port
;
253 ret
= nouveau_subdev_init(&i2c
->base
);
255 list_for_each_entry(port
, &i2c
->ports
, head
) {
256 ret
= nv_ofuncs(port
)->init(nv_object(port
));
264 list_for_each_entry_continue_reverse(port
, &i2c
->ports
, head
) {
265 nv_ofuncs(port
)->fini(nv_object(port
), false);
272 _nouveau_i2c_dtor(struct nouveau_object
*object
)
274 struct nouveau_i2c
*i2c
= (void *)object
;
275 struct nouveau_i2c_port
*port
, *temp
;
277 list_for_each_entry_safe(port
, temp
, &i2c
->ports
, head
) {
278 nouveau_object_ref(NULL
, (struct nouveau_object
**)&port
);
281 nouveau_subdev_destroy(&i2c
->base
);
284 static struct nouveau_oclass
*
285 nouveau_i2c_extdev_sclass
[] = {
286 nouveau_anx9805_sclass
,
290 nouveau_i2c_create_(struct nouveau_object
*parent
,
291 struct nouveau_object
*engine
,
292 struct nouveau_oclass
*oclass
,
293 struct nouveau_oclass
*sclass
,
294 int length
, void **pobject
)
296 struct nouveau_bios
*bios
= nouveau_bios(parent
);
297 struct nouveau_i2c
*i2c
;
298 struct nouveau_object
*object
;
299 struct dcb_i2c_entry info
;
300 int ret
, i
, j
, index
= -1;
301 struct dcb_output outp
;
305 ret
= nouveau_subdev_create(parent
, engine
, oclass
, 0,
307 *pobject
= nv_object(i2c
);
311 i2c
->find
= nouveau_i2c_find
;
312 i2c
->find_type
= nouveau_i2c_find_type
;
313 i2c
->identify
= nouveau_i2c_identify
;
314 INIT_LIST_HEAD(&i2c
->ports
);
316 while (!dcb_i2c_parse(bios
, ++index
, &info
)) {
317 if (info
.type
== DCB_I2C_UNUSED
)
323 if (oclass
->handle
== info
.type
) {
324 ret
= nouveau_object_ctor(*pobject
, *pobject
,
328 } while (ret
&& (++oclass
)->handle
);
331 /* in addition to the busses specified in the i2c table, there
332 * may be ddc/aux channels hiding behind external tmds/dp/etc
335 index
= ((index
+ 0x0f) / 0x10) * 0x10;
337 while ((data
= dcb_outp_parse(bios
, ++i
, &ver
, &hdr
, &outp
))) {
338 if (!outp
.location
|| !outp
.extdev
)
342 case DCB_OUTPUT_TMDS
:
343 info
.type
= NV_I2C_TYPE_EXTDDC(outp
.extdev
);
346 info
.type
= NV_I2C_TYPE_EXTAUX(outp
.extdev
);
354 while (ret
&& ++j
< ARRAY_SIZE(nouveau_i2c_extdev_sclass
)) {
355 parent
= nv_object(i2c
->find(i2c
, outp
.i2c_index
));
356 oclass
= nouveau_i2c_extdev_sclass
[j
];
358 if (oclass
->handle
!= info
.type
)
360 ret
= nouveau_object_ctor(parent
, *pobject
,
363 } while (ret
&& (++oclass
)->handle
);