1/* $NetBSD: nouveau_subdev_i2c_nv50.c,v 1.1.1.1 2014/08/06 12:36:30 riastradh Exp $ */
2
3/*
4 * Copyright 2012 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_i2c_nv50.c,v 1.1.1.1 2014/08/06 12:36:30 riastradh Exp $");
29
30#include "nv50.h"
31
32void
33nv50_i2c_drive_scl(struct nouveau_i2c_port *base, int state)
34{
35 struct nv50_i2c_priv *priv = (void *)nv_object(base)->engine;
36 struct nv50_i2c_port *port = (void *)base;
37 if (state) port->state |= 0x01;
38 else port->state &= 0xfe;
39 nv_wr32(priv, port->addr, port->state);
40}
41
42void
43nv50_i2c_drive_sda(struct nouveau_i2c_port *base, int state)
44{
45 struct nv50_i2c_priv *priv = (void *)nv_object(base)->engine;
46 struct nv50_i2c_port *port = (void *)base;
47 if (state) port->state |= 0x02;
48 else port->state &= 0xfd;
49 nv_wr32(priv, port->addr, port->state);
50}
51
52int
53nv50_i2c_sense_scl(struct nouveau_i2c_port *base)
54{
55 struct nv50_i2c_priv *priv = (void *)nv_object(base)->engine;
56 struct nv50_i2c_port *port = (void *)base;
57 return !!(nv_rd32(priv, port->addr) & 0x00000001);
58}
59
60int
61nv50_i2c_sense_sda(struct nouveau_i2c_port *base)
62{
63 struct nv50_i2c_priv *priv = (void *)nv_object(base)->engine;
64 struct nv50_i2c_port *port = (void *)base;
65 return !!(nv_rd32(priv, port->addr) & 0x00000002);
66}
67
68static const struct nouveau_i2c_func
69nv50_i2c_func = {
70 .drive_scl = nv50_i2c_drive_scl,
71 .drive_sda = nv50_i2c_drive_sda,
72 .sense_scl = nv50_i2c_sense_scl,
73 .sense_sda = nv50_i2c_sense_sda,
74};
75
76const u32 nv50_i2c_addr[] = {
77 0x00e138, 0x00e150, 0x00e168, 0x00e180,
78 0x00e254, 0x00e274, 0x00e764, 0x00e780,
79 0x00e79c, 0x00e7b8
80};
81const int nv50_i2c_addr_nr = ARRAY_SIZE(nv50_i2c_addr);
82
83static int
84nv50_i2c_port_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
85 struct nouveau_oclass *oclass, void *data, u32 index,
86 struct nouveau_object **pobject)
87{
88 struct dcb_i2c_entry *info = data;
89 struct nv50_i2c_port *port;
90 int ret;
91
92 ret = nouveau_i2c_port_create(parent, engine, oclass, index,
93 &nouveau_i2c_bit_algo, &nv50_i2c_func,
94 &port);
95 *pobject = nv_object(port);
96 if (ret)
97 return ret;
98
99 if (info->drive >= nv50_i2c_addr_nr)
100 return -EINVAL;
101
102 port->state = 0x00000007;
103 port->addr = nv50_i2c_addr[info->drive];
104 return 0;
105}
106
107int
108nv50_i2c_port_init(struct nouveau_object *object)
109{
110 struct nv50_i2c_priv *priv = (void *)object->engine;
111 struct nv50_i2c_port *port = (void *)object;
112 nv_wr32(priv, port->addr, port->state);
113 return nouveau_i2c_port_init(&port->base);
114}
115
116static struct nouveau_oclass
117nv50_i2c_sclass[] = {
118 { .handle = NV_I2C_TYPE_DCBI2C(DCB_I2C_NVIO_BIT),
119 .ofuncs = &(struct nouveau_ofuncs) {
120 .ctor = nv50_i2c_port_ctor,
121 .dtor = _nouveau_i2c_port_dtor,
122 .init = nv50_i2c_port_init,
123 .fini = _nouveau_i2c_port_fini,
124 },
125 },
126 {}
127};
128
129static int
130nv50_i2c_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
131 struct nouveau_oclass *oclass, void *data, u32 size,
132 struct nouveau_object **pobject)
133{
134 struct nv50_i2c_priv *priv;
135 int ret;
136
137 ret = nouveau_i2c_create(parent, engine, oclass, nv50_i2c_sclass, &priv);
138 *pobject = nv_object(priv);
139 if (ret)
140 return ret;
141
142 return 0;
143}
144
145struct nouveau_oclass
146nv50_i2c_oclass = {
147 .handle = NV_SUBDEV(I2C, 0x50),
148 .ofuncs = &(struct nouveau_ofuncs) {
149 .ctor = nv50_i2c_ctor,
150 .dtor = _nouveau_i2c_dtor,
151 .init = _nouveau_i2c_init,
152 .fini = _nouveau_i2c_fini,
153 },
154};
155