linux/drivers/iio/temperature/maxim_thermocouple.c
Greg Kroah-Hartman 7970b9e6a5 Second round of new device support, cleanups and fixes for IIO in the 4.10 cycle
This includes two branch merges for elements that may also go via MFD.
 
 New device support
 * cros_ec
   - new driver to support these Chrome OS contiguous sensors which are behind
     the Chrome OS embedded controller.  Requires a few minor MFD and chrome
     platform changes.  One follow up fix deals with some dependency issues in
     Kconfig.
 * mpu-3050
   - new driver and device tree bindings for this venerable device.
 * st_accel
   - support for the lng2dm an
 
 Driver features
 * ad7192
   - Add DVdd regulator handling
 * ad9832
   - Add DVDD regulator handling
 * at91
   - Suspend and resume support
 * si7020
   - Device tree bindings
 * ti-am335x
   - DMA support - uses dma to accelerate short bursts of read back rather
   than full blown DMA buffer support.  Greatly improved performance.
   Includes an MFD addition to give access to the address needed for DMA.
 * tsl2583
   - Device tree bindings
 
 Cleanups and minor fixes
 * ad7192
   - Fix regulator naming to match datasheet
   - Handle regulator errors correctly (so as to not break deferred probing)
   - Rename reg variable to reflect which regulator it is
 * ad5933
   - Fix regulator naming to match datasheet
   - Handle regulator errors correctly (so as to not break deferred probing)
 * ad7746
   - Fix a missing return value (fallout from previous patch set)
 * ad7780
   - Fix regulator naming to match datasheet
   - Handle regulator errors correctly (so as to not break deferred probing)
 * ad9832
   - Fix regulator naming to match datasheet
   - Handle regulator errors correctly (so as to not break deferred probing)
   - Rename reg regulator to reflect which one it is
 * ad9834
   - Fix regulator naming to match datasheet
   - Handle regulator errors correctly (so as to not break deferred probing)
 * hts221
   - Remove a duplicated include
 * maxim thermocouple
   - Handle a wrong storage side in read function.  Prevent any problems that
   might be introduced by additions to this driver in future.
 * tsl2583 - big set from Brian Masney to drive this towards a staging
   graduation.
   - Convert to iio_chan_spec and read_raw / write_raw (in a couple of steps)
   - Improved error handling in various functions
   - Drop redundant power_state custom sysfs attribute.
   - Use IIO_*_ATTR* macros for remaining attributes.
   - Return an error code to userspace on invalid parameters being writen to
     sysfs files.
   - Add locking to various attribute accesses to remove possible races.
   - Add defines for various magic numbers.
   - Use smbus_read_byte_data instead of a write_byte followed by read_byte.
   - Query only relevant registers in probe.
   - Tidy up ordering of code comments.
   - Remove a pointless power off sequence in taos_chip_on.
   - Don't bother shutting down the chip when updating the lux table.
   The table is held entirely in the driver and doesn't effect the chip at all.
   - Drop a redundant i2c call in taos_als_calibrate where the same register
   is read twice in a row.
 -----BEGIN PGP SIGNATURE-----
 
 iQIuBAABCAAYBQJYH22HERxqaWMyM0BrZXJuZWwub3JnAAoJEFSFNJnE9BaIUg4P
 /1GRFGpTbqyzDOX6KZKAdBRsRMn3XPL0XslvbKpupY4T9WFEJbZi01z4P/x+uRIk
 6TtRlhdWBLdzVQ6sLpVAZVXR2/FxGj+nsc2ONgHJXm02/fDBmjI89Ed9WVS1HR73
 D2yLAFhflvE6mDMExvAnUVBh9ClR2SXoGL0N1k+VQM08Rs+F6IzBAg2albiPkct/
 7UQB9apn4VCCaRY02gllvWrUiJV3w8jsSikwDACZsprQGIxKjLiH4evtRfSOI8bg
 Z7UoJYaYNMiQtn6+rFaNUjFQtzlnWcjB92RUy/MqnpXmkuAvw3+CbXYteYnailO1
 eTsIjKpwssKaQh5HHALrxIwP/a/9a+Wyrd8iheygUQvzSNx3gcxXrtNrUk8eI/9W
 N8pBRBbu+2pzSzVLQb+7SR5wJG1zQ5NEXI1gpZuyed94g30Be0vQpeiZRNCUL+J0
 ta28Xa25cREzHrXGXrhfyxNJuriav/8A5PTtnGJSZZ8RhuHZq24TcC8h6KVOWR+3
 BBiehupFcIErbrGcMKuhq01q8A9+Vj/1z6hQaZzfIpXFjEEUvkBo3OMiNtgpkQYM
 XYdaF2bH25t8wx020z4FrmN48nxg2HDTLtKissP/6bInh4kYyGneKhf6QQVnG2IX
 41XLE/fw4QoAnLuTBXD3boydC08uJmVkXeOH2sfaCYwG
 =ptGw
 -----END PGP SIGNATURE-----

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

Jonathan writes:

Second round of new device support, cleanups and fixes for IIO in the 4.10 cycle

This includes two branch merges for elements that may also go via MFD.

New device support
* cros_ec
  - new driver to support these Chrome OS contiguous sensors which are behind
    the Chrome OS embedded controller.  Requires a few minor MFD and chrome
    platform changes.  One follow up fix deals with some dependency issues in
    Kconfig.
* mpu-3050
  - new driver and device tree bindings for this venerable device.
* st_accel
  - support for the lng2dm an

Driver features
* ad7192
  - Add DVdd regulator handling
* ad9832
  - Add DVDD regulator handling
* at91
  - Suspend and resume support
* si7020
  - Device tree bindings
* ti-am335x
  - DMA support - uses dma to accelerate short bursts of read back rather
  than full blown DMA buffer support.  Greatly improved performance.
  Includes an MFD addition to give access to the address needed for DMA.
* tsl2583
  - Device tree bindings

Cleanups and minor fixes
* ad7192
  - Fix regulator naming to match datasheet
  - Handle regulator errors correctly (so as to not break deferred probing)
  - Rename reg variable to reflect which regulator it is
* ad5933
  - Fix regulator naming to match datasheet
  - Handle regulator errors correctly (so as to not break deferred probing)
* ad7746
  - Fix a missing return value (fallout from previous patch set)
* ad7780
  - Fix regulator naming to match datasheet
  - Handle regulator errors correctly (so as to not break deferred probing)
* ad9832
  - Fix regulator naming to match datasheet
  - Handle regulator errors correctly (so as to not break deferred probing)
  - Rename reg regulator to reflect which one it is
* ad9834
  - Fix regulator naming to match datasheet
  - Handle regulator errors correctly (so as to not break deferred probing)
* hts221
  - Remove a duplicated include
* maxim thermocouple
  - Handle a wrong storage side in read function.  Prevent any problems that
  might be introduced by additions to this driver in future.
* tsl2583 - big set from Brian Masney to drive this towards a staging
  graduation.
  - Convert to iio_chan_spec and read_raw / write_raw (in a couple of steps)
  - Improved error handling in various functions
  - Drop redundant power_state custom sysfs attribute.
  - Use IIO_*_ATTR* macros for remaining attributes.
  - Return an error code to userspace on invalid parameters being writen to
    sysfs files.
  - Add locking to various attribute accesses to remove possible races.
  - Add defines for various magic numbers.
  - Use smbus_read_byte_data instead of a write_byte followed by read_byte.
  - Query only relevant registers in probe.
  - Tidy up ordering of code comments.
  - Remove a pointless power off sequence in taos_chip_on.
  - Don't bother shutting down the chip when updating the lux table.
  The table is held entirely in the driver and doesn't effect the chip at all.
  - Drop a redundant i2c call in taos_als_calibrate where the same register
  is read twice in a row.
2016-11-07 09:14:03 +01:00

286 lines
6.7 KiB
C

/*
* maxim_thermocouple.c - Support for Maxim thermocouple chips
*
* Copyright (C) 2016 Matt Ranostay <mranostay@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mutex.h>
#include <linux/err.h>
#include <linux/spi/spi.h>
#include <linux/iio/iio.h>
#include <linux/iio/trigger.h>
#include <linux/iio/buffer.h>
#include <linux/iio/triggered_buffer.h>
#include <linux/iio/trigger_consumer.h>
#define MAXIM_THERMOCOUPLE_DRV_NAME "maxim_thermocouple"
enum {
MAX6675,
MAX31855,
};
static const struct iio_chan_spec max6675_channels[] = {
{ /* thermocouple temperature */
.type = IIO_TEMP,
.info_mask_separate =
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
.scan_index = 0,
.scan_type = {
.sign = 's',
.realbits = 13,
.storagebits = 16,
.shift = 3,
.endianness = IIO_BE,
},
},
IIO_CHAN_SOFT_TIMESTAMP(1),
};
static const struct iio_chan_spec max31855_channels[] = {
{ /* thermocouple temperature */
.type = IIO_TEMP,
.address = 2,
.info_mask_separate =
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
.scan_index = 0,
.scan_type = {
.sign = 's',
.realbits = 14,
.storagebits = 16,
.shift = 2,
.endianness = IIO_BE,
},
},
{ /* cold junction temperature */
.type = IIO_TEMP,
.address = 0,
.channel2 = IIO_MOD_TEMP_AMBIENT,
.modified = 1,
.info_mask_separate =
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
.scan_index = 1,
.scan_type = {
.sign = 's',
.realbits = 12,
.storagebits = 16,
.shift = 4,
.endianness = IIO_BE,
},
},
IIO_CHAN_SOFT_TIMESTAMP(2),
};
static const unsigned long max31855_scan_masks[] = {0x3, 0};
struct maxim_thermocouple_chip {
const struct iio_chan_spec *channels;
const unsigned long *scan_masks;
u8 num_channels;
u8 read_size;
/* bit-check for valid input */
u32 status_bit;
};
static const struct maxim_thermocouple_chip maxim_thermocouple_chips[] = {
[MAX6675] = {
.channels = max6675_channels,
.num_channels = ARRAY_SIZE(max6675_channels),
.read_size = 2,
.status_bit = BIT(2),
},
[MAX31855] = {
.channels = max31855_channels,
.num_channels = ARRAY_SIZE(max31855_channels),
.read_size = 4,
.scan_masks = max31855_scan_masks,
.status_bit = BIT(16),
},
};
struct maxim_thermocouple_data {
struct spi_device *spi;
const struct maxim_thermocouple_chip *chip;
u8 buffer[16] ____cacheline_aligned;
};
static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
struct iio_chan_spec const *chan, int *val)
{
unsigned int storage_bytes = data->chip->read_size;
unsigned int shift = chan->scan_type.shift + (chan->address * 8);
__be16 buf16;
__be32 buf32;
int ret;
switch (storage_bytes) {
case 2:
ret = spi_read(data->spi, (void *)&buf16, storage_bytes);
*val = be16_to_cpu(buf16);
break;
case 4:
ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
*val = be32_to_cpu(buf32);
break;
default:
ret = -EINVAL;
}
if (ret)
return ret;
/* check to be sure this is a valid reading */
if (*val & data->chip->status_bit)
return -EINVAL;
*val = sign_extend32(*val >> shift, chan->scan_type.realbits - 1);
return 0;
}
static irqreturn_t maxim_thermocouple_trigger_handler(int irq, void *private)
{
struct iio_poll_func *pf = private;
struct iio_dev *indio_dev = pf->indio_dev;
struct maxim_thermocouple_data *data = iio_priv(indio_dev);
int ret;
ret = spi_read(data->spi, data->buffer, data->chip->read_size);
if (!ret) {
iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
iio_get_time_ns(indio_dev));
}
iio_trigger_notify_done(indio_dev->trig);
return IRQ_HANDLED;
}
static int maxim_thermocouple_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
{
struct maxim_thermocouple_data *data = iio_priv(indio_dev);
int ret = -EINVAL;
switch (mask) {
case IIO_CHAN_INFO_RAW:
ret = iio_device_claim_direct_mode(indio_dev);
if (ret)
return ret;
ret = maxim_thermocouple_read(data, chan, val);
iio_device_release_direct_mode(indio_dev);
if (!ret)
return IIO_VAL_INT;
break;
case IIO_CHAN_INFO_SCALE:
switch (chan->channel2) {
case IIO_MOD_TEMP_AMBIENT:
*val = 62;
*val2 = 500000; /* 1000 * 0.0625 */
ret = IIO_VAL_INT_PLUS_MICRO;
break;
default:
*val = 250; /* 1000 * 0.25 */
ret = IIO_VAL_INT;
};
break;
}
return ret;
}
static const struct iio_info maxim_thermocouple_info = {
.driver_module = THIS_MODULE,
.read_raw = maxim_thermocouple_read_raw,
};
static int maxim_thermocouple_probe(struct spi_device *spi)
{
const struct spi_device_id *id = spi_get_device_id(spi);
struct iio_dev *indio_dev;
struct maxim_thermocouple_data *data;
const struct maxim_thermocouple_chip *chip =
&maxim_thermocouple_chips[id->driver_data];
int ret;
indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
if (!indio_dev)
return -ENOMEM;
indio_dev->info = &maxim_thermocouple_info;
indio_dev->name = MAXIM_THERMOCOUPLE_DRV_NAME;
indio_dev->channels = chip->channels;
indio_dev->available_scan_masks = chip->scan_masks;
indio_dev->num_channels = chip->num_channels;
indio_dev->modes = INDIO_DIRECT_MODE;
data = iio_priv(indio_dev);
data->spi = spi;
data->chip = chip;
ret = iio_triggered_buffer_setup(indio_dev, NULL,
maxim_thermocouple_trigger_handler, NULL);
if (ret)
return ret;
ret = iio_device_register(indio_dev);
if (ret)
goto error_unreg_buffer;
return 0;
error_unreg_buffer:
iio_triggered_buffer_cleanup(indio_dev);
return ret;
}
static int maxim_thermocouple_remove(struct spi_device *spi)
{
struct iio_dev *indio_dev = spi_get_drvdata(spi);
iio_device_unregister(indio_dev);
iio_triggered_buffer_cleanup(indio_dev);
return 0;
}
static const struct spi_device_id maxim_thermocouple_id[] = {
{"max6675", MAX6675},
{"max31855", MAX31855},
{},
};
MODULE_DEVICE_TABLE(spi, maxim_thermocouple_id);
static struct spi_driver maxim_thermocouple_driver = {
.driver = {
.name = MAXIM_THERMOCOUPLE_DRV_NAME,
},
.probe = maxim_thermocouple_probe,
.remove = maxim_thermocouple_remove,
.id_table = maxim_thermocouple_id,
};
module_spi_driver(maxim_thermocouple_driver);
MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
MODULE_DESCRIPTION("Maxim thermocouple sensors");
MODULE_LICENSE("GPL");