1 // SPDX-License-Identifier: GPL-2.0
3 * i2c Support for Atmel's AT91 Two-Wire Interface (TWI)
5 * Copyright (C) 2011 Weinmann Medical GmbH
6 * Author: Nikolaus Voss <n.voss@weinmann.de>
8 * Evolved from original work by:
9 * Copyright (C) 2004 Rick Bronson
10 * Converted to 2.6 by Andrew Victor <andrew@sanpeople.com>
12 * Borrowed heavily from original work by:
13 * Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
16 #include <linux/clk.h>
17 #include <linux/err.h>
18 #include <linux/i2c.h>
20 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/pinctrl/consumer.h>
29 unsigned at91_twi_read(struct at91_twi_dev
*dev
, unsigned reg
)
31 return readl_relaxed(dev
->base
+ reg
);
34 void at91_twi_write(struct at91_twi_dev
*dev
, unsigned reg
, unsigned val
)
36 writel_relaxed(val
, dev
->base
+ reg
);
39 void at91_disable_twi_interrupts(struct at91_twi_dev
*dev
)
41 at91_twi_write(dev
, AT91_TWI_IDR
, AT91_TWI_INT_MASK
);
44 void at91_twi_irq_save(struct at91_twi_dev
*dev
)
46 dev
->imr
= at91_twi_read(dev
, AT91_TWI_IMR
) & AT91_TWI_INT_MASK
;
47 at91_disable_twi_interrupts(dev
);
50 void at91_twi_irq_restore(struct at91_twi_dev
*dev
)
52 at91_twi_write(dev
, AT91_TWI_IER
, dev
->imr
);
55 void at91_init_twi_bus(struct at91_twi_dev
*dev
)
57 at91_disable_twi_interrupts(dev
);
58 at91_twi_write(dev
, AT91_TWI_CR
, AT91_TWI_SWRST
);
59 if (dev
->slave_detected
)
60 at91_init_twi_bus_slave(dev
);
62 at91_init_twi_bus_master(dev
);
65 static struct at91_twi_pdata at91rm9200_config
= {
68 .has_unre_flag
= true,
70 .has_hold_field
= false,
73 static struct at91_twi_pdata at91sam9261_config
= {
76 .has_unre_flag
= false,
78 .has_hold_field
= false,
81 static struct at91_twi_pdata at91sam9260_config
= {
84 .has_unre_flag
= false,
86 .has_hold_field
= false,
89 static struct at91_twi_pdata at91sam9g20_config
= {
92 .has_unre_flag
= false,
94 .has_hold_field
= false,
97 static struct at91_twi_pdata at91sam9g10_config
= {
100 .has_unre_flag
= false,
101 .has_alt_cmd
= false,
102 .has_hold_field
= false,
105 static const struct platform_device_id at91_twi_devtypes
[] = {
107 .name
= "i2c-at91rm9200",
108 .driver_data
= (unsigned long) &at91rm9200_config
,
110 .name
= "i2c-at91sam9261",
111 .driver_data
= (unsigned long) &at91sam9261_config
,
113 .name
= "i2c-at91sam9260",
114 .driver_data
= (unsigned long) &at91sam9260_config
,
116 .name
= "i2c-at91sam9g20",
117 .driver_data
= (unsigned long) &at91sam9g20_config
,
119 .name
= "i2c-at91sam9g10",
120 .driver_data
= (unsigned long) &at91sam9g10_config
,
126 #if defined(CONFIG_OF)
127 static struct at91_twi_pdata at91sam9x5_config
= {
130 .has_unre_flag
= false,
131 .has_alt_cmd
= false,
132 .has_hold_field
= false,
135 static struct at91_twi_pdata sama5d4_config
= {
138 .has_unre_flag
= false,
139 .has_alt_cmd
= false,
140 .has_hold_field
= true,
143 static struct at91_twi_pdata sama5d2_config
= {
146 .has_unre_flag
= true,
148 .has_hold_field
= true,
151 static const struct of_device_id atmel_twi_dt_ids
[] = {
153 .compatible
= "atmel,at91rm9200-i2c",
154 .data
= &at91rm9200_config
,
156 .compatible
= "atmel,at91sam9260-i2c",
157 .data
= &at91sam9260_config
,
159 .compatible
= "atmel,at91sam9261-i2c",
160 .data
= &at91sam9261_config
,
162 .compatible
= "atmel,at91sam9g20-i2c",
163 .data
= &at91sam9g20_config
,
165 .compatible
= "atmel,at91sam9g10-i2c",
166 .data
= &at91sam9g10_config
,
168 .compatible
= "atmel,at91sam9x5-i2c",
169 .data
= &at91sam9x5_config
,
171 .compatible
= "atmel,sama5d4-i2c",
172 .data
= &sama5d4_config
,
174 .compatible
= "atmel,sama5d2-i2c",
175 .data
= &sama5d2_config
,
180 MODULE_DEVICE_TABLE(of
, atmel_twi_dt_ids
);
183 static struct at91_twi_pdata
*at91_twi_get_driver_data(
184 struct platform_device
*pdev
)
186 if (pdev
->dev
.of_node
) {
187 const struct of_device_id
*match
;
188 match
= of_match_node(atmel_twi_dt_ids
, pdev
->dev
.of_node
);
191 return (struct at91_twi_pdata
*)match
->data
;
193 return (struct at91_twi_pdata
*) platform_get_device_id(pdev
)->driver_data
;
196 static int at91_twi_probe(struct platform_device
*pdev
)
198 struct at91_twi_dev
*dev
;
199 struct resource
*mem
;
203 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
207 dev
->dev
= &pdev
->dev
;
209 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
212 phy_addr
= mem
->start
;
214 dev
->pdata
= at91_twi_get_driver_data(pdev
);
218 dev
->base
= devm_ioremap_resource(&pdev
->dev
, mem
);
219 if (IS_ERR(dev
->base
))
220 return PTR_ERR(dev
->base
);
222 dev
->irq
= platform_get_irq(pdev
, 0);
226 platform_set_drvdata(pdev
, dev
);
228 dev
->clk
= devm_clk_get(dev
->dev
, NULL
);
229 if (IS_ERR(dev
->clk
)) {
230 dev_err(dev
->dev
, "no clock defined\n");
233 clk_prepare_enable(dev
->clk
);
235 snprintf(dev
->adapter
.name
, sizeof(dev
->adapter
.name
), "AT91");
236 i2c_set_adapdata(&dev
->adapter
, dev
);
237 dev
->adapter
.owner
= THIS_MODULE
;
238 dev
->adapter
.class = I2C_CLASS_DEPRECATED
;
239 dev
->adapter
.dev
.parent
= dev
->dev
;
240 dev
->adapter
.nr
= pdev
->id
;
241 dev
->adapter
.timeout
= AT91_I2C_TIMEOUT
;
242 dev
->adapter
.dev
.of_node
= pdev
->dev
.of_node
;
244 dev
->slave_detected
= i2c_detect_slave_mode(&pdev
->dev
);
246 if (dev
->slave_detected
)
247 rc
= at91_twi_probe_slave(pdev
, phy_addr
, dev
);
249 rc
= at91_twi_probe_master(pdev
, phy_addr
, dev
);
253 at91_init_twi_bus(dev
);
255 pm_runtime_set_autosuspend_delay(dev
->dev
, AUTOSUSPEND_TIMEOUT
);
256 pm_runtime_use_autosuspend(dev
->dev
);
257 pm_runtime_set_active(dev
->dev
);
258 pm_runtime_enable(dev
->dev
);
260 rc
= i2c_add_numbered_adapter(&dev
->adapter
);
262 clk_disable_unprepare(dev
->clk
);
264 pm_runtime_disable(dev
->dev
);
265 pm_runtime_set_suspended(dev
->dev
);
270 dev_info(dev
->dev
, "AT91 i2c bus driver (hw version: %#x).\n",
271 at91_twi_read(dev
, AT91_TWI_VER
));
275 static int at91_twi_remove(struct platform_device
*pdev
)
277 struct at91_twi_dev
*dev
= platform_get_drvdata(pdev
);
279 i2c_del_adapter(&dev
->adapter
);
280 clk_disable_unprepare(dev
->clk
);
282 pm_runtime_disable(dev
->dev
);
283 pm_runtime_set_suspended(dev
->dev
);
290 static int at91_twi_runtime_suspend(struct device
*dev
)
292 struct at91_twi_dev
*twi_dev
= dev_get_drvdata(dev
);
294 clk_disable_unprepare(twi_dev
->clk
);
296 pinctrl_pm_select_sleep_state(dev
);
301 static int at91_twi_runtime_resume(struct device
*dev
)
303 struct at91_twi_dev
*twi_dev
= dev_get_drvdata(dev
);
305 pinctrl_pm_select_default_state(dev
);
307 return clk_prepare_enable(twi_dev
->clk
);
310 static int at91_twi_suspend_noirq(struct device
*dev
)
312 if (!pm_runtime_status_suspended(dev
))
313 at91_twi_runtime_suspend(dev
);
318 static int at91_twi_resume_noirq(struct device
*dev
)
320 struct at91_twi_dev
*twi_dev
= dev_get_drvdata(dev
);
323 if (!pm_runtime_status_suspended(dev
)) {
324 ret
= at91_twi_runtime_resume(dev
);
329 pm_runtime_mark_last_busy(dev
);
330 pm_request_autosuspend(dev
);
332 at91_init_twi_bus(twi_dev
);
337 static const struct dev_pm_ops at91_twi_pm
= {
338 .suspend_noirq
= at91_twi_suspend_noirq
,
339 .resume_noirq
= at91_twi_resume_noirq
,
340 .runtime_suspend
= at91_twi_runtime_suspend
,
341 .runtime_resume
= at91_twi_runtime_resume
,
344 #define at91_twi_pm_ops (&at91_twi_pm)
346 #define at91_twi_pm_ops NULL
349 static struct platform_driver at91_twi_driver
= {
350 .probe
= at91_twi_probe
,
351 .remove
= at91_twi_remove
,
352 .id_table
= at91_twi_devtypes
,
355 .of_match_table
= of_match_ptr(atmel_twi_dt_ids
),
356 .pm
= at91_twi_pm_ops
,
360 static int __init
at91_twi_init(void)
362 return platform_driver_register(&at91_twi_driver
);
365 static void __exit
at91_twi_exit(void)
367 platform_driver_unregister(&at91_twi_driver
);
370 subsys_initcall(at91_twi_init
);
371 module_exit(at91_twi_exit
);
373 MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>");
374 MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
375 MODULE_LICENSE("GPL");
376 MODULE_ALIAS("platform:at91_i2c");