Skip to content

Commit 6ac2bc2

Browse files
gprasadbngbuha
authored andcommitted
drivers: potentiometer: AD5165
This is the driver for AD5165 potentiometer with 256 position respeectively. These devices use SPI communication interface. Signed-off-by: gprasad <gururajendra.prasad@analog.com>
1 parent 2a253df commit 6ac2bc2

File tree

2 files changed

+298
-0
lines changed

2 files changed

+298
-0
lines changed

drivers/potentiometer/ad5165/ad5165.c

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/***************************************************************************//**
2+
* @file ad5165.c
3+
* @brief Source file for the ad5165 digital potentiometer drivers
4+
********************************************************************************
5+
Copyright 2025(c) Analog Devices, Inc.
6+
7+
Redistribution and use in source and binary forms, with or without
8+
modification, are permitted provided that the following conditions are met:
9+
10+
1. Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
13+
2. Redistributions in binary form must reproduce the above copyright notice,
14+
this list of conditions and the following disclaimer in the documentation
15+
and/or other materials provided with the distribution.
16+
17+
3. Neither the name of Analog Devices, Inc. nor the names of its
18+
contributors may be used to endorse or promote products derived from this
19+
software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR
22+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
24+
EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
25+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
27+
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*******************************************************************************/
32+
#include "ad5165.h"
33+
#include "no_os_delay.h"
34+
35+
static int ad5165_dpot_send_cmd_write(struct dpot_dev *desc,
36+
struct dpot_command *cmd);
37+
static uint8_t nRdac_value = 0x80;
38+
/**
39+
* @brief Initialize the ad5165 digital potentiometer.
40+
* @param param - digipot init parameters.
41+
* @param desc - digipot descriptor.
42+
* @return 0 in case of success, negative error code otherwise.
43+
*/
44+
int ad5165_dpot_init(struct dpot_init_param *param, struct dpot_dev **desc)
45+
{
46+
int ret = -EINVAL;
47+
struct dpot_dev *dev = NULL;
48+
struct ad5165_dpot_init_param *ad5165_params;
49+
struct ad5165_dpot_dev *ad5165_dev;
50+
struct no_os_spi_init_param *spi_init;
51+
if (!param)
52+
return -EINVAL;
53+
54+
/* Allocate memory for digipot device descriptor */
55+
dev = (struct dpot_dev *)calloc(1, sizeof(struct dpot_dev));
56+
if (!dev)
57+
return -ENOMEM;
58+
59+
/* Allocate memory for ad5165 digipot device descriptor */
60+
ad5165_dev = (struct ad5165_dpot_dev *)calloc(1, sizeof(*ad5165_dev));
61+
if (!ad5165_dev) {
62+
ret = -ENOMEM;
63+
goto err_ad5165_dev;
64+
}
65+
66+
ad5165_params = param->extra;
67+
ad5165_params->eIntfType = param->intf_type;
68+
dev->extra = ad5165_dev;
69+
70+
/* Initialize the digital interface */
71+
if (param->intf_type == AD_SPI_INTERFACE) {
72+
spi_init = ((struct ad5165_dpot_init_param *)param->extra)->spi_init;
73+
ret = no_os_spi_init(&ad5165_dev->spi_desc, spi_init);
74+
if (ret)
75+
goto err_intf_init;
76+
77+
} else {
78+
/* Interface not supported */
79+
ret = -EINVAL;
80+
goto err_intf_init;
81+
}
82+
83+
ad5165_dev->intf_type = param->intf_type;
84+
85+
*desc = dev;
86+
87+
return 0;
88+
89+
err_intf_init:
90+
free(ad5165_dev);
91+
err_ad5165_dev:
92+
free(dev);
93+
94+
return ret;
95+
}
96+
97+
/**
98+
* @brief Free the memory allocated by ad5165_dpot_init().
99+
* @param desc - digipot descriptor.
100+
* @return 0 in case of success, negative error code otherwise.
101+
*/
102+
int ad5165_dpot_remove(struct dpot_dev *desc)
103+
{
104+
int ret;
105+
struct ad5165_dpot_dev *ad5165_dev;
106+
107+
if (!desc || !desc->extra)
108+
return -EINVAL;
109+
110+
ad5165_dev = desc->extra;
111+
112+
if (ad5165_dev->intf_type == AD_SPI_INTERFACE) {
113+
ret = no_os_spi_remove((struct no_os_spi_desc *)ad5165_dev->spi_desc);
114+
if (ret)
115+
return ret;
116+
}
117+
free(ad5165_dev);
118+
free(desc);
119+
120+
return 0;
121+
}
122+
/**
123+
* @brief Write to the digital potentiometer channel.
124+
* @param desc - digipot descriptor.
125+
* @param chn - digipot channel.
126+
* @param data - Channel data to be written.
127+
* @return 0 in case of success, negative error code otherwise.
128+
*/
129+
int ad5165_dpot_chn_write(struct dpot_dev *desc,
130+
enum dpot_chn_type chn,
131+
uint8_t data)
132+
{
133+
int ret;
134+
struct dpot_command cmd;
135+
136+
if (!desc)
137+
return -EINVAL;
138+
139+
/* Write contents of serial register data to RDAC (command #1) */
140+
cmd.data = data;
141+
cmd.is_readback = true;
142+
143+
ret = ad5165_dpot_send_cmd_write(desc, &cmd);
144+
if (ret)
145+
return ret;
146+
nRdac_value = data;
147+
return 0;
148+
}
149+
/**
150+
* @brief Read the ad5165 digital potentiometer channel.
151+
* @param desc - digipot descriptor.
152+
* @param chn - digipot channel.
153+
* @param data - Channel data to be read.
154+
* @return 0 in case of success, negative error code otherwise.
155+
*/
156+
int ad5165_dpot_chn_read(struct dpot_dev *desc,
157+
enum dpot_chn_type chn, uint8_t *data)
158+
{
159+
if (!desc || chn > DPOT_CHN_RDAC1)
160+
return -EINVAL;
161+
162+
*data = nRdac_value;
163+
164+
return 0;
165+
}
166+
167+
/**
168+
* @brief Send command word to the ad5165 digital potentiometer.
169+
* @param desc - digipot descriptor.
170+
* @param cmd - digipot command word
171+
* @return 0 in case of success, negative error code otherwise.
172+
*/
173+
int ad5165_dpot_send_cmd_write(struct dpot_dev *desc,
174+
struct dpot_command *cmd)
175+
{
176+
int ret;
177+
uint8_t buf[2];
178+
179+
struct ad5165_dpot_dev *ad5165_dev;
180+
if (!desc || !cmd)
181+
return -EINVAL;
182+
183+
ad5165_dev = desc->extra;
184+
185+
/* copy the word to be programmed to RDAC register */
186+
buf[0] = 0;
187+
buf[1] = cmd->data;
188+
/* Send command word based on the interface type selection */
189+
190+
if (ad5165_dev->intf_type == AD_SPI_INTERFACE) {
191+
ret = no_os_spi_write_and_read(ad5165_dev->spi_desc, &buf[0], sizeof(buf));
192+
if (ret)
193+
return ret;
194+
} else {
195+
return -EINVAL;
196+
}
197+
198+
return 0;
199+
}
200+
201+
/* ad5165 digital potentiometer ops structure */
202+
const struct dpot_ops ad5165_dpot_ops = {
203+
.dpot_init = &ad5165_dpot_init,
204+
.dpot_reset = NULL,
205+
.dpot_shutdown = NULL,
206+
.dpot_set_operating_mode = NULL,
207+
.dpot_input_reg_read = NULL,
208+
.dpot_input_reg_write = NULL,
209+
.dpot_sw_lrdac_update = NULL,
210+
.dpot_chn_read = &ad5165_dpot_chn_read,
211+
.dpot_chn_write = &ad5165_dpot_chn_write,
212+
.dpot_nvm_read = NULL,
213+
.dpot_nvm_write = NULL,
214+
.dpot_copy_rdac_to_nvm = NULL,
215+
.dpot_copy_nvm_to_rdac = NULL,
216+
.dpot_rdac_linear_update = NULL,
217+
.dpot_rdac_6db_update = NULL,
218+
.dpot_remove = &ad5165_dpot_remove,
219+
.dpot_enable_top_bottom_scale = NULL,
220+
.dpot_tolerance_read = NULL,
221+
.dpot_set_mid_scale = NULL
222+
};

drivers/potentiometer/ad5165/ad5165.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/***************************************************************************//**
2+
* @file ad5165.h
3+
* @brief Header file for the ad5165 digital potentiometer drivers
4+
********************************************************************************
5+
Copyright 2025(c) Analog Devices, Inc.
6+
7+
Redistribution and use in source and binary forms, with or without
8+
modification, are permitted provided that the following conditions are met:
9+
10+
1. Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
13+
2. Redistributions in binary form must reproduce the above copyright notice,
14+
this list of conditions and the following disclaimer in the documentation
15+
and/or other materials provided with the distribution.
16+
17+
3. Neither the name of Analog Devices, Inc. nor the names of its
18+
contributors may be used to endorse or promote products derived from this
19+
software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR
22+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
24+
EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
25+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
27+
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*******************************************************************************/
32+
#ifndef AD5165_H_
33+
#define AD5165_H_
34+
35+
#include "../common/dpot.h"
36+
#include "no_os_spi.h"
37+
#include "no_os_i2c.h"
38+
#include "no_os_gpio.h"
39+
#include "no_os_util.h"
40+
41+
/**
42+
* @struct ad5165_dpot_init_param.
43+
* @brief ad5165 digital potentiometer init parameters.
44+
*/
45+
struct ad5165_dpot_init_param {
46+
/* Interface type */
47+
enum dpot_intf_type eIntfType;
48+
/* SPI init parameters */
49+
struct no_os_spi_init_param *spi_init;
50+
};
51+
/**
52+
* @struct ad5165_dpot_dev.
53+
* @brief ad5165 digital potentiometer device descriptor parameters.
54+
*/
55+
struct ad5165_dpot_dev {
56+
enum dpot_intf_type intf_type;
57+
/* SPI descriptor */
58+
struct no_os_spi_desc *spi_desc;
59+
};
60+
61+
/* ad5165 digital potentiometer ops */
62+
int ad5165_dpot_init(struct dpot_init_param *init_params,
63+
struct dpot_dev **desc);
64+
int ad5165_dpot_remove(struct dpot_dev *desc);
65+
66+
int ad5165_dpot_chn_read(struct dpot_dev *desc,
67+
enum dpot_chn_type chn, uint8_t *data);
68+
int ad5165_dpot_chn_write(struct dpot_dev *desc,
69+
enum dpot_chn_type chn, uint8_t data);
70+
71+
int ad5165_dpot_send_cmd(struct dpot_dev *desc,
72+
struct dpot_command *cmd);
73+
int ad5165_dpot_shutdown(struct dpot_dev *desc, enum dpot_chn_type chn,
74+
bool shutdown_enable);
75+
76+
#endif // ad525x_H_

0 commit comments

Comments
 (0)