Commit 23560214 authored by Alessandro Rubini's avatar Alessandro Rubini Committed by Wolfram Sang

i2c-nomadik: turn the platform driver to an amba driver

The i2c-nomadik gateware is really a PrimeCell APB device. By hosting
the driver under the amba bus we can access it more easily, for
example using the generic pci-amba driver. The patch also fixes the
mach-ux500 users, so they register an amba device instead than a
platform device.
Signed-off-by: default avatarAlessandro Rubini <rubini@gnudd.com>
Acked-by: default avatarGiancarlo Asnaghi <giancarlo.asnaghi@st.com>
Tested-by: default avatarLinus Walleij <linus.walleij@linaro.org>
Signed-off-by: default avatarWolfram Sang <w.sang@pengutronix.de>
parent af97bace
...@@ -56,27 +56,15 @@ dbx500_add_uart(struct device *parent, const char *name, resource_size_t base, ...@@ -56,27 +56,15 @@ dbx500_add_uart(struct device *parent, const char *name, resource_size_t base,
struct nmk_i2c_controller; struct nmk_i2c_controller;
static inline struct platform_device * static inline struct amba_device *
dbx500_add_i2c(struct device *parent, int id, resource_size_t base, int irq, dbx500_add_i2c(struct device *parent, int id, resource_size_t base, int irq,
struct nmk_i2c_controller *data) struct nmk_i2c_controller *data)
{ {
struct resource res[] = { /* Conjure a name similar to what the platform device used to have */
DEFINE_RES_MEM(base, SZ_4K), char name[16];
DEFINE_RES_IRQ(irq),
};
struct platform_device_info pdevinfo = { snprintf(name, sizeof(name), "nmk-i2c.%d", id);
.parent = parent, return amba_apb_device_add(parent, name, base, SZ_4K, irq, 0, data, 0);
.name = "nmk-i2c",
.id = id,
.res = res,
.num_res = ARRAY_SIZE(res),
.data = data,
.size_data = sizeof(*data),
.dma_mask = DMA_BIT_MASK(32),
};
return platform_device_register_full(&pdevinfo);
} }
static inline struct amba_device * static inline struct amba_device *
......
...@@ -14,7 +14,8 @@ ...@@ -14,7 +14,8 @@
*/ */
#include <linux/init.h> #include <linux/init.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/platform_device.h> #include <linux/amba/bus.h>
#include <linux/atomic.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/i2c.h> #include <linux/i2c.h>
...@@ -135,7 +136,7 @@ struct i2c_nmk_client { ...@@ -135,7 +136,7 @@ struct i2c_nmk_client {
/** /**
* struct nmk_i2c_dev - private data structure of the controller. * struct nmk_i2c_dev - private data structure of the controller.
* @pdev: parent platform device. * @adev: parent amba device.
* @adap: corresponding I2C adapter. * @adap: corresponding I2C adapter.
* @irq: interrupt line for the controller. * @irq: interrupt line for the controller.
* @virtbase: virtual io memory area. * @virtbase: virtual io memory area.
...@@ -149,7 +150,7 @@ struct i2c_nmk_client { ...@@ -149,7 +150,7 @@ struct i2c_nmk_client {
* @busy: Busy doing transfer. * @busy: Busy doing transfer.
*/ */
struct nmk_i2c_dev { struct nmk_i2c_dev {
struct platform_device *pdev; struct amba_device *adev;
struct i2c_adapter adap; struct i2c_adapter adap;
int irq; int irq;
void __iomem *virtbase; void __iomem *virtbase;
...@@ -216,7 +217,7 @@ static int flush_i2c_fifo(struct nmk_i2c_dev *dev) ...@@ -216,7 +217,7 @@ static int flush_i2c_fifo(struct nmk_i2c_dev *dev)
} }
} }
dev_err(&dev->pdev->dev, dev_err(&dev->adev->dev,
"flushing operation timed out giving up after %d attempts", "flushing operation timed out giving up after %d attempts",
LOOP_ATTEMPTS); LOOP_ATTEMPTS);
...@@ -363,7 +364,7 @@ static void setup_i2c_controller(struct nmk_i2c_dev *dev) ...@@ -363,7 +364,7 @@ static void setup_i2c_controller(struct nmk_i2c_dev *dev)
* and high speed (up to 3.4 Mb/s) * and high speed (up to 3.4 Mb/s)
*/ */
if (dev->cfg.sm > I2C_FREQ_MODE_FAST) { if (dev->cfg.sm > I2C_FREQ_MODE_FAST) {
dev_err(&dev->pdev->dev, dev_err(&dev->adev->dev,
"do not support this mode defaulting to std. mode\n"); "do not support this mode defaulting to std. mode\n");
brcr2 = i2c_clk/(100000 * 2) & 0xffff; brcr2 = i2c_clk/(100000 * 2) & 0xffff;
writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR); writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
...@@ -422,7 +423,7 @@ static int read_i2c(struct nmk_i2c_dev *dev) ...@@ -422,7 +423,7 @@ static int read_i2c(struct nmk_i2c_dev *dev)
&dev->xfer_complete, dev->adap.timeout); &dev->xfer_complete, dev->adap.timeout);
if (timeout < 0) { if (timeout < 0) {
dev_err(&dev->pdev->dev, dev_err(&dev->adev->dev,
"wait_for_completion_timeout " "wait_for_completion_timeout "
"returned %d waiting for event\n", timeout); "returned %d waiting for event\n", timeout);
status = timeout; status = timeout;
...@@ -430,7 +431,7 @@ static int read_i2c(struct nmk_i2c_dev *dev) ...@@ -430,7 +431,7 @@ static int read_i2c(struct nmk_i2c_dev *dev)
if (timeout == 0) { if (timeout == 0) {
/* Controller timed out */ /* Controller timed out */
dev_err(&dev->pdev->dev, "read from slave 0x%x timed out\n", dev_err(&dev->adev->dev, "read from slave 0x%x timed out\n",
dev->cli.slave_adr); dev->cli.slave_adr);
status = -ETIMEDOUT; status = -ETIMEDOUT;
} }
...@@ -509,7 +510,7 @@ static int write_i2c(struct nmk_i2c_dev *dev) ...@@ -509,7 +510,7 @@ static int write_i2c(struct nmk_i2c_dev *dev)
&dev->xfer_complete, dev->adap.timeout); &dev->xfer_complete, dev->adap.timeout);
if (timeout < 0) { if (timeout < 0) {
dev_err(&dev->pdev->dev, dev_err(&dev->adev->dev,
"wait_for_completion_timeout " "wait_for_completion_timeout "
"returned %d waiting for event\n", timeout); "returned %d waiting for event\n", timeout);
status = timeout; status = timeout;
...@@ -517,7 +518,7 @@ static int write_i2c(struct nmk_i2c_dev *dev) ...@@ -517,7 +518,7 @@ static int write_i2c(struct nmk_i2c_dev *dev)
if (timeout == 0) { if (timeout == 0) {
/* Controller timed out */ /* Controller timed out */
dev_err(&dev->pdev->dev, "write to slave 0x%x timed out\n", dev_err(&dev->adev->dev, "write to slave 0x%x timed out\n",
dev->cli.slave_adr); dev->cli.slave_adr);
status = -ETIMEDOUT; status = -ETIMEDOUT;
} }
...@@ -556,7 +557,7 @@ static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags) ...@@ -556,7 +557,7 @@ static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags)
if (((i2c_sr >> 2) & 0x3) == 0x3) { if (((i2c_sr >> 2) & 0x3) == 0x3) {
/* get the abort cause */ /* get the abort cause */
cause = (i2c_sr >> 4) & 0x7; cause = (i2c_sr >> 4) & 0x7;
dev_err(&dev->pdev->dev, "%s\n", dev_err(&dev->adev->dev, "%s\n",
cause >= ARRAY_SIZE(abort_causes) ? cause >= ARRAY_SIZE(abort_causes) ?
"unknown reason" : "unknown reason" :
abort_causes[cause]); abort_causes[cause]);
...@@ -629,7 +630,7 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap, ...@@ -629,7 +630,7 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
if (dev->regulator) if (dev->regulator)
regulator_enable(dev->regulator); regulator_enable(dev->regulator);
pm_runtime_get_sync(&dev->pdev->dev); pm_runtime_get_sync(&dev->adev->dev);
clk_enable(dev->clk); clk_enable(dev->clk);
...@@ -644,7 +645,7 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap, ...@@ -644,7 +645,7 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
for (i = 0; i < num_msgs; i++) { for (i = 0; i < num_msgs; i++) {
if (unlikely(msgs[i].flags & I2C_M_TEN)) { if (unlikely(msgs[i].flags & I2C_M_TEN)) {
dev_err(&dev->pdev->dev, dev_err(&dev->adev->dev,
"10 bit addressing not supported\n"); "10 bit addressing not supported\n");
status = -EINVAL; status = -EINVAL;
...@@ -666,7 +667,7 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap, ...@@ -666,7 +667,7 @@ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
out: out:
clk_disable(dev->clk); clk_disable(dev->clk);
pm_runtime_put_sync(&dev->pdev->dev); pm_runtime_put_sync(&dev->adev->dev);
if (dev->regulator) if (dev->regulator)
regulator_disable(dev->regulator); regulator_disable(dev->regulator);
...@@ -789,7 +790,7 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) ...@@ -789,7 +790,7 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg)
if (dev->cli.count) { if (dev->cli.count) {
dev->result = -EIO; dev->result = -EIO;
dev_err(&dev->pdev->dev, dev_err(&dev->adev->dev,
"%lu bytes still remain to be xfered\n", "%lu bytes still remain to be xfered\n",
dev->cli.count); dev->cli.count);
(void) init_hw(dev); (void) init_hw(dev);
...@@ -833,7 +834,7 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) ...@@ -833,7 +834,7 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg)
dev->result = -EIO; dev->result = -EIO;
(void) init_hw(dev); (void) init_hw(dev);
dev_err(&dev->pdev->dev, "Tx Fifo Over run\n"); dev_err(&dev->adev->dev, "Tx Fifo Over run\n");
complete(&dev->xfer_complete); complete(&dev->xfer_complete);
break; break;
...@@ -846,10 +847,10 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) ...@@ -846,10 +847,10 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg)
case I2C_IT_RFSE: case I2C_IT_RFSE:
case I2C_IT_WTSR: case I2C_IT_WTSR:
case I2C_IT_STD: case I2C_IT_STD:
dev_err(&dev->pdev->dev, "unhandled Interrupt\n"); dev_err(&dev->adev->dev, "unhandled Interrupt\n");
break; break;
default: default:
dev_err(&dev->pdev->dev, "spurious Interrupt..\n"); dev_err(&dev->adev->dev, "spurious Interrupt..\n");
break; break;
} }
...@@ -860,8 +861,8 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg) ...@@ -860,8 +861,8 @@ static irqreturn_t i2c_irq_handler(int irq, void *arg)
#ifdef CONFIG_PM #ifdef CONFIG_PM
static int nmk_i2c_suspend(struct device *dev) static int nmk_i2c_suspend(struct device *dev)
{ {
struct platform_device *pdev = to_platform_device(dev); struct amba_device *adev = to_amba_device(dev);
struct nmk_i2c_dev *nmk_i2c = platform_get_drvdata(pdev); struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev);
if (nmk_i2c->busy) if (nmk_i2c->busy)
return -EBUSY; return -EBUSY;
...@@ -898,79 +899,70 @@ static const struct i2c_algorithm nmk_i2c_algo = { ...@@ -898,79 +899,70 @@ static const struct i2c_algorithm nmk_i2c_algo = {
.functionality = nmk_i2c_functionality .functionality = nmk_i2c_functionality
}; };
static int __devinit nmk_i2c_probe(struct platform_device *pdev) static atomic_t adapter_id = ATOMIC_INIT(0);
static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
{ {
int ret = 0; int ret = 0;
struct resource *res;
struct nmk_i2c_controller *pdata = struct nmk_i2c_controller *pdata =
pdev->dev.platform_data; adev->dev.platform_data;
struct nmk_i2c_dev *dev; struct nmk_i2c_dev *dev;
struct i2c_adapter *adap; struct i2c_adapter *adap;
if (!pdata) {
dev_warn(&adev->dev, "no platform data\n");
return -ENODEV;
}
dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL); dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL);
if (!dev) { if (!dev) {
dev_err(&pdev->dev, "cannot allocate memory\n"); dev_err(&adev->dev, "cannot allocate memory\n");
ret = -ENOMEM; ret = -ENOMEM;
goto err_no_mem; goto err_no_mem;
} }
dev->busy = false; dev->busy = false;
dev->pdev = pdev; dev->adev = adev;
platform_set_drvdata(pdev, dev); amba_set_drvdata(adev, dev);
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res) {
ret = -ENOENT;
goto err_no_resource;
}
if (request_mem_region(res->start, resource_size(res), dev->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
DRIVER_NAME "I/O region") == NULL) {
ret = -EBUSY;
goto err_no_region;
}
dev->virtbase = ioremap(res->start, resource_size(res));
if (!dev->virtbase) { if (!dev->virtbase) {
ret = -ENOMEM; ret = -ENOMEM;
goto err_no_ioremap; goto err_no_ioremap;
} }
dev->irq = platform_get_irq(pdev, 0); dev->irq = adev->irq[0];
ret = request_irq(dev->irq, i2c_irq_handler, 0, ret = request_irq(dev->irq, i2c_irq_handler, 0,
DRIVER_NAME, dev); DRIVER_NAME, dev);
if (ret) { if (ret) {
dev_err(&pdev->dev, "cannot claim the irq %d\n", dev->irq); dev_err(&adev->dev, "cannot claim the irq %d\n", dev->irq);
goto err_irq; goto err_irq;
} }
dev->regulator = regulator_get(&pdev->dev, "v-i2c"); dev->regulator = regulator_get(&adev->dev, "v-i2c");
if (IS_ERR(dev->regulator)) { if (IS_ERR(dev->regulator)) {
dev_warn(&pdev->dev, "could not get i2c regulator\n"); dev_warn(&adev->dev, "could not get i2c regulator\n");
dev->regulator = NULL; dev->regulator = NULL;
} }
pm_suspend_ignore_children(&pdev->dev, true); pm_suspend_ignore_children(&adev->dev, true);
pm_runtime_enable(&pdev->dev);
dev->clk = clk_get(&pdev->dev, NULL); dev->clk = clk_get(&adev->dev, NULL);
if (IS_ERR(dev->clk)) { if (IS_ERR(dev->clk)) {
dev_err(&pdev->dev, "could not get i2c clock\n"); dev_err(&adev->dev, "could not get i2c clock\n");
ret = PTR_ERR(dev->clk); ret = PTR_ERR(dev->clk);
goto err_no_clk; goto err_no_clk;
} }
adap = &dev->adap; adap = &dev->adap;
adap->dev.parent = &pdev->dev; adap->dev.parent = &adev->dev;
adap->owner = THIS_MODULE; adap->owner = THIS_MODULE;
adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
adap->algo = &nmk_i2c_algo; adap->algo = &nmk_i2c_algo;
adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) : adap->timeout = pdata->timeout ? msecs_to_jiffies(pdata->timeout) :
msecs_to_jiffies(20000); msecs_to_jiffies(20000);
adap->nr = atomic_read(&adapter_id);
snprintf(adap->name, sizeof(adap->name), snprintf(adap->name, sizeof(adap->name),
"Nomadik I2C%d at %lx", pdev->id, (unsigned long)res->start); "Nomadik I2C%d at %pR", adap->nr, &adev->res);
atomic_inc(&adapter_id);
/* fetch the controller id */
adap->nr = pdev->id;
/* fetch the controller configuration from machine */ /* fetch the controller configuration from machine */
dev->cfg.clk_freq = pdata->clk_freq; dev->cfg.clk_freq = pdata->clk_freq;
...@@ -981,16 +973,18 @@ static int __devinit nmk_i2c_probe(struct platform_device *pdev) ...@@ -981,16 +973,18 @@ static int __devinit nmk_i2c_probe(struct platform_device *pdev)
i2c_set_adapdata(adap, dev); i2c_set_adapdata(adap, dev);
dev_info(&pdev->dev, dev_info(&adev->dev,
"initialize %s on virtual base %p\n", "initialize %s on virtual base %p\n",
adap->name, dev->virtbase); adap->name, dev->virtbase);
ret = i2c_add_numbered_adapter(adap); ret = i2c_add_numbered_adapter(adap);
if (ret) { if (ret) {
dev_err(&pdev->dev, "failed to add adapter\n"); dev_err(&adev->dev, "failed to add adapter\n");
goto err_add_adap; goto err_add_adap;
} }
pm_runtime_put(&adev->dev);
return 0; return 0;
err_add_adap: err_add_adap:
...@@ -998,25 +992,21 @@ static int __devinit nmk_i2c_probe(struct platform_device *pdev) ...@@ -998,25 +992,21 @@ static int __devinit nmk_i2c_probe(struct platform_device *pdev)
err_no_clk: err_no_clk:
if (dev->regulator) if (dev->regulator)
regulator_put(dev->regulator); regulator_put(dev->regulator);
pm_runtime_disable(&pdev->dev);
free_irq(dev->irq, dev); free_irq(dev->irq, dev);
err_irq: err_irq:
iounmap(dev->virtbase); iounmap(dev->virtbase);
err_no_ioremap: err_no_ioremap:
release_mem_region(res->start, resource_size(res)); amba_set_drvdata(adev, NULL);
err_no_region:
platform_set_drvdata(pdev, NULL);
err_no_resource:
kfree(dev); kfree(dev);
err_no_mem: err_no_mem:
return ret; return ret;
} }
static int __devexit nmk_i2c_remove(struct platform_device *pdev) static int nmk_i2c_remove(struct amba_device *adev)
{ {
struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); struct resource *res = &adev->res;
struct nmk_i2c_dev *dev = platform_get_drvdata(pdev); struct nmk_i2c_dev *dev = amba_get_drvdata(adev);
i2c_del_adapter(&dev->adap); i2c_del_adapter(&dev->adap);
flush_i2c_fifo(dev); flush_i2c_fifo(dev);
...@@ -1031,31 +1021,46 @@ static int __devexit nmk_i2c_remove(struct platform_device *pdev) ...@@ -1031,31 +1021,46 @@ static int __devexit nmk_i2c_remove(struct platform_device *pdev)
clk_put(dev->clk); clk_put(dev->clk);
if (dev->regulator) if (dev->regulator)
regulator_put(dev->regulator); regulator_put(dev->regulator);
pm_runtime_disable(&pdev->dev); pm_runtime_disable(&adev->dev);
platform_set_drvdata(pdev, NULL); amba_set_drvdata(adev, NULL);
kfree(dev); kfree(dev);
return 0; return 0;
} }
static struct platform_driver nmk_i2c_driver = { static struct amba_id nmk_i2c_ids[] = {
.driver = { {
.id = 0x00180024,
.mask = 0x00ffffff,
},
{
.id = 0x00380024,
.mask = 0x00ffffff,
},
{},
};
MODULE_DEVICE_TABLE(amba, nmk_i2c_ids);
static struct amba_driver nmk_i2c_driver = {
.drv = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.name = DRIVER_NAME, .name = DRIVER_NAME,
.pm = &nmk_i2c_pm, .pm = &nmk_i2c_pm,
}, },
.id_table = nmk_i2c_ids,
.probe = nmk_i2c_probe, .probe = nmk_i2c_probe,
.remove = __devexit_p(nmk_i2c_remove), .remove = nmk_i2c_remove,
}; };
static int __init nmk_i2c_init(void) static int __init nmk_i2c_init(void)
{ {
return platform_driver_register(&nmk_i2c_driver); return amba_driver_register(&nmk_i2c_driver);
} }
static void __exit nmk_i2c_exit(void) static void __exit nmk_i2c_exit(void)
{ {
platform_driver_unregister(&nmk_i2c_driver); amba_driver_unregister(&nmk_i2c_driver);
} }
subsys_initcall(nmk_i2c_init); subsys_initcall(nmk_i2c_init);
...@@ -1064,4 +1069,3 @@ module_exit(nmk_i2c_exit); ...@@ -1064,4 +1069,3 @@ module_exit(nmk_i2c_exit);
MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR"); MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");
MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver"); MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:" DRIVER_NAME);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment