diff options
author | Nikolaus Voss <n.voss@weinmann.de> | 2011-11-08 05:49:46 -0500 |
---|---|---|
committer | Wolfram Sang <w.sang@pengutronix.de> | 2012-09-12 08:49:13 -0400 |
commit | fac368a040484293006bb488e67972aafcf88ec7 (patch) | |
tree | 9c132f85cd1a0df4cb1dedcd48d90d2bbb15e6b8 /drivers/i2c | |
parent | a879e9c34b93ee43f5caa7f94eb17e7af4f6ef50 (diff) |
i2c: at91: add new driver
This driver has the following properties compared to the old driver:
1. Support for multiple interfaces.
2. Interrupt driven I/O as opposed to polling/busy waiting.
3. Support for _one_ repeated start (Sr) condition, which is enough
for most real-world applications including all SMBus transfer types.
(The hardware does not support issuing arbitrary Sr conditions on the
bus.)
testing: SoC: at91sam9g45
- BQ20Z80 battery SMBus client.
- on a 2.6.38 kernel with several i2c clients (temp-sensor,
audio-codec, touchscreen-controller, w1-bridge, io-expanders)
Signed-off-by: Nikolaus Voss <n.voss@weinmann.de>
Reviewed-by: Felipe Balbi <balbi@ti.com>
Tested-by: Hubert Feurstein <h.feurstein@gmail.com>
Tested-by: Ludovic Desroches <ludovic.desroches@atmel.com>
Reviewed-by: Nicolas Ferre <nicolas.ferre@atmel.com>
[wsa: squashed with the following patches from Ludovic to have some flaws
fixed:
i2c: at91: use managed resources
i2c: at91: add warning about transmission issues for some devices
i2c: at91: use an id table for SoC dependent parameters
]
Signed-off-by: Ludovic Desroches <ludovic.desroches@atmel.com>
Signed-off-by: Wolfram Sang <w.sang@pengutronix.de>
Diffstat (limited to 'drivers/i2c')
-rw-r--r-- | drivers/i2c/busses/Kconfig | 17 | ||||
-rw-r--r-- | drivers/i2c/busses/Makefile | 1 | ||||
-rw-r--r-- | drivers/i2c/busses/i2c-at91.c | 505 |
3 files changed, 516 insertions, 7 deletions
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig index b4aaa1bd6728..da77c37caf1e 100644 --- a/drivers/i2c/busses/Kconfig +++ b/drivers/i2c/busses/Kconfig | |||
@@ -290,18 +290,21 @@ comment "I2C system bus drivers (mostly embedded / system-on-chip)" | |||
290 | 290 | ||
291 | config I2C_AT91 | 291 | config I2C_AT91 |
292 | tristate "Atmel AT91 I2C Two-Wire interface (TWI)" | 292 | tristate "Atmel AT91 I2C Two-Wire interface (TWI)" |
293 | depends on ARCH_AT91 && EXPERIMENTAL && BROKEN | 293 | depends on ARCH_AT91 && EXPERIMENTAL |
294 | help | 294 | help |
295 | This supports the use of the I2C interface on Atmel AT91 | 295 | This supports the use of the I2C interface on Atmel AT91 |
296 | processors. | 296 | processors. |
297 | 297 | ||
298 | This driver is BROKEN because the controller which it uses | 298 | A serious problem is that there is no documented way to issue |
299 | will easily trigger RX overrun and TX underrun errors. Using | 299 | repeated START conditions for more than two messages, as needed |
300 | low I2C clock rates may partially work around those issues | ||
301 | on some systems. Another serious problem is that there is no | ||
302 | documented way to issue repeated START conditions, as needed | ||
303 | to support combined I2C messages. Use the i2c-gpio driver | 300 | to support combined I2C messages. Use the i2c-gpio driver |
304 | unless your system can cope with those limitations. | 301 | unless your system can cope with this limitation. |
302 | |||
303 | Caution! at91rm9200, at91sam9261, at91sam9260, at91sam9263 devices | ||
304 | don't have clock stretching in transmission mode. For that reason, | ||
305 | you can encounter underrun issues causing premature stop sendings if | ||
306 | the latency to fill the transmission register is too long. If you | ||
307 | are facing this situation, use the i2c-gpio driver. | ||
305 | 308 | ||
306 | config I2C_AU1550 | 309 | config I2C_AU1550 |
307 | tristate "Au1550/Au1200/Au1300 SMBus interface" | 310 | tristate "Au1550/Au1200/Au1300 SMBus interface" |
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile index 4f46873282b8..ce3c2be7fb40 100644 --- a/drivers/i2c/busses/Makefile +++ b/drivers/i2c/busses/Makefile | |||
@@ -28,6 +28,7 @@ obj-$(CONFIG_I2C_HYDRA) += i2c-hydra.o | |||
28 | obj-$(CONFIG_I2C_POWERMAC) += i2c-powermac.o | 28 | obj-$(CONFIG_I2C_POWERMAC) += i2c-powermac.o |
29 | 29 | ||
30 | # Embedded system I2C/SMBus host controller drivers | 30 | # Embedded system I2C/SMBus host controller drivers |
31 | obj-$(CONFIG_I2C_AT91) += i2c-at91.o | ||
31 | obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o | 32 | obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o |
32 | obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o | 33 | obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o |
33 | obj-$(CONFIG_I2C_CPM) += i2c-cpm.o | 34 | obj-$(CONFIG_I2C_CPM) += i2c-cpm.o |
diff --git a/drivers/i2c/busses/i2c-at91.c b/drivers/i2c/busses/i2c-at91.c new file mode 100644 index 000000000000..78bcad031d6a --- /dev/null +++ b/drivers/i2c/busses/i2c-at91.c | |||
@@ -0,0 +1,505 @@ | |||
1 | /* | ||
2 | * i2c Support for Atmel's AT91 Two-Wire Interface (TWI) | ||
3 | * | ||
4 | * Copyright (C) 2011 Weinmann Medical GmbH | ||
5 | * Author: Nikolaus Voss <n.voss@weinmann.de> | ||
6 | * | ||
7 | * Evolved from original work by: | ||
8 | * Copyright (C) 2004 Rick Bronson | ||
9 | * Converted to 2.6 by Andrew Victor <andrew@sanpeople.com> | ||
10 | * | ||
11 | * Borrowed heavily from original work by: | ||
12 | * Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com> | ||
13 | * | ||
14 | * This program is free software; you can redistribute it and/or modify | ||
15 | * it under the terms of the GNU General Public License as published by | ||
16 | * the Free Software Foundation; either version 2 of the License, or | ||
17 | * (at your option) any later version. | ||
18 | */ | ||
19 | |||
20 | #include <linux/clk.h> | ||
21 | #include <linux/completion.h> | ||
22 | #include <linux/err.h> | ||
23 | #include <linux/i2c.h> | ||
24 | #include <linux/interrupt.h> | ||
25 | #include <linux/io.h> | ||
26 | #include <linux/module.h> | ||
27 | #include <linux/platform_device.h> | ||
28 | #include <linux/slab.h> | ||
29 | |||
30 | #define TWI_CLK_HZ 100000 /* max 400 Kbits/s */ | ||
31 | #define AT91_I2C_TIMEOUT msecs_to_jiffies(100) /* transfer timeout */ | ||
32 | |||
33 | /* AT91 TWI register definitions */ | ||
34 | #define AT91_TWI_CR 0x0000 /* Control Register */ | ||
35 | #define AT91_TWI_START 0x0001 /* Send a Start Condition */ | ||
36 | #define AT91_TWI_STOP 0x0002 /* Send a Stop Condition */ | ||
37 | #define AT91_TWI_MSEN 0x0004 /* Master Transfer Enable */ | ||
38 | #define AT91_TWI_SVDIS 0x0020 /* Slave Transfer Disable */ | ||
39 | #define AT91_TWI_SWRST 0x0080 /* Software Reset */ | ||
40 | |||
41 | #define AT91_TWI_MMR 0x0004 /* Master Mode Register */ | ||
42 | #define AT91_TWI_IADRSZ_1 0x0100 /* Internal Device Address Size */ | ||
43 | #define AT91_TWI_MREAD 0x1000 /* Master Read Direction */ | ||
44 | |||
45 | #define AT91_TWI_IADR 0x000c /* Internal Address Register */ | ||
46 | |||
47 | #define AT91_TWI_CWGR 0x0010 /* Clock Waveform Generator Reg */ | ||
48 | |||
49 | #define AT91_TWI_SR 0x0020 /* Status Register */ | ||
50 | #define AT91_TWI_TXCOMP 0x0001 /* Transmission Complete */ | ||
51 | #define AT91_TWI_RXRDY 0x0002 /* Receive Holding Register Ready */ | ||
52 | #define AT91_TWI_TXRDY 0x0004 /* Transmit Holding Register Ready */ | ||
53 | |||
54 | #define AT91_TWI_OVRE 0x0040 /* Overrun Error */ | ||
55 | #define AT91_TWI_UNRE 0x0080 /* Underrun Error */ | ||
56 | #define AT91_TWI_NACK 0x0100 /* Not Acknowledged */ | ||
57 | |||
58 | #define AT91_TWI_IER 0x0024 /* Interrupt Enable Register */ | ||
59 | #define AT91_TWI_IDR 0x0028 /* Interrupt Disable Register */ | ||
60 | #define AT91_TWI_IMR 0x002c /* Interrupt Mask Register */ | ||
61 | #define AT91_TWI_RHR 0x0030 /* Receive Holding Register */ | ||
62 | #define AT91_TWI_THR 0x0034 /* Transmit Holding Register */ | ||
63 | |||
64 | struct at91_twi_pdata { | ||
65 | unsigned clk_max_div; | ||
66 | unsigned clk_offset; | ||
67 | bool has_unre_flag; | ||
68 | }; | ||
69 | |||
70 | struct at91_twi_dev { | ||
71 | struct device *dev; | ||
72 | void __iomem *base; | ||
73 | struct completion cmd_complete; | ||
74 | struct clk *clk; | ||
75 | u8 *buf; | ||
76 | size_t buf_len; | ||
77 | struct i2c_msg *msg; | ||
78 | int irq; | ||
79 | unsigned transfer_status; | ||
80 | struct i2c_adapter adapter; | ||
81 | unsigned twi_cwgr_reg; | ||
82 | struct at91_twi_pdata *pdata; | ||
83 | }; | ||
84 | |||
85 | static unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg) | ||
86 | { | ||
87 | return readl_relaxed(dev->base + reg); | ||
88 | } | ||
89 | |||
90 | static void at91_twi_write(struct at91_twi_dev *dev, unsigned reg, unsigned val) | ||
91 | { | ||
92 | writel_relaxed(val, dev->base + reg); | ||
93 | } | ||
94 | |||
95 | static void at91_disable_twi_interrupts(struct at91_twi_dev *dev) | ||
96 | { | ||
97 | at91_twi_write(dev, AT91_TWI_IDR, | ||
98 | AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY); | ||
99 | } | ||
100 | |||
101 | static void at91_init_twi_bus(struct at91_twi_dev *dev) | ||
102 | { | ||
103 | at91_disable_twi_interrupts(dev); | ||
104 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST); | ||
105 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN); | ||
106 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS); | ||
107 | at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg); | ||
108 | } | ||
109 | |||
110 | /* | ||
111 | * Calculate symmetric clock as stated in datasheet: | ||
112 | * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset)) | ||
113 | */ | ||
114 | static void __devinit at91_calc_twi_clock(struct at91_twi_dev *dev, int twi_clk) | ||
115 | { | ||
116 | int ckdiv, cdiv, div; | ||
117 | struct at91_twi_pdata *pdata = dev->pdata; | ||
118 | int offset = pdata->clk_offset; | ||
119 | int max_ckdiv = pdata->clk_max_div; | ||
120 | |||
121 | div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk), | ||
122 | 2 * twi_clk) - offset); | ||
123 | ckdiv = fls(div >> 8); | ||
124 | cdiv = div >> ckdiv; | ||
125 | |||
126 | if (ckdiv > max_ckdiv) { | ||
127 | dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n", | ||
128 | ckdiv, max_ckdiv); | ||
129 | ckdiv = max_ckdiv; | ||
130 | cdiv = 255; | ||
131 | } | ||
132 | |||
133 | dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv; | ||
134 | dev_dbg(dev->dev, "cdiv %d ckdiv %d\n", cdiv, ckdiv); | ||
135 | } | ||
136 | |||
137 | static void at91_twi_write_next_byte(struct at91_twi_dev *dev) | ||
138 | { | ||
139 | if (dev->buf_len <= 0) | ||
140 | return; | ||
141 | |||
142 | at91_twi_write(dev, AT91_TWI_THR, *dev->buf); | ||
143 | |||
144 | /* send stop when last byte has been written */ | ||
145 | if (--dev->buf_len == 0) | ||
146 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP); | ||
147 | |||
148 | dev_dbg(dev->dev, "wrote 0x%x, to go %d\n", *dev->buf, dev->buf_len); | ||
149 | |||
150 | ++dev->buf; | ||
151 | } | ||
152 | |||
153 | static void at91_twi_read_next_byte(struct at91_twi_dev *dev) | ||
154 | { | ||
155 | if (dev->buf_len <= 0) | ||
156 | return; | ||
157 | |||
158 | *dev->buf = at91_twi_read(dev, AT91_TWI_RHR) & 0xff; | ||
159 | --dev->buf_len; | ||
160 | |||
161 | /* handle I2C_SMBUS_BLOCK_DATA */ | ||
162 | if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) { | ||
163 | dev->msg->flags &= ~I2C_M_RECV_LEN; | ||
164 | dev->buf_len += *dev->buf; | ||
165 | dev->msg->len = dev->buf_len + 1; | ||
166 | dev_dbg(dev->dev, "received block length %d\n", dev->buf_len); | ||
167 | } | ||
168 | |||
169 | /* send stop if second but last byte has been read */ | ||
170 | if (dev->buf_len == 1) | ||
171 | at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP); | ||
172 | |||
173 | dev_dbg(dev->dev, "read 0x%x, to go %d\n", *dev->buf, dev->buf_len); | ||
174 | |||
175 | ++dev->buf; | ||
176 | } | ||
177 | |||
178 | static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id) | ||
179 | { | ||
180 | struct at91_twi_dev *dev = dev_id; | ||
181 | const unsigned status = at91_twi_read(dev, AT91_TWI_SR); | ||
182 | const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR); | ||
183 | |||
184 | if (!irqstatus) | ||
185 | return IRQ_NONE; | ||
186 | else if (irqstatus & AT91_TWI_RXRDY) | ||
187 | at91_twi_read_next_byte(dev); | ||
188 | else if (irqstatus & AT91_TWI_TXRDY) | ||
189 | at91_twi_write_next_byte(dev); | ||
190 | |||
191 | /* catch error flags */ | ||
192 | dev->transfer_status |= status; | ||
193 | |||
194 | if (irqstatus & AT91_TWI_TXCOMP) { | ||
195 | at91_disable_twi_interrupts(dev); | ||
196 | complete(&dev->cmd_complete); | ||
197 | } | ||
198 | |||
199 | return IRQ_HANDLED; | ||
200 | } | ||
201 | |||
202 | static int at91_do_twi_transfer(struct at91_twi_dev *dev) | ||
203 | { | ||
204 | int ret; | ||
205 | bool has_unre_flag = dev->pdata->has_unre_flag; | ||
206 | |||
207 | dev_dbg(dev->dev, "transfer: %s %d bytes.\n", | ||
208 | (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len); | ||
209 | |||
210 | INIT_COMPLETION(dev->cmd_complete); | ||
211 | dev->transfer_status = 0; | ||
212 | if (dev->msg->flags & I2C_M_RD) { | ||
213 | unsigned start_flags = AT91_TWI_START; | ||
214 | |||
215 | if (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY) { | ||
216 | dev_err(dev->dev, "RXRDY still set!"); | ||
217 | at91_twi_read(dev, AT91_TWI_RHR); | ||
218 | } | ||
219 | |||
220 | /* if only one byte is to be read, immediately stop transfer */ | ||
221 | if (dev->buf_len <= 1 && !(dev->msg->flags & I2C_M_RECV_LEN)) | ||
222 | start_flags |= AT91_TWI_STOP; | ||
223 | at91_twi_write(dev, AT91_TWI_CR, start_flags); | ||
224 | at91_twi_write(dev, AT91_TWI_IER, | ||
225 | AT91_TWI_TXCOMP | AT91_TWI_RXRDY); | ||
226 | } else { | ||
227 | at91_twi_write_next_byte(dev); | ||
228 | at91_twi_write(dev, AT91_TWI_IER, | ||
229 | AT91_TWI_TXCOMP | AT91_TWI_TXRDY); | ||
230 | } | ||
231 | |||
232 | ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, | ||
233 | dev->adapter.timeout); | ||
234 | if (ret == 0) { | ||
235 | dev_err(dev->dev, "controller timed out\n"); | ||
236 | at91_init_twi_bus(dev); | ||
237 | return -ETIMEDOUT; | ||
238 | } | ||
239 | if (dev->transfer_status & AT91_TWI_NACK) { | ||
240 | dev_dbg(dev->dev, "received nack\n"); | ||
241 | return -EREMOTEIO; | ||
242 | } | ||
243 | if (dev->transfer_status & AT91_TWI_OVRE) { | ||
244 | dev_err(dev->dev, "overrun while reading\n"); | ||
245 | return -EIO; | ||
246 | } | ||
247 | if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) { | ||
248 | dev_err(dev->dev, "underrun while writing\n"); | ||
249 | return -EIO; | ||
250 | } | ||
251 | dev_dbg(dev->dev, "transfer complete\n"); | ||
252 | |||
253 | return 0; | ||
254 | } | ||
255 | |||
256 | static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num) | ||
257 | { | ||
258 | struct at91_twi_dev *dev = i2c_get_adapdata(adap); | ||
259 | int ret; | ||
260 | unsigned int_addr_flag = 0; | ||
261 | struct i2c_msg *m_start = msg; | ||
262 | |||
263 | dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num); | ||
264 | |||
265 | /* | ||
266 | * The hardware can handle at most two messages concatenated by a | ||
267 | * repeated start via it's internal address feature. | ||
268 | */ | ||
269 | if (num > 2) { | ||
270 | dev_err(dev->dev, | ||
271 | "cannot handle more than two concatenated messages.\n"); | ||
272 | return 0; | ||
273 | } else if (num == 2) { | ||
274 | int internal_address = 0; | ||
275 | int i; | ||
276 | |||
277 | if (msg->flags & I2C_M_RD) { | ||
278 | dev_err(dev->dev, "first transfer must be write.\n"); | ||
279 | return -EINVAL; | ||
280 | } | ||
281 | if (msg->len > 3) { | ||
282 | dev_err(dev->dev, "first message size must be <= 3.\n"); | ||
283 | return -EINVAL; | ||
284 | } | ||
285 | |||
286 | /* 1st msg is put into the internal address, start with 2nd */ | ||
287 | m_start = &msg[1]; | ||
288 | for (i = 0; i < msg->len; ++i) { | ||
289 | const unsigned addr = msg->buf[msg->len - 1 - i]; | ||
290 | |||
291 | internal_address |= addr << (8 * i); | ||
292 | int_addr_flag += AT91_TWI_IADRSZ_1; | ||
293 | } | ||
294 | at91_twi_write(dev, AT91_TWI_IADR, internal_address); | ||
295 | } | ||
296 | |||
297 | at91_twi_write(dev, AT91_TWI_MMR, (m_start->addr << 16) | int_addr_flag | ||
298 | | ((m_start->flags & I2C_M_RD) ? AT91_TWI_MREAD : 0)); | ||
299 | |||
300 | dev->buf_len = m_start->len; | ||
301 | dev->buf = m_start->buf; | ||
302 | dev->msg = m_start; | ||
303 | |||
304 | ret = at91_do_twi_transfer(dev); | ||
305 | |||
306 | return (ret < 0) ? ret : num; | ||
307 | } | ||
308 | |||
309 | static u32 at91_twi_func(struct i2c_adapter *adapter) | ||
310 | { | ||
311 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | ||
312 | | I2C_FUNC_SMBUS_READ_BLOCK_DATA; | ||
313 | } | ||
314 | |||
315 | static struct i2c_algorithm at91_twi_algorithm = { | ||
316 | .master_xfer = at91_twi_xfer, | ||
317 | .functionality = at91_twi_func, | ||
318 | }; | ||
319 | |||
320 | static struct at91_twi_pdata at91rm9200_config = { | ||
321 | .clk_max_div = 5, | ||
322 | .clk_offset = 3, | ||
323 | .has_unre_flag = true, | ||
324 | }; | ||
325 | |||
326 | static struct at91_twi_pdata at91sam9261_config = { | ||
327 | .clk_max_div = 5, | ||
328 | .clk_offset = 4, | ||
329 | .has_unre_flag = false, | ||
330 | }; | ||
331 | |||
332 | static struct at91_twi_pdata at91sam9260_config = { | ||
333 | .clk_max_div = 7, | ||
334 | .clk_offset = 4, | ||
335 | .has_unre_flag = false, | ||
336 | }; | ||
337 | |||
338 | static struct at91_twi_pdata at91sam9g20_config = { | ||
339 | .clk_max_div = 7, | ||
340 | .clk_offset = 4, | ||
341 | .has_unre_flag = false, | ||
342 | }; | ||
343 | |||
344 | static struct at91_twi_pdata at91sam9g10_config = { | ||
345 | .clk_max_div = 7, | ||
346 | .clk_offset = 4, | ||
347 | .has_unre_flag = false, | ||
348 | }; | ||
349 | |||
350 | static const struct platform_device_id at91_twi_devtypes[] = { | ||
351 | { | ||
352 | .name = "i2c-at91rm9200", | ||
353 | .driver_data = (unsigned long) &at91rm9200_config, | ||
354 | }, { | ||
355 | .name = "i2c-at91sam9261", | ||
356 | .driver_data = (unsigned long) &at91sam9261_config, | ||
357 | }, { | ||
358 | .name = "i2c-at91sam9260", | ||
359 | .driver_data = (unsigned long) &at91sam9260_config, | ||
360 | }, { | ||
361 | .name = "i2c-at91sam9g20", | ||
362 | .driver_data = (unsigned long) &at91sam9g20_config, | ||
363 | }, { | ||
364 | .name = "i2c-at91sam9g10", | ||
365 | .driver_data = (unsigned long) &at91sam9g10_config, | ||
366 | }, { | ||
367 | /* sentinel */ | ||
368 | } | ||
369 | }; | ||
370 | |||
371 | static int __devinit at91_twi_probe(struct platform_device *pdev) | ||
372 | { | ||
373 | struct at91_twi_dev *dev; | ||
374 | struct resource *mem; | ||
375 | int rc; | ||
376 | |||
377 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); | ||
378 | if (!dev) | ||
379 | return -ENOMEM; | ||
380 | init_completion(&dev->cmd_complete); | ||
381 | dev->dev = &pdev->dev; | ||
382 | |||
383 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); | ||
384 | if (!mem) | ||
385 | return -ENODEV; | ||
386 | |||
387 | dev->pdata = at91_twi_get_driver_data(pdev); | ||
388 | if (!dev->pdata) | ||
389 | return -ENODEV; | ||
390 | |||
391 | dev->base = devm_request_and_ioremap(&pdev->dev, mem); | ||
392 | if (!dev->base) | ||
393 | return -EBUSY; | ||
394 | |||
395 | dev->irq = platform_get_irq(pdev, 0); | ||
396 | if (dev->irq < 0) | ||
397 | return dev->irq; | ||
398 | |||
399 | rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0, | ||
400 | dev_name(dev->dev), dev); | ||
401 | if (rc) { | ||
402 | dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc); | ||
403 | return rc; | ||
404 | } | ||
405 | |||
406 | platform_set_drvdata(pdev, dev); | ||
407 | |||
408 | dev->clk = devm_clk_get(dev->dev, NULL); | ||
409 | if (IS_ERR(dev->clk)) { | ||
410 | dev_err(dev->dev, "no clock defined\n"); | ||
411 | return -ENODEV; | ||
412 | } | ||
413 | clk_prepare_enable(dev->clk); | ||
414 | |||
415 | at91_calc_twi_clock(dev, TWI_CLK_HZ); | ||
416 | at91_init_twi_bus(dev); | ||
417 | |||
418 | snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91"); | ||
419 | i2c_set_adapdata(&dev->adapter, dev); | ||
420 | dev->adapter.owner = THIS_MODULE; | ||
421 | dev->adapter.class = I2C_CLASS_HWMON; | ||
422 | dev->adapter.algo = &at91_twi_algorithm; | ||
423 | dev->adapter.dev.parent = dev->dev; | ||
424 | dev->adapter.nr = pdev->id; | ||
425 | dev->adapter.timeout = AT91_I2C_TIMEOUT; | ||
426 | |||
427 | rc = i2c_add_numbered_adapter(&dev->adapter); | ||
428 | if (rc) { | ||
429 | dev_err(dev->dev, "Adapter %s registration failed\n", | ||
430 | dev->adapter.name); | ||
431 | clk_disable_unprepare(dev->clk); | ||
432 | return rc; | ||
433 | } | ||
434 | |||
435 | dev_info(dev->dev, "AT91 i2c bus driver.\n"); | ||
436 | return 0; | ||
437 | } | ||
438 | |||
439 | static int __devexit at91_twi_remove(struct platform_device *pdev) | ||
440 | { | ||
441 | struct at91_twi_dev *dev = platform_get_drvdata(pdev); | ||
442 | int rc; | ||
443 | |||
444 | rc = i2c_del_adapter(&dev->adapter); | ||
445 | clk_disable_unprepare(dev->clk); | ||
446 | |||
447 | return rc; | ||
448 | } | ||
449 | |||
450 | #ifdef CONFIG_PM | ||
451 | |||
452 | static int at91_twi_runtime_suspend(struct device *dev) | ||
453 | { | ||
454 | struct at91_twi_dev *twi_dev = dev_get_drvdata(dev); | ||
455 | |||
456 | clk_disable(twi_dev->clk); | ||
457 | |||
458 | return 0; | ||
459 | } | ||
460 | |||
461 | static int at91_twi_runtime_resume(struct device *dev) | ||
462 | { | ||
463 | struct at91_twi_dev *twi_dev = dev_get_drvdata(dev); | ||
464 | |||
465 | return clk_enable(twi_dev->clk); | ||
466 | } | ||
467 | |||
468 | static const struct dev_pm_ops at91_twi_pm = { | ||
469 | .runtime_suspend = at91_twi_runtime_suspend, | ||
470 | .runtime_resume = at91_twi_runtime_resume, | ||
471 | }; | ||
472 | |||
473 | #define at91_twi_pm_ops (&at91_twi_pm) | ||
474 | #else | ||
475 | #define at91_twi_pm_ops NULL | ||
476 | #endif | ||
477 | |||
478 | static struct platform_driver at91_twi_driver = { | ||
479 | .probe = at91_twi_probe, | ||
480 | .remove = __devexit_p(at91_twi_remove), | ||
481 | .id_table = at91_twi_devtypes, | ||
482 | .driver = { | ||
483 | .name = "at91_i2c", | ||
484 | .owner = THIS_MODULE, | ||
485 | .pm = at91_twi_pm_ops, | ||
486 | }, | ||
487 | }; | ||
488 | |||
489 | static int __init at91_twi_init(void) | ||
490 | { | ||
491 | return platform_driver_register(&at91_twi_driver); | ||
492 | } | ||
493 | |||
494 | static void __exit at91_twi_exit(void) | ||
495 | { | ||
496 | platform_driver_unregister(&at91_twi_driver); | ||
497 | } | ||
498 | |||
499 | subsys_initcall(at91_twi_init); | ||
500 | module_exit(at91_twi_exit); | ||
501 | |||
502 | MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>"); | ||
503 | MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91"); | ||
504 | MODULE_LICENSE("GPL"); | ||
505 | MODULE_ALIAS("platform:at91_i2c"); | ||