162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright 2021 Google LLC. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Code shared between most Semtech SAR sensor driver. 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#ifndef IIO_SX_COMMON_H 962306a36Sopenharmony_ci#define IIO_SX_COMMON_H 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#include <linux/acpi.h> 1262306a36Sopenharmony_ci#include <linux/iio/iio.h> 1362306a36Sopenharmony_ci#include <linux/iio/types.h> 1462306a36Sopenharmony_ci#include <linux/regulator/consumer.h> 1562306a36Sopenharmony_ci#include <linux/types.h> 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_cistruct device; 1862306a36Sopenharmony_cistruct i2c_client; 1962306a36Sopenharmony_cistruct regmap_config; 2062306a36Sopenharmony_cistruct sx_common_data; 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci#define SX_COMMON_REG_IRQ_SRC 0x00 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci#define SX_COMMON_MAX_NUM_CHANNELS 4 2562306a36Sopenharmony_cistatic_assert(SX_COMMON_MAX_NUM_CHANNELS < BITS_PER_LONG); 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_cistruct sx_common_reg_default { 2862306a36Sopenharmony_ci u8 reg; 2962306a36Sopenharmony_ci u8 def; 3062306a36Sopenharmony_ci const char *property; 3162306a36Sopenharmony_ci}; 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci/** 3462306a36Sopenharmony_ci * struct sx_common_ops: function pointers needed by common code 3562306a36Sopenharmony_ci * 3662306a36Sopenharmony_ci * List functions needed by common code to gather information or configure 3762306a36Sopenharmony_ci * the sensor. 3862306a36Sopenharmony_ci * 3962306a36Sopenharmony_ci * @read_prox_data: Function to read raw proximity data. 4062306a36Sopenharmony_ci * @check_whoami: Set device name based on whoami register. 4162306a36Sopenharmony_ci * @init_compensation: Function to set initial compensation. 4262306a36Sopenharmony_ci * @wait_for_sample: When there are no physical IRQ, function to wait for a 4362306a36Sopenharmony_ci * sample to be ready. 4462306a36Sopenharmony_ci * @get_default_reg: Populate the initial value for a given register. 4562306a36Sopenharmony_ci */ 4662306a36Sopenharmony_cistruct sx_common_ops { 4762306a36Sopenharmony_ci int (*read_prox_data)(struct sx_common_data *data, 4862306a36Sopenharmony_ci const struct iio_chan_spec *chan, __be16 *val); 4962306a36Sopenharmony_ci int (*check_whoami)(struct device *dev, struct iio_dev *indio_dev); 5062306a36Sopenharmony_ci int (*init_compensation)(struct iio_dev *indio_dev); 5162306a36Sopenharmony_ci int (*wait_for_sample)(struct sx_common_data *data); 5262306a36Sopenharmony_ci const struct sx_common_reg_default * 5362306a36Sopenharmony_ci (*get_default_reg)(struct device *dev, int idx, 5462306a36Sopenharmony_ci struct sx_common_reg_default *reg_def); 5562306a36Sopenharmony_ci}; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci/** 5862306a36Sopenharmony_ci * struct sx_common_chip_info: Semtech Sensor private chip information 5962306a36Sopenharmony_ci * 6062306a36Sopenharmony_ci * @reg_stat: Main status register address. 6162306a36Sopenharmony_ci * @reg_irq_msk: IRQ mask register address. 6262306a36Sopenharmony_ci * @reg_enable_chan: Address to enable/disable channels. 6362306a36Sopenharmony_ci * Each phase presented by the sensor is an IIO channel.. 6462306a36Sopenharmony_ci * @reg_reset: Reset register address. 6562306a36Sopenharmony_ci * @mask_enable_chan: Mask over the channels bits in the enable channel 6662306a36Sopenharmony_ci * register. 6762306a36Sopenharmony_ci * @stat_offset: Offset to check phase status. 6862306a36Sopenharmony_ci * @irq_msk_offset: Offset to enable interrupt in the IRQ mask 6962306a36Sopenharmony_ci * register. 7062306a36Sopenharmony_ci * @num_channels: Number of channels. 7162306a36Sopenharmony_ci * @num_default_regs: Number of internal registers that can be configured. 7262306a36Sopenharmony_ci * 7362306a36Sopenharmony_ci * @ops: Private functions pointers. 7462306a36Sopenharmony_ci * @iio_channels: Description of exposed iio channels. 7562306a36Sopenharmony_ci * @num_iio_channels: Number of iio_channels. 7662306a36Sopenharmony_ci * @iio_info: iio_info structure for this driver. 7762306a36Sopenharmony_ci */ 7862306a36Sopenharmony_cistruct sx_common_chip_info { 7962306a36Sopenharmony_ci unsigned int reg_stat; 8062306a36Sopenharmony_ci unsigned int reg_irq_msk; 8162306a36Sopenharmony_ci unsigned int reg_enable_chan; 8262306a36Sopenharmony_ci unsigned int reg_reset; 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci unsigned int mask_enable_chan; 8562306a36Sopenharmony_ci unsigned int stat_offset; 8662306a36Sopenharmony_ci unsigned int irq_msk_offset; 8762306a36Sopenharmony_ci unsigned int num_channels; 8862306a36Sopenharmony_ci int num_default_regs; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci struct sx_common_ops ops; 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci const struct iio_chan_spec *iio_channels; 9362306a36Sopenharmony_ci int num_iio_channels; 9462306a36Sopenharmony_ci struct iio_info iio_info; 9562306a36Sopenharmony_ci}; 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci/** 9862306a36Sopenharmony_ci * struct sx_common_data: Semtech Sensor private data structure. 9962306a36Sopenharmony_ci * 10062306a36Sopenharmony_ci * @chip_info: Structure defining sensor internals. 10162306a36Sopenharmony_ci * @mutex: Serialize access to registers and channel configuration. 10262306a36Sopenharmony_ci * @completion: completion object to wait for data acquisition. 10362306a36Sopenharmony_ci * @client: I2C client structure. 10462306a36Sopenharmony_ci * @trig: IIO trigger object. 10562306a36Sopenharmony_ci * @regmap: Register map. 10662306a36Sopenharmony_ci * @chan_prox_stat: Last reading of the proximity status for each channel. 10762306a36Sopenharmony_ci * We only send an event to user space when this changes. 10862306a36Sopenharmony_ci * @trigger_enabled: True when the device trigger is enabled. 10962306a36Sopenharmony_ci * @buffer: Buffer to store raw samples. 11062306a36Sopenharmony_ci * @suspend_ctrl: Remember enabled channels and sample rate during suspend. 11162306a36Sopenharmony_ci * @chan_read: Bit field for each raw channel enabled. 11262306a36Sopenharmony_ci * @chan_event: Bit field for each event enabled. 11362306a36Sopenharmony_ci */ 11462306a36Sopenharmony_cistruct sx_common_data { 11562306a36Sopenharmony_ci const struct sx_common_chip_info *chip_info; 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci struct mutex mutex; 11862306a36Sopenharmony_ci struct completion completion; 11962306a36Sopenharmony_ci struct i2c_client *client; 12062306a36Sopenharmony_ci struct iio_trigger *trig; 12162306a36Sopenharmony_ci struct regmap *regmap; 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci unsigned long chan_prox_stat; 12462306a36Sopenharmony_ci bool trigger_enabled; 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci /* Ensure correct alignment of timestamp when present. */ 12762306a36Sopenharmony_ci struct { 12862306a36Sopenharmony_ci __be16 channels[SX_COMMON_MAX_NUM_CHANNELS]; 12962306a36Sopenharmony_ci s64 ts __aligned(8); 13062306a36Sopenharmony_ci } buffer; 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci unsigned int suspend_ctrl; 13362306a36Sopenharmony_ci unsigned long chan_read; 13462306a36Sopenharmony_ci unsigned long chan_event; 13562306a36Sopenharmony_ci}; 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ciint sx_common_read_proximity(struct sx_common_data *data, 13862306a36Sopenharmony_ci const struct iio_chan_spec *chan, int *val); 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ciint sx_common_read_event_config(struct iio_dev *indio_dev, 14162306a36Sopenharmony_ci const struct iio_chan_spec *chan, 14262306a36Sopenharmony_ci enum iio_event_type type, 14362306a36Sopenharmony_ci enum iio_event_direction dir); 14462306a36Sopenharmony_ciint sx_common_write_event_config(struct iio_dev *indio_dev, 14562306a36Sopenharmony_ci const struct iio_chan_spec *chan, 14662306a36Sopenharmony_ci enum iio_event_type type, 14762306a36Sopenharmony_ci enum iio_event_direction dir, int state); 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ciint sx_common_probe(struct i2c_client *client, 15062306a36Sopenharmony_ci const struct sx_common_chip_info *chip_info, 15162306a36Sopenharmony_ci const struct regmap_config *regmap_config); 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_civoid sx_common_get_raw_register_config(struct device *dev, 15462306a36Sopenharmony_ci struct sx_common_reg_default *reg_def); 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci/* 3 is the number of events defined by a single phase. */ 15762306a36Sopenharmony_ciextern const struct iio_event_spec sx_common_events[3]; 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci#endif /* IIO_SX_COMMON_H */ 160