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 #define NV_I2C_PORT(n)    (0x00 + (n))
10 #define NV_I2C_AUX(n)     (0x10 + (n))
11 #define NV_I2C_EXT(n)     (0x20 + (n))
12 #define NV_I2C_DEFAULT(n) (0x80 + (n))
13 
14 #define NV_I2C_TYPE_DCBI2C(n) (0x0000 | (n))
15 #define NV_I2C_TYPE_EXTDDC(e) (0x0005 | (e) << 8)
16 #define NV_I2C_TYPE_EXTAUX(e) (0x0006 | (e) << 8)
17 
18 struct nvkm_i2c_ntfy_req {
19 #define NVKM_I2C_PLUG                                                      0x01
20 #define NVKM_I2C_UNPLUG                                                    0x02
21 #define NVKM_I2C_IRQ                                                       0x04
22 #define NVKM_I2C_DONE                                                      0x08
23 #define NVKM_I2C_ANY                                                       0x0f
24 	u8 mask;
25 	u8 port;
26 };
27 
28 struct nvkm_i2c_ntfy_rep {
29 	u8 mask;
30 };
31 
32 struct nvkm_i2c_port {
33 	struct nvkm_object base;
34 	struct i2c_adapter adapter;
35 	struct mutex mutex;
36 
37 	struct list_head head;
38 	u8  index;
39 	int aux;
40 
41 	const struct nvkm_i2c_func *func;
42 };
43 
44 struct nvkm_i2c_func {
45 	void (*drive_scl)(struct nvkm_i2c_port *, int);
46 	void (*drive_sda)(struct nvkm_i2c_port *, int);
47 	int  (*sense_scl)(struct nvkm_i2c_port *);
48 	int  (*sense_sda)(struct nvkm_i2c_port *);
49 
50 	int  (*aux)(struct nvkm_i2c_port *, bool, u8, u32, u8 *, u8);
51 	int  (*pattern)(struct nvkm_i2c_port *, int pattern);
52 	int  (*lnk_ctl)(struct nvkm_i2c_port *, int nr, int bw, bool enh);
53 	int  (*drv_ctl)(struct nvkm_i2c_port *, int lane, int sw, int pe);
54 };
55 
56 struct nvkm_i2c_board_info {
57 	struct i2c_board_info dev;
58 	u8 udelay; /* set to 0 to use the standard delay */
59 };
60 
61 struct nvkm_i2c {
62 	struct nvkm_subdev base;
63 	struct nvkm_event event;
64 
65 	struct nvkm_i2c_port *(*find)(struct nvkm_i2c *, u8 index);
66 	struct nvkm_i2c_port *(*find_type)(struct nvkm_i2c *, u16 type);
67 	int  (*acquire_pad)(struct nvkm_i2c_port *, unsigned long timeout);
68 	void (*release_pad)(struct nvkm_i2c_port *);
69 	int  (*acquire)(struct nvkm_i2c_port *, unsigned long timeout);
70 	void (*release)(struct nvkm_i2c_port *);
71 	int  (*identify)(struct nvkm_i2c *, int index,
72 			 const char *what, struct nvkm_i2c_board_info *,
73 			 bool (*match)(struct nvkm_i2c_port *,
74 				       struct i2c_board_info *, void *),
75 			 void *);
76 
77 	wait_queue_head_t wait;
78 	struct list_head ports;
79 };
80 
81 static inline struct nvkm_i2c *
82 nvkm_i2c(void *obj)
83 {
84 	return (void *)nvkm_subdev(obj, NVDEV_SUBDEV_I2C);
85 }
86 
87 extern struct nvkm_oclass *nv04_i2c_oclass;
88 extern struct nvkm_oclass *nv4e_i2c_oclass;
89 extern struct nvkm_oclass *nv50_i2c_oclass;
90 extern struct nvkm_oclass *g94_i2c_oclass;
91 extern struct nvkm_oclass *gf110_i2c_oclass;
92 extern struct nvkm_oclass *gf117_i2c_oclass;
93 extern struct nvkm_oclass *gk104_i2c_oclass;
94 extern struct nvkm_oclass *gm204_i2c_oclass;
95 
96 static inline int
97 nv_rdi2cr(struct nvkm_i2c_port *port, u8 addr, u8 reg)
98 {
99 	u8 val;
100 	struct i2c_msg msgs[] = {
101 		{ .addr = addr, .flags = 0, .len = 1, .buf = &reg },
102 		{ .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = &val },
103 	};
104 
105 	int ret = i2c_transfer(&port->adapter, msgs, 2);
106 	if (ret != 2)
107 		return -EIO;
108 
109 	return val;
110 }
111 
112 static inline int
113 nv_wri2cr(struct nvkm_i2c_port *port, 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(&port->adapter, msgs, 1);
121 	if (ret != 1)
122 		return -EIO;
123 
124 	return 0;
125 }
126 
127 static inline bool
128 nv_probe_i2c(struct nvkm_i2c_port *port, u8 addr)
129 {
130 	return nv_rdi2cr(port, addr, 0) >= 0;
131 }
132 
133 int nv_rdaux(struct nvkm_i2c_port *, u32 addr, u8 *data, u8 size);
134 int nv_wraux(struct nvkm_i2c_port *, u32 addr, u8 *data, u8 size);
135 #endif
136