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 nv4e_i2c_port {
29 	struct nvkm_i2c_port base;
30 	u32 addr;
31 };
32 
33 static void
34 nv4e_i2c_drive_scl(struct nvkm_i2c_port *base, int state)
35 {
36 	struct nvkm_i2c *i2c = (void *)nvkm_i2c(base);
37 	struct nv4e_i2c_port *port = (void *)base;
38 	nv_mask(i2c, port->addr, 0x2f, state ? 0x21 : 0x01);
39 }
40 
41 static void
42 nv4e_i2c_drive_sda(struct nvkm_i2c_port *base, int state)
43 {
44 	struct nvkm_i2c *i2c = (void *)nvkm_i2c(base);
45 	struct nv4e_i2c_port *port = (void *)base;
46 	nv_mask(i2c, port->addr, 0x1f, state ? 0x11 : 0x01);
47 }
48 
49 static int
50 nv4e_i2c_sense_scl(struct nvkm_i2c_port *base)
51 {
52 	struct nvkm_i2c *i2c = (void *)nvkm_i2c(base);
53 	struct nv4e_i2c_port *port = (void *)base;
54 	return !!(nv_rd32(i2c, port->addr) & 0x00040000);
55 }
56 
57 static int
58 nv4e_i2c_sense_sda(struct nvkm_i2c_port *base)
59 {
60 	struct nvkm_i2c *i2c = (void *)nvkm_i2c(base);
61 	struct nv4e_i2c_port *port = (void *)base;
62 	return !!(nv_rd32(i2c, port->addr) & 0x00080000);
63 }
64 
65 static const struct nvkm_i2c_func
66 nv4e_i2c_func = {
67 	.drive_scl = nv4e_i2c_drive_scl,
68 	.drive_sda = nv4e_i2c_drive_sda,
69 	.sense_scl = nv4e_i2c_sense_scl,
70 	.sense_sda = nv4e_i2c_sense_sda,
71 };
72 
73 static int
74 nv4e_i2c_port_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
75 		   struct nvkm_oclass *oclass, void *data, u32 index,
76 		   struct nvkm_object **pobject)
77 {
78 	struct dcb_i2c_entry *info = data;
79 	struct nv4e_i2c_port *port;
80 	int ret;
81 
82 	ret = nvkm_i2c_port_create(parent, engine, oclass, index,
83 				   &nvkm_i2c_bit_algo, &nv4e_i2c_func, &port);
84 	*pobject = nv_object(port);
85 	if (ret)
86 		return ret;
87 
88 	port->addr = 0x600800 + info->drive;
89 	return 0;
90 }
91 
92 static struct nvkm_oclass
93 nv4e_i2c_sclass[] = {
94 	{ .handle = NV_I2C_TYPE_DCBI2C(DCB_I2C_NV4E_BIT),
95 	  .ofuncs = &(struct nvkm_ofuncs) {
96 		  .ctor = nv4e_i2c_port_ctor,
97 		  .dtor = _nvkm_i2c_port_dtor,
98 		  .init = _nvkm_i2c_port_init,
99 		  .fini = _nvkm_i2c_port_fini,
100 	  },
101 	},
102 	{}
103 };
104 
105 struct nvkm_oclass *
106 nv4e_i2c_oclass = &(struct nvkm_i2c_impl) {
107 	.base.handle = NV_SUBDEV(I2C, 0x4e),
108 	.base.ofuncs = &(struct nvkm_ofuncs) {
109 		.ctor = _nvkm_i2c_ctor,
110 		.dtor = _nvkm_i2c_dtor,
111 		.init = _nvkm_i2c_init,
112 		.fini = _nvkm_i2c_fini,
113 	},
114 	.sclass = nv4e_i2c_sclass,
115 	.pad_x = &nv04_i2c_pad_oclass,
116 }.base;
117