mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2025-01-13 01:04:35 +08:00
269b9d8faf
New device support - honeywell,mprls0025pa * New driver and dt-bindings for this series of pressure sensors. - invensense,mpu6050 * Add support for ICM 20600 IMU (ID, bindings and device data). - melexis,mlx90614 * Add support for mlx90615 Infra Red Thermometer after driver cleanup and refactoring to support the differences in this device. - renesas,x9250 * New driver and bindings for this quad potentiometer. - rockchip,saradc * Add support for RK3588. Also included is a bunch of refactoring and cleanup for that driver. - rohm,bu27008 * New driver bindings etc for this 5 photodiode color sensor. - st,lsm9ds0/st,st-sensors * ID added for LSM303D accelerometer and magnetometer including ACPI binding. - ti,opt4001 * New driver and bindings for this ambient light sensor. Features - core * Introduce iio_validate_own_trigger() for cases where a driver can only consumer a trigger it registered (detected via same parent device). Use it in the kionix,kx022a driver and new rohm,by27008 driver. - dynaimage,al3320a * ACPI binding CALS0001 seen on Lenovo Yoga Table 2 devices. - kionix,kx002a * Enable asynchronous probe. - rohm,bu27034 * Enable asynchronous probe. - ti,tmp006 * Explicit support for DT including binding documentation. Cleanups, minor fixes and misc improvements. - treewide * Switch I2C drivers from probe_new() back to probe() - part of the long process of getting rid of a parameter from probe() * Various whitespace and typo fixes not otherwise called out. - core * industrialio-buffer,Style cleanup. * Add documentation to extend_name field of struct iio_chan_spec to direct people using it towards the label infrastructure instead. extend_name was a design mistake a long time back so directly people away from it may be useful. - adi,ad7606 * Add HAS_IOPORT dependency to prepare for some Kconfig changes. - bosch,bma400 * Drop pointless print of ret in a dev_err_probe() message. - invensense,icm42600 * Rework timestamp handling to reduce jitter. - mediatek,mt7986-auxdac * Add DT binding for this part. - qcom,spmi-vadc * Allow for 1/16th prescaling used on a few devices. * Various changes to channel labeling and naming, including dropping use of fwnode_name which generates odd channel names. Small ABI change as a result, but not thought to be a problem for users of this platform. - st,lsm6dsx * dt-binding: Use common schema for mount-matrix via a reference. - st,stm32 * Add a debug print for when legacy channel config is used. - ti,palmas-adc * Drop unused i2c.h include. -----BEGIN PGP SIGNATURE----- iQJFBAABCAAvFiEEbilms4eEBlKRJoGxVIU0mcT0FogFAmSF83ARHGppYzIzQGtl cm5lbC5vcmcACgkQVIU0mcT0Foi+NRAAi8E2NRkzxteTuiFRsAokm/8HbAc/9rDq 2Xyj8zT5B6jmqpbNKPtIbxxAxLV/JY+3HBIorvDNcOVfmOfshMpT31eaKpehxPJ3 A00WaJ6bi7CTp0h/7QOZnZ27yr3tuJ2jpCiGKWERmTZ60kn3S/5JRrXWbfKVccyU DR1SrsiVSbtDhD9w6kA7HBRsL0EmTQOP/ARlUcO4SB5BOC7rj9akaN6Q6LYSafOa BFfTd2suekRA94mB/ugm25xAWLsl4Rdr1iNkHyaGcXaMOJZ/zWXq14y1eLbqIJIc am8Wu2zvo+umNzuG5DMi3gxP2B57Zhcieh6sJ5egdbxB4kh5z3jrsAFiw/MLTEME 4Tc7uHBPFuXDdb8saxtmV2FVbC43+31FgCLivD0BGHtv0DndP7i//KCdp2aztmHo juyxRCw40xiul5Ihzh3NjukM0pb1inuhgblcEZVKOyaCjhur8NXoTEEcSqXczDVU BLHBmhQY2cKwqKWt1hbLXDs9iGlJBg0+P+P0VBsvy7WWOlvc9Hw8SryYwyBRJMYH LW/Wh85eIcyKaaGGEd+HQAyXwGSlrhycUSJzwdcc1IrK8TrIxn5k4BpQpeToQPjJ wBRRDigeciIN0K7L09N4hg+qTxtpLwFY945meAS0N60o5hZM4P40hnzL8m/Co0pH gx8SugY1NGk= =0gJ9 -----END PGP SIGNATURE----- Merge tag 'iio-for-6.5a' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into char-misc-next Jonathan writes: 1st set of IIO new device support, features and cleanup for the 6.5 cycle. New device support - honeywell,mprls0025pa * New driver and dt-bindings for this series of pressure sensors. - invensense,mpu6050 * Add support for ICM 20600 IMU (ID, bindings and device data). - melexis,mlx90614 * Add support for mlx90615 Infra Red Thermometer after driver cleanup and refactoring to support the differences in this device. - renesas,x9250 * New driver and bindings for this quad potentiometer. - rockchip,saradc * Add support for RK3588. Also included is a bunch of refactoring and cleanup for that driver. - rohm,bu27008 * New driver bindings etc for this 5 photodiode color sensor. - st,lsm9ds0/st,st-sensors * ID added for LSM303D accelerometer and magnetometer including ACPI binding. - ti,opt4001 * New driver and bindings for this ambient light sensor. Features - core * Introduce iio_validate_own_trigger() for cases where a driver can only consumer a trigger it registered (detected via same parent device). Use it in the kionix,kx022a driver and new rohm,by27008 driver. - dynaimage,al3320a * ACPI binding CALS0001 seen on Lenovo Yoga Table 2 devices. - kionix,kx002a * Enable asynchronous probe. - rohm,bu27034 * Enable asynchronous probe. - ti,tmp006 * Explicit support for DT including binding documentation. Cleanups, minor fixes and misc improvements. - treewide * Switch I2C drivers from probe_new() back to probe() - part of the long process of getting rid of a parameter from probe() * Various whitespace and typo fixes not otherwise called out. - core * industrialio-buffer,Style cleanup. * Add documentation to extend_name field of struct iio_chan_spec to direct people using it towards the label infrastructure instead. extend_name was a design mistake a long time back so directly people away from it may be useful. - adi,ad7606 * Add HAS_IOPORT dependency to prepare for some Kconfig changes. - bosch,bma400 * Drop pointless print of ret in a dev_err_probe() message. - invensense,icm42600 * Rework timestamp handling to reduce jitter. - mediatek,mt7986-auxdac * Add DT binding for this part. - qcom,spmi-vadc * Allow for 1/16th prescaling used on a few devices. * Various changes to channel labeling and naming, including dropping use of fwnode_name which generates odd channel names. Small ABI change as a result, but not thought to be a problem for users of this platform. - st,lsm6dsx * dt-binding: Use common schema for mount-matrix via a reference. - st,stm32 * Add a debug print for when legacy channel config is used. - ti,palmas-adc * Drop unused i2c.h include. * tag 'iio-for-6.5a' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio: (59 commits) dt-bindings: iio: rockchip: Fix 'oneOf' condition failed warning dt-bindings: iio: afe: voltage-divider: Spelling s/curcuit/circuit/ dt-bindings: iio: adc: Add rockchip,rk3588-saradc string iio: adc: rockchip_saradc: Use dev_err_probe iio: adc: rockchip_saradc: Match alignment with open parenthesis iio: adc: rockchip_saradc: Use of_device_get_match_data iio: adc: rockchip_saradc: Make use of devm_clk_get_enabled iio: adc: rockchip_saradc: Add support for RK3588 iio: adc: rockchip_saradc: Add callback functions iio: temperature: tmp006: Add OF device matching support dt-bindings: iio: temperature: Add support for tmp006 staging: iio: Switch i2c drivers back to use .probe() iio: amplifiers: ad8366 Fix whitespace issue iio: imu: inv_icm42600: avoid frequent timestamp jitter MAINTAINERS: Add ROHM BU27008 iio: light: ROHM BU27008 color sensor iio: kx022a: Use new iio_validate_own_trigger() iio: trigger: Add simple trigger_validation helper dt-bindings: iio: light: ROHM BU27008 iio: mlx90614: Add MLX90615 support ...
548 lines
12 KiB
C
548 lines
12 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* mcp4725.c - Support for Microchip MCP4725/6
|
|
*
|
|
* Copyright (C) 2012 Peter Meerwald <pmeerw@pmeerw.net>
|
|
*
|
|
* Based on max517 by Roland Stigge <stigge@antcom.de>
|
|
*
|
|
* driver for the Microchip I2C 12-bit digital-to-analog converter (DAC)
|
|
* (7-bit I2C slave address 0x60, the three LSBs can be configured in
|
|
* hardware)
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/i2c.h>
|
|
#include <linux/err.h>
|
|
#include <linux/delay.h>
|
|
#include <linux/regulator/consumer.h>
|
|
#include <linux/mod_devicetable.h>
|
|
#include <linux/property.h>
|
|
|
|
#include <linux/iio/iio.h>
|
|
#include <linux/iio/sysfs.h>
|
|
|
|
#include <linux/iio/dac/mcp4725.h>
|
|
|
|
#define MCP4725_DRV_NAME "mcp4725"
|
|
|
|
#define MCP472X_REF_VDD 0x00
|
|
#define MCP472X_REF_VREF_UNBUFFERED 0x02
|
|
#define MCP472X_REF_VREF_BUFFERED 0x03
|
|
|
|
struct mcp4725_data {
|
|
struct i2c_client *client;
|
|
int id;
|
|
unsigned ref_mode;
|
|
bool vref_buffered;
|
|
u16 dac_value;
|
|
bool powerdown;
|
|
unsigned powerdown_mode;
|
|
struct regulator *vdd_reg;
|
|
struct regulator *vref_reg;
|
|
};
|
|
|
|
static int mcp4725_suspend(struct device *dev)
|
|
{
|
|
struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
|
|
to_i2c_client(dev)));
|
|
u8 outbuf[2];
|
|
int ret;
|
|
|
|
outbuf[0] = (data->powerdown_mode + 1) << 4;
|
|
outbuf[1] = 0;
|
|
data->powerdown = true;
|
|
|
|
ret = i2c_master_send(data->client, outbuf, 2);
|
|
if (ret < 0)
|
|
return ret;
|
|
else if (ret != 2)
|
|
return -EIO;
|
|
return 0;
|
|
}
|
|
|
|
static int mcp4725_resume(struct device *dev)
|
|
{
|
|
struct mcp4725_data *data = iio_priv(i2c_get_clientdata(
|
|
to_i2c_client(dev)));
|
|
u8 outbuf[2];
|
|
int ret;
|
|
|
|
/* restore previous DAC value */
|
|
outbuf[0] = (data->dac_value >> 8) & 0xf;
|
|
outbuf[1] = data->dac_value & 0xff;
|
|
data->powerdown = false;
|
|
|
|
ret = i2c_master_send(data->client, outbuf, 2);
|
|
if (ret < 0)
|
|
return ret;
|
|
else if (ret != 2)
|
|
return -EIO;
|
|
return 0;
|
|
}
|
|
static DEFINE_SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend,
|
|
mcp4725_resume);
|
|
|
|
static ssize_t mcp4725_store_eeprom(struct device *dev,
|
|
struct device_attribute *attr, const char *buf, size_t len)
|
|
{
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
|
struct mcp4725_data *data = iio_priv(indio_dev);
|
|
int tries = 20;
|
|
u8 inoutbuf[3];
|
|
bool state;
|
|
int ret;
|
|
|
|
ret = kstrtobool(buf, &state);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
if (!state)
|
|
return 0;
|
|
|
|
inoutbuf[0] = 0x60; /* write EEPROM */
|
|
inoutbuf[0] |= data->ref_mode << 3;
|
|
inoutbuf[0] |= data->powerdown ? ((data->powerdown_mode + 1) << 1) : 0;
|
|
inoutbuf[1] = data->dac_value >> 4;
|
|
inoutbuf[2] = (data->dac_value & 0xf) << 4;
|
|
|
|
ret = i2c_master_send(data->client, inoutbuf, 3);
|
|
if (ret < 0)
|
|
return ret;
|
|
else if (ret != 3)
|
|
return -EIO;
|
|
|
|
/* wait for write complete, takes up to 50ms */
|
|
while (tries--) {
|
|
msleep(20);
|
|
ret = i2c_master_recv(data->client, inoutbuf, 3);
|
|
if (ret < 0)
|
|
return ret;
|
|
else if (ret != 3)
|
|
return -EIO;
|
|
|
|
if (inoutbuf[0] & 0x80)
|
|
break;
|
|
}
|
|
|
|
if (tries < 0) {
|
|
dev_err(&data->client->dev,
|
|
"mcp4725_store_eeprom() failed, incomplete\n");
|
|
return -EIO;
|
|
}
|
|
|
|
return len;
|
|
}
|
|
|
|
static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
|
|
|
|
static struct attribute *mcp4725_attributes[] = {
|
|
&iio_dev_attr_store_eeprom.dev_attr.attr,
|
|
NULL,
|
|
};
|
|
|
|
static const struct attribute_group mcp4725_attribute_group = {
|
|
.attrs = mcp4725_attributes,
|
|
};
|
|
|
|
static const char * const mcp4725_powerdown_modes[] = {
|
|
"1kohm_to_gnd",
|
|
"100kohm_to_gnd",
|
|
"500kohm_to_gnd"
|
|
};
|
|
|
|
static const char * const mcp4726_powerdown_modes[] = {
|
|
"1kohm_to_gnd",
|
|
"125kohm_to_gnd",
|
|
"640kohm_to_gnd"
|
|
};
|
|
|
|
static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev,
|
|
const struct iio_chan_spec *chan)
|
|
{
|
|
struct mcp4725_data *data = iio_priv(indio_dev);
|
|
|
|
return data->powerdown_mode;
|
|
}
|
|
|
|
static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev,
|
|
const struct iio_chan_spec *chan, unsigned mode)
|
|
{
|
|
struct mcp4725_data *data = iio_priv(indio_dev);
|
|
|
|
data->powerdown_mode = mode;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev,
|
|
uintptr_t private, const struct iio_chan_spec *chan, char *buf)
|
|
{
|
|
struct mcp4725_data *data = iio_priv(indio_dev);
|
|
|
|
return sysfs_emit(buf, "%d\n", data->powerdown);
|
|
}
|
|
|
|
static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev,
|
|
uintptr_t private, const struct iio_chan_spec *chan,
|
|
const char *buf, size_t len)
|
|
{
|
|
struct mcp4725_data *data = iio_priv(indio_dev);
|
|
bool state;
|
|
int ret;
|
|
|
|
ret = kstrtobool(buf, &state);
|
|
if (ret)
|
|
return ret;
|
|
|
|
if (state)
|
|
ret = mcp4725_suspend(&data->client->dev);
|
|
else
|
|
ret = mcp4725_resume(&data->client->dev);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
return len;
|
|
}
|
|
|
|
enum chip_id {
|
|
MCP4725,
|
|
MCP4726,
|
|
};
|
|
|
|
static const struct iio_enum mcp472x_powerdown_mode_enum[] = {
|
|
[MCP4725] = {
|
|
.items = mcp4725_powerdown_modes,
|
|
.num_items = ARRAY_SIZE(mcp4725_powerdown_modes),
|
|
.get = mcp4725_get_powerdown_mode,
|
|
.set = mcp4725_set_powerdown_mode,
|
|
},
|
|
[MCP4726] = {
|
|
.items = mcp4726_powerdown_modes,
|
|
.num_items = ARRAY_SIZE(mcp4726_powerdown_modes),
|
|
.get = mcp4725_get_powerdown_mode,
|
|
.set = mcp4725_set_powerdown_mode,
|
|
},
|
|
};
|
|
|
|
static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = {
|
|
{
|
|
.name = "powerdown",
|
|
.read = mcp4725_read_powerdown,
|
|
.write = mcp4725_write_powerdown,
|
|
.shared = IIO_SEPARATE,
|
|
},
|
|
IIO_ENUM("powerdown_mode", IIO_SEPARATE,
|
|
&mcp472x_powerdown_mode_enum[MCP4725]),
|
|
IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE,
|
|
&mcp472x_powerdown_mode_enum[MCP4725]),
|
|
{ },
|
|
};
|
|
|
|
static const struct iio_chan_spec_ext_info mcp4726_ext_info[] = {
|
|
{
|
|
.name = "powerdown",
|
|
.read = mcp4725_read_powerdown,
|
|
.write = mcp4725_write_powerdown,
|
|
.shared = IIO_SEPARATE,
|
|
},
|
|
IIO_ENUM("powerdown_mode", IIO_SEPARATE,
|
|
&mcp472x_powerdown_mode_enum[MCP4726]),
|
|
IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE,
|
|
&mcp472x_powerdown_mode_enum[MCP4726]),
|
|
{ },
|
|
};
|
|
|
|
static const struct iio_chan_spec mcp472x_channel[] = {
|
|
[MCP4725] = {
|
|
.type = IIO_VOLTAGE,
|
|
.indexed = 1,
|
|
.output = 1,
|
|
.channel = 0,
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
|
|
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
|
|
.ext_info = mcp4725_ext_info,
|
|
},
|
|
[MCP4726] = {
|
|
.type = IIO_VOLTAGE,
|
|
.indexed = 1,
|
|
.output = 1,
|
|
.channel = 0,
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
|
|
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
|
|
.ext_info = mcp4726_ext_info,
|
|
},
|
|
};
|
|
|
|
static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
|
|
{
|
|
struct mcp4725_data *data = iio_priv(indio_dev);
|
|
u8 outbuf[2];
|
|
int ret;
|
|
|
|
if (val >= (1 << 12) || val < 0)
|
|
return -EINVAL;
|
|
|
|
outbuf[0] = (val >> 8) & 0xf;
|
|
outbuf[1] = val & 0xff;
|
|
|
|
ret = i2c_master_send(data->client, outbuf, 2);
|
|
if (ret < 0)
|
|
return ret;
|
|
else if (ret != 2)
|
|
return -EIO;
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
static int mcp4726_set_cfg(struct iio_dev *indio_dev)
|
|
{
|
|
struct mcp4725_data *data = iio_priv(indio_dev);
|
|
u8 outbuf[3];
|
|
int ret;
|
|
|
|
outbuf[0] = 0x40;
|
|
outbuf[0] |= data->ref_mode << 3;
|
|
if (data->powerdown)
|
|
outbuf[0] |= data->powerdown << 1;
|
|
outbuf[1] = data->dac_value >> 4;
|
|
outbuf[2] = (data->dac_value & 0xf) << 4;
|
|
|
|
ret = i2c_master_send(data->client, outbuf, 3);
|
|
if (ret < 0)
|
|
return ret;
|
|
else if (ret != 3)
|
|
return -EIO;
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
static int mcp4725_read_raw(struct iio_dev *indio_dev,
|
|
struct iio_chan_spec const *chan,
|
|
int *val, int *val2, long mask)
|
|
{
|
|
struct mcp4725_data *data = iio_priv(indio_dev);
|
|
int ret;
|
|
|
|
switch (mask) {
|
|
case IIO_CHAN_INFO_RAW:
|
|
*val = data->dac_value;
|
|
return IIO_VAL_INT;
|
|
case IIO_CHAN_INFO_SCALE:
|
|
if (data->ref_mode == MCP472X_REF_VDD)
|
|
ret = regulator_get_voltage(data->vdd_reg);
|
|
else
|
|
ret = regulator_get_voltage(data->vref_reg);
|
|
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
*val = ret / 1000;
|
|
*val2 = 12;
|
|
return IIO_VAL_FRACTIONAL_LOG2;
|
|
}
|
|
return -EINVAL;
|
|
}
|
|
|
|
static int mcp4725_write_raw(struct iio_dev *indio_dev,
|
|
struct iio_chan_spec const *chan,
|
|
int val, int val2, long mask)
|
|
{
|
|
struct mcp4725_data *data = iio_priv(indio_dev);
|
|
int ret;
|
|
|
|
switch (mask) {
|
|
case IIO_CHAN_INFO_RAW:
|
|
ret = mcp4725_set_value(indio_dev, val);
|
|
data->dac_value = val;
|
|
break;
|
|
default:
|
|
ret = -EINVAL;
|
|
break;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static const struct iio_info mcp4725_info = {
|
|
.read_raw = mcp4725_read_raw,
|
|
.write_raw = mcp4725_write_raw,
|
|
.attrs = &mcp4725_attribute_group,
|
|
};
|
|
|
|
static int mcp4725_probe_dt(struct device *dev,
|
|
struct mcp4725_platform_data *pdata)
|
|
{
|
|
/* check if is the vref-supply defined */
|
|
pdata->use_vref = device_property_read_bool(dev, "vref-supply");
|
|
pdata->vref_buffered =
|
|
device_property_read_bool(dev, "microchip,vref-buffered");
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int mcp4725_probe(struct i2c_client *client)
|
|
{
|
|
const struct i2c_device_id *id = i2c_client_get_device_id(client);
|
|
struct mcp4725_data *data;
|
|
struct iio_dev *indio_dev;
|
|
struct mcp4725_platform_data *pdata, pdata_dt;
|
|
u8 inbuf[4];
|
|
u8 pd;
|
|
u8 ref;
|
|
int err;
|
|
|
|
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
|
|
if (indio_dev == NULL)
|
|
return -ENOMEM;
|
|
data = iio_priv(indio_dev);
|
|
i2c_set_clientdata(client, indio_dev);
|
|
data->client = client;
|
|
if (dev_fwnode(&client->dev))
|
|
data->id = (uintptr_t)device_get_match_data(&client->dev);
|
|
else
|
|
data->id = id->driver_data;
|
|
pdata = dev_get_platdata(&client->dev);
|
|
|
|
if (!pdata) {
|
|
err = mcp4725_probe_dt(&client->dev, &pdata_dt);
|
|
if (err) {
|
|
dev_err(&client->dev,
|
|
"invalid platform or devicetree data");
|
|
return err;
|
|
}
|
|
pdata = &pdata_dt;
|
|
}
|
|
|
|
if (data->id == MCP4725 && pdata->use_vref) {
|
|
dev_err(&client->dev,
|
|
"external reference is unavailable on MCP4725");
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (!pdata->use_vref && pdata->vref_buffered) {
|
|
dev_err(&client->dev,
|
|
"buffering is unavailable on the internal reference");
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (!pdata->use_vref)
|
|
data->ref_mode = MCP472X_REF_VDD;
|
|
else
|
|
data->ref_mode = pdata->vref_buffered ?
|
|
MCP472X_REF_VREF_BUFFERED :
|
|
MCP472X_REF_VREF_UNBUFFERED;
|
|
|
|
data->vdd_reg = devm_regulator_get(&client->dev, "vdd");
|
|
if (IS_ERR(data->vdd_reg))
|
|
return PTR_ERR(data->vdd_reg);
|
|
|
|
err = regulator_enable(data->vdd_reg);
|
|
if (err)
|
|
return err;
|
|
|
|
if (pdata->use_vref) {
|
|
data->vref_reg = devm_regulator_get(&client->dev, "vref");
|
|
if (IS_ERR(data->vref_reg)) {
|
|
err = PTR_ERR(data->vref_reg);
|
|
goto err_disable_vdd_reg;
|
|
}
|
|
|
|
err = regulator_enable(data->vref_reg);
|
|
if (err)
|
|
goto err_disable_vdd_reg;
|
|
}
|
|
|
|
indio_dev->name = id->name;
|
|
indio_dev->info = &mcp4725_info;
|
|
indio_dev->channels = &mcp472x_channel[id->driver_data];
|
|
indio_dev->num_channels = 1;
|
|
indio_dev->modes = INDIO_DIRECT_MODE;
|
|
|
|
/* read current DAC value and settings */
|
|
err = i2c_master_recv(client, inbuf, data->id == MCP4725 ? 3 : 4);
|
|
|
|
if (err < 0) {
|
|
dev_err(&client->dev, "failed to read DAC value");
|
|
goto err_disable_vref_reg;
|
|
}
|
|
pd = (inbuf[0] >> 1) & 0x3;
|
|
data->powerdown = pd > 0;
|
|
data->powerdown_mode = pd ? pd - 1 : 2; /* largest resistor to gnd */
|
|
data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
|
|
if (data->id == MCP4726)
|
|
ref = (inbuf[3] >> 3) & 0x3;
|
|
|
|
if (data->id == MCP4726 && ref != data->ref_mode) {
|
|
dev_info(&client->dev,
|
|
"voltage reference mode differs (conf: %u, eeprom: %u), setting %u",
|
|
data->ref_mode, ref, data->ref_mode);
|
|
err = mcp4726_set_cfg(indio_dev);
|
|
if (err < 0)
|
|
goto err_disable_vref_reg;
|
|
}
|
|
|
|
err = iio_device_register(indio_dev);
|
|
if (err)
|
|
goto err_disable_vref_reg;
|
|
|
|
return 0;
|
|
|
|
err_disable_vref_reg:
|
|
if (data->vref_reg)
|
|
regulator_disable(data->vref_reg);
|
|
|
|
err_disable_vdd_reg:
|
|
regulator_disable(data->vdd_reg);
|
|
|
|
return err;
|
|
}
|
|
|
|
static void mcp4725_remove(struct i2c_client *client)
|
|
{
|
|
struct iio_dev *indio_dev = i2c_get_clientdata(client);
|
|
struct mcp4725_data *data = iio_priv(indio_dev);
|
|
|
|
iio_device_unregister(indio_dev);
|
|
|
|
if (data->vref_reg)
|
|
regulator_disable(data->vref_reg);
|
|
regulator_disable(data->vdd_reg);
|
|
}
|
|
|
|
static const struct i2c_device_id mcp4725_id[] = {
|
|
{ "mcp4725", MCP4725 },
|
|
{ "mcp4726", MCP4726 },
|
|
{ }
|
|
};
|
|
MODULE_DEVICE_TABLE(i2c, mcp4725_id);
|
|
|
|
static const struct of_device_id mcp4725_of_match[] = {
|
|
{
|
|
.compatible = "microchip,mcp4725",
|
|
.data = (void *)MCP4725
|
|
},
|
|
{
|
|
.compatible = "microchip,mcp4726",
|
|
.data = (void *)MCP4726
|
|
},
|
|
{ }
|
|
};
|
|
MODULE_DEVICE_TABLE(of, mcp4725_of_match);
|
|
|
|
static struct i2c_driver mcp4725_driver = {
|
|
.driver = {
|
|
.name = MCP4725_DRV_NAME,
|
|
.of_match_table = mcp4725_of_match,
|
|
.pm = pm_sleep_ptr(&mcp4725_pm_ops),
|
|
},
|
|
.probe = mcp4725_probe,
|
|
.remove = mcp4725_remove,
|
|
.id_table = mcp4725_id,
|
|
};
|
|
module_i2c_driver(mcp4725_driver);
|
|
|
|
MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
|
|
MODULE_DESCRIPTION("MCP4725/6 12-bit DAC");
|
|
MODULE_LICENSE("GPL");
|