blob: 4275007c2907b690b3ed0ece2eb301e77418f4eb [file] [log] [blame]
Rade Bozic85660f42010-01-28 12:47:07 -08001/*
2 * (C) Copyright 2009-2010
3 * Nokia Siemens Networks, michael.lawnick.ext@nsn.com
4 *
Jan Glauberdfcd8212016-03-18 09:46:26 +01005 * Portions Copyright (C) 2010 - 2016 Cavium, Inc.
Rade Bozic85660f42010-01-28 12:47:07 -08006 *
7 * This is a driver for the i2c adapter in Cavium Networks' OCTEON processors.
8 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
David Daneyf353a212012-07-05 18:12:39 +020014#include <linux/platform_device.h>
15#include <linux/interrupt.h>
Rade Bozic85660f42010-01-28 12:47:07 -080016#include <linux/kernel.h>
17#include <linux/module.h>
David Daneyf353a212012-07-05 18:12:39 +020018#include <linux/delay.h>
Rade Bozic85660f42010-01-28 12:47:07 -080019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Rade Bozic85660f42010-01-28 12:47:07 -080021#include <linux/i2c.h>
David Daneyf353a212012-07-05 18:12:39 +020022#include <linux/io.h>
23#include <linux/of.h>
Rade Bozic85660f42010-01-28 12:47:07 -080024
25#include <asm/octeon/octeon.h>
26
27#define DRV_NAME "i2c-octeon"
28
Jan Glauberdfcd8212016-03-18 09:46:26 +010029/* Register offsets */
30#define SW_TWSI 0x00
31#define TWSI_INT 0x10
Rade Bozic85660f42010-01-28 12:47:07 -080032
33/* Controller command patterns */
Jan Glauberdfcd8212016-03-18 09:46:26 +010034#define SW_TWSI_V BIT_ULL(63) /* Valid bit */
35#define SW_TWSI_R BIT_ULL(56) /* Result or read bit */
36
37/* Controller opcode word (bits 60:57) */
38#define SW_TWSI_OP_SHIFT 57
39#define SW_TWSI_OP_TWSI_CLK (4ULL << SW_TWSI_OP_SHIFT)
40#define SW_TWSI_OP_EOP (6ULL << SW_TWSI_OP_SHIFT) /* Extended opcode */
41
42/* Controller extended opcode word (bits 34:32) */
43#define SW_TWSI_EOP_SHIFT 32
44#define SW_TWSI_EOP_TWSI_DATA (SW_TWSI_OP_EOP | 1ULL << SW_TWSI_EOP_SHIFT)
45#define SW_TWSI_EOP_TWSI_CTL (SW_TWSI_OP_EOP | 2ULL << SW_TWSI_EOP_SHIFT)
46#define SW_TWSI_EOP_TWSI_CLKCTL (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
47#define SW_TWSI_EOP_TWSI_STAT (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
48#define SW_TWSI_EOP_TWSI_RST (SW_TWSI_OP_EOP | 7ULL << SW_TWSI_EOP_SHIFT)
Rade Bozic85660f42010-01-28 12:47:07 -080049
50/* Controller command and status bits */
Jan Glauberdfcd8212016-03-18 09:46:26 +010051#define TWSI_CTL_CE 0x80
52#define TWSI_CTL_ENAB 0x40 /* Bus enable */
53#define TWSI_CTL_STA 0x20 /* Master-mode start, HW clears when done */
54#define TWSI_CTL_STP 0x10 /* Master-mode stop, HW clears when done */
55#define TWSI_CTL_IFLG 0x08 /* HW event, SW writes 0 to ACK */
56#define TWSI_CTL_AAK 0x04 /* Assert ACK */
Rade Bozic85660f42010-01-28 12:47:07 -080057
58/* Some status values */
Jan Glauberdfcd8212016-03-18 09:46:26 +010059#define STAT_START 0x08
60#define STAT_RSTART 0x10
61#define STAT_TXADDR_ACK 0x18
62#define STAT_TXDATA_ACK 0x28
63#define STAT_RXADDR_ACK 0x40
64#define STAT_RXDATA_ACK 0x50
65#define STAT_IDLE 0xF8
66
67/* TWSI_INT values */
68#define TWSI_INT_CORE_EN BIT_ULL(6)
69#define TWSI_INT_SDA_OVR BIT_ULL(8)
70#define TWSI_INT_SCL_OVR BIT_ULL(9)
Rade Bozic85660f42010-01-28 12:47:07 -080071
72struct octeon_i2c {
73 wait_queue_head_t queue;
74 struct i2c_adapter adap;
75 int irq;
David Daneyf353a212012-07-05 18:12:39 +020076 u32 twsi_freq;
Rade Bozic85660f42010-01-28 12:47:07 -080077 int sys_freq;
Rade Bozic85660f42010-01-28 12:47:07 -080078 void __iomem *twsi_base;
Rade Bozic85660f42010-01-28 12:47:07 -080079 struct device *dev;
80};
81
82/**
Jan Glauber9cb94802016-04-11 17:28:34 +020083 * octeon_i2c_reg_write - write an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +010084 * @i2c: The struct octeon_i2c
85 * @eop_reg: Register selector
86 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -080087 *
88 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
89 */
Jan Glauber9cb94802016-04-11 17:28:34 +020090static void octeon_i2c_reg_write(struct octeon_i2c *i2c, u64 eop_reg, u8 data)
Rade Bozic85660f42010-01-28 12:47:07 -080091{
92 u64 tmp;
93
94 __raw_writeq(SW_TWSI_V | eop_reg | data, i2c->twsi_base + SW_TWSI);
95 do {
96 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
97 } while ((tmp & SW_TWSI_V) != 0);
98}
99
Jan Glauberc57db702016-04-11 17:28:35 +0200100#define octeon_i2c_ctl_write(i2c, val) \
101 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CTL, val)
102#define octeon_i2c_data_write(i2c, val) \
103 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_DATA, val)
104
Rade Bozic85660f42010-01-28 12:47:07 -0800105/**
Jan Glauber9cb94802016-04-11 17:28:34 +0200106 * octeon_i2c_reg_read - read lower bits of an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100107 * @i2c: The struct octeon_i2c
108 * @eop_reg: Register selector
Rade Bozic85660f42010-01-28 12:47:07 -0800109 *
110 * Returns the data.
111 *
112 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
113 */
Jan Glauber9cb94802016-04-11 17:28:34 +0200114static u8 octeon_i2c_reg_read(struct octeon_i2c *i2c, u64 eop_reg)
Rade Bozic85660f42010-01-28 12:47:07 -0800115{
116 u64 tmp;
117
118 __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI);
119 do {
120 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
121 } while ((tmp & SW_TWSI_V) != 0);
122
123 return tmp & 0xFF;
124}
125
Jan Glauberc57db702016-04-11 17:28:35 +0200126#define octeon_i2c_ctl_read(i2c) \
127 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_CTL)
128#define octeon_i2c_data_read(i2c) \
129 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_DATA)
130#define octeon_i2c_stat_read(i2c) \
131 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_STAT)
132
Rade Bozic85660f42010-01-28 12:47:07 -0800133/**
134 * octeon_i2c_write_int - write the TWSI_INT register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100135 * @i2c: The struct octeon_i2c
136 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -0800137 */
138static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
139{
Rade Bozic85660f42010-01-28 12:47:07 -0800140 __raw_writeq(data, i2c->twsi_base + TWSI_INT);
David Daneyf353a212012-07-05 18:12:39 +0200141 __raw_readq(i2c->twsi_base + TWSI_INT);
Rade Bozic85660f42010-01-28 12:47:07 -0800142}
143
144/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100145 * octeon_i2c_int_enable - enable the CORE interrupt
146 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800147 *
148 * The interrupt will be asserted when there is non-STAT_IDLE state in
149 * the SW_TWSI_EOP_TWSI_STAT register.
150 */
151static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
152{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100153 octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
Rade Bozic85660f42010-01-28 12:47:07 -0800154}
155
Jan Glauberbd7784c2016-03-07 16:10:44 +0100156/* disable the CORE interrupt */
Rade Bozic85660f42010-01-28 12:47:07 -0800157static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
158{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100159 /* clear TS/ST/IFLG events */
Rade Bozic85660f42010-01-28 12:47:07 -0800160 octeon_i2c_write_int(i2c, 0);
161}
162
163/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100164 * octeon_i2c_unblock - unblock the bus
165 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800166 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100167 * If there was a reset while a device was driving 0 to bus, bus is blocked.
168 * We toggle it free manually by some clock cycles and send a stop.
Rade Bozic85660f42010-01-28 12:47:07 -0800169 */
170static void octeon_i2c_unblock(struct octeon_i2c *i2c)
171{
172 int i;
173
174 dev_dbg(i2c->dev, "%s\n", __func__);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100175
Rade Bozic85660f42010-01-28 12:47:07 -0800176 for (i = 0; i < 9; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100177 octeon_i2c_write_int(i2c, 0);
Rade Bozic85660f42010-01-28 12:47:07 -0800178 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100179 octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800180 udelay(5);
181 }
Jan Glauberdfcd8212016-03-18 09:46:26 +0100182 /* hand-crank a STOP */
183 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR | TWSI_INT_SCL_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800184 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100185 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800186 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100187 octeon_i2c_write_int(i2c, 0);
Rade Bozic85660f42010-01-28 12:47:07 -0800188}
189
Jan Glauberbd7784c2016-03-07 16:10:44 +0100190/* interrupt service routine */
Rade Bozic85660f42010-01-28 12:47:07 -0800191static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
192{
193 struct octeon_i2c *i2c = dev_id;
194
195 octeon_i2c_int_disable(i2c);
송은봉2637e5f2013-04-17 21:40:17 +0000196 wake_up(&i2c->queue);
Rade Bozic85660f42010-01-28 12:47:07 -0800197
198 return IRQ_HANDLED;
199}
200
Rade Bozic85660f42010-01-28 12:47:07 -0800201static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
202{
Jan Glauberb69e5c62016-04-11 17:28:36 +0200203 return (octeon_i2c_ctl_read(i2c) & TWSI_CTL_IFLG);
Rade Bozic85660f42010-01-28 12:47:07 -0800204}
205
206/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100207 * octeon_i2c_wait - wait for the IFLG to be set
208 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800209 *
210 * Returns 0 on success, otherwise a negative errno.
211 */
212static int octeon_i2c_wait(struct octeon_i2c *i2c)
213{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100214 long time_left;
Rade Bozic85660f42010-01-28 12:47:07 -0800215
216 octeon_i2c_int_enable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100217 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
218 i2c->adap.timeout);
Rade Bozic85660f42010-01-28 12:47:07 -0800219 octeon_i2c_int_disable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100220 if (!time_left) {
Rade Bozic85660f42010-01-28 12:47:07 -0800221 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
Bernhard Wallecc33e542010-09-27 12:55:16 +0200222 return -ETIMEDOUT;
Rade Bozic85660f42010-01-28 12:47:07 -0800223 }
224
225 return 0;
226}
227
Jan Glauberf541bb32016-04-11 17:28:33 +0200228/* calculate and set clock divisors */
229static void octeon_i2c_set_clock(struct octeon_i2c *i2c)
230{
231 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
232 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
233
234 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
235 /*
236 * An mdiv value of less than 2 seems to not work well
237 * with ds1337 RTCs, so we constrain it to larger values.
238 */
239 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
240 /*
241 * For given ndiv and mdiv values check the
242 * two closest thp values.
243 */
244 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
245 tclk *= (1 << ndiv_idx);
246 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
247
248 for (inc = 0; inc <= 1; inc++) {
249 thp_idx = thp_base + inc;
250 if (thp_idx < 5 || thp_idx > 0xff)
251 continue;
252
253 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
254 foscl = foscl / (1 << ndiv_idx);
255 foscl = foscl / (mdiv_idx + 1) / 10;
256 diff = abs(foscl - i2c->twsi_freq);
257 if (diff < delta_hz) {
258 delta_hz = diff;
259 thp = thp_idx;
260 mdiv = mdiv_idx;
261 ndiv = ndiv_idx;
262 }
263 }
264 }
265 }
Jan Glauber9cb94802016-04-11 17:28:34 +0200266 octeon_i2c_reg_write(i2c, SW_TWSI_OP_TWSI_CLK, thp);
267 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
Jan Glauberf541bb32016-04-11 17:28:33 +0200268}
269
270static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
271{
272 u8 status;
273 int tries;
274
275 /* disable high level controller, enable bus access */
Jan Glauberc57db702016-04-11 17:28:35 +0200276 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Jan Glauberf541bb32016-04-11 17:28:33 +0200277
278 /* reset controller */
Jan Glauber9cb94802016-04-11 17:28:34 +0200279 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0);
Jan Glauberf541bb32016-04-11 17:28:33 +0200280
281 for (tries = 10; tries; tries--) {
282 udelay(1);
Jan Glauberc57db702016-04-11 17:28:35 +0200283 status = octeon_i2c_stat_read(i2c);
Jan Glauberf541bb32016-04-11 17:28:33 +0200284 if (status == STAT_IDLE)
285 return 0;
286 }
287 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", __func__, status);
288 return -EIO;
289}
290
Rade Bozic85660f42010-01-28 12:47:07 -0800291/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100292 * octeon_i2c_start - send START to the bus
293 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800294 *
295 * Returns 0 on success, otherwise a negative errno.
296 */
297static int octeon_i2c_start(struct octeon_i2c *i2c)
298{
Rade Bozic85660f42010-01-28 12:47:07 -0800299 int result;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100300 u8 data;
Rade Bozic85660f42010-01-28 12:47:07 -0800301
Jan Glauberc57db702016-04-11 17:28:35 +0200302 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
Rade Bozic85660f42010-01-28 12:47:07 -0800303
304 result = octeon_i2c_wait(i2c);
305 if (result) {
Jan Glauberc57db702016-04-11 17:28:35 +0200306 if (octeon_i2c_stat_read(i2c) == STAT_IDLE) {
Rade Bozic85660f42010-01-28 12:47:07 -0800307 /*
308 * Controller refused to send start flag May
309 * be a client is holding SDA low - let's try
310 * to free it.
311 */
312 octeon_i2c_unblock(i2c);
Jan Glauberc57db702016-04-11 17:28:35 +0200313 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
Rade Bozic85660f42010-01-28 12:47:07 -0800314 result = octeon_i2c_wait(i2c);
315 }
316 if (result)
317 return result;
318 }
319
Jan Glauberc57db702016-04-11 17:28:35 +0200320 data = octeon_i2c_stat_read(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800321 if ((data != STAT_START) && (data != STAT_RSTART)) {
322 dev_err(i2c->dev, "%s: bad status (0x%x)\n", __func__, data);
323 return -EIO;
324 }
325
326 return 0;
327}
328
Jan Glauberdfcd8212016-03-18 09:46:26 +0100329/* send STOP to the bus */
330static void octeon_i2c_stop(struct octeon_i2c *i2c)
Rade Bozic85660f42010-01-28 12:47:07 -0800331{
Jan Glauberc57db702016-04-11 17:28:35 +0200332 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STP);
Rade Bozic85660f42010-01-28 12:47:07 -0800333}
334
335/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100336 * octeon_i2c_write - send data to the bus via low-level controller
337 * @i2c: The struct octeon_i2c
338 * @target: Target address
339 * @data: Pointer to the data to be sent
340 * @length: Length of the data
Rade Bozic85660f42010-01-28 12:47:07 -0800341 *
342 * The address is sent over the bus, then the data.
343 *
344 * Returns 0 on success, otherwise a negative errno.
345 */
346static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
347 const u8 *data, int length)
348{
349 int i, result;
350 u8 tmp;
351
352 result = octeon_i2c_start(i2c);
353 if (result)
354 return result;
355
Jan Glauberc57db702016-04-11 17:28:35 +0200356 octeon_i2c_data_write(i2c, target << 1);
357 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800358
359 result = octeon_i2c_wait(i2c);
360 if (result)
361 return result;
362
363 for (i = 0; i < length; i++) {
Jan Glauberc57db702016-04-11 17:28:35 +0200364 tmp = octeon_i2c_stat_read(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100365
Rade Bozic85660f42010-01-28 12:47:07 -0800366 if ((tmp != STAT_TXADDR_ACK) && (tmp != STAT_TXDATA_ACK)) {
367 dev_err(i2c->dev,
368 "%s: bad status before write (0x%x)\n",
369 __func__, tmp);
370 return -EIO;
371 }
372
Jan Glauberc57db702016-04-11 17:28:35 +0200373 octeon_i2c_data_write(i2c, data[i]);
374 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800375
376 result = octeon_i2c_wait(i2c);
377 if (result)
378 return result;
379 }
380
381 return 0;
382}
383
384/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100385 * octeon_i2c_read - receive data from the bus via low-level controller
386 * @i2c: The struct octeon_i2c
387 * @target: Target address
388 * @data: Pointer to the location to store the data
David Daney886f6f82016-03-18 09:46:29 +0100389 * @rlength: Length of the data
390 * @recv_len: flag for length byte
Rade Bozic85660f42010-01-28 12:47:07 -0800391 *
392 * The address is sent over the bus, then the data is read.
393 *
394 * Returns 0 on success, otherwise a negative errno.
395 */
396static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
David Daney886f6f82016-03-18 09:46:29 +0100397 u8 *data, u16 *rlength, bool recv_len)
Rade Bozic85660f42010-01-28 12:47:07 -0800398{
David Daney886f6f82016-03-18 09:46:29 +0100399 int i, result, length = *rlength;
Rade Bozic85660f42010-01-28 12:47:07 -0800400 u8 tmp;
401
402 if (length < 1)
403 return -EINVAL;
404
405 result = octeon_i2c_start(i2c);
406 if (result)
407 return result;
408
Jan Glauberc57db702016-04-11 17:28:35 +0200409 octeon_i2c_data_write(i2c, (target << 1) | 1);
410 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800411
412 result = octeon_i2c_wait(i2c);
413 if (result)
414 return result;
415
416 for (i = 0; i < length; i++) {
Jan Glauberc57db702016-04-11 17:28:35 +0200417 tmp = octeon_i2c_stat_read(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800418 if ((tmp != STAT_RXDATA_ACK) && (tmp != STAT_RXADDR_ACK)) {
419 dev_err(i2c->dev,
420 "%s: bad status before read (0x%x)\n",
421 __func__, tmp);
422 return -EIO;
423 }
424
Jan Glauberdfcd8212016-03-18 09:46:26 +0100425 if (i + 1 < length)
Jan Glauberc57db702016-04-11 17:28:35 +0200426 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_AAK);
Rade Bozic85660f42010-01-28 12:47:07 -0800427 else
Jan Glauberc57db702016-04-11 17:28:35 +0200428 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800429
430 result = octeon_i2c_wait(i2c);
431 if (result)
432 return result;
433
Jan Glauberc57db702016-04-11 17:28:35 +0200434 data[i] = octeon_i2c_data_read(i2c);
David Daney886f6f82016-03-18 09:46:29 +0100435 if (recv_len && i == 0) {
436 if (data[i] > I2C_SMBUS_BLOCK_MAX + 1) {
437 dev_err(i2c->dev,
438 "%s: read len > I2C_SMBUS_BLOCK_MAX %d\n",
439 __func__, data[i]);
440 return -EPROTO;
441 }
442 length += data[i];
443 }
Rade Bozic85660f42010-01-28 12:47:07 -0800444 }
David Daney886f6f82016-03-18 09:46:29 +0100445 *rlength = length;
Rade Bozic85660f42010-01-28 12:47:07 -0800446 return 0;
447}
448
449/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100450 * octeon_i2c_xfer - The driver's master_xfer function
451 * @adap: Pointer to the i2c_adapter structure
452 * @msgs: Pointer to the messages to be processed
453 * @num: Length of the MSGS array
Rade Bozic85660f42010-01-28 12:47:07 -0800454 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100455 * Returns the number of messages processed, or a negative errno on failure.
Rade Bozic85660f42010-01-28 12:47:07 -0800456 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100457static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
Rade Bozic85660f42010-01-28 12:47:07 -0800458 int num)
459{
Rade Bozic85660f42010-01-28 12:47:07 -0800460 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100461 int i, ret = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800462
463 for (i = 0; ret == 0 && i < num; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100464 struct i2c_msg *pmsg = &msgs[i];
465
Rade Bozic85660f42010-01-28 12:47:07 -0800466 dev_dbg(i2c->dev,
467 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
468 pmsg->flags & I2C_M_RD ? "read" : "write",
469 pmsg->len, pmsg->addr, i + 1, num);
470 if (pmsg->flags & I2C_M_RD)
471 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
David Daney886f6f82016-03-18 09:46:29 +0100472 &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
Rade Bozic85660f42010-01-28 12:47:07 -0800473 else
474 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100475 pmsg->len);
Rade Bozic85660f42010-01-28 12:47:07 -0800476 }
477 octeon_i2c_stop(i2c);
478
479 return (ret != 0) ? ret : num;
480}
481
482static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
483{
David Daney886f6f82016-03-18 09:46:29 +0100484 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
485 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
Rade Bozic85660f42010-01-28 12:47:07 -0800486}
487
488static const struct i2c_algorithm octeon_i2c_algo = {
489 .master_xfer = octeon_i2c_xfer,
490 .functionality = octeon_i2c_functionality,
491};
492
493static struct i2c_adapter octeon_i2c_ops = {
494 .owner = THIS_MODULE,
495 .name = "OCTEON adapter",
496 .algo = &octeon_i2c_algo,
Rade Bozic85660f42010-01-28 12:47:07 -0800497};
498
Bill Pemberton0b255e92012-11-27 15:59:38 -0500499static int octeon_i2c_probe(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800500{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100501 struct device_node *node = pdev->dev.of_node;
Rade Bozic85660f42010-01-28 12:47:07 -0800502 struct resource *res_mem;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100503 struct octeon_i2c *i2c;
504 int irq, result = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800505
506 /* All adaptors have an irq. */
507 irq = platform_get_irq(pdev, 0);
508 if (irq < 0)
509 return irq;
510
David Daneyf353a212012-07-05 18:12:39 +0200511 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Rade Bozic85660f42010-01-28 12:47:07 -0800512 if (!i2c) {
Rade Bozic85660f42010-01-28 12:47:07 -0800513 result = -ENOMEM;
514 goto out;
515 }
516 i2c->dev = &pdev->dev;
Rade Bozic85660f42010-01-28 12:47:07 -0800517
518 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jan Glauber54108e52016-03-18 09:46:27 +0100519 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
520 if (IS_ERR(i2c->twsi_base)) {
521 result = PTR_ERR(i2c->twsi_base);
David Daneyf353a212012-07-05 18:12:39 +0200522 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800523 }
Rade Bozic85660f42010-01-28 12:47:07 -0800524
David Daneyf353a212012-07-05 18:12:39 +0200525 /*
526 * "clock-rate" is a legacy binding, the official binding is
527 * "clock-frequency". Try the official one first and then
528 * fall back if it doesn't exist.
529 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100530 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
531 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
David Daneyf353a212012-07-05 18:12:39 +0200532 dev_err(i2c->dev,
533 "no I2C 'clock-rate' or 'clock-frequency' property\n");
534 result = -ENXIO;
535 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800536 }
David Daneyf353a212012-07-05 18:12:39 +0200537
538 i2c->sys_freq = octeon_get_io_clock_rate();
539
Rade Bozic85660f42010-01-28 12:47:07 -0800540 init_waitqueue_head(&i2c->queue);
541
542 i2c->irq = irq;
543
David Daneyf353a212012-07-05 18:12:39 +0200544 result = devm_request_irq(&pdev->dev, i2c->irq,
545 octeon_i2c_isr, 0, DRV_NAME, i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800546 if (result < 0) {
547 dev_err(i2c->dev, "failed to attach interrupt\n");
David Daneyf353a212012-07-05 18:12:39 +0200548 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800549 }
550
Jan Glauberdfcd8212016-03-18 09:46:26 +0100551 result = octeon_i2c_init_lowlevel(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800552 if (result) {
553 dev_err(i2c->dev, "init low level failed\n");
David Daneyf353a212012-07-05 18:12:39 +0200554 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800555 }
556
Jan Glauberdfcd8212016-03-18 09:46:26 +0100557 octeon_i2c_set_clock(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800558
559 i2c->adap = octeon_i2c_ops;
Jan Glaubera035d712016-04-11 17:28:32 +0200560 i2c->adap.timeout = msecs_to_jiffies(2);
561 i2c->adap.retries = 5;
Rade Bozic85660f42010-01-28 12:47:07 -0800562 i2c->adap.dev.parent = &pdev->dev;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100563 i2c->adap.dev.of_node = node;
Rade Bozic85660f42010-01-28 12:47:07 -0800564 i2c_set_adapdata(&i2c->adap, i2c);
565 platform_set_drvdata(pdev, i2c);
566
David Daneyf353a212012-07-05 18:12:39 +0200567 result = i2c_add_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800568 if (result < 0) {
569 dev_err(i2c->dev, "failed to add adapter\n");
Doug Anderson55827f42013-02-15 13:18:35 +0000570 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800571 }
Jan Glauberdfcd8212016-03-18 09:46:26 +0100572 dev_info(i2c->dev, "probed\n");
David Daneyf353a212012-07-05 18:12:39 +0200573 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800574
Rade Bozic85660f42010-01-28 12:47:07 -0800575out:
576 return result;
577};
578
Bill Pemberton0b255e92012-11-27 15:59:38 -0500579static int octeon_i2c_remove(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800580{
581 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
582
583 i2c_del_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800584 return 0;
585};
586
Jan Glauberdfcd8212016-03-18 09:46:26 +0100587static const struct of_device_id octeon_i2c_match[] = {
588 { .compatible = "cavium,octeon-3860-twsi", },
David Daneyf353a212012-07-05 18:12:39 +0200589 {},
590};
591MODULE_DEVICE_TABLE(of, octeon_i2c_match);
592
Rade Bozic85660f42010-01-28 12:47:07 -0800593static struct platform_driver octeon_i2c_driver = {
594 .probe = octeon_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500595 .remove = octeon_i2c_remove,
Rade Bozic85660f42010-01-28 12:47:07 -0800596 .driver = {
Rade Bozic85660f42010-01-28 12:47:07 -0800597 .name = DRV_NAME,
David Daneyf353a212012-07-05 18:12:39 +0200598 .of_match_table = octeon_i2c_match,
Rade Bozic85660f42010-01-28 12:47:07 -0800599 },
600};
601
Axel Lina3664b52012-01-12 20:32:04 +0100602module_platform_driver(octeon_i2c_driver);
Rade Bozic85660f42010-01-28 12:47:07 -0800603
604MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
605MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
606MODULE_LICENSE("GPL");