This source file includes following definitions.
- mga_i2c_read_gpio
- mga_i2c_set_gpio
- mga_i2c_set
- mga_gpio_setsda
- mga_gpio_setscl
- mga_gpio_getsda
- mga_gpio_getscl
- mgag200_i2c_create
- mgag200_i2c_destroy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include <linux/export.h>
30 #include <linux/i2c-algo-bit.h>
31 #include <linux/i2c.h>
32
33 #include <drm/drm_pci.h>
34
35 #include "mgag200_drv.h"
36
37 static int mga_i2c_read_gpio(struct mga_device *mdev)
38 {
39 WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
40 return RREG8(DAC_DATA);
41 }
42
43 static void mga_i2c_set_gpio(struct mga_device *mdev, int mask, int val)
44 {
45 int tmp;
46
47 WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
48 tmp = (RREG8(DAC_DATA) & mask) | val;
49 WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
50 WREG_DAC(MGA1064_GEN_IO_DATA, 0);
51 }
52
53 static inline void mga_i2c_set(struct mga_device *mdev, int mask, int state)
54 {
55 if (state)
56 state = 0;
57 else
58 state = mask;
59 mga_i2c_set_gpio(mdev, ~mask, state);
60 }
61
62 static void mga_gpio_setsda(void *data, int state)
63 {
64 struct mga_i2c_chan *i2c = data;
65 struct mga_device *mdev = i2c->dev->dev_private;
66 mga_i2c_set(mdev, i2c->data, state);
67 }
68
69 static void mga_gpio_setscl(void *data, int state)
70 {
71 struct mga_i2c_chan *i2c = data;
72 struct mga_device *mdev = i2c->dev->dev_private;
73 mga_i2c_set(mdev, i2c->clock, state);
74 }
75
76 static int mga_gpio_getsda(void *data)
77 {
78 struct mga_i2c_chan *i2c = data;
79 struct mga_device *mdev = i2c->dev->dev_private;
80 return (mga_i2c_read_gpio(mdev) & i2c->data) ? 1 : 0;
81 }
82
83 static int mga_gpio_getscl(void *data)
84 {
85 struct mga_i2c_chan *i2c = data;
86 struct mga_device *mdev = i2c->dev->dev_private;
87 return (mga_i2c_read_gpio(mdev) & i2c->clock) ? 1 : 0;
88 }
89
90 struct mga_i2c_chan *mgag200_i2c_create(struct drm_device *dev)
91 {
92 struct mga_device *mdev = dev->dev_private;
93 struct mga_i2c_chan *i2c;
94 int ret;
95 int data, clock;
96
97 WREG_DAC(MGA1064_GEN_IO_CTL2, 1);
98 WREG_DAC(MGA1064_GEN_IO_DATA, 0xff);
99 WREG_DAC(MGA1064_GEN_IO_CTL, 0);
100
101 switch (mdev->type) {
102 case G200_SE_A:
103 case G200_SE_B:
104 case G200_EV:
105 case G200_WB:
106 case G200_EW3:
107 data = 1;
108 clock = 2;
109 break;
110 case G200_EH:
111 case G200_EH3:
112 case G200_ER:
113 data = 2;
114 clock = 1;
115 break;
116 default:
117 data = 2;
118 clock = 8;
119 break;
120 }
121
122 i2c = kzalloc(sizeof(struct mga_i2c_chan), GFP_KERNEL);
123 if (!i2c)
124 return NULL;
125
126 i2c->data = data;
127 i2c->clock = clock;
128 i2c->adapter.owner = THIS_MODULE;
129 i2c->adapter.class = I2C_CLASS_DDC;
130 i2c->adapter.dev.parent = &dev->pdev->dev;
131 i2c->dev = dev;
132 i2c_set_adapdata(&i2c->adapter, i2c);
133 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name), "mga i2c");
134
135 i2c->adapter.algo_data = &i2c->bit;
136
137 i2c->bit.udelay = 10;
138 i2c->bit.timeout = 2;
139 i2c->bit.data = i2c;
140 i2c->bit.setsda = mga_gpio_setsda;
141 i2c->bit.setscl = mga_gpio_setscl;
142 i2c->bit.getsda = mga_gpio_getsda;
143 i2c->bit.getscl = mga_gpio_getscl;
144
145 ret = i2c_bit_add_bus(&i2c->adapter);
146 if (ret) {
147 kfree(i2c);
148 i2c = NULL;
149 }
150 return i2c;
151 }
152
153 void mgag200_i2c_destroy(struct mga_i2c_chan *i2c)
154 {
155 if (!i2c)
156 return;
157 i2c_del_adapter(&i2c->adapter);
158 kfree(i2c);
159 }
160