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