1 /* SPDX-License-Identifier: MIT */
2 #ifndef __NVKM_I2C_H__
3 #define __NVKM_I2C_H__
4 #include <core/subdev.h>
5 #include <core/event.h>
6 
7 #include <subdev/bios.h>
8 #include <subdev/bios/i2c.h>
9 
10 struct nvkm_i2c_bus_probe {
11 	struct i2c_board_info dev;
12 	u8 udelay; /* set to 0 to use the standard delay */
13 };
14 
15 struct nvkm_i2c_bus {
16 	const struct nvkm_i2c_bus_func *func;
17 	struct nvkm_i2c_pad *pad;
18 #define NVKM_I2C_BUS_CCB(n) /* 'n' is ccb index */                           (n)
19 #define NVKM_I2C_BUS_EXT(n) /* 'n' is dcb external encoder type */  ((n) + 0x10)
20 #define NVKM_I2C_BUS_PRI /* ccb primary comm. port */                        -1
21 #define NVKM_I2C_BUS_SEC /* ccb secondary comm. port */                      -2
22 	int id;
23 
24 	struct mutex mutex;
25 	struct list_head head;
26 	struct i2c_adapter i2c;
27 	u8 enabled;
28 };
29 
30 int nvkm_i2c_bus_acquire(struct nvkm_i2c_bus *);
31 void nvkm_i2c_bus_release(struct nvkm_i2c_bus *);
32 int nvkm_i2c_bus_probe(struct nvkm_i2c_bus *, const char *,
33 		       struct nvkm_i2c_bus_probe *,
34 		       bool (*)(struct nvkm_i2c_bus *,
35 			        struct i2c_board_info *, void *), void *);
36 
37 struct nvkm_i2c_aux {
38 	const struct nvkm_i2c_aux_func *func;
39 	struct nvkm_i2c_pad *pad;
40 #define NVKM_I2C_AUX_CCB(n) /* 'n' is ccb index */                           (n)
41 #define NVKM_I2C_AUX_EXT(n) /* 'n' is dcb external encoder type */  ((n) + 0x10)
42 	int id;
43 
44 	struct mutex mutex;
45 	struct list_head head;
46 	struct i2c_adapter i2c;
47 	u8 enabled;
48 
49 	u32 intr;
50 };
51 
52 void nvkm_i2c_aux_monitor(struct nvkm_i2c_aux *, bool monitor);
53 int nvkm_i2c_aux_acquire(struct nvkm_i2c_aux *);
54 void nvkm_i2c_aux_release(struct nvkm_i2c_aux *);
55 int nvkm_i2c_aux_xfer(struct nvkm_i2c_aux *, bool retry, u8 type,
56 		      u32 addr, u8 *data, u8 *size);
57 int nvkm_i2c_aux_lnk_ctl(struct nvkm_i2c_aux *, int link_nr, int link_bw,
58 			 bool enhanced_framing);
59 
60 struct nvkm_i2c {
61 	const struct nvkm_i2c_func *func;
62 	struct nvkm_subdev subdev;
63 
64 	struct list_head pad;
65 	struct list_head bus;
66 	struct list_head aux;
67 
68 #define NVKM_I2C_PLUG   BIT(0)
69 #define NVKM_I2C_UNPLUG BIT(1)
70 #define NVKM_I2C_IRQ    BIT(2)
71 #define NVKM_I2C_DONE   BIT(3)
72 #define NVKM_I2C_ANY   (NVKM_I2C_PLUG | NVKM_I2C_UNPLUG | NVKM_I2C_IRQ | NVKM_I2C_DONE)
73 	struct nvkm_event event;
74 };
75 
76 struct nvkm_i2c_bus *nvkm_i2c_bus_find(struct nvkm_i2c *, int);
77 struct nvkm_i2c_aux *nvkm_i2c_aux_find(struct nvkm_i2c *, int);
78 
79 int nv04_i2c_new(struct nvkm_device *, enum nvkm_subdev_type, int inst, struct nvkm_i2c **);
80 int nv4e_i2c_new(struct nvkm_device *, enum nvkm_subdev_type, int inst, struct nvkm_i2c **);
81 int nv50_i2c_new(struct nvkm_device *, enum nvkm_subdev_type, int inst, struct nvkm_i2c **);
82 int g94_i2c_new(struct nvkm_device *, enum nvkm_subdev_type, int inst, struct nvkm_i2c **);
83 int gf117_i2c_new(struct nvkm_device *, enum nvkm_subdev_type, int inst, struct nvkm_i2c **);
84 int gf119_i2c_new(struct nvkm_device *, enum nvkm_subdev_type, int inst, struct nvkm_i2c **);
85 int gk104_i2c_new(struct nvkm_device *, enum nvkm_subdev_type, int inst, struct nvkm_i2c **);
86 int gk110_i2c_new(struct nvkm_device *, enum nvkm_subdev_type, int inst, struct nvkm_i2c **);
87 int gm200_i2c_new(struct nvkm_device *, enum nvkm_subdev_type, int inst, struct nvkm_i2c **);
88 
89 static inline int
nvkm_rdi2cr(struct i2c_adapter * adap,u8 addr,u8 reg)90 nvkm_rdi2cr(struct i2c_adapter *adap, u8 addr, u8 reg)
91 {
92 	u8 val;
93 	struct i2c_msg msgs[] = {
94 		{ .addr = addr, .flags = 0, .len = 1, .buf = &reg },
95 		{ .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = &val },
96 	};
97 
98 	int ret = i2c_transfer(adap, msgs, ARRAY_SIZE(msgs));
99 	if (ret != 2)
100 		return -EIO;
101 
102 	return val;
103 }
104 
105 static inline int
nv_rd16i2cr(struct i2c_adapter * adap,u8 addr,u8 reg)106 nv_rd16i2cr(struct i2c_adapter *adap, u8 addr, u8 reg)
107 {
108 	u8 val[2];
109 	struct i2c_msg msgs[] = {
110 		{ .addr = addr, .flags = 0, .len = 1, .buf = &reg },
111 		{ .addr = addr, .flags = I2C_M_RD, .len = 2, .buf = val },
112 	};
113 
114 	int ret = i2c_transfer(adap, msgs, ARRAY_SIZE(msgs));
115 	if (ret != 2)
116 		return -EIO;
117 
118 	return val[0] << 8 | val[1];
119 }
120 
121 static inline int
nvkm_wri2cr(struct i2c_adapter * adap,u8 addr,u8 reg,u8 val)122 nvkm_wri2cr(struct i2c_adapter *adap, u8 addr, u8 reg, u8 val)
123 {
124 	u8 buf[2] = { reg, val };
125 	struct i2c_msg msgs[] = {
126 		{ .addr = addr, .flags = 0, .len = 2, .buf = buf },
127 	};
128 
129 	int ret = i2c_transfer(adap, msgs, ARRAY_SIZE(msgs));
130 	if (ret != 1)
131 		return -EIO;
132 
133 	return 0;
134 }
135 
136 static inline int
nv_wr16i2cr(struct i2c_adapter * adap,u8 addr,u8 reg,u16 val)137 nv_wr16i2cr(struct i2c_adapter *adap, u8 addr, u8 reg, u16 val)
138 {
139 	u8 buf[3] = { reg, val >> 8, val & 0xff};
140 	struct i2c_msg msgs[] = {
141 		{ .addr = addr, .flags = 0, .len = 3, .buf = buf },
142 	};
143 
144 	int ret = i2c_transfer(adap, msgs, ARRAY_SIZE(msgs));
145 	if (ret != 1)
146 		return -EIO;
147 
148 	return 0;
149 }
150 
151 static inline bool
nvkm_probe_i2c(struct i2c_adapter * adap,u8 addr)152 nvkm_probe_i2c(struct i2c_adapter *adap, u8 addr)
153 {
154 	return nvkm_rdi2cr(adap, addr, 0) >= 0;
155 }
156 
157 static inline int
nvkm_rdaux(struct nvkm_i2c_aux * aux,u32 addr,u8 * data,u8 size)158 nvkm_rdaux(struct nvkm_i2c_aux *aux, u32 addr, u8 *data, u8 size)
159 {
160 	const u8 xfer = size;
161 	int ret = nvkm_i2c_aux_acquire(aux);
162 	if (ret == 0) {
163 		ret = nvkm_i2c_aux_xfer(aux, true, 9, addr, data, &size);
164 		WARN_ON(!ret && size != xfer);
165 		nvkm_i2c_aux_release(aux);
166 	}
167 	return ret;
168 }
169 
170 static inline int
nvkm_wraux(struct nvkm_i2c_aux * aux,u32 addr,u8 * data,u8 size)171 nvkm_wraux(struct nvkm_i2c_aux *aux, u32 addr, u8 *data, u8 size)
172 {
173 	int ret = nvkm_i2c_aux_acquire(aux);
174 	if (ret == 0) {
175 		ret = nvkm_i2c_aux_xfer(aux, true, 8, addr, data, &size);
176 		nvkm_i2c_aux_release(aux);
177 	}
178 	return ret;
179 }
180 #endif
181