diff options
Diffstat (limited to 'drivers/i2c')
-rw-r--r-- | drivers/i2c/busses/i2c-gpio.c | 95 | ||||
-rw-r--r-- | drivers/i2c/busses/i2c-imx.c | 4 | ||||
-rw-r--r-- | drivers/i2c/busses/i2c-pxa.c | 95 |
3 files changed, 151 insertions, 43 deletions
diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c index 69fbfaedc039..c0330a41db03 100644 --- a/drivers/i2c/busses/i2c-gpio.c +++ b/drivers/i2c/busses/i2c-gpio.c | |||
@@ -15,6 +15,14 @@ | |||
15 | #include <linux/slab.h> | 15 | #include <linux/slab.h> |
16 | #include <linux/platform_device.h> | 16 | #include <linux/platform_device.h> |
17 | #include <linux/gpio.h> | 17 | #include <linux/gpio.h> |
18 | #include <linux/of_gpio.h> | ||
19 | #include <linux/of_i2c.h> | ||
20 | |||
21 | struct i2c_gpio_private_data { | ||
22 | struct i2c_adapter adap; | ||
23 | struct i2c_algo_bit_data bit_data; | ||
24 | struct i2c_gpio_platform_data pdata; | ||
25 | }; | ||
18 | 26 | ||
19 | /* Toggle SDA by changing the direction of the pin */ | 27 | /* Toggle SDA by changing the direction of the pin */ |
20 | static void i2c_gpio_setsda_dir(void *data, int state) | 28 | static void i2c_gpio_setsda_dir(void *data, int state) |
@@ -77,24 +85,62 @@ static int i2c_gpio_getscl(void *data) | |||
77 | return gpio_get_value(pdata->scl_pin); | 85 | return gpio_get_value(pdata->scl_pin); |
78 | } | 86 | } |
79 | 87 | ||
88 | static int __devinit of_i2c_gpio_probe(struct device_node *np, | ||
89 | struct i2c_gpio_platform_data *pdata) | ||
90 | { | ||
91 | u32 reg; | ||
92 | |||
93 | if (of_gpio_count(np) < 2) | ||
94 | return -ENODEV; | ||
95 | |||
96 | pdata->sda_pin = of_get_gpio(np, 0); | ||
97 | pdata->scl_pin = of_get_gpio(np, 1); | ||
98 | |||
99 | if (!gpio_is_valid(pdata->sda_pin) || !gpio_is_valid(pdata->scl_pin)) { | ||
100 | pr_err("%s: invalid GPIO pins, sda=%d/scl=%d\n", | ||
101 | np->full_name, pdata->sda_pin, pdata->scl_pin); | ||
102 | return -ENODEV; | ||
103 | } | ||
104 | |||
105 | of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay); | ||
106 | |||
107 | if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", ®)) | ||
108 | pdata->timeout = msecs_to_jiffies(reg); | ||
109 | |||
110 | pdata->sda_is_open_drain = | ||
111 | of_property_read_bool(np, "i2c-gpio,sda-open-drain"); | ||
112 | pdata->scl_is_open_drain = | ||
113 | of_property_read_bool(np, "i2c-gpio,scl-open-drain"); | ||
114 | pdata->scl_is_output_only = | ||
115 | of_property_read_bool(np, "i2c-gpio,scl-output-only"); | ||
116 | |||
117 | return 0; | ||
118 | } | ||
119 | |||
80 | static int __devinit i2c_gpio_probe(struct platform_device *pdev) | 120 | static int __devinit i2c_gpio_probe(struct platform_device *pdev) |
81 | { | 121 | { |
122 | struct i2c_gpio_private_data *priv; | ||
82 | struct i2c_gpio_platform_data *pdata; | 123 | struct i2c_gpio_platform_data *pdata; |
83 | struct i2c_algo_bit_data *bit_data; | 124 | struct i2c_algo_bit_data *bit_data; |
84 | struct i2c_adapter *adap; | 125 | struct i2c_adapter *adap; |
85 | int ret; | 126 | int ret; |
86 | 127 | ||
87 | pdata = pdev->dev.platform_data; | 128 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
88 | if (!pdata) | 129 | if (!priv) |
89 | return -ENXIO; | 130 | return -ENOMEM; |
90 | 131 | adap = &priv->adap; | |
91 | ret = -ENOMEM; | 132 | bit_data = &priv->bit_data; |
92 | adap = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL); | 133 | pdata = &priv->pdata; |
93 | if (!adap) | 134 | |
94 | goto err_alloc_adap; | 135 | if (pdev->dev.of_node) { |
95 | bit_data = kzalloc(sizeof(struct i2c_algo_bit_data), GFP_KERNEL); | 136 | ret = of_i2c_gpio_probe(pdev->dev.of_node, pdata); |
96 | if (!bit_data) | 137 | if (ret) |
97 | goto err_alloc_bit_data; | 138 | return ret; |
139 | } else { | ||
140 | if (!pdev->dev.platform_data) | ||
141 | return -ENXIO; | ||
142 | memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata)); | ||
143 | } | ||
98 | 144 | ||
99 | ret = gpio_request(pdata->sda_pin, "sda"); | 145 | ret = gpio_request(pdata->sda_pin, "sda"); |
100 | if (ret) | 146 | if (ret) |
@@ -142,6 +188,7 @@ static int __devinit i2c_gpio_probe(struct platform_device *pdev) | |||
142 | adap->algo_data = bit_data; | 188 | adap->algo_data = bit_data; |
143 | adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; | 189 | adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
144 | adap->dev.parent = &pdev->dev; | 190 | adap->dev.parent = &pdev->dev; |
191 | adap->dev.of_node = pdev->dev.of_node; | ||
145 | 192 | ||
146 | /* | 193 | /* |
147 | * If "dev->id" is negative we consider it as zero. | 194 | * If "dev->id" is negative we consider it as zero. |
@@ -153,7 +200,9 @@ static int __devinit i2c_gpio_probe(struct platform_device *pdev) | |||
153 | if (ret) | 200 | if (ret) |
154 | goto err_add_bus; | 201 | goto err_add_bus; |
155 | 202 | ||
156 | platform_set_drvdata(pdev, adap); | 203 | of_i2c_register_devices(adap); |
204 | |||
205 | platform_set_drvdata(pdev, priv); | ||
157 | 206 | ||
158 | dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n", | 207 | dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n", |
159 | pdata->sda_pin, pdata->scl_pin, | 208 | pdata->sda_pin, pdata->scl_pin, |
@@ -167,34 +216,40 @@ err_add_bus: | |||
167 | err_request_scl: | 216 | err_request_scl: |
168 | gpio_free(pdata->sda_pin); | 217 | gpio_free(pdata->sda_pin); |
169 | err_request_sda: | 218 | err_request_sda: |
170 | kfree(bit_data); | ||
171 | err_alloc_bit_data: | ||
172 | kfree(adap); | ||
173 | err_alloc_adap: | ||
174 | return ret; | 219 | return ret; |
175 | } | 220 | } |
176 | 221 | ||
177 | static int __devexit i2c_gpio_remove(struct platform_device *pdev) | 222 | static int __devexit i2c_gpio_remove(struct platform_device *pdev) |
178 | { | 223 | { |
224 | struct i2c_gpio_private_data *priv; | ||
179 | struct i2c_gpio_platform_data *pdata; | 225 | struct i2c_gpio_platform_data *pdata; |
180 | struct i2c_adapter *adap; | 226 | struct i2c_adapter *adap; |
181 | 227 | ||
182 | adap = platform_get_drvdata(pdev); | 228 | priv = platform_get_drvdata(pdev); |
183 | pdata = pdev->dev.platform_data; | 229 | adap = &priv->adap; |
230 | pdata = &priv->pdata; | ||
184 | 231 | ||
185 | i2c_del_adapter(adap); | 232 | i2c_del_adapter(adap); |
186 | gpio_free(pdata->scl_pin); | 233 | gpio_free(pdata->scl_pin); |
187 | gpio_free(pdata->sda_pin); | 234 | gpio_free(pdata->sda_pin); |
188 | kfree(adap->algo_data); | ||
189 | kfree(adap); | ||
190 | 235 | ||
191 | return 0; | 236 | return 0; |
192 | } | 237 | } |
193 | 238 | ||
239 | #if defined(CONFIG_OF) | ||
240 | static const struct of_device_id i2c_gpio_dt_ids[] = { | ||
241 | { .compatible = "i2c-gpio", }, | ||
242 | { /* sentinel */ } | ||
243 | }; | ||
244 | |||
245 | MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids); | ||
246 | #endif | ||
247 | |||
194 | static struct platform_driver i2c_gpio_driver = { | 248 | static struct platform_driver i2c_gpio_driver = { |
195 | .driver = { | 249 | .driver = { |
196 | .name = "i2c-gpio", | 250 | .name = "i2c-gpio", |
197 | .owner = THIS_MODULE, | 251 | .owner = THIS_MODULE, |
252 | .of_match_table = of_match_ptr(i2c_gpio_dt_ids), | ||
198 | }, | 253 | }, |
199 | .probe = i2c_gpio_probe, | 254 | .probe = i2c_gpio_probe, |
200 | .remove = __devexit_p(i2c_gpio_remove), | 255 | .remove = __devexit_p(i2c_gpio_remove), |
diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c index 124d9c594f40..dfb84b7ee550 100644 --- a/drivers/i2c/busses/i2c-imx.c +++ b/drivers/i2c/busses/i2c-imx.c | |||
@@ -191,7 +191,7 @@ static int i2c_imx_start(struct imx_i2c_struct *i2c_imx) | |||
191 | 191 | ||
192 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); | 192 | dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); |
193 | 193 | ||
194 | clk_enable(i2c_imx->clk); | 194 | clk_prepare_enable(i2c_imx->clk); |
195 | writeb(i2c_imx->ifdr, i2c_imx->base + IMX_I2C_IFDR); | 195 | writeb(i2c_imx->ifdr, i2c_imx->base + IMX_I2C_IFDR); |
196 | /* Enable I2C controller */ | 196 | /* Enable I2C controller */ |
197 | writeb(0, i2c_imx->base + IMX_I2C_I2SR); | 197 | writeb(0, i2c_imx->base + IMX_I2C_I2SR); |
@@ -240,7 +240,7 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx) | |||
240 | 240 | ||
241 | /* Disable I2C controller */ | 241 | /* Disable I2C controller */ |
242 | writeb(0, i2c_imx->base + IMX_I2C_I2CR); | 242 | writeb(0, i2c_imx->base + IMX_I2C_I2CR); |
243 | clk_disable(i2c_imx->clk); | 243 | clk_disable_unprepare(i2c_imx->clk); |
244 | } | 244 | } |
245 | 245 | ||
246 | static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx, | 246 | static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx, |
diff --git a/drivers/i2c/busses/i2c-pxa.c b/drivers/i2c/busses/i2c-pxa.c index d60364650990..f6733267fa9c 100644 --- a/drivers/i2c/busses/i2c-pxa.c +++ b/drivers/i2c/busses/i2c-pxa.c | |||
@@ -29,6 +29,8 @@ | |||
29 | #include <linux/errno.h> | 29 | #include <linux/errno.h> |
30 | #include <linux/interrupt.h> | 30 | #include <linux/interrupt.h> |
31 | #include <linux/i2c-pxa.h> | 31 | #include <linux/i2c-pxa.h> |
32 | #include <linux/of.h> | ||
33 | #include <linux/of_device.h> | ||
32 | #include <linux/of_i2c.h> | 34 | #include <linux/of_i2c.h> |
33 | #include <linux/platform_device.h> | 35 | #include <linux/platform_device.h> |
34 | #include <linux/err.h> | 36 | #include <linux/err.h> |
@@ -1044,23 +1046,60 @@ static const struct i2c_algorithm i2c_pxa_pio_algorithm = { | |||
1044 | .functionality = i2c_pxa_functionality, | 1046 | .functionality = i2c_pxa_functionality, |
1045 | }; | 1047 | }; |
1046 | 1048 | ||
1047 | static int i2c_pxa_probe(struct platform_device *dev) | 1049 | static struct of_device_id i2c_pxa_dt_ids[] = { |
1050 | { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX }, | ||
1051 | { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX }, | ||
1052 | { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA2XX }, | ||
1053 | {} | ||
1054 | }; | ||
1055 | MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids); | ||
1056 | |||
1057 | static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c, | ||
1058 | enum pxa_i2c_types *i2c_types) | ||
1048 | { | 1059 | { |
1049 | struct pxa_i2c *i2c; | 1060 | struct device_node *np = pdev->dev.of_node; |
1050 | struct resource *res; | 1061 | const struct of_device_id *of_id = |
1051 | struct i2c_pxa_platform_data *plat = dev->dev.platform_data; | 1062 | of_match_device(i2c_pxa_dt_ids, &pdev->dev); |
1052 | const struct platform_device_id *id = platform_get_device_id(dev); | ||
1053 | enum pxa_i2c_types i2c_type = id->driver_data; | ||
1054 | int ret; | 1063 | int ret; |
1055 | int irq; | ||
1056 | 1064 | ||
1057 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); | 1065 | if (!of_id) |
1058 | irq = platform_get_irq(dev, 0); | 1066 | return 1; |
1059 | if (res == NULL || irq < 0) | 1067 | ret = of_alias_get_id(np, "i2c"); |
1060 | return -ENODEV; | 1068 | if (ret < 0) { |
1069 | dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret); | ||
1070 | return ret; | ||
1071 | } | ||
1072 | pdev->id = ret; | ||
1073 | if (of_get_property(np, "mrvl,i2c-polling", NULL)) | ||
1074 | i2c->use_pio = 1; | ||
1075 | if (of_get_property(np, "mrvl,i2c-fast-mode", NULL)) | ||
1076 | i2c->fast_mode = 1; | ||
1077 | *i2c_types = (u32)(of_id->data); | ||
1078 | return 0; | ||
1079 | } | ||
1061 | 1080 | ||
1062 | if (!request_mem_region(res->start, resource_size(res), res->name)) | 1081 | static int i2c_pxa_probe_pdata(struct platform_device *pdev, |
1063 | return -ENOMEM; | 1082 | struct pxa_i2c *i2c, |
1083 | enum pxa_i2c_types *i2c_types) | ||
1084 | { | ||
1085 | struct i2c_pxa_platform_data *plat = pdev->dev.platform_data; | ||
1086 | const struct platform_device_id *id = platform_get_device_id(pdev); | ||
1087 | |||
1088 | *i2c_types = id->driver_data; | ||
1089 | if (plat) { | ||
1090 | i2c->use_pio = plat->use_pio; | ||
1091 | i2c->fast_mode = plat->fast_mode; | ||
1092 | } | ||
1093 | return 0; | ||
1094 | } | ||
1095 | |||
1096 | static int i2c_pxa_probe(struct platform_device *dev) | ||
1097 | { | ||
1098 | struct i2c_pxa_platform_data *plat = dev->dev.platform_data; | ||
1099 | enum pxa_i2c_types i2c_type; | ||
1100 | struct pxa_i2c *i2c; | ||
1101 | struct resource *res = NULL; | ||
1102 | int ret, irq; | ||
1064 | 1103 | ||
1065 | i2c = kzalloc(sizeof(struct pxa_i2c), GFP_KERNEL); | 1104 | i2c = kzalloc(sizeof(struct pxa_i2c), GFP_KERNEL); |
1066 | if (!i2c) { | 1105 | if (!i2c) { |
@@ -1068,6 +1107,24 @@ static int i2c_pxa_probe(struct platform_device *dev) | |||
1068 | goto emalloc; | 1107 | goto emalloc; |
1069 | } | 1108 | } |
1070 | 1109 | ||
1110 | ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type); | ||
1111 | if (ret > 0) | ||
1112 | ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type); | ||
1113 | if (ret < 0) | ||
1114 | goto eclk; | ||
1115 | |||
1116 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); | ||
1117 | irq = platform_get_irq(dev, 0); | ||
1118 | if (res == NULL || irq < 0) { | ||
1119 | ret = -ENODEV; | ||
1120 | goto eclk; | ||
1121 | } | ||
1122 | |||
1123 | if (!request_mem_region(res->start, resource_size(res), res->name)) { | ||
1124 | ret = -ENOMEM; | ||
1125 | goto eclk; | ||
1126 | } | ||
1127 | |||
1071 | i2c->adap.owner = THIS_MODULE; | 1128 | i2c->adap.owner = THIS_MODULE; |
1072 | i2c->adap.retries = 5; | 1129 | i2c->adap.retries = 5; |
1073 | 1130 | ||
@@ -1109,21 +1166,16 @@ static int i2c_pxa_probe(struct platform_device *dev) | |||
1109 | 1166 | ||
1110 | i2c->slave_addr = I2C_PXA_SLAVE_ADDR; | 1167 | i2c->slave_addr = I2C_PXA_SLAVE_ADDR; |
1111 | 1168 | ||
1112 | #ifdef CONFIG_I2C_PXA_SLAVE | ||
1113 | if (plat) { | 1169 | if (plat) { |
1170 | #ifdef CONFIG_I2C_PXA_SLAVE | ||
1114 | i2c->slave_addr = plat->slave_addr; | 1171 | i2c->slave_addr = plat->slave_addr; |
1115 | i2c->slave = plat->slave; | 1172 | i2c->slave = plat->slave; |
1116 | } | ||
1117 | #endif | 1173 | #endif |
1118 | |||
1119 | clk_enable(i2c->clk); | ||
1120 | |||
1121 | if (plat) { | ||
1122 | i2c->adap.class = plat->class; | 1174 | i2c->adap.class = plat->class; |
1123 | i2c->use_pio = plat->use_pio; | ||
1124 | i2c->fast_mode = plat->fast_mode; | ||
1125 | } | 1175 | } |
1126 | 1176 | ||
1177 | clk_enable(i2c->clk); | ||
1178 | |||
1127 | if (i2c->use_pio) { | 1179 | if (i2c->use_pio) { |
1128 | i2c->adap.algo = &i2c_pxa_pio_algorithm; | 1180 | i2c->adap.algo = &i2c_pxa_pio_algorithm; |
1129 | } else { | 1181 | } else { |
@@ -1234,6 +1286,7 @@ static struct platform_driver i2c_pxa_driver = { | |||
1234 | .name = "pxa2xx-i2c", | 1286 | .name = "pxa2xx-i2c", |
1235 | .owner = THIS_MODULE, | 1287 | .owner = THIS_MODULE, |
1236 | .pm = I2C_PXA_DEV_PM_OPS, | 1288 | .pm = I2C_PXA_DEV_PM_OPS, |
1289 | .of_match_table = i2c_pxa_dt_ids, | ||
1237 | }, | 1290 | }, |
1238 | .id_table = i2c_pxa_id_table, | 1291 | .id_table = i2c_pxa_id_table, |
1239 | }; | 1292 | }; |