Commit ac33ffd3 authored by Marc Kleine-Budde's avatar Marc Kleine-Budde

can: m_can: let m_can_class_allocate_dev() allocate driver specific private data

This patch enhances m_can_class_allocate_dev() to allocate driver specific
private data. The driver's private data struct must contain struct
m_can_classdev as its first member followed by the remaining private data.

Link: https://lore.kernel.org/r/20201212175518.139651-7-mkl@pengutronix.deReviewed-by: default avatarSean Nyekjaer <sean@geanix.com>
Reviewed-by: default avatarDan Murphy <dmurphy@ti.com>
Signed-off-by: default avatarMarc Kleine-Budde <mkl@pengutronix.de>
parent b8d62555
...@@ -1759,7 +1759,8 @@ int m_can_class_get_clocks(struct m_can_classdev *cdev) ...@@ -1759,7 +1759,8 @@ int m_can_class_get_clocks(struct m_can_classdev *cdev)
} }
EXPORT_SYMBOL_GPL(m_can_class_get_clocks); EXPORT_SYMBOL_GPL(m_can_class_get_clocks);
struct m_can_classdev *m_can_class_allocate_dev(struct device *dev) struct m_can_classdev *m_can_class_allocate_dev(struct device *dev,
int sizeof_priv)
{ {
struct m_can_classdev *class_dev = NULL; struct m_can_classdev *class_dev = NULL;
u32 mram_config_vals[MRAM_CFG_LEN]; u32 mram_config_vals[MRAM_CFG_LEN];
...@@ -1782,7 +1783,7 @@ struct m_can_classdev *m_can_class_allocate_dev(struct device *dev) ...@@ -1782,7 +1783,7 @@ struct m_can_classdev *m_can_class_allocate_dev(struct device *dev)
tx_fifo_size = mram_config_vals[7]; tx_fifo_size = mram_config_vals[7];
/* allocate the m_can device */ /* allocate the m_can device */
net_dev = alloc_candev(sizeof(*class_dev), tx_fifo_size); net_dev = alloc_candev(sizeof_priv, tx_fifo_size);
if (!net_dev) { if (!net_dev) {
dev_err(dev, "Failed to allocate CAN device"); dev_err(dev, "Failed to allocate CAN device");
goto out; goto out;
......
...@@ -86,8 +86,6 @@ struct m_can_classdev { ...@@ -86,8 +86,6 @@ struct m_can_classdev {
struct m_can_ops *ops; struct m_can_ops *ops;
void *device_data;
int version; int version;
u32 irqstatus; u32 irqstatus;
...@@ -97,7 +95,7 @@ struct m_can_classdev { ...@@ -97,7 +95,7 @@ struct m_can_classdev {
struct mram_cfg mcfg[MRAM_CFG_NUM]; struct mram_cfg mcfg[MRAM_CFG_NUM];
}; };
struct m_can_classdev *m_can_class_allocate_dev(struct device *dev); struct m_can_classdev *m_can_class_allocate_dev(struct device *dev, int sizeof_priv);
void m_can_class_free_dev(struct net_device *net); void m_can_class_free_dev(struct net_device *net);
int m_can_class_register(struct m_can_classdev *cdev); int m_can_class_register(struct m_can_classdev *cdev);
void m_can_class_unregister(struct m_can_classdev *cdev); void m_can_class_unregister(struct m_can_classdev *cdev);
......
...@@ -22,26 +22,33 @@ ...@@ -22,26 +22,33 @@
#define CTL_CSR_INT_CTL_OFFSET 0x508 #define CTL_CSR_INT_CTL_OFFSET 0x508
struct m_can_pci_priv { struct m_can_pci_priv {
struct m_can_classdev cdev;
void __iomem *base; void __iomem *base;
}; };
static inline struct m_can_pci_priv *cdev_to_priv(struct m_can_classdev *cdev)
{
return container_of(cdev, struct m_can_pci_priv, cdev);
}
static u32 iomap_read_reg(struct m_can_classdev *cdev, int reg) static u32 iomap_read_reg(struct m_can_classdev *cdev, int reg)
{ {
struct m_can_pci_priv *priv = cdev->device_data; struct m_can_pci_priv *priv = cdev_to_priv(cdev);
return readl(priv->base + reg); return readl(priv->base + reg);
} }
static u32 iomap_read_fifo(struct m_can_classdev *cdev, int offset) static u32 iomap_read_fifo(struct m_can_classdev *cdev, int offset)
{ {
struct m_can_pci_priv *priv = cdev->device_data; struct m_can_pci_priv *priv = cdev_to_priv(cdev);
return readl(priv->base + offset); return readl(priv->base + offset);
} }
static int iomap_write_reg(struct m_can_classdev *cdev, int reg, int val) static int iomap_write_reg(struct m_can_classdev *cdev, int reg, int val)
{ {
struct m_can_pci_priv *priv = cdev->device_data; struct m_can_pci_priv *priv = cdev_to_priv(cdev);
writel(val, priv->base + reg); writel(val, priv->base + reg);
...@@ -50,7 +57,7 @@ static int iomap_write_reg(struct m_can_classdev *cdev, int reg, int val) ...@@ -50,7 +57,7 @@ static int iomap_write_reg(struct m_can_classdev *cdev, int reg, int val)
static int iomap_write_fifo(struct m_can_classdev *cdev, int offset, int val) static int iomap_write_fifo(struct m_can_classdev *cdev, int offset, int val)
{ {
struct m_can_pci_priv *priv = cdev->device_data; struct m_can_pci_priv *priv = cdev_to_priv(cdev);
writel(val, priv->base + offset); writel(val, priv->base + offset);
...@@ -89,21 +96,19 @@ static int m_can_pci_probe(struct pci_dev *pci, const struct pci_device_id *id) ...@@ -89,21 +96,19 @@ static int m_can_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
return -ENOMEM; return -ENOMEM;
} }
priv = devm_kzalloc(&pci->dev, sizeof(*priv), GFP_KERNEL); mcan_class = m_can_class_allocate_dev(&pci->dev,
if (!priv) sizeof(struct m_can_pci_priv));
return -ENOMEM;
mcan_class = m_can_class_allocate_dev(&pci->dev);
if (!mcan_class) if (!mcan_class)
return -ENOMEM; return -ENOMEM;
priv = cdev_to_priv(mcan_class);
priv->base = base; priv->base = base;
ret = pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_ALL_TYPES); ret = pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_ALL_TYPES);
if (ret < 0) if (ret < 0)
return ret; return ret;
mcan_class->device_data = priv;
mcan_class->dev = &pci->dev; mcan_class->dev = &pci->dev;
mcan_class->net->irq = pci_irq_vector(pci, 0); mcan_class->net->irq = pci_irq_vector(pci, 0);
mcan_class->pm_clock_support = 1; mcan_class->pm_clock_support = 1;
...@@ -135,7 +140,7 @@ static void m_can_pci_remove(struct pci_dev *pci) ...@@ -135,7 +140,7 @@ static void m_can_pci_remove(struct pci_dev *pci)
{ {
struct net_device *dev = pci_get_drvdata(pci); struct net_device *dev = pci_get_drvdata(pci);
struct m_can_classdev *mcan_class = netdev_priv(dev); struct m_can_classdev *mcan_class = netdev_priv(dev);
struct m_can_pci_priv *priv = mcan_class->device_data; struct m_can_pci_priv *priv = cdev_to_priv(mcan_class);
pm_runtime_forbid(&pci->dev); pm_runtime_forbid(&pci->dev);
pm_runtime_get_noresume(&pci->dev); pm_runtime_get_noresume(&pci->dev);
......
...@@ -10,27 +10,34 @@ ...@@ -10,27 +10,34 @@
#include "m_can.h" #include "m_can.h"
struct m_can_plat_priv { struct m_can_plat_priv {
struct m_can_classdev cdev;
void __iomem *base; void __iomem *base;
void __iomem *mram_base; void __iomem *mram_base;
}; };
static inline struct m_can_plat_priv *cdev_to_priv(struct m_can_classdev *cdev)
{
return container_of(cdev, struct m_can_plat_priv, cdev);
}
static u32 iomap_read_reg(struct m_can_classdev *cdev, int reg) static u32 iomap_read_reg(struct m_can_classdev *cdev, int reg)
{ {
struct m_can_plat_priv *priv = cdev->device_data; struct m_can_plat_priv *priv = cdev_to_priv(cdev);
return readl(priv->base + reg); return readl(priv->base + reg);
} }
static u32 iomap_read_fifo(struct m_can_classdev *cdev, int offset) static u32 iomap_read_fifo(struct m_can_classdev *cdev, int offset)
{ {
struct m_can_plat_priv *priv = cdev->device_data; struct m_can_plat_priv *priv = cdev_to_priv(cdev);
return readl(priv->mram_base + offset); return readl(priv->mram_base + offset);
} }
static int iomap_write_reg(struct m_can_classdev *cdev, int reg, int val) static int iomap_write_reg(struct m_can_classdev *cdev, int reg, int val)
{ {
struct m_can_plat_priv *priv = cdev->device_data; struct m_can_plat_priv *priv = cdev_to_priv(cdev);
writel(val, priv->base + reg); writel(val, priv->base + reg);
...@@ -39,7 +46,7 @@ static int iomap_write_reg(struct m_can_classdev *cdev, int reg, int val) ...@@ -39,7 +46,7 @@ static int iomap_write_reg(struct m_can_classdev *cdev, int reg, int val)
static int iomap_write_fifo(struct m_can_classdev *cdev, int offset, int val) static int iomap_write_fifo(struct m_can_classdev *cdev, int offset, int val)
{ {
struct m_can_plat_priv *priv = cdev->device_data; struct m_can_plat_priv *priv = cdev_to_priv(cdev);
writel(val, priv->mram_base + offset); writel(val, priv->mram_base + offset);
...@@ -62,17 +69,12 @@ static int m_can_plat_probe(struct platform_device *pdev) ...@@ -62,17 +69,12 @@ static int m_can_plat_probe(struct platform_device *pdev)
void __iomem *mram_addr; void __iomem *mram_addr;
int irq, ret = 0; int irq, ret = 0;
mcan_class = m_can_class_allocate_dev(&pdev->dev); mcan_class = m_can_class_allocate_dev(&pdev->dev,
sizeof(struct m_can_plat_priv));
if (!mcan_class) if (!mcan_class)
return -ENOMEM; return -ENOMEM;
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); priv = cdev_to_priv(mcan_class);
if (!priv) {
ret = -ENOMEM;
goto probe_fail;
}
mcan_class->device_data = priv;
ret = m_can_class_get_clocks(mcan_class); ret = m_can_class_get_clocks(mcan_class);
if (ret) if (ret)
......
...@@ -114,17 +114,23 @@ ...@@ -114,17 +114,23 @@
#define TCAN4X5X_WD_6_S_TIMER (BIT(28) | BIT(29)) #define TCAN4X5X_WD_6_S_TIMER (BIT(28) | BIT(29))
struct tcan4x5x_priv { struct tcan4x5x_priv {
struct m_can_classdev cdev;
struct regmap *regmap; struct regmap *regmap;
struct spi_device *spi; struct spi_device *spi;
struct m_can_classdev *mcan_dev;
struct gpio_desc *reset_gpio; struct gpio_desc *reset_gpio;
struct gpio_desc *device_wake_gpio; struct gpio_desc *device_wake_gpio;
struct gpio_desc *device_state_gpio; struct gpio_desc *device_state_gpio;
struct regulator *power; struct regulator *power;
}; };
static inline struct tcan4x5x_priv *cdev_to_priv(struct m_can_classdev *cdev)
{
return container_of(cdev, struct tcan4x5x_priv, cdev);
}
static struct can_bittiming_const tcan4x5x_bittiming_const = { static struct can_bittiming_const tcan4x5x_bittiming_const = {
.name = DEVICE_NAME, .name = DEVICE_NAME,
.tseg1_min = 2, .tseg1_min = 2,
...@@ -253,7 +259,7 @@ static struct regmap_bus tcan4x5x_bus = { ...@@ -253,7 +259,7 @@ static struct regmap_bus tcan4x5x_bus = {
static u32 tcan4x5x_read_reg(struct m_can_classdev *cdev, int reg) static u32 tcan4x5x_read_reg(struct m_can_classdev *cdev, int reg)
{ {
struct tcan4x5x_priv *priv = cdev->device_data; struct tcan4x5x_priv *priv = cdev_to_priv(cdev);
u32 val; u32 val;
regmap_read(priv->regmap, TCAN4X5X_MCAN_OFFSET + reg, &val); regmap_read(priv->regmap, TCAN4X5X_MCAN_OFFSET + reg, &val);
...@@ -263,7 +269,7 @@ static u32 tcan4x5x_read_reg(struct m_can_classdev *cdev, int reg) ...@@ -263,7 +269,7 @@ static u32 tcan4x5x_read_reg(struct m_can_classdev *cdev, int reg)
static u32 tcan4x5x_read_fifo(struct m_can_classdev *cdev, int addr_offset) static u32 tcan4x5x_read_fifo(struct m_can_classdev *cdev, int addr_offset)
{ {
struct tcan4x5x_priv *priv = cdev->device_data; struct tcan4x5x_priv *priv = cdev_to_priv(cdev);
u32 val; u32 val;
regmap_read(priv->regmap, TCAN4X5X_MRAM_START + addr_offset, &val); regmap_read(priv->regmap, TCAN4X5X_MRAM_START + addr_offset, &val);
...@@ -273,7 +279,7 @@ static u32 tcan4x5x_read_fifo(struct m_can_classdev *cdev, int addr_offset) ...@@ -273,7 +279,7 @@ static u32 tcan4x5x_read_fifo(struct m_can_classdev *cdev, int addr_offset)
static int tcan4x5x_write_reg(struct m_can_classdev *cdev, int reg, int val) static int tcan4x5x_write_reg(struct m_can_classdev *cdev, int reg, int val)
{ {
struct tcan4x5x_priv *priv = cdev->device_data; struct tcan4x5x_priv *priv = cdev_to_priv(cdev);
return regmap_write(priv->regmap, TCAN4X5X_MCAN_OFFSET + reg, val); return regmap_write(priv->regmap, TCAN4X5X_MCAN_OFFSET + reg, val);
} }
...@@ -281,7 +287,7 @@ static int tcan4x5x_write_reg(struct m_can_classdev *cdev, int reg, int val) ...@@ -281,7 +287,7 @@ static int tcan4x5x_write_reg(struct m_can_classdev *cdev, int reg, int val)
static int tcan4x5x_write_fifo(struct m_can_classdev *cdev, static int tcan4x5x_write_fifo(struct m_can_classdev *cdev,
int addr_offset, int val) int addr_offset, int val)
{ {
struct tcan4x5x_priv *priv = cdev->device_data; struct tcan4x5x_priv *priv = cdev_to_priv(cdev);
return regmap_write(priv->regmap, TCAN4X5X_MRAM_START + addr_offset, val); return regmap_write(priv->regmap, TCAN4X5X_MRAM_START + addr_offset, val);
} }
...@@ -300,7 +306,7 @@ static int tcan4x5x_power_enable(struct regulator *reg, int enable) ...@@ -300,7 +306,7 @@ static int tcan4x5x_power_enable(struct regulator *reg, int enable)
static int tcan4x5x_write_tcan_reg(struct m_can_classdev *cdev, static int tcan4x5x_write_tcan_reg(struct m_can_classdev *cdev,
int reg, int val) int reg, int val)
{ {
struct tcan4x5x_priv *priv = cdev->device_data; struct tcan4x5x_priv *priv = cdev_to_priv(cdev);
return regmap_write(priv->regmap, reg, val); return regmap_write(priv->regmap, reg, val);
} }
...@@ -330,7 +336,7 @@ static int tcan4x5x_clear_interrupts(struct m_can_classdev *cdev) ...@@ -330,7 +336,7 @@ static int tcan4x5x_clear_interrupts(struct m_can_classdev *cdev)
static int tcan4x5x_init(struct m_can_classdev *cdev) static int tcan4x5x_init(struct m_can_classdev *cdev)
{ {
struct tcan4x5x_priv *tcan4x5x = cdev->device_data; struct tcan4x5x_priv *tcan4x5x = cdev_to_priv(cdev);
int ret; int ret;
tcan4x5x_check_wake(tcan4x5x); tcan4x5x_check_wake(tcan4x5x);
...@@ -357,7 +363,7 @@ static int tcan4x5x_init(struct m_can_classdev *cdev) ...@@ -357,7 +363,7 @@ static int tcan4x5x_init(struct m_can_classdev *cdev)
static int tcan4x5x_disable_wake(struct m_can_classdev *cdev) static int tcan4x5x_disable_wake(struct m_can_classdev *cdev)
{ {
struct tcan4x5x_priv *tcan4x5x = cdev->device_data; struct tcan4x5x_priv *tcan4x5x = cdev_to_priv(cdev);
return regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG, return regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG,
TCAN4X5X_DISABLE_WAKE_MSK, 0x00); TCAN4X5X_DISABLE_WAKE_MSK, 0x00);
...@@ -365,7 +371,7 @@ static int tcan4x5x_disable_wake(struct m_can_classdev *cdev) ...@@ -365,7 +371,7 @@ static int tcan4x5x_disable_wake(struct m_can_classdev *cdev)
static int tcan4x5x_disable_state(struct m_can_classdev *cdev) static int tcan4x5x_disable_state(struct m_can_classdev *cdev)
{ {
struct tcan4x5x_priv *tcan4x5x = cdev->device_data; struct tcan4x5x_priv *tcan4x5x = cdev_to_priv(cdev);
return regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG, return regmap_update_bits(tcan4x5x->regmap, TCAN4X5X_CONFIG,
TCAN4X5X_DISABLE_INH_MSK, 0x01); TCAN4X5X_DISABLE_INH_MSK, 0x01);
...@@ -373,7 +379,7 @@ static int tcan4x5x_disable_state(struct m_can_classdev *cdev) ...@@ -373,7 +379,7 @@ static int tcan4x5x_disable_state(struct m_can_classdev *cdev)
static int tcan4x5x_get_gpios(struct m_can_classdev *cdev) static int tcan4x5x_get_gpios(struct m_can_classdev *cdev)
{ {
struct tcan4x5x_priv *tcan4x5x = cdev->device_data; struct tcan4x5x_priv *tcan4x5x = cdev_to_priv(cdev);
int ret; int ret;
tcan4x5x->device_wake_gpio = devm_gpiod_get(cdev->dev, "device-wake", tcan4x5x->device_wake_gpio = devm_gpiod_get(cdev->dev, "device-wake",
...@@ -427,15 +433,12 @@ static int tcan4x5x_can_probe(struct spi_device *spi) ...@@ -427,15 +433,12 @@ static int tcan4x5x_can_probe(struct spi_device *spi)
struct m_can_classdev *mcan_class; struct m_can_classdev *mcan_class;
int freq, ret; int freq, ret;
mcan_class = m_can_class_allocate_dev(&spi->dev); mcan_class = m_can_class_allocate_dev(&spi->dev,
sizeof(struct tcan4x5x_priv));
if (!mcan_class) if (!mcan_class)
return -ENOMEM; return -ENOMEM;
priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL); priv = cdev_to_priv(mcan_class);
if (!priv) {
ret = -ENOMEM;
goto out_m_can_class_free_dev;
}
priv->power = devm_regulator_get_optional(&spi->dev, "vsup"); priv->power = devm_regulator_get_optional(&spi->dev, "vsup");
if (PTR_ERR(priv->power) == -EPROBE_DEFER) { if (PTR_ERR(priv->power) == -EPROBE_DEFER) {
...@@ -445,8 +448,6 @@ static int tcan4x5x_can_probe(struct spi_device *spi) ...@@ -445,8 +448,6 @@ static int tcan4x5x_can_probe(struct spi_device *spi)
priv->power = NULL; priv->power = NULL;
} }
mcan_class->device_data = priv;
m_can_class_get_clocks(mcan_class); m_can_class_get_clocks(mcan_class);
if (IS_ERR(mcan_class->cclk)) { if (IS_ERR(mcan_class->cclk)) {
dev_err(&spi->dev, "no CAN clock source defined\n"); dev_err(&spi->dev, "no CAN clock source defined\n");
...@@ -462,7 +463,6 @@ static int tcan4x5x_can_probe(struct spi_device *spi) ...@@ -462,7 +463,6 @@ static int tcan4x5x_can_probe(struct spi_device *spi)
} }
priv->spi = spi; priv->spi = spi;
priv->mcan_dev = mcan_class;
mcan_class->pm_clock_support = 0; mcan_class->pm_clock_support = 0;
mcan_class->can.clock.freq = freq; mcan_class->can.clock.freq = freq;
...@@ -518,11 +518,11 @@ static int tcan4x5x_can_remove(struct spi_device *spi) ...@@ -518,11 +518,11 @@ static int tcan4x5x_can_remove(struct spi_device *spi)
{ {
struct tcan4x5x_priv *priv = spi_get_drvdata(spi); struct tcan4x5x_priv *priv = spi_get_drvdata(spi);
m_can_class_unregister(priv->mcan_dev); m_can_class_unregister(&priv->cdev);
tcan4x5x_power_enable(priv->power, 0); tcan4x5x_power_enable(priv->power, 0);
m_can_class_free_dev(priv->mcan_dev->net); m_can_class_free_dev(priv->cdev.net);
return 0; return 0;
} }
......
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