Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)   * iio/dac/max5821.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)   * Copyright (C) 2014 Philippe Reynes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)   */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define MAX5821_MAX_DAC_CHANNELS		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /* command bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define MAX5821_LOAD_DAC_A_IN_REG_B		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define MAX5821_LOAD_DAC_B_IN_REG_A		0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define MAX5821_EXTENDED_COMMAND_MODE		0xf0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define MAX5821_READ_DAC_A_COMMAND		0xf1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define MAX5821_READ_DAC_B_COMMAND		0xf2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define MAX5821_EXTENDED_POWER_UP		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define MAX5821_EXTENDED_POWER_DOWN_MODE0	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define MAX5821_EXTENDED_POWER_DOWN_MODE1	0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define MAX5821_EXTENDED_POWER_DOWN_MODE2	0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define MAX5821_EXTENDED_DAC_A			0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define MAX5821_EXTENDED_DAC_B			0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) enum max5821_device_ids {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	ID_MAX5821,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) struct max5821_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct i2c_client	*client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct regulator	*vref_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	unsigned short		vref_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	bool			powerdown[MAX5821_MAX_DAC_CHANNELS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	u8			powerdown_mode[MAX5821_MAX_DAC_CHANNELS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct mutex		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static const char * const max5821_powerdown_modes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	"three_state",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	"1kohm_to_gnd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	"100kohm_to_gnd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	MAX5821_THREE_STATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	MAX5821_1KOHM_TO_GND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	MAX5821_100KOHM_TO_GND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static int max5821_get_powerdown_mode(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 				      const struct iio_chan_spec *chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct max5821_data *st = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return st->powerdown_mode[chan->channel];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static int max5821_set_powerdown_mode(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 				      const struct iio_chan_spec *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 				      unsigned int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct max5821_data *st = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	st->powerdown_mode[chan->channel] = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static const struct iio_enum max5821_powerdown_mode_enum = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	.items = max5821_powerdown_modes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	.num_items = ARRAY_SIZE(max5821_powerdown_modes),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	.get = max5821_get_powerdown_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	.set = max5821_set_powerdown_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static ssize_t max5821_read_dac_powerdown(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 					  uintptr_t private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 					  const struct iio_chan_spec *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 					  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct max5821_data *st = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return sprintf(buf, "%d\n", st->powerdown[chan->channel]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static int max5821_sync_powerdown_mode(struct max5821_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 				       const struct iio_chan_spec *chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	u8 outbuf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	outbuf[0] = MAX5821_EXTENDED_COMMAND_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (chan->channel == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		outbuf[1] = MAX5821_EXTENDED_DAC_A;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		outbuf[1] = MAX5821_EXTENDED_DAC_B;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (data->powerdown[chan->channel])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		outbuf[1] |= data->powerdown_mode[chan->channel] + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		outbuf[1] |= MAX5821_EXTENDED_POWER_UP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return i2c_master_send(data->client, outbuf, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static ssize_t max5821_write_dac_powerdown(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 					   uintptr_t private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 					   const struct iio_chan_spec *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 					   const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct max5821_data *data = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	bool powerdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	ret = strtobool(buf, &powerdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	data->powerdown[chan->channel] = powerdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	ret = max5821_sync_powerdown_mode(data, chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static const struct iio_chan_spec_ext_info max5821_ext_info[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		.name = "powerdown",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		.read = max5821_read_dac_powerdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		.write = max5821_write_dac_powerdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		.shared = IIO_SEPARATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	IIO_ENUM("powerdown_mode", IIO_SEPARATE, &max5821_powerdown_mode_enum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	IIO_ENUM_AVAILABLE("powerdown_mode", &max5821_powerdown_mode_enum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #define MAX5821_CHANNEL(chan) {					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	.type = IIO_VOLTAGE,					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	.indexed = 1,						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	.output = 1,						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	.channel = (chan),					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE),	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	.ext_info = max5821_ext_info,				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static const struct iio_chan_spec max5821_channels[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	MAX5821_CHANNEL(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	MAX5821_CHANNEL(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static const u8 max5821_read_dac_command[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	MAX5821_READ_DAC_A_COMMAND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	MAX5821_READ_DAC_B_COMMAND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static const u8 max5821_load_dac_command[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	MAX5821_LOAD_DAC_A_IN_REG_B,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	MAX5821_LOAD_DAC_B_IN_REG_A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int max5821_get_value(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			     int *val, int channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct max5821_data *data = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	u8 outbuf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	u8 inbuf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if ((channel != 0) && (channel != 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	outbuf[0] = max5821_read_dac_command[channel];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	ret = i2c_master_send(client, outbuf, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	} else if (ret != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	ret = i2c_master_recv(client, inbuf, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	} else if (ret != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	*val = ((inbuf[0] & 0x0f) << 6) | (inbuf[1] >> 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static int max5821_set_value(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			     int val, int channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	struct max5821_data *data = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	u8 outbuf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if ((val < 0) || (val > 1023))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if ((channel != 0) && (channel != 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	outbuf[0] = max5821_load_dac_command[channel];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	outbuf[0] |= val >> 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	outbuf[1] = (val & 0x3f) << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	ret = i2c_master_send(client, outbuf, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	else if (ret != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static int max5821_read_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			   struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			   int *val, int *val2, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct max5821_data *data = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	case IIO_CHAN_INFO_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return max5821_get_value(indio_dev, val, chan->channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	case IIO_CHAN_INFO_SCALE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		*val = data->vref_mv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		*val2 = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		return IIO_VAL_FRACTIONAL_LOG2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static int max5821_write_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			     struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			     int val, int val2, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (val2 != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	case IIO_CHAN_INFO_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return max5821_set_value(indio_dev, val, chan->channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static int __maybe_unused max5821_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			 MAX5821_EXTENDED_DAC_A |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			 MAX5821_EXTENDED_DAC_B |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			 MAX5821_EXTENDED_POWER_DOWN_MODE2 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	return i2c_master_send(to_i2c_client(dev), outbuf, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static int __maybe_unused max5821_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			 MAX5821_EXTENDED_DAC_A |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			 MAX5821_EXTENDED_DAC_B |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			 MAX5821_EXTENDED_POWER_UP };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	return i2c_master_send(to_i2c_client(dev), outbuf, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static SIMPLE_DEV_PM_OPS(max5821_pm_ops, max5821_suspend, max5821_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static const struct iio_info max5821_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	.read_raw = max5821_read_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	.write_raw = max5821_write_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static int max5821_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	struct max5821_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	u32 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (!indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	data = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	i2c_set_clientdata(client, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	mutex_init(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	/* max5821 start in powerdown mode 100Kohm to ground */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	for (tmp = 0; tmp < MAX5821_MAX_DAC_CHANNELS; tmp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		data->powerdown[tmp] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		data->powerdown_mode[tmp] = MAX5821_100KOHM_TO_GND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	data->vref_reg = devm_regulator_get(&client->dev, "vref");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (IS_ERR(data->vref_reg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		ret = PTR_ERR(data->vref_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			"Failed to get vref regulator: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		goto error_free_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	ret = regulator_enable(data->vref_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			"Failed to enable vref regulator: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		goto error_free_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	ret = regulator_get_voltage(data->vref_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			"Failed to get voltage on regulator: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		goto error_disable_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	data->vref_mv = ret / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	indio_dev->name = id->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	indio_dev->num_channels = ARRAY_SIZE(max5821_channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	indio_dev->channels = max5821_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	indio_dev->modes = INDIO_DIRECT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	indio_dev->info = &max5821_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	return iio_device_register(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) error_disable_reg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	regulator_disable(data->vref_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) error_free_reg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static int max5821_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	struct max5821_data *data = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	iio_device_unregister(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	regulator_disable(data->vref_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static const struct i2c_device_id max5821_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	{ "max5821", ID_MAX5821 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) MODULE_DEVICE_TABLE(i2c, max5821_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static const struct of_device_id max5821_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	{ .compatible = "maxim,max5821" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) MODULE_DEVICE_TABLE(of, max5821_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static struct i2c_driver max5821_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		.name	= "max5821",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		.of_match_table = max5821_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		.pm     = &max5821_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	.probe		= max5821_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	.remove		= max5821_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	.id_table	= max5821_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) module_i2c_driver(max5821_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) MODULE_AUTHOR("Philippe Reynes <tremyfr@yahoo.fr>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) MODULE_DESCRIPTION("MAX5821 DAC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) MODULE_LICENSE("GPL v2");