xref: /openbmc/linux/drivers/iio/dac/max517.c (revision addee42a)
1 /*
2  *  max517.c - Support for Maxim MAX517, MAX518 and MAX519
3  *
4  *  Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/err.h>
26 
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/dac/max517.h>
30 
31 #define MAX517_DRV_NAME	"max517"
32 
33 /* Commands */
34 #define COMMAND_CHANNEL0	0x00
35 #define COMMAND_CHANNEL1	0x01 /* for MAX518 and MAX519 */
36 #define COMMAND_PD		0x08 /* Power Down */
37 
38 enum max517_device_ids {
39 	ID_MAX517,
40 	ID_MAX518,
41 	ID_MAX519,
42 	ID_MAX520,
43 	ID_MAX521,
44 };
45 
46 struct max517_data {
47 	struct i2c_client	*client;
48 	unsigned short		vref_mv[8];
49 };
50 
51 /*
52  * channel: bit 0: channel 1
53  *          bit 1: channel 2
54  * (this way, it's possible to set both channels at once)
55  */
56 static int max517_set_value(struct iio_dev *indio_dev,
57 	long val, int channel)
58 {
59 	struct max517_data *data = iio_priv(indio_dev);
60 	struct i2c_client *client = data->client;
61 	u8 outbuf[2];
62 	int res;
63 
64 	if (val < 0 || val > 255)
65 		return -EINVAL;
66 
67 	outbuf[0] = channel;
68 	outbuf[1] = val;
69 
70 	res = i2c_master_send(client, outbuf, 2);
71 	if (res < 0)
72 		return res;
73 	else if (res != 2)
74 		return -EIO;
75 	else
76 		return 0;
77 }
78 
79 static int max517_read_raw(struct iio_dev *indio_dev,
80 			   struct iio_chan_spec const *chan,
81 			   int *val,
82 			   int *val2,
83 			   long m)
84 {
85 	struct max517_data *data = iio_priv(indio_dev);
86 
87 	switch (m) {
88 	case IIO_CHAN_INFO_SCALE:
89 		/* Corresponds to Vref / 2^(bits) */
90 		*val = data->vref_mv[chan->channel];
91 		*val2 = 8;
92 		return IIO_VAL_FRACTIONAL_LOG2;
93 	default:
94 		break;
95 	}
96 	return -EINVAL;
97 }
98 
99 static int max517_write_raw(struct iio_dev *indio_dev,
100 	struct iio_chan_spec const *chan, int val, int val2, long mask)
101 {
102 	int ret;
103 
104 	switch (mask) {
105 	case IIO_CHAN_INFO_RAW:
106 		ret = max517_set_value(indio_dev, val, chan->channel);
107 		break;
108 	default:
109 		ret = -EINVAL;
110 		break;
111 	}
112 
113 	return ret;
114 }
115 
116 #ifdef CONFIG_PM_SLEEP
117 static int max517_suspend(struct device *dev)
118 {
119 	u8 outbuf = COMMAND_PD;
120 
121 	return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
122 }
123 
124 static int max517_resume(struct device *dev)
125 {
126 	u8 outbuf = 0;
127 
128 	return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
129 }
130 
131 static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
132 #define MAX517_PM_OPS (&max517_pm_ops)
133 #else
134 #define MAX517_PM_OPS NULL
135 #endif
136 
137 static const struct iio_info max517_info = {
138 	.read_raw = max517_read_raw,
139 	.write_raw = max517_write_raw,
140 };
141 
142 #define MAX517_CHANNEL(chan) {				\
143 	.type = IIO_VOLTAGE,				\
144 	.indexed = 1,					\
145 	.output = 1,					\
146 	.channel = (chan),				\
147 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
148 	BIT(IIO_CHAN_INFO_SCALE),			\
149 }
150 
151 static const struct iio_chan_spec max517_channels[] = {
152 	MAX517_CHANNEL(0),
153 	MAX517_CHANNEL(1),
154 	MAX517_CHANNEL(2),
155 	MAX517_CHANNEL(3),
156 	MAX517_CHANNEL(4),
157 	MAX517_CHANNEL(5),
158 	MAX517_CHANNEL(6),
159 	MAX517_CHANNEL(7),
160 };
161 
162 static int max517_probe(struct i2c_client *client,
163 			const struct i2c_device_id *id)
164 {
165 	struct max517_data *data;
166 	struct iio_dev *indio_dev;
167 	struct max517_platform_data *platform_data = client->dev.platform_data;
168 	int chan;
169 
170 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
171 	if (!indio_dev)
172 		return -ENOMEM;
173 	data = iio_priv(indio_dev);
174 	i2c_set_clientdata(client, indio_dev);
175 	data->client = client;
176 
177 	/* establish that the iio_dev is a child of the i2c device */
178 	indio_dev->dev.parent = &client->dev;
179 
180 	switch (id->driver_data) {
181 	case ID_MAX521:
182 		indio_dev->num_channels = 8;
183 		break;
184 	case ID_MAX520:
185 		indio_dev->num_channels = 4;
186 		break;
187 	case ID_MAX519:
188 	case ID_MAX518:
189 		indio_dev->num_channels = 2;
190 		break;
191 	default:  /* single channel for MAX517 */
192 		indio_dev->num_channels = 1;
193 		break;
194 	}
195 	indio_dev->channels = max517_channels;
196 	indio_dev->modes = INDIO_DIRECT_MODE;
197 	indio_dev->info = &max517_info;
198 
199 	/*
200 	 * Reference voltage on MAX518 and default is 5V, else take vref_mv
201 	 * from platform_data
202 	 */
203 	for (chan = 0; chan < indio_dev->num_channels; chan++) {
204 		if (id->driver_data == ID_MAX518 || !platform_data)
205 			data->vref_mv[chan] = 5000; /* mV */
206 		else
207 			data->vref_mv[chan] = platform_data->vref_mv[chan];
208 	}
209 
210 	return iio_device_register(indio_dev);
211 }
212 
213 static int max517_remove(struct i2c_client *client)
214 {
215 	iio_device_unregister(i2c_get_clientdata(client));
216 	return 0;
217 }
218 
219 static const struct i2c_device_id max517_id[] = {
220 	{ "max517", ID_MAX517 },
221 	{ "max518", ID_MAX518 },
222 	{ "max519", ID_MAX519 },
223 	{ "max520", ID_MAX520 },
224 	{ "max521", ID_MAX521 },
225 	{ }
226 };
227 MODULE_DEVICE_TABLE(i2c, max517_id);
228 
229 static struct i2c_driver max517_driver = {
230 	.driver = {
231 		.name	= MAX517_DRV_NAME,
232 		.pm		= MAX517_PM_OPS,
233 	},
234 	.probe		= max517_probe,
235 	.remove		= max517_remove,
236 	.id_table	= max517_id,
237 };
238 module_i2c_driver(max517_driver);
239 
240 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
241 MODULE_DESCRIPTION("MAX517/518/519/520/521 8-bit DAC");
242 MODULE_LICENSE("GPL");
243