summaryrefslogtreecommitdiff
path: root/components/drivers/phy/phy.c
blob: 8e91aea99fccaf4c65b749c1f467b53befcd9d46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author        Notes
 * 2020-09-27     wangqiang     first version
 */
#include <stddef.h>
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "PHY"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>


static rt_size_t phy_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t count)
{
    struct rt_phy_device *phy = (struct rt_phy_device *)dev->user_data;
    struct rt_phy_msg *msg = (struct rt_phy_msg *)buffer;
    return phy->bus->ops->read(phy->bus, phy->addr, msg->reg, &(msg->value), 4);
}
static rt_size_t phy_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t count)
{
    struct rt_phy_device *phy = (struct rt_phy_device *)dev->user_data;
    struct rt_phy_msg *msg = (struct rt_phy_msg *)buffer;
    return phy->bus->ops->write(phy->bus, phy->addr, msg->reg, &(msg->value), 4);
}



#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops phy_ops =
{
    RT_NULL,
    RT_NULL,
    RT_NULL,
    phy_device_read,
    phy_device_write,
    RT_NULL,
};
#endif

/*
* phy device register
*/
rt_err_t rt_hw_phy_register(struct rt_phy_device *phy, const char *name)
{
    rt_err_t ret;
    struct rt_device *device;

    device = &(phy->parent);

    device->type = RT_Device_Class_PHY;
    device->rx_indicate = RT_NULL;
    device->tx_complete = RT_NULL;

#ifdef RT_USING_DEVICE_OPS
    device->ops = &phy_ops;
#else
    device->init = NULL;
    device->open = NULL;
    device->close = NULL;
    device->read = phy_device_read;
    device->write = phy_device_write;
    device->control = NULL;
#endif
    device->user_data = phy;

    /* register a character device */
    ret = rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);

    return ret;
}