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