18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for the Analog Devices digital potentiometers
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2010 Michael Hennerich, Analog Devices Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _AD_DPOT_H_
98c2ecf20Sopenharmony_ci#define _AD_DPOT_H_
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/types.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define DPOT_CONF(features, wipers, max_pos, uid) \
148c2ecf20Sopenharmony_ci		(((features) << 18) | (((wipers) & 0xFF) << 10) | \
158c2ecf20Sopenharmony_ci		((max_pos & 0xF) << 6) | (uid & 0x3F))
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define DPOT_UID(conf)		(conf & 0x3F)
188c2ecf20Sopenharmony_ci#define DPOT_MAX_POS(conf)	((conf >> 6) & 0xF)
198c2ecf20Sopenharmony_ci#define DPOT_WIPERS(conf)	((conf >> 10) & 0xFF)
208c2ecf20Sopenharmony_ci#define DPOT_FEAT(conf)		(conf >> 18)
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define BRDAC0			(1 << 0)
238c2ecf20Sopenharmony_ci#define BRDAC1			(1 << 1)
248c2ecf20Sopenharmony_ci#define BRDAC2			(1 << 2)
258c2ecf20Sopenharmony_ci#define BRDAC3			(1 << 3)
268c2ecf20Sopenharmony_ci#define BRDAC4			(1 << 4)
278c2ecf20Sopenharmony_ci#define BRDAC5			(1 << 5)
288c2ecf20Sopenharmony_ci#define MAX_RDACS		6
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define F_CMD_INC		(1 << 0)	/* Features INC/DEC ALL, 6dB */
318c2ecf20Sopenharmony_ci#define F_CMD_EEP		(1 << 1)	/* Features EEPROM */
328c2ecf20Sopenharmony_ci#define F_CMD_OTP		(1 << 2)	/* Features OTP */
338c2ecf20Sopenharmony_ci#define F_CMD_TOL		(1 << 3)	/* RDACS feature Tolerance REG */
348c2ecf20Sopenharmony_ci#define F_RDACS_RW		(1 << 4)	/* RDACS are Read/Write  */
358c2ecf20Sopenharmony_ci#define F_RDACS_WONLY		(1 << 5)	/* RDACS are Write only */
368c2ecf20Sopenharmony_ci#define F_AD_APPDATA		(1 << 6)	/* RDAC Address append to data */
378c2ecf20Sopenharmony_ci#define F_SPI_8BIT		(1 << 7)	/* All SPI XFERS are 8-bit */
388c2ecf20Sopenharmony_ci#define F_SPI_16BIT		(1 << 8)	/* All SPI XFERS are 16-bit */
398c2ecf20Sopenharmony_ci#define F_SPI_24BIT		(1 << 9)	/* All SPI XFERS are 24-bit */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define F_RDACS_RW_TOL	(F_RDACS_RW | F_CMD_EEP | F_CMD_TOL)
428c2ecf20Sopenharmony_ci#define F_RDACS_RW_EEP	(F_RDACS_RW | F_CMD_EEP)
438c2ecf20Sopenharmony_ci#define F_SPI		(F_SPI_8BIT | F_SPI_16BIT | F_SPI_24BIT)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cienum dpot_devid {
468c2ecf20Sopenharmony_ci	AD5258_ID = DPOT_CONF(F_RDACS_RW_TOL, BRDAC0, 6, 0), /* I2C */
478c2ecf20Sopenharmony_ci	AD5259_ID = DPOT_CONF(F_RDACS_RW_TOL, BRDAC0, 8, 1),
488c2ecf20Sopenharmony_ci	AD5251_ID = DPOT_CONF(F_RDACS_RW_TOL | F_CMD_INC,
498c2ecf20Sopenharmony_ci			BRDAC1 | BRDAC3, 6, 2),
508c2ecf20Sopenharmony_ci	AD5252_ID = DPOT_CONF(F_RDACS_RW_TOL | F_CMD_INC,
518c2ecf20Sopenharmony_ci			BRDAC1 | BRDAC3, 8, 3),
528c2ecf20Sopenharmony_ci	AD5253_ID = DPOT_CONF(F_RDACS_RW_TOL | F_CMD_INC,
538c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1 | BRDAC2 | BRDAC3, 6, 4),
548c2ecf20Sopenharmony_ci	AD5254_ID = DPOT_CONF(F_RDACS_RW_TOL | F_CMD_INC,
558c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1 | BRDAC2 | BRDAC3, 8, 5),
568c2ecf20Sopenharmony_ci	AD5255_ID = DPOT_CONF(F_RDACS_RW_TOL | F_CMD_INC,
578c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1 | BRDAC2, 9, 6),
588c2ecf20Sopenharmony_ci	AD5160_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_8BIT,
598c2ecf20Sopenharmony_ci			BRDAC0, 8, 7), /* SPI */
608c2ecf20Sopenharmony_ci	AD5161_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_8BIT,
618c2ecf20Sopenharmony_ci			BRDAC0, 8, 8),
628c2ecf20Sopenharmony_ci	AD5162_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_16BIT,
638c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1, 8, 9),
648c2ecf20Sopenharmony_ci	AD5165_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_8BIT,
658c2ecf20Sopenharmony_ci			BRDAC0, 8, 10),
668c2ecf20Sopenharmony_ci	AD5200_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_8BIT,
678c2ecf20Sopenharmony_ci			BRDAC0, 8, 11),
688c2ecf20Sopenharmony_ci	AD5201_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_8BIT,
698c2ecf20Sopenharmony_ci			BRDAC0, 5, 12),
708c2ecf20Sopenharmony_ci	AD5203_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_8BIT,
718c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1 | BRDAC2 | BRDAC3, 6, 13),
728c2ecf20Sopenharmony_ci	AD5204_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_16BIT,
738c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1 | BRDAC2 | BRDAC3, 8, 14),
748c2ecf20Sopenharmony_ci	AD5206_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_16BIT,
758c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1 | BRDAC2 | BRDAC3 | BRDAC4 | BRDAC5,
768c2ecf20Sopenharmony_ci			8, 15),
778c2ecf20Sopenharmony_ci	AD5207_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_16BIT,
788c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1, 8, 16),
798c2ecf20Sopenharmony_ci	AD5231_ID = DPOT_CONF(F_RDACS_RW_EEP | F_CMD_INC | F_SPI_24BIT,
808c2ecf20Sopenharmony_ci			BRDAC0, 10, 17),
818c2ecf20Sopenharmony_ci	AD5232_ID = DPOT_CONF(F_RDACS_RW_EEP | F_CMD_INC | F_SPI_16BIT,
828c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1, 8, 18),
838c2ecf20Sopenharmony_ci	AD5233_ID = DPOT_CONF(F_RDACS_RW_EEP | F_CMD_INC | F_SPI_16BIT,
848c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1 | BRDAC2 | BRDAC3, 6, 19),
858c2ecf20Sopenharmony_ci	AD5235_ID = DPOT_CONF(F_RDACS_RW_EEP | F_CMD_INC | F_SPI_24BIT,
868c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1, 10, 20),
878c2ecf20Sopenharmony_ci	AD5260_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_8BIT,
888c2ecf20Sopenharmony_ci			BRDAC0, 8, 21),
898c2ecf20Sopenharmony_ci	AD5262_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_16BIT,
908c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1, 8, 22),
918c2ecf20Sopenharmony_ci	AD5263_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_16BIT,
928c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1 | BRDAC2 | BRDAC3, 8, 23),
938c2ecf20Sopenharmony_ci	AD5290_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_8BIT,
948c2ecf20Sopenharmony_ci			BRDAC0, 8, 24),
958c2ecf20Sopenharmony_ci	AD5291_ID = DPOT_CONF(F_RDACS_RW | F_SPI_16BIT | F_CMD_OTP,
968c2ecf20Sopenharmony_ci			BRDAC0, 8, 25),
978c2ecf20Sopenharmony_ci	AD5292_ID = DPOT_CONF(F_RDACS_RW | F_SPI_16BIT | F_CMD_OTP,
988c2ecf20Sopenharmony_ci			BRDAC0, 10, 26),
998c2ecf20Sopenharmony_ci	AD5293_ID = DPOT_CONF(F_RDACS_RW | F_SPI_16BIT, BRDAC0, 10, 27),
1008c2ecf20Sopenharmony_ci	AD7376_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_8BIT,
1018c2ecf20Sopenharmony_ci			BRDAC0, 7, 28),
1028c2ecf20Sopenharmony_ci	AD8400_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_16BIT,
1038c2ecf20Sopenharmony_ci			BRDAC0, 8, 29),
1048c2ecf20Sopenharmony_ci	AD8402_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_16BIT,
1058c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1, 8, 30),
1068c2ecf20Sopenharmony_ci	AD8403_ID = DPOT_CONF(F_RDACS_WONLY | F_AD_APPDATA | F_SPI_16BIT,
1078c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1 | BRDAC2, 8, 31),
1088c2ecf20Sopenharmony_ci	ADN2850_ID = DPOT_CONF(F_RDACS_RW_EEP | F_CMD_INC | F_SPI_24BIT,
1098c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1, 10, 32),
1108c2ecf20Sopenharmony_ci	AD5241_ID = DPOT_CONF(F_RDACS_RW, BRDAC0, 8, 33),
1118c2ecf20Sopenharmony_ci	AD5242_ID = DPOT_CONF(F_RDACS_RW, BRDAC0 | BRDAC1, 8, 34),
1128c2ecf20Sopenharmony_ci	AD5243_ID = DPOT_CONF(F_RDACS_RW, BRDAC0 | BRDAC1, 8, 35),
1138c2ecf20Sopenharmony_ci	AD5245_ID = DPOT_CONF(F_RDACS_RW, BRDAC0, 8, 36),
1148c2ecf20Sopenharmony_ci	AD5246_ID = DPOT_CONF(F_RDACS_RW, BRDAC0, 7, 37),
1158c2ecf20Sopenharmony_ci	AD5247_ID = DPOT_CONF(F_RDACS_RW, BRDAC0, 7, 38),
1168c2ecf20Sopenharmony_ci	AD5248_ID = DPOT_CONF(F_RDACS_RW, BRDAC0 | BRDAC1, 8, 39),
1178c2ecf20Sopenharmony_ci	AD5280_ID = DPOT_CONF(F_RDACS_RW, BRDAC0, 8, 40),
1188c2ecf20Sopenharmony_ci	AD5282_ID = DPOT_CONF(F_RDACS_RW, BRDAC0 | BRDAC1, 8, 41),
1198c2ecf20Sopenharmony_ci	ADN2860_ID = DPOT_CONF(F_RDACS_RW_TOL | F_CMD_INC,
1208c2ecf20Sopenharmony_ci			BRDAC0 | BRDAC1 | BRDAC2, 9, 42),
1218c2ecf20Sopenharmony_ci	AD5273_ID = DPOT_CONF(F_RDACS_RW | F_CMD_OTP, BRDAC0, 6, 43),
1228c2ecf20Sopenharmony_ci	AD5171_ID = DPOT_CONF(F_RDACS_RW | F_CMD_OTP, BRDAC0, 6, 44),
1238c2ecf20Sopenharmony_ci	AD5170_ID = DPOT_CONF(F_RDACS_RW | F_CMD_OTP, BRDAC0, 8, 45),
1248c2ecf20Sopenharmony_ci	AD5172_ID = DPOT_CONF(F_RDACS_RW | F_CMD_OTP, BRDAC0 | BRDAC1, 8, 46),
1258c2ecf20Sopenharmony_ci	AD5173_ID = DPOT_CONF(F_RDACS_RW | F_CMD_OTP, BRDAC0 | BRDAC1, 8, 47),
1268c2ecf20Sopenharmony_ci	AD5270_ID = DPOT_CONF(F_RDACS_RW | F_CMD_OTP | F_SPI_16BIT,
1278c2ecf20Sopenharmony_ci			BRDAC0, 10, 48),
1288c2ecf20Sopenharmony_ci	AD5271_ID = DPOT_CONF(F_RDACS_RW | F_CMD_OTP | F_SPI_16BIT,
1298c2ecf20Sopenharmony_ci			BRDAC0, 8, 49),
1308c2ecf20Sopenharmony_ci	AD5272_ID = DPOT_CONF(F_RDACS_RW | F_CMD_OTP, BRDAC0, 10, 50),
1318c2ecf20Sopenharmony_ci	AD5274_ID = DPOT_CONF(F_RDACS_RW | F_CMD_OTP, BRDAC0, 8, 51),
1328c2ecf20Sopenharmony_ci};
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#define DPOT_RDAC0		0
1358c2ecf20Sopenharmony_ci#define DPOT_RDAC1		1
1368c2ecf20Sopenharmony_ci#define DPOT_RDAC2		2
1378c2ecf20Sopenharmony_ci#define DPOT_RDAC3		3
1388c2ecf20Sopenharmony_ci#define DPOT_RDAC4		4
1398c2ecf20Sopenharmony_ci#define DPOT_RDAC5		5
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#define DPOT_RDAC_MASK		0x1F
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#define DPOT_REG_TOL		0x18
1448c2ecf20Sopenharmony_ci#define DPOT_TOL_RDAC0		(DPOT_REG_TOL | DPOT_RDAC0)
1458c2ecf20Sopenharmony_ci#define DPOT_TOL_RDAC1		(DPOT_REG_TOL | DPOT_RDAC1)
1468c2ecf20Sopenharmony_ci#define DPOT_TOL_RDAC2		(DPOT_REG_TOL | DPOT_RDAC2)
1478c2ecf20Sopenharmony_ci#define DPOT_TOL_RDAC3		(DPOT_REG_TOL | DPOT_RDAC3)
1488c2ecf20Sopenharmony_ci#define DPOT_TOL_RDAC4		(DPOT_REG_TOL | DPOT_RDAC4)
1498c2ecf20Sopenharmony_ci#define DPOT_TOL_RDAC5		(DPOT_REG_TOL | DPOT_RDAC5)
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/* RDAC-to-EEPROM Interface Commands */
1528c2ecf20Sopenharmony_ci#define DPOT_ADDR_RDAC		(0x0 << 5)
1538c2ecf20Sopenharmony_ci#define DPOT_ADDR_EEPROM	(0x1 << 5)
1548c2ecf20Sopenharmony_ci#define DPOT_ADDR_OTP		(0x1 << 6)
1558c2ecf20Sopenharmony_ci#define DPOT_ADDR_CMD		(0x1 << 7)
1568c2ecf20Sopenharmony_ci#define DPOT_ADDR_OTP_EN	(0x1 << 9)
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci#define DPOT_DEC_ALL_6DB	(DPOT_ADDR_CMD | (0x4 << 3))
1598c2ecf20Sopenharmony_ci#define DPOT_INC_ALL_6DB	(DPOT_ADDR_CMD | (0x9 << 3))
1608c2ecf20Sopenharmony_ci#define DPOT_DEC_ALL		(DPOT_ADDR_CMD | (0x6 << 3))
1618c2ecf20Sopenharmony_ci#define DPOT_INC_ALL		(DPOT_ADDR_CMD | (0xB << 3))
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci#define DPOT_SPI_RDAC		0xB0
1648c2ecf20Sopenharmony_ci#define DPOT_SPI_EEPROM		0x30
1658c2ecf20Sopenharmony_ci#define DPOT_SPI_READ_RDAC	0xA0
1668c2ecf20Sopenharmony_ci#define DPOT_SPI_READ_EEPROM	0x90
1678c2ecf20Sopenharmony_ci#define DPOT_SPI_DEC_ALL_6DB	0x50
1688c2ecf20Sopenharmony_ci#define DPOT_SPI_INC_ALL_6DB	0xD0
1698c2ecf20Sopenharmony_ci#define DPOT_SPI_DEC_ALL	0x70
1708c2ecf20Sopenharmony_ci#define DPOT_SPI_INC_ALL	0xF0
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/* AD5291/2/3 use special commands */
1738c2ecf20Sopenharmony_ci#define DPOT_AD5291_RDAC	0x01
1748c2ecf20Sopenharmony_ci#define DPOT_AD5291_READ_RDAC	0x02
1758c2ecf20Sopenharmony_ci#define DPOT_AD5291_STORE_XTPM	0x03
1768c2ecf20Sopenharmony_ci#define DPOT_AD5291_CTRLREG	0x06
1778c2ecf20Sopenharmony_ci#define DPOT_AD5291_UNLOCK_CMD	0x03
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/* AD5270/1/2/4 use special commands */
1808c2ecf20Sopenharmony_ci#define DPOT_AD5270_1_2_4_RDAC		0x01
1818c2ecf20Sopenharmony_ci#define DPOT_AD5270_1_2_4_READ_RDAC	0x02
1828c2ecf20Sopenharmony_ci#define DPOT_AD5270_1_2_4_STORE_XTPM	0x03
1838c2ecf20Sopenharmony_ci#define DPOT_AD5270_1_2_4_CTRLREG	0x07
1848c2ecf20Sopenharmony_ci#define DPOT_AD5270_1_2_4_UNLOCK_CMD	0x03
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci#define DPOT_AD5282_RDAC_AB	0x80
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci#define DPOT_AD5273_FUSE	0x80
1898c2ecf20Sopenharmony_ci#define DPOT_AD5170_2_3_FUSE	0x20
1908c2ecf20Sopenharmony_ci#define DPOT_AD5170_2_3_OW	0x08
1918c2ecf20Sopenharmony_ci#define DPOT_AD5172_3_A0	0x08
1928c2ecf20Sopenharmony_ci#define DPOT_AD5170_2FUSE	0x80
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistruct dpot_data;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistruct ad_dpot_bus_ops {
1978c2ecf20Sopenharmony_ci	int (*read_d8)(void *client);
1988c2ecf20Sopenharmony_ci	int (*read_r8d8)(void *client, u8 reg);
1998c2ecf20Sopenharmony_ci	int (*read_r8d16)(void *client, u8 reg);
2008c2ecf20Sopenharmony_ci	int (*write_d8)(void *client, u8 val);
2018c2ecf20Sopenharmony_ci	int (*write_r8d8)(void *client, u8 reg, u8 val);
2028c2ecf20Sopenharmony_ci	int (*write_r8d16)(void *client, u8 reg, u16 val);
2038c2ecf20Sopenharmony_ci};
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistruct ad_dpot_bus_data {
2068c2ecf20Sopenharmony_ci	void *client;
2078c2ecf20Sopenharmony_ci	const struct ad_dpot_bus_ops *bops;
2088c2ecf20Sopenharmony_ci};
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ciint ad_dpot_probe(struct device *dev, struct ad_dpot_bus_data *bdata,
2118c2ecf20Sopenharmony_ci		  unsigned long devid, const char *name);
2128c2ecf20Sopenharmony_ciint ad_dpot_remove(struct device *dev);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci#endif
215