blob: 83adcbf1a205e73f173763258c08b5cac2f9ad06 [file] [log] [blame]
Roland Stigge6ddc5fb2011-01-12 11:41:59 +01001/*
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/init.h>
23#include <linux/slab.h>
24#include <linux/jiffies.h>
25#include <linux/i2c.h>
26#include <linux/err.h>
27
Jonathan Cameron06458e22012-04-25 15:54:58 +010028#include <linux/iio/iio.h>
29#include <linux/iio/sysfs.h>
Lars-Peter Clausendbdc0252012-06-04 11:36:28 +020030#include <linux/iio/dac/max517.h>
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010031
32#define MAX517_DRV_NAME "max517"
33
34/* Commands */
35#define COMMAND_CHANNEL0 0x00
36#define COMMAND_CHANNEL1 0x01 /* for MAX518 and MAX519 */
37#define COMMAND_PD 0x08 /* Power Down */
38
39enum max517_device_ids {
40 ID_MAX517,
41 ID_MAX518,
42 ID_MAX519,
43};
44
45struct max517_data {
Roland Stigge826514b2011-01-29 16:36:46 +010046 struct i2c_client *client;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010047 unsigned short vref_mv[2];
48};
49
50/*
51 * channel: bit 0: channel 1
52 * bit 1: channel 2
53 * (this way, it's possible to set both channels at once)
54 */
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020055static int max517_set_value(struct iio_dev *indio_dev,
56 long val, int channel)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010057{
Jonathan Cameron638e59f2011-10-06 17:14:38 +010058 struct max517_data *data = iio_priv(indio_dev);
Roland Stigge826514b2011-01-29 16:36:46 +010059 struct i2c_client *client = data->client;
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020060 u8 outbuf[2];
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010061 int res;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010062
63 if (val < 0 || val > 255)
64 return -EINVAL;
65
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020066 outbuf[0] = channel;
67 outbuf[1] = val;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010068
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020069 res = i2c_master_send(client, outbuf, 2);
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010070 if (res < 0)
71 return res;
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020072 else if (res != 2)
73 return -EIO;
74 else
75 return 0;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010076}
77
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020078static int max517_read_raw(struct iio_dev *indio_dev,
79 struct iio_chan_spec const *chan,
80 int *val,
81 int *val2,
82 long m)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010083{
Jonathan Cameron638e59f2011-10-06 17:14:38 +010084 struct max517_data *data = iio_priv(indio_dev);
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020085 unsigned int scale_uv;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010086
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020087 switch (m) {
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;
94 default:
95 break;
96 }
97 return -EINVAL;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010098}
99
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200100static int max517_write_raw(struct iio_dev *indio_dev,
101 struct iio_chan_spec const *chan, int val, int val2, long mask)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100102{
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200103 int ret;
104
105 switch (mask) {
106 case IIO_CHAN_INFO_RAW:
107 ret = max517_set_value(indio_dev, val, chan->channel);
108 break;
109 default:
110 ret = -EINVAL;
111 break;
112 }
113
114 return ret;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100115}
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100116
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100117#ifdef CONFIG_PM_SLEEP
118static int max517_suspend(struct device *dev)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100119{
120 u8 outbuf = COMMAND_PD;
121
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100122 return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100123}
124
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100125static int max517_resume(struct device *dev)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100126{
127 u8 outbuf = 0;
128
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100129 return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100130}
131
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100132static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
133#define MAX517_PM_OPS (&max517_pm_ops)
134#else
135#define MAX517_PM_OPS NULL
136#endif
137
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100138static const struct iio_info max517_info = {
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200139 .read_raw = max517_read_raw,
140 .write_raw = max517_write_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100141 .driver_module = THIS_MODULE,
142};
143
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200144#define MAX517_CHANNEL(chan) { \
145 .type = IIO_VOLTAGE, \
146 .indexed = 1, \
147 .output = 1, \
148 .channel = (chan), \
Jonathan Cameron040b8372013-02-27 19:28:22 +0000149 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
150 BIT(IIO_CHAN_INFO_SCALE), \
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200151 .scan_type = IIO_ST('u', 8, 8, 0), \
152}
153
154static const struct iio_chan_spec max517_channels[] = {
155 MAX517_CHANNEL(0),
156 MAX517_CHANNEL(1)
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100157};
158
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800159static int max517_probe(struct i2c_client *client,
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100160 const struct i2c_device_id *id)
161{
162 struct max517_data *data;
Jonathan Cameroncada11f2011-06-27 13:07:35 +0100163 struct iio_dev *indio_dev;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100164 struct max517_platform_data *platform_data = client->dev.platform_data;
165 int err;
166
Sachin Kamatcc566fd2013-08-19 12:38:00 +0100167 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
168 if (!indio_dev)
169 return -ENOMEM;
Jonathan Cameroncada11f2011-06-27 13:07:35 +0100170 data = iio_priv(indio_dev);
171 i2c_set_clientdata(client, indio_dev);
Roland Stigge826514b2011-01-29 16:36:46 +0100172 data->client = client;
173
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100174 /* establish that the iio_dev is a child of the i2c device */
Jonathan Cameroncada11f2011-06-27 13:07:35 +0100175 indio_dev->dev.parent = &client->dev;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100176
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200177 /* reduced channel set for MAX517 */
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100178 if (id->driver_data == ID_MAX517)
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200179 indio_dev->num_channels = 1;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100180 else
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200181 indio_dev->num_channels = 2;
182 indio_dev->channels = max517_channels;
Jonathan Cameroncada11f2011-06-27 13:07:35 +0100183 indio_dev->modes = INDIO_DIRECT_MODE;
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200184 indio_dev->info = &max517_info;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100185
186 /*
187 * Reference voltage on MAX518 and default is 5V, else take vref_mv
188 * from platform_data
189 */
190 if (id->driver_data == ID_MAX518 || !platform_data) {
191 data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */
192 } else {
193 data->vref_mv[0] = platform_data->vref_mv[0];
194 data->vref_mv[1] = platform_data->vref_mv[1];
195 }
196
Jonathan Cameroncada11f2011-06-27 13:07:35 +0100197 err = iio_device_register(indio_dev);
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100198 if (err)
Sachin Kamatcc566fd2013-08-19 12:38:00 +0100199 return err;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100200
201 dev_info(&client->dev, "DAC registered\n");
202
203 return 0;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100204}
205
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800206static int max517_remove(struct i2c_client *client)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100207{
Peter Meerwalde8ccc112012-05-02 01:13:33 +0200208 iio_device_unregister(i2c_get_clientdata(client));
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100209 return 0;
210}
211
212static const struct i2c_device_id max517_id[] = {
213 { "max517", ID_MAX517 },
214 { "max518", ID_MAX518 },
215 { "max519", ID_MAX519 },
216 { }
217};
218MODULE_DEVICE_TABLE(i2c, max517_id);
219
220static struct i2c_driver max517_driver = {
221 .driver = {
222 .name = MAX517_DRV_NAME,
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100223 .pm = MAX517_PM_OPS,
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100224 },
225 .probe = max517_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800226 .remove = max517_remove,
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100227 .id_table = max517_id,
228};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100229module_i2c_driver(max517_driver);
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100230
231MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
232MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC");
233MODULE_LICENSE("GPL");