blob: 9da04ede04f356aaef1495bd0cf01408e6556da5 [file] [log] [blame]
Michael Hennerich6c536e42010-05-24 14:33:14 -07001/*
2 * Driver for the Analog Devices digital potentiometers (SPI bus)
3 *
Michael Hennerich7f3379d2011-11-18 11:05:11 +01004 * Copyright (C) 2010-2011 Michael Hennerich, Analog Devices Inc.
Michael Hennerich6c536e42010-05-24 14:33:14 -07005 *
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
Michael Hennerich6c536e42010-05-24 14:33:14 -070014/* SPI bus functions */
15static int write8(void *client, u8 val)
16{
17 u8 data = val;
18 return spi_write(client, &data, 1);
19}
20
21static int write16(void *client, u8 reg, u8 val)
22{
23 u8 data[2] = {reg, val};
Michael Hennerich1f9fa522010-10-26 14:21:16 -070024 return spi_write(client, data, 2);
Michael Hennerich6c536e42010-05-24 14:33:14 -070025}
26
27static int write24(void *client, u8 reg, u16 val)
28{
29 u8 data[3] = {reg, val >> 8, val};
Michael Hennerich1f9fa522010-10-26 14:21:16 -070030 return spi_write(client, data, 3);
Michael Hennerich6c536e42010-05-24 14:33:14 -070031}
32
33static 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
44static 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
57static 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
70static 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};
Bill Pemberton80c8ae22012-11-19 13:23:05 -050078static int ad_dpot_spi_probe(struct spi_device *spi)
Michael Hennerich6c536e42010-05-24 14:33:14 -070079{
Michael Hennerich6c536e42010-05-24 14:33:14 -070080 struct ad_dpot_bus_data bdata = {
81 .client = spi,
82 .bops = &bops,
83 };
84
Michael Hennerich7f3379d2011-11-18 11:05:11 +010085 return ad_dpot_probe(&spi->dev, &bdata,
86 spi_get_device_id(spi)->driver_data,
87 spi_get_device_id(spi)->name);
Michael Hennerich6c536e42010-05-24 14:33:14 -070088}
89
Bill Pemberton486a5c22012-11-19 13:26:02 -050090static int ad_dpot_spi_remove(struct spi_device *spi)
Michael Hennerich6c536e42010-05-24 14:33:14 -070091{
92 return ad_dpot_remove(&spi->dev);
93}
94
Michael Hennerich7f3379d2011-11-18 11:05:11 +010095static 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};
126MODULE_DEVICE_TABLE(spi, ad_dpot_spi_id);
127
Michael Hennerich6c536e42010-05-24 14:33:14 -0700128static struct spi_driver ad_dpot_spi_driver = {
129 .driver = {
130 .name = "ad_dpot",
Michael Hennerich6c536e42010-05-24 14:33:14 -0700131 .owner = THIS_MODULE,
132 },
133 .probe = ad_dpot_spi_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500134 .remove = ad_dpot_spi_remove,
Michael Hennerich7f3379d2011-11-18 11:05:11 +0100135 .id_table = ad_dpot_spi_id,
Michael Hennerich6c536e42010-05-24 14:33:14 -0700136};
137
Axel Lina3dc3c92012-01-22 15:38:22 +0800138module_spi_driver(ad_dpot_spi_driver);
Michael Hennerich6c536e42010-05-24 14:33:14 -0700139
140MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
141MODULE_DESCRIPTION("digital potentiometer SPI bus driver");
142MODULE_LICENSE("GPL");
143MODULE_ALIAS("spi:ad_dpot");