2 * max517.c - Support for Maxim MAX517, MAX518 and MAX519
4 * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
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.
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.
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.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/err.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/sysfs.h>
30 #include <linux/iio/dac/max517.h>
32 #define MAX517_DRV_NAME "max517"
35 #define COMMAND_CHANNEL0 0x00
36 #define COMMAND_CHANNEL1 0x01 /* for MAX518 and MAX519 */
37 #define COMMAND_PD 0x08 /* Power Down */
39 enum max517_device_ids {
46 struct i2c_client *client;
47 unsigned short vref_mv[2];
51 * channel: bit 0: channel 1
53 * (this way, it's possible to set both channels at once)
55 static int max517_set_value(struct iio_dev *indio_dev,
56 long val, int channel)
58 struct max517_data *data = iio_priv(indio_dev);
59 struct i2c_client *client = data->client;
63 if (val < 0 || val > 255)
69 res = i2c_master_send(client, outbuf, 2);
78 static int max517_read_raw(struct iio_dev *indio_dev,
79 struct iio_chan_spec const *chan,
84 struct max517_data *data = iio_priv(indio_dev);
85 unsigned int scale_uv;
88 case IIO_CHAN_INFO_SCALE:
89 /* Corresponds to Vref / 2^(bits) */
90 scale_uv = (data->vref_mv[chan->channel] * 1000) >> 8;
91 *val = scale_uv / 1000000;
92 *val2 = scale_uv % 1000000;
93 return IIO_VAL_INT_PLUS_MICRO;
100 static int max517_write_raw(struct iio_dev *indio_dev,
101 struct iio_chan_spec const *chan, int val, int val2, long mask)
106 case IIO_CHAN_INFO_RAW:
107 ret = max517_set_value(indio_dev, val, chan->channel);
117 #ifdef CONFIG_PM_SLEEP
118 static int max517_suspend(struct device *dev)
120 u8 outbuf = COMMAND_PD;
122 return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
125 static int max517_resume(struct device *dev)
129 return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
132 static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
133 #define MAX517_PM_OPS (&max517_pm_ops)
135 #define MAX517_PM_OPS NULL
138 static const struct iio_info max517_info = {
139 .read_raw = max517_read_raw,
140 .write_raw = max517_write_raw,
141 .driver_module = THIS_MODULE,
144 #define MAX517_CHANNEL(chan) { \
145 .type = IIO_VOLTAGE, \
149 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
150 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
151 .scan_type = IIO_ST('u', 8, 8, 0), \
154 static const struct iio_chan_spec max517_channels[] = {
159 static int __devinit max517_probe(struct i2c_client *client,
160 const struct i2c_device_id *id)
162 struct max517_data *data;
163 struct iio_dev *indio_dev;
164 struct max517_platform_data *platform_data = client->dev.platform_data;
167 indio_dev = iio_device_alloc(sizeof(*data));
168 if (indio_dev == NULL) {
172 data = iio_priv(indio_dev);
173 i2c_set_clientdata(client, indio_dev);
174 data->client = client;
176 /* establish that the iio_dev is a child of the i2c device */
177 indio_dev->dev.parent = &client->dev;
179 /* reduced channel set for MAX517 */
180 if (id->driver_data == ID_MAX517)
181 indio_dev->num_channels = 1;
183 indio_dev->num_channels = 2;
184 indio_dev->channels = max517_channels;
185 indio_dev->modes = INDIO_DIRECT_MODE;
186 indio_dev->info = &max517_info;
189 * Reference voltage on MAX518 and default is 5V, else take vref_mv
192 if (id->driver_data == ID_MAX518 || !platform_data) {
193 data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */
195 data->vref_mv[0] = platform_data->vref_mv[0];
196 data->vref_mv[1] = platform_data->vref_mv[1];
199 err = iio_device_register(indio_dev);
201 goto exit_free_device;
203 dev_info(&client->dev, "DAC registered\n");
208 iio_device_free(indio_dev);
213 static int __devexit max517_remove(struct i2c_client *client)
215 iio_device_unregister(i2c_get_clientdata(client));
216 iio_device_free(i2c_get_clientdata(client));
221 static const struct i2c_device_id max517_id[] = {
222 { "max517", ID_MAX517 },
223 { "max518", ID_MAX518 },
224 { "max519", ID_MAX519 },
227 MODULE_DEVICE_TABLE(i2c, max517_id);
229 static struct i2c_driver max517_driver = {
231 .name = MAX517_DRV_NAME,
234 .probe = max517_probe,
235 .remove = __devexit_p(max517_remove),
236 .id_table = max517_id,
238 module_i2c_driver(max517_driver);
240 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
241 MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC");
242 MODULE_LICENSE("GPL");