1 /*
2  * Driver for the Analog Devices digital potentiometers (SPI bus)
3  *
4  * Copyright (C) 2010-2011 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 /* SPI bus functions */
15 static int write8(void *client, u8 val)
16 {
17 	u8 data = val;
18 	return spi_write(client, &data, 1);
19 }
20 
21 static int write16(void *client, u8 reg, u8 val)
22 {
23 	u8 data[2] = {reg, val};
24 	return spi_write(client, data, 2);
25 }
26 
27 static int write24(void *client, u8 reg, u16 val)
28 {
29 	u8 data[3] = {reg, val >> 8, val};
30 	return spi_write(client, data, 3);
31 }
32 
33 static int read8(void *client)
34 {
35 	int ret;
36 	u8 data;
37 	ret = spi_read(client, &data, 1);
38 	if (ret < 0)
39 		return ret;
40 
41 	return data;
42 }
43 
44 static int read16(void *client, u8 reg)
45 {
46 	int ret;
47 	u8 buf_rx[2];
48 
49 	write16(client, reg, 0);
50 	ret = spi_read(client, buf_rx, 2);
51 	if (ret < 0)
52 		return ret;
53 
54 	return (buf_rx[0] << 8) |  buf_rx[1];
55 }
56 
57 static int read24(void *client, u8 reg)
58 {
59 	int ret;
60 	u8 buf_rx[3];
61 
62 	write24(client, reg, 0);
63 	ret = spi_read(client, buf_rx, 3);
64 	if (ret < 0)
65 		return ret;
66 
67 	return (buf_rx[1] << 8) |  buf_rx[2];
68 }
69 
70 static const struct ad_dpot_bus_ops bops = {
71 	.read_d8	= read8,
72 	.read_r8d8	= read16,
73 	.read_r8d16	= read24,
74 	.write_d8	= write8,
75 	.write_r8d8	= write16,
76 	.write_r8d16	= write24,
77 };
78 static int ad_dpot_spi_probe(struct spi_device *spi)
79 {
80 	struct ad_dpot_bus_data bdata = {
81 		.client = spi,
82 		.bops = &bops,
83 	};
84 
85 	return ad_dpot_probe(&spi->dev, &bdata,
86 			     spi_get_device_id(spi)->driver_data,
87 			     spi_get_device_id(spi)->name);
88 }
89 
90 static int ad_dpot_spi_remove(struct spi_device *spi)
91 {
92 	return ad_dpot_remove(&spi->dev);
93 }
94 
95 static const struct spi_device_id ad_dpot_spi_id[] = {
96 	{"ad5160", AD5160_ID},
97 	{"ad5161", AD5161_ID},
98 	{"ad5162", AD5162_ID},
99 	{"ad5165", AD5165_ID},
100 	{"ad5200", AD5200_ID},
101 	{"ad5201", AD5201_ID},
102 	{"ad5203", AD5203_ID},
103 	{"ad5204", AD5204_ID},
104 	{"ad5206", AD5206_ID},
105 	{"ad5207", AD5207_ID},
106 	{"ad5231", AD5231_ID},
107 	{"ad5232", AD5232_ID},
108 	{"ad5233", AD5233_ID},
109 	{"ad5235", AD5235_ID},
110 	{"ad5260", AD5260_ID},
111 	{"ad5262", AD5262_ID},
112 	{"ad5263", AD5263_ID},
113 	{"ad5290", AD5290_ID},
114 	{"ad5291", AD5291_ID},
115 	{"ad5292", AD5292_ID},
116 	{"ad5293", AD5293_ID},
117 	{"ad7376", AD7376_ID},
118 	{"ad8400", AD8400_ID},
119 	{"ad8402", AD8402_ID},
120 	{"ad8403", AD8403_ID},
121 	{"adn2850", ADN2850_ID},
122 	{"ad5270", AD5270_ID},
123 	{"ad5271", AD5271_ID},
124 	{}
125 };
126 MODULE_DEVICE_TABLE(spi, ad_dpot_spi_id);
127 
128 static struct spi_driver ad_dpot_spi_driver = {
129 	.driver = {
130 		.name	= "ad_dpot",
131 		.owner	= THIS_MODULE,
132 	},
133 	.probe		= ad_dpot_spi_probe,
134 	.remove		= ad_dpot_spi_remove,
135 	.id_table	= ad_dpot_spi_id,
136 };
137 
138 module_spi_driver(ad_dpot_spi_driver);
139 
140 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
141 MODULE_DESCRIPTION("digital potentiometer SPI bus driver");
142 MODULE_LICENSE("GPL");
143 MODULE_ALIAS("spi:ad_dpot");
144