xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvkm/subdev/i2c/busnv50.c (revision e5451c8f8330e03ad3cfa16048b4daf961af434f)
1*2aa5eac5SBen Skeggs /*
2*2aa5eac5SBen Skeggs  * Copyright 2015 Red Hat Inc.
3*2aa5eac5SBen Skeggs  *
4*2aa5eac5SBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
5*2aa5eac5SBen Skeggs  * copy of this software and associated documentation files (the "Software"),
6*2aa5eac5SBen Skeggs  * to deal in the Software without restriction, including without limitation
7*2aa5eac5SBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2aa5eac5SBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
9*2aa5eac5SBen Skeggs  * Software is furnished to do so, subject to the following conditions:
10*2aa5eac5SBen Skeggs  *
11*2aa5eac5SBen Skeggs  * The above copyright notice and this permission notice shall be included in
12*2aa5eac5SBen Skeggs  * all copies or substantial busions of the Software.
13*2aa5eac5SBen Skeggs  *
14*2aa5eac5SBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*2aa5eac5SBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*2aa5eac5SBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*2aa5eac5SBen Skeggs  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*2aa5eac5SBen Skeggs  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*2aa5eac5SBen Skeggs  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*2aa5eac5SBen Skeggs  * OTHER DEALINGS IN THE SOFTWARE.
21*2aa5eac5SBen Skeggs  *
22*2aa5eac5SBen Skeggs  * Authors: Ben Skeggs <bskeggs@redhat.com>
23*2aa5eac5SBen Skeggs  */
24*2aa5eac5SBen Skeggs #define nv50_i2c_bus(p) container_of((p), struct nv50_i2c_bus, base)
25*2aa5eac5SBen Skeggs #include "bus.h"
26*2aa5eac5SBen Skeggs 
27*2aa5eac5SBen Skeggs #include <subdev/vga.h>
28*2aa5eac5SBen Skeggs 
29*2aa5eac5SBen Skeggs struct nv50_i2c_bus {
30*2aa5eac5SBen Skeggs 	struct nvkm_i2c_bus base;
31*2aa5eac5SBen Skeggs 	u32 addr;
32*2aa5eac5SBen Skeggs 	u32 data;
33*2aa5eac5SBen Skeggs };
34*2aa5eac5SBen Skeggs 
35*2aa5eac5SBen Skeggs static void
nv50_i2c_bus_drive_scl(struct nvkm_i2c_bus * base,int state)36*2aa5eac5SBen Skeggs nv50_i2c_bus_drive_scl(struct nvkm_i2c_bus *base, int state)
37*2aa5eac5SBen Skeggs {
38*2aa5eac5SBen Skeggs 	struct nv50_i2c_bus *bus = nv50_i2c_bus(base);
39*2aa5eac5SBen Skeggs 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
40*2aa5eac5SBen Skeggs 	if (state) bus->data |= 0x01;
41*2aa5eac5SBen Skeggs 	else	   bus->data &= 0xfe;
42*2aa5eac5SBen Skeggs 	nvkm_wr32(device, bus->addr, bus->data);
43*2aa5eac5SBen Skeggs }
44*2aa5eac5SBen Skeggs 
45*2aa5eac5SBen Skeggs static void
nv50_i2c_bus_drive_sda(struct nvkm_i2c_bus * base,int state)46*2aa5eac5SBen Skeggs nv50_i2c_bus_drive_sda(struct nvkm_i2c_bus *base, int state)
47*2aa5eac5SBen Skeggs {
48*2aa5eac5SBen Skeggs 	struct nv50_i2c_bus *bus = nv50_i2c_bus(base);
49*2aa5eac5SBen Skeggs 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
50*2aa5eac5SBen Skeggs 	if (state) bus->data |= 0x02;
51*2aa5eac5SBen Skeggs 	else	   bus->data &= 0xfd;
52*2aa5eac5SBen Skeggs 	nvkm_wr32(device, bus->addr, bus->data);
53*2aa5eac5SBen Skeggs }
54*2aa5eac5SBen Skeggs 
55*2aa5eac5SBen Skeggs static int
nv50_i2c_bus_sense_scl(struct nvkm_i2c_bus * base)56*2aa5eac5SBen Skeggs nv50_i2c_bus_sense_scl(struct nvkm_i2c_bus *base)
57*2aa5eac5SBen Skeggs {
58*2aa5eac5SBen Skeggs 	struct nv50_i2c_bus *bus = nv50_i2c_bus(base);
59*2aa5eac5SBen Skeggs 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
60*2aa5eac5SBen Skeggs 	return !!(nvkm_rd32(device, bus->addr) & 0x00000001);
61*2aa5eac5SBen Skeggs }
62*2aa5eac5SBen Skeggs 
63*2aa5eac5SBen Skeggs static int
nv50_i2c_bus_sense_sda(struct nvkm_i2c_bus * base)64*2aa5eac5SBen Skeggs nv50_i2c_bus_sense_sda(struct nvkm_i2c_bus *base)
65*2aa5eac5SBen Skeggs {
66*2aa5eac5SBen Skeggs 	struct nv50_i2c_bus *bus = nv50_i2c_bus(base);
67*2aa5eac5SBen Skeggs 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
68*2aa5eac5SBen Skeggs 	return !!(nvkm_rd32(device, bus->addr) & 0x00000002);
69*2aa5eac5SBen Skeggs }
70*2aa5eac5SBen Skeggs 
71*2aa5eac5SBen Skeggs static void
nv50_i2c_bus_init(struct nvkm_i2c_bus * base)72*2aa5eac5SBen Skeggs nv50_i2c_bus_init(struct nvkm_i2c_bus *base)
73*2aa5eac5SBen Skeggs {
74*2aa5eac5SBen Skeggs 	struct nv50_i2c_bus *bus = nv50_i2c_bus(base);
75*2aa5eac5SBen Skeggs 	struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
76*2aa5eac5SBen Skeggs 	nvkm_wr32(device, bus->addr, (bus->data = 0x00000007));
77*2aa5eac5SBen Skeggs }
78*2aa5eac5SBen Skeggs 
79*2aa5eac5SBen Skeggs static const struct nvkm_i2c_bus_func
80*2aa5eac5SBen Skeggs nv50_i2c_bus_func = {
81*2aa5eac5SBen Skeggs 	.init = nv50_i2c_bus_init,
82*2aa5eac5SBen Skeggs 	.drive_scl = nv50_i2c_bus_drive_scl,
83*2aa5eac5SBen Skeggs 	.drive_sda = nv50_i2c_bus_drive_sda,
84*2aa5eac5SBen Skeggs 	.sense_scl = nv50_i2c_bus_sense_scl,
85*2aa5eac5SBen Skeggs 	.sense_sda = nv50_i2c_bus_sense_sda,
86*2aa5eac5SBen Skeggs 	.xfer = nvkm_i2c_bit_xfer,
87*2aa5eac5SBen Skeggs };
88*2aa5eac5SBen Skeggs 
89*2aa5eac5SBen Skeggs int
nv50_i2c_bus_new(struct nvkm_i2c_pad * pad,int id,u8 drive,struct nvkm_i2c_bus ** pbus)90*2aa5eac5SBen Skeggs nv50_i2c_bus_new(struct nvkm_i2c_pad *pad, int id, u8 drive,
91*2aa5eac5SBen Skeggs 		 struct nvkm_i2c_bus **pbus)
92*2aa5eac5SBen Skeggs {
93*2aa5eac5SBen Skeggs 	static const u32 addr[] = {
94*2aa5eac5SBen Skeggs 		0x00e138, 0x00e150, 0x00e168, 0x00e180,
95*2aa5eac5SBen Skeggs 		0x00e254, 0x00e274, 0x00e764, 0x00e780,
96*2aa5eac5SBen Skeggs 		0x00e79c, 0x00e7b8
97*2aa5eac5SBen Skeggs 	};
98*2aa5eac5SBen Skeggs 	struct nv50_i2c_bus *bus;
99*2aa5eac5SBen Skeggs 
100*2aa5eac5SBen Skeggs 	if (drive >= ARRAY_SIZE(addr)) {
101*2aa5eac5SBen Skeggs 		nvkm_warn(&pad->i2c->subdev, "bus %d unknown\n", drive);
102*2aa5eac5SBen Skeggs 		return -ENODEV;
103*2aa5eac5SBen Skeggs 	}
104*2aa5eac5SBen Skeggs 
105*2aa5eac5SBen Skeggs 	if (!(bus = kzalloc(sizeof(*bus), GFP_KERNEL)))
106*2aa5eac5SBen Skeggs 		return -ENOMEM;
107*2aa5eac5SBen Skeggs 	*pbus = &bus->base;
108*2aa5eac5SBen Skeggs 
109*2aa5eac5SBen Skeggs 	nvkm_i2c_bus_ctor(&nv50_i2c_bus_func, pad, id, &bus->base);
110*2aa5eac5SBen Skeggs 	bus->addr = addr[drive];
111*2aa5eac5SBen Skeggs 	bus->data = 0x00000007;
112*2aa5eac5SBen Skeggs 	return 0;
113*2aa5eac5SBen Skeggs }
114