2 * Copyright (C) 2008 Maarten Maathuis.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 /* This is largely a clone from xorg i2c functions, as i had serious trouble getting an i2c_bit_algo adaptor running. */
31 static void nv50_i2c_set_bits(struct nv50_i2c_channel *chan, int clock_high, int data_high)
33 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
35 NV_WRITE(NV50_PCONNECTOR_I2C_PORT(chan->index), 4 | (data_high << 1) | clock_high);
38 static void nv50_i2c_get_bits(struct nv50_i2c_channel *chan, int *clock_high, int *data_high)
40 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
42 uint32_t val = NV_READ(NV50_PCONNECTOR_I2C_PORT(chan->index));
55 static bool nv50_i2c_raise_clock(struct nv50_i2c_channel *chan, int data)
59 nv50_i2c_set_bits(chan, 1, data);
62 for (i = 2200; i > 0; i -= 2) {
63 nv50_i2c_get_bits(chan, &clock, &data);
69 printk("a timeout occured in nv50_i2c_raise_clock\n");
74 static bool nv50_i2c_start(struct nv50_i2c_channel *chan)
76 if (!nv50_i2c_raise_clock(chan, 1))
79 nv50_i2c_set_bits(chan, 1, 0);
82 nv50_i2c_set_bits(chan, 0, 0);
88 static void nv50_i2c_stop(struct nv50_i2c_channel *chan)
90 nv50_i2c_set_bits(chan, 0, 0);
93 nv50_i2c_set_bits(chan, 1, 0);
96 nv50_i2c_set_bits(chan, 1, 1);
100 static bool nv50_i2c_write_bit(struct nv50_i2c_channel *chan, int data)
104 nv50_i2c_set_bits(chan, 0, data);
107 rval = nv50_i2c_raise_clock(chan, data);
110 nv50_i2c_set_bits(chan, 0, data);
116 static bool nv50_i2c_read_bit(struct nv50_i2c_channel *chan, int *data)
121 rval = nv50_i2c_raise_clock(chan, 1);
124 nv50_i2c_get_bits(chan, &clock, data);
127 nv50_i2c_set_bits(chan, 0, 1);
133 static bool nv50_i2c_write_byte(struct nv50_i2c_channel *chan, uint8_t byte)
138 for (i = 7; i >= 0; i--)
139 if (!nv50_i2c_write_bit(chan, (byte >> i) & 1))
142 nv50_i2c_set_bits(chan, 0, 1);
145 rval = nv50_i2c_raise_clock(chan, 1);
148 for (i = 40; i > 0; i -= 2) {
150 nv50_i2c_get_bits(chan, &clock, &data);
156 printk("a timeout occured in nv50_i2c_write_byte\n");
161 nv50_i2c_set_bits(chan, 0, 1);
167 static bool nv50_i2c_read_byte(struct nv50_i2c_channel *chan, uint8_t *byte, bool last)
171 nv50_i2c_set_bits(chan, 0, 1);
176 for (i = 7; i >= 0; i--) {
177 if (nv50_i2c_read_bit(chan, &bit)) {
185 if (!nv50_i2c_write_bit(chan, last ? 1 : 0))
191 /* only 7 bits addresses. */
192 static bool nv50_i2c_address(struct nv50_i2c_channel *chan, uint8_t address, bool write)
194 if (nv50_i2c_start(chan)) {
195 uint8_t real_addr = (address << 1);
199 if (nv50_i2c_write_byte(chan, real_addr))
202 /* failure, so issue stop */
209 static bool nv50_i2c_read(struct nv50_i2c_channel *chan, uint8_t address, uint8_t *buffer, uint32_t length)
215 for (i = 0; i < 4; i++) {
216 rval = nv50_i2c_address(chan, address, FALSE);
220 for (j = 0; j < length; j++) {
222 if (j == (length - 1))
224 rval = nv50_i2c_read_byte(chan, &buffer[j], last);
239 printk("nv50_i2c_read failed\n");
244 static bool nv50_i2c_write(struct nv50_i2c_channel *chan, uint8_t address, uint8_t *buffer, uint32_t length)
250 for (i = 0; i < 4; i++) {
251 rval = nv50_i2c_address(chan, address, TRUE);
255 for (j = 0; j < length; j++) {
256 rval = nv50_i2c_write_byte(chan, buffer[j]);
270 printk("nv50_i2c_write failed\n");
275 static int nv50_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
277 struct nv50_i2c_channel *chan = i2c_get_adapdata(i2c_adap);
281 for (i = 0; i < num; i++) {
282 if (msgs[i].flags & I2C_M_RD) { /* read */
283 rval = nv50_i2c_read(chan, msgs[i].addr, msgs[i].buf, msgs[i].len);
285 rval = nv50_i2c_write(chan, msgs[i].addr, msgs[i].buf, msgs[i].len);
298 static u32 nv50_i2c_functionality(struct i2c_adapter *adap)
303 static const struct i2c_algorithm nv50_i2c_algo = {
304 .master_xfer = nv50_i2c_xfer,
305 .functionality = nv50_i2c_functionality,
308 static int nv50_i2c_register_bus(struct i2c_adapter *adap)
310 adap->algo = &nv50_i2c_algo;
315 return i2c_add_adapter(adap);
318 #define I2C_HW_B_NOUVEAU 0x010030
319 struct nv50_i2c_channel *nv50_i2c_channel_create(struct drm_device *dev, uint32_t index)
321 struct nv50_i2c_channel *chan;
323 chan = kzalloc(sizeof(struct nv50_i2c_channel), GFP_KERNEL);
328 DRM_INFO("Creating i2c bus with index %d\n", index);
332 snprintf(chan->adapter.name, I2C_NAME_SIZE, "nv50 i2c %d", index);
333 chan->adapter.owner = THIS_MODULE;
334 chan->adapter.id = I2C_HW_B_NOUVEAU;
335 chan->adapter.dev.parent = &dev->pdev->dev;
337 i2c_set_adapdata(&chan->adapter, chan);
339 if (nv50_i2c_register_bus(&chan->adapter))
349 void nv50_i2c_channel_destroy(struct nv50_i2c_channel *chan)
354 i2c_del_adapter(&chan->adapter);