-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathregulator.c
180 lines (155 loc) · 3.59 KB
/
regulator.c
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
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/regulator/consumer.h>
/*
* TestScript: Input the shell script below
*
* while true
* do
* insmod regulator.ko
* sleep 5
* rmmod regulator
* sleep 5
* done
*/
/* describe in device tree */
#if 0
goodix_ts@5d {
compatible = "goodix,gt9xx";
status = "okay";
reg = <0x5d>;
VCC_TP-supply = <&ldo4_reg>;
}
#endif
struct myi2c_chip {
struct i2c_client *client;
struct device *dev;
struct regulator *supply;
};
static ssize_t myi2c_debug_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
printk("%s, %d\n", __FUNCTION__, __LINE__);
return 1;
}
/* FIXME:Don't figure it out how it cann't work */
static ssize_t myi2c_debug_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct myi2c_chip *chip;
int ret;
int tmp;
struct regulator *supply;
tmp = simple_strtoul(buf, NULL, 0);
chip = container_of(&dev, struct myi2c_chip, dev);
switch (tmp)
{
case 0:
ret = regulator_disable(chip->supply);
printk("Disable regulator :( ret = %d\n", ret);
msleep(2000);
break;
case 1:
ret = regulator_enable(chip->supply);
printk("Enable regulator :) ret = %d\n", ret);
msleep(2000);
break;
default:
break;
};
return count;
}
static DEVICE_ATTR(myi2c_debug, S_IRUGO | S_IWUSR, myi2c_debug_show,
myi2c_debug_store);
static struct attribute *myi2c_attrs[] = {
&dev_attr_myi2c_debug.attr,
NULL,
};
static const struct attribute_group myi2c_attr_group = {
.attrs = myi2c_attrs,
};
static const struct i2c_device_id myi2c_id_table[] = {
{ "myi2c", 0 },
{},
};
MODULE_DEVICE_TABLE(i2c, myi2c_id_table);
static int myi2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
static struct regulator *supply;
struct myi2c_chip *chip;
int ret;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
{
printk("i2c_check_functionality error\n");
return -ENODEV;
}
supply = devm_regulator_get(&client->dev, "VCC_TP");
if (IS_ERR(supply)) {
printk("regulator get of vdd_ana failed");
ret = PTR_ERR(supply);
supply = NULL;
return -1;
}
chip = kzalloc(sizeof(struct myi2c_chip), GFP_KERNEL);
if (chip == NULL) {
printk("kzalloc error\n");
return -ENOMEM;
}
chip->supply = supply;
chip->client = client;
chip->dev = &client->dev;
i2c_set_clientdata(client, chip);
ret = sysfs_create_group(&client->dev.kobj, &myi2c_attr_group);
if (ret) {
printk("failed to create sysfs device attributes\n");
return -1;
}
/* Enable the regulator */
ret = regulator_enable(chip->supply);
printk("Enable regulator :) ret = %d\n", ret);
return 0;
}
static int myi2c_remove(struct i2c_client *client)
{
int ret;
struct myi2c_chip *chip = i2c_get_clientdata(client);
/* Disable the regulator */
regulator_disable(chip->supply);
printk("Disable regulator :( ret = %d\n", ret);
sysfs_remove_group(&client->dev.kobj, &myi2c_attr_group);
kfree(chip);
return 0;
}
/* Use gt9xx device to test */
static const struct of_device_id of_myi2c_match[] = {
{ .compatible = "goodix,gt9xx" },
{ /* Sentinel */ }
};
static struct i2c_driver myi2c_driver = {
.driver = {
.name = "myi2c",
.owner = THIS_MODULE,
.of_match_table = of_myi2c_match,
},
.probe = myi2c_probe,
.remove = myi2c_remove,
.id_table = myi2c_id_table,
};
static int myi2c_init(void)
{
int ret;
ret = i2c_add_driver(&myi2c_driver);
return ret;
}
static void myi2c_exit(void)
{
i2c_del_driver(&myi2c_driver);
}
module_init(myi2c_init);
module_exit(myi2c_exit);
MODULE_LICENSE("GPL");