blob: f647667a3a474447371aec1fc60098eb21cda01d [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 Glauberbd7784c2016-03-07 16:10:44 +010083 * octeon_i2c_write_sw - write an I2C core register
84 * @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 Glauberdfcd8212016-03-18 09:46:26 +010090static void octeon_i2c_write_sw(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
100/**
Jan Glauberdfcd8212016-03-18 09:46:26 +0100101 * octeon_i2c_read_sw - read lower bits of an I2C core register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100102 * @i2c: The struct octeon_i2c
103 * @eop_reg: Register selector
Rade Bozic85660f42010-01-28 12:47:07 -0800104 *
105 * Returns the data.
106 *
107 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
108 */
109static u8 octeon_i2c_read_sw(struct octeon_i2c *i2c, u64 eop_reg)
110{
111 u64 tmp;
112
113 __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI);
114 do {
115 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
116 } while ((tmp & SW_TWSI_V) != 0);
117
118 return tmp & 0xFF;
119}
120
121/**
122 * octeon_i2c_write_int - write the TWSI_INT register
Jan Glauberbd7784c2016-03-07 16:10:44 +0100123 * @i2c: The struct octeon_i2c
124 * @data: Value to be written
Rade Bozic85660f42010-01-28 12:47:07 -0800125 */
126static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
127{
Rade Bozic85660f42010-01-28 12:47:07 -0800128 __raw_writeq(data, i2c->twsi_base + TWSI_INT);
David Daneyf353a212012-07-05 18:12:39 +0200129 __raw_readq(i2c->twsi_base + TWSI_INT);
Rade Bozic85660f42010-01-28 12:47:07 -0800130}
131
132/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100133 * octeon_i2c_int_enable - enable the CORE interrupt
134 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800135 *
136 * The interrupt will be asserted when there is non-STAT_IDLE state in
137 * the SW_TWSI_EOP_TWSI_STAT register.
138 */
139static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
140{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100141 octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
Rade Bozic85660f42010-01-28 12:47:07 -0800142}
143
Jan Glauberbd7784c2016-03-07 16:10:44 +0100144/* disable the CORE interrupt */
Rade Bozic85660f42010-01-28 12:47:07 -0800145static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
146{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100147 /* clear TS/ST/IFLG events */
Rade Bozic85660f42010-01-28 12:47:07 -0800148 octeon_i2c_write_int(i2c, 0);
149}
150
151/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100152 * octeon_i2c_unblock - unblock the bus
153 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800154 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100155 * If there was a reset while a device was driving 0 to bus, bus is blocked.
156 * We toggle it free manually by some clock cycles and send a stop.
Rade Bozic85660f42010-01-28 12:47:07 -0800157 */
158static void octeon_i2c_unblock(struct octeon_i2c *i2c)
159{
160 int i;
161
162 dev_dbg(i2c->dev, "%s\n", __func__);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100163
Rade Bozic85660f42010-01-28 12:47:07 -0800164 for (i = 0; i < 9; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100165 octeon_i2c_write_int(i2c, 0);
Rade Bozic85660f42010-01-28 12:47:07 -0800166 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100167 octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800168 udelay(5);
169 }
Jan Glauberdfcd8212016-03-18 09:46:26 +0100170 /* hand-crank a STOP */
171 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR | TWSI_INT_SCL_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800172 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100173 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR);
Rade Bozic85660f42010-01-28 12:47:07 -0800174 udelay(5);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100175 octeon_i2c_write_int(i2c, 0);
Rade Bozic85660f42010-01-28 12:47:07 -0800176}
177
Jan Glauberbd7784c2016-03-07 16:10:44 +0100178/* interrupt service routine */
Rade Bozic85660f42010-01-28 12:47:07 -0800179static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
180{
181 struct octeon_i2c *i2c = dev_id;
182
183 octeon_i2c_int_disable(i2c);
송은봉2637e5f2013-04-17 21:40:17 +0000184 wake_up(&i2c->queue);
Rade Bozic85660f42010-01-28 12:47:07 -0800185
186 return IRQ_HANDLED;
187}
188
189
190static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
191{
192 return (octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_CTL) & TWSI_CTL_IFLG) != 0;
193}
194
195/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100196 * octeon_i2c_wait - wait for the IFLG to be set
197 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800198 *
199 * Returns 0 on success, otherwise a negative errno.
200 */
201static int octeon_i2c_wait(struct octeon_i2c *i2c)
202{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100203 long time_left;
Rade Bozic85660f42010-01-28 12:47:07 -0800204
205 octeon_i2c_int_enable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100206 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
207 i2c->adap.timeout);
Rade Bozic85660f42010-01-28 12:47:07 -0800208 octeon_i2c_int_disable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100209 if (!time_left) {
Rade Bozic85660f42010-01-28 12:47:07 -0800210 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
Bernhard Wallecc33e542010-09-27 12:55:16 +0200211 return -ETIMEDOUT;
Rade Bozic85660f42010-01-28 12:47:07 -0800212 }
213
214 return 0;
215}
216
Jan Glauberf541bb32016-04-11 17:28:33 +0200217/* calculate and set clock divisors */
218static void octeon_i2c_set_clock(struct octeon_i2c *i2c)
219{
220 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
221 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
222
223 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
224 /*
225 * An mdiv value of less than 2 seems to not work well
226 * with ds1337 RTCs, so we constrain it to larger values.
227 */
228 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
229 /*
230 * For given ndiv and mdiv values check the
231 * two closest thp values.
232 */
233 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
234 tclk *= (1 << ndiv_idx);
235 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
236
237 for (inc = 0; inc <= 1; inc++) {
238 thp_idx = thp_base + inc;
239 if (thp_idx < 5 || thp_idx > 0xff)
240 continue;
241
242 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
243 foscl = foscl / (1 << ndiv_idx);
244 foscl = foscl / (mdiv_idx + 1) / 10;
245 diff = abs(foscl - i2c->twsi_freq);
246 if (diff < delta_hz) {
247 delta_hz = diff;
248 thp = thp_idx;
249 mdiv = mdiv_idx;
250 ndiv = ndiv_idx;
251 }
252 }
253 }
254 }
255 octeon_i2c_write_sw(i2c, SW_TWSI_OP_TWSI_CLK, thp);
256 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
257}
258
259static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
260{
261 u8 status;
262 int tries;
263
264 /* disable high level controller, enable bus access */
265 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
266
267 /* reset controller */
268 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_RST, 0);
269
270 for (tries = 10; tries; tries--) {
271 udelay(1);
272 status = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
273 if (status == STAT_IDLE)
274 return 0;
275 }
276 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", __func__, status);
277 return -EIO;
278}
279
Rade Bozic85660f42010-01-28 12:47:07 -0800280/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100281 * octeon_i2c_start - send START to the bus
282 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800283 *
284 * Returns 0 on success, otherwise a negative errno.
285 */
286static int octeon_i2c_start(struct octeon_i2c *i2c)
287{
Rade Bozic85660f42010-01-28 12:47:07 -0800288 int result;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100289 u8 data;
Rade Bozic85660f42010-01-28 12:47:07 -0800290
291 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100292 TWSI_CTL_ENAB | TWSI_CTL_STA);
Rade Bozic85660f42010-01-28 12:47:07 -0800293
294 result = octeon_i2c_wait(i2c);
295 if (result) {
296 if (octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT) == STAT_IDLE) {
297 /*
298 * Controller refused to send start flag May
299 * be a client is holding SDA low - let's try
300 * to free it.
301 */
302 octeon_i2c_unblock(i2c);
303 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
304 TWSI_CTL_ENAB | TWSI_CTL_STA);
Rade Bozic85660f42010-01-28 12:47:07 -0800305 result = octeon_i2c_wait(i2c);
306 }
307 if (result)
308 return result;
309 }
310
311 data = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
312 if ((data != STAT_START) && (data != STAT_RSTART)) {
313 dev_err(i2c->dev, "%s: bad status (0x%x)\n", __func__, data);
314 return -EIO;
315 }
316
317 return 0;
318}
319
Jan Glauberdfcd8212016-03-18 09:46:26 +0100320/* send STOP to the bus */
321static void octeon_i2c_stop(struct octeon_i2c *i2c)
Rade Bozic85660f42010-01-28 12:47:07 -0800322{
Rade Bozic85660f42010-01-28 12:47:07 -0800323 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
324 TWSI_CTL_ENAB | TWSI_CTL_STP);
Rade Bozic85660f42010-01-28 12:47:07 -0800325}
326
327/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100328 * octeon_i2c_write - send data to the bus via low-level controller
329 * @i2c: The struct octeon_i2c
330 * @target: Target address
331 * @data: Pointer to the data to be sent
332 * @length: Length of the data
Rade Bozic85660f42010-01-28 12:47:07 -0800333 *
334 * The address is sent over the bus, then the data.
335 *
336 * Returns 0 on success, otherwise a negative errno.
337 */
338static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
339 const u8 *data, int length)
340{
341 int i, result;
342 u8 tmp;
343
344 result = octeon_i2c_start(i2c);
345 if (result)
346 return result;
347
348 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, target << 1);
349 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
350
351 result = octeon_i2c_wait(i2c);
352 if (result)
353 return result;
354
355 for (i = 0; i < length; i++) {
356 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100357
Rade Bozic85660f42010-01-28 12:47:07 -0800358 if ((tmp != STAT_TXADDR_ACK) && (tmp != STAT_TXDATA_ACK)) {
359 dev_err(i2c->dev,
360 "%s: bad status before write (0x%x)\n",
361 __func__, tmp);
362 return -EIO;
363 }
364
365 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, data[i]);
366 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
367
368 result = octeon_i2c_wait(i2c);
369 if (result)
370 return result;
371 }
372
373 return 0;
374}
375
376/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100377 * octeon_i2c_read - receive data from the bus via low-level controller
378 * @i2c: The struct octeon_i2c
379 * @target: Target address
380 * @data: Pointer to the location to store the data
David Daney886f6f82016-03-18 09:46:29 +0100381 * @rlength: Length of the data
382 * @recv_len: flag for length byte
Rade Bozic85660f42010-01-28 12:47:07 -0800383 *
384 * The address is sent over the bus, then the data is read.
385 *
386 * Returns 0 on success, otherwise a negative errno.
387 */
388static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
David Daney886f6f82016-03-18 09:46:29 +0100389 u8 *data, u16 *rlength, bool recv_len)
Rade Bozic85660f42010-01-28 12:47:07 -0800390{
David Daney886f6f82016-03-18 09:46:29 +0100391 int i, result, length = *rlength;
Rade Bozic85660f42010-01-28 12:47:07 -0800392 u8 tmp;
393
394 if (length < 1)
395 return -EINVAL;
396
397 result = octeon_i2c_start(i2c);
398 if (result)
399 return result;
400
Jan Glauberdfcd8212016-03-18 09:46:26 +0100401 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, (target << 1) | 1);
Rade Bozic85660f42010-01-28 12:47:07 -0800402 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
403
404 result = octeon_i2c_wait(i2c);
405 if (result)
406 return result;
407
408 for (i = 0; i < length; i++) {
409 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100410
Rade Bozic85660f42010-01-28 12:47:07 -0800411 if ((tmp != STAT_RXDATA_ACK) && (tmp != STAT_RXADDR_ACK)) {
412 dev_err(i2c->dev,
413 "%s: bad status before read (0x%x)\n",
414 __func__, tmp);
415 return -EIO;
416 }
417
Jan Glauberdfcd8212016-03-18 09:46:26 +0100418 if (i + 1 < length)
Rade Bozic85660f42010-01-28 12:47:07 -0800419 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100420 TWSI_CTL_ENAB | TWSI_CTL_AAK);
Rade Bozic85660f42010-01-28 12:47:07 -0800421 else
422 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100423 TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800424
425 result = octeon_i2c_wait(i2c);
426 if (result)
427 return result;
428
429 data[i] = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_DATA);
David Daney886f6f82016-03-18 09:46:29 +0100430 if (recv_len && i == 0) {
431 if (data[i] > I2C_SMBUS_BLOCK_MAX + 1) {
432 dev_err(i2c->dev,
433 "%s: read len > I2C_SMBUS_BLOCK_MAX %d\n",
434 __func__, data[i]);
435 return -EPROTO;
436 }
437 length += data[i];
438 }
Rade Bozic85660f42010-01-28 12:47:07 -0800439 }
David Daney886f6f82016-03-18 09:46:29 +0100440 *rlength = length;
Rade Bozic85660f42010-01-28 12:47:07 -0800441 return 0;
442}
443
444/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100445 * octeon_i2c_xfer - The driver's master_xfer function
446 * @adap: Pointer to the i2c_adapter structure
447 * @msgs: Pointer to the messages to be processed
448 * @num: Length of the MSGS array
Rade Bozic85660f42010-01-28 12:47:07 -0800449 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100450 * Returns the number of messages processed, or a negative errno on failure.
Rade Bozic85660f42010-01-28 12:47:07 -0800451 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100452static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
Rade Bozic85660f42010-01-28 12:47:07 -0800453 int num)
454{
Rade Bozic85660f42010-01-28 12:47:07 -0800455 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100456 int i, ret = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800457
458 for (i = 0; ret == 0 && i < num; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100459 struct i2c_msg *pmsg = &msgs[i];
460
Rade Bozic85660f42010-01-28 12:47:07 -0800461 dev_dbg(i2c->dev,
462 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
463 pmsg->flags & I2C_M_RD ? "read" : "write",
464 pmsg->len, pmsg->addr, i + 1, num);
465 if (pmsg->flags & I2C_M_RD)
466 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
David Daney886f6f82016-03-18 09:46:29 +0100467 &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
Rade Bozic85660f42010-01-28 12:47:07 -0800468 else
469 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100470 pmsg->len);
Rade Bozic85660f42010-01-28 12:47:07 -0800471 }
472 octeon_i2c_stop(i2c);
473
474 return (ret != 0) ? ret : num;
475}
476
477static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
478{
David Daney886f6f82016-03-18 09:46:29 +0100479 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
480 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
Rade Bozic85660f42010-01-28 12:47:07 -0800481}
482
483static const struct i2c_algorithm octeon_i2c_algo = {
484 .master_xfer = octeon_i2c_xfer,
485 .functionality = octeon_i2c_functionality,
486};
487
488static struct i2c_adapter octeon_i2c_ops = {
489 .owner = THIS_MODULE,
490 .name = "OCTEON adapter",
491 .algo = &octeon_i2c_algo,
Rade Bozic85660f42010-01-28 12:47:07 -0800492};
493
Bill Pemberton0b255e92012-11-27 15:59:38 -0500494static int octeon_i2c_probe(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800495{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100496 struct device_node *node = pdev->dev.of_node;
Rade Bozic85660f42010-01-28 12:47:07 -0800497 struct resource *res_mem;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100498 struct octeon_i2c *i2c;
499 int irq, result = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800500
501 /* All adaptors have an irq. */
502 irq = platform_get_irq(pdev, 0);
503 if (irq < 0)
504 return irq;
505
David Daneyf353a212012-07-05 18:12:39 +0200506 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Rade Bozic85660f42010-01-28 12:47:07 -0800507 if (!i2c) {
Rade Bozic85660f42010-01-28 12:47:07 -0800508 result = -ENOMEM;
509 goto out;
510 }
511 i2c->dev = &pdev->dev;
Rade Bozic85660f42010-01-28 12:47:07 -0800512
513 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jan Glauber54108e52016-03-18 09:46:27 +0100514 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
515 if (IS_ERR(i2c->twsi_base)) {
516 result = PTR_ERR(i2c->twsi_base);
David Daneyf353a212012-07-05 18:12:39 +0200517 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800518 }
Rade Bozic85660f42010-01-28 12:47:07 -0800519
David Daneyf353a212012-07-05 18:12:39 +0200520 /*
521 * "clock-rate" is a legacy binding, the official binding is
522 * "clock-frequency". Try the official one first and then
523 * fall back if it doesn't exist.
524 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100525 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
526 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
David Daneyf353a212012-07-05 18:12:39 +0200527 dev_err(i2c->dev,
528 "no I2C 'clock-rate' or 'clock-frequency' property\n");
529 result = -ENXIO;
530 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800531 }
David Daneyf353a212012-07-05 18:12:39 +0200532
533 i2c->sys_freq = octeon_get_io_clock_rate();
534
Rade Bozic85660f42010-01-28 12:47:07 -0800535 init_waitqueue_head(&i2c->queue);
536
537 i2c->irq = irq;
538
David Daneyf353a212012-07-05 18:12:39 +0200539 result = devm_request_irq(&pdev->dev, i2c->irq,
540 octeon_i2c_isr, 0, DRV_NAME, i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800541 if (result < 0) {
542 dev_err(i2c->dev, "failed to attach interrupt\n");
David Daneyf353a212012-07-05 18:12:39 +0200543 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800544 }
545
Jan Glauberdfcd8212016-03-18 09:46:26 +0100546 result = octeon_i2c_init_lowlevel(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800547 if (result) {
548 dev_err(i2c->dev, "init low level failed\n");
David Daneyf353a212012-07-05 18:12:39 +0200549 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800550 }
551
Jan Glauberdfcd8212016-03-18 09:46:26 +0100552 octeon_i2c_set_clock(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800553
554 i2c->adap = octeon_i2c_ops;
Jan Glaubera035d712016-04-11 17:28:32 +0200555 i2c->adap.timeout = msecs_to_jiffies(2);
556 i2c->adap.retries = 5;
Rade Bozic85660f42010-01-28 12:47:07 -0800557 i2c->adap.dev.parent = &pdev->dev;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100558 i2c->adap.dev.of_node = node;
Rade Bozic85660f42010-01-28 12:47:07 -0800559 i2c_set_adapdata(&i2c->adap, i2c);
560 platform_set_drvdata(pdev, i2c);
561
David Daneyf353a212012-07-05 18:12:39 +0200562 result = i2c_add_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800563 if (result < 0) {
564 dev_err(i2c->dev, "failed to add adapter\n");
Doug Anderson55827f42013-02-15 13:18:35 +0000565 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800566 }
Jan Glauberdfcd8212016-03-18 09:46:26 +0100567 dev_info(i2c->dev, "probed\n");
David Daneyf353a212012-07-05 18:12:39 +0200568 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800569
Rade Bozic85660f42010-01-28 12:47:07 -0800570out:
571 return result;
572};
573
Bill Pemberton0b255e92012-11-27 15:59:38 -0500574static int octeon_i2c_remove(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800575{
576 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
577
578 i2c_del_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800579 return 0;
580};
581
Jan Glauberdfcd8212016-03-18 09:46:26 +0100582static const struct of_device_id octeon_i2c_match[] = {
583 { .compatible = "cavium,octeon-3860-twsi", },
David Daneyf353a212012-07-05 18:12:39 +0200584 {},
585};
586MODULE_DEVICE_TABLE(of, octeon_i2c_match);
587
Rade Bozic85660f42010-01-28 12:47:07 -0800588static struct platform_driver octeon_i2c_driver = {
589 .probe = octeon_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500590 .remove = octeon_i2c_remove,
Rade Bozic85660f42010-01-28 12:47:07 -0800591 .driver = {
Rade Bozic85660f42010-01-28 12:47:07 -0800592 .name = DRV_NAME,
David Daneyf353a212012-07-05 18:12:39 +0200593 .of_match_table = octeon_i2c_match,
Rade Bozic85660f42010-01-28 12:47:07 -0800594 },
595};
596
Axel Lina3664b52012-01-12 20:32:04 +0100597module_platform_driver(octeon_i2c_driver);
Rade Bozic85660f42010-01-28 12:47:07 -0800598
599MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
600MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
601MODULE_LICENSE("GPL");