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 u8 enabled;
41 };
42
43 int nvkm_i2c_bus_acquire(struct nvkm_i2c_bus *);
44 void nvkm_i2c_bus_release(struct nvkm_i2c_bus *);
45 int nvkm_i2c_bus_probe(struct nvkm_i2c_bus *, const char *,
46 struct nvkm_i2c_bus_probe *,
47 bool (*)(struct nvkm_i2c_bus *,
48 struct i2c_board_info *, void *), void *);
49
50 struct nvkm_i2c_aux {
51 const struct nvkm_i2c_aux_func *func;
52 struct nvkm_i2c_pad *pad;
53 #define NVKM_I2C_AUX_CCB(n) /* 'n' is ccb index */ (n)
54 #define NVKM_I2C_AUX_EXT(n) /* 'n' is dcb external encoder type */ ((n) + 0x100)
55 int id;
56
57 struct mutex mutex;
58 struct list_head head;
59 struct i2c_adapter i2c;
60 u8 enabled;
61
62 u32 intr;
63 };
64
65 void nvkm_i2c_aux_monitor(struct nvkm_i2c_aux *, bool monitor);
66 int nvkm_i2c_aux_acquire(struct nvkm_i2c_aux *);
67 void nvkm_i2c_aux_release(struct nvkm_i2c_aux *);
68 int nvkm_i2c_aux_xfer(struct nvkm_i2c_aux *, bool retry, u8 type,
69 u32 addr, u8 *data, u8 size);
70 int nvkm_i2c_aux_lnk_ctl(struct nvkm_i2c_aux *, int link_nr, int link_bw,
71 bool enhanced_framing);
72
73 struct nvkm_i2c {
74 const struct nvkm_i2c_func *func;
75 struct nvkm_subdev subdev;
76
77 struct list_head pad;
78 struct list_head bus;
79 struct list_head aux;
80
81 struct nvkm_event event;
82 };
83
84 struct nvkm_i2c_bus *nvkm_i2c_bus_find(struct nvkm_i2c *, int);
85 struct nvkm_i2c_aux *nvkm_i2c_aux_find(struct nvkm_i2c *, int);
86
87 int nv04_i2c_new(struct nvkm_device *, int, struct nvkm_i2c **);
88 int nv4e_i2c_new(struct nvkm_device *, int, struct nvkm_i2c **);
89 int nv50_i2c_new(struct nvkm_device *, int, struct nvkm_i2c **);
90 int g94_i2c_new(struct nvkm_device *, int, struct nvkm_i2c **);
91 int gf117_i2c_new(struct nvkm_device *, int, struct nvkm_i2c **);
92 int gf119_i2c_new(struct nvkm_device *, int, struct nvkm_i2c **);
93 int gk104_i2c_new(struct nvkm_device *, int, struct nvkm_i2c **);
94 int gm204_i2c_new(struct nvkm_device *, int, struct nvkm_i2c **);
95
96 static inline int
nvkm_rdi2cr(struct i2c_adapter * adap,u8 addr,u8 reg)97 nvkm_rdi2cr(struct i2c_adapter *adap, u8 addr, u8 reg)
98 {
99 u8 val;
100 struct i2c_msg msgs[] = {
101 { .addr = addr, .flags = 0, .len = 1, .buf = ® },
102 { .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = &val },
103 };
104
105 int ret = i2c_transfer(adap, msgs, ARRAY_SIZE(msgs));
106 if (ret != 2)
107 return -EIO;
108
109 return val;
110 }
111
112 static inline int
nvkm_wri2cr(struct i2c_adapter * adap,u8 addr,u8 reg,u8 val)113 nvkm_wri2cr(struct i2c_adapter *adap, u8 addr, u8 reg, u8 val)
114 {
115 u8 buf[2] = { reg, val };
116 struct i2c_msg msgs[] = {
117 { .addr = addr, .flags = 0, .len = 2, .buf = buf },
118 };
119
120 int ret = i2c_transfer(adap, msgs, ARRAY_SIZE(msgs));
121 if (ret != 1)
122 return -EIO;
123
124 return 0;
125 }
126
127 static inline bool
nvkm_probe_i2c(struct i2c_adapter * adap,u8 addr)128 nvkm_probe_i2c(struct i2c_adapter *adap, u8 addr)
129 {
130 return nvkm_rdi2cr(adap, addr, 0) >= 0;
131 }
132
133 static inline int
nvkm_rdaux(struct nvkm_i2c_aux * aux,u32 addr,u8 * data,u8 size)134 nvkm_rdaux(struct nvkm_i2c_aux *aux, u32 addr, u8 *data, u8 size)
135 {
136 int ret = nvkm_i2c_aux_acquire(aux);
137 if (ret == 0) {
138 ret = nvkm_i2c_aux_xfer(aux, true, 9, addr, data, size);
139 nvkm_i2c_aux_release(aux);
140 }
141 return ret;
142 }
143
144 static inline int
nvkm_wraux(struct nvkm_i2c_aux * aux,u32 addr,u8 * data,u8 size)145 nvkm_wraux(struct nvkm_i2c_aux *aux, u32 addr, u8 *data, u8 size)
146 {
147 int ret = nvkm_i2c_aux_acquire(aux);
148 if (ret == 0) {
149 ret = nvkm_i2c_aux_xfer(aux, true, 8, addr, data, size);
150 nvkm_i2c_aux_release(aux);
151 }
152 return ret;
153 }
154 #endif
155