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