Skip to content

Commit 2137b8a

Browse files
committed
i2c regmap
1 parent 25b7b9f commit 2137b8a

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed

debug/i2c/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ DEBFLAGS = -O2
1515
endif
1616

1717
obj-m := myi2c.o
18+
obj-m += i2c_regmap.o
1819
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
1920
CC ?= gcc
2021
PWD := $(shell pwd)

debug/i2c/i2c_regmap.c

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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/of_gpio.h>
7+
#include <linux/of.h>
8+
#include <linux/of_device.h>
9+
#include <linux/regmap.h>
10+
11+
/* describe in device tree */
12+
#if 0
13+
&i2c0 {
14+
myi2c@5a {
15+
compatible = "myi2c";
16+
status = "okay";
17+
reg = <0x5a>;
18+
};
19+
}
20+
#endif
21+
22+
struct myi2c_chip {
23+
char name[100];
24+
struct i2c_client *client;
25+
struct device *dev;
26+
struct regmap *regmap;
27+
};
28+
struct myi2c_chip chip;
29+
30+
/* regmap config */
31+
#define REGISTER_NUMBERS 0xF5
32+
static const struct regmap_config regmap_config = {
33+
.reg_bits = 8,
34+
.val_bits = 8,
35+
.max_register = REGISTER_NUMBERS,
36+
.cache_type = REGCACHE_RBTREE,
37+
};
38+
39+
static ssize_t myi2c_debug_show(struct device *dev,
40+
struct device_attribute *attr,
41+
char *buf)
42+
{
43+
unsigned int rval;
44+
//struct myi2c_chip *chip;
45+
//chip = container_of(dev, struct myi2c_chip, dev);
46+
printk("chip name = %s\n", chip.name);
47+
48+
printk("regmap = %p\n", &chip.regmap);
49+
if (regmap_read(chip.regmap, 0x22, &rval) == 0)
50+
{
51+
printk("reg map read ERROR\n");
52+
return -1;
53+
}
54+
else
55+
{
56+
printk("reg map read ok\n");
57+
if ((rval < 0) || (rval == 0xff))
58+
printk("The device is not act8846\n");
59+
}
60+
61+
// printk("myi2c reg[0x22] = %d\n", rval);
62+
return sprintf(buf,"REG[0x22]=%d\n", rval);
63+
}
64+
65+
static ssize_t myi2c_debug_store(struct device *dev,
66+
struct device_attribute *attr, const char *buf, size_t count)
67+
{
68+
int tmp;
69+
tmp = simple_strtoul(buf, NULL, 16);
70+
return count;
71+
}
72+
73+
static DEVICE_ATTR(myi2c_debug, S_IRUGO | S_IWUSR, myi2c_debug_show, myi2c_debug_store);
74+
75+
static struct attribute *myi2c_attrs[] = {
76+
&dev_attr_myi2c_debug.attr,
77+
NULL,
78+
};
79+
80+
static const struct attribute_group myi2c_attr_group = {
81+
.attrs = myi2c_attrs,
82+
};
83+
84+
/* I2C device id */
85+
static const struct i2c_device_id myi2c_id_table[] = {
86+
{ "myi2c", 0 },
87+
{},
88+
};
89+
MODULE_DEVICE_TABLE(i2c, myi2c_id_table);
90+
91+
static const struct of_device_id of_myi2c_match[] = {
92+
{ .compatible = "myi2c" },
93+
{ /* Sentinel */ }
94+
};
95+
96+
static int myi2c_probe(struct i2c_client *client,
97+
const struct i2c_device_id *id)
98+
{
99+
//struct myi2c_chip *chip;
100+
int ret;
101+
const struct of_device_id *match;
102+
unsigned int rval;
103+
104+
/* 检查DT */
105+
if (client->dev.of_node) {
106+
match = of_match_device(of_myi2c_match, &client->dev);
107+
if (!match) {
108+
printk("Failed to find matching dt id\n");
109+
return -EINVAL;
110+
}
111+
}
112+
113+
#if 0
114+
/* 为自定义结构提分配数据空间 */
115+
chip = kzalloc(sizeof(struct myi2c_chip), GFP_KERNEL);
116+
//chip = devm_kzalloc(&client->dev, sizeof(struct myi2c_chip), GFP_KERNEL);
117+
if (chip == NULL) {
118+
ret = -ENOMEM;
119+
printk("No enough memory ERROR!!\n");
120+
}
121+
#endif
122+
/* 设置chip */
123+
strcpy(chip.name, "myi2ctest");
124+
chip.client = client;
125+
chip.dev = &client->dev;
126+
127+
/* 设置client 的driver data 指向chip */
128+
i2c_set_clientdata(client, &chip);
129+
130+
/* 映射寄存器地址 */
131+
chip.regmap = devm_regmap_init_i2c(client, &regmap_config);
132+
if (IS_ERR(chip.regmap)) {
133+
ret = PTR_ERR(chip.regmap);
134+
printk("regmap initialization failed: %d\n", ret);
135+
return ret;
136+
}
137+
138+
/* I2C 通讯 */
139+
regmap_read(chip.regmap, 0x22, &rval);
140+
if ((rval < 0) || (rval == 0xff)){
141+
printk("The device is not act8846 %x \n",ret);
142+
ret = -1;
143+
}
144+
printk("myi2c reg[0x22] = %d\n", rval);
145+
printk("regmap = %p\n", &chip.regmap);
146+
147+
/* read write 0xF4 for test */
148+
regmap_read(chip.regmap, 0xf4, &rval);
149+
if ((rval < 0) || (rval == 0xff)){
150+
printk("The device is not act8846 %x \n",ret);
151+
ret = -1;
152+
}
153+
printk("myi2c read reg[0xf4] = %d\n", rval);
154+
155+
ret = regmap_write(chip.regmap, 0xf4, 1);
156+
if (ret < 0) {
157+
printk("myi2c set 0xf4 error!\n");
158+
return -1;
159+
}
160+
printk("write 0xf4 = 1\n");
161+
regmap_read(chip.regmap, 0xf4, &rval);
162+
if ((rval < 0) || (rval == 0xff)){
163+
printk("The device is not act8846 %x \n",ret);
164+
ret = -1;
165+
}
166+
printk("myi2c read reg[0xf4] = %d\n", rval);
167+
168+
ret = sysfs_create_group(&client->dev.kobj, &myi2c_attr_group);
169+
if (ret) {
170+
printk("failed to create sysfs device attributes\n");
171+
return -1;
172+
}
173+
printk("Probe Done\n");
174+
175+
return 0;
176+
}
177+
178+
static int myi2c_remove(struct i2c_client *client)
179+
{
180+
sysfs_remove_group(&client->dev.kobj, &myi2c_attr_group);
181+
182+
return 0;
183+
}
184+
185+
static struct i2c_driver myi2c_driver = {
186+
.driver = {
187+
.name = "myi2c",
188+
.owner = THIS_MODULE,
189+
.of_match_table = of_myi2c_match,
190+
},
191+
.probe = myi2c_probe,
192+
.remove = myi2c_remove,
193+
.id_table = myi2c_id_table,
194+
};
195+
196+
static int myi2c_init(void)
197+
{
198+
int ret;
199+
ret = i2c_add_driver(&myi2c_driver);
200+
return ret;
201+
}
202+
203+
static void myi2c_exit(void)
204+
{
205+
i2c_del_driver(&myi2c_driver);
206+
}
207+
208+
module_init(myi2c_init);
209+
module_exit(myi2c_exit);
210+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)