2009-12-11 17:24:15 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* Authors: Ben Skeggs
|
|
|
|
*/
|
|
|
|
|
2011-08-30 23:04:30 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
|
2009-12-11 17:24:15 +08:00
|
|
|
#include "drmP.h"
|
|
|
|
#include "nouveau_drv.h"
|
|
|
|
#include "nouveau_i2c.h"
|
|
|
|
#include "nouveau_hw.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
nv04_i2c_setscl(void *data, int state)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
struct drm_device *dev = i2c->dev;
|
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
val = (NVReadVgaCrtc(dev, 0, i2c->wr) & 0xd0) | (state ? 0x20 : 0);
|
|
|
|
NVWriteVgaCrtc(dev, 0, i2c->wr, val | 0x01);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nv04_i2c_setsda(void *data, int state)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
struct drm_device *dev = i2c->dev;
|
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
val = (NVReadVgaCrtc(dev, 0, i2c->wr) & 0xe0) | (state ? 0x10 : 0);
|
|
|
|
NVWriteVgaCrtc(dev, 0, i2c->wr, val | 0x01);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nv04_i2c_getscl(void *data)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
struct drm_device *dev = i2c->dev;
|
|
|
|
|
|
|
|
return !!(NVReadVgaCrtc(dev, 0, i2c->rd) & 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nv04_i2c_getsda(void *data)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
struct drm_device *dev = i2c->dev;
|
|
|
|
|
|
|
|
return !!(NVReadVgaCrtc(dev, 0, i2c->rd) & 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nv4e_i2c_setscl(void *data, int state)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
struct drm_device *dev = i2c->dev;
|
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
val = (nv_rd32(dev, i2c->wr) & 0xd0) | (state ? 0x20 : 0);
|
|
|
|
nv_wr32(dev, i2c->wr, val | 0x01);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nv4e_i2c_setsda(void *data, int state)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
struct drm_device *dev = i2c->dev;
|
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
val = (nv_rd32(dev, i2c->wr) & 0xe0) | (state ? 0x10 : 0);
|
|
|
|
nv_wr32(dev, i2c->wr, val | 0x01);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nv4e_i2c_getscl(void *data)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
struct drm_device *dev = i2c->dev;
|
|
|
|
|
|
|
|
return !!((nv_rd32(dev, i2c->rd) >> 16) & 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nv4e_i2c_getsda(void *data)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
struct drm_device *dev = i2c->dev;
|
|
|
|
|
|
|
|
return !!((nv_rd32(dev, i2c->rd) >> 16) & 8);
|
|
|
|
}
|
|
|
|
|
2011-07-02 18:43:42 +08:00
|
|
|
static const uint32_t nv50_i2c_port[] = {
|
|
|
|
0x00e138, 0x00e150, 0x00e168, 0x00e180,
|
|
|
|
0x00e254, 0x00e274, 0x00e764, 0x00e780,
|
|
|
|
0x00e79c, 0x00e7b8
|
|
|
|
};
|
|
|
|
#define NV50_I2C_PORTS ARRAY_SIZE(nv50_i2c_port)
|
|
|
|
|
2009-12-11 17:24:15 +08:00
|
|
|
static int
|
|
|
|
nv50_i2c_getscl(void *data)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
struct drm_device *dev = i2c->dev;
|
|
|
|
|
|
|
|
return !!(nv_rd32(dev, i2c->rd) & 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
nv50_i2c_getsda(void *data)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
struct drm_device *dev = i2c->dev;
|
|
|
|
|
|
|
|
return !!(nv_rd32(dev, i2c->rd) & 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nv50_i2c_setscl(void *data, int state)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
|
2011-07-02 18:43:42 +08:00
|
|
|
nv_wr32(i2c->dev, i2c->wr, 4 | (i2c->data ? 2 : 0) | (state ? 1 : 0));
|
2009-12-11 17:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nv50_i2c_setsda(void *data, int state)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
|
2011-07-02 18:43:42 +08:00
|
|
|
nv_mask(i2c->dev, i2c->wr, 0x00000006, 4 | (state ? 2 : 0));
|
2009-12-11 17:24:15 +08:00
|
|
|
i2c->data = state;
|
|
|
|
}
|
|
|
|
|
2011-07-02 18:43:42 +08:00
|
|
|
static int
|
|
|
|
nvd0_i2c_getscl(void *data)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
return !!(nv_rd32(i2c->dev, i2c->rd) & 0x10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nvd0_i2c_getsda(void *data)
|
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = data;
|
|
|
|
return !!(nv_rd32(i2c->dev, i2c->rd) & 0x20);
|
|
|
|
}
|
2009-12-11 17:24:15 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
nouveau_i2c_init(struct drm_device *dev, struct dcb_i2c_entry *entry, int index)
|
|
|
|
{
|
|
|
|
struct drm_nouveau_private *dev_priv = dev->dev_private;
|
|
|
|
struct nouveau_i2c_chan *i2c;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (entry->chan)
|
|
|
|
return -EEXIST;
|
|
|
|
|
2011-07-02 18:43:42 +08:00
|
|
|
if (dev_priv->card_type >= NV_50 &&
|
|
|
|
dev_priv->card_type <= NV_C0 && entry->read >= NV50_I2C_PORTS) {
|
2009-12-11 17:24:15 +08:00
|
|
|
NV_ERROR(dev, "unknown i2c port %d\n", entry->read);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
|
|
|
|
if (i2c == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
switch (entry->port_type) {
|
|
|
|
case 0:
|
2010-07-29 19:01:45 +08:00
|
|
|
i2c->bit.setsda = nv04_i2c_setsda;
|
|
|
|
i2c->bit.setscl = nv04_i2c_setscl;
|
|
|
|
i2c->bit.getsda = nv04_i2c_getsda;
|
|
|
|
i2c->bit.getscl = nv04_i2c_getscl;
|
2009-12-11 17:24:15 +08:00
|
|
|
i2c->rd = entry->read;
|
|
|
|
i2c->wr = entry->write;
|
|
|
|
break;
|
|
|
|
case 4:
|
2010-07-29 19:01:45 +08:00
|
|
|
i2c->bit.setsda = nv4e_i2c_setsda;
|
|
|
|
i2c->bit.setscl = nv4e_i2c_setscl;
|
|
|
|
i2c->bit.getsda = nv4e_i2c_getsda;
|
|
|
|
i2c->bit.getscl = nv4e_i2c_getscl;
|
2009-12-11 17:24:15 +08:00
|
|
|
i2c->rd = 0x600800 + entry->read;
|
|
|
|
i2c->wr = 0x600800 + entry->write;
|
|
|
|
break;
|
|
|
|
case 5:
|
2010-07-29 19:01:45 +08:00
|
|
|
i2c->bit.setsda = nv50_i2c_setsda;
|
|
|
|
i2c->bit.setscl = nv50_i2c_setscl;
|
2011-07-02 18:43:42 +08:00
|
|
|
if (dev_priv->card_type < NV_D0) {
|
|
|
|
i2c->bit.getsda = nv50_i2c_getsda;
|
|
|
|
i2c->bit.getscl = nv50_i2c_getscl;
|
|
|
|
i2c->rd = nv50_i2c_port[entry->read];
|
|
|
|
i2c->wr = i2c->rd;
|
|
|
|
} else {
|
|
|
|
i2c->bit.getsda = nvd0_i2c_getsda;
|
|
|
|
i2c->bit.getscl = nvd0_i2c_getscl;
|
|
|
|
i2c->rd = 0x00d014 + (entry->read * 0x20);
|
|
|
|
i2c->wr = i2c->rd;
|
|
|
|
}
|
2009-12-11 17:24:15 +08:00
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
i2c->rd = entry->read;
|
|
|
|
i2c->wr = entry->write;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NV_ERROR(dev, "DCB I2C port type %d unknown\n",
|
|
|
|
entry->port_type);
|
|
|
|
kfree(i2c);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
|
|
|
|
"nouveau-%s-%d", pci_name(dev->pdev), index);
|
|
|
|
i2c->adapter.owner = THIS_MODULE;
|
|
|
|
i2c->adapter.dev.parent = &dev->pdev->dev;
|
|
|
|
i2c->dev = dev;
|
|
|
|
i2c_set_adapdata(&i2c->adapter, i2c);
|
|
|
|
|
|
|
|
if (entry->port_type < 6) {
|
2010-07-29 19:01:45 +08:00
|
|
|
i2c->adapter.algo_data = &i2c->bit;
|
|
|
|
i2c->bit.udelay = 40;
|
|
|
|
i2c->bit.timeout = usecs_to_jiffies(5000);
|
|
|
|
i2c->bit.data = i2c;
|
2009-12-11 17:24:15 +08:00
|
|
|
ret = i2c_bit_add_bus(&i2c->adapter);
|
|
|
|
} else {
|
2010-07-29 19:01:45 +08:00
|
|
|
i2c->adapter.algo = &nouveau_dp_i2c_algo;
|
|
|
|
ret = i2c_add_adapter(&i2c->adapter);
|
2009-12-11 17:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
NV_ERROR(dev, "Failed to register i2c %d\n", index);
|
|
|
|
kfree(i2c);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->chan = i2c;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nouveau_i2c_fini(struct drm_device *dev, struct dcb_i2c_entry *entry)
|
|
|
|
{
|
|
|
|
if (!entry->chan)
|
|
|
|
return;
|
|
|
|
|
|
|
|
i2c_del_adapter(&entry->chan->adapter);
|
|
|
|
kfree(entry->chan);
|
|
|
|
entry->chan = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct nouveau_i2c_chan *
|
|
|
|
nouveau_i2c_find(struct drm_device *dev, int index)
|
|
|
|
{
|
|
|
|
struct drm_nouveau_private *dev_priv = dev->dev_private;
|
2010-04-24 01:53:14 +08:00
|
|
|
struct dcb_i2c_entry *i2c = &dev_priv->vbios.dcb.i2c[index];
|
2009-12-11 17:24:15 +08:00
|
|
|
|
2010-02-18 02:04:01 +08:00
|
|
|
if (index >= DCB_MAX_NUM_I2C_ENTRIES)
|
2009-12-11 17:24:15 +08:00
|
|
|
return NULL;
|
|
|
|
|
2010-11-11 07:56:37 +08:00
|
|
|
if (dev_priv->card_type >= NV_50 && (i2c->entry & 0x00000100)) {
|
2010-04-24 01:53:14 +08:00
|
|
|
uint32_t reg = 0xe500, val;
|
|
|
|
|
|
|
|
if (i2c->port_type == 6) {
|
|
|
|
reg += i2c->read * 0x50;
|
|
|
|
val = 0x2002;
|
|
|
|
} else {
|
|
|
|
reg += ((i2c->entry & 0x1e00) >> 9) * 0x50;
|
|
|
|
val = 0xe001;
|
|
|
|
}
|
|
|
|
|
2011-07-19 13:59:39 +08:00
|
|
|
/* nfi, but neither auxch or i2c work if it's 1 */
|
|
|
|
nv_mask(dev, reg + 0x0c, 0x00000001, 0x00000000);
|
|
|
|
/* nfi, but switches auxch vs normal i2c */
|
|
|
|
nv_mask(dev, reg + 0x00, 0x0000f003, val);
|
2009-12-11 17:24:15 +08:00
|
|
|
}
|
|
|
|
|
2010-04-24 01:53:14 +08:00
|
|
|
if (!i2c->chan && nouveau_i2c_init(dev, i2c, index))
|
|
|
|
return NULL;
|
|
|
|
return i2c->chan;
|
2009-12-11 17:24:15 +08:00
|
|
|
}
|
|
|
|
|
2010-07-19 21:55:08 +08:00
|
|
|
bool
|
|
|
|
nouveau_probe_i2c_addr(struct nouveau_i2c_chan *i2c, int addr)
|
|
|
|
{
|
2010-07-24 05:17:57 +08:00
|
|
|
uint8_t buf[] = { 0 };
|
|
|
|
struct i2c_msg msgs[] = {
|
|
|
|
{
|
|
|
|
.addr = addr,
|
|
|
|
.flags = 0,
|
|
|
|
.len = 1,
|
|
|
|
.buf = buf,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.addr = addr,
|
|
|
|
.flags = I2C_M_RD,
|
|
|
|
.len = 1,
|
|
|
|
.buf = buf,
|
|
|
|
}
|
2010-07-19 21:55:08 +08:00
|
|
|
};
|
|
|
|
|
2010-07-24 05:17:57 +08:00
|
|
|
return i2c_transfer(&i2c->adapter, msgs, 2) == 2;
|
2010-07-19 21:55:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nouveau_i2c_identify(struct drm_device *dev, const char *what,
|
2010-09-24 03:00:40 +08:00
|
|
|
struct i2c_board_info *info,
|
|
|
|
bool (*match)(struct nouveau_i2c_chan *,
|
|
|
|
struct i2c_board_info *),
|
|
|
|
int index)
|
2010-07-19 21:55:08 +08:00
|
|
|
{
|
|
|
|
struct nouveau_i2c_chan *i2c = nouveau_i2c_find(dev, index);
|
2010-07-24 23:41:48 +08:00
|
|
|
int i;
|
2010-07-19 21:55:08 +08:00
|
|
|
|
|
|
|
NV_DEBUG(dev, "Probing %ss on I2C bus: %d\n", what, index);
|
|
|
|
|
2011-09-22 10:51:31 +08:00
|
|
|
for (i = 0; i2c && info[i].addr; i++) {
|
2010-09-24 03:00:40 +08:00
|
|
|
if (nouveau_probe_i2c_addr(i2c, info[i].addr) &&
|
|
|
|
(!match || match(i2c, &info[i]))) {
|
2010-07-19 21:55:08 +08:00
|
|
|
NV_INFO(dev, "Detected %s: %s\n", what, info[i].type);
|
2010-07-24 23:41:48 +08:00
|
|
|
return i;
|
2010-07-19 21:55:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NV_DEBUG(dev, "No devices found.\n");
|
|
|
|
|
2010-07-24 23:41:48 +08:00
|
|
|
return -ENODEV;
|
2010-07-19 21:55:08 +08:00
|
|
|
}
|