2 * Copyright (C) 2017 Pengutronix, Juergen Borleis <kernel@pengutronix.de>
4 * Partially based on a patch from
5 * Copyright (c) 2014 Stefan Roese <sr@denx.de>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/mdio.h>
20 #include <linux/phy.h>
25 /* Generate phy-addr and -reg from the input address */
26 #define PHY_ADDR(x) ((((x) >> 6) + 0x10) & 0x1f)
27 #define PHY_REG(x) (((x) >> 1) & 0x1f)
30 struct mdio_device
*device
;
34 static void lan9303_mdio_real_write(struct mdio_device
*mdio
, int reg
, u16 val
)
36 mdio
->bus
->write(mdio
->bus
, PHY_ADDR(reg
), PHY_REG(reg
), val
);
39 static int lan9303_mdio_write(void *ctx
, uint32_t reg
, uint32_t val
)
41 struct lan9303_mdio
*sw_dev
= (struct lan9303_mdio
*)ctx
;
43 reg
<<= 2; /* reg num to offset */
44 mutex_lock(&sw_dev
->device
->bus
->mdio_lock
);
45 lan9303_mdio_real_write(sw_dev
->device
, reg
, val
& 0xffff);
46 lan9303_mdio_real_write(sw_dev
->device
, reg
+ 2, (val
>> 16) & 0xffff);
47 mutex_unlock(&sw_dev
->device
->bus
->mdio_lock
);
52 static u16
lan9303_mdio_real_read(struct mdio_device
*mdio
, int reg
)
54 return mdio
->bus
->read(mdio
->bus
, PHY_ADDR(reg
), PHY_REG(reg
));
57 static int lan9303_mdio_read(void *ctx
, uint32_t reg
, uint32_t *val
)
59 struct lan9303_mdio
*sw_dev
= (struct lan9303_mdio
*)ctx
;
61 reg
<<= 2; /* reg num to offset */
62 mutex_lock(&sw_dev
->device
->bus
->mdio_lock
);
63 *val
= lan9303_mdio_real_read(sw_dev
->device
, reg
);
64 *val
|= (lan9303_mdio_real_read(sw_dev
->device
, reg
+ 2) << 16);
65 mutex_unlock(&sw_dev
->device
->bus
->mdio_lock
);
70 static int lan9303_mdio_phy_write(struct lan9303
*chip
, int phy
, int reg
,
73 struct lan9303_mdio
*sw_dev
= dev_get_drvdata(chip
->dev
);
75 return mdiobus_write_nested(sw_dev
->device
->bus
, phy
, reg
, val
);
78 static int lan9303_mdio_phy_read(struct lan9303
*chip
, int phy
, int reg
)
80 struct lan9303_mdio
*sw_dev
= dev_get_drvdata(chip
->dev
);
82 return mdiobus_read_nested(sw_dev
->device
->bus
, phy
, reg
);
85 static const struct lan9303_phy_ops lan9303_mdio_phy_ops
= {
86 .phy_read
= lan9303_mdio_phy_read
,
87 .phy_write
= lan9303_mdio_phy_write
,
90 static const struct regmap_config lan9303_mdio_regmap_config
= {
94 .can_multi_write
= true,
95 .max_register
= 0x0ff, /* address bits 0..1 are not used */
96 .reg_format_endian
= REGMAP_ENDIAN_LITTLE
,
98 .volatile_table
= &lan9303_register_set
,
99 .wr_table
= &lan9303_register_set
,
100 .rd_table
= &lan9303_register_set
,
102 .reg_read
= lan9303_mdio_read
,
103 .reg_write
= lan9303_mdio_write
,
105 .cache_type
= REGCACHE_NONE
,
108 static int lan9303_mdio_probe(struct mdio_device
*mdiodev
)
110 struct lan9303_mdio
*sw_dev
;
113 sw_dev
= devm_kzalloc(&mdiodev
->dev
, sizeof(struct lan9303_mdio
),
118 sw_dev
->chip
.regmap
= devm_regmap_init(&mdiodev
->dev
, NULL
, sw_dev
,
119 &lan9303_mdio_regmap_config
);
120 if (IS_ERR(sw_dev
->chip
.regmap
)) {
121 ret
= PTR_ERR(sw_dev
->chip
.regmap
);
122 dev_err(&mdiodev
->dev
, "regmap init failed: %d\n", ret
);
126 /* link forward and backward */
127 sw_dev
->device
= mdiodev
;
128 dev_set_drvdata(&mdiodev
->dev
, sw_dev
);
129 sw_dev
->chip
.dev
= &mdiodev
->dev
;
131 sw_dev
->chip
.ops
= &lan9303_mdio_phy_ops
;
133 ret
= lan9303_probe(&sw_dev
->chip
, mdiodev
->dev
.of_node
);
137 dev_info(&mdiodev
->dev
, "LAN9303 MDIO driver loaded successfully\n");
142 static void lan9303_mdio_remove(struct mdio_device
*mdiodev
)
144 struct lan9303_mdio
*sw_dev
= dev_get_drvdata(&mdiodev
->dev
);
149 lan9303_remove(&sw_dev
->chip
);
152 /*-------------------------------------------------------------------------*/
154 static const struct of_device_id lan9303_mdio_of_match
[] = {
155 { .compatible
= "smsc,lan9303-mdio" },
158 MODULE_DEVICE_TABLE(of
, lan9303_mdio_of_match
);
160 static struct mdio_driver lan9303_mdio_driver
= {
162 .name
= "LAN9303_MDIO",
163 .of_match_table
= of_match_ptr(lan9303_mdio_of_match
),
165 .probe
= lan9303_mdio_probe
,
166 .remove
= lan9303_mdio_remove
,
168 mdio_module_driver(lan9303_mdio_driver
);
170 MODULE_AUTHOR("Stefan Roese <sr@denx.de>, Juergen Borleis <kernel@pengutronix.de>");
171 MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in MDIO managed mode");
172 MODULE_LICENSE("GPL v2");