Russell King | 6b65cd7 | 2006-12-10 21:21:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * i2c-versatile.c |
| 3 | * |
| 4 | * Copyright (C) 2006 ARM Ltd. |
| 5 | * written by Russell King, Deep Blue Solutions Ltd. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/i2c-algo-bit.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
H Hartley Sweeten | 2178218 | 2010-05-21 18:41:01 +0200 | [diff] [blame] | 18 | #include <linux/io.h> |
Russell King | 6b65cd7 | 2006-12-10 21:21:32 +0100 | [diff] [blame] | 19 | |
| 20 | #define I2C_CONTROL 0x00 |
| 21 | #define I2C_CONTROLS 0x00 |
| 22 | #define I2C_CONTROLC 0x04 |
| 23 | #define SCL (1 << 0) |
| 24 | #define SDA (1 << 1) |
| 25 | |
| 26 | struct i2c_versatile { |
| 27 | struct i2c_adapter adap; |
| 28 | struct i2c_algo_bit_data algo; |
| 29 | void __iomem *base; |
| 30 | }; |
| 31 | |
| 32 | static void i2c_versatile_setsda(void *data, int state) |
| 33 | { |
| 34 | struct i2c_versatile *i2c = data; |
| 35 | |
| 36 | writel(SDA, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC)); |
| 37 | } |
| 38 | |
| 39 | static void i2c_versatile_setscl(void *data, int state) |
| 40 | { |
| 41 | struct i2c_versatile *i2c = data; |
| 42 | |
| 43 | writel(SCL, i2c->base + (state ? I2C_CONTROLS : I2C_CONTROLC)); |
| 44 | } |
| 45 | |
| 46 | static int i2c_versatile_getsda(void *data) |
| 47 | { |
| 48 | struct i2c_versatile *i2c = data; |
| 49 | return !!(readl(i2c->base + I2C_CONTROL) & SDA); |
| 50 | } |
| 51 | |
| 52 | static int i2c_versatile_getscl(void *data) |
| 53 | { |
| 54 | struct i2c_versatile *i2c = data; |
| 55 | return !!(readl(i2c->base + I2C_CONTROL) & SCL); |
| 56 | } |
| 57 | |
| 58 | static struct i2c_algo_bit_data i2c_versatile_algo = { |
| 59 | .setsda = i2c_versatile_setsda, |
| 60 | .setscl = i2c_versatile_setscl, |
| 61 | .getsda = i2c_versatile_getsda, |
| 62 | .getscl = i2c_versatile_getscl, |
| 63 | .udelay = 30, |
| 64 | .timeout = HZ, |
| 65 | }; |
| 66 | |
| 67 | static int i2c_versatile_probe(struct platform_device *dev) |
| 68 | { |
| 69 | struct i2c_versatile *i2c; |
| 70 | struct resource *r; |
| 71 | int ret; |
| 72 | |
Axel Lin | cc46188 | 2016-07-09 13:32:23 +0800 | [diff] [blame] | 73 | i2c = devm_kzalloc(&dev->dev, sizeof(struct i2c_versatile), GFP_KERNEL); |
| 74 | if (!i2c) |
| 75 | return -ENOMEM; |
| 76 | |
Russell King | 6b65cd7 | 2006-12-10 21:21:32 +0100 | [diff] [blame] | 77 | r = platform_get_resource(dev, IORESOURCE_MEM, 0); |
Axel Lin | cc46188 | 2016-07-09 13:32:23 +0800 | [diff] [blame] | 78 | i2c->base = devm_ioremap_resource(&dev->dev, r); |
| 79 | if (IS_ERR(i2c->base)) |
| 80 | return PTR_ERR(i2c->base); |
Russell King | 6b65cd7 | 2006-12-10 21:21:32 +0100 | [diff] [blame] | 81 | |
| 82 | writel(SCL | SDA, i2c->base + I2C_CONTROLS); |
| 83 | |
| 84 | i2c->adap.owner = THIS_MODULE; |
| 85 | strlcpy(i2c->adap.name, "Versatile I2C adapter", sizeof(i2c->adap.name)); |
| 86 | i2c->adap.algo_data = &i2c->algo; |
| 87 | i2c->adap.dev.parent = &dev->dev; |
Pawel Moll | 237ab40 | 2012-02-23 18:17:21 +0000 | [diff] [blame] | 88 | i2c->adap.dev.of_node = dev->dev.of_node; |
Russell King | 6b65cd7 | 2006-12-10 21:21:32 +0100 | [diff] [blame] | 89 | i2c->algo = i2c_versatile_algo; |
| 90 | i2c->algo.data = i2c; |
| 91 | |
Karol Lewandowski | 44454ba | 2012-03-27 11:10:28 +0200 | [diff] [blame] | 92 | i2c->adap.nr = dev->id; |
| 93 | ret = i2c_bit_add_numbered_bus(&i2c->adap); |
Axel Lin | cc46188 | 2016-07-09 13:32:23 +0800 | [diff] [blame] | 94 | if (ret < 0) |
| 95 | return ret; |
Russell King | 6b65cd7 | 2006-12-10 21:21:32 +0100 | [diff] [blame] | 96 | |
Axel Lin | cc46188 | 2016-07-09 13:32:23 +0800 | [diff] [blame] | 97 | platform_set_drvdata(dev, i2c); |
| 98 | |
| 99 | return 0; |
Russell King | 6b65cd7 | 2006-12-10 21:21:32 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static int i2c_versatile_remove(struct platform_device *dev) |
| 103 | { |
| 104 | struct i2c_versatile *i2c = platform_get_drvdata(dev); |
| 105 | |
Russell King | 6b65cd7 | 2006-12-10 21:21:32 +0100 | [diff] [blame] | 106 | i2c_del_adapter(&i2c->adap); |
| 107 | return 0; |
| 108 | } |
| 109 | |
Pawel Moll | 237ab40 | 2012-02-23 18:17:21 +0000 | [diff] [blame] | 110 | static const struct of_device_id i2c_versatile_match[] = { |
| 111 | { .compatible = "arm,versatile-i2c", }, |
| 112 | {}, |
| 113 | }; |
| 114 | MODULE_DEVICE_TABLE(of, i2c_versatile_match); |
| 115 | |
Russell King | 6b65cd7 | 2006-12-10 21:21:32 +0100 | [diff] [blame] | 116 | static struct platform_driver i2c_versatile_driver = { |
| 117 | .probe = i2c_versatile_probe, |
| 118 | .remove = i2c_versatile_remove, |
| 119 | .driver = { |
| 120 | .name = "versatile-i2c", |
Pawel Moll | 237ab40 | 2012-02-23 18:17:21 +0000 | [diff] [blame] | 121 | .of_match_table = i2c_versatile_match, |
Russell King | 6b65cd7 | 2006-12-10 21:21:32 +0100 | [diff] [blame] | 122 | }, |
| 123 | }; |
| 124 | |
| 125 | static int __init i2c_versatile_init(void) |
| 126 | { |
| 127 | return platform_driver_register(&i2c_versatile_driver); |
| 128 | } |
| 129 | |
| 130 | static void __exit i2c_versatile_exit(void) |
| 131 | { |
| 132 | platform_driver_unregister(&i2c_versatile_driver); |
| 133 | } |
| 134 | |
Catalin Marinas | 2514cca | 2009-02-12 15:57:20 +0100 | [diff] [blame] | 135 | subsys_initcall(i2c_versatile_init); |
Russell King | 6b65cd7 | 2006-12-10 21:21:32 +0100 | [diff] [blame] | 136 | module_exit(i2c_versatile_exit); |
| 137 | |
| 138 | MODULE_DESCRIPTION("ARM Versatile I2C bus driver"); |
| 139 | MODULE_LICENSE("GPL"); |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 140 | MODULE_ALIAS("platform:versatile-i2c"); |