2012-07-10 12:36:38 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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
|
2015-08-20 12:54:15 +08:00
|
|
|
* all copies or substantial busions of the Software.
|
2012-07-10 12:36:38 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2015-08-20 12:54:15 +08:00
|
|
|
#include "bus.h"
|
2012-07-10 12:36:38 +08:00
|
|
|
|
2015-01-14 13:05:26 +08:00
|
|
|
#ifdef CONFIG_NOUVEAU_I2C_INTERNAL
|
2012-07-10 12:36:38 +08:00
|
|
|
#define T_TIMEOUT 2200000
|
|
|
|
#define T_RISEFALL 1000
|
|
|
|
#define T_HOLD 5000
|
|
|
|
|
|
|
|
static inline void
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_drive_scl(struct nvkm_i2c_bus *bus, int state)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
2015-08-20 12:54:15 +08:00
|
|
|
bus->func->drive_scl(bus, state);
|
2012-07-10 12:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_drive_sda(struct nvkm_i2c_bus *bus, int state)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
2015-08-20 12:54:15 +08:00
|
|
|
bus->func->drive_sda(bus, state);
|
2012-07-10 12:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_sense_scl(struct nvkm_i2c_bus *bus)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
2015-08-20 12:54:15 +08:00
|
|
|
return bus->func->sense_scl(bus);
|
2012-07-10 12:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_sense_sda(struct nvkm_i2c_bus *bus)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
2015-08-20 12:54:15 +08:00
|
|
|
return bus->func->sense_sda(bus);
|
2012-07-10 12:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_delay(struct nvkm_i2c_bus *bus, u32 nsec)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
|
|
|
udelay((nsec + 500) / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_raise_scl(struct nvkm_i2c_bus *bus)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
|
|
|
u32 timeout = T_TIMEOUT / T_RISEFALL;
|
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_drive_scl(bus, 1);
|
2012-07-10 12:36:38 +08:00
|
|
|
do {
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_delay(bus, T_RISEFALL);
|
|
|
|
} while (!nvkm_i2c_sense_scl(bus) && --timeout);
|
2012-07-10 12:36:38 +08:00
|
|
|
|
|
|
|
return timeout != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-08-20 12:54:15 +08:00
|
|
|
i2c_start(struct nvkm_i2c_bus *bus)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
if (!nvkm_i2c_sense_scl(bus) ||
|
|
|
|
!nvkm_i2c_sense_sda(bus)) {
|
|
|
|
nvkm_i2c_drive_scl(bus, 0);
|
|
|
|
nvkm_i2c_drive_sda(bus, 1);
|
|
|
|
if (!nvkm_i2c_raise_scl(bus))
|
2012-07-10 12:36:38 +08:00
|
|
|
ret = -EBUSY;
|
|
|
|
}
|
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_drive_sda(bus, 0);
|
|
|
|
nvkm_i2c_delay(bus, T_HOLD);
|
|
|
|
nvkm_i2c_drive_scl(bus, 0);
|
|
|
|
nvkm_i2c_delay(bus, T_HOLD);
|
2012-07-10 12:36:38 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-08-20 12:54:15 +08:00
|
|
|
i2c_stop(struct nvkm_i2c_bus *bus)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_drive_scl(bus, 0);
|
|
|
|
nvkm_i2c_drive_sda(bus, 0);
|
|
|
|
nvkm_i2c_delay(bus, T_RISEFALL);
|
|
|
|
|
|
|
|
nvkm_i2c_drive_scl(bus, 1);
|
|
|
|
nvkm_i2c_delay(bus, T_HOLD);
|
|
|
|
nvkm_i2c_drive_sda(bus, 1);
|
|
|
|
nvkm_i2c_delay(bus, T_HOLD);
|
2012-07-10 12:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-08-20 12:54:15 +08:00
|
|
|
i2c_bitw(struct nvkm_i2c_bus *bus, int sda)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_drive_sda(bus, sda);
|
|
|
|
nvkm_i2c_delay(bus, T_RISEFALL);
|
2012-07-10 12:36:38 +08:00
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
if (!nvkm_i2c_raise_scl(bus))
|
2012-07-10 12:36:38 +08:00
|
|
|
return -ETIMEDOUT;
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_delay(bus, T_HOLD);
|
2012-07-10 12:36:38 +08:00
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_drive_scl(bus, 0);
|
|
|
|
nvkm_i2c_delay(bus, T_HOLD);
|
2012-07-10 12:36:38 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-08-20 12:54:15 +08:00
|
|
|
i2c_bitr(struct nvkm_i2c_bus *bus)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
|
|
|
int sda;
|
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_drive_sda(bus, 1);
|
|
|
|
nvkm_i2c_delay(bus, T_RISEFALL);
|
2012-07-10 12:36:38 +08:00
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
if (!nvkm_i2c_raise_scl(bus))
|
2012-07-10 12:36:38 +08:00
|
|
|
return -ETIMEDOUT;
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_delay(bus, T_HOLD);
|
2012-07-10 12:36:38 +08:00
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
sda = nvkm_i2c_sense_sda(bus);
|
2012-07-10 12:36:38 +08:00
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_drive_scl(bus, 0);
|
|
|
|
nvkm_i2c_delay(bus, T_HOLD);
|
2012-07-10 12:36:38 +08:00
|
|
|
return sda;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_get_byte(struct nvkm_i2c_bus *bus, u8 *byte, bool last)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
|
|
|
int i, bit;
|
|
|
|
|
|
|
|
*byte = 0;
|
|
|
|
for (i = 7; i >= 0; i--) {
|
2015-08-20 12:54:15 +08:00
|
|
|
bit = i2c_bitr(bus);
|
2012-07-10 12:36:38 +08:00
|
|
|
if (bit < 0)
|
|
|
|
return bit;
|
|
|
|
*byte |= bit << i;
|
|
|
|
}
|
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
return i2c_bitw(bus, last ? 1 : 0);
|
2012-07-10 12:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-08-20 12:54:15 +08:00
|
|
|
nvkm_i2c_put_byte(struct nvkm_i2c_bus *bus, u8 byte)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
for (i = 7; i >= 0; i--) {
|
2015-08-20 12:54:15 +08:00
|
|
|
ret = i2c_bitw(bus, !!(byte & (1 << i)));
|
2012-07-10 12:36:38 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
ret = i2c_bitr(bus);
|
2012-07-10 12:36:38 +08:00
|
|
|
if (ret == 1) /* nack */
|
|
|
|
ret = -EIO;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-08-20 12:54:15 +08:00
|
|
|
i2c_addr(struct nvkm_i2c_bus *bus, struct i2c_msg *msg)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
|
|
|
u32 addr = msg->addr << 1;
|
|
|
|
if (msg->flags & I2C_M_RD)
|
|
|
|
addr |= 1;
|
2015-08-20 12:54:15 +08:00
|
|
|
return nvkm_i2c_put_byte(bus, addr);
|
2012-07-10 12:36:38 +08:00
|
|
|
}
|
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
int
|
|
|
|
nvkm_i2c_bit_xfer(struct nvkm_i2c_bus *bus, struct i2c_msg *msgs, int num)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
|
|
|
struct i2c_msg *msg = msgs;
|
|
|
|
int ret = 0, mcnt = num;
|
|
|
|
|
|
|
|
while (!ret && mcnt--) {
|
|
|
|
u8 remaining = msg->len;
|
|
|
|
u8 *ptr = msg->buf;
|
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
ret = i2c_start(bus);
|
2012-07-10 12:36:38 +08:00
|
|
|
if (ret == 0)
|
2015-08-20 12:54:15 +08:00
|
|
|
ret = i2c_addr(bus, msg);
|
2012-07-10 12:36:38 +08:00
|
|
|
|
|
|
|
if (msg->flags & I2C_M_RD) {
|
|
|
|
while (!ret && remaining--)
|
2015-08-20 12:54:15 +08:00
|
|
|
ret = nvkm_i2c_get_byte(bus, ptr++, !remaining);
|
2012-07-10 12:36:38 +08:00
|
|
|
} else {
|
|
|
|
while (!ret && remaining--)
|
2015-08-20 12:54:15 +08:00
|
|
|
ret = nvkm_i2c_put_byte(bus, *ptr++);
|
2012-07-10 12:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
msg++;
|
|
|
|
}
|
|
|
|
|
2015-08-20 12:54:15 +08:00
|
|
|
i2c_stop(bus);
|
2012-07-10 12:36:38 +08:00
|
|
|
return (ret < 0) ? ret : num;
|
|
|
|
}
|
|
|
|
#else
|
2015-08-20 12:54:15 +08:00
|
|
|
int
|
|
|
|
nvkm_i2c_bit_xfer(struct nvkm_i2c_bus *bus, struct i2c_msg *msgs, int num)
|
2012-07-10 12:36:38 +08:00
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|