1 /*
2  * Copyright 2015 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 busions 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 <bskeggs@redhat.com>
23  */
24 #define gf119_i2c_bus(p) container_of((p), struct gf119_i2c_bus, base)
25 #include "bus.h"
26 
27 struct gf119_i2c_bus {
28 	struct nvkm_i2c_bus base;
29 	u32 addr;
30 };
31 
32 static void
33 gf119_i2c_bus_drive_scl(struct nvkm_i2c_bus *base, int state)
34 {
35 	struct gf119_i2c_bus *bus = gf119_i2c_bus(base);
36 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
37 	nvkm_mask(device, bus->addr, 0x00000001, state ? 0x00000001 : 0);
38 }
39 
40 static void
41 gf119_i2c_bus_drive_sda(struct nvkm_i2c_bus *base, int state)
42 {
43 	struct gf119_i2c_bus *bus = gf119_i2c_bus(base);
44 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
45 	nvkm_mask(device, bus->addr, 0x00000002, state ? 0x00000002 : 0);
46 }
47 
48 static int
49 gf119_i2c_bus_sense_scl(struct nvkm_i2c_bus *base)
50 {
51 	struct gf119_i2c_bus *bus = gf119_i2c_bus(base);
52 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
53 	return !!(nvkm_rd32(device, bus->addr) & 0x00000010);
54 }
55 
56 static int
57 gf119_i2c_bus_sense_sda(struct nvkm_i2c_bus *base)
58 {
59 	struct gf119_i2c_bus *bus = gf119_i2c_bus(base);
60 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
61 	return !!(nvkm_rd32(device, bus->addr) & 0x00000020);
62 }
63 
64 static void
65 gf119_i2c_bus_init(struct nvkm_i2c_bus *base)
66 {
67 	struct gf119_i2c_bus *bus = gf119_i2c_bus(base);
68 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
69 	nvkm_wr32(device, bus->addr, 0x00000007);
70 }
71 
72 static const struct nvkm_i2c_bus_func
73 gf119_i2c_bus_func = {
74 	.init = gf119_i2c_bus_init,
75 	.drive_scl = gf119_i2c_bus_drive_scl,
76 	.drive_sda = gf119_i2c_bus_drive_sda,
77 	.sense_scl = gf119_i2c_bus_sense_scl,
78 	.sense_sda = gf119_i2c_bus_sense_sda,
79 	.xfer = nvkm_i2c_bit_xfer,
80 };
81 
82 int
83 gf119_i2c_bus_new(struct nvkm_i2c_pad *pad, int id, u8 drive,
84 		 struct nvkm_i2c_bus **pbus)
85 {
86 	struct gf119_i2c_bus *bus;
87 
88 	if (!(bus = kzalloc(sizeof(*bus), GFP_KERNEL)))
89 		return -ENOMEM;
90 	*pbus = &bus->base;
91 
92 	nvkm_i2c_bus_ctor(&gf119_i2c_bus_func, pad, id, &bus->base);
93 	bus->addr = 0x00d014 + (drive * 0x20);
94 	return 0;
95 }
96