196a78ac0SYen Lin /* 296a78ac0SYen Lin * Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 396a78ac0SYen Lin * Copyright (c) 2010-2011 NVIDIA Corporation 496a78ac0SYen Lin * NVIDIA Corporation <www.nvidia.com> 596a78ac0SYen Lin * 61a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 796a78ac0SYen Lin */ 896a78ac0SYen Lin 996a78ac0SYen Lin #include <common.h> 10b0e6ef46SSimon Glass #include <dm.h> 11b0e6ef46SSimon Glass #include <errno.h> 1296a78ac0SYen Lin #include <fdtdec.h> 1396a78ac0SYen Lin #include <i2c.h> 1496a78ac0SYen Lin #include <asm/io.h> 1596a78ac0SYen Lin #include <asm/arch/clock.h> 1696a78ac0SYen Lin #include <asm/arch/funcmux.h> 1796a78ac0SYen Lin #include <asm/arch/gpio.h> 1896a78ac0SYen Lin #include <asm/arch/pinmux.h> 19150c2493STom Warren #include <asm/arch-tegra/clk_rst.h> 20150c2493STom Warren #include <asm/arch-tegra/tegra_i2c.h> 2196a78ac0SYen Lin 2296a78ac0SYen Lin DECLARE_GLOBAL_DATA_PTR; 2396a78ac0SYen Lin 24b0e6ef46SSimon Glass enum i2c_type { 25b0e6ef46SSimon Glass TYPE_114, 26b0e6ef46SSimon Glass TYPE_STD, 27b0e6ef46SSimon Glass TYPE_DVC, 28b0e6ef46SSimon Glass }; 29b0e6ef46SSimon Glass 3096a78ac0SYen Lin /* Information about i2c controller */ 3196a78ac0SYen Lin struct i2c_bus { 3296a78ac0SYen Lin int id; 3396a78ac0SYen Lin enum periph_id periph_id; 3496a78ac0SYen Lin int speed; 3596a78ac0SYen Lin int pinmux_config; 3696a78ac0SYen Lin struct i2c_control *control; 3796a78ac0SYen Lin struct i2c_ctlr *regs; 38b0e6ef46SSimon Glass enum i2c_type type; 3996a78ac0SYen Lin int inited; /* bus is inited */ 4096a78ac0SYen Lin }; 4196a78ac0SYen Lin 4296a78ac0SYen Lin static void set_packet_mode(struct i2c_bus *i2c_bus) 4396a78ac0SYen Lin { 4496a78ac0SYen Lin u32 config; 4596a78ac0SYen Lin 4696a78ac0SYen Lin config = I2C_CNFG_NEW_MASTER_FSM_MASK | I2C_CNFG_PACKET_MODE_MASK; 4796a78ac0SYen Lin 48b0e6ef46SSimon Glass if (i2c_bus->type == TYPE_DVC) { 4996a78ac0SYen Lin struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs; 5096a78ac0SYen Lin 5196a78ac0SYen Lin writel(config, &dvc->cnfg); 5296a78ac0SYen Lin } else { 5396a78ac0SYen Lin writel(config, &i2c_bus->regs->cnfg); 5496a78ac0SYen Lin /* 5596a78ac0SYen Lin * program I2C_SL_CNFG.NEWSL to ENABLE. This fixes probe 5696a78ac0SYen Lin * issues, i.e., some slaves may be wrongly detected. 5796a78ac0SYen Lin */ 5896a78ac0SYen Lin setbits_le32(&i2c_bus->regs->sl_cnfg, I2C_SL_CNFG_NEWSL_MASK); 5996a78ac0SYen Lin } 6096a78ac0SYen Lin } 6196a78ac0SYen Lin 6296a78ac0SYen Lin static void i2c_reset_controller(struct i2c_bus *i2c_bus) 6396a78ac0SYen Lin { 6496a78ac0SYen Lin /* Reset I2C controller. */ 6596a78ac0SYen Lin reset_periph(i2c_bus->periph_id, 1); 6696a78ac0SYen Lin 6796a78ac0SYen Lin /* re-program config register to packet mode */ 6896a78ac0SYen Lin set_packet_mode(i2c_bus); 6996a78ac0SYen Lin } 7096a78ac0SYen Lin 7196a78ac0SYen Lin static void i2c_init_controller(struct i2c_bus *i2c_bus) 7296a78ac0SYen Lin { 73b0e6ef46SSimon Glass if (!i2c_bus->speed) 74b0e6ef46SSimon Glass return; 75b0e6ef46SSimon Glass debug("%s: speed=%d\n", __func__, i2c_bus->speed); 7696a78ac0SYen Lin /* 7796a78ac0SYen Lin * Use PLLP - DP-04508-001_v06 datasheet indicates a divisor of 8 7896a78ac0SYen Lin * here, in section 23.3.1, but in fact we seem to need a factor of 7996a78ac0SYen Lin * 16 to get the right frequency. 8096a78ac0SYen Lin */ 8196a78ac0SYen Lin clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH, 8296a78ac0SYen Lin i2c_bus->speed * 2 * 8); 8396a78ac0SYen Lin 84b0e6ef46SSimon Glass if (i2c_bus->type == TYPE_114) { 85e32624efSTom Warren /* 86e32624efSTom Warren * T114 I2C went to a single clock source for standard/fast and 87e32624efSTom Warren * HS clock speeds. The new clock rate setting calculation is: 88e32624efSTom Warren * SCL = CLK_SOURCE.I2C / 89e32624efSTom Warren * (CLK_MULT_STD_FAST_MODE * (I2C_CLK_DIV_STD_FAST_MODE+1) * 90e32624efSTom Warren * I2C FREQUENCY DIVISOR) as per the T114 TRM (sec 30.3.1). 91e32624efSTom Warren * 92e32624efSTom Warren * NOTE: We do this here, after the initial clock/pll start, 93e32624efSTom Warren * because if we read the clk_div reg before the controller 94e32624efSTom Warren * is running, we hang, and we need it for the new calc. 95e32624efSTom Warren */ 96e32624efSTom Warren int clk_div_stdfst_mode = readl(&i2c_bus->regs->clk_div) >> 16; 97e32624efSTom Warren debug("%s: CLK_DIV_STD_FAST_MODE setting = %d\n", __func__, 98e32624efSTom Warren clk_div_stdfst_mode); 99e32624efSTom Warren 100e32624efSTom Warren clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH, 101e32624efSTom Warren CLK_MULT_STD_FAST_MODE * (clk_div_stdfst_mode + 1) * 102e32624efSTom Warren i2c_bus->speed * 2); 103e32624efSTom Warren } 104e32624efSTom Warren 10596a78ac0SYen Lin /* Reset I2C controller. */ 10696a78ac0SYen Lin i2c_reset_controller(i2c_bus); 10796a78ac0SYen Lin 10896a78ac0SYen Lin /* Configure I2C controller. */ 109b0e6ef46SSimon Glass if (i2c_bus->type == TYPE_DVC) { /* only for DVC I2C */ 11096a78ac0SYen Lin struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs; 11196a78ac0SYen Lin 11296a78ac0SYen Lin setbits_le32(&dvc->ctrl3, DVC_CTRL_REG3_I2C_HW_SW_PROG_MASK); 11396a78ac0SYen Lin } 11496a78ac0SYen Lin 11596a78ac0SYen Lin funcmux_select(i2c_bus->periph_id, i2c_bus->pinmux_config); 11696a78ac0SYen Lin } 11796a78ac0SYen Lin 11896a78ac0SYen Lin static void send_packet_headers( 11996a78ac0SYen Lin struct i2c_bus *i2c_bus, 12096a78ac0SYen Lin struct i2c_trans_info *trans, 12168049a08SStephen Warren u32 packet_id, 12268049a08SStephen Warren bool end_with_repeated_start) 12396a78ac0SYen Lin { 12496a78ac0SYen Lin u32 data; 12596a78ac0SYen Lin 12696a78ac0SYen Lin /* prepare header1: Header size = 0 Protocol = I2C, pktType = 0 */ 12796a78ac0SYen Lin data = PROTOCOL_TYPE_I2C << PKT_HDR1_PROTOCOL_SHIFT; 12896a78ac0SYen Lin data |= packet_id << PKT_HDR1_PKT_ID_SHIFT; 12996a78ac0SYen Lin data |= i2c_bus->id << PKT_HDR1_CTLR_ID_SHIFT; 13096a78ac0SYen Lin writel(data, &i2c_bus->control->tx_fifo); 13196a78ac0SYen Lin debug("pkt header 1 sent (0x%x)\n", data); 13296a78ac0SYen Lin 13396a78ac0SYen Lin /* prepare header2 */ 13496a78ac0SYen Lin data = (trans->num_bytes - 1) << PKT_HDR2_PAYLOAD_SIZE_SHIFT; 13596a78ac0SYen Lin writel(data, &i2c_bus->control->tx_fifo); 13696a78ac0SYen Lin debug("pkt header 2 sent (0x%x)\n", data); 13796a78ac0SYen Lin 13896a78ac0SYen Lin /* prepare IO specific header: configure the slave address */ 13996a78ac0SYen Lin data = trans->address << PKT_HDR3_SLAVE_ADDR_SHIFT; 14096a78ac0SYen Lin 14196a78ac0SYen Lin /* Enable Read if it is not a write transaction */ 14296a78ac0SYen Lin if (!(trans->flags & I2C_IS_WRITE)) 14396a78ac0SYen Lin data |= PKT_HDR3_READ_MODE_MASK; 14468049a08SStephen Warren if (end_with_repeated_start) 14568049a08SStephen Warren data |= PKT_HDR3_REPEAT_START_MASK; 14696a78ac0SYen Lin 14796a78ac0SYen Lin /* Write I2C specific header */ 14896a78ac0SYen Lin writel(data, &i2c_bus->control->tx_fifo); 14996a78ac0SYen Lin debug("pkt header 3 sent (0x%x)\n", data); 15096a78ac0SYen Lin } 15196a78ac0SYen Lin 15296a78ac0SYen Lin static int wait_for_tx_fifo_empty(struct i2c_control *control) 15396a78ac0SYen Lin { 15496a78ac0SYen Lin u32 count; 15596a78ac0SYen Lin int timeout_us = I2C_TIMEOUT_USEC; 15696a78ac0SYen Lin 15796a78ac0SYen Lin while (timeout_us >= 0) { 15896a78ac0SYen Lin count = (readl(&control->fifo_status) & TX_FIFO_EMPTY_CNT_MASK) 15996a78ac0SYen Lin >> TX_FIFO_EMPTY_CNT_SHIFT; 16096a78ac0SYen Lin if (count == I2C_FIFO_DEPTH) 16196a78ac0SYen Lin return 1; 16296a78ac0SYen Lin udelay(10); 16396a78ac0SYen Lin timeout_us -= 10; 16496a78ac0SYen Lin } 16596a78ac0SYen Lin 16696a78ac0SYen Lin return 0; 16796a78ac0SYen Lin } 16896a78ac0SYen Lin 16996a78ac0SYen Lin static int wait_for_rx_fifo_notempty(struct i2c_control *control) 17096a78ac0SYen Lin { 17196a78ac0SYen Lin u32 count; 17296a78ac0SYen Lin int timeout_us = I2C_TIMEOUT_USEC; 17396a78ac0SYen Lin 17496a78ac0SYen Lin while (timeout_us >= 0) { 17596a78ac0SYen Lin count = (readl(&control->fifo_status) & TX_FIFO_FULL_CNT_MASK) 17696a78ac0SYen Lin >> TX_FIFO_FULL_CNT_SHIFT; 17796a78ac0SYen Lin if (count) 17896a78ac0SYen Lin return 1; 17996a78ac0SYen Lin udelay(10); 18096a78ac0SYen Lin timeout_us -= 10; 18196a78ac0SYen Lin } 18296a78ac0SYen Lin 18396a78ac0SYen Lin return 0; 18496a78ac0SYen Lin } 18596a78ac0SYen Lin 18696a78ac0SYen Lin static int wait_for_transfer_complete(struct i2c_control *control) 18796a78ac0SYen Lin { 18896a78ac0SYen Lin int int_status; 18996a78ac0SYen Lin int timeout_us = I2C_TIMEOUT_USEC; 19096a78ac0SYen Lin 19196a78ac0SYen Lin while (timeout_us >= 0) { 19296a78ac0SYen Lin int_status = readl(&control->int_status); 19396a78ac0SYen Lin if (int_status & I2C_INT_NO_ACK_MASK) 19496a78ac0SYen Lin return -int_status; 19596a78ac0SYen Lin if (int_status & I2C_INT_ARBITRATION_LOST_MASK) 19696a78ac0SYen Lin return -int_status; 19796a78ac0SYen Lin if (int_status & I2C_INT_XFER_COMPLETE_MASK) 19896a78ac0SYen Lin return 0; 19996a78ac0SYen Lin 20096a78ac0SYen Lin udelay(10); 20196a78ac0SYen Lin timeout_us -= 10; 20296a78ac0SYen Lin } 20396a78ac0SYen Lin 20496a78ac0SYen Lin return -1; 20596a78ac0SYen Lin } 20696a78ac0SYen Lin 20796a78ac0SYen Lin static int send_recv_packets(struct i2c_bus *i2c_bus, 20896a78ac0SYen Lin struct i2c_trans_info *trans) 20996a78ac0SYen Lin { 21096a78ac0SYen Lin struct i2c_control *control = i2c_bus->control; 21196a78ac0SYen Lin u32 int_status; 21296a78ac0SYen Lin u32 words; 21396a78ac0SYen Lin u8 *dptr; 21496a78ac0SYen Lin u32 local; 21596a78ac0SYen Lin uchar last_bytes; 21696a78ac0SYen Lin int error = 0; 21796a78ac0SYen Lin int is_write = trans->flags & I2C_IS_WRITE; 21896a78ac0SYen Lin 21996a78ac0SYen Lin /* clear status from previous transaction, XFER_COMPLETE, NOACK, etc. */ 22096a78ac0SYen Lin int_status = readl(&control->int_status); 22196a78ac0SYen Lin writel(int_status, &control->int_status); 22296a78ac0SYen Lin 22368049a08SStephen Warren send_packet_headers(i2c_bus, trans, 1, 22468049a08SStephen Warren trans->flags & I2C_USE_REPEATED_START); 22596a78ac0SYen Lin 22696a78ac0SYen Lin words = DIV_ROUND_UP(trans->num_bytes, 4); 22796a78ac0SYen Lin last_bytes = trans->num_bytes & 3; 22896a78ac0SYen Lin dptr = trans->buf; 22996a78ac0SYen Lin 23096a78ac0SYen Lin while (words) { 23196a78ac0SYen Lin u32 *wptr = (u32 *)dptr; 23296a78ac0SYen Lin 23396a78ac0SYen Lin if (is_write) { 23496a78ac0SYen Lin /* deal with word alignment */ 235981b14f0SStephen Warren if ((words == 1) && last_bytes) { 236981b14f0SStephen Warren local = 0; 237981b14f0SStephen Warren memcpy(&local, dptr, last_bytes); 2388e67c5d0SThierry Reding } else if ((unsigned long)dptr & 3) { 23996a78ac0SYen Lin memcpy(&local, dptr, sizeof(u32)); 240981b14f0SStephen Warren } else { 241981b14f0SStephen Warren local = *wptr; 242981b14f0SStephen Warren } 24396a78ac0SYen Lin writel(local, &control->tx_fifo); 24496a78ac0SYen Lin debug("pkt data sent (0x%x)\n", local); 24596a78ac0SYen Lin if (!wait_for_tx_fifo_empty(control)) { 24696a78ac0SYen Lin error = -1; 24796a78ac0SYen Lin goto exit; 24896a78ac0SYen Lin } 24996a78ac0SYen Lin } else { 25096a78ac0SYen Lin if (!wait_for_rx_fifo_notempty(control)) { 25196a78ac0SYen Lin error = -1; 25296a78ac0SYen Lin goto exit; 25396a78ac0SYen Lin } 25496a78ac0SYen Lin /* 25596a78ac0SYen Lin * for the last word, we read into our local buffer, 25696a78ac0SYen Lin * in case that caller did not provide enough buffer. 25796a78ac0SYen Lin */ 25896a78ac0SYen Lin local = readl(&control->rx_fifo); 25996a78ac0SYen Lin if ((words == 1) && last_bytes) 26096a78ac0SYen Lin memcpy(dptr, (char *)&local, last_bytes); 2618e67c5d0SThierry Reding else if ((unsigned long)dptr & 3) 26296a78ac0SYen Lin memcpy(dptr, &local, sizeof(u32)); 26396a78ac0SYen Lin else 26496a78ac0SYen Lin *wptr = local; 26596a78ac0SYen Lin debug("pkt data received (0x%x)\n", local); 26696a78ac0SYen Lin } 26796a78ac0SYen Lin words--; 26896a78ac0SYen Lin dptr += sizeof(u32); 26996a78ac0SYen Lin } 27096a78ac0SYen Lin 27196a78ac0SYen Lin if (wait_for_transfer_complete(control)) { 27296a78ac0SYen Lin error = -1; 27396a78ac0SYen Lin goto exit; 27496a78ac0SYen Lin } 27596a78ac0SYen Lin return 0; 27696a78ac0SYen Lin exit: 27796a78ac0SYen Lin /* error, reset the controller. */ 27896a78ac0SYen Lin i2c_reset_controller(i2c_bus); 27996a78ac0SYen Lin 28096a78ac0SYen Lin return error; 28196a78ac0SYen Lin } 28296a78ac0SYen Lin 283b0e6ef46SSimon Glass static int tegra_i2c_write_data(struct i2c_bus *i2c_bus, u32 addr, u8 *data, 28468049a08SStephen Warren u32 len, bool end_with_repeated_start) 28596a78ac0SYen Lin { 28696a78ac0SYen Lin int error; 28796a78ac0SYen Lin struct i2c_trans_info trans_info; 28896a78ac0SYen Lin 28996a78ac0SYen Lin trans_info.address = addr; 29096a78ac0SYen Lin trans_info.buf = data; 29196a78ac0SYen Lin trans_info.flags = I2C_IS_WRITE; 29268049a08SStephen Warren if (end_with_repeated_start) 29368049a08SStephen Warren trans_info.flags |= I2C_USE_REPEATED_START; 29496a78ac0SYen Lin trans_info.num_bytes = len; 29596a78ac0SYen Lin trans_info.is_10bit_address = 0; 29696a78ac0SYen Lin 297b0e6ef46SSimon Glass error = send_recv_packets(i2c_bus, &trans_info); 29896a78ac0SYen Lin if (error) 29929f3e3f2STom Warren debug("tegra_i2c_write_data: Error (%d) !!!\n", error); 30096a78ac0SYen Lin 30196a78ac0SYen Lin return error; 30296a78ac0SYen Lin } 30396a78ac0SYen Lin 304b0e6ef46SSimon Glass static int tegra_i2c_read_data(struct i2c_bus *i2c_bus, u32 addr, u8 *data, 305d84eb856SSimon Glass u32 len) 30696a78ac0SYen Lin { 30796a78ac0SYen Lin int error; 30896a78ac0SYen Lin struct i2c_trans_info trans_info; 30996a78ac0SYen Lin 31096a78ac0SYen Lin trans_info.address = addr | 1; 31196a78ac0SYen Lin trans_info.buf = data; 31296a78ac0SYen Lin trans_info.flags = 0; 31396a78ac0SYen Lin trans_info.num_bytes = len; 31496a78ac0SYen Lin trans_info.is_10bit_address = 0; 31596a78ac0SYen Lin 316b0e6ef46SSimon Glass error = send_recv_packets(i2c_bus, &trans_info); 31796a78ac0SYen Lin if (error) 31829f3e3f2STom Warren debug("tegra_i2c_read_data: Error (%d) !!!\n", error); 31996a78ac0SYen Lin 32096a78ac0SYen Lin return error; 32196a78ac0SYen Lin } 32296a78ac0SYen Lin 323b0e6ef46SSimon Glass static int tegra_i2c_set_bus_speed(struct udevice *dev, unsigned int speed) 32496a78ac0SYen Lin { 325b0e6ef46SSimon Glass struct i2c_bus *i2c_bus = dev_get_priv(dev); 326d84eb856SSimon Glass 327b0e6ef46SSimon Glass i2c_bus->speed = speed; 328b0e6ef46SSimon Glass i2c_init_controller(i2c_bus); 32996a78ac0SYen Lin 33096a78ac0SYen Lin return 0; 33196a78ac0SYen Lin } 33296a78ac0SYen Lin 333b0e6ef46SSimon Glass static int tegra_i2c_probe(struct udevice *dev) 33496a78ac0SYen Lin { 335b0e6ef46SSimon Glass struct i2c_bus *i2c_bus = dev_get_priv(dev); 336b0e6ef46SSimon Glass const void *blob = gd->fdt_blob; 337b0e6ef46SSimon Glass int node = dev->of_offset; 338b0e6ef46SSimon Glass bool is_dvc; 339b0e6ef46SSimon Glass 340b0e6ef46SSimon Glass i2c_bus->id = dev->seq; 34139de8433SSimon Glass i2c_bus->type = dev_get_driver_data(dev); 342*4e9838c1SSimon Glass i2c_bus->regs = (struct i2c_ctlr *)dev_get_addr(dev); 34396a78ac0SYen Lin 34496a78ac0SYen Lin /* 34596a78ac0SYen Lin * We don't have a binding for pinmux yet. Leave it out for now. So 34696a78ac0SYen Lin * far no one needs anything other than the default. 34796a78ac0SYen Lin */ 34896a78ac0SYen Lin i2c_bus->pinmux_config = FUNCMUX_DEFAULT; 34996a78ac0SYen Lin i2c_bus->periph_id = clock_decode_periph_id(blob, node); 35096a78ac0SYen Lin 35196a78ac0SYen Lin /* 35296a78ac0SYen Lin * We can't specify the pinmux config in the fdt, so I2C2 will not 35396a78ac0SYen Lin * work on Seaboard. It normally has no devices on it anyway. 35496a78ac0SYen Lin * You could add in this little hack if you need to use it. 35596a78ac0SYen Lin * The correct solution is a pinmux binding in the fdt. 35696a78ac0SYen Lin * 35796a78ac0SYen Lin * if (i2c_bus->periph_id == PERIPH_ID_I2C2) 35896a78ac0SYen Lin * i2c_bus->pinmux_config = FUNCMUX_I2C2_PTA; 35996a78ac0SYen Lin */ 36096a78ac0SYen Lin if (i2c_bus->periph_id == -1) 361b0e6ef46SSimon Glass return -EINVAL; 36296a78ac0SYen Lin 36339de8433SSimon Glass is_dvc = dev_get_driver_data(dev) == TYPE_DVC; 36496a78ac0SYen Lin if (is_dvc) { 36596a78ac0SYen Lin i2c_bus->control = 36696a78ac0SYen Lin &((struct dvc_ctlr *)i2c_bus->regs)->control; 36796a78ac0SYen Lin } else { 36896a78ac0SYen Lin i2c_bus->control = &i2c_bus->regs->control; 36996a78ac0SYen Lin } 37096a78ac0SYen Lin i2c_init_controller(i2c_bus); 371b0e6ef46SSimon Glass debug("%s: controller bus %d at %p, periph_id %d, speed %d: ", 372b0e6ef46SSimon Glass is_dvc ? "dvc" : "i2c", dev->seq, i2c_bus->regs, 373b0e6ef46SSimon Glass i2c_bus->periph_id, i2c_bus->speed); 37496a78ac0SYen Lin 37596a78ac0SYen Lin return 0; 37696a78ac0SYen Lin } 37796a78ac0SYen Lin 37896a78ac0SYen Lin /* i2c write version without the register address */ 379b0e6ef46SSimon Glass static int i2c_write_data(struct i2c_bus *i2c_bus, uchar chip, uchar *buffer, 38019d7bf3dSJeroen Hofstee int len, bool end_with_repeated_start) 38196a78ac0SYen Lin { 38296a78ac0SYen Lin int rc; 38396a78ac0SYen Lin 38496a78ac0SYen Lin debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len); 38596a78ac0SYen Lin debug("write_data: "); 38696a78ac0SYen Lin /* use rc for counter */ 38796a78ac0SYen Lin for (rc = 0; rc < len; ++rc) 38896a78ac0SYen Lin debug(" 0x%02x", buffer[rc]); 38996a78ac0SYen Lin debug("\n"); 39096a78ac0SYen Lin 39196a78ac0SYen Lin /* Shift 7-bit address over for lower-level i2c functions */ 392b0e6ef46SSimon Glass rc = tegra_i2c_write_data(i2c_bus, chip << 1, buffer, len, 39368049a08SStephen Warren end_with_repeated_start); 39496a78ac0SYen Lin if (rc) 39596a78ac0SYen Lin debug("i2c_write_data(): rc=%d\n", rc); 39696a78ac0SYen Lin 39796a78ac0SYen Lin return rc; 39896a78ac0SYen Lin } 39996a78ac0SYen Lin 40096a78ac0SYen Lin /* i2c read version without the register address */ 401b0e6ef46SSimon Glass static int i2c_read_data(struct i2c_bus *i2c_bus, uchar chip, uchar *buffer, 40219d7bf3dSJeroen Hofstee int len) 40396a78ac0SYen Lin { 40496a78ac0SYen Lin int rc; 40596a78ac0SYen Lin 40696a78ac0SYen Lin debug("inside i2c_read_data():\n"); 40796a78ac0SYen Lin /* Shift 7-bit address over for lower-level i2c functions */ 408b0e6ef46SSimon Glass rc = tegra_i2c_read_data(i2c_bus, chip << 1, buffer, len); 40996a78ac0SYen Lin if (rc) { 41096a78ac0SYen Lin debug("i2c_read_data(): rc=%d\n", rc); 41196a78ac0SYen Lin return rc; 41296a78ac0SYen Lin } 41396a78ac0SYen Lin 41496a78ac0SYen Lin debug("i2c_read_data: "); 41596a78ac0SYen Lin /* reuse rc for counter*/ 41696a78ac0SYen Lin for (rc = 0; rc < len; ++rc) 41796a78ac0SYen Lin debug(" 0x%02x", buffer[rc]); 41896a78ac0SYen Lin debug("\n"); 41996a78ac0SYen Lin 42096a78ac0SYen Lin return 0; 42196a78ac0SYen Lin } 42296a78ac0SYen Lin 42396a78ac0SYen Lin /* Probe to see if a chip is present. */ 424b0e6ef46SSimon Glass static int tegra_i2c_probe_chip(struct udevice *bus, uint chip_addr, 425b0e6ef46SSimon Glass uint chip_flags) 42696a78ac0SYen Lin { 427b0e6ef46SSimon Glass struct i2c_bus *i2c_bus = dev_get_priv(bus); 42896a78ac0SYen Lin int rc; 429b0e6ef46SSimon Glass u8 reg; 43096a78ac0SYen Lin 431b0e6ef46SSimon Glass /* Shift 7-bit address over for lower-level i2c functions */ 432b0e6ef46SSimon Glass rc = tegra_i2c_write_data(i2c_bus, chip_addr << 1, ®, sizeof(reg), 433b0e6ef46SSimon Glass false); 434b0e6ef46SSimon Glass 435b0e6ef46SSimon Glass return rc; 43696a78ac0SYen Lin } 43796a78ac0SYen Lin 438b0e6ef46SSimon Glass static int tegra_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, 439b0e6ef46SSimon Glass int nmsgs) 44096a78ac0SYen Lin { 441b0e6ef46SSimon Glass struct i2c_bus *i2c_bus = dev_get_priv(bus); 442b0e6ef46SSimon Glass int ret; 44396a78ac0SYen Lin 444b0e6ef46SSimon Glass debug("i2c_xfer: %d messages\n", nmsgs); 445b0e6ef46SSimon Glass for (; nmsgs > 0; nmsgs--, msg++) { 446b0e6ef46SSimon Glass bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD); 44796a78ac0SYen Lin 448b0e6ef46SSimon Glass debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len); 449b0e6ef46SSimon Glass if (msg->flags & I2C_M_RD) { 450b0e6ef46SSimon Glass ret = i2c_read_data(i2c_bus, msg->addr, msg->buf, 451b0e6ef46SSimon Glass msg->len); 452b0e6ef46SSimon Glass } else { 453b0e6ef46SSimon Glass ret = i2c_write_data(i2c_bus, msg->addr, msg->buf, 454b0e6ef46SSimon Glass msg->len, next_is_read); 45596a78ac0SYen Lin } 456b0e6ef46SSimon Glass if (ret) { 457b0e6ef46SSimon Glass debug("i2c_write: error sending\n"); 458b0e6ef46SSimon Glass return -EREMOTEIO; 45996a78ac0SYen Lin } 46096a78ac0SYen Lin } 46196a78ac0SYen Lin 46296a78ac0SYen Lin return 0; 46396a78ac0SYen Lin } 46496a78ac0SYen Lin 465b0e6ef46SSimon Glass int tegra_i2c_get_dvc_bus(struct udevice **busp) 46696a78ac0SYen Lin { 467b0e6ef46SSimon Glass struct udevice *bus; 46896a78ac0SYen Lin 469b0e6ef46SSimon Glass for (uclass_first_device(UCLASS_I2C, &bus); 470b0e6ef46SSimon Glass bus; 471b0e6ef46SSimon Glass uclass_next_device(&bus)) { 47239de8433SSimon Glass if (dev_get_driver_data(bus) == TYPE_DVC) { 473b0e6ef46SSimon Glass *busp = bus; 474b0e6ef46SSimon Glass return 0; 47596a78ac0SYen Lin } 47696a78ac0SYen Lin } 47796a78ac0SYen Lin 478b0e6ef46SSimon Glass return -ENODEV; 479b0e6ef46SSimon Glass } 480b0e6ef46SSimon Glass 481b0e6ef46SSimon Glass static const struct dm_i2c_ops tegra_i2c_ops = { 482b0e6ef46SSimon Glass .xfer = tegra_i2c_xfer, 483b0e6ef46SSimon Glass .probe_chip = tegra_i2c_probe_chip, 484b0e6ef46SSimon Glass .set_bus_speed = tegra_i2c_set_bus_speed, 485b0e6ef46SSimon Glass }; 486b0e6ef46SSimon Glass 487b0e6ef46SSimon Glass static const struct udevice_id tegra_i2c_ids[] = { 488b0e6ef46SSimon Glass { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 }, 489b0e6ef46SSimon Glass { .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD }, 490b0e6ef46SSimon Glass { .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC }, 491b0e6ef46SSimon Glass { } 492b0e6ef46SSimon Glass }; 493e31c1e50SSimon Glass 494b0e6ef46SSimon Glass U_BOOT_DRIVER(i2c_tegra) = { 495b0e6ef46SSimon Glass .name = "i2c_tegra", 496b0e6ef46SSimon Glass .id = UCLASS_I2C, 497b0e6ef46SSimon Glass .of_match = tegra_i2c_ids, 498b0e6ef46SSimon Glass .probe = tegra_i2c_probe, 499b0e6ef46SSimon Glass .priv_auto_alloc_size = sizeof(struct i2c_bus), 500b0e6ef46SSimon Glass .ops = &tegra_i2c_ops, 501b0e6ef46SSimon Glass }; 502