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 uint32_t nv50_i2c_port(int index)
37 port = NV50_PCONNECTOR_I2C_PORT_0;
40 port = NV50_PCONNECTOR_I2C_PORT_1;
43 port = NV50_PCONNECTOR_I2C_PORT_2;
46 port = NV50_PCONNECTOR_I2C_PORT_3;
49 port = NV50_PCONNECTOR_I2C_PORT_4;
52 port = NV50_PCONNECTOR_I2C_PORT_5;
59 DRM_ERROR("Invalid i2c port, returning 0.\n");
66 static void nv50_i2c_set_bits(struct nv50_i2c_channel *chan, int clock_high, int data_high)
68 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
69 uint32_t port = nv50_i2c_port(chan->index);
74 NV_WRITE(port, 4 | (data_high << 1) | clock_high);
77 static void nv50_i2c_get_bits(struct nv50_i2c_channel *chan, int *clock_high, int *data_high)
79 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
80 uint32_t port = nv50_i2c_port(chan->index);
99 static bool nv50_i2c_raise_clock(struct nv50_i2c_channel *chan, int data)
103 nv50_i2c_set_bits(chan, 1, data);
106 for (i = 2200; i > 0; i -= 2) {
107 nv50_i2c_get_bits(chan, &clock, &data);
113 printk("a timeout occured in nv50_i2c_raise_clock\n");
118 static bool nv50_i2c_start(struct nv50_i2c_channel *chan)
120 if (!nv50_i2c_raise_clock(chan, 1))
123 nv50_i2c_set_bits(chan, 1, 0);
126 nv50_i2c_set_bits(chan, 0, 0);
132 static void nv50_i2c_stop(struct nv50_i2c_channel *chan)
134 nv50_i2c_set_bits(chan, 0, 0);
137 nv50_i2c_set_bits(chan, 1, 0);
140 nv50_i2c_set_bits(chan, 1, 1);
144 static bool nv50_i2c_write_bit(struct nv50_i2c_channel *chan, int data)
148 nv50_i2c_set_bits(chan, 0, data);
151 rval = nv50_i2c_raise_clock(chan, data);
154 nv50_i2c_set_bits(chan, 0, data);
160 static bool nv50_i2c_read_bit(struct nv50_i2c_channel *chan, int *data)
165 rval = nv50_i2c_raise_clock(chan, 1);
168 nv50_i2c_get_bits(chan, &clock, data);
171 nv50_i2c_set_bits(chan, 0, 1);
177 static bool nv50_i2c_write_byte(struct nv50_i2c_channel *chan, uint8_t byte)
182 for (i = 7; i >= 0; i--)
183 if (!nv50_i2c_write_bit(chan, (byte >> i) & 1))
186 nv50_i2c_set_bits(chan, 0, 1);
189 rval = nv50_i2c_raise_clock(chan, 1);
192 for (i = 40; i > 0; i -= 2) {
194 nv50_i2c_get_bits(chan, &clock, &data);
200 printk("a timeout occured in nv50_i2c_write_byte\n");
205 nv50_i2c_set_bits(chan, 0, 1);
211 static bool nv50_i2c_read_byte(struct nv50_i2c_channel *chan, uint8_t *byte, bool last)
215 nv50_i2c_set_bits(chan, 0, 1);
220 for (i = 7; i >= 0; i--) {
221 if (nv50_i2c_read_bit(chan, &bit)) {
229 if (!nv50_i2c_write_bit(chan, last ? 1 : 0))
235 /* only 7 bits addresses. */
236 static bool nv50_i2c_address(struct nv50_i2c_channel *chan, uint8_t address, bool write)
238 if (nv50_i2c_start(chan)) {
239 uint8_t real_addr = (address << 1);
243 if (nv50_i2c_write_byte(chan, real_addr))
246 /* failure, so issue stop */
253 static bool nv50_i2c_read(struct nv50_i2c_channel *chan, uint8_t address, uint8_t *buffer, uint32_t length)
259 for (i = 0; i < 4; i++) {
260 rval = nv50_i2c_address(chan, address, false);
264 for (j = 0; j < length; j++) {
266 if (j == (length - 1))
268 rval = nv50_i2c_read_byte(chan, &buffer[j], last);
283 printk("nv50_i2c_read failed\n");
288 static bool nv50_i2c_write(struct nv50_i2c_channel *chan, uint8_t address, uint8_t *buffer, uint32_t length)
294 for (i = 0; i < 4; i++) {
295 rval = nv50_i2c_address(chan, address, true);
299 for (j = 0; j < length; j++) {
300 rval = nv50_i2c_write_byte(chan, buffer[j]);
314 printk("nv50_i2c_write failed\n");
319 static int nv50_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
321 struct nv50_i2c_channel *chan = i2c_get_adapdata(i2c_adap);
325 for (i = 0; i < num; i++) {
326 if (msgs[i].flags & I2C_M_RD) { /* read */
327 rval = nv50_i2c_read(chan, msgs[i].addr, msgs[i].buf, msgs[i].len);
329 rval = nv50_i2c_write(chan, msgs[i].addr, msgs[i].buf, msgs[i].len);
342 static u32 nv50_i2c_functionality(struct i2c_adapter *adap)
347 static const struct i2c_algorithm nv50_i2c_algo = {
348 .master_xfer = nv50_i2c_xfer,
349 .functionality = nv50_i2c_functionality,
352 static int nv50_i2c_register_bus(struct i2c_adapter *adap)
354 adap->algo = &nv50_i2c_algo;
359 return i2c_add_adapter(adap);
362 #define I2C_HW_B_NOUVEAU 0x010030
363 struct nv50_i2c_channel *nv50_i2c_channel_create(struct drm_device *dev, uint32_t index)
365 struct nv50_i2c_channel *chan;
367 chan = kzalloc(sizeof(struct nv50_i2c_channel), GFP_KERNEL);
372 DRM_INFO("Creating i2c bus with index %d\n", index);
376 snprintf(chan->adapter.name, I2C_NAME_SIZE, "nv50 i2c %d", index);
377 chan->adapter.owner = THIS_MODULE;
378 chan->adapter.id = I2C_HW_B_NOUVEAU;
379 chan->adapter.dev.parent = &dev->pdev->dev;
381 i2c_set_adapdata(&chan->adapter, chan);
383 if (nv50_i2c_register_bus(&chan->adapter))
393 void nv50_i2c_channel_destroy(struct nv50_i2c_channel *chan)
398 i2c_del_adapter(&chan->adapter);