linux/drivers/iio/adc/pac1934.c

// SPDX-License-Identifier: GPL-2.0+
/*
 * IIO driver for PAC1934 Multi-Channel DC Power/Energy Monitor
 *
 * Copyright (C) 2017-2024 Microchip Technology Inc. and its subsidiaries
 *
 * Author: Bogdan Bolocan <[email protected]>
 * Author: Victor Tudose
 * Author: Marius Cristea <[email protected]>
 *
 * Datasheet for PAC1931, PAC1932, PAC1933 and PAC1934 can be found here:
 * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/PAC1931-Family-Data-Sheet-DS20005850E.pdf
 */

#include <linux/acpi.h>
#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/unaligned.h>

/*
 * maximum accumulation time should be (17 * 60 * 1000) around 17 minutes@1024 sps
 * till PAC1934 accumulation registers starts to saturate
 */
#define PAC1934_MAX_RFSH_LIMIT_MS
/* 50msec is the timeout for validity of the cached registers */
#define PAC1934_MIN_POLLING_TIME_MS
/*
 * 1000usec is the minimum wait time for normal conversions when sample
 * rate doesn't change
 */
#define PAC1934_MIN_UPDATE_WAIT_TIME_US

/* 32000mV */
#define PAC1934_VOLTAGE_MILLIVOLTS_MAX
/* voltage bits resolution when set for unsigned values */
#define PAC1934_VOLTAGE_U_RES
/* voltage bits resolution when set for signed values */
#define PAC1934_VOLTAGE_S_RES

/*
 * max signed value that can be stored on 32 bits and 8 digits fractional value
 * (2^31 - 1) * 10^8 + 99999999
 */
#define PAC_193X_MAX_POWER_ACC
/*
 * min signed value that can be stored on 32 bits and 8 digits fractional value
 * -(2^31) * 10^8 - 99999999
 */
#define PAC_193X_MIN_POWER_ACC

#define PAC1934_MAX_NUM_CHANNELS

#define PAC1934_MEAS_REG_LEN
#define PAC1934_CTRL_REG_LEN

#define PAC1934_DEFAULT_CHIP_SAMP_SPEED_HZ

/* I2C address map */
#define PAC1934_REFRESH_REG_ADDR
#define PAC1934_CTRL_REG_ADDR
#define PAC1934_ACC_COUNT_REG_ADDR
#define PAC1934_VPOWER_ACC_1_ADDR
#define PAC1934_VPOWER_ACC_2_ADDR
#define PAC1934_VPOWER_ACC_3_ADDR
#define PAC1934_VPOWER_ACC_4_ADDR
#define PAC1934_VBUS_1_ADDR
#define PAC1934_VBUS_2_ADDR
#define PAC1934_VBUS_3_ADDR
#define PAC1934_VBUS_4_ADDR
#define PAC1934_VSENSE_1_ADDR
#define PAC1934_VSENSE_2_ADDR
#define PAC1934_VSENSE_3_ADDR
#define PAC1934_VSENSE_4_ADDR
#define PAC1934_VBUS_AVG_1_ADDR
#define PAC1934_VBUS_AVG_2_ADDR
#define PAC1934_VBUS_AVG_3_ADDR
#define PAC1934_VBUS_AVG_4_ADDR
#define PAC1934_VSENSE_AVG_1_ADDR
#define PAC1934_VSENSE_AVG_2_ADDR
#define PAC1934_VSENSE_AVG_3_ADDR
#define PAC1934_VSENSE_AVG_4_ADDR
#define PAC1934_VPOWER_1_ADDR
#define PAC1934_VPOWER_2_ADDR
#define PAC1934_VPOWER_3_ADDR
#define PAC1934_VPOWER_4_ADDR
#define PAC1934_REFRESH_V_REG_ADDR
#define PAC1934_CTRL_STAT_REGS_ADDR
#define PAC1934_PID_REG_ADDR
#define PAC1934_MID_REG_ADDR
#define PAC1934_RID_REG_ADDR

/* PRODUCT ID REGISTER + MANUFACTURER ID REGISTER + REVISION ID REGISTER */
#define PAC1934_ID_REG_LEN
#define PAC1934_PID_IDX
#define PAC1934_MID_IDX
#define PAC1934_RID_IDX

#define PAC1934_ACPI_GET_NAMES_AND_MOHMS_VALS
#define PAC1934_ACPI_GET_UOHMS_VALS
#define PAC1934_ACPI_GET_BIPOLAR_SETTINGS
#define PAC1934_ACPI_GET_SAMP

#define PAC1934_SAMPLE_RATE_SHIFT

#define PAC1934_VBUS_SENSE_REG_LEN
#define PAC1934_ACC_REG_LEN
#define PAC1934_VPOWER_REG_LEN
#define PAC1934_VPOWER_ACC_REG_LEN
#define PAC1934_MAX_REGISTER_LENGTH

#define PAC1934_CUSTOM_ATTR_FOR_CHANNEL

/*
 * relative offsets when using multi-byte reads/writes even though these
 * bytes are read one after the other, they are not at adjacent memory
 * locations within the I2C memory map. The chip can skip some addresses
 */
#define PAC1934_CHANNEL_DIS_REG_OFF
#define PAC1934_NEG_PWR_REG_OFF

/*
 * when reading/writing multiple bytes from offset PAC1934_CHANNEL_DIS_REG_OFF,
 * the chip jumps over the 0x1E (REFRESH_G) and 0x1F (REFRESH_V) offsets
 */
#define PAC1934_SLOW_REG_OFF
#define PAC1934_CTRL_ACT_REG_OFF
#define PAC1934_CHANNEL_DIS_ACT_REG_OFF
#define PAC1934_NEG_PWR_ACT_REG_OFF
#define PAC1934_CTRL_LAT_REG_OFF
#define PAC1934_CHANNEL_DIS_LAT_REG_OFF
#define PAC1934_NEG_PWR_LAT_REG_OFF
#define PAC1934_PID_REG_OFF
#define PAC1934_MID_REG_OFF
#define PAC1934_REV_REG_OFF
#define PAC1934_CTRL_STATUS_INFO_LEN

#define PAC1934_MID
#define PAC1931_PID
#define PAC1932_PID
#define PAC1933_PID
#define PAC1934_PID

/* Scale constant = (10^3 * 3.2 * 10^9 / 2^28) for mili Watt-second */
#define PAC1934_SCALE_CONSTANT

#define PAC1934_MAX_VPOWER_RSHIFTED_BY_28B
#define PAC1934_MAX_VSENSE_RSHIFTED_BY_16B

#define PAC1934_DEV_ATTR(name)

#define PAC1934_CRTL_SAMPLE_RATE_MASK
#define PAC1934_CHAN_SLEEP_MASK
#define PAC1934_CHAN_SLEEP_SET
#define PAC1934_CHAN_SINGLE_MASK
#define PAC1934_CHAN_SINGLE_SHOT_SET
#define PAC1934_CHAN_ALERT_MASK
#define PAC1934_CHAN_ALERT_EN
#define PAC1934_CHAN_ALERT_CC_MASK
#define PAC1934_CHAN_ALERT_CC_EN
#define PAC1934_CHAN_OVF_ALERT_MASK
#define PAC1934_CHAN_OVF_ALERT_EN
#define PAC1934_CHAN_OVF_MASK

#define PAC1934_CHAN_DIS_CH1_OFF_MASK
#define PAC1934_CHAN_DIS_CH2_OFF_MASK
#define PAC1934_CHAN_DIS_CH3_OFF_MASK
#define PAC1934_CHAN_DIS_CH4_OFF_MASK
#define PAC1934_SMBUS_TIMEOUT_MASK
#define PAC1934_SMBUS_BYTECOUNT_MASK
#define PAC1934_SMBUS_NO_SKIP_MASK

#define PAC1934_NEG_PWR_CH1_BIDI_MASK
#define PAC1934_NEG_PWR_CH2_BIDI_MASK
#define PAC1934_NEG_PWR_CH3_BIDI_MASK
#define PAC1934_NEG_PWR_CH4_BIDI_MASK
#define PAC1934_NEG_PWR_CH1_BIDV_MASK
#define PAC1934_NEG_PWR_CH2_BIDV_MASK
#define PAC1934_NEG_PWR_CH3_BIDV_MASK
#define PAC1934_NEG_PWR_CH4_BIDV_MASK

/*
 * Universal Unique Identifier (UUID),
 * 033771E0-1705-47B4-9535-D1BBE14D9A09,
 * is reserved to Microchip for the PAC1934.
 */
#define PAC1934_DSM_UUID

enum pac1934_ids {};

enum pac1934_samps {};

/*
 * these indexes are exactly describing the element order within a single
 * PAC1934 phys channel IIO channel descriptor; see the static const struct
 * iio_chan_spec pac1934_single_channel[] declaration
 */
enum pac1934_ch_idx {};

/**
 * struct pac1934_features - features of a pac1934 instance
 * @phys_channels:	number of physical channels supported by the chip
 * @name:		chip's name
 */
struct pac1934_features {};

static const unsigned int samp_rate_map_tbl[] =;

static const struct pac1934_features pac1934_chip_config[] =;

/**
 * struct reg_data - data from the registers
 * @meas_regs:			snapshot of raw measurements registers
 * @ctrl_regs:			snapshot of control registers
 * @energy_sec_acc:		snapshot of energy values
 * @vpower_acc:			accumulated vpower values
 * @vpower:			snapshot of vpower registers
 * @vbus:			snapshot of vbus registers
 * @vbus_avg:			averages of vbus registers
 * @vsense:			snapshot of vsense registers
 * @vsense_avg:			averages of vsense registers
 * @num_enabled_channels:	count of how many chip channels are currently enabled
 */
struct reg_data {};

/**
 * struct pac1934_chip_info - information about the chip
 * @client:			the i2c-client attached to the device
 * @lock:			synchronize access to driver's state members
 * @work_chip_rfsh:		work queue used for refresh commands
 * @phys_channels:		phys channels count
 * @active_channels:		array of values, true means that channel is active
 * @enable_energy:		array of values, true means that channel energy is measured
 * @bi_dir:			array of bools, true means that channel is bidirectional
 * @chip_variant:		chip variant
 * @chip_revision:		chip revision
 * @shunts:			shunts
 * @chip_reg_data:		chip reg data
 * @sample_rate_value:		sampling frequency
 * @labels:			table with channels labels
 * @iio_info:			iio_info
 * @tstamp:			chip's uptime
 */
struct pac1934_chip_info {};

#define TO_PAC1934_CHIP_INFO(d)

#define PAC1934_VPOWER_ACC_CHANNEL(_index, _si, _address)

#define PAC1934_VBUS_CHANNEL(_index, _si, _address)

#define PAC1934_VBUS_AVG_CHANNEL(_index, _si, _address)

#define PAC1934_VSENSE_CHANNEL(_index, _si, _address)

#define PAC1934_VSENSE_AVG_CHANNEL(_index, _si, _address)

#define PAC1934_VPOWER_CHANNEL(_index, _si, _address)

static const struct iio_chan_spec pac1934_single_channel[] =;

/* Low-level I2c functions used to transfer up to 76 bytes at once */
static int pac1934_i2c_read(struct i2c_client *client, u8 reg_addr,
			    void *databuf, u8 len)
{}

static int pac1934_get_samp_rate_idx(struct pac1934_chip_info *info,
				     u32 new_samp_rate)
{}

static ssize_t pac1934_shunt_value_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{}

static ssize_t pac1934_shunt_value_store(struct device *dev,
					 struct device_attribute *attr,
					 const char *buf, size_t count)
{}

static int pac1934_read_avail(struct iio_dev *indio_dev,
			      struct iio_chan_spec const *channel,
			      const int **vals, int *type, int *length, long mask)
{}

static int pac1934_send_refresh(struct pac1934_chip_info *info,
				u8 refresh_cmd, u32 wait_time)
{}

static int pac1934_reg_snapshot(struct pac1934_chip_info *info,
				bool do_refresh, u8 refresh_cmd, u32 wait_time)
{}

static int pac1934_retrieve_data(struct pac1934_chip_info *info,
				 u32 wait_time)
{}

static int pac1934_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan, int *val,
			    int *val2, long mask)
{}

static int pac1934_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
			     int val, int val2, long mask)
{}

static int pac1934_read_label(struct iio_dev *indio_dev,
			      struct iio_chan_spec const *chan, char *label)
{}

static void pac1934_work_periodic_rfsh(struct work_struct *work)
{}

static int pac1934_read_revision(struct pac1934_chip_info *info, u8 *buf)
{}

static int pac1934_chip_identify(struct pac1934_chip_info *info)
{}

/*
 * documentation related to the ACPI device definition
 * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/PAC1934-Integration-Notes-for-Microsoft-Windows-10-and-Windows-11-Driver-Support-DS00002534.pdf
 */
static int pac1934_acpi_parse_channel_config(struct i2c_client *client,
					     struct pac1934_chip_info *info)
{}

static int pac1934_fw_parse_channel_config(struct i2c_client *client,
					   struct pac1934_chip_info *info)
{}

static void pac1934_cancel_delayed_work(void *dwork)
{}

static int pac1934_chip_configure(struct pac1934_chip_info *info)
{}

static int pac1934_prep_iio_channels(struct pac1934_chip_info *info, struct iio_dev *indio_dev)
{}

static IIO_DEVICE_ATTR(in_shunt_resistor1, 0644,
		       pac1934_shunt_value_show, pac1934_shunt_value_store, 0);
static IIO_DEVICE_ATTR(in_shunt_resistor2, 0644,
		       pac1934_shunt_value_show, pac1934_shunt_value_store, 1);
static IIO_DEVICE_ATTR(in_shunt_resistor3, 0644,
		       pac1934_shunt_value_show, pac1934_shunt_value_store, 2);
static IIO_DEVICE_ATTR(in_shunt_resistor4, 0644,
		       pac1934_shunt_value_show, pac1934_shunt_value_store, 3);

static int pac1934_prep_custom_attributes(struct pac1934_chip_info *info,
					  struct iio_dev *indio_dev)
{}

static void pac1934_mutex_destroy(void *data)
{}

static const struct iio_info pac1934_info =;

static int pac1934_probe(struct i2c_client *client)
{}

static const struct i2c_device_id pac1934_id[] =;
MODULE_DEVICE_TABLE(i2c, pac1934_id);

static const struct of_device_id pac1934_of_match[] =;
MODULE_DEVICE_TABLE(of, pac1934_of_match);

/*
 * using MCHP1930 to be compatible with BIOS ACPI. See example:
 * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/PAC1934-Integration-Notes-for-Microsoft-Windows-10-and-Windows-11-Driver-Support-DS00002534.pdf
 */
static const struct acpi_device_id pac1934_acpi_match[] =;
MODULE_DEVICE_TABLE(acpi, pac1934_acpi_match);

static struct i2c_driver pac1934_driver =;

module_i2c_driver();

MODULE_AUTHOR();
MODULE_AUTHOR();
MODULE_AUTHOR();
MODULE_DESCRIPTION();
MODULE_LICENSE();