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 nv4e_i2c_bus(p) container_of((p), struct nv4e_i2c_bus, base)
25*2aa5eac5SBen Skeggs #include "bus.h"
26*2aa5eac5SBen Skeggs
27*2aa5eac5SBen Skeggs struct nv4e_i2c_bus {
28*2aa5eac5SBen Skeggs struct nvkm_i2c_bus base;
29*2aa5eac5SBen Skeggs u32 addr;
30*2aa5eac5SBen Skeggs };
31*2aa5eac5SBen Skeggs
32*2aa5eac5SBen Skeggs static void
nv4e_i2c_bus_drive_scl(struct nvkm_i2c_bus * base,int state)33*2aa5eac5SBen Skeggs nv4e_i2c_bus_drive_scl(struct nvkm_i2c_bus *base, int state)
34*2aa5eac5SBen Skeggs {
35*2aa5eac5SBen Skeggs struct nv4e_i2c_bus *bus = nv4e_i2c_bus(base);
36*2aa5eac5SBen Skeggs struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
37*2aa5eac5SBen Skeggs nvkm_mask(device, bus->addr, 0x2f, state ? 0x21 : 0x01);
38*2aa5eac5SBen Skeggs }
39*2aa5eac5SBen Skeggs
40*2aa5eac5SBen Skeggs static void
nv4e_i2c_bus_drive_sda(struct nvkm_i2c_bus * base,int state)41*2aa5eac5SBen Skeggs nv4e_i2c_bus_drive_sda(struct nvkm_i2c_bus *base, int state)
42*2aa5eac5SBen Skeggs {
43*2aa5eac5SBen Skeggs struct nv4e_i2c_bus *bus = nv4e_i2c_bus(base);
44*2aa5eac5SBen Skeggs struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
45*2aa5eac5SBen Skeggs nvkm_mask(device, bus->addr, 0x1f, state ? 0x11 : 0x01);
46*2aa5eac5SBen Skeggs }
47*2aa5eac5SBen Skeggs
48*2aa5eac5SBen Skeggs static int
nv4e_i2c_bus_sense_scl(struct nvkm_i2c_bus * base)49*2aa5eac5SBen Skeggs nv4e_i2c_bus_sense_scl(struct nvkm_i2c_bus *base)
50*2aa5eac5SBen Skeggs {
51*2aa5eac5SBen Skeggs struct nv4e_i2c_bus *bus = nv4e_i2c_bus(base);
52*2aa5eac5SBen Skeggs struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
53*2aa5eac5SBen Skeggs return !!(nvkm_rd32(device, bus->addr) & 0x00040000);
54*2aa5eac5SBen Skeggs }
55*2aa5eac5SBen Skeggs
56*2aa5eac5SBen Skeggs static int
nv4e_i2c_bus_sense_sda(struct nvkm_i2c_bus * base)57*2aa5eac5SBen Skeggs nv4e_i2c_bus_sense_sda(struct nvkm_i2c_bus *base)
58*2aa5eac5SBen Skeggs {
59*2aa5eac5SBen Skeggs struct nv4e_i2c_bus *bus = nv4e_i2c_bus(base);
60*2aa5eac5SBen Skeggs struct nvkm_device *device = bus->base.pad->i2c->subdev.device;
61*2aa5eac5SBen Skeggs return !!(nvkm_rd32(device, bus->addr) & 0x00080000);
62*2aa5eac5SBen Skeggs }
63*2aa5eac5SBen Skeggs
64*2aa5eac5SBen Skeggs static const struct nvkm_i2c_bus_func
65*2aa5eac5SBen Skeggs nv4e_i2c_bus_func = {
66*2aa5eac5SBen Skeggs .drive_scl = nv4e_i2c_bus_drive_scl,
67*2aa5eac5SBen Skeggs .drive_sda = nv4e_i2c_bus_drive_sda,
68*2aa5eac5SBen Skeggs .sense_scl = nv4e_i2c_bus_sense_scl,
69*2aa5eac5SBen Skeggs .sense_sda = nv4e_i2c_bus_sense_sda,
70*2aa5eac5SBen Skeggs .xfer = nvkm_i2c_bit_xfer,
71*2aa5eac5SBen Skeggs };
72*2aa5eac5SBen Skeggs
73*2aa5eac5SBen Skeggs int
nv4e_i2c_bus_new(struct nvkm_i2c_pad * pad,int id,u8 drive,struct nvkm_i2c_bus ** pbus)74*2aa5eac5SBen Skeggs nv4e_i2c_bus_new(struct nvkm_i2c_pad *pad, int id, u8 drive,
75*2aa5eac5SBen Skeggs struct nvkm_i2c_bus **pbus)
76*2aa5eac5SBen Skeggs {
77*2aa5eac5SBen Skeggs struct nv4e_i2c_bus *bus;
78*2aa5eac5SBen Skeggs
79*2aa5eac5SBen Skeggs if (!(bus = kzalloc(sizeof(*bus), GFP_KERNEL)))
80*2aa5eac5SBen Skeggs return -ENOMEM;
81*2aa5eac5SBen Skeggs *pbus = &bus->base;
82*2aa5eac5SBen Skeggs
83*2aa5eac5SBen Skeggs nvkm_i2c_bus_ctor(&nv4e_i2c_bus_func, pad, id, &bus->base);
84*2aa5eac5SBen Skeggs bus->addr = 0x600800 + drive;
85*2aa5eac5SBen Skeggs return 0;
86*2aa5eac5SBen Skeggs }
87