Skip to content

Commit f5fc879

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap: regulator: Convert tps65023 to use regmap API regmap: Add SPI bus support regmap: Add I2C bus support regmap: Add generic non-memory mapped register access API
2 parents d4e0670 + 9092335 commit f5fc879

11 files changed

Lines changed: 778 additions & 70 deletions

File tree

‎MAINTAINERS‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5327,6 +5327,13 @@ L: reiserfs-devel@vger.kernel.org
53275327
S: Supported
53285328
F: fs/reiserfs/
53295329

5330+
REGISTER MAP ABSTRACTION
5331+
M: Mark Brown <broonie@opensource.wolfsonmicro.com>
5332+
T: git git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git
5333+
S: Supported
5334+
F: drivers/base/regmap/
5335+
F: include/linux/regmap.h
5336+
53305337
RFKILL
53315338
M: Johannes Berg <johannes@sipsolutions.net>
53325339
L: linux-wireless@vger.kernel.org

‎drivers/base/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,6 @@ config SYS_HYPERVISOR
168168
bool
169169
default n
170170

171+
source "drivers/base/regmap/Kconfig"
172+
171173
endmenu

‎drivers/base/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ifeq ($(CONFIG_SYSFS),y)
1717
obj-$(CONFIG_MODULES) += module.o
1818
endif
1919
obj-$(CONFIG_SYS_HYPERVISOR) += hypervisor.o
20+
obj-$(CONFIG_REGMAP) += regmap/
2021

2122
ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG
2223

‎drivers/base/regmap/Kconfig‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Generic register map support. There are no user servicable options here,
2+
# this is an API intended to be used by other kernel subsystems. These
3+
# subsystems should select the appropriate symbols.
4+
5+
config REGMAP
6+
default y if (REGMAP_I2C || REGMAP_SPI)
7+
bool
8+
9+
config REGMAP_I2C
10+
tristate
11+
12+
config REGMAP_SPI
13+
tristate

‎drivers/base/regmap/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
obj-$(CONFIG_REGMAP) += regmap.o
2+
obj-$(CONFIG_REGMAP_I2C) += regmap-i2c.o
3+
obj-$(CONFIG_REGMAP_SPI) += regmap-spi.o

‎drivers/base/regmap/regmap-i2c.c‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Register map access API - I2C support
3+
*
4+
* Copyright 2011 Wolfson Microelectronics plc
5+
*
6+
* Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7+
*
8+
* This program is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License version 2 as
10+
* published by the Free Software Foundation.
11+
*/
12+
13+
#include <linux/regmap.h>
14+
#include <linux/i2c.h>
15+
#include <linux/module.h>
16+
#include <linux/init.h>
17+
18+
static int regmap_i2c_write(struct device *dev, const void *data, size_t count)
19+
{
20+
struct i2c_client *i2c = to_i2c_client(dev);
21+
int ret;
22+
23+
ret = i2c_master_send(i2c, data, count);
24+
if (ret == count)
25+
return 0;
26+
else if (ret < 0)
27+
return ret;
28+
else
29+
return -EIO;
30+
}
31+
32+
static int regmap_i2c_gather_write(struct device *dev,
33+
const void *reg, size_t reg_size,
34+
const void *val, size_t val_size)
35+
{
36+
struct i2c_client *i2c = to_i2c_client(dev);
37+
struct i2c_msg xfer[2];
38+
int ret;
39+
40+
/* If the I2C controller can't do a gather tell the core, it
41+
* will substitute in a linear write for us.
42+
*/
43+
if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_PROTOCOL_MANGLING))
44+
return -ENOTSUPP;
45+
46+
xfer[0].addr = i2c->addr;
47+
xfer[0].flags = 0;
48+
xfer[0].len = reg_size;
49+
xfer[0].buf = (void *)reg;
50+
51+
xfer[1].addr = i2c->addr;
52+
xfer[1].flags = I2C_M_NOSTART;
53+
xfer[1].len = val_size;
54+
xfer[1].buf = (void *)val;
55+
56+
ret = i2c_transfer(i2c->adapter, xfer, 2);
57+
if (ret == 2)
58+
return 0;
59+
if (ret < 0)
60+
return ret;
61+
else
62+
return -EIO;
63+
}
64+
65+
static int regmap_i2c_read(struct device *dev,
66+
const void *reg, size_t reg_size,
67+
void *val, size_t val_size)
68+
{
69+
struct i2c_client *i2c = to_i2c_client(dev);
70+
struct i2c_msg xfer[2];
71+
int ret;
72+
73+
xfer[0].addr = i2c->addr;
74+
xfer[0].flags = 0;
75+
xfer[0].len = reg_size;
76+
xfer[0].buf = (void *)reg;
77+
78+
xfer[1].addr = i2c->addr;
79+
xfer[1].flags = I2C_M_RD;
80+
xfer[1].len = val_size;
81+
xfer[1].buf = val;
82+
83+
ret = i2c_transfer(i2c->adapter, xfer, 2);
84+
if (ret == 2)
85+
return 0;
86+
else if (ret < 0)
87+
return ret;
88+
else
89+
return -EIO;
90+
}
91+
92+
static struct regmap_bus regmap_i2c = {
93+
.type = &i2c_bus_type,
94+
.write = regmap_i2c_write,
95+
.gather_write = regmap_i2c_gather_write,
96+
.read = regmap_i2c_read,
97+
.owner = THIS_MODULE,
98+
};
99+
100+
/**
101+
* regmap_init_i2c(): Initialise register map
102+
*
103+
* @i2c: Device that will be interacted with
104+
* @config: Configuration for register map
105+
*
106+
* The return value will be an ERR_PTR() on error or a valid pointer to
107+
* a struct regmap.
108+
*/
109+
struct regmap *regmap_init_i2c(struct i2c_client *i2c,
110+
const struct regmap_config *config)
111+
{
112+
return regmap_init(&i2c->dev, &regmap_i2c, config);
113+
}
114+
EXPORT_SYMBOL_GPL(regmap_init_i2c);
115+

‎drivers/base/regmap/regmap-spi.c‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Register map access API - SPI support
3+
*
4+
* Copyright 2011 Wolfson Microelectronics plc
5+
*
6+
* Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7+
*
8+
* This program is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License version 2 as
10+
* published by the Free Software Foundation.
11+
*/
12+
13+
#include <linux/regmap.h>
14+
#include <linux/spi/spi.h>
15+
#include <linux/init.h>
16+
17+
static int regmap_spi_write(struct device *dev, const void *data, size_t count)
18+
{
19+
struct spi_device *spi = to_spi_device(dev);
20+
21+
return spi_write(spi, data, count);
22+
}
23+
24+
static int regmap_spi_gather_write(struct device *dev,
25+
const void *reg, size_t reg_len,
26+
const void *val, size_t val_len)
27+
{
28+
struct spi_device *spi = to_spi_device(dev);
29+
struct spi_message m;
30+
struct spi_transfer t[2] = { { .tx_buf = reg, .len = reg_len, },
31+
{ .tx_buf = val, .len = val_len, }, };
32+
33+
spi_message_init(&m);
34+
spi_message_add_tail(&t[0], &m);
35+
spi_message_add_tail(&t[1], &m);
36+
37+
return spi_sync(spi, &m);
38+
}
39+
40+
static int regmap_spi_read(struct device *dev,
41+
const void *reg, size_t reg_size,
42+
void *val, size_t val_size)
43+
{
44+
struct spi_device *spi = to_spi_device(dev);
45+
46+
return spi_write_then_read(spi, reg, reg_size, val, val_size);
47+
}
48+
49+
static struct regmap_bus regmap_spi = {
50+
.type = &spi_bus_type,
51+
.write = regmap_spi_write,
52+
.gather_write = regmap_spi_gather_write,
53+
.read = regmap_spi_read,
54+
.owner = THIS_MODULE,
55+
.read_flag_mask = 0x80,
56+
};
57+
58+
/**
59+
* regmap_init_spi(): Initialise register map
60+
*
61+
* @spi: Device that will be interacted with
62+
* @config: Configuration for register map
63+
*
64+
* The return value will be an ERR_PTR() on error or a valid pointer to
65+
* a struct regmap.
66+
*/
67+
struct regmap *regmap_init_spi(struct spi_device *spi,
68+
const struct regmap_config *config)
69+
{
70+
return regmap_init(&spi->dev, &regmap_spi, config);
71+
}
72+
EXPORT_SYMBOL_GPL(regmap_init_spi);

0 commit comments

Comments
 (0)