Jonathan writes:
First set of IIO new device support, features and cleanup for 5.19
Usual mixed bag. Stand out this time is Andy Shevchenko's continuing
effort to move drivers over the generic firmware interfaces.
Device support
* sprd,sc2720
- upm9620 binding addition.
- Refactor and support for sc2720, sc2721 and sc2730.
* ti,ads1015
- Refactor driver and add support for TLA2024.
Device support (IDs only)
* invensense,mpu6050
- Add ID for ICM-20608-D.
* st,accel:
- Add ID for lis302dl.
* st,lsm6dsx
- Add support for ASM330LHHX (can fallback to LSM6DSR.)
Features
* convert drivers to device properties
- IIO core
- adi,ad7266
- adi,adis16480
- adi,adxl355
- bosch,bmi160
- domintech,dmard06
- fsl,fxas21002c
- invensense,mpu3050
- linear,ltc2983
- linear,ltc2632
- maxbotix,mb1232
- maxim,max31856
- maxim,max31865
- multiplexer
- ping
- rescale
- taos,tsl2772
* core
- Add runtime check on whether realbits fit in storagebits for each
channel.
* adi,ad_sigma_delta
- Add sequencer support and relevant update_scan_mode callbacks for
adi,ad7192 and adi,ad7124.
Cleanup and minor fixes
* MAINTAINERS
- Update Lorenzo Bianconi's email address for IIO drivers.
- Add entry for ad3552r and update maintainer in dt-binding doc.
* tree-wide
- Replace strtobool() with kstrtobool().
- Drop false OF dependencies.
* core
- Tidy up and document IIO modes.
- Take iio_buffer_enabled() out of header allowing current_mode to be
moved to the opaque structure.
- As all kfifo buffers use the same mode value, drop that parameter
and set it unconditionally.
- White space fixes and similar.
- Drop use of list iterator variable for
list_for_each_entry_continue_reverse and use list_prepare_entry to
restart.
* sysfs-trigger
- Replace use of 'found' variable with dedicate list iterator variable.
* adi,ad7124
- Drop misleading shift.
* adi,ad2s1210
- Remove redundant local variable assignment.
* adi,adis16480
- Use local device pointer to reduce repetition.
- Improve handling of clocks.
* domintech,dmard09
- White space.
* dummy driver
- Improve error handling.
* fsl,mma8452
- Add missing documentation of name element.
* invensense,mpu3050
- Stop remove() returning non 0.
* kionix,kxsd9
- White space.
* linear,ltc2688
- Use local variable for struct device.
- Combine of_node_put() error handling paths.
* linear,ltc2983
- Avoid use of constants in messages where a define is available.
* microchip,mcp4131
- Fix compatible in dt example.
* pni,rm3100
- Stop directly accessing iio_dev->current_mode just to find out
if the buffer is enabled.
* renesas,rzg2l
- Relax kconfig constraint to include newer devices.
* sprd,sc27xx
- Fix wrong scaling mask.
- Improve the calibration values.
* samsung,ssp
- Replace a 'found' variable in favor of an explicit value that was
found.
* sensortek,stk3xx
- Add proximity-near-level binding and driver support.
* st,st_sensors:
- Drop unused accel_type enum.
- Return early in *_write_raw()
- Drop unnecessary locking in _avail functions.
- Add local lock to protect odr against concurrent updates allowing
mlock to no longer be used outside of the core.
- Use iio_device_claim_direct_mode() rather than racy checking of
the current mode.
* st,stmpe-adc
- Fix checks on wait_for_completion_timeout().
- Allow use of of_device_id for matching.
* st,stm32-dfsdm
- Stop accessing iio_dev->current_mode to find out if the buffer
is enabled (so we can hide that variable in the opaque structure)
* st,vl53l0x
- Fix checks on wait_for_completion_timeout.
* ti,ads1015
- Add missing ID for ti,ads1115 in binding doc.
- Convert from repeated chip ID look up to selecting static const
data.
- Switch to read_avail() callback.
* ti,ads8688
- Use of_device_id for driver matching.
* ti,palmas-adc
- Drop a warning on minor calibration mismatch leading to slightly
negative values after applying the calibration.
* tag 'iio-for-5.19a' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio: (95 commits)
iio: ti-ads8688: use of_device_id for OF matching
iio: stmpe-adc: use of_device_id for OF matching
dt-bindings: iio: Fix incorrect compatible strings in examples
iio: gyro: mpu3050: Make mpu3050_common_remove() return void
iio: dac: ltc2632: Make use of device properties
iio: temperature: max31865: Make use of device properties
iio: proximity: mb1232: Switch to use fwnode_irq_get()
iio: imu: adis16480: Improve getting the optional clocks
iio: imu: adis16480: Use temporary variable for struct device
iio: imu: adis16480: Make use of device properties
staging: iio: ad2s1210: remove redundant assignment to variable negative
iio: adc: sc27xx: add support for PMIC sc2730
iio: adc: sc27xx: add support for PMIC sc2720 and sc2721
iio: adc: sc27xx: refactor some functions for support more PMiCs
iio: adc: sc27xx: structure adjustment and optimization
iio: adc: sc27xx: Fine tune the scale calibration values
iio: adc: sc27xx: fix read big scale voltage not right
dt-bindings:iio:adc: add sprd,ump9620-adc dt-binding
iio: proximity: stk3310: Export near level property for proximity sensor
dt-bindings: iio: light: stk33xx: Add proximity-near-level
...
437 lines
11 KiB
C
437 lines
11 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* ti-dac5571.c - Texas Instruments 8/10/12-bit 1/4-channel DAC driver
|
|
*
|
|
* Copyright (C) 2018 Prevas A/S
|
|
*
|
|
* https://www.ti.com/lit/ds/symlink/dac5571.pdf
|
|
* https://www.ti.com/lit/ds/symlink/dac6571.pdf
|
|
* https://www.ti.com/lit/ds/symlink/dac7571.pdf
|
|
* https://www.ti.com/lit/ds/symlink/dac5574.pdf
|
|
* https://www.ti.com/lit/ds/symlink/dac6574.pdf
|
|
* https://www.ti.com/lit/ds/symlink/dac7574.pdf
|
|
* https://www.ti.com/lit/ds/symlink/dac5573.pdf
|
|
* https://www.ti.com/lit/ds/symlink/dac6573.pdf
|
|
* https://www.ti.com/lit/ds/symlink/dac7573.pdf
|
|
*/
|
|
|
|
#include <linux/iio/iio.h>
|
|
#include <linux/i2c.h>
|
|
#include <linux/module.h>
|
|
#include <linux/mod_devicetable.h>
|
|
#include <linux/property.h>
|
|
#include <linux/regulator/consumer.h>
|
|
|
|
enum chip_id {
|
|
single_8bit, single_10bit, single_12bit,
|
|
quad_8bit, quad_10bit, quad_12bit
|
|
};
|
|
|
|
struct dac5571_spec {
|
|
u8 num_channels;
|
|
u8 resolution;
|
|
};
|
|
|
|
static const struct dac5571_spec dac5571_spec[] = {
|
|
[single_8bit] = {.num_channels = 1, .resolution = 8},
|
|
[single_10bit] = {.num_channels = 1, .resolution = 10},
|
|
[single_12bit] = {.num_channels = 1, .resolution = 12},
|
|
[quad_8bit] = {.num_channels = 4, .resolution = 8},
|
|
[quad_10bit] = {.num_channels = 4, .resolution = 10},
|
|
[quad_12bit] = {.num_channels = 4, .resolution = 12},
|
|
};
|
|
|
|
struct dac5571_data {
|
|
struct i2c_client *client;
|
|
int id;
|
|
struct mutex lock;
|
|
struct regulator *vref;
|
|
u16 val[4];
|
|
bool powerdown[4];
|
|
u8 powerdown_mode[4];
|
|
struct dac5571_spec const *spec;
|
|
int (*dac5571_cmd)(struct dac5571_data *data, int channel, u16 val);
|
|
int (*dac5571_pwrdwn)(struct dac5571_data *data, int channel, u8 pwrdwn);
|
|
u8 buf[3] ____cacheline_aligned;
|
|
};
|
|
|
|
#define DAC5571_POWERDOWN(mode) ((mode) + 1)
|
|
#define DAC5571_POWERDOWN_FLAG BIT(0)
|
|
#define DAC5571_CHANNEL_SELECT 1
|
|
#define DAC5571_LOADMODE_DIRECT BIT(4)
|
|
#define DAC5571_SINGLE_PWRDWN_BITS 4
|
|
#define DAC5571_QUAD_PWRDWN_BITS 6
|
|
|
|
static int dac5571_cmd_single(struct dac5571_data *data, int channel, u16 val)
|
|
{
|
|
unsigned int shift;
|
|
|
|
shift = 12 - data->spec->resolution;
|
|
data->buf[1] = val << shift;
|
|
data->buf[0] = val >> (8 - shift);
|
|
|
|
if (i2c_master_send(data->client, data->buf, 2) != 2)
|
|
return -EIO;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int dac5571_cmd_quad(struct dac5571_data *data, int channel, u16 val)
|
|
{
|
|
unsigned int shift;
|
|
|
|
shift = 16 - data->spec->resolution;
|
|
data->buf[2] = val << shift;
|
|
data->buf[1] = (val >> (8 - shift));
|
|
data->buf[0] = (channel << DAC5571_CHANNEL_SELECT) |
|
|
DAC5571_LOADMODE_DIRECT;
|
|
|
|
if (i2c_master_send(data->client, data->buf, 3) != 3)
|
|
return -EIO;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int dac5571_pwrdwn_single(struct dac5571_data *data, int channel, u8 pwrdwn)
|
|
{
|
|
data->buf[1] = 0;
|
|
data->buf[0] = pwrdwn << DAC5571_SINGLE_PWRDWN_BITS;
|
|
|
|
if (i2c_master_send(data->client, data->buf, 2) != 2)
|
|
return -EIO;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int dac5571_pwrdwn_quad(struct dac5571_data *data, int channel, u8 pwrdwn)
|
|
{
|
|
data->buf[2] = 0;
|
|
data->buf[1] = pwrdwn << DAC5571_QUAD_PWRDWN_BITS;
|
|
data->buf[0] = (channel << DAC5571_CHANNEL_SELECT) |
|
|
DAC5571_LOADMODE_DIRECT | DAC5571_POWERDOWN_FLAG;
|
|
|
|
if (i2c_master_send(data->client, data->buf, 3) != 3)
|
|
return -EIO;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const char *const dac5571_powerdown_modes[] = {
|
|
"1kohm_to_gnd", "100kohm_to_gnd", "three_state",
|
|
};
|
|
|
|
static int dac5571_get_powerdown_mode(struct iio_dev *indio_dev,
|
|
const struct iio_chan_spec *chan)
|
|
{
|
|
struct dac5571_data *data = iio_priv(indio_dev);
|
|
|
|
return data->powerdown_mode[chan->channel];
|
|
}
|
|
|
|
static int dac5571_set_powerdown_mode(struct iio_dev *indio_dev,
|
|
const struct iio_chan_spec *chan,
|
|
unsigned int mode)
|
|
{
|
|
struct dac5571_data *data = iio_priv(indio_dev);
|
|
int ret = 0;
|
|
|
|
if (data->powerdown_mode[chan->channel] == mode)
|
|
return 0;
|
|
|
|
mutex_lock(&data->lock);
|
|
if (data->powerdown[chan->channel]) {
|
|
ret = data->dac5571_pwrdwn(data, chan->channel,
|
|
DAC5571_POWERDOWN(mode));
|
|
if (ret)
|
|
goto out;
|
|
}
|
|
data->powerdown_mode[chan->channel] = mode;
|
|
|
|
out:
|
|
mutex_unlock(&data->lock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static const struct iio_enum dac5571_powerdown_mode = {
|
|
.items = dac5571_powerdown_modes,
|
|
.num_items = ARRAY_SIZE(dac5571_powerdown_modes),
|
|
.get = dac5571_get_powerdown_mode,
|
|
.set = dac5571_set_powerdown_mode,
|
|
};
|
|
|
|
static ssize_t dac5571_read_powerdown(struct iio_dev *indio_dev,
|
|
uintptr_t private,
|
|
const struct iio_chan_spec *chan,
|
|
char *buf)
|
|
{
|
|
struct dac5571_data *data = iio_priv(indio_dev);
|
|
|
|
return sysfs_emit(buf, "%d\n", data->powerdown[chan->channel]);
|
|
}
|
|
|
|
static ssize_t dac5571_write_powerdown(struct iio_dev *indio_dev,
|
|
uintptr_t private,
|
|
const struct iio_chan_spec *chan,
|
|
const char *buf, size_t len)
|
|
{
|
|
struct dac5571_data *data = iio_priv(indio_dev);
|
|
bool powerdown;
|
|
int ret;
|
|
|
|
ret = kstrtobool(buf, &powerdown);
|
|
if (ret)
|
|
return ret;
|
|
|
|
if (data->powerdown[chan->channel] == powerdown)
|
|
return len;
|
|
|
|
mutex_lock(&data->lock);
|
|
if (powerdown)
|
|
ret = data->dac5571_pwrdwn(data, chan->channel,
|
|
DAC5571_POWERDOWN(data->powerdown_mode[chan->channel]));
|
|
else
|
|
ret = data->dac5571_cmd(data, chan->channel,
|
|
data->val[chan->channel]);
|
|
if (ret)
|
|
goto out;
|
|
|
|
data->powerdown[chan->channel] = powerdown;
|
|
|
|
out:
|
|
mutex_unlock(&data->lock);
|
|
|
|
return ret ? ret : len;
|
|
}
|
|
|
|
|
|
static const struct iio_chan_spec_ext_info dac5571_ext_info[] = {
|
|
{
|
|
.name = "powerdown",
|
|
.read = dac5571_read_powerdown,
|
|
.write = dac5571_write_powerdown,
|
|
.shared = IIO_SEPARATE,
|
|
},
|
|
IIO_ENUM("powerdown_mode", IIO_SEPARATE, &dac5571_powerdown_mode),
|
|
IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE, &dac5571_powerdown_mode),
|
|
{},
|
|
};
|
|
|
|
#define dac5571_CHANNEL(chan, name) { \
|
|
.type = IIO_VOLTAGE, \
|
|
.channel = (chan), \
|
|
.address = (chan), \
|
|
.indexed = true, \
|
|
.output = true, \
|
|
.datasheet_name = name, \
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
|
|
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
|
|
.ext_info = dac5571_ext_info, \
|
|
}
|
|
|
|
static const struct iio_chan_spec dac5571_channels[] = {
|
|
dac5571_CHANNEL(0, "A"),
|
|
dac5571_CHANNEL(1, "B"),
|
|
dac5571_CHANNEL(2, "C"),
|
|
dac5571_CHANNEL(3, "D"),
|
|
};
|
|
|
|
static int dac5571_read_raw(struct iio_dev *indio_dev,
|
|
struct iio_chan_spec const *chan,
|
|
int *val, int *val2, long mask)
|
|
{
|
|
struct dac5571_data *data = iio_priv(indio_dev);
|
|
int ret;
|
|
|
|
switch (mask) {
|
|
case IIO_CHAN_INFO_RAW:
|
|
*val = data->val[chan->channel];
|
|
return IIO_VAL_INT;
|
|
|
|
case IIO_CHAN_INFO_SCALE:
|
|
ret = regulator_get_voltage(data->vref);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
*val = ret / 1000;
|
|
*val2 = data->spec->resolution;
|
|
return IIO_VAL_FRACTIONAL_LOG2;
|
|
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
|
|
static int dac5571_write_raw(struct iio_dev *indio_dev,
|
|
struct iio_chan_spec const *chan,
|
|
int val, int val2, long mask)
|
|
{
|
|
struct dac5571_data *data = iio_priv(indio_dev);
|
|
int ret;
|
|
|
|
switch (mask) {
|
|
case IIO_CHAN_INFO_RAW:
|
|
if (data->val[chan->channel] == val)
|
|
return 0;
|
|
|
|
if (val >= (1 << data->spec->resolution) || val < 0)
|
|
return -EINVAL;
|
|
|
|
if (data->powerdown[chan->channel])
|
|
return -EBUSY;
|
|
|
|
mutex_lock(&data->lock);
|
|
ret = data->dac5571_cmd(data, chan->channel, val);
|
|
if (ret == 0)
|
|
data->val[chan->channel] = val;
|
|
mutex_unlock(&data->lock);
|
|
return ret;
|
|
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
|
|
static int dac5571_write_raw_get_fmt(struct iio_dev *indio_dev,
|
|
struct iio_chan_spec const *chan,
|
|
long mask)
|
|
{
|
|
return IIO_VAL_INT;
|
|
}
|
|
|
|
static const struct iio_info dac5571_info = {
|
|
.read_raw = dac5571_read_raw,
|
|
.write_raw = dac5571_write_raw,
|
|
.write_raw_get_fmt = dac5571_write_raw_get_fmt,
|
|
};
|
|
|
|
static int dac5571_probe(struct i2c_client *client,
|
|
const struct i2c_device_id *id)
|
|
{
|
|
struct device *dev = &client->dev;
|
|
const struct dac5571_spec *spec;
|
|
struct dac5571_data *data;
|
|
struct iio_dev *indio_dev;
|
|
enum chip_id chip_id;
|
|
int ret, i;
|
|
|
|
indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
|
|
if (!indio_dev)
|
|
return -ENOMEM;
|
|
|
|
data = iio_priv(indio_dev);
|
|
i2c_set_clientdata(client, indio_dev);
|
|
data->client = client;
|
|
|
|
indio_dev->info = &dac5571_info;
|
|
indio_dev->name = id->name;
|
|
indio_dev->modes = INDIO_DIRECT_MODE;
|
|
indio_dev->channels = dac5571_channels;
|
|
|
|
if (dev_fwnode(dev))
|
|
chip_id = (uintptr_t)device_get_match_data(dev);
|
|
else
|
|
chip_id = id->driver_data;
|
|
|
|
spec = &dac5571_spec[chip_id];
|
|
|
|
indio_dev->num_channels = spec->num_channels;
|
|
data->spec = spec;
|
|
|
|
data->vref = devm_regulator_get(dev, "vref");
|
|
if (IS_ERR(data->vref))
|
|
return PTR_ERR(data->vref);
|
|
|
|
ret = regulator_enable(data->vref);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
mutex_init(&data->lock);
|
|
|
|
switch (spec->num_channels) {
|
|
case 1:
|
|
data->dac5571_cmd = dac5571_cmd_single;
|
|
data->dac5571_pwrdwn = dac5571_pwrdwn_single;
|
|
break;
|
|
case 4:
|
|
data->dac5571_cmd = dac5571_cmd_quad;
|
|
data->dac5571_pwrdwn = dac5571_pwrdwn_quad;
|
|
break;
|
|
default:
|
|
ret = -EINVAL;
|
|
goto err;
|
|
}
|
|
|
|
for (i = 0; i < spec->num_channels; i++) {
|
|
ret = data->dac5571_cmd(data, i, 0);
|
|
if (ret) {
|
|
dev_err(dev, "failed to initialize channel %d to 0\n", i);
|
|
goto err;
|
|
}
|
|
}
|
|
|
|
ret = iio_device_register(indio_dev);
|
|
if (ret)
|
|
goto err;
|
|
|
|
return 0;
|
|
|
|
err:
|
|
regulator_disable(data->vref);
|
|
return ret;
|
|
}
|
|
|
|
static int dac5571_remove(struct i2c_client *i2c)
|
|
{
|
|
struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
|
|
struct dac5571_data *data = iio_priv(indio_dev);
|
|
|
|
iio_device_unregister(indio_dev);
|
|
regulator_disable(data->vref);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct of_device_id dac5571_of_id[] = {
|
|
{.compatible = "ti,dac5571", .data = (void *)single_8bit},
|
|
{.compatible = "ti,dac6571", .data = (void *)single_10bit},
|
|
{.compatible = "ti,dac7571", .data = (void *)single_12bit},
|
|
{.compatible = "ti,dac5574", .data = (void *)quad_8bit},
|
|
{.compatible = "ti,dac6574", .data = (void *)quad_10bit},
|
|
{.compatible = "ti,dac7574", .data = (void *)quad_12bit},
|
|
{.compatible = "ti,dac5573", .data = (void *)quad_8bit},
|
|
{.compatible = "ti,dac6573", .data = (void *)quad_10bit},
|
|
{.compatible = "ti,dac7573", .data = (void *)quad_12bit},
|
|
{}
|
|
};
|
|
MODULE_DEVICE_TABLE(of, dac5571_of_id);
|
|
|
|
static const struct i2c_device_id dac5571_id[] = {
|
|
{"dac5571", single_8bit},
|
|
{"dac6571", single_10bit},
|
|
{"dac7571", single_12bit},
|
|
{"dac5574", quad_8bit},
|
|
{"dac6574", quad_10bit},
|
|
{"dac7574", quad_12bit},
|
|
{"dac5573", quad_8bit},
|
|
{"dac6573", quad_10bit},
|
|
{"dac7573", quad_12bit},
|
|
{}
|
|
};
|
|
MODULE_DEVICE_TABLE(i2c, dac5571_id);
|
|
|
|
static struct i2c_driver dac5571_driver = {
|
|
.driver = {
|
|
.name = "ti-dac5571",
|
|
.of_match_table = dac5571_of_id,
|
|
},
|
|
.probe = dac5571_probe,
|
|
.remove = dac5571_remove,
|
|
.id_table = dac5571_id,
|
|
};
|
|
module_i2c_driver(dac5571_driver);
|
|
|
|
MODULE_AUTHOR("Sean Nyekjaer <sean@geanix.dk>");
|
|
MODULE_DESCRIPTION("Texas Instruments 8/10/12-bit 1/4-channel DAC driver");
|
|
MODULE_LICENSE("GPL v2");
|