-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathconsumer2.c
157 lines (134 loc) · 3.4 KB
/
consumer2.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
#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>
struct consumer1_chip {
struct i2c_client *client;
struct device *dev;
struct regulator *supply;
};
static ssize_t consumer1_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 consumer1_debug_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct consumer1_chip *chip;
int ret;
int tmp;
struct regulator *supply;
tmp = simple_strtoul(buf, NULL, 0);
chip = container_of(&dev, struct consumer1_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(consumer1_debug, S_IRUGO | S_IWUSR, consumer1_debug_show,
consumer1_debug_store);
static struct attribute *consumer1_attrs[] = {
&dev_attr_consumer1_debug.attr,
NULL,
};
static const struct attribute_group consumer1_attr_group = {
.attrs = consumer1_attrs,
};
static const struct i2c_device_id consumer1_id_table[] = {
{ "consumer1", 0 },
{},
};
MODULE_DEVICE_TABLE(i2c, consumer1_id_table);
static int consumer1_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
static struct regulator *supply;
struct consumer1_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 consumer1_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, &consumer1_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 consumer1_remove(struct i2c_client *client)
{
int ret;
struct consumer1_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, &consumer1_attr_group);
kfree(chip);
return 0;
}
static const struct of_device_id of_consumer1_match[] = {
{ .compatible = "Consumer2" },
{ /* Sentinel */ }
};
static struct i2c_driver consumer1_driver = {
.driver = {
.name = "consumer1",
.owner = THIS_MODULE,
.of_match_table = of_consumer1_match,
},
.probe = consumer1_probe,
.remove = consumer1_remove,
.id_table = consumer1_id_table,
};
static int consumer1_init(void)
{
int ret;
ret = i2c_add_driver(&consumer1_driver);
return ret;
}
static void consumer1_exit(void)
{
i2c_del_driver(&consumer1_driver);
}
module_init(consumer1_init);
module_exit(consumer1_exit);
MODULE_LICENSE("GPL");