• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 void
nv50_i2c_drive_scl(struct nouveau_i2c_port * base,int state)28 nv50_i2c_drive_scl(struct nouveau_i2c_port *base, int state)
29 {
30 	struct nv50_i2c_priv *priv = (void *)nv_object(base)->engine;
31 	struct nv50_i2c_port *port = (void *)base;
32 	if (state) port->state |= 0x01;
33 	else	   port->state &= 0xfe;
34 	nv_wr32(priv, port->addr, port->state);
35 }
36 
37 void
nv50_i2c_drive_sda(struct nouveau_i2c_port * base,int state)38 nv50_i2c_drive_sda(struct nouveau_i2c_port *base, int state)
39 {
40 	struct nv50_i2c_priv *priv = (void *)nv_object(base)->engine;
41 	struct nv50_i2c_port *port = (void *)base;
42 	if (state) port->state |= 0x02;
43 	else	   port->state &= 0xfd;
44 	nv_wr32(priv, port->addr, port->state);
45 }
46 
47 int
nv50_i2c_sense_scl(struct nouveau_i2c_port * base)48 nv50_i2c_sense_scl(struct nouveau_i2c_port *base)
49 {
50 	struct nv50_i2c_priv *priv = (void *)nv_object(base)->engine;
51 	struct nv50_i2c_port *port = (void *)base;
52 	return !!(nv_rd32(priv, port->addr) & 0x00000001);
53 }
54 
55 int
nv50_i2c_sense_sda(struct nouveau_i2c_port * base)56 nv50_i2c_sense_sda(struct nouveau_i2c_port *base)
57 {
58 	struct nv50_i2c_priv *priv = (void *)nv_object(base)->engine;
59 	struct nv50_i2c_port *port = (void *)base;
60 	return !!(nv_rd32(priv, port->addr) & 0x00000002);
61 }
62 
63 static const struct nouveau_i2c_func
64 nv50_i2c_func = {
65 	.drive_scl = nv50_i2c_drive_scl,
66 	.drive_sda = nv50_i2c_drive_sda,
67 	.sense_scl = nv50_i2c_sense_scl,
68 	.sense_sda = nv50_i2c_sense_sda,
69 };
70 
71 const u32 nv50_i2c_addr[] = {
72 	0x00e138, 0x00e150, 0x00e168, 0x00e180,
73 	0x00e254, 0x00e274, 0x00e764, 0x00e780,
74 	0x00e79c, 0x00e7b8
75 };
76 const int nv50_i2c_addr_nr = ARRAY_SIZE(nv50_i2c_addr);
77 
78 static int
nv50_i2c_port_ctor(struct nouveau_object * parent,struct nouveau_object * engine,struct nouveau_oclass * oclass,void * data,u32 index,struct nouveau_object ** pobject)79 nv50_i2c_port_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
80 		   struct nouveau_oclass *oclass, void *data, u32 index,
81 		   struct nouveau_object **pobject)
82 {
83 	struct dcb_i2c_entry *info = data;
84 	struct nv50_i2c_port *port;
85 	int ret;
86 
87 	ret = nouveau_i2c_port_create(parent, engine, oclass, index,
88 				     &nouveau_i2c_bit_algo, &port);
89 	*pobject = nv_object(port);
90 	if (ret)
91 		return ret;
92 
93 	if (info->drive >= nv50_i2c_addr_nr)
94 		return -EINVAL;
95 
96 	port->base.func = &nv50_i2c_func;
97 	port->state = 0x00000007;
98 	port->addr = nv50_i2c_addr[info->drive];
99 	return 0;
100 }
101 
102 int
nv50_i2c_port_init(struct nouveau_object * object)103 nv50_i2c_port_init(struct nouveau_object *object)
104 {
105 	struct nv50_i2c_priv *priv = (void *)object->engine;
106 	struct nv50_i2c_port *port = (void *)object;
107 	nv_wr32(priv, port->addr, port->state);
108 	return nouveau_i2c_port_init(&port->base);
109 }
110 
111 static struct nouveau_oclass
112 nv50_i2c_sclass[] = {
113 	{ .handle = NV_I2C_TYPE_DCBI2C(DCB_I2C_NVIO_BIT),
114 	  .ofuncs = &(struct nouveau_ofuncs) {
115 		  .ctor = nv50_i2c_port_ctor,
116 		  .dtor = _nouveau_i2c_port_dtor,
117 		  .init = nv50_i2c_port_init,
118 		  .fini = _nouveau_i2c_port_fini,
119 	  },
120 	},
121 	{}
122 };
123 
124 static int
nv50_i2c_ctor(struct nouveau_object * parent,struct nouveau_object * engine,struct nouveau_oclass * oclass,void * data,u32 size,struct nouveau_object ** pobject)125 nv50_i2c_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
126 	      struct nouveau_oclass *oclass, void *data, u32 size,
127 	      struct nouveau_object **pobject)
128 {
129 	struct nv50_i2c_priv *priv;
130 	int ret;
131 
132 	ret = nouveau_i2c_create(parent, engine, oclass, nv50_i2c_sclass, &priv);
133 	*pobject = nv_object(priv);
134 	if (ret)
135 		return ret;
136 
137 	return 0;
138 }
139 
140 struct nouveau_oclass
141 nv50_i2c_oclass = {
142 	.handle = NV_SUBDEV(I2C, 0x50),
143 	.ofuncs = &(struct nouveau_ofuncs) {
144 		.ctor = nv50_i2c_ctor,
145 		.dtor = _nouveau_i2c_dtor,
146 		.init = _nouveau_i2c_init,
147 		.fini = _nouveau_i2c_fini,
148 	},
149 };
150