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 #include "priv.h"
25 
26 #include <subdev/vga.h>
27 
28 struct nv04_i2c_port {
29 	struct nvkm_i2c_port base;
30 	u8 drive;
31 	u8 sense;
32 };
33 
34 static void
35 nv04_i2c_drive_scl(struct nvkm_i2c_port *base, int state)
36 {
37 	struct nvkm_i2c *i2c = (void *)nvkm_i2c(base);
38 	struct nv04_i2c_port *port = (void *)base;
39 	u8 val = nv_rdvgac(i2c, 0, port->drive);
40 	if (state) val |= 0x20;
41 	else	   val &= 0xdf;
42 	nv_wrvgac(i2c, 0, port->drive, val | 0x01);
43 }
44 
45 static void
46 nv04_i2c_drive_sda(struct nvkm_i2c_port *base, int state)
47 {
48 	struct nvkm_i2c *i2c = (void *)nvkm_i2c(base);
49 	struct nv04_i2c_port *port = (void *)base;
50 	u8 val = nv_rdvgac(i2c, 0, port->drive);
51 	if (state) val |= 0x10;
52 	else	   val &= 0xef;
53 	nv_wrvgac(i2c, 0, port->drive, val | 0x01);
54 }
55 
56 static int
57 nv04_i2c_sense_scl(struct nvkm_i2c_port *base)
58 {
59 	struct nvkm_i2c *i2c = (void *)nvkm_i2c(base);
60 	struct nv04_i2c_port *port = (void *)base;
61 	return !!(nv_rdvgac(i2c, 0, port->sense) & 0x04);
62 }
63 
64 static int
65 nv04_i2c_sense_sda(struct nvkm_i2c_port *base)
66 {
67 	struct nvkm_i2c *i2c = (void *)nvkm_i2c(base);
68 	struct nv04_i2c_port *port = (void *)base;
69 	return !!(nv_rdvgac(i2c, 0, port->sense) & 0x08);
70 }
71 
72 static const struct nvkm_i2c_func
73 nv04_i2c_func = {
74 	.drive_scl = nv04_i2c_drive_scl,
75 	.drive_sda = nv04_i2c_drive_sda,
76 	.sense_scl = nv04_i2c_sense_scl,
77 	.sense_sda = nv04_i2c_sense_sda,
78 };
79 
80 static int
81 nv04_i2c_port_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
82 		   struct nvkm_oclass *oclass, void *data, u32 index,
83 		   struct nvkm_object **pobject)
84 {
85 	struct dcb_i2c_entry *info = data;
86 	struct nv04_i2c_port *port;
87 	int ret;
88 
89 	ret = nvkm_i2c_port_create(parent, engine, oclass, index,
90 				   &nvkm_i2c_bit_algo, &nv04_i2c_func, &port);
91 	*pobject = nv_object(port);
92 	if (ret)
93 		return ret;
94 
95 	port->drive = info->drive;
96 	port->sense = info->sense;
97 	return 0;
98 }
99 
100 static struct nvkm_oclass
101 nv04_i2c_sclass[] = {
102 	{ .handle = NV_I2C_TYPE_DCBI2C(DCB_I2C_NV04_BIT),
103 	  .ofuncs = &(struct nvkm_ofuncs) {
104 		  .ctor = nv04_i2c_port_ctor,
105 		  .dtor = _nvkm_i2c_port_dtor,
106 		  .init = _nvkm_i2c_port_init,
107 		  .fini = _nvkm_i2c_port_fini,
108 	  },
109 	},
110 	{}
111 };
112 
113 struct nvkm_oclass *
114 nv04_i2c_oclass = &(struct nvkm_i2c_impl) {
115 	.base.handle = NV_SUBDEV(I2C, 0x04),
116 	.base.ofuncs = &(struct nvkm_ofuncs) {
117 		.ctor = _nvkm_i2c_ctor,
118 		.dtor = _nvkm_i2c_dtor,
119 		.init = _nvkm_i2c_init,
120 		.fini = _nvkm_i2c_fini,
121 	},
122 	.sclass = nv04_i2c_sclass,
123 	.pad_x = &nv04_i2c_pad_oclass,
124 }.base;
125