blob: 31ba263b72952177679db7650126616ac207ad2c [file] [log] [blame]
Yen Lin96a78ac2012-03-06 19:00:23 +00001/*
2 * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3 * Copyright (c) 2010-2011 NVIDIA Corporation
4 * NVIDIA Corporation <www.nvidia.com>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Yen Lin96a78ac2012-03-06 19:00:23 +00007 */
8
9#include <common.h>
Simon Glassb0e6ef42014-12-10 08:55:57 -070010#include <dm.h>
11#include <errno.h>
Yen Lin96a78ac2012-03-06 19:00:23 +000012#include <fdtdec.h>
13#include <i2c.h>
14#include <asm/io.h>
Bryan Wu3c27fa22016-08-05 16:10:35 -060015#ifdef CONFIG_TEGRA186
16#include <clk.h>
17#include <reset.h>
18#else
Yen Lin96a78ac2012-03-06 19:00:23 +000019#include <asm/arch/clock.h>
20#include <asm/arch/funcmux.h>
Yen Lin96a78ac2012-03-06 19:00:23 +000021#include <asm/arch/pinmux.h>
Tom Warren150c2492012-09-19 15:50:56 -070022#include <asm/arch-tegra/clk_rst.h>
Bryan Wu3c27fa22016-08-05 16:10:35 -060023#endif
24#include <asm/arch/gpio.h>
Tom Warren150c2492012-09-19 15:50:56 -070025#include <asm/arch-tegra/tegra_i2c.h>
Yen Lin96a78ac2012-03-06 19:00:23 +000026
Bryan Wu3c27fa22016-08-05 16:10:35 -060027/*
28 * FIXME: TODO: This driver contains a number of ifdef CONFIG_TEGRA186 that
29 * should not be present. These are needed because newer Tegra SoCs support
30 * only the standard clock/reset APIs, whereas older Tegra SoCs support only
31 * a custom Tegra-specific API. ASAP the older Tegra SoCs' code should be
32 * fixed to implement the standard APIs, and all drivers converted to solely
33 * use the new standard APIs, with no ifdefs.
34 */
35
Yen Lin96a78ac2012-03-06 19:00:23 +000036DECLARE_GLOBAL_DATA_PTR;
37
Simon Glassb0e6ef42014-12-10 08:55:57 -070038enum i2c_type {
39 TYPE_114,
40 TYPE_STD,
41 TYPE_DVC,
42};
43
Yen Lin96a78ac2012-03-06 19:00:23 +000044/* Information about i2c controller */
45struct i2c_bus {
46 int id;
Bryan Wu3c27fa22016-08-05 16:10:35 -060047#ifdef CONFIG_TEGRA186
48 struct reset_ctl reset_ctl;
49 struct clk clk;
50#else
Yen Lin96a78ac2012-03-06 19:00:23 +000051 enum periph_id periph_id;
Bryan Wu3c27fa22016-08-05 16:10:35 -060052#endif
Yen Lin96a78ac2012-03-06 19:00:23 +000053 int speed;
54 int pinmux_config;
55 struct i2c_control *control;
56 struct i2c_ctlr *regs;
Simon Glassb0e6ef42014-12-10 08:55:57 -070057 enum i2c_type type;
Yen Lin96a78ac2012-03-06 19:00:23 +000058 int inited; /* bus is inited */
59};
60
Yen Lin96a78ac2012-03-06 19:00:23 +000061static void set_packet_mode(struct i2c_bus *i2c_bus)
62{
63 u32 config;
64
65 config = I2C_CNFG_NEW_MASTER_FSM_MASK | I2C_CNFG_PACKET_MODE_MASK;
66
Simon Glassb0e6ef42014-12-10 08:55:57 -070067 if (i2c_bus->type == TYPE_DVC) {
Yen Lin96a78ac2012-03-06 19:00:23 +000068 struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
69
70 writel(config, &dvc->cnfg);
71 } else {
72 writel(config, &i2c_bus->regs->cnfg);
73 /*
74 * program I2C_SL_CNFG.NEWSL to ENABLE. This fixes probe
75 * issues, i.e., some slaves may be wrongly detected.
76 */
77 setbits_le32(&i2c_bus->regs->sl_cnfg, I2C_SL_CNFG_NEWSL_MASK);
78 }
79}
80
81static void i2c_reset_controller(struct i2c_bus *i2c_bus)
82{
83 /* Reset I2C controller. */
Bryan Wu3c27fa22016-08-05 16:10:35 -060084#ifdef CONFIG_TEGRA186
85 reset_assert(&i2c_bus->reset_ctl);
86 udelay(1);
87 reset_deassert(&i2c_bus->reset_ctl);
88 udelay(1);
89#else
Yen Lin96a78ac2012-03-06 19:00:23 +000090 reset_periph(i2c_bus->periph_id, 1);
Bryan Wu3c27fa22016-08-05 16:10:35 -060091#endif
Yen Lin96a78ac2012-03-06 19:00:23 +000092
93 /* re-program config register to packet mode */
94 set_packet_mode(i2c_bus);
95}
96
Bryan Wu3c27fa22016-08-05 16:10:35 -060097#ifdef CONFIG_TEGRA186
98static int i2c_init_clock(struct i2c_bus *i2c_bus, unsigned rate)
99{
100 int ret;
101
102 ret = reset_assert(&i2c_bus->reset_ctl);
103 if (ret)
104 return ret;
105 ret = clk_enable(&i2c_bus->clk);
106 if (ret)
107 return ret;
108 ret = clk_set_rate(&i2c_bus->clk, rate);
109 if (IS_ERR_VALUE(ret))
110 return ret;
111 ret = reset_deassert(&i2c_bus->reset_ctl);
112 if (ret)
113 return ret;
114
115 return 0;
116}
117#endif
118
Yen Lin96a78ac2012-03-06 19:00:23 +0000119static void i2c_init_controller(struct i2c_bus *i2c_bus)
120{
Simon Glassb0e6ef42014-12-10 08:55:57 -0700121 if (!i2c_bus->speed)
122 return;
123 debug("%s: speed=%d\n", __func__, i2c_bus->speed);
Yen Lin96a78ac2012-03-06 19:00:23 +0000124 /*
125 * Use PLLP - DP-04508-001_v06 datasheet indicates a divisor of 8
126 * here, in section 23.3.1, but in fact we seem to need a factor of
127 * 16 to get the right frequency.
128 */
Bryan Wu3c27fa22016-08-05 16:10:35 -0600129#ifdef CONFIG_TEGRA186
130 i2c_init_clock(i2c_bus, i2c_bus->speed * 2 * 8);
131#else
Yen Lin96a78ac2012-03-06 19:00:23 +0000132 clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
Tom Warrene32624e2013-02-08 07:25:30 +0000133 i2c_bus->speed * 2 * 8);
Bryan Wu3c27fa22016-08-05 16:10:35 -0600134#endif
Tom Warrene32624e2013-02-08 07:25:30 +0000135
Simon Glassb0e6ef42014-12-10 08:55:57 -0700136 if (i2c_bus->type == TYPE_114) {
Tom Warrene32624e2013-02-08 07:25:30 +0000137 /*
138 * T114 I2C went to a single clock source for standard/fast and
139 * HS clock speeds. The new clock rate setting calculation is:
140 * SCL = CLK_SOURCE.I2C /
141 * (CLK_MULT_STD_FAST_MODE * (I2C_CLK_DIV_STD_FAST_MODE+1) *
142 * I2C FREQUENCY DIVISOR) as per the T114 TRM (sec 30.3.1).
143 *
144 * NOTE: We do this here, after the initial clock/pll start,
145 * because if we read the clk_div reg before the controller
146 * is running, we hang, and we need it for the new calc.
147 */
148 int clk_div_stdfst_mode = readl(&i2c_bus->regs->clk_div) >> 16;
Bryan Wu3c27fa22016-08-05 16:10:35 -0600149 unsigned rate = CLK_MULT_STD_FAST_MODE *
150 (clk_div_stdfst_mode + 1) * i2c_bus->speed * 2;
Tom Warrene32624e2013-02-08 07:25:30 +0000151 debug("%s: CLK_DIV_STD_FAST_MODE setting = %d\n", __func__,
152 clk_div_stdfst_mode);
153
Bryan Wu3c27fa22016-08-05 16:10:35 -0600154#ifdef CONFIG_TEGRA186
155 i2c_init_clock(i2c_bus, rate);
156#else
Tom Warrene32624e2013-02-08 07:25:30 +0000157 clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
Bryan Wu3c27fa22016-08-05 16:10:35 -0600158 rate);
159#endif
Tom Warrene32624e2013-02-08 07:25:30 +0000160 }
Yen Lin96a78ac2012-03-06 19:00:23 +0000161
162 /* Reset I2C controller. */
163 i2c_reset_controller(i2c_bus);
164
165 /* Configure I2C controller. */
Simon Glassb0e6ef42014-12-10 08:55:57 -0700166 if (i2c_bus->type == TYPE_DVC) { /* only for DVC I2C */
Yen Lin96a78ac2012-03-06 19:00:23 +0000167 struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
168
169 setbits_le32(&dvc->ctrl3, DVC_CTRL_REG3_I2C_HW_SW_PROG_MASK);
170 }
171
Bryan Wu3c27fa22016-08-05 16:10:35 -0600172#ifndef CONFIG_TEGRA186
Yen Lin96a78ac2012-03-06 19:00:23 +0000173 funcmux_select(i2c_bus->periph_id, i2c_bus->pinmux_config);
Bryan Wu3c27fa22016-08-05 16:10:35 -0600174#endif
Yen Lin96a78ac2012-03-06 19:00:23 +0000175}
176
177static void send_packet_headers(
178 struct i2c_bus *i2c_bus,
179 struct i2c_trans_info *trans,
Stephen Warren68049a02014-06-25 10:57:27 -0600180 u32 packet_id,
181 bool end_with_repeated_start)
Yen Lin96a78ac2012-03-06 19:00:23 +0000182{
183 u32 data;
184
185 /* prepare header1: Header size = 0 Protocol = I2C, pktType = 0 */
186 data = PROTOCOL_TYPE_I2C << PKT_HDR1_PROTOCOL_SHIFT;
187 data |= packet_id << PKT_HDR1_PKT_ID_SHIFT;
188 data |= i2c_bus->id << PKT_HDR1_CTLR_ID_SHIFT;
189 writel(data, &i2c_bus->control->tx_fifo);
190 debug("pkt header 1 sent (0x%x)\n", data);
191
192 /* prepare header2 */
193 data = (trans->num_bytes - 1) << PKT_HDR2_PAYLOAD_SIZE_SHIFT;
194 writel(data, &i2c_bus->control->tx_fifo);
195 debug("pkt header 2 sent (0x%x)\n", data);
196
197 /* prepare IO specific header: configure the slave address */
198 data = trans->address << PKT_HDR3_SLAVE_ADDR_SHIFT;
199
200 /* Enable Read if it is not a write transaction */
201 if (!(trans->flags & I2C_IS_WRITE))
202 data |= PKT_HDR3_READ_MODE_MASK;
Stephen Warren68049a02014-06-25 10:57:27 -0600203 if (end_with_repeated_start)
204 data |= PKT_HDR3_REPEAT_START_MASK;
Yen Lin96a78ac2012-03-06 19:00:23 +0000205
206 /* Write I2C specific header */
207 writel(data, &i2c_bus->control->tx_fifo);
208 debug("pkt header 3 sent (0x%x)\n", data);
209}
210
211static int wait_for_tx_fifo_empty(struct i2c_control *control)
212{
213 u32 count;
214 int timeout_us = I2C_TIMEOUT_USEC;
215
216 while (timeout_us >= 0) {
217 count = (readl(&control->fifo_status) & TX_FIFO_EMPTY_CNT_MASK)
218 >> TX_FIFO_EMPTY_CNT_SHIFT;
219 if (count == I2C_FIFO_DEPTH)
220 return 1;
221 udelay(10);
222 timeout_us -= 10;
223 }
224
225 return 0;
226}
227
228static int wait_for_rx_fifo_notempty(struct i2c_control *control)
229{
230 u32 count;
231 int timeout_us = I2C_TIMEOUT_USEC;
232
233 while (timeout_us >= 0) {
234 count = (readl(&control->fifo_status) & TX_FIFO_FULL_CNT_MASK)
235 >> TX_FIFO_FULL_CNT_SHIFT;
236 if (count)
237 return 1;
238 udelay(10);
239 timeout_us -= 10;
240 }
241
242 return 0;
243}
244
245static int wait_for_transfer_complete(struct i2c_control *control)
246{
247 int int_status;
248 int timeout_us = I2C_TIMEOUT_USEC;
249
250 while (timeout_us >= 0) {
251 int_status = readl(&control->int_status);
252 if (int_status & I2C_INT_NO_ACK_MASK)
253 return -int_status;
254 if (int_status & I2C_INT_ARBITRATION_LOST_MASK)
255 return -int_status;
256 if (int_status & I2C_INT_XFER_COMPLETE_MASK)
257 return 0;
258
259 udelay(10);
260 timeout_us -= 10;
261 }
262
263 return -1;
264}
265
266static int send_recv_packets(struct i2c_bus *i2c_bus,
267 struct i2c_trans_info *trans)
268{
269 struct i2c_control *control = i2c_bus->control;
270 u32 int_status;
271 u32 words;
272 u8 *dptr;
273 u32 local;
274 uchar last_bytes;
275 int error = 0;
276 int is_write = trans->flags & I2C_IS_WRITE;
277
278 /* clear status from previous transaction, XFER_COMPLETE, NOACK, etc. */
279 int_status = readl(&control->int_status);
280 writel(int_status, &control->int_status);
281
Stephen Warren68049a02014-06-25 10:57:27 -0600282 send_packet_headers(i2c_bus, trans, 1,
283 trans->flags & I2C_USE_REPEATED_START);
Yen Lin96a78ac2012-03-06 19:00:23 +0000284
285 words = DIV_ROUND_UP(trans->num_bytes, 4);
286 last_bytes = trans->num_bytes & 3;
287 dptr = trans->buf;
288
289 while (words) {
290 u32 *wptr = (u32 *)dptr;
291
292 if (is_write) {
293 /* deal with word alignment */
Stephen Warren981b14f2014-06-25 10:57:28 -0600294 if ((words == 1) && last_bytes) {
295 local = 0;
296 memcpy(&local, dptr, last_bytes);
Thierry Reding8e67c5d2015-07-22 15:33:22 -0600297 } else if ((unsigned long)dptr & 3) {
Yen Lin96a78ac2012-03-06 19:00:23 +0000298 memcpy(&local, dptr, sizeof(u32));
Yen Lin96a78ac2012-03-06 19:00:23 +0000299 } else {
Stephen Warren981b14f2014-06-25 10:57:28 -0600300 local = *wptr;
Yen Lin96a78ac2012-03-06 19:00:23 +0000301 }
Stephen Warren981b14f2014-06-25 10:57:28 -0600302 writel(local, &control->tx_fifo);
303 debug("pkt data sent (0x%x)\n", local);
Yen Lin96a78ac2012-03-06 19:00:23 +0000304 if (!wait_for_tx_fifo_empty(control)) {
305 error = -1;
306 goto exit;
307 }
308 } else {
309 if (!wait_for_rx_fifo_notempty(control)) {
310 error = -1;
311 goto exit;
312 }
313 /*
314 * for the last word, we read into our local buffer,
315 * in case that caller did not provide enough buffer.
316 */
317 local = readl(&control->rx_fifo);
318 if ((words == 1) && last_bytes)
319 memcpy(dptr, (char *)&local, last_bytes);
Thierry Reding8e67c5d2015-07-22 15:33:22 -0600320 else if ((unsigned long)dptr & 3)
Yen Lin96a78ac2012-03-06 19:00:23 +0000321 memcpy(dptr, &local, sizeof(u32));
322 else
323 *wptr = local;
324 debug("pkt data received (0x%x)\n", local);
325 }
326 words--;
327 dptr += sizeof(u32);
328 }
329
330 if (wait_for_transfer_complete(control)) {
331 error = -1;
332 goto exit;
333 }
334 return 0;
335exit:
336 /* error, reset the controller. */
337 i2c_reset_controller(i2c_bus);
338
339 return error;
340}
341
Simon Glassb0e6ef42014-12-10 08:55:57 -0700342static int tegra_i2c_write_data(struct i2c_bus *i2c_bus, u32 addr, u8 *data,
Stephen Warren68049a02014-06-25 10:57:27 -0600343 u32 len, bool end_with_repeated_start)
Yen Lin96a78ac2012-03-06 19:00:23 +0000344{
345 int error;
346 struct i2c_trans_info trans_info;
347
348 trans_info.address = addr;
349 trans_info.buf = data;
350 trans_info.flags = I2C_IS_WRITE;
Stephen Warren68049a02014-06-25 10:57:27 -0600351 if (end_with_repeated_start)
352 trans_info.flags |= I2C_USE_REPEATED_START;
Yen Lin96a78ac2012-03-06 19:00:23 +0000353 trans_info.num_bytes = len;
354 trans_info.is_10bit_address = 0;
355
Simon Glassb0e6ef42014-12-10 08:55:57 -0700356 error = send_recv_packets(i2c_bus, &trans_info);
Yen Lin96a78ac2012-03-06 19:00:23 +0000357 if (error)
Tom Warren29f3e3f2012-09-04 17:00:24 -0700358 debug("tegra_i2c_write_data: Error (%d) !!!\n", error);
Yen Lin96a78ac2012-03-06 19:00:23 +0000359
360 return error;
361}
362
Simon Glassb0e6ef42014-12-10 08:55:57 -0700363static int tegra_i2c_read_data(struct i2c_bus *i2c_bus, u32 addr, u8 *data,
Simon Glassd84eb852012-10-30 07:28:52 +0000364 u32 len)
Yen Lin96a78ac2012-03-06 19:00:23 +0000365{
366 int error;
367 struct i2c_trans_info trans_info;
368
369 trans_info.address = addr | 1;
370 trans_info.buf = data;
371 trans_info.flags = 0;
372 trans_info.num_bytes = len;
373 trans_info.is_10bit_address = 0;
374
Simon Glassb0e6ef42014-12-10 08:55:57 -0700375 error = send_recv_packets(i2c_bus, &trans_info);
Yen Lin96a78ac2012-03-06 19:00:23 +0000376 if (error)
Tom Warren29f3e3f2012-09-04 17:00:24 -0700377 debug("tegra_i2c_read_data: Error (%d) !!!\n", error);
Yen Lin96a78ac2012-03-06 19:00:23 +0000378
379 return error;
380}
381
Simon Glassb0e6ef42014-12-10 08:55:57 -0700382static int tegra_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
Yen Lin96a78ac2012-03-06 19:00:23 +0000383{
Simon Glassb0e6ef42014-12-10 08:55:57 -0700384 struct i2c_bus *i2c_bus = dev_get_priv(dev);
Simon Glassd84eb852012-10-30 07:28:52 +0000385
Simon Glassb0e6ef42014-12-10 08:55:57 -0700386 i2c_bus->speed = speed;
387 i2c_init_controller(i2c_bus);
Yen Lin96a78ac2012-03-06 19:00:23 +0000388
389 return 0;
390}
391
Simon Glassb0e6ef42014-12-10 08:55:57 -0700392static int tegra_i2c_probe(struct udevice *dev)
Yen Lin96a78ac2012-03-06 19:00:23 +0000393{
Simon Glassb0e6ef42014-12-10 08:55:57 -0700394 struct i2c_bus *i2c_bus = dev_get_priv(dev);
Bryan Wu3c27fa22016-08-05 16:10:35 -0600395#ifdef CONFIG_TEGRA186
396 int ret;
397#else
Simon Glassb0e6ef42014-12-10 08:55:57 -0700398 const void *blob = gd->fdt_blob;
399 int node = dev->of_offset;
Bryan Wu3c27fa22016-08-05 16:10:35 -0600400#endif
Simon Glassb0e6ef42014-12-10 08:55:57 -0700401 bool is_dvc;
402
403 i2c_bus->id = dev->seq;
Simon Glass39de8432015-03-25 12:21:55 -0600404 i2c_bus->type = dev_get_driver_data(dev);
Simon Glass4e9838c2015-08-11 08:33:29 -0600405 i2c_bus->regs = (struct i2c_ctlr *)dev_get_addr(dev);
Yen Lin96a78ac2012-03-06 19:00:23 +0000406
407 /*
408 * We don't have a binding for pinmux yet. Leave it out for now. So
409 * far no one needs anything other than the default.
410 */
Bryan Wu3c27fa22016-08-05 16:10:35 -0600411#ifdef CONFIG_TEGRA186
412 ret = reset_get_by_name(dev, "i2c", &i2c_bus->reset_ctl);
413 if (ret) {
414 error("reset_get_by_name() failed: %d\n", ret);
415 return ret;
416 }
417 ret = clk_get_by_name(dev, "i2c", &i2c_bus->clk);
418 if (ret) {
419 error("clk_get_by_name() failed: %d\n", ret);
420 return ret;
421 }
422#else
Yen Lin96a78ac2012-03-06 19:00:23 +0000423 i2c_bus->pinmux_config = FUNCMUX_DEFAULT;
Yen Lin96a78ac2012-03-06 19:00:23 +0000424 i2c_bus->periph_id = clock_decode_periph_id(blob, node);
425
426 /*
427 * We can't specify the pinmux config in the fdt, so I2C2 will not
428 * work on Seaboard. It normally has no devices on it anyway.
429 * You could add in this little hack if you need to use it.
430 * The correct solution is a pinmux binding in the fdt.
431 *
432 * if (i2c_bus->periph_id == PERIPH_ID_I2C2)
433 * i2c_bus->pinmux_config = FUNCMUX_I2C2_PTA;
434 */
435 if (i2c_bus->periph_id == -1)
Simon Glassb0e6ef42014-12-10 08:55:57 -0700436 return -EINVAL;
Bryan Wu3c27fa22016-08-05 16:10:35 -0600437#endif
Yen Lin96a78ac2012-03-06 19:00:23 +0000438
Simon Glass39de8432015-03-25 12:21:55 -0600439 is_dvc = dev_get_driver_data(dev) == TYPE_DVC;
Simon Glassb0e6ef42014-12-10 08:55:57 -0700440 if (is_dvc) {
441 i2c_bus->control =
442 &((struct dvc_ctlr *)i2c_bus->regs)->control;
443 } else {
444 i2c_bus->control = &i2c_bus->regs->control;
Yen Lin96a78ac2012-03-06 19:00:23 +0000445 }
Simon Glassb0e6ef42014-12-10 08:55:57 -0700446 i2c_init_controller(i2c_bus);
447 debug("%s: controller bus %d at %p, periph_id %d, speed %d: ",
448 is_dvc ? "dvc" : "i2c", dev->seq, i2c_bus->regs,
Bryan Wu3c27fa22016-08-05 16:10:35 -0600449#ifndef CONFIG_TEGRA186
450 i2c_bus->periph_id,
451#else
452 -1,
453#endif
454 i2c_bus->speed);
Yen Lin96a78ac2012-03-06 19:00:23 +0000455
456 return 0;
457}
458
Yen Lin96a78ac2012-03-06 19:00:23 +0000459/* i2c write version without the register address */
Simon Glassb0e6ef42014-12-10 08:55:57 -0700460static int i2c_write_data(struct i2c_bus *i2c_bus, uchar chip, uchar *buffer,
Jeroen Hofstee19d7bf32014-10-08 22:57:46 +0200461 int len, bool end_with_repeated_start)
Yen Lin96a78ac2012-03-06 19:00:23 +0000462{
463 int rc;
464
465 debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
466 debug("write_data: ");
467 /* use rc for counter */
468 for (rc = 0; rc < len; ++rc)
469 debug(" 0x%02x", buffer[rc]);
470 debug("\n");
471
472 /* Shift 7-bit address over for lower-level i2c functions */
Simon Glassb0e6ef42014-12-10 08:55:57 -0700473 rc = tegra_i2c_write_data(i2c_bus, chip << 1, buffer, len,
Stephen Warren68049a02014-06-25 10:57:27 -0600474 end_with_repeated_start);
Yen Lin96a78ac2012-03-06 19:00:23 +0000475 if (rc)
476 debug("i2c_write_data(): rc=%d\n", rc);
477
478 return rc;
479}
480
481/* i2c read version without the register address */
Simon Glassb0e6ef42014-12-10 08:55:57 -0700482static int i2c_read_data(struct i2c_bus *i2c_bus, uchar chip, uchar *buffer,
483 int len)
Yen Lin96a78ac2012-03-06 19:00:23 +0000484{
485 int rc;
486
487 debug("inside i2c_read_data():\n");
488 /* Shift 7-bit address over for lower-level i2c functions */
Simon Glassb0e6ef42014-12-10 08:55:57 -0700489 rc = tegra_i2c_read_data(i2c_bus, chip << 1, buffer, len);
Yen Lin96a78ac2012-03-06 19:00:23 +0000490 if (rc) {
491 debug("i2c_read_data(): rc=%d\n", rc);
492 return rc;
493 }
494
495 debug("i2c_read_data: ");
496 /* reuse rc for counter*/
497 for (rc = 0; rc < len; ++rc)
498 debug(" 0x%02x", buffer[rc]);
499 debug("\n");
500
501 return 0;
502}
503
504/* Probe to see if a chip is present. */
Simon Glassb0e6ef42014-12-10 08:55:57 -0700505static int tegra_i2c_probe_chip(struct udevice *bus, uint chip_addr,
506 uint chip_flags)
Yen Lin96a78ac2012-03-06 19:00:23 +0000507{
Simon Glassb0e6ef42014-12-10 08:55:57 -0700508 struct i2c_bus *i2c_bus = dev_get_priv(bus);
Yen Lin96a78ac2012-03-06 19:00:23 +0000509 int rc;
Simon Glassb0e6ef42014-12-10 08:55:57 -0700510 u8 reg;
Yen Lin96a78ac2012-03-06 19:00:23 +0000511
Simon Glassb0e6ef42014-12-10 08:55:57 -0700512 /* Shift 7-bit address over for lower-level i2c functions */
513 rc = tegra_i2c_write_data(i2c_bus, chip_addr << 1, &reg, sizeof(reg),
514 false);
515
516 return rc;
Yen Lin96a78ac2012-03-06 19:00:23 +0000517}
518
Simon Glassb0e6ef42014-12-10 08:55:57 -0700519static int tegra_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
520 int nmsgs)
Yen Lin96a78ac2012-03-06 19:00:23 +0000521{
Simon Glassb0e6ef42014-12-10 08:55:57 -0700522 struct i2c_bus *i2c_bus = dev_get_priv(bus);
523 int ret;
Yen Lin96a78ac2012-03-06 19:00:23 +0000524
Simon Glassb0e6ef42014-12-10 08:55:57 -0700525 debug("i2c_xfer: %d messages\n", nmsgs);
526 for (; nmsgs > 0; nmsgs--, msg++) {
527 bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
Yen Lin96a78ac2012-03-06 19:00:23 +0000528
Simon Glassb0e6ef42014-12-10 08:55:57 -0700529 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
530 if (msg->flags & I2C_M_RD) {
531 ret = i2c_read_data(i2c_bus, msg->addr, msg->buf,
532 msg->len);
533 } else {
534 ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
535 msg->len, next_is_read);
Yen Lin96a78ac2012-03-06 19:00:23 +0000536 }
Simon Glassb0e6ef42014-12-10 08:55:57 -0700537 if (ret) {
538 debug("i2c_write: error sending\n");
539 return -EREMOTEIO;
Yen Lin96a78ac2012-03-06 19:00:23 +0000540 }
541 }
542
543 return 0;
544}
545
Simon Glassb0e6ef42014-12-10 08:55:57 -0700546int tegra_i2c_get_dvc_bus(struct udevice **busp)
Yen Lin96a78ac2012-03-06 19:00:23 +0000547{
Simon Glassb0e6ef42014-12-10 08:55:57 -0700548 struct udevice *bus;
Yen Lin96a78ac2012-03-06 19:00:23 +0000549
Simon Glassb0e6ef42014-12-10 08:55:57 -0700550 for (uclass_first_device(UCLASS_I2C, &bus);
551 bus;
552 uclass_next_device(&bus)) {
Simon Glass39de8432015-03-25 12:21:55 -0600553 if (dev_get_driver_data(bus) == TYPE_DVC) {
Simon Glassb0e6ef42014-12-10 08:55:57 -0700554 *busp = bus;
555 return 0;
Yen Lin96a78ac2012-03-06 19:00:23 +0000556 }
557 }
558
Simon Glassb0e6ef42014-12-10 08:55:57 -0700559 return -ENODEV;
560}
561
562static const struct dm_i2c_ops tegra_i2c_ops = {
563 .xfer = tegra_i2c_xfer,
564 .probe_chip = tegra_i2c_probe_chip,
565 .set_bus_speed = tegra_i2c_set_bus_speed,
566};
567
Simon Glassb0e6ef42014-12-10 08:55:57 -0700568static const struct udevice_id tegra_i2c_ids[] = {
569 { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
570 { .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD },
571 { .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC },
572 { }
573};
Simon Glasse31c1e52012-04-02 13:19:01 +0000574
Simon Glassb0e6ef42014-12-10 08:55:57 -0700575U_BOOT_DRIVER(i2c_tegra) = {
576 .name = "i2c_tegra",
577 .id = UCLASS_I2C,
578 .of_match = tegra_i2c_ids,
Simon Glassb0e6ef42014-12-10 08:55:57 -0700579 .probe = tegra_i2c_probe,
Simon Glassb0e6ef42014-12-10 08:55:57 -0700580 .priv_auto_alloc_size = sizeof(struct i2c_bus),
581 .ops = &tegra_i2c_ops,
582};