Lars-Peter Clausen | c03f2c5 | 2011-12-06 10:07:02 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Analog devices AD5380, AD5381, AD5382, AD5383, AD5390, AD5391, AD5392 |
| 3 | * multi-channel Digital to Analog Converters driver |
| 4 | * |
| 5 | * Copyright 2011 Analog Devices Inc. |
| 6 | * |
| 7 | * Licensed under the GPL-2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/i2c.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/spi/spi.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/sysfs.h> |
| 18 | #include <linux/regmap.h> |
| 19 | #include <linux/regulator/consumer.h> |
| 20 | |
| 21 | #include "../iio.h" |
| 22 | #include "../sysfs.h" |
| 23 | #include "dac.h" |
| 24 | |
| 25 | |
| 26 | #define AD5380_REG_DATA(x) (((x) << 2) | 3) |
| 27 | #define AD5380_REG_OFFSET(x) (((x) << 2) | 2) |
| 28 | #define AD5380_REG_GAIN(x) (((x) << 2) | 1) |
| 29 | #define AD5380_REG_SF_PWR_DOWN (8 << 2) |
| 30 | #define AD5380_REG_SF_PWR_UP (9 << 2) |
| 31 | #define AD5380_REG_SF_CTRL (12 << 2) |
| 32 | |
| 33 | #define AD5380_CTRL_PWR_DOWN_MODE_OFFSET 13 |
| 34 | #define AD5380_CTRL_INT_VREF_2V5 BIT(12) |
| 35 | #define AD5380_CTRL_INT_VREF_EN BIT(10) |
| 36 | |
| 37 | /** |
| 38 | * struct ad5380_chip_info - chip specific information |
| 39 | * @channel_template: channel specification template |
| 40 | * @num_channels: number of channels |
| 41 | * @int_vref: internal vref in uV |
| 42 | */ |
| 43 | |
| 44 | struct ad5380_chip_info { |
| 45 | struct iio_chan_spec channel_template; |
| 46 | unsigned int num_channels; |
| 47 | unsigned int int_vref; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * struct ad5380_state - driver instance specific data |
| 52 | * @regmap: regmap instance used by the device |
| 53 | * @chip_info: chip model specific constants, available modes etc |
| 54 | * @vref_reg: vref supply regulator |
| 55 | * @vref: actual reference voltage used in uA |
| 56 | * @pwr_down: whether the chip is currently in power down mode |
| 57 | */ |
| 58 | |
| 59 | struct ad5380_state { |
| 60 | struct regmap *regmap; |
| 61 | const struct ad5380_chip_info *chip_info; |
| 62 | struct regulator *vref_reg; |
| 63 | int vref; |
| 64 | bool pwr_down; |
| 65 | }; |
| 66 | |
| 67 | enum ad5380_type { |
| 68 | ID_AD5380_3, |
| 69 | ID_AD5380_5, |
| 70 | ID_AD5381_3, |
| 71 | ID_AD5381_5, |
| 72 | ID_AD5382_3, |
| 73 | ID_AD5382_5, |
| 74 | ID_AD5383_3, |
| 75 | ID_AD5383_5, |
| 76 | ID_AD5390_3, |
| 77 | ID_AD5390_5, |
| 78 | ID_AD5391_3, |
| 79 | ID_AD5391_5, |
| 80 | ID_AD5392_3, |
| 81 | ID_AD5392_5, |
| 82 | }; |
| 83 | |
| 84 | #define AD5380_CHANNEL(_bits) { \ |
| 85 | .type = IIO_VOLTAGE, \ |
| 86 | .indexed = 1, \ |
| 87 | .output = 1, \ |
| 88 | .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT | \ |
| 89 | IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT | \ |
| 90 | IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT, \ |
| 91 | .scan_type = IIO_ST('u', (_bits), 16, 14 - (_bits)) \ |
| 92 | } |
| 93 | |
| 94 | static const struct ad5380_chip_info ad5380_chip_info_tbl[] = { |
| 95 | [ID_AD5380_3] = { |
| 96 | .channel_template = AD5380_CHANNEL(14), |
| 97 | .num_channels = 40, |
| 98 | .int_vref = 1250000, |
| 99 | }, |
| 100 | [ID_AD5380_5] = { |
| 101 | .channel_template = AD5380_CHANNEL(14), |
| 102 | .num_channels = 40, |
| 103 | .int_vref = 2500000, |
| 104 | }, |
| 105 | [ID_AD5381_3] = { |
| 106 | .channel_template = AD5380_CHANNEL(12), |
| 107 | .num_channels = 16, |
| 108 | .int_vref = 1250000, |
| 109 | }, |
| 110 | [ID_AD5381_5] = { |
| 111 | .channel_template = AD5380_CHANNEL(12), |
| 112 | .num_channels = 16, |
| 113 | .int_vref = 2500000, |
| 114 | }, |
| 115 | [ID_AD5382_3] = { |
| 116 | .channel_template = AD5380_CHANNEL(14), |
| 117 | .num_channels = 32, |
| 118 | .int_vref = 1250000, |
| 119 | }, |
| 120 | [ID_AD5382_5] = { |
| 121 | .channel_template = AD5380_CHANNEL(14), |
| 122 | .num_channels = 32, |
| 123 | .int_vref = 2500000, |
| 124 | }, |
| 125 | [ID_AD5383_3] = { |
| 126 | .channel_template = AD5380_CHANNEL(12), |
| 127 | .num_channels = 32, |
| 128 | .int_vref = 1250000, |
| 129 | }, |
| 130 | [ID_AD5383_5] = { |
| 131 | .channel_template = AD5380_CHANNEL(12), |
| 132 | .num_channels = 32, |
| 133 | .int_vref = 2500000, |
| 134 | }, |
| 135 | [ID_AD5390_3] = { |
| 136 | .channel_template = AD5380_CHANNEL(14), |
| 137 | .num_channels = 16, |
| 138 | .int_vref = 1250000, |
| 139 | }, |
| 140 | [ID_AD5390_5] = { |
| 141 | .channel_template = AD5380_CHANNEL(14), |
| 142 | .num_channels = 16, |
| 143 | .int_vref = 2500000, |
| 144 | }, |
| 145 | [ID_AD5391_3] = { |
| 146 | .channel_template = AD5380_CHANNEL(12), |
| 147 | .num_channels = 16, |
| 148 | .int_vref = 1250000, |
| 149 | }, |
| 150 | [ID_AD5391_5] = { |
| 151 | .channel_template = AD5380_CHANNEL(12), |
| 152 | .num_channels = 16, |
| 153 | .int_vref = 2500000, |
| 154 | }, |
| 155 | [ID_AD5392_3] = { |
| 156 | .channel_template = AD5380_CHANNEL(14), |
| 157 | .num_channels = 8, |
| 158 | .int_vref = 1250000, |
| 159 | }, |
| 160 | [ID_AD5392_5] = { |
| 161 | .channel_template = AD5380_CHANNEL(14), |
| 162 | .num_channels = 8, |
| 163 | .int_vref = 2500000, |
| 164 | }, |
| 165 | }; |
| 166 | |
| 167 | static ssize_t ad5380_read_dac_powerdown(struct device *dev, |
| 168 | struct device_attribute *attr, char *buf) |
| 169 | { |
| 170 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 171 | struct ad5380_state *st = iio_priv(indio_dev); |
| 172 | |
| 173 | return sprintf(buf, "%d\n", st->pwr_down); |
| 174 | } |
| 175 | |
| 176 | static ssize_t ad5380_write_dac_powerdown(struct device *dev, |
| 177 | struct device_attribute *attr, const char *buf, size_t len) |
| 178 | { |
| 179 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 180 | struct ad5380_state *st = iio_priv(indio_dev); |
| 181 | bool pwr_down; |
| 182 | int ret; |
| 183 | |
| 184 | ret = strtobool(buf, &pwr_down); |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | |
| 188 | mutex_lock(&indio_dev->mlock); |
| 189 | |
| 190 | if (pwr_down) |
| 191 | ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_DOWN, 0); |
| 192 | else |
| 193 | ret = regmap_write(st->regmap, AD5380_REG_SF_PWR_UP, 0); |
| 194 | |
| 195 | st->pwr_down = pwr_down; |
| 196 | |
| 197 | mutex_unlock(&indio_dev->mlock); |
| 198 | |
| 199 | return ret ? ret : len; |
| 200 | } |
| 201 | |
| 202 | static IIO_DEVICE_ATTR(out_voltage_powerdown, |
| 203 | S_IRUGO | S_IWUSR, |
| 204 | ad5380_read_dac_powerdown, |
| 205 | ad5380_write_dac_powerdown, 0); |
| 206 | |
| 207 | static const char ad5380_powerdown_modes[][15] = { |
| 208 | [0] = "100kohm_to_gnd", |
| 209 | [1] = "three_state", |
| 210 | }; |
| 211 | |
| 212 | static ssize_t ad5380_read_powerdown_mode(struct device *dev, |
| 213 | struct device_attribute *attr, char *buf) |
| 214 | { |
| 215 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 216 | struct ad5380_state *st = iio_priv(indio_dev); |
| 217 | unsigned int mode; |
| 218 | int ret; |
| 219 | |
| 220 | ret = regmap_read(st->regmap, AD5380_REG_SF_CTRL, &mode); |
| 221 | if (ret) |
| 222 | return ret; |
| 223 | |
| 224 | mode = (mode >> AD5380_CTRL_PWR_DOWN_MODE_OFFSET) & 1; |
| 225 | |
| 226 | return sprintf(buf, "%s\n", ad5380_powerdown_modes[mode]); |
| 227 | } |
| 228 | |
| 229 | static ssize_t ad5380_write_powerdown_mode(struct device *dev, |
| 230 | struct device_attribute *attr, const char *buf, size_t len) |
| 231 | { |
| 232 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 233 | struct ad5380_state *st = iio_priv(indio_dev); |
| 234 | unsigned int i; |
| 235 | int ret; |
| 236 | |
| 237 | for (i = 0; i < ARRAY_SIZE(ad5380_powerdown_modes); ++i) { |
| 238 | if (sysfs_streq(buf, ad5380_powerdown_modes[i])) |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | if (i == ARRAY_SIZE(ad5380_powerdown_modes)) |
| 243 | return -EINVAL; |
| 244 | |
| 245 | ret = regmap_update_bits(st->regmap, AD5380_REG_SF_CTRL, |
| 246 | 1 << AD5380_CTRL_PWR_DOWN_MODE_OFFSET, |
| 247 | i << AD5380_CTRL_PWR_DOWN_MODE_OFFSET); |
| 248 | |
| 249 | return ret ? ret : len; |
| 250 | } |
| 251 | |
| 252 | static IIO_DEVICE_ATTR(out_voltage_powerdown_mode, |
| 253 | S_IRUGO | S_IWUSR, |
| 254 | ad5380_read_powerdown_mode, |
| 255 | ad5380_write_powerdown_mode, 0); |
| 256 | |
| 257 | static IIO_CONST_ATTR(out_voltage_powerdown_mode_available, |
| 258 | "100kohm_to_gnd three_state"); |
| 259 | |
| 260 | static struct attribute *ad5380_attributes[] = { |
| 261 | &iio_dev_attr_out_voltage_powerdown.dev_attr.attr, |
| 262 | &iio_dev_attr_out_voltage_powerdown_mode.dev_attr.attr, |
| 263 | &iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr, |
| 264 | NULL, |
| 265 | }; |
| 266 | |
| 267 | static const struct attribute_group ad5380_attribute_group = { |
| 268 | .attrs = ad5380_attributes, |
| 269 | }; |
| 270 | |
| 271 | static unsigned int ad5380_info_to_reg(struct iio_chan_spec const *chan, |
| 272 | long info) |
| 273 | { |
| 274 | switch (info) { |
| 275 | case 0: |
| 276 | return AD5380_REG_DATA(chan->address); |
| 277 | case IIO_CHAN_INFO_CALIBBIAS: |
| 278 | return AD5380_REG_OFFSET(chan->address); |
| 279 | case IIO_CHAN_INFO_CALIBSCALE: |
| 280 | return AD5380_REG_GAIN(chan->address); |
| 281 | default: |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int ad5380_write_raw(struct iio_dev *indio_dev, |
| 289 | struct iio_chan_spec const *chan, int val, int val2, long info) |
| 290 | { |
| 291 | const unsigned int max_val = (1 << chan->scan_type.realbits); |
| 292 | struct ad5380_state *st = iio_priv(indio_dev); |
| 293 | |
| 294 | switch (info) { |
| 295 | case 0: |
| 296 | case IIO_CHAN_INFO_CALIBSCALE: |
| 297 | if (val >= max_val || val < 0) |
| 298 | return -EINVAL; |
| 299 | |
| 300 | return regmap_write(st->regmap, |
| 301 | ad5380_info_to_reg(chan, info), |
| 302 | val << chan->scan_type.shift); |
| 303 | case IIO_CHAN_INFO_CALIBBIAS: |
| 304 | val += (1 << chan->scan_type.realbits) / 2; |
| 305 | if (val >= max_val || val < 0) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | return regmap_write(st->regmap, |
| 309 | AD5380_REG_OFFSET(chan->address), |
| 310 | val << chan->scan_type.shift); |
| 311 | default: |
| 312 | break; |
| 313 | } |
| 314 | return -EINVAL; |
| 315 | } |
| 316 | |
| 317 | static int ad5380_read_raw(struct iio_dev *indio_dev, |
| 318 | struct iio_chan_spec const *chan, int *val, int *val2, long info) |
| 319 | { |
| 320 | struct ad5380_state *st = iio_priv(indio_dev); |
| 321 | unsigned long scale_uv; |
| 322 | int ret; |
| 323 | |
| 324 | switch (info) { |
| 325 | case 0: |
| 326 | case IIO_CHAN_INFO_CALIBSCALE: |
| 327 | ret = regmap_read(st->regmap, ad5380_info_to_reg(chan, info), |
| 328 | val); |
| 329 | if (ret) |
| 330 | return ret; |
| 331 | *val >>= chan->scan_type.shift; |
| 332 | return IIO_VAL_INT; |
| 333 | case IIO_CHAN_INFO_CALIBBIAS: |
| 334 | ret = regmap_read(st->regmap, AD5380_REG_OFFSET(chan->address), |
| 335 | val); |
| 336 | if (ret) |
| 337 | return ret; |
| 338 | *val >>= chan->scan_type.shift; |
| 339 | val -= (1 << chan->scan_type.realbits) / 2; |
| 340 | return IIO_VAL_INT; |
| 341 | case IIO_CHAN_INFO_SCALE: |
| 342 | scale_uv = ((2 * st->vref) >> chan->scan_type.realbits) * 100; |
| 343 | *val = scale_uv / 100000; |
| 344 | *val2 = (scale_uv % 100000) * 10; |
| 345 | return IIO_VAL_INT_PLUS_MICRO; |
| 346 | default: |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | return -EINVAL; |
| 351 | } |
| 352 | |
| 353 | static const struct iio_info ad5380_info = { |
| 354 | .read_raw = ad5380_read_raw, |
| 355 | .write_raw = ad5380_write_raw, |
| 356 | .attrs = &ad5380_attribute_group, |
| 357 | .driver_module = THIS_MODULE, |
| 358 | }; |
| 359 | |
| 360 | static int __devinit ad5380_alloc_channels(struct iio_dev *indio_dev) |
| 361 | { |
| 362 | struct ad5380_state *st = iio_priv(indio_dev); |
| 363 | struct iio_chan_spec *channels; |
| 364 | unsigned int i; |
| 365 | |
| 366 | channels = kcalloc(sizeof(struct iio_chan_spec), |
| 367 | st->chip_info->num_channels, GFP_KERNEL); |
| 368 | |
| 369 | if (!channels) |
| 370 | return -ENOMEM; |
| 371 | |
| 372 | for (i = 0; i < st->chip_info->num_channels; ++i) { |
| 373 | channels[i] = st->chip_info->channel_template; |
| 374 | channels[i].channel = i; |
| 375 | channels[i].address = i; |
| 376 | } |
| 377 | |
| 378 | indio_dev->channels = channels; |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static int __devinit ad5380_probe(struct device *dev, struct regmap *regmap, |
| 384 | enum ad5380_type type, const char *name) |
| 385 | { |
| 386 | struct iio_dev *indio_dev; |
| 387 | struct ad5380_state *st; |
| 388 | unsigned int ctrl = 0; |
| 389 | int ret; |
| 390 | |
| 391 | indio_dev = iio_allocate_device(sizeof(*st)); |
| 392 | if (indio_dev == NULL) { |
| 393 | dev_err(dev, "Failed to allocate iio device\n"); |
| 394 | ret = -ENOMEM; |
| 395 | goto error_regmap_exit; |
| 396 | } |
| 397 | |
| 398 | st = iio_priv(indio_dev); |
| 399 | dev_set_drvdata(dev, indio_dev); |
| 400 | |
| 401 | st->chip_info = &ad5380_chip_info_tbl[type]; |
| 402 | st->regmap = regmap; |
| 403 | |
| 404 | indio_dev->dev.parent = dev; |
| 405 | indio_dev->name = name; |
| 406 | indio_dev->info = &ad5380_info; |
| 407 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 408 | indio_dev->num_channels = st->chip_info->num_channels; |
| 409 | |
| 410 | ret = ad5380_alloc_channels(indio_dev); |
| 411 | if (ret) { |
| 412 | dev_err(dev, "Failed to allocate channel spec: %d\n", ret); |
| 413 | goto error_free; |
| 414 | } |
| 415 | |
| 416 | if (st->chip_info->int_vref == 2500000) |
| 417 | ctrl |= AD5380_CTRL_INT_VREF_2V5; |
| 418 | |
| 419 | st->vref_reg = regulator_get(dev, "vref"); |
| 420 | if (!IS_ERR(st->vref_reg)) { |
| 421 | ret = regulator_enable(st->vref_reg); |
| 422 | if (ret) { |
| 423 | dev_err(dev, "Failed to enable vref regulators: %d\n", |
| 424 | ret); |
| 425 | goto error_free_reg; |
| 426 | } |
| 427 | |
| 428 | st->vref = regulator_get_voltage(st->vref_reg); |
| 429 | } else { |
| 430 | st->vref = st->chip_info->int_vref; |
| 431 | ctrl |= AD5380_CTRL_INT_VREF_EN; |
| 432 | } |
| 433 | |
| 434 | ret = regmap_write(st->regmap, AD5380_REG_SF_CTRL, ctrl); |
| 435 | if (ret) { |
| 436 | dev_err(dev, "Failed to write to device: %d\n", ret); |
| 437 | goto error_disable_reg; |
| 438 | } |
| 439 | |
| 440 | ret = iio_device_register(indio_dev); |
| 441 | if (ret) { |
| 442 | dev_err(dev, "Failed to register iio device: %d\n", ret); |
| 443 | goto error_disable_reg; |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | |
| 448 | error_disable_reg: |
| 449 | if (!IS_ERR(st->vref_reg)) |
| 450 | regulator_disable(st->vref_reg); |
| 451 | error_free_reg: |
| 452 | if (!IS_ERR(st->vref_reg)) |
| 453 | regulator_put(st->vref_reg); |
| 454 | |
| 455 | kfree(indio_dev->channels); |
| 456 | error_free: |
| 457 | iio_free_device(indio_dev); |
| 458 | error_regmap_exit: |
| 459 | regmap_exit(regmap); |
| 460 | |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | static int __devexit ad5380_remove(struct device *dev) |
| 465 | { |
| 466 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 467 | struct ad5380_state *st = iio_priv(indio_dev); |
| 468 | |
| 469 | iio_device_unregister(indio_dev); |
| 470 | |
| 471 | kfree(indio_dev->channels); |
| 472 | |
| 473 | if (!IS_ERR(st->vref_reg)) { |
| 474 | regulator_disable(st->vref_reg); |
| 475 | regulator_put(st->vref_reg); |
| 476 | } |
| 477 | |
| 478 | regmap_exit(st->regmap); |
| 479 | iio_free_device(indio_dev); |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static bool ad5380_reg_false(struct device *dev, unsigned int reg) |
| 485 | { |
| 486 | return false; |
| 487 | } |
| 488 | |
| 489 | static const struct regmap_config ad5380_regmap_config = { |
| 490 | .reg_bits = 10, |
| 491 | .val_bits = 14, |
| 492 | |
| 493 | .max_register = AD5380_REG_DATA(40), |
| 494 | .cache_type = REGCACHE_RBTREE, |
| 495 | |
| 496 | .volatile_reg = ad5380_reg_false, |
| 497 | .readable_reg = ad5380_reg_false, |
| 498 | }; |
| 499 | |
| 500 | #if IS_ENABLED(CONFIG_SPI_MASTER) |
| 501 | |
| 502 | static int __devinit ad5380_spi_probe(struct spi_device *spi) |
| 503 | { |
| 504 | const struct spi_device_id *id = spi_get_device_id(spi); |
| 505 | struct regmap *regmap; |
| 506 | |
| 507 | regmap = regmap_init_spi(spi, &ad5380_regmap_config); |
| 508 | |
| 509 | if (IS_ERR(regmap)) |
| 510 | return PTR_ERR(regmap); |
| 511 | |
| 512 | return ad5380_probe(&spi->dev, regmap, id->driver_data, id->name); |
| 513 | } |
| 514 | |
| 515 | static int __devexit ad5380_spi_remove(struct spi_device *spi) |
| 516 | { |
| 517 | return ad5380_remove(&spi->dev); |
| 518 | } |
| 519 | |
| 520 | static const struct spi_device_id ad5380_spi_ids[] = { |
| 521 | { "ad5380-3", ID_AD5380_3 }, |
| 522 | { "ad5380-5", ID_AD5380_5 }, |
| 523 | { "ad5381-3", ID_AD5381_3 }, |
| 524 | { "ad5381-5", ID_AD5381_5 }, |
| 525 | { "ad5382-3", ID_AD5382_3 }, |
| 526 | { "ad5382-5", ID_AD5382_5 }, |
| 527 | { "ad5383-3", ID_AD5383_3 }, |
| 528 | { "ad5383-5", ID_AD5383_5 }, |
| 529 | { "ad5384-3", ID_AD5380_3 }, |
| 530 | { "ad5384-5", ID_AD5380_5 }, |
| 531 | { "ad5390-3", ID_AD5390_3 }, |
| 532 | { "ad5390-5", ID_AD5390_5 }, |
| 533 | { "ad5391-3", ID_AD5391_3 }, |
| 534 | { "ad5391-5", ID_AD5391_5 }, |
| 535 | { "ad5392-3", ID_AD5392_3 }, |
| 536 | { "ad5392-5", ID_AD5392_5 }, |
| 537 | { } |
| 538 | }; |
| 539 | MODULE_DEVICE_TABLE(spi, ad5380_spi_ids); |
| 540 | |
| 541 | static struct spi_driver ad5380_spi_driver = { |
| 542 | .driver = { |
| 543 | .name = "ad5380", |
| 544 | .owner = THIS_MODULE, |
| 545 | }, |
| 546 | .probe = ad5380_spi_probe, |
| 547 | .remove = __devexit_p(ad5380_spi_remove), |
| 548 | .id_table = ad5380_spi_ids, |
| 549 | }; |
| 550 | |
| 551 | static inline int ad5380_spi_register_driver(void) |
| 552 | { |
| 553 | return spi_register_driver(&ad5380_spi_driver); |
| 554 | } |
| 555 | |
| 556 | static inline void ad5380_spi_unregister_driver(void) |
| 557 | { |
| 558 | spi_unregister_driver(&ad5380_spi_driver); |
| 559 | } |
| 560 | |
| 561 | #else |
| 562 | |
| 563 | static inline int ad5380_spi_register_driver(void) |
| 564 | { |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static inline void ad5380_spi_unregister_driver(void) |
| 569 | { |
| 570 | } |
| 571 | |
| 572 | #endif |
| 573 | |
| 574 | #if IS_ENABLED(CONFIG_I2C) |
| 575 | |
| 576 | static int __devinit ad5380_i2c_probe(struct i2c_client *i2c, |
| 577 | const struct i2c_device_id *id) |
| 578 | { |
| 579 | struct regmap *regmap; |
| 580 | |
| 581 | regmap = regmap_init_i2c(i2c, &ad5380_regmap_config); |
| 582 | |
| 583 | if (IS_ERR(regmap)) |
| 584 | return PTR_ERR(regmap); |
| 585 | |
| 586 | return ad5380_probe(&i2c->dev, regmap, id->driver_data, id->name); |
| 587 | } |
| 588 | |
| 589 | static int __devexit ad5380_i2c_remove(struct i2c_client *i2c) |
| 590 | { |
| 591 | return ad5380_remove(&i2c->dev); |
| 592 | } |
| 593 | |
| 594 | static const struct i2c_device_id ad5380_i2c_ids[] = { |
| 595 | { "ad5380-3", ID_AD5380_3 }, |
| 596 | { "ad5380-5", ID_AD5380_5 }, |
| 597 | { "ad5381-3", ID_AD5381_3 }, |
| 598 | { "ad5381-5", ID_AD5381_5 }, |
| 599 | { "ad5382-3", ID_AD5382_3 }, |
| 600 | { "ad5382-5", ID_AD5382_5 }, |
| 601 | { "ad5383-3", ID_AD5383_3 }, |
| 602 | { "ad5383-5", ID_AD5383_5 }, |
| 603 | { "ad5384-3", ID_AD5380_3 }, |
| 604 | { "ad5384-5", ID_AD5380_5 }, |
| 605 | { "ad5390-3", ID_AD5390_3 }, |
| 606 | { "ad5390-5", ID_AD5390_5 }, |
| 607 | { "ad5391-3", ID_AD5391_3 }, |
| 608 | { "ad5391-5", ID_AD5391_5 }, |
| 609 | { "ad5392-3", ID_AD5392_3 }, |
| 610 | { "ad5392-5", ID_AD5392_5 }, |
| 611 | { } |
| 612 | }; |
| 613 | MODULE_DEVICE_TABLE(i2c, ad5380_i2c_ids); |
| 614 | |
| 615 | static struct i2c_driver ad5380_i2c_driver = { |
| 616 | .driver = { |
| 617 | .name = "ad5380", |
| 618 | .owner = THIS_MODULE, |
| 619 | }, |
| 620 | .probe = ad5380_i2c_probe, |
| 621 | .remove = __devexit_p(ad5380_i2c_remove), |
| 622 | .id_table = ad5380_i2c_ids, |
| 623 | }; |
| 624 | |
| 625 | static inline int ad5380_i2c_register_driver(void) |
| 626 | { |
| 627 | return i2c_add_driver(&ad5380_i2c_driver); |
| 628 | } |
| 629 | |
| 630 | static inline void ad5380_i2c_unregister_driver(void) |
| 631 | { |
| 632 | i2c_del_driver(&ad5380_i2c_driver); |
| 633 | } |
| 634 | |
| 635 | #else |
| 636 | |
| 637 | static inline int ad5380_i2c_register_driver(void) |
| 638 | { |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static inline void ad5380_i2c_unregister_driver(void) |
| 643 | { |
| 644 | } |
| 645 | |
| 646 | #endif |
| 647 | |
| 648 | static int __init ad5380_spi_init(void) |
| 649 | { |
| 650 | int ret; |
| 651 | |
| 652 | ret = ad5380_spi_register_driver(); |
| 653 | if (ret) |
| 654 | return ret; |
| 655 | |
| 656 | ret = ad5380_i2c_register_driver(); |
| 657 | if (ret) { |
| 658 | ad5380_spi_unregister_driver(); |
| 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | module_init(ad5380_spi_init); |
| 665 | |
| 666 | static void __exit ad5380_spi_exit(void) |
| 667 | { |
| 668 | ad5380_i2c_unregister_driver(); |
| 669 | ad5380_spi_unregister_driver(); |
| 670 | |
| 671 | } |
| 672 | module_exit(ad5380_spi_exit); |
| 673 | |
| 674 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
| 675 | MODULE_DESCRIPTION("Analog Devices AD5380/81/82/83/84/90/91/92 DAC"); |
| 676 | MODULE_LICENSE("GPL v2"); |