Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 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/init.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/jiffies.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/err.h> |
| 27 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 28 | #include <linux/iio/iio.h> |
| 29 | #include <linux/iio/sysfs.h> |
Lars-Peter Clausen | dbdc025 | 2012-06-04 11:36:28 +0200 | [diff] [blame] | 30 | #include <linux/iio/dac/max517.h> |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 31 | |
| 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 | |
| 39 | enum max517_device_ids { |
| 40 | ID_MAX517, |
| 41 | ID_MAX518, |
| 42 | ID_MAX519, |
| 43 | }; |
| 44 | |
| 45 | struct max517_data { |
Roland Stigge | 826514b | 2011-01-29 16:36:46 +0100 | [diff] [blame] | 46 | struct i2c_client *client; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 47 | 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 Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 55 | static int max517_set_value(struct iio_dev *indio_dev, |
| 56 | long val, int channel) |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 57 | { |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 58 | struct max517_data *data = iio_priv(indio_dev); |
Roland Stigge | 826514b | 2011-01-29 16:36:46 +0100 | [diff] [blame] | 59 | struct i2c_client *client = data->client; |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 60 | u8 outbuf[2]; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 61 | int res; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 62 | |
| 63 | if (val < 0 || val > 255) |
| 64 | return -EINVAL; |
| 65 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 66 | outbuf[0] = channel; |
| 67 | outbuf[1] = val; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 68 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 69 | res = i2c_master_send(client, outbuf, 2); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 70 | if (res < 0) |
| 71 | return res; |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 72 | else if (res != 2) |
| 73 | return -EIO; |
| 74 | else |
| 75 | return 0; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 78 | static 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 Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 83 | { |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 84 | struct max517_data *data = iio_priv(indio_dev); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 85 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 86 | switch (m) { |
| 87 | case IIO_CHAN_INFO_SCALE: |
| 88 | /* Corresponds to Vref / 2^(bits) */ |
Lars-Peter Clausen | 998f129 | 2013-09-28 10:31:00 +0100 | [diff] [blame^] | 89 | *val = data->vref_mv[chan->channel]; |
| 90 | *val2 = 8; |
| 91 | return IIO_VAL_FRACTIONAL_LOG2; |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | return -EINVAL; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 98 | static int max517_write_raw(struct iio_dev *indio_dev, |
| 99 | struct iio_chan_spec const *chan, int val, int val2, long mask) |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 100 | { |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 101 | int ret; |
| 102 | |
| 103 | switch (mask) { |
| 104 | case IIO_CHAN_INFO_RAW: |
| 105 | ret = max517_set_value(indio_dev, val, chan->channel); |
| 106 | break; |
| 107 | default: |
| 108 | ret = -EINVAL; |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | return ret; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 113 | } |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 114 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 115 | #ifdef CONFIG_PM_SLEEP |
| 116 | static int max517_suspend(struct device *dev) |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 117 | { |
| 118 | u8 outbuf = COMMAND_PD; |
| 119 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 120 | return i2c_master_send(to_i2c_client(dev), &outbuf, 1); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 123 | static int max517_resume(struct device *dev) |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 124 | { |
| 125 | u8 outbuf = 0; |
| 126 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 127 | return i2c_master_send(to_i2c_client(dev), &outbuf, 1); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 130 | static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume); |
| 131 | #define MAX517_PM_OPS (&max517_pm_ops) |
| 132 | #else |
| 133 | #define MAX517_PM_OPS NULL |
| 134 | #endif |
| 135 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 136 | static const struct iio_info max517_info = { |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 137 | .read_raw = max517_read_raw, |
| 138 | .write_raw = max517_write_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 139 | .driver_module = THIS_MODULE, |
| 140 | }; |
| 141 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 142 | #define MAX517_CHANNEL(chan) { \ |
| 143 | .type = IIO_VOLTAGE, \ |
| 144 | .indexed = 1, \ |
| 145 | .output = 1, \ |
| 146 | .channel = (chan), \ |
Jonathan Cameron | 040b837 | 2013-02-27 19:28:22 +0000 | [diff] [blame] | 147 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 148 | BIT(IIO_CHAN_INFO_SCALE), \ |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 149 | .scan_type = IIO_ST('u', 8, 8, 0), \ |
| 150 | } |
| 151 | |
| 152 | static const struct iio_chan_spec max517_channels[] = { |
| 153 | MAX517_CHANNEL(0), |
| 154 | MAX517_CHANNEL(1) |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 155 | }; |
| 156 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 157 | static int max517_probe(struct i2c_client *client, |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 158 | const struct i2c_device_id *id) |
| 159 | { |
| 160 | struct max517_data *data; |
Jonathan Cameron | cada11f | 2011-06-27 13:07:35 +0100 | [diff] [blame] | 161 | struct iio_dev *indio_dev; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 162 | struct max517_platform_data *platform_data = client->dev.platform_data; |
| 163 | int err; |
| 164 | |
Sachin Kamat | cc566fd | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 165 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 166 | if (!indio_dev) |
| 167 | return -ENOMEM; |
Jonathan Cameron | cada11f | 2011-06-27 13:07:35 +0100 | [diff] [blame] | 168 | data = iio_priv(indio_dev); |
| 169 | i2c_set_clientdata(client, indio_dev); |
Roland Stigge | 826514b | 2011-01-29 16:36:46 +0100 | [diff] [blame] | 170 | data->client = client; |
| 171 | |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 172 | /* establish that the iio_dev is a child of the i2c device */ |
Jonathan Cameron | cada11f | 2011-06-27 13:07:35 +0100 | [diff] [blame] | 173 | indio_dev->dev.parent = &client->dev; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 174 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 175 | /* reduced channel set for MAX517 */ |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 176 | if (id->driver_data == ID_MAX517) |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 177 | indio_dev->num_channels = 1; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 178 | else |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 179 | indio_dev->num_channels = 2; |
| 180 | indio_dev->channels = max517_channels; |
Jonathan Cameron | cada11f | 2011-06-27 13:07:35 +0100 | [diff] [blame] | 181 | indio_dev->modes = INDIO_DIRECT_MODE; |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 182 | indio_dev->info = &max517_info; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * Reference voltage on MAX518 and default is 5V, else take vref_mv |
| 186 | * from platform_data |
| 187 | */ |
| 188 | if (id->driver_data == ID_MAX518 || !platform_data) { |
| 189 | data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */ |
| 190 | } else { |
| 191 | data->vref_mv[0] = platform_data->vref_mv[0]; |
| 192 | data->vref_mv[1] = platform_data->vref_mv[1]; |
| 193 | } |
| 194 | |
Jonathan Cameron | cada11f | 2011-06-27 13:07:35 +0100 | [diff] [blame] | 195 | err = iio_device_register(indio_dev); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 196 | if (err) |
Sachin Kamat | cc566fd | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 197 | return err; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 198 | |
| 199 | dev_info(&client->dev, "DAC registered\n"); |
| 200 | |
| 201 | return 0; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 204 | static int max517_remove(struct i2c_client *client) |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 205 | { |
Peter Meerwald | e8ccc11 | 2012-05-02 01:13:33 +0200 | [diff] [blame] | 206 | iio_device_unregister(i2c_get_clientdata(client)); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static const struct i2c_device_id max517_id[] = { |
| 211 | { "max517", ID_MAX517 }, |
| 212 | { "max518", ID_MAX518 }, |
| 213 | { "max519", ID_MAX519 }, |
| 214 | { } |
| 215 | }; |
| 216 | MODULE_DEVICE_TABLE(i2c, max517_id); |
| 217 | |
| 218 | static struct i2c_driver max517_driver = { |
| 219 | .driver = { |
| 220 | .name = MAX517_DRV_NAME, |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 221 | .pm = MAX517_PM_OPS, |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 222 | }, |
| 223 | .probe = max517_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 224 | .remove = max517_remove, |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 225 | .id_table = max517_id, |
| 226 | }; |
Lars-Peter Clausen | 6e5af18 | 2011-11-16 10:13:38 +0100 | [diff] [blame] | 227 | module_i2c_driver(max517_driver); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 228 | |
| 229 | MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); |
| 230 | MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC"); |
| 231 | MODULE_LICENSE("GPL"); |