summaryrefslogtreecommitdiff
path: root/lwis_phy.c
blob: 45cb0196b55a7dc0502f815b5383226ac112127b (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Google LWIS PHY Interface
 *
 * Copyright (c) 2018 Google, LLC
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#define pr_fmt(fmt) KBUILD_MODNAME "-phy: " fmt

#include "lwis_phy.h"

#include <linux/kernel.h>
#include <linux/slab.h>

struct lwis_phy_list *lwis_phy_list_alloc(int count)
{
	struct lwis_phy_list *list;

	/* No need to allocate if count is invalid */
	if (count <= 0) {
		return ERR_PTR(-EINVAL);
	}

	list = kmalloc(sizeof(struct lwis_phy_list), GFP_KERNEL);
	if (!list) {
		return ERR_PTR(-ENOMEM);
	}

	list->phy = kzalloc(count * sizeof(struct phy), GFP_KERNEL);
	if (!list->phy) {
		kfree(list);
		return ERR_PTR(-ENOMEM);
	}

	list->count = count;

	return list;
}

void lwis_phy_list_free(struct lwis_phy_list *list)
{
	if (!list) {
		return;
	}

	if (list->phy) {
		kfree(list->phy);
	}

	kfree(list);
}

int lwis_phy_get(struct lwis_phy_list *list, char *name, struct device *dev)
{
	struct phy *phy;
	int i;
	int index = -1;

	if (!list || !dev) {
		return -EINVAL;
	}

	/* Look for empty slot and duplicate entries */
	for (i = 0; i < list->count; ++i) {
		if (list->phy[i].phy == NULL) {
			index = i;
		} else if (!strcmp(list->phy[i].name, name)) {
			pr_info("PHY %s already allocated\n", name);
			return i;
		}
	}

	/* No empty slots */
	if (index < 0) {
		pr_err("No empty slots in the lwis_phy struct\n");
		return -ENOMEM;
	}

	/* Make sure PHY exists */
	phy = devm_phy_get(dev, name);
	if (IS_ERR_OR_NULL(phy)) {
		pr_err("PHY %s not found\n", name);
		return PTR_ERR(phy);
	}

	list->phy[index].phy = phy;
	list->phy[index].name = name;

	return index;
}

int lwis_phy_put_by_idx(struct lwis_phy_list *list, int index, struct device *dev)
{
	if (!list || index < 0 || index >= list->count) {
		return -EINVAL;
	}

	if (IS_ERR_OR_NULL(list->phy[index].phy)) {
		return -EINVAL;
	}

	devm_phy_put(dev, list->phy[index].phy);
	memset(list->phy + index, 0, sizeof(struct lwis_phy));

	return 0;
}

int lwis_phy_put_by_name(struct lwis_phy_list *list, char *name, struct device *dev)
{
	int i;

	if (!dev || !list) {
		return -EINVAL;
	}

	/* Find entry by name */
	for (i = 0; i < list->count; ++i) {
		if (!strcmp(list->phy[i].name, name)) {
			if (IS_ERR_OR_NULL(list->phy[i].phy)) {
				return -EINVAL;
			}
			devm_phy_put(dev, list->phy[i].phy);
			memset(list->phy + i, 0, sizeof(struct lwis_phy));
			return 0;
		}
	}

	pr_err("PHY %s not found\n", name);
	return -EINVAL;
}

int lwis_phy_set_power_by_idx(struct lwis_phy_list *list, int index, bool power_on)
{
	if (!list || index < 0 || index >= list->count) {
		return -EINVAL;
	}

	if (power_on) {
		return phy_power_on(list->phy[index].phy);
	}

	return phy_power_off(list->phy[index].phy);
}

int lwis_phy_set_power_by_name(struct lwis_phy_list *list, char *name, bool power_on)
{
	int i;

	if (!list) {
		return -EINVAL;
	}

	/* Find entry by name */
	for (i = 0; i < list->count; ++i) {
		if (!strcmp(list->phy[i].name, name)) {
			return lwis_phy_set_power_by_idx(list, i, power_on);
		}
	}

	/* Entry not found */
	pr_err("PHY %s not found\n", name);
	return -ENOENT;
}

int lwis_phy_set_power_all(struct lwis_phy_list *list, bool power_on)
{
	int i;
	int ret;

	if (!list) {
		return -EINVAL;
	}

	for (i = 0; i < list->count; ++i) {
		ret = lwis_phy_set_power_by_idx(list, i, power_on);
		if (ret) {
			pr_err("Failed to set PHY power\n");
			return ret;
		}
	}

	return 0;
}

void lwis_phy_print(struct lwis_phy_list *list)
{
	int i;

	for (i = 0; i < list->count; ++i) {
		pr_info("%s: PHY: %s\n", __func__, list->phy[i].name);
	}
}