Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'iio-for-3.17b' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into staging-next

Jonathan writes:

Second round of new drivers and cleanups for IIO in the 3.17 cycle.

New drivers
* mcp4902, mcp4912 and mcp4922 SPI DAC driver.
* max1027, max1029 and max1031 SPI ADC driver.

Cleanups
* cm32181 - use devm APIs to simplify error paths.
* ak8975 - use devm APIs to simplify error paths.
* ad9850 - drop some unused defines and an unnecessary goto.
* hmc5843 - add missing devices to the device id table and the documentation.
* ad9832 - small formatting cleanups.
* sca3000 - hide direct use of the stufftoread element by adding a
data_available function. This is a precursor for the addition of buffer
watermarks to the subsystem but stands as a good cleanup on its own.

+814 -73
+22
Documentation/devicetree/bindings/iio/adc/max1027-adc.txt
··· 1 + * Maxim 1027/1029/1031 Analog to Digital Converter (ADC) 2 + 3 + Required properties: 4 + - compatible: Should be "maxim,max1027" or "maxim,max1029" or "maxim,max1031" 5 + - reg: SPI chip select number for the device 6 + - interrupt-parent: phandle to the parent interrupt controller 7 + see: Documentation/devicetree/bindings/interrupt-controller/interrupts.txt 8 + - interrupts: IRQ line for the ADC 9 + see: Documentation/devicetree/bindings/interrupt-controller/interrupts.txt 10 + 11 + Recommended properties: 12 + - spi-max-frequency: Definition as per 13 + Documentation/devicetree/bindings/spi/spi-bus.txt 14 + 15 + Example: 16 + adc@0 { 17 + compatible = "maxim,max1027"; 18 + reg = <0>; 19 + interrupt-parent = <&gpio5>; 20 + interrupts = <15 IRQ_TYPE_EDGE_RISING>; 21 + spi-max-frequency = <1000000>; 22 + };
+3
Documentation/devicetree/bindings/iio/magnetometer/hmc5843.txt
··· 3 3 Required properties: 4 4 5 5 - compatible : should be "honeywell,hmc5843" 6 + Other models which are supported with driver are: 7 + "honeywell,hmc5883" 8 + "honeywell,hmc5883l" 6 9 - reg : the I2C address of the magnetometer - typically 0x1e 7 10 8 11 Optional properties:
+9
drivers/iio/adc/Kconfig
··· 131 131 help 132 132 Say yes here to build support for TI LP8788 ADC. 133 133 134 + config MAX1027 135 + tristate "Maxim max1027 ADC driver" 136 + depends on SPI 137 + select IIO_BUFFER 138 + select IIO_TRIGGERED_BUFFER 139 + help 140 + Say yes here to build support for Maxim SPI ADC models 141 + max1027, max1029 and max1031. 142 + 134 143 config MAX1363 135 144 tristate "Maxim max1363 ADC driver" 136 145 depends on I2C
+1
drivers/iio/adc/Makefile
··· 15 15 obj-$(CONFIG_AT91_ADC) += at91_adc.o 16 16 obj-$(CONFIG_EXYNOS_ADC) += exynos_adc.o 17 17 obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o 18 + obj-$(CONFIG_MAX1027) += max1027.o 18 19 obj-$(CONFIG_MAX1363) += max1363.o 19 20 obj-$(CONFIG_MCP320X) += mcp320x.o 20 21 obj-$(CONFIG_MCP3422) += mcp3422.o
+521
drivers/iio/adc/max1027.c
··· 1 + /* 2 + * iio/adc/max1027.c 3 + * Copyright (C) 2014 Philippe Reynes 4 + * 5 + * based on linux/drivers/iio/ad7923.c 6 + * Copyright 2011 Analog Devices Inc (from AD7923 Driver) 7 + * Copyright 2012 CS Systemes d'Information 8 + * 9 + * This program is free software; you can redistribute it and/or modify 10 + * it under the terms of the GNU General Public License version 2 as 11 + * published by the Free Software Foundation. 12 + * 13 + * max1027.c 14 + * 15 + * Partial support for max1027 and similar chips. 16 + */ 17 + 18 + #include <linux/kernel.h> 19 + #include <linux/module.h> 20 + #include <linux/spi/spi.h> 21 + #include <linux/delay.h> 22 + 23 + #include <linux/iio/iio.h> 24 + #include <linux/iio/buffer.h> 25 + #include <linux/iio/trigger.h> 26 + #include <linux/iio/trigger_consumer.h> 27 + #include <linux/iio/triggered_buffer.h> 28 + 29 + #define MAX1027_CONV_REG BIT(7) 30 + #define MAX1027_SETUP_REG BIT(6) 31 + #define MAX1027_AVG_REG BIT(5) 32 + #define MAX1027_RST_REG BIT(4) 33 + 34 + /* conversion register */ 35 + #define MAX1027_TEMP BIT(0) 36 + #define MAX1027_SCAN_0_N (0x00 << 1) 37 + #define MAX1027_SCAN_N_M (0x01 << 1) 38 + #define MAX1027_SCAN_N (0x02 << 1) 39 + #define MAX1027_NOSCAN (0x03 << 1) 40 + #define MAX1027_CHAN(n) ((n) << 3) 41 + 42 + /* setup register */ 43 + #define MAX1027_UNIPOLAR 0x02 44 + #define MAX1027_BIPOLAR 0x03 45 + #define MAX1027_REF_MODE0 (0x00 << 2) 46 + #define MAX1027_REF_MODE1 (0x01 << 2) 47 + #define MAX1027_REF_MODE2 (0x02 << 2) 48 + #define MAX1027_REF_MODE3 (0x03 << 2) 49 + #define MAX1027_CKS_MODE0 (0x00 << 4) 50 + #define MAX1027_CKS_MODE1 (0x01 << 4) 51 + #define MAX1027_CKS_MODE2 (0x02 << 4) 52 + #define MAX1027_CKS_MODE3 (0x03 << 4) 53 + 54 + /* averaging register */ 55 + #define MAX1027_NSCAN_4 0x00 56 + #define MAX1027_NSCAN_8 0x01 57 + #define MAX1027_NSCAN_12 0x02 58 + #define MAX1027_NSCAN_16 0x03 59 + #define MAX1027_NAVG_4 (0x00 << 2) 60 + #define MAX1027_NAVG_8 (0x01 << 2) 61 + #define MAX1027_NAVG_16 (0x02 << 2) 62 + #define MAX1027_NAVG_32 (0x03 << 2) 63 + #define MAX1027_AVG_EN BIT(4) 64 + 65 + enum max1027_id { 66 + max1027, 67 + max1029, 68 + max1031, 69 + }; 70 + 71 + static const struct spi_device_id max1027_id[] = { 72 + {"max1027", max1027}, 73 + {"max1029", max1029}, 74 + {"max1031", max1031}, 75 + {} 76 + }; 77 + MODULE_DEVICE_TABLE(spi, max1027_id); 78 + 79 + #ifdef CONFIG_OF 80 + static const struct of_device_id max1027_adc_dt_ids[] = { 81 + { .compatible = "maxim,max1027" }, 82 + { .compatible = "maxim,max1029" }, 83 + { .compatible = "maxim,max1031" }, 84 + {}, 85 + }; 86 + MODULE_DEVICE_TABLE(of, max1027_adc_dt_ids); 87 + #endif 88 + 89 + #define MAX1027_V_CHAN(index) \ 90 + { \ 91 + .type = IIO_VOLTAGE, \ 92 + .indexed = 1, \ 93 + .channel = index, \ 94 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 95 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 96 + .scan_index = index + 1, \ 97 + .scan_type = { \ 98 + .sign = 'u', \ 99 + .realbits = 10, \ 100 + .storagebits = 16, \ 101 + .shift = 2, \ 102 + .endianness = IIO_BE, \ 103 + }, \ 104 + } 105 + 106 + #define MAX1027_T_CHAN \ 107 + { \ 108 + .type = IIO_TEMP, \ 109 + .channel = 0, \ 110 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 111 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 112 + .scan_index = 0, \ 113 + .scan_type = { \ 114 + .sign = 'u', \ 115 + .realbits = 12, \ 116 + .storagebits = 16, \ 117 + .endianness = IIO_BE, \ 118 + }, \ 119 + } 120 + 121 + static const struct iio_chan_spec max1027_channels[] = { 122 + MAX1027_T_CHAN, 123 + MAX1027_V_CHAN(0), 124 + MAX1027_V_CHAN(1), 125 + MAX1027_V_CHAN(2), 126 + MAX1027_V_CHAN(3), 127 + MAX1027_V_CHAN(4), 128 + MAX1027_V_CHAN(5), 129 + MAX1027_V_CHAN(6), 130 + MAX1027_V_CHAN(7) 131 + }; 132 + 133 + static const struct iio_chan_spec max1029_channels[] = { 134 + MAX1027_T_CHAN, 135 + MAX1027_V_CHAN(0), 136 + MAX1027_V_CHAN(1), 137 + MAX1027_V_CHAN(2), 138 + MAX1027_V_CHAN(3), 139 + MAX1027_V_CHAN(4), 140 + MAX1027_V_CHAN(5), 141 + MAX1027_V_CHAN(6), 142 + MAX1027_V_CHAN(7), 143 + MAX1027_V_CHAN(8), 144 + MAX1027_V_CHAN(9), 145 + MAX1027_V_CHAN(10), 146 + MAX1027_V_CHAN(11) 147 + }; 148 + 149 + static const struct iio_chan_spec max1031_channels[] = { 150 + MAX1027_T_CHAN, 151 + MAX1027_V_CHAN(0), 152 + MAX1027_V_CHAN(1), 153 + MAX1027_V_CHAN(2), 154 + MAX1027_V_CHAN(3), 155 + MAX1027_V_CHAN(4), 156 + MAX1027_V_CHAN(5), 157 + MAX1027_V_CHAN(6), 158 + MAX1027_V_CHAN(7), 159 + MAX1027_V_CHAN(8), 160 + MAX1027_V_CHAN(9), 161 + MAX1027_V_CHAN(10), 162 + MAX1027_V_CHAN(11), 163 + MAX1027_V_CHAN(12), 164 + MAX1027_V_CHAN(13), 165 + MAX1027_V_CHAN(14), 166 + MAX1027_V_CHAN(15) 167 + }; 168 + 169 + static const unsigned long max1027_available_scan_masks[] = { 170 + 0x000001ff, 171 + 0x00000000, 172 + }; 173 + 174 + static const unsigned long max1029_available_scan_masks[] = { 175 + 0x00001fff, 176 + 0x00000000, 177 + }; 178 + 179 + static const unsigned long max1031_available_scan_masks[] = { 180 + 0x0001ffff, 181 + 0x00000000, 182 + }; 183 + 184 + struct max1027_chip_info { 185 + const struct iio_chan_spec *channels; 186 + unsigned int num_channels; 187 + const unsigned long *available_scan_masks; 188 + }; 189 + 190 + static const struct max1027_chip_info max1027_chip_info_tbl[] = { 191 + [max1027] = { 192 + .channels = max1027_channels, 193 + .num_channels = ARRAY_SIZE(max1027_channels), 194 + .available_scan_masks = max1027_available_scan_masks, 195 + }, 196 + [max1029] = { 197 + .channels = max1029_channels, 198 + .num_channels = ARRAY_SIZE(max1029_channels), 199 + .available_scan_masks = max1029_available_scan_masks, 200 + }, 201 + [max1031] = { 202 + .channels = max1031_channels, 203 + .num_channels = ARRAY_SIZE(max1031_channels), 204 + .available_scan_masks = max1031_available_scan_masks, 205 + }, 206 + }; 207 + 208 + struct max1027_state { 209 + const struct max1027_chip_info *info; 210 + struct spi_device *spi; 211 + struct iio_trigger *trig; 212 + __be16 *buffer; 213 + struct mutex lock; 214 + 215 + u8 reg ____cacheline_aligned; 216 + }; 217 + 218 + static int max1027_read_single_value(struct iio_dev *indio_dev, 219 + struct iio_chan_spec const *chan, 220 + int *val) 221 + { 222 + int ret; 223 + struct max1027_state *st = iio_priv(indio_dev); 224 + 225 + if (iio_buffer_enabled(indio_dev)) { 226 + dev_warn(&indio_dev->dev, "trigger mode already enabled"); 227 + return -EBUSY; 228 + } 229 + 230 + /* Start acquisition on conversion register write */ 231 + st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2 | MAX1027_CKS_MODE2; 232 + ret = spi_write(st->spi, &st->reg, 1); 233 + if (ret < 0) { 234 + dev_err(&indio_dev->dev, 235 + "Failed to configure setup register\n"); 236 + return ret; 237 + } 238 + 239 + /* Configure conversion register with the requested chan */ 240 + st->reg = MAX1027_CONV_REG | MAX1027_CHAN(chan->channel) | 241 + MAX1027_NOSCAN | !!(chan->type == IIO_TEMP); 242 + ret = spi_write(st->spi, &st->reg, 1); 243 + if (ret < 0) { 244 + dev_err(&indio_dev->dev, 245 + "Failed to configure conversion register\n"); 246 + return ret; 247 + } 248 + 249 + /* 250 + * For an unknown reason, when we use the mode "10" (write 251 + * conversion register), the interrupt doesn't occur every time. 252 + * So we just wait 1 ms. 253 + */ 254 + mdelay(1); 255 + 256 + /* Read result */ 257 + ret = spi_read(st->spi, st->buffer, (chan->type == IIO_TEMP) ? 4 : 2); 258 + if (ret < 0) 259 + return ret; 260 + 261 + *val = be16_to_cpu(st->buffer[0]); 262 + 263 + return IIO_VAL_INT; 264 + } 265 + 266 + static int max1027_read_raw(struct iio_dev *indio_dev, 267 + struct iio_chan_spec const *chan, 268 + int *val, int *val2, long mask) 269 + { 270 + int ret = 0; 271 + struct max1027_state *st = iio_priv(indio_dev); 272 + 273 + mutex_lock(&st->lock); 274 + 275 + switch (mask) { 276 + case IIO_CHAN_INFO_RAW: 277 + ret = max1027_read_single_value(indio_dev, chan, val); 278 + break; 279 + case IIO_CHAN_INFO_SCALE: 280 + switch (chan->type) { 281 + case IIO_TEMP: 282 + *val = 1; 283 + *val2 = 8; 284 + ret = IIO_VAL_FRACTIONAL; 285 + break; 286 + case IIO_VOLTAGE: 287 + *val = 2500; 288 + *val2 = 10; 289 + ret = IIO_VAL_FRACTIONAL_LOG2; 290 + break; 291 + default: 292 + ret = -EINVAL; 293 + break; 294 + } 295 + break; 296 + default: 297 + ret = -EINVAL; 298 + break; 299 + } 300 + 301 + mutex_unlock(&st->lock); 302 + 303 + return ret; 304 + } 305 + 306 + static int max1027_debugfs_reg_access(struct iio_dev *indio_dev, 307 + unsigned reg, unsigned writeval, 308 + unsigned *readval) 309 + { 310 + struct max1027_state *st = iio_priv(indio_dev); 311 + u8 *val = (u8 *)st->buffer; 312 + 313 + if (readval != NULL) 314 + return -EINVAL; 315 + 316 + *val = (u8)writeval; 317 + return spi_write(st->spi, val, 1); 318 + } 319 + 320 + static int max1027_validate_trigger(struct iio_dev *indio_dev, 321 + struct iio_trigger *trig) 322 + { 323 + struct max1027_state *st = iio_priv(indio_dev); 324 + 325 + if (st->trig != trig) 326 + return -EINVAL; 327 + 328 + return 0; 329 + } 330 + 331 + static int max1027_set_trigger_state(struct iio_trigger *trig, bool state) 332 + { 333 + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 334 + struct max1027_state *st = iio_priv(indio_dev); 335 + int ret; 336 + 337 + if (state) { 338 + /* Start acquisition on cnvst */ 339 + st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 | 340 + MAX1027_REF_MODE2; 341 + ret = spi_write(st->spi, &st->reg, 1); 342 + if (ret < 0) 343 + return ret; 344 + 345 + /* Scan from 0 to max */ 346 + st->reg = MAX1027_CONV_REG | MAX1027_CHAN(0) | 347 + MAX1027_SCAN_N_M | MAX1027_TEMP; 348 + ret = spi_write(st->spi, &st->reg, 1); 349 + if (ret < 0) 350 + return ret; 351 + } else { 352 + /* Start acquisition on conversion register write */ 353 + st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE2 | 354 + MAX1027_REF_MODE2; 355 + ret = spi_write(st->spi, &st->reg, 1); 356 + if (ret < 0) 357 + return ret; 358 + } 359 + 360 + return 0; 361 + } 362 + 363 + static int max1027_validate_device(struct iio_trigger *trig, 364 + struct iio_dev *indio_dev) 365 + { 366 + struct iio_dev *indio = iio_trigger_get_drvdata(trig); 367 + 368 + if (indio != indio_dev) 369 + return -EINVAL; 370 + 371 + return 0; 372 + } 373 + 374 + static irqreturn_t max1027_trigger_handler(int irq, void *private) 375 + { 376 + struct iio_poll_func *pf = (struct iio_poll_func *)private; 377 + struct iio_dev *indio_dev = pf->indio_dev; 378 + struct max1027_state *st = iio_priv(indio_dev); 379 + 380 + pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private); 381 + 382 + /* fill buffer with all channel */ 383 + spi_read(st->spi, st->buffer, indio_dev->masklength * 2); 384 + 385 + iio_push_to_buffers(indio_dev, st->buffer); 386 + 387 + iio_trigger_notify_done(indio_dev->trig); 388 + 389 + return IRQ_HANDLED; 390 + } 391 + 392 + static const struct iio_trigger_ops max1027_trigger_ops = { 393 + .owner = THIS_MODULE, 394 + .validate_device = &max1027_validate_device, 395 + .set_trigger_state = &max1027_set_trigger_state, 396 + }; 397 + 398 + static const struct iio_info max1027_info = { 399 + .driver_module = THIS_MODULE, 400 + .read_raw = &max1027_read_raw, 401 + .validate_trigger = &max1027_validate_trigger, 402 + .debugfs_reg_access = &max1027_debugfs_reg_access, 403 + }; 404 + 405 + static int max1027_probe(struct spi_device *spi) 406 + { 407 + int ret; 408 + struct iio_dev *indio_dev; 409 + struct max1027_state *st; 410 + 411 + pr_debug("%s: probe(spi = 0x%p)\n", __func__, spi); 412 + 413 + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 414 + if (indio_dev == NULL) { 415 + pr_err("Can't allocate iio device\n"); 416 + return -ENOMEM; 417 + } 418 + 419 + spi_set_drvdata(spi, indio_dev); 420 + 421 + st = iio_priv(indio_dev); 422 + st->spi = spi; 423 + st->info = &max1027_chip_info_tbl[spi_get_device_id(spi)->driver_data]; 424 + 425 + mutex_init(&st->lock); 426 + 427 + indio_dev->name = spi_get_device_id(spi)->name; 428 + indio_dev->dev.parent = &spi->dev; 429 + indio_dev->info = &max1027_info; 430 + indio_dev->modes = INDIO_DIRECT_MODE; 431 + indio_dev->channels = st->info->channels; 432 + indio_dev->num_channels = st->info->num_channels; 433 + indio_dev->available_scan_masks = st->info->available_scan_masks; 434 + 435 + st->buffer = devm_kmalloc(&indio_dev->dev, 436 + indio_dev->num_channels * 2, 437 + GFP_KERNEL); 438 + if (st->buffer == NULL) { 439 + dev_err(&indio_dev->dev, "Can't allocate bufffer\n"); 440 + return -ENOMEM; 441 + } 442 + 443 + ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 444 + &max1027_trigger_handler, NULL); 445 + if (ret < 0) { 446 + dev_err(&indio_dev->dev, "Failed to setup buffer\n"); 447 + return ret; 448 + } 449 + 450 + st->trig = devm_iio_trigger_alloc(&spi->dev, "%s-trigger", 451 + indio_dev->name); 452 + if (st->trig == NULL) { 453 + ret = -ENOMEM; 454 + dev_err(&indio_dev->dev, "Failed to allocate iio trigger\n"); 455 + goto fail_trigger_alloc; 456 + } 457 + 458 + st->trig->ops = &max1027_trigger_ops; 459 + st->trig->dev.parent = &spi->dev; 460 + iio_trigger_set_drvdata(st->trig, indio_dev); 461 + iio_trigger_register(st->trig); 462 + 463 + ret = devm_request_threaded_irq(&spi->dev, spi->irq, 464 + iio_trigger_generic_data_rdy_poll, 465 + NULL, 466 + IRQF_TRIGGER_FALLING, 467 + spi->dev.driver->name, st->trig); 468 + if (ret < 0) { 469 + dev_err(&indio_dev->dev, "Failed to allocate IRQ.\n"); 470 + goto fail_dev_register; 471 + } 472 + 473 + /* Disable averaging */ 474 + st->reg = MAX1027_AVG_REG; 475 + ret = spi_write(st->spi, &st->reg, 1); 476 + if (ret < 0) { 477 + dev_err(&indio_dev->dev, "Failed to configure averaging register\n"); 478 + goto fail_dev_register; 479 + } 480 + 481 + ret = iio_device_register(indio_dev); 482 + if (ret < 0) { 483 + dev_err(&indio_dev->dev, "Failed to register iio device\n"); 484 + goto fail_dev_register; 485 + } 486 + 487 + return 0; 488 + 489 + fail_dev_register: 490 + fail_trigger_alloc: 491 + iio_triggered_buffer_cleanup(indio_dev); 492 + 493 + return ret; 494 + } 495 + 496 + static int max1027_remove(struct spi_device *spi) 497 + { 498 + struct iio_dev *indio_dev = spi_get_drvdata(spi); 499 + 500 + pr_debug("%s: remove(spi = 0x%p)\n", __func__, spi); 501 + 502 + iio_device_unregister(indio_dev); 503 + iio_triggered_buffer_cleanup(indio_dev); 504 + 505 + return 0; 506 + } 507 + 508 + static struct spi_driver max1027_driver = { 509 + .driver = { 510 + .name = "max1027", 511 + .owner = THIS_MODULE, 512 + }, 513 + .probe = max1027_probe, 514 + .remove = max1027_remove, 515 + .id_table = max1027_id, 516 + }; 517 + module_spi_driver(max1027_driver); 518 + 519 + MODULE_AUTHOR("Philippe Reynes <tremyfr@yahoo.fr>"); 520 + MODULE_DESCRIPTION("MAX1027/MAX1029/MAX1031 ADC"); 521 + MODULE_LICENSE("GPL v2");
+10
drivers/iio/dac/Kconfig
··· 163 163 To compile this driver as a module, choose M here: the module 164 164 will be called mcp4725. 165 165 166 + config MCP4922 167 + tristate "MCP4902, MCP4912, MCP4922 DAC driver" 168 + depends on SPI 169 + help 170 + Say yes here to build the driver for the Microchip MCP4902 171 + MCP4912, and MCP4922 DAC devices. 172 + 173 + To compile this driver as a module, choose M here: the module 174 + will be called mcp4922. 175 + 166 176 endmenu
+1
drivers/iio/dac/Makefile
··· 18 18 obj-$(CONFIG_AD7303) += ad7303.o 19 19 obj-$(CONFIG_MAX517) += max517.o 20 20 obj-$(CONFIG_MCP4725) += mcp4725.o 21 + obj-$(CONFIG_MCP4922) += mcp4922.o
+216
drivers/iio/dac/mcp4922.c
··· 1 + /* 2 + * mcp4922.c 3 + * 4 + * Driver for Microchip Digital to Analog Converters. 5 + * Supports MCP4902, MCP4912, and MCP4922. 6 + * 7 + * Copyright (c) 2014 EMAC Inc. 8 + * 9 + * This program is free software; you can redistribute it and/or modify 10 + * it under the terms of the GNU General Public License as published by 11 + * the Free Software Foundation; either version 2 of the License, or 12 + * (at your option) any later version. 13 + * 14 + * This program is distributed in the hope that it will be useful, 15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 + * GNU General Public License for more details. 18 + * 19 + */ 20 + 21 + #include <linux/module.h> 22 + #include <linux/init.h> 23 + #include <linux/spi/spi.h> 24 + #include <linux/iio/iio.h> 25 + #include <linux/iio/sysfs.h> 26 + #include <linux/regulator/consumer.h> 27 + #include <linux/bitops.h> 28 + 29 + #define MCP4922_NUM_CHANNELS 2 30 + 31 + enum mcp4922_supported_device_ids { 32 + ID_MCP4902, 33 + ID_MCP4912, 34 + ID_MCP4922, 35 + }; 36 + 37 + struct mcp4922_state { 38 + struct spi_device *spi; 39 + unsigned int value[MCP4922_NUM_CHANNELS]; 40 + unsigned int vref_mv; 41 + struct regulator *vref_reg; 42 + u8 mosi[2] ____cacheline_aligned; 43 + }; 44 + 45 + #define MCP4922_CHAN(chan, bits) { \ 46 + .type = IIO_VOLTAGE, \ 47 + .output = 1, \ 48 + .indexed = 1, \ 49 + .channel = chan, \ 50 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 51 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 52 + .scan_type = { \ 53 + .sign = 'u', \ 54 + .realbits = (bits), \ 55 + .storagebits = 16, \ 56 + .shift = 12 - (bits), \ 57 + }, \ 58 + } 59 + 60 + static int mcp4922_spi_write(struct mcp4922_state *state, u8 addr, u32 val) 61 + { 62 + state->mosi[1] = val & 0xff; 63 + state->mosi[0] = (addr == 0) ? 0x00 : 0x80; 64 + state->mosi[0] |= 0x30 | ((val >> 8) & 0x0f); 65 + 66 + return spi_write(state->spi, state->mosi, 2); 67 + } 68 + 69 + static int mcp4922_read_raw(struct iio_dev *indio_dev, 70 + struct iio_chan_spec const *chan, 71 + int *val, 72 + int *val2, 73 + long mask) 74 + { 75 + struct mcp4922_state *state = iio_priv(indio_dev); 76 + 77 + switch (mask) { 78 + case IIO_CHAN_INFO_RAW: 79 + *val = state->value[chan->channel]; 80 + return IIO_VAL_INT; 81 + case IIO_CHAN_INFO_SCALE: 82 + *val = state->vref_mv; 83 + *val2 = chan->scan_type.realbits; 84 + return IIO_VAL_FRACTIONAL_LOG2; 85 + default: 86 + return -EINVAL; 87 + } 88 + } 89 + 90 + static int mcp4922_write_raw(struct iio_dev *indio_dev, 91 + struct iio_chan_spec const *chan, 92 + int val, 93 + int val2, 94 + long mask) 95 + { 96 + struct mcp4922_state *state = iio_priv(indio_dev); 97 + 98 + if (val2 != 0) 99 + return -EINVAL; 100 + 101 + switch (mask) { 102 + case IIO_CHAN_INFO_RAW: 103 + if (val > GENMASK(chan->scan_type.realbits-1, 0)) 104 + return -EINVAL; 105 + val <<= chan->scan_type.shift; 106 + state->value[chan->channel] = val; 107 + return mcp4922_spi_write(state, chan->channel, val); 108 + default: 109 + return -EINVAL; 110 + } 111 + } 112 + 113 + static const struct iio_chan_spec mcp4922_channels[3][MCP4922_NUM_CHANNELS] = { 114 + [ID_MCP4902] = { MCP4922_CHAN(0, 8), MCP4922_CHAN(1, 8) }, 115 + [ID_MCP4912] = { MCP4922_CHAN(0, 10), MCP4922_CHAN(1, 10) }, 116 + [ID_MCP4922] = { MCP4922_CHAN(0, 12), MCP4922_CHAN(1, 12) }, 117 + }; 118 + 119 + static const struct iio_info mcp4922_info = { 120 + .read_raw = &mcp4922_read_raw, 121 + .write_raw = &mcp4922_write_raw, 122 + .driver_module = THIS_MODULE, 123 + }; 124 + 125 + static int mcp4922_probe(struct spi_device *spi) 126 + { 127 + struct iio_dev *indio_dev; 128 + struct mcp4922_state *state; 129 + const struct spi_device_id *id; 130 + int ret; 131 + 132 + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state)); 133 + if (indio_dev == NULL) 134 + return -ENOMEM; 135 + 136 + state = iio_priv(indio_dev); 137 + state->spi = spi; 138 + state->vref_reg = devm_regulator_get(&spi->dev, "vref"); 139 + if (IS_ERR(state->vref_reg)) { 140 + dev_err(&spi->dev, "Vref regulator not specified\n"); 141 + return PTR_ERR(state->vref_reg); 142 + } 143 + 144 + ret = regulator_enable(state->vref_reg); 145 + if (ret) { 146 + dev_err(&spi->dev, "Failed to enable vref regulator: %d\n", 147 + ret); 148 + return ret; 149 + } 150 + 151 + ret = regulator_get_voltage(state->vref_reg); 152 + if (ret < 0) { 153 + dev_err(&spi->dev, "Failed to read vref regulator: %d\n", 154 + ret); 155 + goto error_disable_reg; 156 + } 157 + state->vref_mv = ret / 1000; 158 + 159 + spi_set_drvdata(spi, indio_dev); 160 + id = spi_get_device_id(spi); 161 + indio_dev->dev.parent = &spi->dev; 162 + indio_dev->info = &mcp4922_info; 163 + indio_dev->modes = INDIO_DIRECT_MODE; 164 + indio_dev->channels = mcp4922_channels[id->driver_data]; 165 + indio_dev->num_channels = MCP4922_NUM_CHANNELS; 166 + indio_dev->name = id->name; 167 + 168 + ret = iio_device_register(indio_dev); 169 + if (ret) { 170 + dev_err(&spi->dev, "Failed to register iio device: %d\n", 171 + ret); 172 + goto error_disable_reg; 173 + } 174 + 175 + return 0; 176 + 177 + error_disable_reg: 178 + regulator_disable(state->vref_reg); 179 + 180 + return ret; 181 + } 182 + 183 + static int mcp4922_remove(struct spi_device *spi) 184 + { 185 + struct iio_dev *indio_dev = spi_get_drvdata(spi); 186 + struct mcp4922_state *state; 187 + 188 + iio_device_unregister(indio_dev); 189 + state = iio_priv(indio_dev); 190 + regulator_disable(state->vref_reg); 191 + 192 + return 0; 193 + } 194 + 195 + static const struct spi_device_id mcp4922_id[] = { 196 + {"mcp4902", ID_MCP4902}, 197 + {"mcp4912", ID_MCP4912}, 198 + {"mcp4922", ID_MCP4922}, 199 + {} 200 + }; 201 + MODULE_DEVICE_TABLE(spi, mcp4922_id); 202 + 203 + static struct spi_driver mcp4922_driver = { 204 + .driver = { 205 + .name = "mcp4922", 206 + .owner = THIS_MODULE, 207 + }, 208 + .probe = mcp4922_probe, 209 + .remove = mcp4922_remove, 210 + .id_table = mcp4922_id, 211 + }; 212 + module_spi_driver(mcp4922_driver); 213 + 214 + MODULE_AUTHOR("Michael Welling <mwelling@ieee.org>"); 215 + MODULE_DESCRIPTION("Microchip MCP4902, MCP4912, MCP4922 DAC"); 216 + MODULE_LICENSE("GPL v2");
+1 -4
drivers/iio/industrialio-buffer.c
··· 39 39 40 40 static bool iio_buffer_data_available(struct iio_buffer *buf) 41 41 { 42 - if (buf->access->data_available) 43 - return buf->access->data_available(buf); 44 - 45 - return buf->stufftoread; 42 + return buf->access->data_available(buf); 46 43 } 47 44 48 45 /**
+1 -10
drivers/iio/light/cm32181.c
··· 331 331 return ret; 332 332 } 333 333 334 - ret = iio_device_register(indio_dev); 334 + ret = devm_iio_device_register(&client->dev, indio_dev); 335 335 if (ret) { 336 336 dev_err(&client->dev, 337 337 "%s: regist device failed\n", ··· 339 339 return ret; 340 340 } 341 341 342 - return 0; 343 - } 344 - 345 - static int cm32181_remove(struct i2c_client *client) 346 - { 347 - struct iio_dev *indio_dev = i2c_get_clientdata(client); 348 - 349 - iio_device_unregister(indio_dev); 350 342 return 0; 351 343 } 352 344 ··· 362 370 }, 363 371 .id_table = cm32181_id, 364 372 .probe = cm32181_probe, 365 - .remove = cm32181_remove, 366 373 }; 367 374 368 375 module_i2c_driver(cm32181_driver);
+14 -44
drivers/iio/magnetometer/ak8975.c
··· 165 165 else 166 166 irq = gpio_to_irq(data->eoc_gpio); 167 167 168 - rc = request_irq(irq, ak8975_irq_handler, 168 + rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler, 169 169 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 170 170 dev_name(&client->dev), data); 171 171 if (rc < 0) { ··· 513 513 /* We may not have a GPIO based IRQ to scan, that is fine, we will 514 514 poll if so */ 515 515 if (gpio_is_valid(eoc_gpio)) { 516 - err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975"); 516 + err = devm_gpio_request_one(&client->dev, eoc_gpio, 517 + GPIOF_IN, "ak_8975"); 517 518 if (err < 0) { 518 519 dev_err(&client->dev, 519 520 "failed to request GPIO %d, error %d\n", 520 521 eoc_gpio, err); 521 - goto exit; 522 + return err; 522 523 } 523 524 } 524 525 525 526 /* Register with IIO */ 526 - indio_dev = iio_device_alloc(sizeof(*data)); 527 - if (indio_dev == NULL) { 528 - err = -ENOMEM; 529 - goto exit_gpio; 530 - } 527 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 528 + if (indio_dev == NULL) 529 + return -ENOMEM; 530 + 531 531 data = iio_priv(indio_dev); 532 532 i2c_set_clientdata(client, indio_dev); 533 533 ··· 542 542 name = (char *) id->name; 543 543 } else if (ACPI_HANDLE(&client->dev)) 544 544 name = ak8975_match_acpi_device(&client->dev, &data->chipset); 545 - else { 546 - err = -ENOSYS; 547 - goto exit_free_iio; 548 - } 545 + else 546 + return -ENOSYS; 547 + 549 548 dev_dbg(&client->dev, "Asahi compass chip %s\n", name); 550 549 551 550 /* Perform some basic start-of-day setup of the device. */ 552 551 err = ak8975_setup(client); 553 552 if (err < 0) { 554 553 dev_err(&client->dev, "AK8975 initialization fails\n"); 555 - goto exit_free_iio; 554 + return err; 556 555 } 557 556 558 557 data->client = client; ··· 563 564 indio_dev->info = &ak8975_info; 564 565 indio_dev->modes = INDIO_DIRECT_MODE; 565 566 indio_dev->name = name; 566 - err = iio_device_register(indio_dev); 567 + err = devm_iio_device_register(&client->dev, indio_dev); 567 568 if (err < 0) 568 - goto exit_free_iio; 569 - 570 - return 0; 571 - 572 - exit_free_iio: 573 - iio_device_free(indio_dev); 574 - if (data->eoc_irq) 575 - free_irq(data->eoc_irq, data); 576 - exit_gpio: 577 - if (gpio_is_valid(eoc_gpio)) 578 - gpio_free(eoc_gpio); 579 - exit: 580 - return err; 581 - } 582 - 583 - static int ak8975_remove(struct i2c_client *client) 584 - { 585 - struct iio_dev *indio_dev = i2c_get_clientdata(client); 586 - struct ak8975_data *data = iio_priv(indio_dev); 587 - 588 - iio_device_unregister(indio_dev); 589 - 590 - if (data->eoc_irq) 591 - free_irq(data->eoc_irq, data); 592 - 593 - if (gpio_is_valid(data->eoc_gpio)) 594 - gpio_free(data->eoc_gpio); 595 - 596 - iio_device_free(indio_dev); 569 + return err; 597 570 598 571 return 0; 599 572 } ··· 592 621 .acpi_match_table = ACPI_PTR(ak_acpi_match), 593 622 }, 594 623 .probe = ak8975_probe, 595 - .remove = ak8975_remove, 596 624 .id_table = ak8975_id, 597 625 }; 598 626 module_i2c_driver(ak8975_driver);
+6
drivers/staging/iio/accel/sca3000_ring.c
··· 141 141 return 6; 142 142 } 143 143 144 + static bool sca3000_ring_buf_data_available(struct iio_buffer *r) 145 + { 146 + return r->stufftoread; 147 + } 148 + 144 149 static IIO_BUFFER_ENABLE_ATTR; 145 150 static IIO_BUFFER_LENGTH_ATTR; 146 151 ··· 279 274 .read_first_n = &sca3000_read_first_n_hw_rb, 280 275 .get_length = &sca3000_ring_get_length, 281 276 .get_bytes_per_datum = &sca3000_ring_get_bytes_per_datum, 277 + .data_available = sca3000_ring_buf_data_available, 282 278 .release = sca3000_ring_release, 283 279 }; 284 280
+6 -8
drivers/staging/iio/frequency/ad9832.c
··· 57 57 } 58 58 59 59 static int ad9832_write_phase(struct ad9832_state *st, 60 - unsigned long addr, unsigned long phase) 60 + unsigned long addr, unsigned long phase) 61 61 { 62 62 if (phase > (1 << AD9832_PHASE_BITS)) 63 63 return -EINVAL; ··· 72 72 return spi_sync(st->spi, &st->phase_msg); 73 73 } 74 74 75 - static ssize_t ad9832_write(struct device *dev, 76 - struct device_attribute *attr, 77 - const char *buf, 78 - size_t len) 75 + static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, 76 + const char *buf, size_t len) 79 77 { 80 78 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 81 79 struct ad9832_state *st = iio_priv(indio_dev); ··· 107 109 ret = spi_sync(st->spi, &st->msg); 108 110 break; 109 111 case AD9832_FREQ_SYM: 110 - if (val == 1) 112 + if (val == 1) { 111 113 st->ctrl_fp |= AD9832_FREQ; 112 - else if (val == 0) 114 + } else if (val == 0) { 113 115 st->ctrl_fp &= ~AD9832_FREQ; 114 - else { 116 + } else { 115 117 ret = -EINVAL; 116 118 break; 117 119 }
-6
drivers/staging/iio/frequency/ad9850.c
··· 21 21 22 22 #define DRV_NAME "ad9850" 23 23 24 - #define value_mask (u16)0xf000 25 - #define addr_shift 12 26 - 27 24 /* Register format: 4 bits addr + 12 bits value */ 28 25 struct ad9850_config { 29 26 u8 control[5]; ··· 47 50 mutex_lock(&st->lock); 48 51 49 52 ret = spi_sync_transfer(st->sdev, &xfer, 1); 50 - if (ret) 51 - goto error_ret; 52 - error_ret: 53 53 mutex_unlock(&st->lock); 54 54 55 55 return ret ? ret : len;
+3 -1
drivers/staging/iio/magnetometer/hmc5843.c
··· 630 630 MODULE_DEVICE_TABLE(i2c, hmc5843_id); 631 631 632 632 static const struct of_device_id hmc5843_of_match[] = { 633 - { .compatible = "honeywell,hmc5843" }, 633 + { .compatible = "honeywell,hmc5843", .data = (void *)HMC5843_ID }, 634 + { .compatible = "honeywell,hmc5883", .data = (void *)HMC5883_ID }, 635 + { .compatible = "honeywell,hmc5883l", .data = (void *)HMC5883L_ID }, 634 636 {} 635 637 }; 636 638 MODULE_DEVICE_TABLE(of, hmc5843_of_match);