blob: b25c49151ea4bdd42588fa945cdf143271bc39dd [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
201
202static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
203{
Jan Glauberc57db702016-04-11 17:28:35 +0200204 return (octeon_i2c_ctl_read(i2c) & TWSI_CTL_IFLG) != 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800205}
206
207/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100208 * octeon_i2c_wait - wait for the IFLG to be set
209 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800210 *
211 * Returns 0 on success, otherwise a negative errno.
212 */
213static int octeon_i2c_wait(struct octeon_i2c *i2c)
214{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100215 long time_left;
Rade Bozic85660f42010-01-28 12:47:07 -0800216
217 octeon_i2c_int_enable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100218 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
219 i2c->adap.timeout);
Rade Bozic85660f42010-01-28 12:47:07 -0800220 octeon_i2c_int_disable(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100221 if (!time_left) {
Rade Bozic85660f42010-01-28 12:47:07 -0800222 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
Bernhard Wallecc33e542010-09-27 12:55:16 +0200223 return -ETIMEDOUT;
Rade Bozic85660f42010-01-28 12:47:07 -0800224 }
225
226 return 0;
227}
228
Jan Glauberf541bb32016-04-11 17:28:33 +0200229/* calculate and set clock divisors */
230static void octeon_i2c_set_clock(struct octeon_i2c *i2c)
231{
232 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
233 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
234
235 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
236 /*
237 * An mdiv value of less than 2 seems to not work well
238 * with ds1337 RTCs, so we constrain it to larger values.
239 */
240 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
241 /*
242 * For given ndiv and mdiv values check the
243 * two closest thp values.
244 */
245 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
246 tclk *= (1 << ndiv_idx);
247 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
248
249 for (inc = 0; inc <= 1; inc++) {
250 thp_idx = thp_base + inc;
251 if (thp_idx < 5 || thp_idx > 0xff)
252 continue;
253
254 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
255 foscl = foscl / (1 << ndiv_idx);
256 foscl = foscl / (mdiv_idx + 1) / 10;
257 diff = abs(foscl - i2c->twsi_freq);
258 if (diff < delta_hz) {
259 delta_hz = diff;
260 thp = thp_idx;
261 mdiv = mdiv_idx;
262 ndiv = ndiv_idx;
263 }
264 }
265 }
266 }
Jan Glauber9cb94802016-04-11 17:28:34 +0200267 octeon_i2c_reg_write(i2c, SW_TWSI_OP_TWSI_CLK, thp);
268 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
Jan Glauberf541bb32016-04-11 17:28:33 +0200269}
270
271static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
272{
273 u8 status;
274 int tries;
275
276 /* disable high level controller, enable bus access */
Jan Glauberc57db702016-04-11 17:28:35 +0200277 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Jan Glauberf541bb32016-04-11 17:28:33 +0200278
279 /* reset controller */
Jan Glauber9cb94802016-04-11 17:28:34 +0200280 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0);
Jan Glauberf541bb32016-04-11 17:28:33 +0200281
282 for (tries = 10; tries; tries--) {
283 udelay(1);
Jan Glauberc57db702016-04-11 17:28:35 +0200284 status = octeon_i2c_stat_read(i2c);
Jan Glauberf541bb32016-04-11 17:28:33 +0200285 if (status == STAT_IDLE)
286 return 0;
287 }
288 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", __func__, status);
289 return -EIO;
290}
291
Rade Bozic85660f42010-01-28 12:47:07 -0800292/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100293 * octeon_i2c_start - send START to the bus
294 * @i2c: The struct octeon_i2c
Rade Bozic85660f42010-01-28 12:47:07 -0800295 *
296 * Returns 0 on success, otherwise a negative errno.
297 */
298static int octeon_i2c_start(struct octeon_i2c *i2c)
299{
Rade Bozic85660f42010-01-28 12:47:07 -0800300 int result;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100301 u8 data;
Rade Bozic85660f42010-01-28 12:47:07 -0800302
Jan Glauberc57db702016-04-11 17:28:35 +0200303 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
Rade Bozic85660f42010-01-28 12:47:07 -0800304
305 result = octeon_i2c_wait(i2c);
306 if (result) {
Jan Glauberc57db702016-04-11 17:28:35 +0200307 if (octeon_i2c_stat_read(i2c) == STAT_IDLE) {
Rade Bozic85660f42010-01-28 12:47:07 -0800308 /*
309 * Controller refused to send start flag May
310 * be a client is holding SDA low - let's try
311 * to free it.
312 */
313 octeon_i2c_unblock(i2c);
Jan Glauberc57db702016-04-11 17:28:35 +0200314 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
Rade Bozic85660f42010-01-28 12:47:07 -0800315 result = octeon_i2c_wait(i2c);
316 }
317 if (result)
318 return result;
319 }
320
Jan Glauberc57db702016-04-11 17:28:35 +0200321 data = octeon_i2c_stat_read(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800322 if ((data != STAT_START) && (data != STAT_RSTART)) {
323 dev_err(i2c->dev, "%s: bad status (0x%x)\n", __func__, data);
324 return -EIO;
325 }
326
327 return 0;
328}
329
Jan Glauberdfcd8212016-03-18 09:46:26 +0100330/* send STOP to the bus */
331static void octeon_i2c_stop(struct octeon_i2c *i2c)
Rade Bozic85660f42010-01-28 12:47:07 -0800332{
Jan Glauberc57db702016-04-11 17:28:35 +0200333 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STP);
Rade Bozic85660f42010-01-28 12:47:07 -0800334}
335
336/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100337 * octeon_i2c_write - send data to the bus via low-level controller
338 * @i2c: The struct octeon_i2c
339 * @target: Target address
340 * @data: Pointer to the data to be sent
341 * @length: Length of the data
Rade Bozic85660f42010-01-28 12:47:07 -0800342 *
343 * The address is sent over the bus, then the data.
344 *
345 * Returns 0 on success, otherwise a negative errno.
346 */
347static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
348 const u8 *data, int length)
349{
350 int i, result;
351 u8 tmp;
352
353 result = octeon_i2c_start(i2c);
354 if (result)
355 return result;
356
Jan Glauberc57db702016-04-11 17:28:35 +0200357 octeon_i2c_data_write(i2c, target << 1);
358 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800359
360 result = octeon_i2c_wait(i2c);
361 if (result)
362 return result;
363
364 for (i = 0; i < length; i++) {
Jan Glauberc57db702016-04-11 17:28:35 +0200365 tmp = octeon_i2c_stat_read(i2c);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100366
Rade Bozic85660f42010-01-28 12:47:07 -0800367 if ((tmp != STAT_TXADDR_ACK) && (tmp != STAT_TXDATA_ACK)) {
368 dev_err(i2c->dev,
369 "%s: bad status before write (0x%x)\n",
370 __func__, tmp);
371 return -EIO;
372 }
373
Jan Glauberc57db702016-04-11 17:28:35 +0200374 octeon_i2c_data_write(i2c, data[i]);
375 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800376
377 result = octeon_i2c_wait(i2c);
378 if (result)
379 return result;
380 }
381
382 return 0;
383}
384
385/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100386 * octeon_i2c_read - receive data from the bus via low-level controller
387 * @i2c: The struct octeon_i2c
388 * @target: Target address
389 * @data: Pointer to the location to store the data
David Daney886f6f82016-03-18 09:46:29 +0100390 * @rlength: Length of the data
391 * @recv_len: flag for length byte
Rade Bozic85660f42010-01-28 12:47:07 -0800392 *
393 * The address is sent over the bus, then the data is read.
394 *
395 * Returns 0 on success, otherwise a negative errno.
396 */
397static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
David Daney886f6f82016-03-18 09:46:29 +0100398 u8 *data, u16 *rlength, bool recv_len)
Rade Bozic85660f42010-01-28 12:47:07 -0800399{
David Daney886f6f82016-03-18 09:46:29 +0100400 int i, result, length = *rlength;
Rade Bozic85660f42010-01-28 12:47:07 -0800401 u8 tmp;
402
403 if (length < 1)
404 return -EINVAL;
405
406 result = octeon_i2c_start(i2c);
407 if (result)
408 return result;
409
Jan Glauberc57db702016-04-11 17:28:35 +0200410 octeon_i2c_data_write(i2c, (target << 1) | 1);
411 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800412
413 result = octeon_i2c_wait(i2c);
414 if (result)
415 return result;
416
417 for (i = 0; i < length; i++) {
Jan Glauberc57db702016-04-11 17:28:35 +0200418 tmp = octeon_i2c_stat_read(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800419 if ((tmp != STAT_RXDATA_ACK) && (tmp != STAT_RXADDR_ACK)) {
420 dev_err(i2c->dev,
421 "%s: bad status before read (0x%x)\n",
422 __func__, tmp);
423 return -EIO;
424 }
425
Jan Glauberdfcd8212016-03-18 09:46:26 +0100426 if (i + 1 < length)
Jan Glauberc57db702016-04-11 17:28:35 +0200427 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_AAK);
Rade Bozic85660f42010-01-28 12:47:07 -0800428 else
Jan Glauberc57db702016-04-11 17:28:35 +0200429 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
Rade Bozic85660f42010-01-28 12:47:07 -0800430
431 result = octeon_i2c_wait(i2c);
432 if (result)
433 return result;
434
Jan Glauberc57db702016-04-11 17:28:35 +0200435 data[i] = octeon_i2c_data_read(i2c);
David Daney886f6f82016-03-18 09:46:29 +0100436 if (recv_len && i == 0) {
437 if (data[i] > I2C_SMBUS_BLOCK_MAX + 1) {
438 dev_err(i2c->dev,
439 "%s: read len > I2C_SMBUS_BLOCK_MAX %d\n",
440 __func__, data[i]);
441 return -EPROTO;
442 }
443 length += data[i];
444 }
Rade Bozic85660f42010-01-28 12:47:07 -0800445 }
David Daney886f6f82016-03-18 09:46:29 +0100446 *rlength = length;
Rade Bozic85660f42010-01-28 12:47:07 -0800447 return 0;
448}
449
450/**
Jan Glauberbd7784c2016-03-07 16:10:44 +0100451 * octeon_i2c_xfer - The driver's master_xfer function
452 * @adap: Pointer to the i2c_adapter structure
453 * @msgs: Pointer to the messages to be processed
454 * @num: Length of the MSGS array
Rade Bozic85660f42010-01-28 12:47:07 -0800455 *
Jan Glauberbd7784c2016-03-07 16:10:44 +0100456 * Returns the number of messages processed, or a negative errno on failure.
Rade Bozic85660f42010-01-28 12:47:07 -0800457 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100458static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
Rade Bozic85660f42010-01-28 12:47:07 -0800459 int num)
460{
Rade Bozic85660f42010-01-28 12:47:07 -0800461 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
Jan Glauberdfcd8212016-03-18 09:46:26 +0100462 int i, ret = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800463
464 for (i = 0; ret == 0 && i < num; i++) {
Jan Glauberdfcd8212016-03-18 09:46:26 +0100465 struct i2c_msg *pmsg = &msgs[i];
466
Rade Bozic85660f42010-01-28 12:47:07 -0800467 dev_dbg(i2c->dev,
468 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
469 pmsg->flags & I2C_M_RD ? "read" : "write",
470 pmsg->len, pmsg->addr, i + 1, num);
471 if (pmsg->flags & I2C_M_RD)
472 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
David Daney886f6f82016-03-18 09:46:29 +0100473 &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
Rade Bozic85660f42010-01-28 12:47:07 -0800474 else
475 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
Jan Glauberdfcd8212016-03-18 09:46:26 +0100476 pmsg->len);
Rade Bozic85660f42010-01-28 12:47:07 -0800477 }
478 octeon_i2c_stop(i2c);
479
480 return (ret != 0) ? ret : num;
481}
482
483static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
484{
David Daney886f6f82016-03-18 09:46:29 +0100485 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
486 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
Rade Bozic85660f42010-01-28 12:47:07 -0800487}
488
489static const struct i2c_algorithm octeon_i2c_algo = {
490 .master_xfer = octeon_i2c_xfer,
491 .functionality = octeon_i2c_functionality,
492};
493
494static struct i2c_adapter octeon_i2c_ops = {
495 .owner = THIS_MODULE,
496 .name = "OCTEON adapter",
497 .algo = &octeon_i2c_algo,
Rade Bozic85660f42010-01-28 12:47:07 -0800498};
499
Bill Pemberton0b255e92012-11-27 15:59:38 -0500500static int octeon_i2c_probe(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800501{
Jan Glauberdfcd8212016-03-18 09:46:26 +0100502 struct device_node *node = pdev->dev.of_node;
Rade Bozic85660f42010-01-28 12:47:07 -0800503 struct resource *res_mem;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100504 struct octeon_i2c *i2c;
505 int irq, result = 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800506
507 /* All adaptors have an irq. */
508 irq = platform_get_irq(pdev, 0);
509 if (irq < 0)
510 return irq;
511
David Daneyf353a212012-07-05 18:12:39 +0200512 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
Rade Bozic85660f42010-01-28 12:47:07 -0800513 if (!i2c) {
Rade Bozic85660f42010-01-28 12:47:07 -0800514 result = -ENOMEM;
515 goto out;
516 }
517 i2c->dev = &pdev->dev;
Rade Bozic85660f42010-01-28 12:47:07 -0800518
519 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jan Glauber54108e52016-03-18 09:46:27 +0100520 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
521 if (IS_ERR(i2c->twsi_base)) {
522 result = PTR_ERR(i2c->twsi_base);
David Daneyf353a212012-07-05 18:12:39 +0200523 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800524 }
Rade Bozic85660f42010-01-28 12:47:07 -0800525
David Daneyf353a212012-07-05 18:12:39 +0200526 /*
527 * "clock-rate" is a legacy binding, the official binding is
528 * "clock-frequency". Try the official one first and then
529 * fall back if it doesn't exist.
530 */
Jan Glauberdfcd8212016-03-18 09:46:26 +0100531 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
532 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
David Daneyf353a212012-07-05 18:12:39 +0200533 dev_err(i2c->dev,
534 "no I2C 'clock-rate' or 'clock-frequency' property\n");
535 result = -ENXIO;
536 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800537 }
David Daneyf353a212012-07-05 18:12:39 +0200538
539 i2c->sys_freq = octeon_get_io_clock_rate();
540
Rade Bozic85660f42010-01-28 12:47:07 -0800541 init_waitqueue_head(&i2c->queue);
542
543 i2c->irq = irq;
544
David Daneyf353a212012-07-05 18:12:39 +0200545 result = devm_request_irq(&pdev->dev, i2c->irq,
546 octeon_i2c_isr, 0, DRV_NAME, i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800547 if (result < 0) {
548 dev_err(i2c->dev, "failed to attach interrupt\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 result = octeon_i2c_init_lowlevel(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800553 if (result) {
554 dev_err(i2c->dev, "init low level failed\n");
David Daneyf353a212012-07-05 18:12:39 +0200555 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800556 }
557
Jan Glauberdfcd8212016-03-18 09:46:26 +0100558 octeon_i2c_set_clock(i2c);
Rade Bozic85660f42010-01-28 12:47:07 -0800559
560 i2c->adap = octeon_i2c_ops;
Jan Glaubera035d712016-04-11 17:28:32 +0200561 i2c->adap.timeout = msecs_to_jiffies(2);
562 i2c->adap.retries = 5;
Rade Bozic85660f42010-01-28 12:47:07 -0800563 i2c->adap.dev.parent = &pdev->dev;
Jan Glauberdfcd8212016-03-18 09:46:26 +0100564 i2c->adap.dev.of_node = node;
Rade Bozic85660f42010-01-28 12:47:07 -0800565 i2c_set_adapdata(&i2c->adap, i2c);
566 platform_set_drvdata(pdev, i2c);
567
David Daneyf353a212012-07-05 18:12:39 +0200568 result = i2c_add_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800569 if (result < 0) {
570 dev_err(i2c->dev, "failed to add adapter\n");
Doug Anderson55827f42013-02-15 13:18:35 +0000571 goto out;
Rade Bozic85660f42010-01-28 12:47:07 -0800572 }
Jan Glauberdfcd8212016-03-18 09:46:26 +0100573 dev_info(i2c->dev, "probed\n");
David Daneyf353a212012-07-05 18:12:39 +0200574 return 0;
Rade Bozic85660f42010-01-28 12:47:07 -0800575
Rade Bozic85660f42010-01-28 12:47:07 -0800576out:
577 return result;
578};
579
Bill Pemberton0b255e92012-11-27 15:59:38 -0500580static int octeon_i2c_remove(struct platform_device *pdev)
Rade Bozic85660f42010-01-28 12:47:07 -0800581{
582 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
583
584 i2c_del_adapter(&i2c->adap);
Rade Bozic85660f42010-01-28 12:47:07 -0800585 return 0;
586};
587
Jan Glauberdfcd8212016-03-18 09:46:26 +0100588static const struct of_device_id octeon_i2c_match[] = {
589 { .compatible = "cavium,octeon-3860-twsi", },
David Daneyf353a212012-07-05 18:12:39 +0200590 {},
591};
592MODULE_DEVICE_TABLE(of, octeon_i2c_match);
593
Rade Bozic85660f42010-01-28 12:47:07 -0800594static struct platform_driver octeon_i2c_driver = {
595 .probe = octeon_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500596 .remove = octeon_i2c_remove,
Rade Bozic85660f42010-01-28 12:47:07 -0800597 .driver = {
Rade Bozic85660f42010-01-28 12:47:07 -0800598 .name = DRV_NAME,
David Daneyf353a212012-07-05 18:12:39 +0200599 .of_match_table = octeon_i2c_match,
Rade Bozic85660f42010-01-28 12:47:07 -0800600 },
601};
602
Axel Lina3664b52012-01-12 20:32:04 +0100603module_platform_driver(octeon_i2c_driver);
Rade Bozic85660f42010-01-28 12:47:07 -0800604
605MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
606MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
607MODULE_LICENSE("GPL");