diff mbox

[v2,4/6] soc: mediatek: Add SMI driver

Message ID 1431683009-18158-5-git-send-email-yong.wu@mediatek.com (mailing list archive)
State New, archived
Headers show

Commit Message

Yong Wu (吴勇) May 15, 2015, 9:43 a.m. UTC
This patch add SMI(Smart Multimedia Interface) driver. This driver is
responsible to enable/disable iommu and control the clocks of each local arbiter.

Signed-off-by: Yong Wu <yong.wu@mediatek.com>
---
 drivers/soc/mediatek/Kconfig      |   6 +
 drivers/soc/mediatek/Makefile     |   1 +
 drivers/soc/mediatek/mt8173-smi.c | 298 ++++++++++++++++++++++++++++++++++++++
 include/linux/mtk-smi.h           |  39 +++++
 4 files changed, 344 insertions(+)
 create mode 100644 drivers/soc/mediatek/mt8173-smi.c
 create mode 100644 include/linux/mtk-smi.h

Comments

Matthias Brugger May 19, 2015, 11:14 a.m. UTC | #1
2015-05-15 11:43 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
>     This patch add SMI(Smart Multimedia Interface) driver. This driver is
> responsible to enable/disable iommu and control the clocks of each local arbiter.
>
> Signed-off-by: Yong Wu <yong.wu@mediatek.com>
> ---
>  drivers/soc/mediatek/Kconfig      |   6 +
>  drivers/soc/mediatek/Makefile     |   1 +
>  drivers/soc/mediatek/mt8173-smi.c | 298 ++++++++++++++++++++++++++++++++++++++
>  include/linux/mtk-smi.h           |  39 +++++
>  4 files changed, 344 insertions(+)
>  create mode 100644 drivers/soc/mediatek/mt8173-smi.c
>  create mode 100644 include/linux/mtk-smi.h
>
> diff --git a/drivers/soc/mediatek/Kconfig b/drivers/soc/mediatek/Kconfig
> index 1d34819..5935ead 100644
> --- a/drivers/soc/mediatek/Kconfig
> +++ b/drivers/soc/mediatek/Kconfig
> @@ -15,3 +15,9 @@ config MTK_SCPSYS
>         help
>           Say yes here to add support for the MediaTek SCPSYS power domain
>           driver.
> +
> +config MTK_SMI
> +       bool
> +       help
> +         Smi help enable/disable iommu in MediaTek SoCs and control the clock
> +         for each local arbiter.
> diff --git a/drivers/soc/mediatek/Makefile b/drivers/soc/mediatek/Makefile
> index ce88693..c086261 100644
> --- a/drivers/soc/mediatek/Makefile
> +++ b/drivers/soc/mediatek/Makefile
> @@ -1,2 +1,3 @@
>  obj-$(CONFIG_MTK_PMIC_WRAP) += mtk-pmic-wrap.o
>  obj-$(CONFIG_MTK_SCPSYS) += mtk-scpsys.o
> +obj-$(CONFIG_MTK_SMI) += mt8173-smi.o
> diff --git a/drivers/soc/mediatek/mt8173-smi.c b/drivers/soc/mediatek/mt8173-smi.c
> new file mode 100644
> index 0000000..67bccec
> --- /dev/null
> +++ b/drivers/soc/mediatek/mt8173-smi.c
> @@ -0,0 +1,298 @@
> +/*
> + * Copyright (c) 2014-2015 MediaTek Inc.
> + * Author: Yong Wu <yong.wu@mediatek.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + */
> +#include <linux/io.h>
> +#include <linux/interrupt.h>
> +#include <linux/platform_device.h>
> +#include <linux/clk.h>
> +#include <linux/err.h>
> +#include <linux/mm.h>
> +#include <linux/of_address.h>
> +#include <linux/of_irq.h>
> +#include <linux/of_platform.h>
> +#include <linux/mtk-smi.h>
> +
> +#define SMI_LARB_MMU_EN                (0xf00)
> +#define F_SMI_MMU_EN(port)     (1 << (port))
> +
> +enum {
> +       MTK_CLK_APB,
> +       MTK_CLK_SMI,
> +       MTK_CLK_MAX,

Maybe add something like:
MTK_CLK_FIRST = MTK_CLK_APB,
to make the for loops better readable.

> +};
> +
> +struct mtk_smi_common {
> +       void __iomem            *base;

That seems to be never used. Please delete it.

> +       struct clk              *clk[MTK_CLK_MAX];
> +};
> +
> +struct mtk_smi_larb {
> +       void __iomem            *base;
> +       spinlock_t              portlock; /* lock for config port */
> +       struct clk              *clk[MTK_CLK_MAX];
> +       struct device           *smi;
> +};
> +
> +static const char * const mtk_smi_clk_name[MTK_CLK_MAX] = {
> +       "apb", "smi"
> +};
> +
> +static int mtk_smi_common_get(struct device *smidev)
> +{
> +       struct mtk_smi_common *smipriv = dev_get_drvdata(smidev);
> +       int i, ret;
> +
> +       for (i = MTK_CLK_APB; i < MTK_CLK_MAX; i++) {
> +               ret = clk_enable(smipriv->clk[i]);
> +               if (ret) {
> +                       dev_err(smidev,
> +                               "Failed to enable the clock of smi_common\n");
> +                       goto err_smi_clk;
> +               }
> +       }
> +       return ret;
> +
> +err_smi_clk:
> +       if (i == MTK_CLK_SMI)
> +               clk_disable(smipriv->clk[MTK_CLK_APB]);
> +       return ret;
> +}
> +
> +static void mtk_smi_common_put(struct device *smidev)
> +{
> +       struct mtk_smi_common *smipriv = dev_get_drvdata(smidev);
> +       int i;
> +
> +       for (i = MTK_CLK_SMI; i >= MTK_CLK_APB; i--)
> +               clk_disable(smipriv->clk[i]);
> +}
> +
> +int mtk_smi_larb_get(struct device *larbdev)
> +{
> +       struct mtk_smi_larb *larbpriv = dev_get_drvdata(larbdev);
> +       int i, ret = 0;
> +
> +       ret = mtk_smi_common_get(larbpriv->smi);
> +       if (ret)
> +               return ret;
> +
> +       for (i = MTK_CLK_APB; i < MTK_CLK_MAX; i++) {
> +               ret = clk_enable(larbpriv->clk[i]);
> +               if (ret) {
> +                       dev_err(larbdev,
> +                               "Failed to enable larb clock%d. ret 0x%x\n",
> +                               i, ret);
> +                       goto err_larb_clk;
> +               }
> +       }
> +
> +       return ret;
> +
> +err_larb_clk:
> +       if (i == MTK_CLK_SMI)
> +               clk_disable(larbpriv->clk[MTK_CLK_APB]);
> +       mtk_smi_common_put(larbpriv->smi);
> +       return ret;
> +}
> +
> +void mtk_smi_larb_put(struct device *larbdev)

You can pass mtk_smi_larb as parameter instead of struct device

> +{
> +       struct mtk_smi_larb *larbpriv = dev_get_drvdata(larbdev);
> +       int i;
> +
> +       for (i = MTK_CLK_SMI; i >= MTK_CLK_APB; i--)
> +               clk_disable(larbpriv->clk[i]);
> +
> +       mtk_smi_common_put(larbpriv->smi);
> +}
> +
> +int mtk_smi_config_port(struct device *larbdev,        unsigned int larbportid,
> +                       bool iommuen)
> +{
> +       struct mtk_smi_larb *larbpriv = dev_get_drvdata(larbdev);
> +       unsigned long flags;
> +       int ret;
> +       u32 reg;
> +
> +       ret = mtk_smi_larb_get(larbdev);
> +       if (ret)
> +               return ret;
> +
> +       spin_lock_irqsave(&larbpriv->portlock, flags);
> +       reg = readl(larbpriv->base + SMI_LARB_MMU_EN);
> +       reg &= ~F_SMI_MMU_EN(larbportid);
> +       if (iommuen)
> +               reg |= F_SMI_MMU_EN(larbportid);
> +       writel(reg, larbpriv->base + SMI_LARB_MMU_EN);
> +       spin_unlock_irqrestore(&larbpriv->portlock, flags);
> +
> +       mtk_smi_larb_put(larbdev);
> +
> +       return 0;
> +}
> +
> +static int mtk_smi_larb_probe(struct platform_device *pdev)
> +{
> +       struct mtk_smi_larb *larbpriv;
> +       struct resource *res;
> +       struct device *dev = &pdev->dev;
> +       struct device_node *smi_node;
> +       struct platform_device *smi_pdev;
> +       int i, ret;
> +
> +       larbpriv = devm_kzalloc(dev, sizeof(struct mtk_smi_larb), GFP_KERNEL);
> +       if (!larbpriv)
> +               return -ENOMEM;
> +
> +       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +       larbpriv->base = devm_ioremap_resource(dev, res);
> +       if (IS_ERR(larbpriv->base)) {
> +               dev_err(dev, "Failed to get the larbbase (0x%lx)\n",
> +                       PTR_ERR(larbpriv->base));
> +               return PTR_ERR(larbpriv->base);
> +       }
> +
> +       for (i = MTK_CLK_APB; i < MTK_CLK_MAX; i++) {
> +               larbpriv->clk[i] = devm_clk_get(dev, mtk_smi_clk_name[i]);
> +
> +               if (IS_ERR(larbpriv->clk[i])) {

Please delete the blank line before the if.

> +                       ret = PTR_ERR(larbpriv->clk[i]);
> +                       dev_err(dev, "Failed to get larb%d clock (0x%x)\n",
> +                               i, ret);
> +                       goto fail_larb_clk;
> +               } else {
> +                       ret = clk_prepare(larbpriv->clk[i]);
> +                       if (ret) {
> +                               dev_err(dev, "Failed to prepare larb clock%d (0x%x)\n",
> +                                       i, ret);
> +                               goto fail_larb_clk;
> +                       }
> +               }
> +       }
> +
> +       smi_node = of_parse_phandle(dev->of_node, "smi", 0);
> +       if (!smi_node) {
> +               dev_err(dev, "Failed to get smi node\n");
> +               ret = -EINVAL;
> +               goto fail_larb_clk;
> +       }
> +
> +       smi_pdev = of_find_device_by_node(smi_node);
> +       of_node_put(smi_node);
> +       if (smi_pdev) {
> +               larbpriv->smi = &smi_pdev->dev;
> +       } else {
> +               dev_err(dev, "Failed to get the smi_common device\n");
> +               ret = -EINVAL;
> +               goto fail_larb_clk;
> +       }
> +
> +       spin_lock_init(&larbpriv->portlock);
> +       dev_set_drvdata(dev, larbpriv);
> +       return 0;
> +
> +fail_larb_clk:
> +       while (--i >= MTK_CLK_APB)
> +               clk_unprepare(larbpriv->clk[i]);
> +       return ret;
> +}
> +
> +static const struct of_device_id mtk_smi_larb_of_ids[] = {
> +       { .compatible = "mediatek,mt8173-smi-larb",
> +       },
> +       {}
> +};
> +
> +static struct platform_driver mtk_smi_larb_driver = {
> +       .probe  = mtk_smi_larb_probe,
> +       .driver = {
> +               .name = "mtksmilarb",

Huh, what about something more readable like mtk-smi-larb?

> +               .of_match_table = mtk_smi_larb_of_ids,
> +       }
> +};
> +
> +static int mtk_smi_probe(struct platform_device *pdev)
> +{
> +       struct device *dev = &pdev->dev;
> +       struct mtk_smi_common *smipriv;
> +       int ret, i;
> +
> +       smipriv = devm_kzalloc(dev, sizeof(*smipriv), GFP_KERNEL);
> +       if (!smipriv)
> +               return -ENOMEM;
> +
> +       for (i = MTK_CLK_APB; i < MTK_CLK_MAX; i++) {
> +               smipriv->clk[i] = devm_clk_get(dev, mtk_smi_clk_name[i]);
> +
> +               if (IS_ERR(smipriv->clk[i])) {
> +                       ret = PTR_ERR(smipriv->clk[i]);
> +                       dev_err(dev, "Failed to get smi-%s clock (0x%x)\n",
> +                               mtk_smi_clk_name[i], ret);
> +                       goto fail_smi_clk;
> +               } else {
> +                       ret = clk_prepare(smipriv->clk[i]);
> +                       if (ret) {
> +                               dev_err(dev, "Failed to prepare smi%d clock 0x%x\n",
> +                                       i, ret);
> +                               goto fail_smi_clk;
> +                       }
> +               }
> +       }
> +
> +       dev_set_drvdata(dev, smipriv);
> +       return ret;
> +
> +fail_smi_clk:
> +       if (i == MTK_CLK_SMI)
> +               clk_unprepare(smipriv->clk[MTK_CLK_APB]);
> +       return ret;
> +}
> +
> +static const struct of_device_id mtk_smi_of_ids[] = {
> +       { .compatible = "mediatek,mt8173-smi",
> +       },
> +       {}
> +};
> +
> +static struct platform_driver mtk_smi_driver = {
> +       .probe  = mtk_smi_probe,
> +       .driver = {
> +               .name = "mtksmi",

Same here.

Thanks,
Matthias
Yong Wu (吴勇) May 21, 2015, 6:16 a.m. UTC | #2
Hi Matthias,
     Thanks very much for your suggestion.
     Abort the smi clock name, Could you help check below.
     The others I will improve in next time.

On Tue, 2015-05-19 at 13:14 +0200, Matthias Brugger wrote:
> 2015-05-15 11:43 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
> >     This patch add SMI(Smart Multimedia Interface) driver. This driver is
> > responsible to enable/disable iommu and control the clocks of each local arbiter.
> >
[snip]
> > +
> > +#define SMI_LARB_MMU_EN                (0xf00)
> > +#define F_SMI_MMU_EN(port)     (1 << (port))
> > +
> > +enum {
> > +       MTK_CLK_APB,
> > +       MTK_CLK_SMI,
> > +       MTK_CLK_MAX,
> 
> Maybe add something like:
> MTK_CLK_FIRST = MTK_CLK_APB,
> to make the for loops better readable.
> 
Then, Is it like this? : 
 enum {
        MTK_CLK_FIRST = MTK_CLK_APB,
        MTK_CLK_SMI,
        MTK_CLK_MAX,
 }
or the CLK_SMI also need MTK_CLK_SECOND = MTK_CLK_SMI.
> > +};
> > +
> > +struct mtk_smi_common {
> > +       void __iomem            *base;
> 
> That seems to be never used. Please delete it.
> 
> > +       struct clk              *clk[MTK_CLK_MAX];
> > +};
> > +
> > +struct mtk_smi_larb {
> > +       void __iomem            *base;
> > +       spinlock_t              portlock; /* lock for config port */
> > +       struct clk              *clk[MTK_CLK_MAX];
> > +       struct device           *smi;
> > +};
> > +
> Thanks,
> Matthias
Matthias Brugger May 21, 2015, 7:30 a.m. UTC | #3
2015-05-21 8:16 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
> Hi Matthias,
>      Thanks very much for your suggestion.
>      Abort the smi clock name, Could you help check below.
>      The others I will improve in next time.
>
> On Tue, 2015-05-19 at 13:14 +0200, Matthias Brugger wrote:
>> 2015-05-15 11:43 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
>> >     This patch add SMI(Smart Multimedia Interface) driver. This driver is
>> > responsible to enable/disable iommu and control the clocks of each local arbiter.
>> >
> [snip]
>> > +
>> > +#define SMI_LARB_MMU_EN                (0xf00)
>> > +#define F_SMI_MMU_EN(port)     (1 << (port))
>> > +
>> > +enum {
>> > +       MTK_CLK_APB,
>> > +       MTK_CLK_SMI,
>> > +       MTK_CLK_MAX,
>>
>> Maybe add something like:
>> MTK_CLK_FIRST = MTK_CLK_APB,
>> to make the for loops better readable.
>>
> Then, Is it like this? :
>  enum {
>         MTK_CLK_FIRST = MTK_CLK_APB,
>         MTK_CLK_SMI,
>         MTK_CLK_MAX,
>  }
> or the CLK_SMI also need MTK_CLK_SECOND = MTK_CLK_SMI.


something like:
enum {
         MTK_CLK_FIRST,
         MTK_CLK_APB = MTK_CLK_FIRST,
         MTK_CLK_SMI,
         MTK_CLK_LAST,
}

So you can rewrite the for loop:
if (i = MTK_CLK_FIRST; i < MTK_CLK_LAST; i++)

Regards,
Matthias

>> > +};
>> > +
>> > +struct mtk_smi_common {
>> > +       void __iomem            *base;
>>
>> That seems to be never used. Please delete it.
>>
>> > +       struct clk              *clk[MTK_CLK_MAX];
>> > +};
>> > +
>> > +struct mtk_smi_larb {
>> > +       void __iomem            *base;
>> > +       spinlock_t              portlock; /* lock for config port */
>> > +       struct clk              *clk[MTK_CLK_MAX];
>> > +       struct device           *smi;
>> > +};
>> > +
>> Thanks,
>> Matthias
>
>
Yong Wu (吴勇) May 21, 2015, 7:38 a.m. UTC | #4
On Thu, 2015-05-21 at 09:30 +0200, Matthias Brugger wrote:
> 2015-05-21 8:16 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
> > Hi Matthias,
> >      Thanks very much for your suggestion.
> >      Abort the smi clock name, Could you help check below.
> >      The others I will improve in next time.
> >
> > On Tue, 2015-05-19 at 13:14 +0200, Matthias Brugger wrote:
> >> 2015-05-15 11:43 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
> >> >     This patch add SMI(Smart Multimedia Interface) driver. This driver is
> >> > responsible to enable/disable iommu and control the clocks of each local arbiter.
> >> >
> > [snip]
> >> > +
> >> > +#define SMI_LARB_MMU_EN                (0xf00)
> >> > +#define F_SMI_MMU_EN(port)     (1 << (port))
> >> > +
> >> > +enum {
> >> > +       MTK_CLK_APB,
> >> > +       MTK_CLK_SMI,
> >> > +       MTK_CLK_MAX,
> >>
> >> Maybe add something like:
> >> MTK_CLK_FIRST = MTK_CLK_APB,
> >> to make the for loops better readable.
> >>
> > Then, Is it like this? :
> >  enum {
> >         MTK_CLK_FIRST = MTK_CLK_APB,
> >         MTK_CLK_SMI,
> >         MTK_CLK_MAX,
> >  }
> > or the CLK_SMI also need MTK_CLK_SECOND = MTK_CLK_SMI.
> 
> 
> something like:
> enum {
>          MTK_CLK_FIRST,
>          MTK_CLK_APB = MTK_CLK_FIRST,
>          MTK_CLK_SMI,
>          MTK_CLK_LAST,
> }
> 
> So you can rewrite the for loop:
> if (i = MTK_CLK_FIRST; i < MTK_CLK_LAST; i++)
Get it.
Thanks very much.
> 
> Regards,
> Matthias
> 
> >> > +};
> >> > +
> >> > +struct mtk_smi_common {
> >> > +       void __iomem            *base;
> >>
> >> That seems to be never used. Please delete it.
> >>
> >> > +       struct clk              *clk[MTK_CLK_MAX];
> >> > +};
> >> > +
> >> > +struct mtk_smi_larb {
> >> > +       void __iomem            *base;
> >> > +       spinlock_t              portlock; /* lock for config port */
> >> > +       struct clk              *clk[MTK_CLK_MAX];
> >> > +       struct device           *smi;
> >> > +};
> >> > +
> >> Thanks,
> >> Matthias
> >
> >
> 
> 
>
Daniel Kurtz May 21, 2015, 2:33 p.m. UTC | #5
On Thu, May 21, 2015 at 3:30 PM, Matthias Brugger
<matthias.bgg@gmail.com> wrote:
> 2015-05-21 8:16 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
>> Hi Matthias,
>>      Thanks very much for your suggestion.
>>      Abort the smi clock name, Could you help check below.
>>      The others I will improve in next time.
>>
>> On Tue, 2015-05-19 at 13:14 +0200, Matthias Brugger wrote:
>>> 2015-05-15 11:43 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
>>> >     This patch add SMI(Smart Multimedia Interface) driver. This driver is
>>> > responsible to enable/disable iommu and control the clocks of each local arbiter.
>>> >
>> [snip]
>>> > +
>>> > +#define SMI_LARB_MMU_EN                (0xf00)
>>> > +#define F_SMI_MMU_EN(port)     (1 << (port))
>>> > +
>>> > +enum {
>>> > +       MTK_CLK_APB,
>>> > +       MTK_CLK_SMI,
>>> > +       MTK_CLK_MAX,
>>>
>>> Maybe add something like:
>>> MTK_CLK_FIRST = MTK_CLK_APB,
>>> to make the for loops better readable.
>>>
>> Then, Is it like this? :
>>  enum {
>>         MTK_CLK_FIRST = MTK_CLK_APB,
>>         MTK_CLK_SMI,
>>         MTK_CLK_MAX,
>>  }
>> or the CLK_SMI also need MTK_CLK_SECOND = MTK_CLK_SMI.
>
>
> something like:
> enum {
>          MTK_CLK_FIRST,
>          MTK_CLK_APB = MTK_CLK_FIRST,
>          MTK_CLK_SMI,
>          MTK_CLK_LAST,
> }
>
> So you can rewrite the for loop:
> if (i = MTK_CLK_FIRST; i < MTK_CLK_LAST; i++)

Actually, do we ever plan to add more clks per smi node?
If not, perhaps the whole driver would be simpler if you just
explicitly handle the apb & smi clocks:

struct mtk_smi_larb {
       void __iomem            *base;
       spinlock_t              portlock; /* lock for config port */
       struct device           *smi;
       struct clk              *clk_apb;
       struct clk              *clk_smi;
};

And then all of the loops become just a pair of clock operations.

Best Regards,
-Dan

>
> Regards,
> Matthias
>
>>> > +};
>>> > +
>>> > +struct mtk_smi_common {
>>> > +       void __iomem            *base;
>>>
>>> That seems to be never used. Please delete it.
>>>
>>> > +       struct clk              *clk[MTK_CLK_MAX];
>>> > +};
>>> > +
>>> > +struct mtk_smi_larb {
>>> > +       void __iomem            *base;
>>> > +       spinlock_t              portlock; /* lock for config port */
>>> > +       struct clk              *clk[MTK_CLK_MAX];
>>> > +       struct device           *smi;
>>> > +};
>>> > +
>>> Thanks,
>>> Matthias
>>
>>
>
>
>
> --
> motzblog.wordpress.com
Yong Wu (吴勇) May 21, 2015, 2:49 p.m. UTC | #6
On Thu, 2015-05-21 at 22:33 +0800, Daniel Kurtz wrote:
> On Thu, May 21, 2015 at 3:30 PM, Matthias Brugger
> <matthias.bgg@gmail.com> wrote:
> > 2015-05-21 8:16 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
> >> Hi Matthias,
> >>      Thanks very much for your suggestion.
> >>      Abort the smi clock name, Could you help check below.
> >>      The others I will improve in next time.
> >>
> >> On Tue, 2015-05-19 at 13:14 +0200, Matthias Brugger wrote:
> >>> 2015-05-15 11:43 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
> >>> >     This patch add SMI(Smart Multimedia Interface) driver. This driver is
> >>> > responsible to enable/disable iommu and control the clocks of each local arbiter.
> >>> >
> >> [snip]
> >>> > +
> >>> > +#define SMI_LARB_MMU_EN                (0xf00)
> >>> > +#define F_SMI_MMU_EN(port)     (1 << (port))
> >>> > +
> >>> > +enum {
> >>> > +       MTK_CLK_APB,
> >>> > +       MTK_CLK_SMI,
> >>> > +       MTK_CLK_MAX,
> >>>
> >>> Maybe add something like:
> >>> MTK_CLK_FIRST = MTK_CLK_APB,
> >>> to make the for loops better readable.
> >>>
> >> Then, Is it like this? :
> >>  enum {
> >>         MTK_CLK_FIRST = MTK_CLK_APB,
> >>         MTK_CLK_SMI,
> >>         MTK_CLK_MAX,
> >>  }
> >> or the CLK_SMI also need MTK_CLK_SECOND = MTK_CLK_SMI.
> >
> >
> > something like:
> > enum {
> >          MTK_CLK_FIRST,
> >          MTK_CLK_APB = MTK_CLK_FIRST,
> >          MTK_CLK_SMI,
> >          MTK_CLK_LAST,
> > }
> >
> > So you can rewrite the for loop:
> > if (i = MTK_CLK_FIRST; i < MTK_CLK_LAST; i++)
> 
> Actually, do we ever plan to add more clks per smi node?
No.
> If not, perhaps the whole driver would be simpler if you just
> explicitly handle the apb & smi clocks:
> 
> struct mtk_smi_larb {
>        void __iomem            *base;
>        spinlock_t              portlock; /* lock for config port */
>        struct device           *smi;
>        struct clk              *clk_apb;
>        struct clk              *clk_smi;
> };
> 
> And then all of the loops become just a pair of clock operations.
Thanks. I will try this and compare whether it will add many lines.
> 
> Best Regards,
> -Dan
> 
> >
> > Regards,
> > Matthias
> >
> >>> > +};
> >>> > +
> >>> > +struct mtk_smi_common {
> >>> > +       void __iomem            *base;
> >>>
> >>> That seems to be never used. Please delete it.
> >>>
> >>> > +       struct clk              *clk[MTK_CLK_MAX];
> >>> > +};
> >>> > +
> >>> > +struct mtk_smi_larb {
> >>> > +       void __iomem            *base;
> >>> > +       spinlock_t              portlock; /* lock for config port */
> >>> > +       struct clk              *clk[MTK_CLK_MAX];
> >>> > +       struct device           *smi;
> >>> > +};
> >>> > +
> >>> Thanks,
> >>> Matthias
> >>
> > --
> > motzblog.wordpress.com
Matthias Brugger May 21, 2015, 3:20 p.m. UTC | #7
2015-05-21 16:49 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
> On Thu, 2015-05-21 at 22:33 +0800, Daniel Kurtz wrote:
>> On Thu, May 21, 2015 at 3:30 PM, Matthias Brugger
>> <matthias.bgg@gmail.com> wrote:
>> > 2015-05-21 8:16 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
>> >> Hi Matthias,
>> >>      Thanks very much for your suggestion.
>> >>      Abort the smi clock name, Could you help check below.
>> >>      The others I will improve in next time.
>> >>
>> >> On Tue, 2015-05-19 at 13:14 +0200, Matthias Brugger wrote:
>> >>> 2015-05-15 11:43 GMT+02:00 Yong Wu <yong.wu@mediatek.com>:
>> >>> >     This patch add SMI(Smart Multimedia Interface) driver. This driver is
>> >>> > responsible to enable/disable iommu and control the clocks of each local arbiter.
>> >>> >
>> >> [snip]
>> >>> > +
>> >>> > +#define SMI_LARB_MMU_EN                (0xf00)
>> >>> > +#define F_SMI_MMU_EN(port)     (1 << (port))
>> >>> > +
>> >>> > +enum {
>> >>> > +       MTK_CLK_APB,
>> >>> > +       MTK_CLK_SMI,
>> >>> > +       MTK_CLK_MAX,
>> >>>
>> >>> Maybe add something like:
>> >>> MTK_CLK_FIRST = MTK_CLK_APB,
>> >>> to make the for loops better readable.
>> >>>
>> >> Then, Is it like this? :
>> >>  enum {
>> >>         MTK_CLK_FIRST = MTK_CLK_APB,
>> >>         MTK_CLK_SMI,
>> >>         MTK_CLK_MAX,
>> >>  }
>> >> or the CLK_SMI also need MTK_CLK_SECOND = MTK_CLK_SMI.
>> >
>> >
>> > something like:
>> > enum {
>> >          MTK_CLK_FIRST,
>> >          MTK_CLK_APB = MTK_CLK_FIRST,
>> >          MTK_CLK_SMI,
>> >          MTK_CLK_LAST,
>> > }
>> >
>> > So you can rewrite the for loop:
>> > if (i = MTK_CLK_FIRST; i < MTK_CLK_LAST; i++)
>>
>> Actually, do we ever plan to add more clks per smi node?
> No.
>> If not, perhaps the whole driver would be simpler if you just
>> explicitly handle the apb & smi clocks:
>>
>> struct mtk_smi_larb {
>>        void __iomem            *base;
>>        spinlock_t              portlock; /* lock for config port */
>>        struct device           *smi;
>>        struct clk              *clk_apb;
>>        struct clk              *clk_smi;
>> };
>>
>> And then all of the loops become just a pair of clock operations.
> Thanks. I will try this and compare whether it will add many lines.

If you don't plan to add more clocks in the future, then I think the
suggestion Dan made is the best one.

Regards,
Matthias
diff mbox

Patch

diff --git a/drivers/soc/mediatek/Kconfig b/drivers/soc/mediatek/Kconfig
index 1d34819..5935ead 100644
--- a/drivers/soc/mediatek/Kconfig
+++ b/drivers/soc/mediatek/Kconfig
@@ -15,3 +15,9 @@  config MTK_SCPSYS
 	help
 	  Say yes here to add support for the MediaTek SCPSYS power domain
 	  driver.
+
+config MTK_SMI
+	bool
+	help
+	  Smi help enable/disable iommu in MediaTek SoCs and control the clock
+	  for each local arbiter.
diff --git a/drivers/soc/mediatek/Makefile b/drivers/soc/mediatek/Makefile
index ce88693..c086261 100644
--- a/drivers/soc/mediatek/Makefile
+++ b/drivers/soc/mediatek/Makefile
@@ -1,2 +1,3 @@ 
 obj-$(CONFIG_MTK_PMIC_WRAP) += mtk-pmic-wrap.o
 obj-$(CONFIG_MTK_SCPSYS) += mtk-scpsys.o
+obj-$(CONFIG_MTK_SMI) += mt8173-smi.o
diff --git a/drivers/soc/mediatek/mt8173-smi.c b/drivers/soc/mediatek/mt8173-smi.c
new file mode 100644
index 0000000..67bccec
--- /dev/null
+++ b/drivers/soc/mediatek/mt8173-smi.c
@@ -0,0 +1,298 @@ 
+/*
+ * Copyright (c) 2014-2015 MediaTek Inc.
+ * Author: Yong Wu <yong.wu@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#include <linux/io.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/clk.h>
+#include <linux/err.h>
+#include <linux/mm.h>
+#include <linux/of_address.h>
+#include <linux/of_irq.h>
+#include <linux/of_platform.h>
+#include <linux/mtk-smi.h>
+
+#define SMI_LARB_MMU_EN		(0xf00)
+#define F_SMI_MMU_EN(port)	(1 << (port))
+
+enum {
+	MTK_CLK_APB,
+	MTK_CLK_SMI,
+	MTK_CLK_MAX,
+};
+
+struct mtk_smi_common {
+	void __iomem		*base;
+	struct clk		*clk[MTK_CLK_MAX];
+};
+
+struct mtk_smi_larb {
+	void __iomem		*base;
+	spinlock_t		portlock; /* lock for config port */
+	struct clk		*clk[MTK_CLK_MAX];
+	struct device		*smi;
+};
+
+static const char * const mtk_smi_clk_name[MTK_CLK_MAX] = {
+	"apb", "smi"
+};
+
+static int mtk_smi_common_get(struct device *smidev)
+{
+	struct mtk_smi_common *smipriv = dev_get_drvdata(smidev);
+	int i, ret;
+
+	for (i = MTK_CLK_APB; i < MTK_CLK_MAX; i++) {
+		ret = clk_enable(smipriv->clk[i]);
+		if (ret) {
+			dev_err(smidev,
+				"Failed to enable the clock of smi_common\n");
+			goto err_smi_clk;
+		}
+	}
+	return ret;
+
+err_smi_clk:
+	if (i == MTK_CLK_SMI)
+		clk_disable(smipriv->clk[MTK_CLK_APB]);
+	return ret;
+}
+
+static void mtk_smi_common_put(struct device *smidev)
+{
+	struct mtk_smi_common *smipriv = dev_get_drvdata(smidev);
+	int i;
+
+	for (i = MTK_CLK_SMI; i >= MTK_CLK_APB; i--)
+		clk_disable(smipriv->clk[i]);
+}
+
+int mtk_smi_larb_get(struct device *larbdev)
+{
+	struct mtk_smi_larb *larbpriv = dev_get_drvdata(larbdev);
+	int i, ret = 0;
+
+	ret = mtk_smi_common_get(larbpriv->smi);
+	if (ret)
+		return ret;
+
+	for (i = MTK_CLK_APB; i < MTK_CLK_MAX; i++) {
+		ret = clk_enable(larbpriv->clk[i]);
+		if (ret) {
+			dev_err(larbdev,
+				"Failed to enable larb clock%d. ret 0x%x\n",
+				i, ret);
+			goto err_larb_clk;
+		}
+	}
+
+	return ret;
+
+err_larb_clk:
+	if (i == MTK_CLK_SMI)
+		clk_disable(larbpriv->clk[MTK_CLK_APB]);
+	mtk_smi_common_put(larbpriv->smi);
+	return ret;
+}
+
+void mtk_smi_larb_put(struct device *larbdev)
+{
+	struct mtk_smi_larb *larbpriv = dev_get_drvdata(larbdev);
+	int i;
+
+	for (i = MTK_CLK_SMI; i >= MTK_CLK_APB; i--)
+		clk_disable(larbpriv->clk[i]);
+
+	mtk_smi_common_put(larbpriv->smi);
+}
+
+int mtk_smi_config_port(struct device *larbdev,	unsigned int larbportid,
+			bool iommuen)
+{
+	struct mtk_smi_larb *larbpriv = dev_get_drvdata(larbdev);
+	unsigned long flags;
+	int ret;
+	u32 reg;
+
+	ret = mtk_smi_larb_get(larbdev);
+	if (ret)
+		return ret;
+
+	spin_lock_irqsave(&larbpriv->portlock, flags);
+	reg = readl(larbpriv->base + SMI_LARB_MMU_EN);
+	reg &= ~F_SMI_MMU_EN(larbportid);
+	if (iommuen)
+		reg |= F_SMI_MMU_EN(larbportid);
+	writel(reg, larbpriv->base + SMI_LARB_MMU_EN);
+	spin_unlock_irqrestore(&larbpriv->portlock, flags);
+
+	mtk_smi_larb_put(larbdev);
+
+	return 0;
+}
+
+static int mtk_smi_larb_probe(struct platform_device *pdev)
+{
+	struct mtk_smi_larb *larbpriv;
+	struct resource *res;
+	struct device *dev = &pdev->dev;
+	struct device_node *smi_node;
+	struct platform_device *smi_pdev;
+	int i, ret;
+
+	larbpriv = devm_kzalloc(dev, sizeof(struct mtk_smi_larb), GFP_KERNEL);
+	if (!larbpriv)
+		return -ENOMEM;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	larbpriv->base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(larbpriv->base)) {
+		dev_err(dev, "Failed to get the larbbase (0x%lx)\n",
+			PTR_ERR(larbpriv->base));
+		return PTR_ERR(larbpriv->base);
+	}
+
+	for (i = MTK_CLK_APB; i < MTK_CLK_MAX; i++) {
+		larbpriv->clk[i] = devm_clk_get(dev, mtk_smi_clk_name[i]);
+
+		if (IS_ERR(larbpriv->clk[i])) {
+			ret = PTR_ERR(larbpriv->clk[i]);
+			dev_err(dev, "Failed to get larb%d clock (0x%x)\n",
+				i, ret);
+			goto fail_larb_clk;
+		} else {
+			ret = clk_prepare(larbpriv->clk[i]);
+			if (ret) {
+				dev_err(dev, "Failed to prepare larb clock%d (0x%x)\n",
+					i, ret);
+				goto fail_larb_clk;
+			}
+		}
+	}
+
+	smi_node = of_parse_phandle(dev->of_node, "smi", 0);
+	if (!smi_node) {
+		dev_err(dev, "Failed to get smi node\n");
+		ret = -EINVAL;
+		goto fail_larb_clk;
+	}
+
+	smi_pdev = of_find_device_by_node(smi_node);
+	of_node_put(smi_node);
+	if (smi_pdev) {
+		larbpriv->smi = &smi_pdev->dev;
+	} else {
+		dev_err(dev, "Failed to get the smi_common device\n");
+		ret = -EINVAL;
+		goto fail_larb_clk;
+	}
+
+	spin_lock_init(&larbpriv->portlock);
+	dev_set_drvdata(dev, larbpriv);
+	return 0;
+
+fail_larb_clk:
+	while (--i >= MTK_CLK_APB)
+		clk_unprepare(larbpriv->clk[i]);
+	return ret;
+}
+
+static const struct of_device_id mtk_smi_larb_of_ids[] = {
+	{ .compatible = "mediatek,mt8173-smi-larb",
+	},
+	{}
+};
+
+static struct platform_driver mtk_smi_larb_driver = {
+	.probe	= mtk_smi_larb_probe,
+	.driver	= {
+		.name = "mtksmilarb",
+		.of_match_table = mtk_smi_larb_of_ids,
+	}
+};
+
+static int mtk_smi_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct mtk_smi_common *smipriv;
+	int ret, i;
+
+	smipriv = devm_kzalloc(dev, sizeof(*smipriv), GFP_KERNEL);
+	if (!smipriv)
+		return -ENOMEM;
+
+	for (i = MTK_CLK_APB; i < MTK_CLK_MAX; i++) {
+		smipriv->clk[i] = devm_clk_get(dev, mtk_smi_clk_name[i]);
+
+		if (IS_ERR(smipriv->clk[i])) {
+			ret = PTR_ERR(smipriv->clk[i]);
+			dev_err(dev, "Failed to get smi-%s clock (0x%x)\n",
+				mtk_smi_clk_name[i], ret);
+			goto fail_smi_clk;
+		} else {
+			ret = clk_prepare(smipriv->clk[i]);
+			if (ret) {
+				dev_err(dev, "Failed to prepare smi%d clock 0x%x\n",
+					i, ret);
+				goto fail_smi_clk;
+			}
+		}
+	}
+
+	dev_set_drvdata(dev, smipriv);
+	return ret;
+
+fail_smi_clk:
+	if (i == MTK_CLK_SMI)
+		clk_unprepare(smipriv->clk[MTK_CLK_APB]);
+	return ret;
+}
+
+static const struct of_device_id mtk_smi_of_ids[] = {
+	{ .compatible = "mediatek,mt8173-smi",
+	},
+	{}
+};
+
+static struct platform_driver mtk_smi_driver = {
+	.probe	= mtk_smi_probe,
+	.driver	= {
+		.name = "mtksmi",
+		.of_match_table = mtk_smi_of_ids,
+	}
+};
+
+static int __init mtk_smi_init(void)
+{
+	int ret;
+
+	ret = platform_driver_register(&mtk_smi_driver);
+	if (ret != 0) {
+		pr_err("Failed to register SMI driver\n");
+		return ret;
+	}
+
+	ret = platform_driver_register(&mtk_smi_larb_driver);
+	if (ret != 0) {
+		pr_err("Failed to register SMI-LARB driver\n");
+		goto fail_smi_larb;
+	}
+	return ret;
+
+fail_smi_larb:
+	platform_driver_unregister(&mtk_smi_driver);
+	return ret;
+}
+
+subsys_initcall(mtk_smi_init);
+
diff --git a/include/linux/mtk-smi.h b/include/linux/mtk-smi.h
new file mode 100644
index 0000000..ad07e6a
--- /dev/null
+++ b/include/linux/mtk-smi.h
@@ -0,0 +1,39 @@ 
+/*
+ * Copyright (c) 2014-2015 MediaTek Inc.
+ * Author: Yong Wu <yong.wu@mediatek.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#ifndef MTK_IOMMU_SMI_H
+#define MTK_IOMMU_SMI_H
+#include <linux/device.h>
+
+/*
+ * Enable iommu for each port, it is only for iommu.
+ *
+ * Returns 0 if successfully, others if failed.
+ */
+int mtk_smi_config_port(struct device *larbdev,	unsigned int larbportid,
+			bool iommuen);
+/*
+ * The multimedia module should call the two function below
+ * which help open/close the clock of the larb.
+ * so the client dtsi should add the larb like "larb = <&larb0>"
+ * to get platform_device.
+ *
+ * mtk_smi_larb_get should be called before the multimedia h/w work.
+ * mtk_smi_larb_put should be called after h/w done.
+ *
+ * Returns 0 if successfully, others if failed.
+ */
+int mtk_smi_larb_get(struct device *plarbdev);
+void mtk_smi_larb_put(struct device *plarbdev);
+
+#endif