xref: /openbmc/linux/drivers/iio/dac/max517.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2dbdc025bSLars-Peter Clausen /*
3dbdc025bSLars-Peter Clausen  *  max517.c - Support for Maxim MAX517, MAX518 and MAX519
4dbdc025bSLars-Peter Clausen  *
5dbdc025bSLars-Peter Clausen  *  Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
6dbdc025bSLars-Peter Clausen  */
7dbdc025bSLars-Peter Clausen 
8dbdc025bSLars-Peter Clausen #include <linux/module.h>
9dbdc025bSLars-Peter Clausen #include <linux/slab.h>
10dbdc025bSLars-Peter Clausen #include <linux/jiffies.h>
11dbdc025bSLars-Peter Clausen #include <linux/i2c.h>
12dbdc025bSLars-Peter Clausen #include <linux/err.h>
13dbdc025bSLars-Peter Clausen 
14dbdc025bSLars-Peter Clausen #include <linux/iio/iio.h>
15dbdc025bSLars-Peter Clausen #include <linux/iio/sysfs.h>
16dbdc025bSLars-Peter Clausen #include <linux/iio/dac/max517.h>
17dbdc025bSLars-Peter Clausen 
18dbdc025bSLars-Peter Clausen #define MAX517_DRV_NAME	"max517"
19dbdc025bSLars-Peter Clausen 
20dbdc025bSLars-Peter Clausen /* Commands */
21dbdc025bSLars-Peter Clausen #define COMMAND_CHANNEL0	0x00
22dbdc025bSLars-Peter Clausen #define COMMAND_CHANNEL1	0x01 /* for MAX518 and MAX519 */
23dbdc025bSLars-Peter Clausen #define COMMAND_PD		0x08 /* Power Down */
24dbdc025bSLars-Peter Clausen 
25dbdc025bSLars-Peter Clausen enum max517_device_ids {
26dbdc025bSLars-Peter Clausen 	ID_MAX517,
27dbdc025bSLars-Peter Clausen 	ID_MAX518,
28dbdc025bSLars-Peter Clausen 	ID_MAX519,
29a878a1a6SAntonio Fiol 	ID_MAX520,
30a878a1a6SAntonio Fiol 	ID_MAX521,
31dbdc025bSLars-Peter Clausen };
32dbdc025bSLars-Peter Clausen 
33dbdc025bSLars-Peter Clausen struct max517_data {
34dbdc025bSLars-Peter Clausen 	struct i2c_client	*client;
35a878a1a6SAntonio Fiol 	unsigned short		vref_mv[8];
36dbdc025bSLars-Peter Clausen };
37dbdc025bSLars-Peter Clausen 
38dbdc025bSLars-Peter Clausen /*
39dbdc025bSLars-Peter Clausen  * channel: bit 0: channel 1
40dbdc025bSLars-Peter Clausen  *          bit 1: channel 2
41dbdc025bSLars-Peter Clausen  * (this way, it's possible to set both channels at once)
42dbdc025bSLars-Peter Clausen  */
max517_set_value(struct iio_dev * indio_dev,long val,int channel)43dbdc025bSLars-Peter Clausen static int max517_set_value(struct iio_dev *indio_dev,
44dbdc025bSLars-Peter Clausen 	long val, int channel)
45dbdc025bSLars-Peter Clausen {
46dbdc025bSLars-Peter Clausen 	struct max517_data *data = iio_priv(indio_dev);
47dbdc025bSLars-Peter Clausen 	struct i2c_client *client = data->client;
48dbdc025bSLars-Peter Clausen 	u8 outbuf[2];
49dbdc025bSLars-Peter Clausen 	int res;
50dbdc025bSLars-Peter Clausen 
51dbdc025bSLars-Peter Clausen 	if (val < 0 || val > 255)
52dbdc025bSLars-Peter Clausen 		return -EINVAL;
53dbdc025bSLars-Peter Clausen 
54dbdc025bSLars-Peter Clausen 	outbuf[0] = channel;
55dbdc025bSLars-Peter Clausen 	outbuf[1] = val;
56dbdc025bSLars-Peter Clausen 
57dbdc025bSLars-Peter Clausen 	res = i2c_master_send(client, outbuf, 2);
58dbdc025bSLars-Peter Clausen 	if (res < 0)
59dbdc025bSLars-Peter Clausen 		return res;
60dbdc025bSLars-Peter Clausen 	else if (res != 2)
61dbdc025bSLars-Peter Clausen 		return -EIO;
62dbdc025bSLars-Peter Clausen 	else
63dbdc025bSLars-Peter Clausen 		return 0;
64dbdc025bSLars-Peter Clausen }
65dbdc025bSLars-Peter Clausen 
max517_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)66dbdc025bSLars-Peter Clausen static int max517_read_raw(struct iio_dev *indio_dev,
67dbdc025bSLars-Peter Clausen 			   struct iio_chan_spec const *chan,
68dbdc025bSLars-Peter Clausen 			   int *val,
69dbdc025bSLars-Peter Clausen 			   int *val2,
70dbdc025bSLars-Peter Clausen 			   long m)
71dbdc025bSLars-Peter Clausen {
72dbdc025bSLars-Peter Clausen 	struct max517_data *data = iio_priv(indio_dev);
73dbdc025bSLars-Peter Clausen 
74dbdc025bSLars-Peter Clausen 	switch (m) {
75dbdc025bSLars-Peter Clausen 	case IIO_CHAN_INFO_SCALE:
76dbdc025bSLars-Peter Clausen 		/* Corresponds to Vref / 2^(bits) */
77998f129cSLars-Peter Clausen 		*val = data->vref_mv[chan->channel];
78998f129cSLars-Peter Clausen 		*val2 = 8;
79998f129cSLars-Peter Clausen 		return IIO_VAL_FRACTIONAL_LOG2;
80dbdc025bSLars-Peter Clausen 	default:
81dbdc025bSLars-Peter Clausen 		break;
82dbdc025bSLars-Peter Clausen 	}
83dbdc025bSLars-Peter Clausen 	return -EINVAL;
84dbdc025bSLars-Peter Clausen }
85dbdc025bSLars-Peter Clausen 
max517_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)86dbdc025bSLars-Peter Clausen static int max517_write_raw(struct iio_dev *indio_dev,
87dbdc025bSLars-Peter Clausen 	struct iio_chan_spec const *chan, int val, int val2, long mask)
88dbdc025bSLars-Peter Clausen {
89dbdc025bSLars-Peter Clausen 	int ret;
90dbdc025bSLars-Peter Clausen 
91dbdc025bSLars-Peter Clausen 	switch (mask) {
92dbdc025bSLars-Peter Clausen 	case IIO_CHAN_INFO_RAW:
93dbdc025bSLars-Peter Clausen 		ret = max517_set_value(indio_dev, val, chan->channel);
94dbdc025bSLars-Peter Clausen 		break;
95dbdc025bSLars-Peter Clausen 	default:
96dbdc025bSLars-Peter Clausen 		ret = -EINVAL;
97dbdc025bSLars-Peter Clausen 		break;
98dbdc025bSLars-Peter Clausen 	}
99dbdc025bSLars-Peter Clausen 
100dbdc025bSLars-Peter Clausen 	return ret;
101dbdc025bSLars-Peter Clausen }
102dbdc025bSLars-Peter Clausen 
max517_suspend(struct device * dev)1037b9d3e85SJonathan Cameron static int max517_suspend(struct device *dev)
104dbdc025bSLars-Peter Clausen {
105dbdc025bSLars-Peter Clausen 	u8 outbuf = COMMAND_PD;
106dbdc025bSLars-Peter Clausen 
107dbdc025bSLars-Peter Clausen 	return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
108dbdc025bSLars-Peter Clausen }
109dbdc025bSLars-Peter Clausen 
max517_resume(struct device * dev)1107b9d3e85SJonathan Cameron static int max517_resume(struct device *dev)
111dbdc025bSLars-Peter Clausen {
112dbdc025bSLars-Peter Clausen 	u8 outbuf = 0;
113dbdc025bSLars-Peter Clausen 
114dbdc025bSLars-Peter Clausen 	return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
115dbdc025bSLars-Peter Clausen }
116dbdc025bSLars-Peter Clausen 
1177b9d3e85SJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
118dbdc025bSLars-Peter Clausen 
119dbdc025bSLars-Peter Clausen static const struct iio_info max517_info = {
120dbdc025bSLars-Peter Clausen 	.read_raw = max517_read_raw,
121dbdc025bSLars-Peter Clausen 	.write_raw = max517_write_raw,
122dbdc025bSLars-Peter Clausen };
123dbdc025bSLars-Peter Clausen 
124dbdc025bSLars-Peter Clausen #define MAX517_CHANNEL(chan) {				\
125dbdc025bSLars-Peter Clausen 	.type = IIO_VOLTAGE,				\
126dbdc025bSLars-Peter Clausen 	.indexed = 1,					\
127dbdc025bSLars-Peter Clausen 	.output = 1,					\
128dbdc025bSLars-Peter Clausen 	.channel = (chan),				\
129040b837eSJonathan Cameron 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
130040b837eSJonathan Cameron 	BIT(IIO_CHAN_INFO_SCALE),			\
131dbdc025bSLars-Peter Clausen }
132dbdc025bSLars-Peter Clausen 
133dbdc025bSLars-Peter Clausen static const struct iio_chan_spec max517_channels[] = {
134dbdc025bSLars-Peter Clausen 	MAX517_CHANNEL(0),
135a878a1a6SAntonio Fiol 	MAX517_CHANNEL(1),
136a878a1a6SAntonio Fiol 	MAX517_CHANNEL(2),
137a878a1a6SAntonio Fiol 	MAX517_CHANNEL(3),
138a878a1a6SAntonio Fiol 	MAX517_CHANNEL(4),
139a878a1a6SAntonio Fiol 	MAX517_CHANNEL(5),
140a878a1a6SAntonio Fiol 	MAX517_CHANNEL(6),
141a878a1a6SAntonio Fiol 	MAX517_CHANNEL(7),
142dbdc025bSLars-Peter Clausen };
143dbdc025bSLars-Peter Clausen 
max517_probe(struct i2c_client * client)144090515edSUwe Kleine-König static int max517_probe(struct i2c_client *client)
145dbdc025bSLars-Peter Clausen {
146090515edSUwe Kleine-König 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
147dbdc025bSLars-Peter Clausen 	struct max517_data *data;
148dbdc025bSLars-Peter Clausen 	struct iio_dev *indio_dev;
149dbdc025bSLars-Peter Clausen 	struct max517_platform_data *platform_data = client->dev.platform_data;
150a878a1a6SAntonio Fiol 	int chan;
151dbdc025bSLars-Peter Clausen 
152cc566fd5SSachin Kamat 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
153cc566fd5SSachin Kamat 	if (!indio_dev)
154cc566fd5SSachin Kamat 		return -ENOMEM;
155dbdc025bSLars-Peter Clausen 	data = iio_priv(indio_dev);
156dbdc025bSLars-Peter Clausen 	data->client = client;
157dbdc025bSLars-Peter Clausen 
158a878a1a6SAntonio Fiol 	switch (id->driver_data) {
159a878a1a6SAntonio Fiol 	case ID_MAX521:
160a878a1a6SAntonio Fiol 		indio_dev->num_channels = 8;
161a878a1a6SAntonio Fiol 		break;
162a878a1a6SAntonio Fiol 	case ID_MAX520:
163a878a1a6SAntonio Fiol 		indio_dev->num_channels = 4;
164a878a1a6SAntonio Fiol 		break;
165a878a1a6SAntonio Fiol 	case ID_MAX519:
166a878a1a6SAntonio Fiol 	case ID_MAX518:
167dbdc025bSLars-Peter Clausen 		indio_dev->num_channels = 2;
168a878a1a6SAntonio Fiol 		break;
169a878a1a6SAntonio Fiol 	default:  /* single channel for MAX517 */
170a878a1a6SAntonio Fiol 		indio_dev->num_channels = 1;
171a878a1a6SAntonio Fiol 		break;
172a878a1a6SAntonio Fiol 	}
173dbdc025bSLars-Peter Clausen 	indio_dev->channels = max517_channels;
174dbdc025bSLars-Peter Clausen 	indio_dev->modes = INDIO_DIRECT_MODE;
175dbdc025bSLars-Peter Clausen 	indio_dev->info = &max517_info;
176dbdc025bSLars-Peter Clausen 
177dbdc025bSLars-Peter Clausen 	/*
178dbdc025bSLars-Peter Clausen 	 * Reference voltage on MAX518 and default is 5V, else take vref_mv
179dbdc025bSLars-Peter Clausen 	 * from platform_data
180dbdc025bSLars-Peter Clausen 	 */
181a878a1a6SAntonio Fiol 	for (chan = 0; chan < indio_dev->num_channels; chan++) {
182a878a1a6SAntonio Fiol 		if (id->driver_data == ID_MAX518 || !platform_data)
183a878a1a6SAntonio Fiol 			data->vref_mv[chan] = 5000; /* mV */
184a878a1a6SAntonio Fiol 		else
185a878a1a6SAntonio Fiol 			data->vref_mv[chan] = platform_data->vref_mv[chan];
186dbdc025bSLars-Peter Clausen 	}
187dbdc025bSLars-Peter Clausen 
1882662e817SMugilraj Dhavachelvan 	return devm_iio_device_register(&client->dev, indio_dev);
189dbdc025bSLars-Peter Clausen }
190dbdc025bSLars-Peter Clausen 
191dbdc025bSLars-Peter Clausen static const struct i2c_device_id max517_id[] = {
192dbdc025bSLars-Peter Clausen 	{ "max517", ID_MAX517 },
193dbdc025bSLars-Peter Clausen 	{ "max518", ID_MAX518 },
194dbdc025bSLars-Peter Clausen 	{ "max519", ID_MAX519 },
195a878a1a6SAntonio Fiol 	{ "max520", ID_MAX520 },
196a878a1a6SAntonio Fiol 	{ "max521", ID_MAX521 },
197dbdc025bSLars-Peter Clausen 	{ }
198dbdc025bSLars-Peter Clausen };
199dbdc025bSLars-Peter Clausen MODULE_DEVICE_TABLE(i2c, max517_id);
200dbdc025bSLars-Peter Clausen 
201dbdc025bSLars-Peter Clausen static struct i2c_driver max517_driver = {
202dbdc025bSLars-Peter Clausen 	.driver = {
203dbdc025bSLars-Peter Clausen 		.name	= MAX517_DRV_NAME,
2047b9d3e85SJonathan Cameron 		.pm	= pm_sleep_ptr(&max517_pm_ops),
205dbdc025bSLars-Peter Clausen 	},
206*7cf15f42SUwe Kleine-König 	.probe		= max517_probe,
207dbdc025bSLars-Peter Clausen 	.id_table	= max517_id,
208dbdc025bSLars-Peter Clausen };
209dbdc025bSLars-Peter Clausen module_i2c_driver(max517_driver);
210dbdc025bSLars-Peter Clausen 
211dbdc025bSLars-Peter Clausen MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
212a878a1a6SAntonio Fiol MODULE_DESCRIPTION("MAX517/518/519/520/521 8-bit DAC");
213dbdc025bSLars-Peter Clausen MODULE_LICENSE("GPL");
214