Skip to content

Commit 6c536e4

Browse files
mhennerichtorvalds
authored andcommitted
ad525x_dpot: add support for SPI parts
Split the bus logic out into separate files so that we can handle I2C and SPI busses independently. The new SPI bus logic brings in support for a lot more parts: AD5160, AD5161, AD5162, AD5165, AD5200, AD5201, AD5203, AD5204, AD5206, AD5207, AD5231, AD5232, AD5233, AD5235, AD5260, AD5262, AD5263, AD5290, AD5291, AD5292, AD5293, AD7376, AD8400, AD8402, AD8403, ADN2850 [randy.dunlap@oracle.com: fix ad525X_dpot build] Signed-off-by: Michael Hennerich <michael.hennerich@analog.com> Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Randy Dunlap <randy.dunlap@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0c53b9f commit 6c536e4

6 files changed

Lines changed: 806 additions & 319 deletions

File tree

‎drivers/misc/Kconfig‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ menuconfig MISC_DEVICES
1414
if MISC_DEVICES
1515

1616
config AD525X_DPOT
17-
tristate "Analog Devices AD525x Digital Potentiometers"
18-
depends on I2C && SYSFS
17+
tristate "Analog Devices Digital Potentiometers"
18+
depends on (I2C || SPI) && SYSFS
1919
help
2020
If you say yes here, you get support for the Analog Devices
21-
AD5258, AD5259, AD5251, AD5252, AD5253, AD5254 and AD5255
21+
AD5258, AD5259, AD5251, AD5252, AD5253, AD5254, AD5255
22+
AD5160, AD5161, AD5162, AD5165, AD5200, AD5201, AD5203,
23+
AD5204, AD5206, AD5207, AD5231, AD5232, AD5233, AD5235,
24+
AD5260, AD5262, AD5263, AD5290, AD5291, AD5292, AD5293,
25+
AD7376, AD8400, AD8402, AD8403, ADN2850
2226
digital potentiometer chips.
2327

2428
See Documentation/misc-devices/ad525x_dpot.txt for the
@@ -27,6 +31,26 @@ config AD525X_DPOT
2731
This driver can also be built as a module. If so, the module
2832
will be called ad525x_dpot.
2933

34+
config AD525X_DPOT_I2C
35+
tristate "support I2C bus connection"
36+
depends on AD525X_DPOT && I2C
37+
help
38+
Say Y here if you have a digital potentiometers hooked to an I2C bus.
39+
40+
To compile this driver as a module, choose M here: the
41+
module will be called ad525x_dpot-i2c.
42+
43+
config AD525X_DPOT_SPI
44+
tristate "support SPI bus connection"
45+
depends on AD525X_DPOT && SPI_MASTER
46+
help
47+
Say Y here if you have a digital potentiometers hooked to an SPI bus.
48+
49+
If unsure, say N (but it's safe to say "Y").
50+
51+
To compile this driver as a module, choose M here: the
52+
module will be called ad525x_dpot-spi.
53+
3054
config ATMEL_PWM
3155
tristate "Atmel AT32/AT91 PWM support"
3256
depends on AVR32 || ARCH_AT91SAM9263 || ARCH_AT91SAM9RL || ARCH_AT91CAP9

‎drivers/misc/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
obj-$(CONFIG_IBM_ASM) += ibmasm/
66
obj-$(CONFIG_AD525X_DPOT) += ad525x_dpot.o
7+
obj-$(CONFIG_AD525X_DPOT_I2C) += ad525x_dpot-i2c.o
8+
obj-$(CONFIG_AD525X_DPOT_SPI) += ad525x_dpot-spi.o
79
obj-$(CONFIG_ATMEL_PWM) += atmel_pwm.o
810
obj-$(CONFIG_ATMEL_SSC) += atmel-ssc.o
911
obj-$(CONFIG_ATMEL_TCLIB) += atmel_tclib.o

‎drivers/misc/ad525x_dpot-i2c.c‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Driver for the Analog Devices digital potentiometers (I2C bus)
3+
*
4+
* Copyright (C) 2010 Michael Hennerich, Analog Devices Inc.
5+
*
6+
* Licensed under the GPL-2 or later.
7+
*/
8+
9+
#include <linux/i2c.h>
10+
#include <linux/module.h>
11+
12+
#include "ad525x_dpot.h"
13+
14+
/* ------------------------------------------------------------------------- */
15+
/* I2C bus functions */
16+
static int write_d8(void *client, u8 val)
17+
{
18+
return i2c_smbus_write_byte(client, val);
19+
}
20+
21+
static int write_r8d8(void *client, u8 reg, u8 val)
22+
{
23+
return i2c_smbus_write_byte_data(client, reg, val);
24+
}
25+
26+
static int write_r8d16(void *client, u8 reg, u16 val)
27+
{
28+
return i2c_smbus_write_word_data(client, reg, val);
29+
}
30+
31+
static int read_d8(void *client)
32+
{
33+
return i2c_smbus_read_byte(client);
34+
}
35+
36+
static int read_r8d8(void *client, u8 reg)
37+
{
38+
return i2c_smbus_read_byte_data(client, reg);
39+
}
40+
41+
static int read_r8d16(void *client, u8 reg)
42+
{
43+
return i2c_smbus_read_word_data(client, reg);
44+
}
45+
46+
static const struct ad_dpot_bus_ops bops = {
47+
.read_d8 = read_d8,
48+
.read_r8d8 = read_r8d8,
49+
.read_r8d16 = read_r8d16,
50+
.write_d8 = write_d8,
51+
.write_r8d8 = write_r8d8,
52+
.write_r8d16 = write_r8d16,
53+
};
54+
55+
static int __devinit ad_dpot_i2c_probe(struct i2c_client *client,
56+
const struct i2c_device_id *id)
57+
{
58+
struct ad_dpot_bus_data bdata = {
59+
.client = client,
60+
.bops = &bops,
61+
};
62+
63+
struct ad_dpot_id dpot_id = {
64+
.name = (char *) &id->name,
65+
.devid = id->driver_data,
66+
};
67+
68+
if (!i2c_check_functionality(client->adapter,
69+
I2C_FUNC_SMBUS_WORD_DATA)) {
70+
dev_err(&client->dev, "SMBUS Word Data not Supported\n");
71+
return -EIO;
72+
}
73+
74+
return ad_dpot_probe(&client->dev, &bdata, &dpot_id);
75+
}
76+
77+
static int __devexit ad_dpot_i2c_remove(struct i2c_client *client)
78+
{
79+
return ad_dpot_remove(&client->dev);
80+
}
81+
82+
static const struct i2c_device_id ad_dpot_id[] = {
83+
{"ad5258", AD5258_ID},
84+
{"ad5259", AD5259_ID},
85+
{"ad5251", AD5251_ID},
86+
{"ad5252", AD5252_ID},
87+
{"ad5253", AD5253_ID},
88+
{"ad5254", AD5254_ID},
89+
{"ad5255", AD5255_ID},
90+
{}
91+
};
92+
MODULE_DEVICE_TABLE(i2c, ad_dpot_id);
93+
94+
static struct i2c_driver ad_dpot_i2c_driver = {
95+
.driver = {
96+
.name = "ad_dpot",
97+
.owner = THIS_MODULE,
98+
},
99+
.probe = ad_dpot_i2c_probe,
100+
.remove = __devexit_p(ad_dpot_i2c_remove),
101+
.id_table = ad_dpot_id,
102+
};
103+
104+
static int __init ad_dpot_i2c_init(void)
105+
{
106+
return i2c_add_driver(&ad_dpot_i2c_driver);
107+
}
108+
module_init(ad_dpot_i2c_init);
109+
110+
static void __exit ad_dpot_i2c_exit(void)
111+
{
112+
i2c_del_driver(&ad_dpot_i2c_driver);
113+
}
114+
module_exit(ad_dpot_i2c_exit);
115+
116+
MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
117+
MODULE_DESCRIPTION("digital potentiometer I2C bus driver");
118+
MODULE_LICENSE("GPL");
119+
MODULE_ALIAS("i2c:ad_dpot");

‎drivers/misc/ad525x_dpot-spi.c‎

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Driver for the Analog Devices digital potentiometers (SPI bus)
3+
*
4+
* Copyright (C) 2010 Michael Hennerich, Analog Devices Inc.
5+
*
6+
* Licensed under the GPL-2 or later.
7+
*/
8+
9+
#include <linux/spi/spi.h>
10+
#include <linux/module.h>
11+
12+
#include "ad525x_dpot.h"
13+
14+
static const struct ad_dpot_id ad_dpot_spi_devlist[] = {
15+
{.name = "ad5160", .devid = AD5160_ID},
16+
{.name = "ad5161", .devid = AD5161_ID},
17+
{.name = "ad5162", .devid = AD5162_ID},
18+
{.name = "ad5165", .devid = AD5165_ID},
19+
{.name = "ad5200", .devid = AD5200_ID},
20+
{.name = "ad5201", .devid = AD5201_ID},
21+
{.name = "ad5203", .devid = AD5203_ID},
22+
{.name = "ad5204", .devid = AD5204_ID},
23+
{.name = "ad5206", .devid = AD5206_ID},
24+
{.name = "ad5207", .devid = AD5207_ID},
25+
{.name = "ad5231", .devid = AD5231_ID},
26+
{.name = "ad5232", .devid = AD5232_ID},
27+
{.name = "ad5233", .devid = AD5233_ID},
28+
{.name = "ad5235", .devid = AD5235_ID},
29+
{.name = "ad5260", .devid = AD5260_ID},
30+
{.name = "ad5262", .devid = AD5262_ID},
31+
{.name = "ad5263", .devid = AD5263_ID},
32+
{.name = "ad5290", .devid = AD5290_ID},
33+
{.name = "ad5291", .devid = AD5291_ID},
34+
{.name = "ad5292", .devid = AD5292_ID},
35+
{.name = "ad5293", .devid = AD5293_ID},
36+
{.name = "ad7376", .devid = AD7376_ID},
37+
{.name = "ad8400", .devid = AD8400_ID},
38+
{.name = "ad8402", .devid = AD8402_ID},
39+
{.name = "ad8403", .devid = AD8403_ID},
40+
{.name = "adn2850", .devid = ADN2850_ID},
41+
{}
42+
};
43+
44+
/* ------------------------------------------------------------------------- */
45+
46+
/* SPI bus functions */
47+
static int write8(void *client, u8 val)
48+
{
49+
u8 data = val;
50+
return spi_write(client, &data, 1);
51+
}
52+
53+
static int write16(void *client, u8 reg, u8 val)
54+
{
55+
u8 data[2] = {reg, val};
56+
return spi_write(client, data, 1);
57+
}
58+
59+
static int write24(void *client, u8 reg, u16 val)
60+
{
61+
u8 data[3] = {reg, val >> 8, val};
62+
return spi_write(client, data, 1);
63+
}
64+
65+
static int read8(void *client)
66+
{
67+
int ret;
68+
u8 data;
69+
ret = spi_read(client, &data, 1);
70+
if (ret < 0)
71+
return ret;
72+
73+
return data;
74+
}
75+
76+
static int read16(void *client, u8 reg)
77+
{
78+
int ret;
79+
u8 buf_rx[2];
80+
81+
write16(client, reg, 0);
82+
ret = spi_read(client, buf_rx, 2);
83+
if (ret < 0)
84+
return ret;
85+
86+
return (buf_rx[0] << 8) | buf_rx[1];
87+
}
88+
89+
static int read24(void *client, u8 reg)
90+
{
91+
int ret;
92+
u8 buf_rx[3];
93+
94+
write24(client, reg, 0);
95+
ret = spi_read(client, buf_rx, 3);
96+
if (ret < 0)
97+
return ret;
98+
99+
return (buf_rx[1] << 8) | buf_rx[2];
100+
}
101+
102+
static const struct ad_dpot_bus_ops bops = {
103+
.read_d8 = read8,
104+
.read_r8d8 = read16,
105+
.read_r8d16 = read24,
106+
.write_d8 = write8,
107+
.write_r8d8 = write16,
108+
.write_r8d16 = write24,
109+
};
110+
111+
static const struct ad_dpot_id *dpot_match_id(const struct ad_dpot_id *id,
112+
char *name)
113+
{
114+
while (id->name && id->name[0]) {
115+
if (strcmp(name, id->name) == 0)
116+
return id;
117+
id++;
118+
}
119+
return NULL;
120+
}
121+
122+
static int __devinit ad_dpot_spi_probe(struct spi_device *spi)
123+
{
124+
char *name = spi->dev.platform_data;
125+
const struct ad_dpot_id *dpot_id;
126+
127+
struct ad_dpot_bus_data bdata = {
128+
.client = spi,
129+
.bops = &bops,
130+
};
131+
132+
dpot_id = dpot_match_id(ad_dpot_spi_devlist, name);
133+
134+
if (dpot_id == NULL) {
135+
dev_err(&spi->dev, "%s not in supported device list", name);
136+
return -ENODEV;
137+
}
138+
139+
return ad_dpot_probe(&spi->dev, &bdata, dpot_id);
140+
}
141+
142+
static int __devexit ad_dpot_spi_remove(struct spi_device *spi)
143+
{
144+
return ad_dpot_remove(&spi->dev);
145+
}
146+
147+
static struct spi_driver ad_dpot_spi_driver = {
148+
.driver = {
149+
.name = "ad_dpot",
150+
.bus = &spi_bus_type,
151+
.owner = THIS_MODULE,
152+
},
153+
.probe = ad_dpot_spi_probe,
154+
.remove = __devexit_p(ad_dpot_spi_remove),
155+
};
156+
157+
static int __init ad_dpot_spi_init(void)
158+
{
159+
return spi_register_driver(&ad_dpot_spi_driver);
160+
}
161+
module_init(ad_dpot_spi_init);
162+
163+
static void __exit ad_dpot_spi_exit(void)
164+
{
165+
spi_unregister_driver(&ad_dpot_spi_driver);
166+
}
167+
module_exit(ad_dpot_spi_exit);
168+
169+
MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
170+
MODULE_DESCRIPTION("digital potentiometer SPI bus driver");
171+
MODULE_LICENSE("GPL");
172+
MODULE_ALIAS("spi:ad_dpot");

0 commit comments

Comments
 (0)