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
|
/*
* Copyright (C) 2007 Sascha Hauer, Pengutronix
* 2009 Marc Kleine-Budde <mkl@pengutronix.de>
* 2009 Eric Benard <eric@eukrea.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*/
#include <common.h>
#include <init.h>
#include <driver.h>
#include <xfuncs.h>
#include <errno.h>
#include <i2c/i2c.h>
#include <asm/byteorder.h>
#define DRIVERNAME "lp3972"
struct lp_priv {
struct cdev cdev;
struct i2c_client *client;
};
#define to_lp_priv(a) container_of(a, struct lp_priv, cdev)
static struct lp_priv *lp_dev;
struct i2c_client *lp3972_get_client(void)
{
if (!lp_dev)
return NULL;
return lp_dev->client;
}
static u32 lp_read_reg(struct lp_priv *lp, int reg)
{
u8 buf;
i2c_read_reg(lp->client, reg, &buf, sizeof(buf));
return buf;
}
static ssize_t lp_read(struct cdev *cdev, void *_buf, size_t count, loff_t offset, ulong flags)
{
struct lp_priv *priv = to_lp_priv(cdev);
int i = count;
u8 *buf = _buf;
while (i) {
*buf = lp_read_reg(priv, offset);
buf++;
i--;
offset++;
}
return count;
}
static struct file_operations lp_fops = {
.lseek = dev_lseek_default,
.read = lp_read,
};
static int lp_probe(struct device_d *dev)
{
if (lp_dev)
return -EBUSY;
lp_dev = xzalloc(sizeof(struct lp_priv));
lp_dev->cdev.name = DRIVERNAME;
lp_dev->client = to_i2c_client(dev);
lp_dev->cdev.size = 256;
lp_dev->cdev.dev = dev;
lp_dev->cdev.ops = &lp_fops;
devfs_create(&lp_dev->cdev);
return 0;
}
static struct driver_d lp_driver = {
.name = DRIVERNAME,
.probe = lp_probe,
};
static int lp_init(void)
{
i2c_driver_register(&lp_driver);
return 0;
}
device_initcall(lp_init);
|