Skip to content

Commit

Permalink
net: phy: spi_ks8995: introduce spi_device_id table
Browse files Browse the repository at this point in the history
Refactor to use spi_device_id table to facilitate easy
extendability.

Signed-off-by: Helmut Buchsbaum <helmut.buchsbaum@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Helmut Buchsbaum authored and David S. Miller committed Feb 11, 2016
1 parent 7463053 commit aa54c8d
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions drivers/net/phy/spi_ks8995.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@

#define KS8995_RESET_DELAY 10 /* usec */

enum ks8995_chip_variant {
ks8995,
ksz8864,
max_variant
};

struct ks8995_chip_params {
char *name;
int regs_size;
};

static const struct ks8995_chip_params ks8995_chip[] = {
[ks8995] = {
.name = "KS8995MA",
.regs_size = KS8995_REGS_SIZE,
},
[ksz8864] = {
.name = "KSZ8864RMN",
.regs_size = KSZ8864_REGS_SIZE,
},
};

struct ks8995_pdata {
/* not yet implemented */
};
Expand All @@ -98,8 +120,16 @@ struct ks8995_switch {
struct mutex lock;
struct ks8995_pdata *pdata;
struct bin_attribute regs_attr;
const struct ks8995_chip_params *chip;
};

static const struct spi_device_id ks8995_id[] = {
{"ks8995", ks8995},
{"ksz8864", ksz8864},
{ }
};
MODULE_DEVICE_TABLE(spi, ks8995_id);

static inline u8 get_chip_id(u8 val)
{
return (val >> ID1_CHIPID_S) & ID1_CHIPID_M;
Expand Down Expand Up @@ -244,24 +274,31 @@ static const struct bin_attribute ks8995_registers_attr = {
};

/* ------------------------------------------------------------------------ */

static int ks8995_probe(struct spi_device *spi)
{
struct ks8995_switch *ks;
struct ks8995_pdata *pdata;
u8 ids[2];
int err;
int variant = spi_get_device_id(spi)->driver_data;

/* Chip description */
pdata = spi->dev.platform_data;

if (variant >= max_variant) {
dev_err(&spi->dev, "bad chip variant %d\n", variant);
return -ENODEV;
}

ks = devm_kzalloc(&spi->dev, sizeof(*ks), GFP_KERNEL);
if (!ks)
return -ENOMEM;

mutex_init(&ks->lock);
ks->pdata = pdata;
ks->spi = spi_dev_get(spi);
ks->chip = &ks8995_chip[variant];

spi_set_drvdata(spi, ks);

spi->mode = SPI_MODE_0;
Expand All @@ -287,6 +324,7 @@ static int ks8995_probe(struct spi_device *spi)
return -ENODEV;
}

ks->regs_attr.size = ks->chip->regs_size;
memcpy(&ks->regs_attr, &ks8995_registers_attr, sizeof(ks->regs_attr));
if (get_chip_id(ids[1]) != CHIPID_M) {
u8 val;
Expand All @@ -303,7 +341,6 @@ static int ks8995_probe(struct spi_device *spi)
dev_err(&spi->dev, "unknown chip:%02x,0\n", ids[1]);
return err;
}
ks->regs_attr.size = KSZ8864_REGS_SIZE;
}

err = ks8995_reset(ks);
Expand Down Expand Up @@ -346,6 +383,7 @@ static struct spi_driver ks8995_driver = {
},
.probe = ks8995_probe,
.remove = ks8995_remove,
.id_table = ks8995_id,
};

module_spi_driver(ks8995_driver);
Expand Down

0 comments on commit aa54c8d

Please sign in to comment.