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 gm204_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 nvkm_wri2cr(struct i2c_adapter *adap, u8 addr, u8 reg, u8 val)
112 {
113 	u8 buf[2] = { reg, val };
114 	struct i2c_msg msgs[] = {
115 		{ .addr = addr, .flags = 0, .len = 2, .buf = buf },
116 	};
117 
118 	int ret = i2c_transfer(adap, msgs, ARRAY_SIZE(msgs));
119 	if (ret != 1)
120 		return -EIO;
121 
122 	return 0;
123 }
124 
125 static inline bool
126 nvkm_probe_i2c(struct i2c_adapter *adap, u8 addr)
127 {
128 	return nvkm_rdi2cr(adap, addr, 0) >= 0;
129 }
130 
131 static inline int
132 nvkm_rdaux(struct nvkm_i2c_aux *aux, u32 addr, u8 *data, u8 size)
133 {
134 	int ret = nvkm_i2c_aux_acquire(aux);
135 	if (ret == 0) {
136 		ret = nvkm_i2c_aux_xfer(aux, true, 9, addr, data, size);
137 		nvkm_i2c_aux_release(aux);
138 	}
139 	return ret;
140 }
141 
142 static inline int
143 nvkm_wraux(struct nvkm_i2c_aux *aux, u32 addr, u8 *data, u8 size)
144 {
145 	int ret = nvkm_i2c_aux_acquire(aux);
146 	if (ret == 0) {
147 		ret = nvkm_i2c_aux_xfer(aux, true, 8, addr, data, size);
148 		nvkm_i2c_aux_release(aux);
149 	}
150 	return ret;
151 }
152 #endif
153