Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * mcp4725.c - Support for Microchip MCP4725 |
| 3 | * |
| 4 | * Copyright (C) 2012 Peter Meerwald <pmeerw@pmeerw.net> |
| 5 | * |
| 6 | * Based on max517 by Roland Stigge <stigge@antcom.de> |
| 7 | * |
| 8 | * This file is subject to the terms and conditions of version 2 of |
| 9 | * the GNU General Public License. See the file COPYING in the main |
| 10 | * directory of this archive for more details. |
| 11 | * |
| 12 | * driver for the Microchip I2C 12-bit digital-to-analog converter (DAC) |
| 13 | * (7-bit I2C slave address 0x60, the three LSBs can be configured in |
| 14 | * hardware) |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 18 | #include <linux/i2c.h> |
| 19 | #include <linux/err.h> |
Peter Meerwald | f17b77d | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 20 | #include <linux/delay.h> |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 21 | |
| 22 | #include <linux/iio/iio.h> |
| 23 | #include <linux/iio/sysfs.h> |
| 24 | |
| 25 | #include <linux/iio/dac/mcp4725.h> |
| 26 | |
| 27 | #define MCP4725_DRV_NAME "mcp4725" |
| 28 | |
| 29 | struct mcp4725_data { |
| 30 | struct i2c_client *client; |
| 31 | u16 vref_mv; |
| 32 | u16 dac_value; |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 33 | bool powerdown; |
| 34 | unsigned powerdown_mode; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 35 | }; |
| 36 | |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 37 | static int mcp4725_suspend(struct device *dev) |
| 38 | { |
Peter Meerwald | a97dd06 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 39 | struct mcp4725_data *data = iio_priv(i2c_get_clientdata( |
| 40 | to_i2c_client(dev))); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 41 | u8 outbuf[2]; |
| 42 | |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 43 | outbuf[0] = (data->powerdown_mode + 1) << 4; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 44 | outbuf[1] = 0; |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 45 | data->powerdown = true; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 46 | |
Peter Meerwald | a97dd06 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 47 | return i2c_master_send(data->client, outbuf, 2); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static int mcp4725_resume(struct device *dev) |
| 51 | { |
Peter Meerwald | a97dd06 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 52 | struct mcp4725_data *data = iio_priv(i2c_get_clientdata( |
| 53 | to_i2c_client(dev))); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 54 | u8 outbuf[2]; |
| 55 | |
| 56 | /* restore previous DAC value */ |
| 57 | outbuf[0] = (data->dac_value >> 8) & 0xf; |
| 58 | outbuf[1] = data->dac_value & 0xff; |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 59 | data->powerdown = false; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 60 | |
Peter Meerwald | a97dd06 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 61 | return i2c_master_send(data->client, outbuf, 2); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 64 | #ifdef CONFIG_PM_SLEEP |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 65 | static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume); |
| 66 | #define MCP4725_PM_OPS (&mcp4725_pm_ops) |
| 67 | #else |
| 68 | #define MCP4725_PM_OPS NULL |
| 69 | #endif |
| 70 | |
Jonathan Cameron | 4fd313d | 2013-07-12 20:54:00 +0100 | [diff] [blame] | 71 | static ssize_t mcp4725_store_eeprom(struct device *dev, |
Peter Meerwald | f17b77d | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 72 | struct device_attribute *attr, const char *buf, size_t len) |
| 73 | { |
| 74 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 75 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 76 | int tries = 20; |
| 77 | u8 inoutbuf[3]; |
| 78 | bool state; |
| 79 | int ret; |
| 80 | |
| 81 | ret = strtobool(buf, &state); |
| 82 | if (ret < 0) |
| 83 | return ret; |
| 84 | |
| 85 | if (!state) |
| 86 | return 0; |
| 87 | |
| 88 | inoutbuf[0] = 0x60; /* write EEPROM */ |
| 89 | inoutbuf[1] = data->dac_value >> 4; |
| 90 | inoutbuf[2] = (data->dac_value & 0xf) << 4; |
| 91 | |
| 92 | ret = i2c_master_send(data->client, inoutbuf, 3); |
| 93 | if (ret < 0) |
| 94 | return ret; |
| 95 | else if (ret != 3) |
| 96 | return -EIO; |
| 97 | |
| 98 | /* wait for write complete, takes up to 50ms */ |
| 99 | while (tries--) { |
| 100 | msleep(20); |
| 101 | ret = i2c_master_recv(data->client, inoutbuf, 3); |
| 102 | if (ret < 0) |
| 103 | return ret; |
| 104 | else if (ret != 3) |
| 105 | return -EIO; |
| 106 | |
| 107 | if (inoutbuf[0] & 0x80) |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | if (tries < 0) { |
| 112 | dev_err(&data->client->dev, |
| 113 | "mcp4725_store_eeprom() failed, incomplete\n"); |
| 114 | return -EIO; |
| 115 | } |
| 116 | |
| 117 | return len; |
| 118 | } |
| 119 | |
| 120 | static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0); |
| 121 | |
| 122 | static struct attribute *mcp4725_attributes[] = { |
| 123 | &iio_dev_attr_store_eeprom.dev_attr.attr, |
| 124 | NULL, |
| 125 | }; |
| 126 | |
| 127 | static const struct attribute_group mcp4725_attribute_group = { |
| 128 | .attrs = mcp4725_attributes, |
| 129 | }; |
| 130 | |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 131 | static const char * const mcp4725_powerdown_modes[] = { |
| 132 | "1kohm_to_gnd", |
| 133 | "100kohm_to_gnd", |
| 134 | "500kohm_to_gnd" |
| 135 | }; |
| 136 | |
| 137 | static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev, |
| 138 | const struct iio_chan_spec *chan) |
| 139 | { |
| 140 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 141 | |
| 142 | return data->powerdown_mode; |
| 143 | } |
| 144 | |
| 145 | static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev, |
| 146 | const struct iio_chan_spec *chan, unsigned mode) |
| 147 | { |
| 148 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 149 | |
| 150 | data->powerdown_mode = mode; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev, |
| 156 | uintptr_t private, const struct iio_chan_spec *chan, char *buf) |
| 157 | { |
| 158 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 159 | |
| 160 | return sprintf(buf, "%d\n", data->powerdown); |
| 161 | } |
| 162 | |
| 163 | static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev, |
| 164 | uintptr_t private, const struct iio_chan_spec *chan, |
| 165 | const char *buf, size_t len) |
| 166 | { |
| 167 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 168 | bool state; |
| 169 | int ret; |
| 170 | |
| 171 | ret = strtobool(buf, &state); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
| 175 | if (state) |
| 176 | ret = mcp4725_suspend(&data->client->dev); |
| 177 | else |
| 178 | ret = mcp4725_resume(&data->client->dev); |
| 179 | if (ret < 0) |
| 180 | return ret; |
| 181 | |
| 182 | return len; |
| 183 | } |
| 184 | |
| 185 | static const struct iio_enum mcp4725_powerdown_mode_enum = { |
| 186 | .items = mcp4725_powerdown_modes, |
| 187 | .num_items = ARRAY_SIZE(mcp4725_powerdown_modes), |
| 188 | .get = mcp4725_get_powerdown_mode, |
| 189 | .set = mcp4725_set_powerdown_mode, |
| 190 | }; |
| 191 | |
| 192 | static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = { |
| 193 | { |
| 194 | .name = "powerdown", |
| 195 | .read = mcp4725_read_powerdown, |
| 196 | .write = mcp4725_write_powerdown, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 197 | .shared = IIO_SEPARATE, |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 198 | }, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 199 | IIO_ENUM("powerdown_mode", IIO_SEPARATE, &mcp4725_powerdown_mode_enum), |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 200 | IIO_ENUM_AVAILABLE("powerdown_mode", &mcp4725_powerdown_mode_enum), |
| 201 | { }, |
| 202 | }; |
| 203 | |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 204 | static const struct iio_chan_spec mcp4725_channel = { |
| 205 | .type = IIO_VOLTAGE, |
| 206 | .indexed = 1, |
| 207 | .output = 1, |
| 208 | .channel = 0, |
Jonathan Cameron | 90b4637 | 2013-02-27 19:28:38 +0000 | [diff] [blame] | 209 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 210 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 211 | .ext_info = mcp4725_ext_info, |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | static int mcp4725_set_value(struct iio_dev *indio_dev, int val) |
| 215 | { |
| 216 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 217 | u8 outbuf[2]; |
| 218 | int ret; |
| 219 | |
| 220 | if (val >= (1 << 12) || val < 0) |
| 221 | return -EINVAL; |
| 222 | |
| 223 | outbuf[0] = (val >> 8) & 0xf; |
| 224 | outbuf[1] = val & 0xff; |
| 225 | |
| 226 | ret = i2c_master_send(data->client, outbuf, 2); |
| 227 | if (ret < 0) |
| 228 | return ret; |
| 229 | else if (ret != 2) |
| 230 | return -EIO; |
| 231 | else |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int mcp4725_read_raw(struct iio_dev *indio_dev, |
| 236 | struct iio_chan_spec const *chan, |
| 237 | int *val, int *val2, long mask) |
| 238 | { |
| 239 | struct mcp4725_data *data = iio_priv(indio_dev); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 240 | |
| 241 | switch (mask) { |
| 242 | case IIO_CHAN_INFO_RAW: |
| 243 | *val = data->dac_value; |
| 244 | return IIO_VAL_INT; |
| 245 | case IIO_CHAN_INFO_SCALE: |
Lars-Peter Clausen | b117f96 | 2013-09-28 10:31:00 +0100 | [diff] [blame] | 246 | *val = data->vref_mv; |
| 247 | *val2 = 12; |
| 248 | return IIO_VAL_FRACTIONAL_LOG2; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 249 | } |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | |
| 253 | static int mcp4725_write_raw(struct iio_dev *indio_dev, |
| 254 | struct iio_chan_spec const *chan, |
| 255 | int val, int val2, long mask) |
| 256 | { |
| 257 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 258 | int ret; |
| 259 | |
| 260 | switch (mask) { |
| 261 | case IIO_CHAN_INFO_RAW: |
| 262 | ret = mcp4725_set_value(indio_dev, val); |
| 263 | data->dac_value = val; |
| 264 | break; |
| 265 | default: |
| 266 | ret = -EINVAL; |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | static const struct iio_info mcp4725_info = { |
| 274 | .read_raw = mcp4725_read_raw, |
| 275 | .write_raw = mcp4725_write_raw, |
Peter Meerwald | f17b77d | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 276 | .attrs = &mcp4725_attribute_group, |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 277 | .driver_module = THIS_MODULE, |
| 278 | }; |
| 279 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 280 | static int mcp4725_probe(struct i2c_client *client, |
| 281 | const struct i2c_device_id *id) |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 282 | { |
| 283 | struct mcp4725_data *data; |
| 284 | struct iio_dev *indio_dev; |
| 285 | struct mcp4725_platform_data *platform_data = client->dev.platform_data; |
| 286 | u8 inbuf[3]; |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 287 | u8 pd; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 288 | int err; |
| 289 | |
| 290 | if (!platform_data || !platform_data->vref_mv) { |
| 291 | dev_err(&client->dev, "invalid platform data"); |
Peter Meerwald | b6ff86f | 2013-07-29 23:18:00 +0100 | [diff] [blame] | 292 | return -EINVAL; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Peter Meerwald | b6ff86f | 2013-07-29 23:18:00 +0100 | [diff] [blame] | 295 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 296 | if (indio_dev == NULL) |
| 297 | return -ENOMEM; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 298 | data = iio_priv(indio_dev); |
| 299 | i2c_set_clientdata(client, indio_dev); |
| 300 | data->client = client; |
| 301 | |
| 302 | indio_dev->dev.parent = &client->dev; |
| 303 | indio_dev->info = &mcp4725_info; |
| 304 | indio_dev->channels = &mcp4725_channel; |
| 305 | indio_dev->num_channels = 1; |
| 306 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 307 | |
| 308 | data->vref_mv = platform_data->vref_mv; |
| 309 | |
| 310 | /* read current DAC value */ |
| 311 | err = i2c_master_recv(client, inbuf, 3); |
| 312 | if (err < 0) { |
| 313 | dev_err(&client->dev, "failed to read DAC value"); |
Peter Meerwald | b6ff86f | 2013-07-29 23:18:00 +0100 | [diff] [blame] | 314 | return err; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 315 | } |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 316 | pd = (inbuf[0] >> 1) & 0x3; |
| 317 | data->powerdown = pd > 0 ? true : false; |
| 318 | data->powerdown_mode = pd ? pd-1 : 2; /* 500kohm_to_gnd */ |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 319 | data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4); |
| 320 | |
Sachin Kamat | a9b6851 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 321 | return iio_device_register(indio_dev); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 324 | static int mcp4725_remove(struct i2c_client *client) |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 325 | { |
Peter Meerwald | b6ff86f | 2013-07-29 23:18:00 +0100 | [diff] [blame] | 326 | iio_device_unregister(i2c_get_clientdata(client)); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static const struct i2c_device_id mcp4725_id[] = { |
| 331 | { "mcp4725", 0 }, |
| 332 | { } |
| 333 | }; |
| 334 | MODULE_DEVICE_TABLE(i2c, mcp4725_id); |
| 335 | |
| 336 | static struct i2c_driver mcp4725_driver = { |
| 337 | .driver = { |
| 338 | .name = MCP4725_DRV_NAME, |
| 339 | .pm = MCP4725_PM_OPS, |
| 340 | }, |
| 341 | .probe = mcp4725_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 342 | .remove = mcp4725_remove, |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 343 | .id_table = mcp4725_id, |
| 344 | }; |
| 345 | module_i2c_driver(mcp4725_driver); |
| 346 | |
| 347 | MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); |
| 348 | MODULE_DESCRIPTION("MCP4725 12-bit DAC"); |
| 349 | MODULE_LICENSE("GPL"); |