ff50ed79e17599170b096a9582e197ee5c257cf3
[cascardo/linux.git] / drivers / gpu / drm / nouveau / core / subdev / i2c / nvd0.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #include "nv50.h"
26
27 static int
28 nvd0_i2c_sense_scl(struct nouveau_i2c_port *base)
29 {
30         struct nv50_i2c_priv *priv = (void *)nv_object(base)->engine;
31         struct nv50_i2c_port *port = (void *)base;
32         return !!(nv_rd32(priv, port->addr) & 0x00000010);
33 }
34
35 static int
36 nvd0_i2c_sense_sda(struct nouveau_i2c_port *base)
37 {
38         struct nv50_i2c_priv *priv = (void *)nv_object(base)->engine;
39         struct nv50_i2c_port *port = (void *)base;
40         return !!(nv_rd32(priv, port->addr) & 0x00000020);
41 }
42
43 static const struct nouveau_i2c_func
44 nvd0_i2c_func = {
45         .drive_scl = nv50_i2c_drive_scl,
46         .drive_sda = nv50_i2c_drive_sda,
47         .sense_scl = nvd0_i2c_sense_scl,
48         .sense_sda = nvd0_i2c_sense_sda,
49 };
50
51 static int
52 nvd0_i2c_port_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
53                    struct nouveau_oclass *oclass, void *data, u32 index,
54                    struct nouveau_object **pobject)
55 {
56         struct dcb_i2c_entry *info = data;
57         struct nv50_i2c_port *port;
58         int ret;
59
60         ret = nouveau_i2c_port_create(parent, engine, oclass, index,
61                                       &nouveau_i2c_bit_algo, &nvd0_i2c_func,
62                                       &port);
63         *pobject = nv_object(port);
64         if (ret)
65                 return ret;
66
67         port->state = 0x00000007;
68         port->addr = 0x00d014 + (info->drive * 0x20);
69         return 0;
70 }
71
72 struct nouveau_oclass
73 nvd0_i2c_sclass[] = {
74         { .handle = NV_I2C_TYPE_DCBI2C(DCB_I2C_NVIO_BIT),
75           .ofuncs = &(struct nouveau_ofuncs) {
76                   .ctor = nvd0_i2c_port_ctor,
77                   .dtor = _nouveau_i2c_port_dtor,
78                   .init = nv50_i2c_port_init,
79                   .fini = _nouveau_i2c_port_fini,
80           },
81         },
82         { .handle = NV_I2C_TYPE_DCBI2C(DCB_I2C_NVIO_AUX),
83           .ofuncs = &(struct nouveau_ofuncs) {
84                   .ctor = nv94_aux_port_ctor,
85                   .dtor = _nouveau_i2c_port_dtor,
86                   .init = _nouveau_i2c_port_init,
87                   .fini = _nouveau_i2c_port_fini,
88           },
89         },
90         {}
91 };
92
93 struct nouveau_oclass *
94 nvd0_i2c_oclass = &(struct nouveau_i2c_impl) {
95         .base.handle = NV_SUBDEV(I2C, 0xd0),
96         .base.ofuncs = &(struct nouveau_ofuncs) {
97                 .ctor = _nouveau_i2c_ctor,
98                 .dtor = _nouveau_i2c_dtor,
99                 .init = _nouveau_i2c_init,
100                 .fini = _nouveau_i2c_fini,
101         },
102         .sclass = nvd0_i2c_sclass,
103         .pad_x = &nv04_i2c_pad_oclass,
104         .pad_s = &nv94_i2c_pad_oclass,
105         .aux = 4,
106         .aux_stat = nv94_aux_stat,
107         .aux_mask = nv94_aux_mask,
108 }.base;