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 	struct nvkm_subdev subdev;
73 	struct nvkm_event event;
74 
75 	struct list_head pad;
76 	struct list_head bus;
77 	struct list_head aux;
78 };
79 
80 struct nvkm_i2c_bus *nvkm_i2c_bus_find(struct nvkm_i2c *, int);
81 struct nvkm_i2c_aux *nvkm_i2c_aux_find(struct nvkm_i2c *, int);
82 
83 static inline struct nvkm_i2c *
84 nvkm_i2c(void *obj)
85 {
86 	return (void *)nvkm_subdev(obj, NVDEV_SUBDEV_I2C);
87 }
88 
89 extern struct nvkm_oclass *nv04_i2c_oclass;
90 extern struct nvkm_oclass *nv4e_i2c_oclass;
91 extern struct nvkm_oclass *nv50_i2c_oclass;
92 extern struct nvkm_oclass *g94_i2c_oclass;
93 extern struct nvkm_oclass *gf110_i2c_oclass;
94 extern struct nvkm_oclass *gf117_i2c_oclass;
95 extern struct nvkm_oclass *gk104_i2c_oclass;
96 extern struct nvkm_oclass *gm204_i2c_oclass;
97 
98 static inline int
99 nvkm_rdi2cr(struct i2c_adapter *adap, u8 addr, u8 reg)
100 {
101 	u8 val;
102 	struct i2c_msg msgs[] = {
103 		{ .addr = addr, .flags = 0, .len = 1, .buf = &reg },
104 		{ .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = &val },
105 	};
106 
107 	int ret = i2c_transfer(adap, msgs, ARRAY_SIZE(msgs));
108 	if (ret != 2)
109 		return -EIO;
110 
111 	return val;
112 }
113 
114 static inline int
115 nvkm_wri2cr(struct i2c_adapter *adap, u8 addr, u8 reg, u8 val)
116 {
117 	u8 buf[2] = { reg, val };
118 	struct i2c_msg msgs[] = {
119 		{ .addr = addr, .flags = 0, .len = 2, .buf = buf },
120 	};
121 
122 	int ret = i2c_transfer(adap, msgs, ARRAY_SIZE(msgs));
123 	if (ret != 1)
124 		return -EIO;
125 
126 	return 0;
127 }
128 
129 static inline bool
130 nvkm_probe_i2c(struct i2c_adapter *adap, u8 addr)
131 {
132 	return nvkm_rdi2cr(adap, addr, 0) >= 0;
133 }
134 
135 static inline int
136 nvkm_rdaux(struct nvkm_i2c_aux *aux, u32 addr, u8 *data, u8 size)
137 {
138 	int ret = nvkm_i2c_aux_acquire(aux);
139 	if (ret == 0) {
140 		ret = nvkm_i2c_aux_xfer(aux, true, 9, addr, data, size);
141 		nvkm_i2c_aux_release(aux);
142 	}
143 	return ret;
144 }
145 
146 static inline int
147 nvkm_wraux(struct nvkm_i2c_aux *aux, u32 addr, u8 *data, u8 size)
148 {
149 	int ret = nvkm_i2c_aux_acquire(aux);
150 	if (ret == 0) {
151 		ret = nvkm_i2c_aux_xfer(aux, true, 8, addr, data, size);
152 		nvkm_i2c_aux_release(aux);
153 	}
154 	return ret;
155 }
156 #endif
157