Skip to content

Commit 1871574

Browse files
committed
regulator : add multiple consume test method
1 parent f558606 commit 1871574

File tree

4 files changed

+368
-10
lines changed

4 files changed

+368
-10
lines changed

debug/regulator/README.md

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ ldo4_reg:regulator@7 {
8989

9090
- 加载PMU驱动
9191

92+
```shell
9293
insmod act8846.ko
94+
```
9395

9496
- 不断开关regulator7的电压
9597

@@ -166,22 +168,43 @@ rk818_ldo2_reg: regulator@5 {
166168
#include "rk818.dtsi"
167169
#include "rk818_test.dtsi"
168170
169-
### 测试方法
171+
### 测试方法1(1个consumer)
170172
171173
- 加载PMU驱动
172174
175+
```shell
173176
insmod rk818.ko
177+
```
174178

175179
- 不断开关LDO2的电压
176180

177181
```shell
178-
while true
179-
do
180-
insmod rk818_test.ko
181-
sleep 5
182-
rmmod rk818_test
183-
sleep 5
184-
done
182+
while true
183+
do
184+
insmod consumer1.ko
185+
sleep 5
186+
rmmod consumer1
187+
sleep 5
188+
done
189+
```
190+
191+
- 查看regulator开关状态
192+
193+
```shell
194+
while true
195+
do
196+
cat /sys/class/regulator/regulator.7/state
197+
cat /sys/class/regulator/regulator.7/num_users
198+
sleep 2
199+
done
200+
```
201+
202+
### 测试方法2(2个consumer)
203+
204+
- 加载PMU驱动
205+
206+
```shell
207+
insmod rk818.ko
185208
```
186209

187210
- 查看regulator开关状态
@@ -194,3 +217,17 @@ cat /sys/class/regulator/regulator.7/num_users
194217
sleep 2
195218
done
196219
```
220+
221+
- 分别加载consumer1,2,并查看信息
222+
223+
```shell
224+
insmod consumer1.ko
225+
insmod consumer2.ko
226+
```
227+
228+
- 分别卸载consumer1,2(只有当num_users为0时state才会disabled)
229+
230+
```shell
231+
rmmod consumer1.ko
232+
rmmod consumer2.ko
233+
```

debug/regulator/consumer1.c

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#include <linux/kernel.h>
2+
#include <linux/module.h>
3+
#include <linux/i2c.h>
4+
#include <linux/delay.h>
5+
#include <linux/slab.h>
6+
#include <linux/regulator/consumer.h>
7+
8+
struct consumer2_chip {
9+
struct i2c_client *client;
10+
struct device *dev;
11+
struct regulator *supply;
12+
};
13+
14+
static ssize_t consumer2_debug_show(struct device *dev,
15+
struct device_attribute *attr,
16+
char *buf)
17+
{
18+
printk("%s, %d\n", __FUNCTION__, __LINE__);
19+
return 1;
20+
}
21+
22+
/* FIXME:Don't figure it out how it cann't work */
23+
static ssize_t consumer2_debug_store(struct device *dev,
24+
struct device_attribute *attr, const char *buf, size_t count)
25+
{
26+
struct consumer2_chip *chip;
27+
int ret;
28+
int tmp;
29+
struct regulator *supply;
30+
tmp = simple_strtoul(buf, NULL, 0);
31+
chip = container_of(&dev, struct consumer2_chip, dev);
32+
switch (tmp)
33+
{
34+
case 0:
35+
ret = regulator_disable(chip->supply);
36+
printk("Disable regulator :( ret = %d\n", ret);
37+
msleep(2000);
38+
break;
39+
case 1:
40+
ret = regulator_enable(chip->supply);
41+
printk("Enable regulator :) ret = %d\n", ret);
42+
msleep(2000);
43+
break;
44+
default:
45+
break;
46+
};
47+
return count;
48+
}
49+
50+
static DEVICE_ATTR(consumer2_debug, S_IRUGO | S_IWUSR, consumer2_debug_show,
51+
consumer2_debug_store);
52+
53+
static struct attribute *consumer2_attrs[] = {
54+
&dev_attr_consumer2_debug.attr,
55+
NULL,
56+
};
57+
58+
static const struct attribute_group consumer2_attr_group = {
59+
.attrs = consumer2_attrs,
60+
};
61+
62+
static const struct i2c_device_id consumer2_id_table[] = {
63+
{ "consumer2", 0 },
64+
{},
65+
};
66+
MODULE_DEVICE_TABLE(i2c, consumer2_id_table);
67+
68+
static int consumer2_probe(struct i2c_client *client,
69+
const struct i2c_device_id *id)
70+
{
71+
static struct regulator *supply;
72+
struct consumer2_chip *chip;
73+
int ret;
74+
75+
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
76+
{
77+
printk("i2c_check_functionality error\n");
78+
return -ENODEV;
79+
}
80+
81+
supply = devm_regulator_get(&client->dev, "VCC_TP");
82+
if (IS_ERR(supply)) {
83+
printk("regulator get of vdd_ana failed");
84+
ret = PTR_ERR(supply);
85+
supply = NULL;
86+
return -1;
87+
}
88+
89+
chip = kzalloc(sizeof(struct consumer2_chip), GFP_KERNEL);
90+
if (chip == NULL) {
91+
printk("kzalloc error\n");
92+
return -ENOMEM;
93+
}
94+
95+
chip->supply = supply;
96+
chip->client = client;
97+
chip->dev = &client->dev;
98+
i2c_set_clientdata(client, chip);
99+
100+
ret = sysfs_create_group(&client->dev.kobj, &consumer2_attr_group);
101+
if (ret) {
102+
printk("failed to create sysfs device attributes\n");
103+
return -1;
104+
}
105+
106+
/* Enable the regulator */
107+
ret = regulator_enable(chip->supply);
108+
printk("Enable regulator :) ret = %d\n", ret);
109+
110+
return 0;
111+
}
112+
113+
static int consumer2_remove(struct i2c_client *client)
114+
{
115+
int ret;
116+
struct consumer2_chip *chip = i2c_get_clientdata(client);
117+
118+
/* Disable the regulator */
119+
regulator_disable(chip->supply);
120+
printk("Disable regulator :( ret = %d\n", ret);
121+
sysfs_remove_group(&client->dev.kobj, &consumer2_attr_group);
122+
kfree(chip);
123+
124+
return 0;
125+
}
126+
127+
static const struct of_device_id of_consumer2_match[] = {
128+
{ .compatible = "Consumer1" },
129+
{ /* Sentinel */ }
130+
};
131+
132+
static struct i2c_driver consumer2_driver = {
133+
.driver = {
134+
.name = "consumer2",
135+
.owner = THIS_MODULE,
136+
.of_match_table = of_consumer2_match,
137+
},
138+
.probe = consumer2_probe,
139+
.remove = consumer2_remove,
140+
.id_table = consumer2_id_table,
141+
};
142+
143+
static int consumer2_init(void)
144+
{
145+
int ret;
146+
ret = i2c_add_driver(&consumer2_driver);
147+
return ret;
148+
}
149+
150+
static void consumer2_exit(void)
151+
{
152+
i2c_del_driver(&consumer2_driver);
153+
}
154+
155+
module_init(consumer2_init);
156+
module_exit(consumer2_exit);
157+
MODULE_LICENSE("GPL");

debug/regulator/consumer2.c

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#include <linux/kernel.h>
2+
#include <linux/module.h>
3+
#include <linux/i2c.h>
4+
#include <linux/delay.h>
5+
#include <linux/slab.h>
6+
#include <linux/regulator/consumer.h>
7+
8+
struct consumer1_chip {
9+
struct i2c_client *client;
10+
struct device *dev;
11+
struct regulator *supply;
12+
};
13+
14+
static ssize_t consumer1_debug_show(struct device *dev,
15+
struct device_attribute *attr,
16+
char *buf)
17+
{
18+
printk("%s, %d\n", __FUNCTION__, __LINE__);
19+
return 1;
20+
}
21+
22+
/* FIXME:Don't figure it out how it cann't work */
23+
static ssize_t consumer1_debug_store(struct device *dev,
24+
struct device_attribute *attr, const char *buf, size_t count)
25+
{
26+
struct consumer1_chip *chip;
27+
int ret;
28+
int tmp;
29+
struct regulator *supply;
30+
tmp = simple_strtoul(buf, NULL, 0);
31+
chip = container_of(&dev, struct consumer1_chip, dev);
32+
switch (tmp)
33+
{
34+
case 0:
35+
ret = regulator_disable(chip->supply);
36+
printk("Disable regulator :( ret = %d\n", ret);
37+
msleep(2000);
38+
break;
39+
case 1:
40+
ret = regulator_enable(chip->supply);
41+
printk("Enable regulator :) ret = %d\n", ret);
42+
msleep(2000);
43+
break;
44+
default:
45+
break;
46+
};
47+
return count;
48+
}
49+
50+
static DEVICE_ATTR(consumer1_debug, S_IRUGO | S_IWUSR, consumer1_debug_show,
51+
consumer1_debug_store);
52+
53+
static struct attribute *consumer1_attrs[] = {
54+
&dev_attr_consumer1_debug.attr,
55+
NULL,
56+
};
57+
58+
static const struct attribute_group consumer1_attr_group = {
59+
.attrs = consumer1_attrs,
60+
};
61+
62+
static const struct i2c_device_id consumer1_id_table[] = {
63+
{ "consumer1", 0 },
64+
{},
65+
};
66+
MODULE_DEVICE_TABLE(i2c, consumer1_id_table);
67+
68+
static int consumer1_probe(struct i2c_client *client,
69+
const struct i2c_device_id *id)
70+
{
71+
static struct regulator *supply;
72+
struct consumer1_chip *chip;
73+
int ret;
74+
75+
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
76+
{
77+
printk("i2c_check_functionality error\n");
78+
return -ENODEV;
79+
}
80+
81+
supply = devm_regulator_get(&client->dev, "VCC_TP");
82+
if (IS_ERR(supply)) {
83+
printk("regulator get of vdd_ana failed");
84+
ret = PTR_ERR(supply);
85+
supply = NULL;
86+
return -1;
87+
}
88+
89+
chip = kzalloc(sizeof(struct consumer1_chip), GFP_KERNEL);
90+
if (chip == NULL) {
91+
printk("kzalloc error\n");
92+
return -ENOMEM;
93+
}
94+
95+
chip->supply = supply;
96+
chip->client = client;
97+
chip->dev = &client->dev;
98+
i2c_set_clientdata(client, chip);
99+
100+
ret = sysfs_create_group(&client->dev.kobj, &consumer1_attr_group);
101+
if (ret) {
102+
printk("failed to create sysfs device attributes\n");
103+
return -1;
104+
}
105+
106+
/* Enable the regulator */
107+
ret = regulator_enable(chip->supply);
108+
printk("Enable regulator :) ret = %d\n", ret);
109+
110+
return 0;
111+
}
112+
113+
static int consumer1_remove(struct i2c_client *client)
114+
{
115+
int ret;
116+
struct consumer1_chip *chip = i2c_get_clientdata(client);
117+
118+
/* Disable the regulator */
119+
regulator_disable(chip->supply);
120+
printk("Disable regulator :( ret = %d\n", ret);
121+
sysfs_remove_group(&client->dev.kobj, &consumer1_attr_group);
122+
kfree(chip);
123+
124+
return 0;
125+
}
126+
127+
static const struct of_device_id of_consumer1_match[] = {
128+
{ .compatible = "Consumer2" },
129+
{ /* Sentinel */ }
130+
};
131+
132+
static struct i2c_driver consumer1_driver = {
133+
.driver = {
134+
.name = "consumer1",
135+
.owner = THIS_MODULE,
136+
.of_match_table = of_consumer1_match,
137+
},
138+
.probe = consumer1_probe,
139+
.remove = consumer1_remove,
140+
.id_table = consumer1_id_table,
141+
};
142+
143+
static int consumer1_init(void)
144+
{
145+
int ret;
146+
ret = i2c_add_driver(&consumer1_driver);
147+
return ret;
148+
}
149+
150+
static void consumer1_exit(void)
151+
{
152+
i2c_del_driver(&consumer1_driver);
153+
}
154+
155+
module_init(consumer1_init);
156+
module_exit(consumer1_exit);
157+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)