can: c_can: Add runtime PM support to Bosch C_CAN/D_CAN controller
authorAnilKumar Ch <anilkumar@ti.com>
Mon, 20 Aug 2012 11:20:54 +0000 (16:50 +0530)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Fri, 21 Sep 2012 21:58:47 +0000 (23:58 +0200)
Add Runtime PM support to C_CAN/D_CAN controller. The runtime PM
APIs control clocks for C_CAN/D_CAN IP and prevent access to the
register of C_CAN/D_CAN IP when clock is turned off.

Signed-off-by: AnilKumar Ch <anilkumar@ti.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
drivers/net/can/c_can/c_can.c
drivers/net/can/c_can/c_can.h
drivers/net/can/c_can/c_can_platform.c

index 4c538e3886553f678729ebe2aa42affd021cc755..768bb481b077d26a740ac56dcb15447d1288067e 100644 (file)
@@ -34,6 +34,7 @@
 #include <linux/if_ether.h>
 #include <linux/list.h>
 #include <linux/io.h>
+#include <linux/pm_runtime.h>
 
 #include <linux/can.h>
 #include <linux/can/dev.h>
@@ -201,6 +202,30 @@ static const struct can_bittiming_const c_can_bittiming_const = {
        .brp_inc = 1,
 };
 
+static inline void c_can_pm_runtime_enable(const struct c_can_priv *priv)
+{
+       if (priv->device)
+               pm_runtime_enable(priv->device);
+}
+
+static inline void c_can_pm_runtime_disable(const struct c_can_priv *priv)
+{
+       if (priv->device)
+               pm_runtime_disable(priv->device);
+}
+
+static inline void c_can_pm_runtime_get_sync(const struct c_can_priv *priv)
+{
+       if (priv->device)
+               pm_runtime_get_sync(priv->device);
+}
+
+static inline void c_can_pm_runtime_put_sync(const struct c_can_priv *priv)
+{
+       if (priv->device)
+               pm_runtime_put_sync(priv->device);
+}
+
 static inline int get_tx_next_msg_obj(const struct c_can_priv *priv)
 {
        return (priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) +
@@ -673,11 +698,15 @@ static int c_can_get_berr_counter(const struct net_device *dev,
        unsigned int reg_err_counter;
        struct c_can_priv *priv = netdev_priv(dev);
 
+       c_can_pm_runtime_get_sync(priv);
+
        reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
        bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >>
                                ERR_CNT_REC_SHIFT;
        bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK;
 
+       c_can_pm_runtime_put_sync(priv);
+
        return 0;
 }
 
@@ -1053,11 +1082,13 @@ static int c_can_open(struct net_device *dev)
        int err;
        struct c_can_priv *priv = netdev_priv(dev);
 
+       c_can_pm_runtime_get_sync(priv);
+
        /* open the can device */
        err = open_candev(dev);
        if (err) {
                netdev_err(dev, "failed to open can device\n");
-               return err;
+               goto exit_open_fail;
        }
 
        /* register interrupt handler */
@@ -1079,6 +1110,8 @@ static int c_can_open(struct net_device *dev)
 
 exit_irq_fail:
        close_candev(dev);
+exit_open_fail:
+       c_can_pm_runtime_put_sync(priv);
        return err;
 }
 
@@ -1091,6 +1124,7 @@ static int c_can_close(struct net_device *dev)
        c_can_stop(dev);
        free_irq(dev->irq, dev);
        close_candev(dev);
+       c_can_pm_runtime_put_sync(priv);
 
        return 0;
 }
@@ -1133,10 +1167,19 @@ static const struct net_device_ops c_can_netdev_ops = {
 
 int register_c_can_dev(struct net_device *dev)
 {
+       struct c_can_priv *priv = netdev_priv(dev);
+       int err;
+
+       c_can_pm_runtime_enable(priv);
+
        dev->flags |= IFF_ECHO; /* we support local echo */
        dev->netdev_ops = &c_can_netdev_ops;
 
-       return register_candev(dev);
+       err = register_candev(dev);
+       if (err)
+               c_can_pm_runtime_disable(priv);
+
+       return err;
 }
 EXPORT_SYMBOL_GPL(register_c_can_dev);
 
@@ -1148,6 +1191,8 @@ void unregister_c_can_dev(struct net_device *dev)
        c_can_enable_all_interrupts(priv, DISABLE_ALL_INTERRUPTS);
 
        unregister_candev(dev);
+
+       c_can_pm_runtime_disable(priv);
 }
 EXPORT_SYMBOL_GPL(unregister_c_can_dev);
 
index 4e56baa9c2da28d386f314ed1e7ad37a088f065e..1437a6dbdca52191df22e9d0791d7ffa58fcf89f 100644 (file)
@@ -153,6 +153,7 @@ struct c_can_priv {
        struct can_priv can;    /* must be the first member */
        struct napi_struct napi;
        struct net_device *dev;
+       struct device *device;
        int tx_object;
        int current_status;
        int last_status;
index d0a66cf298bab322d9140318829b7883ef88ba8a..7b8b7a485d72625411f86397c110fc1e141b081a 100644 (file)
@@ -179,6 +179,7 @@ static int __devinit c_can_plat_probe(struct platform_device *pdev)
 
        dev->irq = irq;
        priv->base = addr;
+       priv->device = &pdev->dev;
        priv->can.clock.freq = clk_get_rate(clk);
        priv->priv = clk;