xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/i2c/nouveau_nvkm_subdev_i2c_busnv50.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nvkm_subdev_i2c_busnv50.c,v 1.3 2021/12/18 23:45:40 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2015 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 busions 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 <bskeggs@redhat.com>
25  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_i2c_busnv50.c,v 1.3 2021/12/18 23:45:40 riastradh Exp $");
28 
29 #define nv50_i2c_bus(p) container_of((p), struct nv50_i2c_bus, base)
30 #include "bus.h"
31 
32 #include <subdev/vga.h>
33 
34 struct nv50_i2c_bus {
35 	struct nvkm_i2c_bus base;
36 	u32 addr;
37 	u32 data;
38 };
39 
40 static void
nv50_i2c_bus_drive_scl(struct nvkm_i2c_bus * base,int state)41 nv50_i2c_bus_drive_scl(struct nvkm_i2c_bus *base, int state)
42 {
43 	struct nv50_i2c_bus *bus = nv50_i2c_bus(base);
44 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
45 	if (state) bus->data |= 0x01;
46 	else	   bus->data &= 0xfe;
47 	nvkm_wr32(device, bus->addr, bus->data);
48 }
49 
50 static void
nv50_i2c_bus_drive_sda(struct nvkm_i2c_bus * base,int state)51 nv50_i2c_bus_drive_sda(struct nvkm_i2c_bus *base, int state)
52 {
53 	struct nv50_i2c_bus *bus = nv50_i2c_bus(base);
54 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
55 	if (state) bus->data |= 0x02;
56 	else	   bus->data &= 0xfd;
57 	nvkm_wr32(device, bus->addr, bus->data);
58 }
59 
60 static int
nv50_i2c_bus_sense_scl(struct nvkm_i2c_bus * base)61 nv50_i2c_bus_sense_scl(struct nvkm_i2c_bus *base)
62 {
63 	struct nv50_i2c_bus *bus = nv50_i2c_bus(base);
64 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
65 	return !!(nvkm_rd32(device, bus->addr) & 0x00000001);
66 }
67 
68 static int
nv50_i2c_bus_sense_sda(struct nvkm_i2c_bus * base)69 nv50_i2c_bus_sense_sda(struct nvkm_i2c_bus *base)
70 {
71 	struct nv50_i2c_bus *bus = nv50_i2c_bus(base);
72 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
73 	return !!(nvkm_rd32(device, bus->addr) & 0x00000002);
74 }
75 
76 static void
nv50_i2c_bus_init(struct nvkm_i2c_bus * base)77 nv50_i2c_bus_init(struct nvkm_i2c_bus *base)
78 {
79 	struct nv50_i2c_bus *bus = nv50_i2c_bus(base);
80 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
81 	nvkm_wr32(device, bus->addr, (bus->data = 0x00000007));
82 }
83 
84 static const struct nvkm_i2c_bus_func
85 nv50_i2c_bus_func = {
86 	.init = nv50_i2c_bus_init,
87 	.drive_scl = nv50_i2c_bus_drive_scl,
88 	.drive_sda = nv50_i2c_bus_drive_sda,
89 	.sense_scl = nv50_i2c_bus_sense_scl,
90 	.sense_sda = nv50_i2c_bus_sense_sda,
91 	.xfer = nvkm_i2c_bit_xfer,
92 };
93 
94 int
nv50_i2c_bus_new(struct nvkm_i2c_pad * pad,int id,u8 drive,struct nvkm_i2c_bus ** pbus)95 nv50_i2c_bus_new(struct nvkm_i2c_pad *pad, int id, u8 drive,
96 		 struct nvkm_i2c_bus **pbus)
97 {
98 	static const u32 addr[] = {
99 		0x00e138, 0x00e150, 0x00e168, 0x00e180,
100 		0x00e254, 0x00e274, 0x00e764, 0x00e780,
101 		0x00e79c, 0x00e7b8
102 	};
103 	struct nv50_i2c_bus *bus;
104 
105 	if (drive >= ARRAY_SIZE(addr)) {
106 		nvkm_warn(&pad->i2c->subdev, "bus %d unknown\n", drive);
107 		return -ENODEV;
108 	}
109 
110 	if (!(bus = kzalloc(sizeof(*bus), GFP_KERNEL)))
111 		return -ENOMEM;
112 	*pbus = &bus->base;
113 
114 	nvkm_i2c_bus_ctor(&nv50_i2c_bus_func, pad, id, &bus->base);
115 	bus->addr = addr[drive];
116 	bus->data = 0x00000007;
117 	return 0;
118 }
119