xref: /openbmc/linux/drivers/iio/dac/max517.c (revision e8f6f3b4)
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 };
43 
44 struct max517_data {
45 	struct i2c_client	*client;
46 	unsigned short		vref_mv[2];
47 };
48 
49 /*
50  * channel: bit 0: channel 1
51  *          bit 1: channel 2
52  * (this way, it's possible to set both channels at once)
53  */
54 static int max517_set_value(struct iio_dev *indio_dev,
55 	long val, int channel)
56 {
57 	struct max517_data *data = iio_priv(indio_dev);
58 	struct i2c_client *client = data->client;
59 	u8 outbuf[2];
60 	int res;
61 
62 	if (val < 0 || val > 255)
63 		return -EINVAL;
64 
65 	outbuf[0] = channel;
66 	outbuf[1] = val;
67 
68 	res = i2c_master_send(client, outbuf, 2);
69 	if (res < 0)
70 		return res;
71 	else if (res != 2)
72 		return -EIO;
73 	else
74 		return 0;
75 }
76 
77 static int max517_read_raw(struct iio_dev *indio_dev,
78 			   struct iio_chan_spec const *chan,
79 			   int *val,
80 			   int *val2,
81 			   long m)
82 {
83 	struct max517_data *data = iio_priv(indio_dev);
84 
85 	switch (m) {
86 	case IIO_CHAN_INFO_SCALE:
87 		/* Corresponds to Vref / 2^(bits) */
88 		*val = data->vref_mv[chan->channel];
89 		*val2 = 8;
90 		return IIO_VAL_FRACTIONAL_LOG2;
91 	default:
92 		break;
93 	}
94 	return -EINVAL;
95 }
96 
97 static int max517_write_raw(struct iio_dev *indio_dev,
98 	struct iio_chan_spec const *chan, int val, int val2, long mask)
99 {
100 	int ret;
101 
102 	switch (mask) {
103 	case IIO_CHAN_INFO_RAW:
104 		ret = max517_set_value(indio_dev, val, chan->channel);
105 		break;
106 	default:
107 		ret = -EINVAL;
108 		break;
109 	}
110 
111 	return ret;
112 }
113 
114 #ifdef CONFIG_PM_SLEEP
115 static int max517_suspend(struct device *dev)
116 {
117 	u8 outbuf = COMMAND_PD;
118 
119 	return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
120 }
121 
122 static int max517_resume(struct device *dev)
123 {
124 	u8 outbuf = 0;
125 
126 	return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
127 }
128 
129 static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
130 #define MAX517_PM_OPS (&max517_pm_ops)
131 #else
132 #define MAX517_PM_OPS NULL
133 #endif
134 
135 static const struct iio_info max517_info = {
136 	.read_raw = max517_read_raw,
137 	.write_raw = max517_write_raw,
138 	.driver_module = THIS_MODULE,
139 };
140 
141 #define MAX517_CHANNEL(chan) {				\
142 	.type = IIO_VOLTAGE,				\
143 	.indexed = 1,					\
144 	.output = 1,					\
145 	.channel = (chan),				\
146 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
147 	BIT(IIO_CHAN_INFO_SCALE),			\
148 }
149 
150 static const struct iio_chan_spec max517_channels[] = {
151 	MAX517_CHANNEL(0),
152 	MAX517_CHANNEL(1)
153 };
154 
155 static int max517_probe(struct i2c_client *client,
156 			const struct i2c_device_id *id)
157 {
158 	struct max517_data *data;
159 	struct iio_dev *indio_dev;
160 	struct max517_platform_data *platform_data = client->dev.platform_data;
161 
162 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
163 	if (!indio_dev)
164 		return -ENOMEM;
165 	data = iio_priv(indio_dev);
166 	i2c_set_clientdata(client, indio_dev);
167 	data->client = client;
168 
169 	/* establish that the iio_dev is a child of the i2c device */
170 	indio_dev->dev.parent = &client->dev;
171 
172 	/* reduced channel set for MAX517 */
173 	if (id->driver_data == ID_MAX517)
174 		indio_dev->num_channels = 1;
175 	else
176 		indio_dev->num_channels = 2;
177 	indio_dev->channels = max517_channels;
178 	indio_dev->modes = INDIO_DIRECT_MODE;
179 	indio_dev->info = &max517_info;
180 
181 	/*
182 	 * Reference voltage on MAX518 and default is 5V, else take vref_mv
183 	 * from platform_data
184 	 */
185 	if (id->driver_data == ID_MAX518 || !platform_data) {
186 		data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */
187 	} else {
188 		data->vref_mv[0] = platform_data->vref_mv[0];
189 		data->vref_mv[1] = platform_data->vref_mv[1];
190 	}
191 
192 	return iio_device_register(indio_dev);
193 }
194 
195 static int max517_remove(struct i2c_client *client)
196 {
197 	iio_device_unregister(i2c_get_clientdata(client));
198 	return 0;
199 }
200 
201 static const struct i2c_device_id max517_id[] = {
202 	{ "max517", ID_MAX517 },
203 	{ "max518", ID_MAX518 },
204 	{ "max519", ID_MAX519 },
205 	{ }
206 };
207 MODULE_DEVICE_TABLE(i2c, max517_id);
208 
209 static struct i2c_driver max517_driver = {
210 	.driver = {
211 		.name	= MAX517_DRV_NAME,
212 		.pm		= MAX517_PM_OPS,
213 	},
214 	.probe		= max517_probe,
215 	.remove		= max517_remove,
216 	.id_table	= max517_id,
217 };
218 module_i2c_driver(max517_driver);
219 
220 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
221 MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC");
222 MODULE_LICENSE("GPL");
223