1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * i2c Support for Atmel's AT91 Two-Wire Interface (TWI) 4 * 5 * Copyright (C) 2011 Weinmann Medical GmbH 6 * Author: Nikolaus Voss <n.voss@weinmann.de> 7 * 8 * Evolved from original work by: 9 * Copyright (C) 2004 Rick Bronson 10 * Converted to 2.6 by Andrew Victor <andrew@sanpeople.com> 11 * 12 * Borrowed heavily from original work by: 13 * Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com> 14 */ 15 16 #include <linux/clk.h> 17 #include <linux/err.h> 18 #include <linux/i2c.h> 19 #include <linux/io.h> 20 #include <linux/module.h> 21 #include <linux/of.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> 26 27 #include "i2c-at91.h" 28 29 unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg) 30 { 31 return readl_relaxed(dev->base + reg); 32 } 33 34 void at91_twi_write(struct at91_twi_dev *dev, unsigned reg, unsigned val) 35 { 36 writel_relaxed(val, dev->base + reg); 37 } 38 39 void at91_disable_twi_interrupts(struct at91_twi_dev *dev) 40 { 41 at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_INT_MASK); 42 } 43 44 void at91_twi_irq_save(struct at91_twi_dev *dev) 45 { 46 dev->imr = at91_twi_read(dev, AT91_TWI_IMR) & AT91_TWI_INT_MASK; 47 at91_disable_twi_interrupts(dev); 48 } 49 50 void at91_twi_irq_restore(struct at91_twi_dev *dev) 51 { 52 at91_twi_write(dev, AT91_TWI_IER, dev->imr); 53 } 54 55 void at91_init_twi_bus(struct at91_twi_dev *dev) 56 { 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); 61 else 62 at91_init_twi_bus_master(dev); 63 } 64 65 static struct at91_twi_pdata at91rm9200_config = { 66 .clk_max_div = 5, 67 .clk_offset = 3, 68 .has_unre_flag = true, 69 .has_alt_cmd = false, 70 .has_hold_field = false, 71 .has_dig_filtr = false, 72 .has_adv_dig_filtr = false, 73 .has_ana_filtr = false, 74 }; 75 76 static struct at91_twi_pdata at91sam9261_config = { 77 .clk_max_div = 5, 78 .clk_offset = 4, 79 .has_unre_flag = false, 80 .has_alt_cmd = false, 81 .has_hold_field = false, 82 .has_dig_filtr = false, 83 .has_adv_dig_filtr = false, 84 .has_ana_filtr = false, 85 }; 86 87 static struct at91_twi_pdata at91sam9260_config = { 88 .clk_max_div = 7, 89 .clk_offset = 4, 90 .has_unre_flag = false, 91 .has_alt_cmd = false, 92 .has_hold_field = false, 93 .has_dig_filtr = false, 94 .has_adv_dig_filtr = false, 95 .has_ana_filtr = false, 96 }; 97 98 static struct at91_twi_pdata at91sam9g20_config = { 99 .clk_max_div = 7, 100 .clk_offset = 4, 101 .has_unre_flag = false, 102 .has_alt_cmd = false, 103 .has_hold_field = false, 104 .has_dig_filtr = false, 105 .has_adv_dig_filtr = false, 106 .has_ana_filtr = false, 107 }; 108 109 static struct at91_twi_pdata at91sam9g10_config = { 110 .clk_max_div = 7, 111 .clk_offset = 4, 112 .has_unre_flag = false, 113 .has_alt_cmd = false, 114 .has_hold_field = false, 115 .has_dig_filtr = false, 116 .has_adv_dig_filtr = false, 117 .has_ana_filtr = false, 118 }; 119 120 static const struct platform_device_id at91_twi_devtypes[] = { 121 { 122 .name = "i2c-at91rm9200", 123 .driver_data = (unsigned long) &at91rm9200_config, 124 }, { 125 .name = "i2c-at91sam9261", 126 .driver_data = (unsigned long) &at91sam9261_config, 127 }, { 128 .name = "i2c-at91sam9260", 129 .driver_data = (unsigned long) &at91sam9260_config, 130 }, { 131 .name = "i2c-at91sam9g20", 132 .driver_data = (unsigned long) &at91sam9g20_config, 133 }, { 134 .name = "i2c-at91sam9g10", 135 .driver_data = (unsigned long) &at91sam9g10_config, 136 }, { 137 /* sentinel */ 138 } 139 }; 140 141 #if defined(CONFIG_OF) 142 static struct at91_twi_pdata at91sam9x5_config = { 143 .clk_max_div = 7, 144 .clk_offset = 4, 145 .has_unre_flag = false, 146 .has_alt_cmd = false, 147 .has_hold_field = false, 148 .has_dig_filtr = false, 149 .has_adv_dig_filtr = false, 150 .has_ana_filtr = false, 151 }; 152 153 static struct at91_twi_pdata sama5d4_config = { 154 .clk_max_div = 7, 155 .clk_offset = 4, 156 .has_unre_flag = false, 157 .has_alt_cmd = false, 158 .has_hold_field = true, 159 .has_dig_filtr = true, 160 .has_adv_dig_filtr = false, 161 .has_ana_filtr = false, 162 }; 163 164 static struct at91_twi_pdata sama5d2_config = { 165 .clk_max_div = 7, 166 .clk_offset = 3, 167 .has_unre_flag = true, 168 .has_alt_cmd = true, 169 .has_hold_field = true, 170 .has_dig_filtr = true, 171 .has_adv_dig_filtr = true, 172 .has_ana_filtr = true, 173 }; 174 175 static struct at91_twi_pdata sam9x60_config = { 176 .clk_max_div = 7, 177 .clk_offset = 4, 178 .has_unre_flag = true, 179 .has_alt_cmd = true, 180 .has_hold_field = true, 181 .has_dig_filtr = true, 182 .has_adv_dig_filtr = true, 183 .has_ana_filtr = true, 184 }; 185 186 static const struct of_device_id atmel_twi_dt_ids[] = { 187 { 188 .compatible = "atmel,at91rm9200-i2c", 189 .data = &at91rm9200_config, 190 }, { 191 .compatible = "atmel,at91sam9260-i2c", 192 .data = &at91sam9260_config, 193 }, { 194 .compatible = "atmel,at91sam9261-i2c", 195 .data = &at91sam9261_config, 196 }, { 197 .compatible = "atmel,at91sam9g20-i2c", 198 .data = &at91sam9g20_config, 199 }, { 200 .compatible = "atmel,at91sam9g10-i2c", 201 .data = &at91sam9g10_config, 202 }, { 203 .compatible = "atmel,at91sam9x5-i2c", 204 .data = &at91sam9x5_config, 205 }, { 206 .compatible = "atmel,sama5d4-i2c", 207 .data = &sama5d4_config, 208 }, { 209 .compatible = "atmel,sama5d2-i2c", 210 .data = &sama5d2_config, 211 }, { 212 .compatible = "microchip,sam9x60-i2c", 213 .data = &sam9x60_config, 214 }, { 215 /* sentinel */ 216 } 217 }; 218 MODULE_DEVICE_TABLE(of, atmel_twi_dt_ids); 219 #endif 220 221 static struct at91_twi_pdata *at91_twi_get_driver_data( 222 struct platform_device *pdev) 223 { 224 if (pdev->dev.of_node) { 225 const struct of_device_id *match; 226 match = of_match_node(atmel_twi_dt_ids, pdev->dev.of_node); 227 if (!match) 228 return NULL; 229 return (struct at91_twi_pdata *)match->data; 230 } 231 return (struct at91_twi_pdata *) platform_get_device_id(pdev)->driver_data; 232 } 233 234 static int at91_twi_probe(struct platform_device *pdev) 235 { 236 struct at91_twi_dev *dev; 237 struct resource *mem; 238 int rc; 239 u32 phy_addr; 240 241 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); 242 if (!dev) 243 return -ENOMEM; 244 245 dev->dev = &pdev->dev; 246 247 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 248 if (!mem) 249 return -ENODEV; 250 phy_addr = mem->start; 251 252 dev->pdata = at91_twi_get_driver_data(pdev); 253 if (!dev->pdata) 254 return -ENODEV; 255 256 dev->base = devm_ioremap_resource(&pdev->dev, mem); 257 if (IS_ERR(dev->base)) 258 return PTR_ERR(dev->base); 259 260 dev->irq = platform_get_irq(pdev, 0); 261 if (dev->irq < 0) 262 return dev->irq; 263 264 platform_set_drvdata(pdev, dev); 265 266 dev->clk = devm_clk_get(dev->dev, NULL); 267 if (IS_ERR(dev->clk)) { 268 dev_err(dev->dev, "no clock defined\n"); 269 return -ENODEV; 270 } 271 clk_prepare_enable(dev->clk); 272 273 snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91"); 274 i2c_set_adapdata(&dev->adapter, dev); 275 dev->adapter.owner = THIS_MODULE; 276 dev->adapter.class = I2C_CLASS_DEPRECATED; 277 dev->adapter.dev.parent = dev->dev; 278 dev->adapter.nr = pdev->id; 279 dev->adapter.timeout = AT91_I2C_TIMEOUT; 280 dev->adapter.dev.of_node = pdev->dev.of_node; 281 282 dev->slave_detected = i2c_detect_slave_mode(&pdev->dev); 283 284 if (dev->slave_detected) 285 rc = at91_twi_probe_slave(pdev, phy_addr, dev); 286 else 287 rc = at91_twi_probe_master(pdev, phy_addr, dev); 288 if (rc) 289 return rc; 290 291 at91_init_twi_bus(dev); 292 293 pm_runtime_set_autosuspend_delay(dev->dev, AUTOSUSPEND_TIMEOUT); 294 pm_runtime_use_autosuspend(dev->dev); 295 pm_runtime_set_active(dev->dev); 296 pm_runtime_enable(dev->dev); 297 298 rc = i2c_add_numbered_adapter(&dev->adapter); 299 if (rc) { 300 clk_disable_unprepare(dev->clk); 301 302 pm_runtime_disable(dev->dev); 303 pm_runtime_set_suspended(dev->dev); 304 305 return rc; 306 } 307 308 dev_info(dev->dev, "AT91 i2c bus driver (hw version: %#x).\n", 309 at91_twi_read(dev, AT91_TWI_VER)); 310 return 0; 311 } 312 313 static int at91_twi_remove(struct platform_device *pdev) 314 { 315 struct at91_twi_dev *dev = platform_get_drvdata(pdev); 316 317 i2c_del_adapter(&dev->adapter); 318 clk_disable_unprepare(dev->clk); 319 320 pm_runtime_disable(dev->dev); 321 pm_runtime_set_suspended(dev->dev); 322 323 return 0; 324 } 325 326 #ifdef CONFIG_PM 327 328 static int at91_twi_runtime_suspend(struct device *dev) 329 { 330 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev); 331 332 clk_disable_unprepare(twi_dev->clk); 333 334 pinctrl_pm_select_sleep_state(dev); 335 336 return 0; 337 } 338 339 static int at91_twi_runtime_resume(struct device *dev) 340 { 341 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev); 342 343 pinctrl_pm_select_default_state(dev); 344 345 return clk_prepare_enable(twi_dev->clk); 346 } 347 348 static int at91_twi_suspend_noirq(struct device *dev) 349 { 350 if (!pm_runtime_status_suspended(dev)) 351 at91_twi_runtime_suspend(dev); 352 353 return 0; 354 } 355 356 static int at91_twi_resume_noirq(struct device *dev) 357 { 358 struct at91_twi_dev *twi_dev = dev_get_drvdata(dev); 359 int ret; 360 361 if (!pm_runtime_status_suspended(dev)) { 362 ret = at91_twi_runtime_resume(dev); 363 if (ret) 364 return ret; 365 } 366 367 pm_runtime_mark_last_busy(dev); 368 pm_request_autosuspend(dev); 369 370 at91_init_twi_bus(twi_dev); 371 372 return 0; 373 } 374 375 static const struct dev_pm_ops at91_twi_pm = { 376 .suspend_noirq = at91_twi_suspend_noirq, 377 .resume_noirq = at91_twi_resume_noirq, 378 .runtime_suspend = at91_twi_runtime_suspend, 379 .runtime_resume = at91_twi_runtime_resume, 380 }; 381 382 #define at91_twi_pm_ops (&at91_twi_pm) 383 #else 384 #define at91_twi_pm_ops NULL 385 #endif 386 387 static struct platform_driver at91_twi_driver = { 388 .probe = at91_twi_probe, 389 .remove = at91_twi_remove, 390 .id_table = at91_twi_devtypes, 391 .driver = { 392 .name = "at91_i2c", 393 .of_match_table = of_match_ptr(atmel_twi_dt_ids), 394 .pm = at91_twi_pm_ops, 395 }, 396 }; 397 398 static int __init at91_twi_init(void) 399 { 400 return platform_driver_register(&at91_twi_driver); 401 } 402 403 static void __exit at91_twi_exit(void) 404 { 405 platform_driver_unregister(&at91_twi_driver); 406 } 407 408 subsys_initcall(at91_twi_init); 409 module_exit(at91_twi_exit); 410 411 MODULE_AUTHOR("Nikolaus Voss <n.voss@weinmann.de>"); 412 MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91"); 413 MODULE_LICENSE("GPL"); 414 MODULE_ALIAS("platform:at91_i2c"); 415