blob: 85cbe4b55578614ade02cec4472920e4b8ad7761 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ------------------------------------------------------------------------- */
2/* i2c-iop3xx.c i2c driver algorithms for Intel XScale IOP3xx & IXP46x */
3/* ------------------------------------------------------------------------- */
4/* Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd
5 * <Peter dot Milne at D hyphen TACQ dot com>
6 *
Rob Herring67ac88f2012-07-04 14:14:16 -05007 * With acknowledgements to i2c-algo-ibm_ocp.c by
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Ian DaSilva, MontaVista Software, Inc. idasilva@mvista.com
9 *
10 * And i2c-algo-pcf.c, which was created by Simon G. Vogl and Hans Berglund:
11 *
12 * Copyright (C) 1995-1997 Simon G. Vogl, 1998-2000 Hans Berglund
Rob Herring67ac88f2012-07-04 14:14:16 -050013 *
Dan Williamsfbd9a6d2005-11-01 22:31:12 +000014 * And which acknowledged Kyösti Mälkki <kmalkki@cc.hut.fi>,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Frodo Looijaard <frodol@dds.nl>, Martin Bailey<mbailey@littlefeet-inc.com>
16 *
17 * Major cleanup by Deepak Saxena <dsaxena@plexity.net>, 01/2005:
18 *
19 * - Use driver model to pass per-chip info instead of hardcoding and #ifdefs
20 * - Use ioremap/__raw_readl/__raw_writel instead of direct dereference
21 * - Make it work with IXP46x chips
22 * - Cleanup function names, coding style, etc
23 *
Peter Milne39288e12006-07-01 17:03:20 +020024 * - writing to slave address causes latchup on iop331.
25 * fix: driver refuses to address self.
26 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, version 2.
30 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/interrupt.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/delay.h>
36#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/errno.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010038#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/i2c.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020040#include <linux/io.h>
Rob Herring701eb262012-07-04 14:16:12 -050041#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "i2c-iop3xx.h"
44
45/* global unit counter */
Jean Delvare60507092005-09-25 16:23:07 +020046static int i2c_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Rob Herring67ac88f2012-07-04 14:14:16 -050048static inline unsigned char
49iic_cook_addr(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 unsigned char addr;
52
Wolfram Sang515e2402016-04-03 20:44:53 +020053 addr = i2c_8bit_addr_from_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Rob Herring67ac88f2012-07-04 14:14:16 -050055 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Rob Herring67ac88f2012-07-04 14:14:16 -050058static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070059iop3xx_i2c_reset(struct i2c_algo_iop3xx_data *iop3xx_adap)
60{
61 /* Follows devman 9.3 */
62 __raw_writel(IOP3XX_ICR_UNIT_RESET, iop3xx_adap->ioaddr + CR_OFFSET);
63 __raw_writel(IOP3XX_ISR_CLEARBITS, iop3xx_adap->ioaddr + SR_OFFSET);
64 __raw_writel(0, iop3xx_adap->ioaddr + CR_OFFSET);
Rob Herring67ac88f2012-07-04 14:14:16 -050065}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Rob Herring67ac88f2012-07-04 14:14:16 -050067static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070068iop3xx_i2c_enable(struct i2c_algo_iop3xx_data *iop3xx_adap)
69{
70 u32 cr = IOP3XX_ICR_GCD | IOP3XX_ICR_SCLEN | IOP3XX_ICR_UE;
71
Rob Herring67ac88f2012-07-04 14:14:16 -050072 /*
Steven Cole44bbe872005-05-03 18:21:25 -060073 * Every time unit enable is asserted, GPOD needs to be cleared
Lennert Buytenhek72edd842006-09-18 23:23:07 +010074 * on IOP3XX to avoid data corruption on the bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
Dan Williams588ef762007-02-13 17:12:04 +010076#if defined(CONFIG_ARCH_IOP32X) || defined(CONFIG_ARCH_IOP33X)
Lennert Buytenhek72edd842006-09-18 23:23:07 +010077 if (iop3xx_adap->id == 0) {
Rob Herring701eb262012-07-04 14:16:12 -050078 gpio_set_value(7, 0);
79 gpio_set_value(6, 0);
Lennert Buytenhek72edd842006-09-18 23:23:07 +010080 } else {
Rob Herring701eb262012-07-04 14:16:12 -050081 gpio_set_value(5, 0);
82 gpio_set_value(4, 0);
Lennert Buytenhek72edd842006-09-18 23:23:07 +010083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#endif
85 /* NB SR bits not same position as CR IE bits :-( */
Rob Herring67ac88f2012-07-04 14:14:16 -050086 iop3xx_adap->SR_enabled =
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD |
88 IOP3XX_ISR_RXFULL | IOP3XX_ISR_TXEMPTY;
89
90 cr |= IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
91 IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE;
92
93 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
94}
95
Rob Herring67ac88f2012-07-04 14:14:16 -050096static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070097iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data *iop3xx_adap)
98{
99 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
Rob Herring67ac88f2012-07-04 14:14:16 -0500100
101 cr &= ~(IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 IOP3XX_ICR_MSTOP | IOP3XX_ICR_SCLEN);
103
104 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
105}
106
Rob Herring67ac88f2012-07-04 14:14:16 -0500107/*
108 * NB: the handler has to clear the source of the interrupt!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * Then it passes the SR flags of interest to BH via adap data
110 */
Rob Herring67ac88f2012-07-04 14:14:16 -0500111static irqreturn_t
112iop3xx_i2c_irq_handler(int this_irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct i2c_algo_iop3xx_data *iop3xx_adap = dev_id;
115 u32 sr = __raw_readl(iop3xx_adap->ioaddr + SR_OFFSET);
116
117 if ((sr &= iop3xx_adap->SR_enabled)) {
118 __raw_writel(sr, iop3xx_adap->ioaddr + SR_OFFSET);
119 iop3xx_adap->SR_received |= sr;
120 wake_up_interruptible(&iop3xx_adap->waitq);
121 }
122 return IRQ_HANDLED;
123}
124
125/* check all error conditions, clear them , report most important */
Rob Herring67ac88f2012-07-04 14:14:16 -0500126static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127iop3xx_i2c_error(u32 sr)
128{
129 int rc = 0;
130
131 if ((sr & IOP3XX_ISR_BERRD)) {
132 if ( !rc ) rc = -I2C_ERR_BERR;
133 }
134 if ((sr & IOP3XX_ISR_ALD)) {
Rob Herring67ac88f2012-07-04 14:14:16 -0500135 if ( !rc ) rc = -I2C_ERR_ALD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Rob Herring67ac88f2012-07-04 14:14:16 -0500137 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Rob Herring67ac88f2012-07-04 14:14:16 -0500140static inline u32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data *iop3xx_adap)
142{
143 unsigned long flags;
144 u32 sr;
145
146 spin_lock_irqsave(&iop3xx_adap->lock, flags);
147 sr = iop3xx_adap->SR_received;
148 iop3xx_adap->SR_received = 0;
149 spin_unlock_irqrestore(&iop3xx_adap->lock, flags);
150
151 return sr;
152}
153
154/*
155 * sleep until interrupted, then recover and analyse the SR
156 * saved by handler
157 */
158typedef int (* compare_func)(unsigned test, unsigned mask);
159/* returns 1 on correct comparison */
160
Rob Herring67ac88f2012-07-04 14:14:16 -0500161static int
162iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data *iop3xx_adap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 unsigned flags, unsigned* status,
164 compare_func compare)
165{
166 unsigned sr = 0;
167 int interrupted;
168 int done;
169 int rc = 0;
170
171 do {
172 interrupted = wait_event_interruptible_timeout (
173 iop3xx_adap->waitq,
Dan Williamsfbd9a6d2005-11-01 22:31:12 +0000174 (done = compare( sr = iop3xx_i2c_get_srstat(iop3xx_adap) ,flags )),
Kevin Hilman88a8e4a2013-06-28 14:35:52 -0700175 1 * HZ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 );
177 if ((rc = iop3xx_i2c_error(sr)) < 0) {
178 *status = sr;
179 return rc;
180 } else if (!interrupted) {
181 *status = sr;
182 return -ETIMEDOUT;
183 }
184 } while(!done);
185
186 *status = sr;
187
188 return 0;
189}
190
191/*
Rob Herring67ac88f2012-07-04 14:14:16 -0500192 * Concrete compare_funcs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
Rob Herring67ac88f2012-07-04 14:14:16 -0500194static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195all_bits_clear(unsigned test, unsigned mask)
196{
197 return (test & mask) == 0;
198}
199
Rob Herring67ac88f2012-07-04 14:14:16 -0500200static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201any_bits_set(unsigned test, unsigned mask)
202{
203 return (test & mask) != 0;
204}
205
Rob Herring67ac88f2012-07-04 14:14:16 -0500206static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
208{
Rob Herring67ac88f2012-07-04 14:14:16 -0500209 return iop3xx_i2c_wait_event(
210 iop3xx_adap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 IOP3XX_ISR_TXEMPTY | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
212 status, any_bits_set);
213}
214
Rob Herring67ac88f2012-07-04 14:14:16 -0500215static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
217{
Rob Herring67ac88f2012-07-04 14:14:16 -0500218 return iop3xx_i2c_wait_event(
219 iop3xx_adap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 IOP3XX_ISR_RXFULL | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
221 status, any_bits_set);
222}
223
Rob Herring67ac88f2012-07-04 14:14:16 -0500224static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
226{
Rob Herring67ac88f2012-07-04 14:14:16 -0500227 return iop3xx_i2c_wait_event(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 iop3xx_adap, IOP3XX_ISR_UNITBUSY, status, all_bits_clear);
229}
230
Rob Herring67ac88f2012-07-04 14:14:16 -0500231static int
232iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data *iop3xx_adap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct i2c_msg* msg)
234{
235 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
236 int status;
237 int rc;
238
Peter Milne39288e12006-07-01 17:03:20 +0200239 /* avoid writing to my slave address (hangs on 80331),
240 * forbidden in Intel developer manual
241 */
242 if (msg->addr == MYSAR) {
243 return -EBUSY;
244 }
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 __raw_writel(iic_cook_addr(msg), iop3xx_adap->ioaddr + DBR_OFFSET);
Rob Herring67ac88f2012-07-04 14:14:16 -0500247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
249 cr |= IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE;
250
251 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
252 rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
253
254 return rc;
255}
256
Rob Herring67ac88f2012-07-04 14:14:16 -0500257static int
258iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char byte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 int stop)
260{
261 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
262 int status;
263 int rc = 0;
264
265 __raw_writel(byte, iop3xx_adap->ioaddr + DBR_OFFSET);
266 cr &= ~IOP3XX_ICR_MSTART;
267 if (stop) {
268 cr |= IOP3XX_ICR_MSTOP;
269 } else {
270 cr &= ~IOP3XX_ICR_MSTOP;
271 }
272 cr |= IOP3XX_ICR_TBYTE;
273 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
274 rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
275
276 return rc;
Rob Herring67ac88f2012-07-04 14:14:16 -0500277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Rob Herring67ac88f2012-07-04 14:14:16 -0500279static int
280iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char* byte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int stop)
282{
283 unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
284 int status;
285 int rc = 0;
286
287 cr &= ~IOP3XX_ICR_MSTART;
288
289 if (stop) {
290 cr |= IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK;
291 } else {
292 cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
293 }
294 cr |= IOP3XX_ICR_TBYTE;
295 __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
296
297 rc = iop3xx_i2c_wait_rx_done(iop3xx_adap, &status);
298
299 *byte = __raw_readl(iop3xx_adap->ioaddr + DBR_OFFSET);
300
301 return rc;
302}
303
Rob Herring67ac88f2012-07-04 14:14:16 -0500304static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305iop3xx_i2c_writebytes(struct i2c_adapter *i2c_adap, const char *buf, int count)
306{
307 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
308 int ii;
309 int rc = 0;
310
Rob Herring67ac88f2012-07-04 14:14:16 -0500311 for (ii = 0; rc == 0 && ii != count; ++ii)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 rc = iop3xx_i2c_write_byte(iop3xx_adap, buf[ii], ii==count-1);
313 return rc;
314}
315
Rob Herring67ac88f2012-07-04 14:14:16 -0500316static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317iop3xx_i2c_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count)
318{
319 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
320 int ii;
321 int rc = 0;
322
323 for (ii = 0; rc == 0 && ii != count; ++ii)
324 rc = iop3xx_i2c_read_byte(iop3xx_adap, &buf[ii], ii==count-1);
Rob Herring67ac88f2012-07-04 14:14:16 -0500325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return rc;
327}
328
329/*
330 * Description: This function implements combined transactions. Combined
331 * transactions consist of combinations of reading and writing blocks of data.
332 * FROM THE SAME ADDRESS
333 * Each transfer (i.e. a read or a write) is separated by a repeated start
334 * condition.
335 */
Rob Herring67ac88f2012-07-04 14:14:16 -0500336static int
337iop3xx_i2c_handle_msg(struct i2c_adapter *i2c_adap, struct i2c_msg* pmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
340 int rc;
341
342 rc = iop3xx_i2c_send_target_addr(iop3xx_adap, pmsg);
343 if (rc < 0) {
344 return rc;
345 }
346
347 if ((pmsg->flags&I2C_M_RD)) {
348 return iop3xx_i2c_readbytes(i2c_adap, pmsg->buf, pmsg->len);
349 } else {
350 return iop3xx_i2c_writebytes(i2c_adap, pmsg->buf, pmsg->len);
351 }
352}
353
354/*
355 * master_xfer() - main read/write entry
356 */
Rob Herring67ac88f2012-07-04 14:14:16 -0500357static int
358iop3xx_i2c_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int num)
360{
361 struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
362 int im = 0;
363 int ret = 0;
364 int status;
365
366 iop3xx_i2c_wait_idle(iop3xx_adap, &status);
367 iop3xx_i2c_reset(iop3xx_adap);
368 iop3xx_i2c_enable(iop3xx_adap);
369
370 for (im = 0; ret == 0 && im != num; im++) {
371 ret = iop3xx_i2c_handle_msg(i2c_adap, &msgs[im]);
372 }
373
374 iop3xx_i2c_transaction_cleanup(iop3xx_adap);
Rob Herring67ac88f2012-07-04 14:14:16 -0500375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if(ret)
377 return ret;
378
Rob Herring67ac88f2012-07-04 14:14:16 -0500379 return im;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Rob Herring67ac88f2012-07-04 14:14:16 -0500382static u32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383iop3xx_i2c_func(struct i2c_adapter *adap)
384{
385 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
386}
387
Jean Delvare8f9082c2006-09-03 22:39:46 +0200388static const struct i2c_algorithm iop3xx_i2c_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 .master_xfer = iop3xx_i2c_master_xfer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 .functionality = iop3xx_i2c_func,
391};
392
Rob Herring67ac88f2012-07-04 14:14:16 -0500393static int
Russell King3ae5eae2005-11-09 22:32:44 +0000394iop3xx_i2c_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Russell King3ae5eae2005-11-09 22:32:44 +0000396 struct i2c_adapter *padapter = platform_get_drvdata(pdev);
Rob Herring67ac88f2012-07-04 14:14:16 -0500397 struct i2c_algo_iop3xx_data *adapter_data =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 (struct i2c_algo_iop3xx_data *)padapter->algo_data;
399 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
400 unsigned long cr = __raw_readl(adapter_data->ioaddr + CR_OFFSET);
401
402 /*
403 * Disable the actual HW unit
404 */
405 cr &= ~(IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
406 IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE);
407 __raw_writel(cr, adapter_data->ioaddr + CR_OFFSET);
408
Aaro Koskinen747fcc92010-11-29 12:33:39 +0200409 iounmap(adapter_data->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
411 kfree(adapter_data);
412 kfree(padapter);
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return 0;
415}
416
Rob Herring67ac88f2012-07-04 14:14:16 -0500417static int
Russell King3ae5eae2005-11-09 22:32:44 +0000418iop3xx_i2c_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct resource *res;
David Vrabel48944732006-01-19 17:56:29 +0000421 int ret, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct i2c_adapter *new_adapter;
423 struct i2c_algo_iop3xx_data *adapter_data;
424
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200425 new_adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!new_adapter) {
427 ret = -ENOMEM;
428 goto out;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200431 adapter_data = kzalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (!adapter_data) {
433 ret = -ENOMEM;
434 goto free_adapter;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
438 if (!res) {
439 ret = -ENODEV;
440 goto free_both;
441 }
442
443 if (!request_mem_region(res->start, IOP3XX_I2C_IO_SIZE, pdev->name)) {
444 ret = -EBUSY;
445 goto free_both;
446 }
447
448 /* set the adapter enumeration # */
449 adapter_data->id = i2c_id++;
450
Aaro Koskinen747fcc92010-11-29 12:33:39 +0200451 adapter_data->ioaddr = ioremap(res->start, IOP3XX_I2C_IO_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!adapter_data->ioaddr) {
453 ret = -ENOMEM;
454 goto release_region;
455 }
456
David Vrabel48944732006-01-19 17:56:29 +0000457 irq = platform_get_irq(pdev, 0);
458 if (irq < 0) {
459 ret = -ENXIO;
460 goto unmap;
461 }
462 ret = request_irq(irq, iop3xx_i2c_irq_handler, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 pdev->name, adapter_data);
Dan Williamsfbd9a6d2005-11-01 22:31:12 +0000464
465 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ret = -EIO;
467 goto unmap;
468 }
469
470 memcpy(new_adapter->name, pdev->name, strlen(pdev->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 new_adapter->owner = THIS_MODULE;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200472 new_adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 new_adapter->dev.parent = &pdev->dev;
Martin Michlmayr757ba4c2007-07-12 14:12:30 +0200474 new_adapter->nr = pdev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /*
477 * Default values...should these come in from board code?
478 */
Jean Delvare8a52c6b2009-03-28 21:34:43 +0100479 new_adapter->timeout = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 new_adapter->algo = &iop3xx_i2c_algo;
481
482 init_waitqueue_head(&adapter_data->waitq);
483 spin_lock_init(&adapter_data->lock);
484
485 iop3xx_i2c_reset(adapter_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 iop3xx_i2c_enable(adapter_data);
487
Russell King3ae5eae2005-11-09 22:32:44 +0000488 platform_set_drvdata(pdev, new_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 new_adapter->algo_data = adapter_data;
490
Martin Michlmayr757ba4c2007-07-12 14:12:30 +0200491 i2c_add_numbered_adapter(new_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 return 0;
494
495unmap:
Aaro Koskinen747fcc92010-11-29 12:33:39 +0200496 iounmap(adapter_data->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498release_region:
499 release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
500
501free_both:
502 kfree(adapter_data);
503
504free_adapter:
505 kfree(new_adapter);
506
507out:
508 return ret;
509}
510
511
Russell King3ae5eae2005-11-09 22:32:44 +0000512static struct platform_driver iop3xx_i2c_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 .probe = iop3xx_i2c_probe,
Russell King3ae5eae2005-11-09 22:32:44 +0000514 .remove = iop3xx_i2c_remove,
515 .driver = {
Russell King3ae5eae2005-11-09 22:32:44 +0000516 .name = "IOP3xx-I2C",
517 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518};
519
Axel Lina3664b52012-01-12 20:32:04 +0100520module_platform_driver(iop3xx_i2c_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522MODULE_AUTHOR("D-TACQ Solutions Ltd <www.d-tacq.com>");
523MODULE_DESCRIPTION("IOP3xx iic algorithm and driver");
524MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200525MODULE_ALIAS("platform:IOP3xx-I2C");