Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.git] / drivers / net / can / at91_can.c
index 81baefda037bfe1d9d6ceb1058162dfe0ce09d0d..db52f4414def171a4b76fffad88231589ddcc5c5 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/netdevice.h>
+#include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/rtnetlink.h>
 #include <linux/skbuff.h>
@@ -37,6 +38,7 @@
 
 #include <linux/can/dev.h>
 #include <linux/can/error.h>
+#include <linux/can/led.h>
 
 #define AT91_MB_MASK(i)                ((1 << (i)) - 1)
 
@@ -154,19 +156,20 @@ struct at91_priv {
        canid_t mb0_id;
 };
 
-static const struct at91_devtype_data at91_devtype_data[] = {
-       [AT91_DEVTYPE_SAM9263] = {
-               .rx_first = 1,
-               .rx_split = 8,
-               .rx_last = 11,
-               .tx_shift = 2,
-       },
-       [AT91_DEVTYPE_SAM9X5] = {
-               .rx_first = 0,
-               .rx_split = 4,
-               .rx_last = 5,
-               .tx_shift = 1,
-       },
+static const struct at91_devtype_data at91_at91sam9263_data = {
+       .rx_first = 1,
+       .rx_split = 8,
+       .rx_last = 11,
+       .tx_shift = 2,
+       .type = AT91_DEVTYPE_SAM9263,
+};
+
+static const struct at91_devtype_data at91_at91sam9x5_data = {
+       .rx_first = 0,
+       .rx_split = 4,
+       .rx_last = 5,
+       .tx_shift = 1,
+       .type = AT91_DEVTYPE_SAM9X5,
 };
 
 static const struct can_bittiming_const at91_bittiming_const = {
@@ -641,6 +644,8 @@ static void at91_read_msg(struct net_device *dev, unsigned int mb)
 
        stats->rx_packets++;
        stats->rx_bytes += cf->can_dlc;
+
+       can_led_event(dev, CAN_LED_EVENT_RX);
 }
 
 /**
@@ -875,6 +880,7 @@ static void at91_irq_tx(struct net_device *dev, u32 reg_sr)
                        /* _NOTE_: subtract AT91_MB_TX_FIRST offset from mb! */
                        can_get_echo_skb(dev, mb - get_mb_tx_first(priv));
                        dev->stats.tx_packets++;
+                       can_led_event(dev, CAN_LED_EVENT_TX);
                }
        }
 
@@ -1128,6 +1134,8 @@ static int at91_open(struct net_device *dev)
                goto out_close;
        }
 
+       can_led_event(dev, CAN_LED_EVENT_OPEN);
+
        /* start chip and queuing */
        at91_chip_start(dev);
        napi_enable(&priv->napi);
@@ -1159,6 +1167,8 @@ static int at91_close(struct net_device *dev)
 
        close_candev(dev);
 
+       can_led_event(dev, CAN_LED_EVENT_STOP);
+
        return 0;
 }
 
@@ -1241,10 +1251,42 @@ static struct attribute_group at91_sysfs_attr_group = {
        .attrs = at91_sysfs_attrs,
 };
 
+#if defined(CONFIG_OF)
+static const struct of_device_id at91_can_dt_ids[] = {
+       {
+               .compatible = "atmel,at91sam9x5-can",
+               .data = &at91_at91sam9x5_data,
+       }, {
+               .compatible = "atmel,at91sam9263-can",
+               .data = &at91_at91sam9263_data,
+       }, {
+               /* sentinel */
+       }
+};
+MODULE_DEVICE_TABLE(of, at91_can_dt_ids);
+#else
+#define at91_can_dt_ids NULL
+#endif
+
+static const struct at91_devtype_data *at91_can_get_driver_data(struct platform_device *pdev)
+{
+       if (pdev->dev.of_node) {
+               const struct of_device_id *match;
+
+               match = of_match_node(at91_can_dt_ids, pdev->dev.of_node);
+               if (!match) {
+                       dev_err(&pdev->dev, "no matching node found in dtb\n");
+                       return NULL;
+               }
+               return (const struct at91_devtype_data *)match->data;
+       }
+       return (const struct at91_devtype_data *)
+               platform_get_device_id(pdev)->driver_data;
+}
+
 static int at91_can_probe(struct platform_device *pdev)
 {
        const struct at91_devtype_data *devtype_data;
-       enum at91_devtype devtype;
        struct net_device *dev;
        struct at91_priv *priv;
        struct resource *res;
@@ -1252,8 +1294,12 @@ static int at91_can_probe(struct platform_device *pdev)
        void __iomem *addr;
        int err, irq;
 
-       devtype = pdev->id_entry->driver_data;
-       devtype_data = &at91_devtype_data[devtype];
+       devtype_data = at91_can_get_driver_data(pdev);
+       if (!devtype_data) {
+               dev_err(&pdev->dev, "no driver data\n");
+               err = -ENODEV;
+               goto exit;
+       }
 
        clk = clk_get(&pdev->dev, "can_clk");
        if (IS_ERR(clk)) {
@@ -1302,7 +1348,6 @@ static int at91_can_probe(struct platform_device *pdev)
        priv->dev = dev;
        priv->reg_base = addr;
        priv->devtype_data = *devtype_data;
-       priv->devtype_data.type = devtype;
        priv->clk = clk;
        priv->pdata = pdev->dev.platform_data;
        priv->mb0_id = 0x7ff;
@@ -1321,6 +1366,8 @@ static int at91_can_probe(struct platform_device *pdev)
                goto exit_free;
        }
 
+       devm_can_led_init(dev);
+
        dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
                 priv->reg_base, dev->irq);
 
@@ -1363,10 +1410,10 @@ static int at91_can_remove(struct platform_device *pdev)
 static const struct platform_device_id at91_can_id_table[] = {
        {
                .name = "at91_can",
-               .driver_data = AT91_DEVTYPE_SAM9263,
+               .driver_data = (kernel_ulong_t)&at91_at91sam9x5_data,
        }, {
                .name = "at91sam9x5_can",
-               .driver_data = AT91_DEVTYPE_SAM9X5,
+               .driver_data = (kernel_ulong_t)&at91_at91sam9263_data,
        }, {
                /* sentinel */
        }
@@ -1379,6 +1426,7 @@ static struct platform_driver at91_can_driver = {
        .driver = {
                .name = KBUILD_MODNAME,
                .owner = THIS_MODULE,
+               .of_match_table = at91_can_dt_ids,
        },
        .id_table = at91_can_id_table,
 };