Message ID | 20200624151736.95785-1-alexandru.tachici@analog.com |
---|---|
Headers | show |
Series | hwmon: pmbus: adm1266: add support | expand |
On 6/24/20 8:17 AM, alexandru.tachici@analog.com wrote: > From: Alexandru Tachici <alexandru.tachici@analog.com> > > Add two ioctl commands for reading the current state > of the adm1266 sequencer and sending commands. > Please note that I am not going to a accept any ioctls for hwmon drivers, much less unprivileged commands like this. Guenter > Signed-off-by: Alexandru Tachici <alexandru.tachici@analog.com> > --- > Documentation/hwmon/adm1266.rst | 15 +++ > .../userspace-api/ioctl/ioctl-number.rst | 1 + > drivers/hwmon/pmbus/adm1266.c | 97 +++++++++++++++++++ > include/uapi/linux/adm1266.h | 16 +++ > 4 files changed, 129 insertions(+) > create mode 100644 include/uapi/linux/adm1266.h > > diff --git a/Documentation/hwmon/adm1266.rst b/Documentation/hwmon/adm1266.rst > index 65662115750c..5dc05808db60 100644 > --- a/Documentation/hwmon/adm1266.rst > +++ b/Documentation/hwmon/adm1266.rst > @@ -33,3 +33,18 @@ inX_min Minimum Voltage. > inX_max Maximum voltage. > inX_min_alarm Voltage low alarm. > inX_max_alarm Voltage high alarm. > + > + > +User API > +======== > + > +ioctls > +------ > + > +ADM1266_SET_GO_COMMAND: > + > + Issue a GO_COMMAND to the device. > + > +ADM1266_GET_STATUS: > + > + Returns state of the sequencer. > diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst > index 59472cd6a11d..df92ca274622 100644 > --- a/Documentation/userspace-api/ioctl/ioctl-number.rst > +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst > @@ -348,6 +348,7 @@ Code Seq# Include File Comments > 0xCC 00-0F drivers/misc/ibmvmc.h pseries VMC driver > 0xCD 01 linux/reiserfs_fs.h > 0xCF 02 fs/cifs/ioctl.c > +0xD1 00-0F linux/adm1266.h > 0xDB 00-0F drivers/char/mwave/mwavepub.h > 0xDD 00-3F ZFCP device driver see drivers/s390/scsi/ > <mailto:aherrman@de.ibm.com> > diff --git a/drivers/hwmon/pmbus/adm1266.c b/drivers/hwmon/pmbus/adm1266.c > index 76bf2c78e737..0960ead8d96a 100644 > --- a/drivers/hwmon/pmbus/adm1266.c > +++ b/drivers/hwmon/pmbus/adm1266.c > @@ -15,11 +15,16 @@ > #include <linux/init.h> > #include <linux/kernel.h> > #include <linux/module.h> > +#include <linux/proc_fs.h> > #include <linux/slab.h> > +#include <linux/uaccess.h> > > +#include <linux/adm1266.h> > #include "pmbus.h" > > #define ADM1266_PDIO_CONFIG 0xD4 > +#define ADM1266_GO_COMMAND 0xD8 > +#define ADM1266_READ_STATE 0xD9 > #define ADM1266_GPIO_CONFIG 0xE1 > #define ADM1266_PDIO_STATUS 0xE9 > #define ADM1266_GPIO_STATUS 0xEA > @@ -46,6 +51,7 @@ struct adm1266_data { > struct gpio_chip gc; > const char *gpio_names[ADM1266_GPIO_NR + ADM1266_PDIO_NR]; > struct i2c_client *client; > + struct mutex ioctl_mutex; /* lock ioctl access */ > }; > > /* Different from Block Read as it sends data and waits for the slave to > @@ -311,6 +317,93 @@ static int adm1266_config_gpio(struct adm1266_data *data) > } > #endif > > +static int adm1266_set_go_command_op(struct adm1266_data *data, u8 val) > +{ > + val = FIELD_GET(GENMASK(4, 0), val); > + > + return i2c_smbus_write_word_data(data->client, ADM1266_GO_COMMAND, val); > +} > + > +static int adm1266_ioctl_unlocked(struct file *fp, unsigned int cmd, > + unsigned long arg) > +{ > + int __user *argp = (int __user *)arg; > + struct adm1266_data *data; > + int val; > + int ret; > + > + data = fp->private_data; > + > + if (!argp) > + return -EINVAL; > + > + switch (cmd) { > + case ADM1266_SET_GO_COMMAND: > + if (copy_from_user(&val, argp, sizeof(int))) > + return -EFAULT; > + > + return adm1266_set_go_command_op(data, val); > + case ADM1266_GET_STATUS: > + ret = i2c_smbus_read_word_data(data->client, > + ADM1266_READ_STATE); > + > + if (ret < 0) > + return ret; > + > + if (copy_to_user(argp, &ret, sizeof(int))) > + return -EFAULT; > + > + break; > + default: > + return -ENOTTY; > + } > + > + return 0; > +} > + > +static long adm1266_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) > +{ > + struct adm1266_data *data; > + long ret; > + > + data = fp->private_data; > + > + mutex_lock(&data->ioctl_mutex); > + ret = adm1266_ioctl_unlocked(fp, cmd, arg); > + mutex_unlock(&data->ioctl_mutex); > + > + return ret; > +} > + > +static int adm1266_open(struct inode *inode, struct file *filp) > +{ > + filp->private_data = PDE_DATA(inode); > + > + return 0; > +} > + > +static const struct proc_ops adm1266_proc_ops = { > + .proc_open = adm1266_open, > + .proc_ioctl = adm1266_ioctl, > +}; > + > +static int adm1266_init_procfs(struct adm1266_data *data) > +{ > + struct proc_dir_entry *proc_dir; > + u8 proc_fs_name[32]; > + > + mutex_init(&data->ioctl_mutex); > + > + snprintf(proc_fs_name, 32, "adm1266-%x", data->client->addr); > + proc_dir = proc_create_data(proc_fs_name, 0, NULL, &adm1266_proc_ops, > + data); > + > + if (!proc_dir) > + return -EINVAL; > + > + return 0; > +} > + > static int adm1266_probe(struct i2c_client *client, > const struct i2c_device_id *id) > { > @@ -333,6 +426,10 @@ static int adm1266_probe(struct i2c_client *client, > > crc8_populate_msb(pmbus_crc_table, 0x7); > > + ret = adm1266_init_procfs(data); > + if (ret < 0) > + return ret; > + > info = &data->info; > info->pages = 17; > info->format[PSC_VOLTAGE_OUT] = linear; > diff --git a/include/uapi/linux/adm1266.h b/include/uapi/linux/adm1266.h > new file mode 100644 > index 000000000000..535d270ee8c5 > --- /dev/null > +++ b/include/uapi/linux/adm1266.h > @@ -0,0 +1,16 @@ > +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ > +/* > + * ADM1266 - Cascadable Super Sequencer with Margin > + * Control and Fault Recording > + * > + * Copyright 2020 Analog Devices Inc. > + */ > + > +#ifndef _LINUX_ADM1266_H > +#define _LINUX_ADM1266_H > + > +/* ADM1266 ioctl commands */ > +#define ADM1266_SET_GO_COMMAND _IOW(0xD1, 0x00, int) > +#define ADM1266_GET_STATUS _IOR(0xD1, 0x01, int) > + > +#endif >
On Wed, Jun 24, 2020 at 06:17:35PM +0300, alexandru.tachici@analog.com wrote: > From: Alexandru Tachici <alexandru.tachici@analog.com> > > Add a debugfs file to print information in the > BLACKBOX_INFORMATION register. Contains information > about the number of stored records, logic index and id > of the latest record. How is this different to the nvram method implemented in toe previous patch ? Why do we need both ? > > Signed-off-by: Alexandru Tachici <alexandru.tachici@analog.com> > --- > drivers/hwmon/pmbus/adm1266.c | 56 ++++++++++++++++++++++++++++++++++- > 1 file changed, 55 insertions(+), 1 deletion(-) > > diff --git a/drivers/hwmon/pmbus/adm1266.c b/drivers/hwmon/pmbus/adm1266.c > index b9e92ab1e39a..ea2dc481094b 100644 > --- a/drivers/hwmon/pmbus/adm1266.c > +++ b/drivers/hwmon/pmbus/adm1266.c > @@ -60,6 +60,7 @@ struct adm1266_data { > const char *gpio_names[ADM1266_GPIO_NR + ADM1266_PDIO_NR]; > struct i2c_client *client; > struct mutex ioctl_mutex; /* lock ioctl access */ > + struct dentry *debugfs_dir; > struct nvmem_config nvmem_config; > struct nvmem_device *nvmem; > u8 *dev_mem; > @@ -406,6 +407,28 @@ static const struct proc_ops adm1266_proc_ops = { > .proc_ioctl = adm1266_ioctl, > }; > > +static int adm1266_blackbox_information_read(struct seq_file *s, void *pdata) > +{ > + struct device *dev = s->private; > + struct i2c_client *client = to_i2c_client(dev); > + u8 read_buf[5]; > + unsigned int latest_id; > + int ret; > + > + ret = i2c_smbus_read_block_data(client, ADM1266_BLACKBOX_INFO, > + read_buf); > + if (ret < 0) > + return ret; > + > + seq_puts(s, "BLACKBOX_INFORMATION:\n"); > + latest_id = read_buf[0] + (read_buf[1] << 8); > + seq_printf(s, "Black box ID: %u\n", latest_id); > + seq_printf(s, "Logic index: %u\n", read_buf[2]); > + seq_printf(s, "Record count: %u\n", read_buf[3]); > + > + return 0; > +} > + > static int adm1266_init_procfs(struct adm1266_data *data) > { > struct proc_dir_entry *proc_dir; > @@ -423,6 +446,29 @@ static int adm1266_init_procfs(struct adm1266_data *data) > return 0; > } > > +static int adm1266_init_debugfs(struct adm1266_data *data) > +{ > + struct dentry *entry; > + struct dentry *root; > + > + root = pmbus_get_debugfs_dir(data->client); > + if (!root) > + return -ENOENT; > + > + data->debugfs_dir = debugfs_create_dir(data->client->name, root); > + if (!data->debugfs_dir) > + return -ENOENT; > + > + entry = debugfs_create_devm_seqfile(&data->client->dev, > + "blackbox_information", > + data->debugfs_dir, > + adm1266_blackbox_information_read); > + if (!entry) > + return -ENOENT; > + > + return 0; > +} > + > static int adm1266_nvmem_read_blackbox(struct adm1266_data *data, u8 *buf) > { > u8 read_buf[5]; > @@ -571,7 +617,15 @@ static int adm1266_probe(struct i2c_client *client, > for (i = 0; i < info->pages; i++) > info->func[i] = funcs; > > - return pmbus_do_probe(client, id, info); > + ret = pmbus_do_probe(client, id, info); > + if (ret) > + return ret; > + > + ret = adm1266_init_debugfs(data); > + if (ret) > + dev_warn(&client->dev, "Failed to register debugfs: %d\n", ret); debugfs functions are supposed to fail silently. > + > + return 0; > } > > static const struct of_device_id adm1266_of_match[] = {
From: Alexandru Tachici <alexandru.tachici@analog.com> Add PMBus probing driver for the adm1266 Cascadable Super Sequencer with Margin Control and Fault Recording. Driver is using the pmbus_core, creating sysfs files under hwmon for inputs: vh1->vh4 and vp1->vp13. 1. Add PMBus probing driver for inputs vh1->vh4 and vp1->vp13. 2. Add Block Write-Read Process Call command. A PMBus specific implementation was required because block write with I2C_SMBUS_PROC_CALL flag allows a maximum of 32 bytes to be received. 3. This makes adm1266 driver expose GPIOs to user-space. Currently are read only. Future developments on the firmware will allow them to be writable. 4. Add two ioctl commands for issuing GO_COMMAND and reading the state of the adm1266 sequencer. 5. Blackboxes are 64 bytes of chip state related data that is generated on faults. Use the nvmem kernel api to expose the blackbox chip functionality to userspace. 6. Expose BLACKBOX_INFO register through debugfs. 7. Device tree bindings for ADM1266. Alexandru Tachici (7): hwmon: pmbus: adm1266: add support hwmon: pmbus: adm1266: Add Block process call hwmon: pmbus: adm1266: Add support for GPIOs hwmon: pmbus: adm1266: Add ioctl commands hwmon: pmbus: adm1266: read blackbox hwmon: pmbus: adm1266: debugfs for blackbox info dt-bindings: hwmon: Add bindings for ADM1266 Changelog v3 -> v4: - moved pmbus_block_wr (pmbus process call) from pmbus_core. to adm1266.c and renamed to pmbus_block_xfer - in pmbus_block_xfer: fixed buffer size bug (from 255 to 257) - in adm1266_gpio_get_multiple: handle pdios and gpios one at a time to lower allocated space on stack - in adm1266_gpio_dbg_show: replaced write_buf with u8 write_cmd var - in adm1266_gpio_dbg_show: check number of bytes received from device returned by pmbus_block_xfer. - now use ioctl to send GO_COMMAND and retrieve current state of adm1266 - split blackbox commit into blackbox nvmem implementation and debugfs blackbox info debugfs - create adm1266 debugfs dir under /sys/kernel/debug/pmbus/hwmon for blackbox_info Changelog v4 -> v5: - added WITH Linux-syscall-note to adm1266.h .../bindings/hwmon/adi,adm1266.yaml | 56 ++ Documentation/hwmon/adm1266.rst | 50 ++ .../userspace-api/ioctl/ioctl-number.rst | 1 + drivers/hwmon/pmbus/Kconfig | 10 + drivers/hwmon/pmbus/Makefile | 1 + drivers/hwmon/pmbus/adm1266.c | 657 ++++++++++++++++++ include/uapi/linux/adm1266.h | 16 + 7 files changed, 791 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/adi,adm1266.yaml create mode 100644 Documentation/hwmon/adm1266.rst create mode 100644 drivers/hwmon/pmbus/adm1266.c create mode 100644 include/uapi/linux/adm1266.h