blob: 033df435f76ce5648f8013370bdadeb8125af9a1 [file] [log] [blame]
Andrew Lunna2443fd2019-01-21 19:05:50 +01001// SPDX-License-Identifier: GPL-2.0+
Florian Fainelli648ea012017-02-04 13:02:44 -08002/*
3 * mdio-boardinfo - Collect pre-declarations for MDIO devices
Florian Fainelli648ea012017-02-04 13:02:44 -08004 */
5
6#include <linux/kernel.h>
7#include <linux/slab.h>
8#include <linux/export.h>
9#include <linux/mutex.h>
10#include <linux/list.h>
11
12#include "mdio-boardinfo.h"
13
14static LIST_HEAD(mdio_board_list);
15static DEFINE_MUTEX(mdio_board_lock);
16
17/**
18 * mdiobus_setup_mdiodev_from_board_info - create and setup MDIO devices
19 * from pre-collected board specific MDIO information
Andrew Lunn19c5a5f2020-07-07 03:49:34 +020020 * @bus: Bus the board_info belongs to
21 * @cb: Callback to create device on bus
Florian Fainelli648ea012017-02-04 13:02:44 -080022 * Context: can sleep
23 */
Florian Fainellid0281a52017-03-28 12:57:09 -070024void mdiobus_setup_mdiodev_from_board_info(struct mii_bus *bus,
25 int (*cb)
26 (struct mii_bus *bus,
27 struct mdio_board_info *bi))
Florian Fainelli648ea012017-02-04 13:02:44 -080028{
29 struct mdio_board_entry *be;
Andrew Lunn0263ea52018-04-19 02:00:47 +020030 struct mdio_board_entry *tmp;
Florian Fainelli648ea012017-02-04 13:02:44 -080031 struct mdio_board_info *bi;
32 int ret;
33
34 mutex_lock(&mdio_board_lock);
Andrew Lunn0263ea52018-04-19 02:00:47 +020035 list_for_each_entry_safe(be, tmp, &mdio_board_list, list) {
Florian Fainelli648ea012017-02-04 13:02:44 -080036 bi = &be->board_info;
37
38 if (strcmp(bus->id, bi->bus_id))
39 continue;
40
Andrew Lunn0263ea52018-04-19 02:00:47 +020041 mutex_unlock(&mdio_board_lock);
Florian Fainellid0281a52017-03-28 12:57:09 -070042 ret = cb(bus, bi);
Andrew Lunn0263ea52018-04-19 02:00:47 +020043 mutex_lock(&mdio_board_lock);
Florian Fainellid0281a52017-03-28 12:57:09 -070044 if (ret)
Florian Fainelli648ea012017-02-04 13:02:44 -080045 continue;
46
Florian Fainelli648ea012017-02-04 13:02:44 -080047 }
48 mutex_unlock(&mdio_board_lock);
49}
Florian Fainellid0281a52017-03-28 12:57:09 -070050EXPORT_SYMBOL(mdiobus_setup_mdiodev_from_board_info);
Florian Fainelli648ea012017-02-04 13:02:44 -080051
52/**
53 * mdio_register_board_info - register MDIO devices for a given board
54 * @info: array of devices descriptors
55 * @n: number of descriptors provided
56 * Context: can sleep
57 *
58 * The board info passed can be marked with __initdata but be pointers
59 * such as platform_data etc. are copied as-is
60 */
61int mdiobus_register_board_info(const struct mdio_board_info *info,
62 unsigned int n)
63{
64 struct mdio_board_entry *be;
65 unsigned int i;
66
67 be = kcalloc(n, sizeof(*be), GFP_KERNEL);
68 if (!be)
69 return -ENOMEM;
70
71 for (i = 0; i < n; i++, be++, info++) {
72 memcpy(&be->board_info, info, sizeof(*info));
73 mutex_lock(&mdio_board_lock);
74 list_add_tail(&be->list, &mdio_board_list);
75 mutex_unlock(&mdio_board_lock);
76 }
77
78 return 0;
79}
Florian Fainelli90eff902017-03-23 10:01:19 -070080EXPORT_SYMBOL(mdiobus_register_board_info);