1 /* $NetBSD: nouveau_nvkm_subdev_i2c_busnv04.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 (at) redhat.com> 25 */ 26 #include <sys/cdefs.h> 27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_i2c_busnv04.c,v 1.3 2021/12/18 23:45:40 riastradh Exp $"); 28 29 #define nv04_i2c_bus(p) container_of((p), struct nv04_i2c_bus, base) 30 #include "bus.h" 31 32 #include <subdev/vga.h> 33 34 struct nv04_i2c_bus { 35 struct nvkm_i2c_bus base; 36 u8 drive; 37 u8 sense; 38 }; 39 40 static void 41 nv04_i2c_bus_drive_scl(struct nvkm_i2c_bus *base, int state) 42 { 43 struct nv04_i2c_bus *bus = nv04_i2c_bus(base); 44 struct nvkm_device *device = bus->base.pad->i2c->subdev.device; 45 u8 val = nvkm_rdvgac(device, 0, bus->drive); 46 if (state) val |= 0x20; 47 else val &= 0xdf; 48 nvkm_wrvgac(device, 0, bus->drive, val | 0x01); 49 } 50 51 static void 52 nv04_i2c_bus_drive_sda(struct nvkm_i2c_bus *base, int state) 53 { 54 struct nv04_i2c_bus *bus = nv04_i2c_bus(base); 55 struct nvkm_device *device = bus->base.pad->i2c->subdev.device; 56 u8 val = nvkm_rdvgac(device, 0, bus->drive); 57 if (state) val |= 0x10; 58 else val &= 0xef; 59 nvkm_wrvgac(device, 0, bus->drive, val | 0x01); 60 } 61 62 static int 63 nv04_i2c_bus_sense_scl(struct nvkm_i2c_bus *base) 64 { 65 struct nv04_i2c_bus *bus = nv04_i2c_bus(base); 66 struct nvkm_device *device = bus->base.pad->i2c->subdev.device; 67 return !!(nvkm_rdvgac(device, 0, bus->sense) & 0x04); 68 } 69 70 static int 71 nv04_i2c_bus_sense_sda(struct nvkm_i2c_bus *base) 72 { 73 struct nv04_i2c_bus *bus = nv04_i2c_bus(base); 74 struct nvkm_device *device = bus->base.pad->i2c->subdev.device; 75 return !!(nvkm_rdvgac(device, 0, bus->sense) & 0x08); 76 } 77 78 static const struct nvkm_i2c_bus_func 79 nv04_i2c_bus_func = { 80 .drive_scl = nv04_i2c_bus_drive_scl, 81 .drive_sda = nv04_i2c_bus_drive_sda, 82 .sense_scl = nv04_i2c_bus_sense_scl, 83 .sense_sda = nv04_i2c_bus_sense_sda, 84 .xfer = nvkm_i2c_bit_xfer, 85 }; 86 87 int 88 nv04_i2c_bus_new(struct nvkm_i2c_pad *pad, int id, u8 drive, u8 sense, 89 struct nvkm_i2c_bus **pbus) 90 { 91 struct nv04_i2c_bus *bus; 92 93 if (!(bus = kzalloc(sizeof(*bus), GFP_KERNEL))) 94 return -ENOMEM; 95 *pbus = &bus->base; 96 97 nvkm_i2c_bus_ctor(&nv04_i2c_bus_func, pad, id, &bus->base); 98 bus->drive = drive; 99 bus->sense = sense; 100 return 0; 101 } 102