Documentation: dt-bindings: Document bindings for Aspeed AST2400/AST2500 ADC
PATCH 1/2 Documentation: dt-bindings: Document bindings for Aspeed AST2400/AST2500 ADC
--- .../devicetree/bindings/hwmon/aspeed_adc.txt | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/aspeed_adc.txt
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +48 -0
@@ -0,0 +1,48 @@ +Aspeed AST2400/2500 ADC + +This device is a 10-bit converter for 16 voltage channels. All inputs are +single ended. Each channel can be individually enabled to allow for use of +alternate pin functions. + +1) adc node + + Required properties: + - compatible : Should be one of + "aspeed,ast2400-adc" + "aspeed,ast2500-adc" + - reg : memory window mapping address and length + - #address-cells : must be <1> corresponding to the channel child binding + - #size-cells : must be <0> corresponding to the channel child binding + - clocks : Input clock used to derive the sample clock. Expected to be the + SoC's APB clock. + - update-interval-ms : initial time between updates on a channel + + The node contains child nodes for each channel that the platform uses. + + Example adc node: + adc@1e6e9000 { + #address-cells = <1>; + #size-cells = <0>; + compatible = "aspeed,ast2400-adc"; + reg = <0x1e6e9000 0xB0>; + clocks = <&clk_apb>; + update-interval-ms = <100>; + + [ child node definitions... ] + }; + +2) channel nodes + + Optional properties: + - status: indicates the operational status of the device. + Value must be either "disabled" or "okay". + - label : string describing the monitored value + + Example channel node: + channel@1 { + status = "okay"; + label = "3V3 rail"; + + pinctrl-names = "default"; + pinctrl-0 = <&pinctrl_adc0_default>; + };
Signature · 6 lines
2.11.0.483.g087da7b7c-goog -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 2/2 hwmon: Aspeed AST2400/AST2500 ADC
Aspeed AST2400/AST2500 BMC SoCs include a 16 channel, 10-bit ADC. This driver implements reading the ADC values, enabling channels via device tree, and optionally providing channel labels via device tree. Low and high threshold interrupts are supported by the hardware but not implemented.
--- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/aspeed_adc.c | 383 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 394 insertions(+) create mode 100644 drivers/hwmon/aspeed_adc.c
drivers/hwmon/Kconfig +10 -0
@@ -261,6 +261,16 @@ config SENSORS_ASC7621 This driver can also be built as a module. If so, the module will be called asc7621. +config SENSORS_ASPEED_ADC + tristate "Aspeed AST2400/AST2500 ADC" + depends on ARCH_ASPEED + help + If you say yes here you get support for the Aspeed AST2400/AST2500 + ADC. + + This driver can also be built as a module. If so, the module + will be called aspeed_adc. + config SENSORS_K8TEMP tristate "AMD Athlon64/FX or Opteron temperature sensor" depends on X86 && PCI
drivers/hwmon/Makefile +1 -0
@@ -46,6 +46,7 @@ obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.o obj-$(CONFIG_SENSORS_ASC7621) += asc7621.o +obj-$(CONFIG_SENSORS_ASPEED_ADC) += aspeed_adc.o obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o obj-$(CONFIG_SENSORS_DA9052_ADC)+= da9052-hwmon.o
drivers/hwmon/aspeed_adc.c new file +383 -0
383 lines not on this page · raw message
Signature · 1 line
2.11.0.483.g087da7b7c-goog
Joel Stanley
maintainerRe: [PATCH 2/2] hwmon: Aspeed AST2400/AST2500 ADC
Quoted 7 lines · Rick Altherr
Aspeed AST2400/AST2500 BMC SoCs include a 16 channel, 10-bit ADC. This driver implements reading the ADC values, enabling channels via device tree, and optionally providing channel labels via device tree. Low and high threshold interrupts are supported by the hardware but not implemented.
Signed-off-by Rick Altherr <[email protected]>
Looks good. Some minor comments below.
Is there a reason you wrote a hwmon driver instead of an iio driver? I wasn't sure what the recommended subsystem is.
Quoted 12 lines
--- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/aspeed_adc.c | 383 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 394 insertions(+) create mode 100644 drivers/hwmon/aspeed_adc.cdrivers/hwmon/Kconfig +0
-0
drivers/hwmon/Kconfig config SENSORS_ASC7621 · line 266
Quoted 6 lines
drivers/hwmon/Kconfig +3
-0@@ -261,6 +261,16 @@ config SENSORS_ASC7621 This driver can also be built as a module. If so, the module will be called asc7621. +config SENSORS_ASPEED_ADC + tristate "Aspeed AST2400/AST2500 ADC" + depends on ARCH_ASPEED
depends on ARCH_ASPEED || COMPILE_TEST.
Quoted 57 lines
drivers/hwmon/Kconfig +7
-0+ help + If you say yes here you get support for the Aspeed AST2400/AST2500 + ADC. + + This driver can also be built as a module. If so, the module + will be called aspeed_adc. + config SENSORS_K8TEMP tristate "AMD Athlon64/FX or Opteron temperature sensor" depends on X86 && PCIdrivers/hwmon/Makefile +1
-0@@ -46,6 +46,7 @@ obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.o obj-$(CONFIG_SENSORS_ASC7621) += asc7621.o +obj-$(CONFIG_SENSORS_ASPEED_ADC) += aspeed_adc.o obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o obj-$(CONFIG_SENSORS_DA9052_ADC)+= da9052-hwmon.odrivers/hwmon/aspeed_adc.c new file +29
-0@@ -0,0 +1,383 @@ +/* +* Aspeed AST2400/2500 ADC +* +* Copyright (C) 2017 Google, Inc. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +*/ + +#include <asm/io.h> +#include <linux/clk.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/of_platform.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/types.h> + +#define ASPEED_ADC_NUM_CHANNELS 16 +#define ASPEED_ADC_REF_VOLTAGE 2500/* millivolts */ + +#define ASPEED_ADC_REG_ADC00 0x00 +#define ASPEED_ADC_REG_ADC04 0x04
drivers/hwmon/aspeed_adc.c line 39
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+#define ASPEED_ADC_REG_ADC08 0x08 +#define ASPEED_ADC_REG_ADC0C 0x0C +#define ASPEED_ADC_REG_ADC10 0x10 +#define ASPEED_ADC_REG_ADC14 0x14 +#define ASPEED_ADC_REG_ADC18 0x18 +#define ASPEED_ADC_REG_ADC1C 0x1C +#define ASPEED_ADC_REG_ADC20 0x20 +#define ASPEED_ADC_REG_ADC24 0x24 +#define ASPEED_ADC_REG_ADC28 0x28 +#define ASPEED_ADC_REG_ADC2C 0x2C
I'm not sure that these defines add any value. I'd either give them names such as "ASPEED_ADC_ENGINE_CTRL". or just use the register offset directly.
Quoted 17 lines
drivers/hwmon/aspeed_adc.c new file +17
-0+ +#define ASPEED_ADC_OPERATION_MODE_POWER_DOWN (0x0 << 1) +#define ASPEED_ADC_OPERATION_MODE_STANDBY (0x1 << 1) +#define ASPEED_ADC_OPERATION_MODE_NORMAL (0x7 << 1) + +#define ASPEED_ADC_ENGINE_ENABLE BIT(0) + +struct aspeed_adc_data { +struct device *dev; +void __iomem *base; +struct clk *pclk; +struct mutex lock; +unsigned long update_interval_ms; +u32 enabled_channel_mask; +const char * channel_labels[ASPEED_ADC_NUM_CHANNELS ]; +}; +
drivers/hwmon/aspeed_adc.c line 66
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+static int aspeed_adc_set_adc_clock_frequency ( +struct aspeed_adc_data *data, +unsigned long desired_update_interval_ms) +{ +long pclk_hz =clk_get_rate (data->pclk); +long adc_combined_divisor; +long adc_pre_divisor; +long adc_divisor; +long adc_clock_control_reg_val; +long num_enabled_channels =hweight32 (data->enabled_channel_mask);
Some whitespace damage here.
drivers/hwmon/aspeed_adc.c line 71
Quoted 5 lines
drivers/hwmon/aspeed_adc.c new file +5
-0+ +if (desired_update_interval_ms <1 ) +return -EINVAL ; + +/* From the AST2400 datasheet:
Nit: kernel style is to leave a blank line on the first line of multi-line comments:
/*
* Foo
* etc
Quoted 54 lines
drivers/hwmon/aspeed_adc.c new file +54
-0+* adc_period_s = pclk_period_s * 2 * (pre_divisor+1) * (divisor+1) +* +* and +* +* sample_period_s = 12 * adc_period_s +* +* Substitute pclk_period_s = (1 / pclk_hz) +* +* Solve for (pre-divisor+1) * (divisor+1) as those are settings we need +* to program: +* (pre-divisor+1) * (divisor+1) = (sample_period_s * pclk_hz) / 24 +*/ + +/* Assume PCLK runs at a fairly high frequency so dividing it first +* loses little accuracy. Also note that the above formulas have +* sample_period in seconds while our desired_update_interval is in +* milliseconds, hence the divide by 1000. +*/ + adc_combined_divisor = desired_update_interval_ms * + (pclk_hz /24 /1000 / num_enabled_channels); + +/* Prefer to use the ADC divisor over the ADC pre-divisor. ADC divisor +* is 10-bits wide so anything over 1024 must use the pre-divisor. +*/ + adc_pre_divisor =1 ; +while (adc_combined_divisor/adc_pre_divisor > (1 <<10 )) { + adc_pre_divisor +=1 ; + }; + adc_divisor = adc_combined_divisor / adc_pre_divisor; + +/* Since ADC divisor and pre-divisor are used in division, the register +* value is implicitly incremented by one before use. This means we +* need to subtract one from our calculated values above. +*/ + adc_pre_divisor -=1 ; + adc_divisor -=1 ; + + adc_clock_control_reg_val = (adc_pre_divisor <<17 ) | adc_divisor; + +mutex_lock (&data->lock); + data->update_interval_ms = + (adc_pre_divisor +1 ) * (adc_divisor +1 ) + / (pclk_hz /24 /1000 ); +writel (adc_clock_control_reg_val, data->base +ASPEED_ADC_REG_ADC0C ); +mutex_unlock (&data->lock); + +return 0 ; +} + +static int aspeed_adc_get_channel_reading (struct aspeed_adc_data *data, +int channel,long *val) +{ +u32 data_reg; +u32 data_reg_val;
drivers/hwmon/aspeed_adc.c line 135
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+long adc_val; + +/* Each 32-bit data register contains 2 10-bit ADC values. */ + data_reg =ASPEED_ADC_REG_ADC10 + (channel /2 ) *4 ; + data_reg_val =readl (data->base + data_reg); +if (channel %2 ==0 ) { + adc_val = data_reg_val &0x3FF ; + }else { + adc_val = (data_reg_val >>16 ) &0x3FF ; + }
I wonder if you could replace the above block with:
adc_val = readw(data->base + ASPEED_ADC_REG_ADC10 + channel);
Quoted 150 lines
drivers/hwmon/aspeed_adc.c new file +150
-0+ +/* Scale 10-bit input reading to millivolts. */ + *val = adc_val *ASPEED_ADC_REF_VOLTAGE /1024 ; + +return 0 ; +} + +static umode_t aspeed_adc_hwmon_is_visible (const void *_data, +enum hwmon_sensor_types type, +u32 attr,int channel) +{ +const struct aspeed_adc_data * data = _data; + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { +return S_IRUGO |S_IWUSR ; + }else if (type == hwmon_in) { +/* Only channels that are enabled should be visible. */ +if (channel >=0 && channel <=ASPEED_ADC_NUM_CHANNELS && + (data->enabled_channel_mask &BIT (channel))) { + +/* All enabled channels have an input but labels are +* optional in the device tree. +*/ +if (attr == hwmon_in_input) { +return S_IRUGO ; + }else if (attr == hwmon_in_label && + data->channel_labels[channel] !=NULL ) { +return S_IRUGO ; + } + } + } + +return 0 ; +} + +static int aspeed_adc_hwmon_read (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long *val) +{ +struct aspeed_adc_data *data =dev_get_drvdata (dev); + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { + *val = data->update_interval_ms; +return 0 ; + }else if (type == hwmon_in && attr == hwmon_in_input) { +return aspeed_adc_get_channel_reading (data, channel, val); + } + +return -EOPNOTSUPP ; +} + +static int aspeed_adc_hwmon_read_string (struct device *dev, +enum hwmon_sensor_types type, +u32 attr,int channel,char **str) +{ +struct aspeed_adc_data *data =dev_get_drvdata (dev); + +if (type == hwmon_in && attr == hwmon_in_label) { + *str = (char *)data->channel_labels[channel]; +return 0 ; + } + +return -EOPNOTSUPP ; +} + +static int aspeed_adc_hwmon_write (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long val) +{ +struct aspeed_adc_data *data =dev_get_drvdata (dev); + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { +return aspeed_adc_set_adc_clock_frequency (data, val); + } + +return -EOPNOTSUPP ; +} + +static const struct hwmon_ops aspeed_adc_hwmon_ops = { + .is_visible = aspeed_adc_hwmon_is_visible, + .read = aspeed_adc_hwmon_read, + .read_string = aspeed_adc_hwmon_read_string, + .write = aspeed_adc_hwmon_write, +}; + +static const u32 aspeed_adc_hwmon_chip_config[] = { +HWMON_C_UPDATE_INTERVAL , +0 +}; + +static const struct hwmon_channel_info aspeed_adc_hwmon_chip_channel = { + .type = hwmon_chip, + .config = aspeed_adc_hwmon_chip_config, +}; + +static const u32 aspeed_adc_hwmon_in_config[] = { +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +0 +}; + +static const struct hwmon_channel_info aspeed_adc_hwmon_in_channel = { + .type = hwmon_in, + .config = aspeed_adc_hwmon_in_config, +}; + +static const struct hwmon_channel_info *aspeed_adc_hwmon_channel_info[] = { + &aspeed_adc_hwmon_chip_channel, + &aspeed_adc_hwmon_in_channel, +NULL +}; + +static const struct hwmon_chip_info aspeed_adc_hwmon_chip_info = { + .ops = &aspeed_adc_hwmon_ops, + .info = aspeed_adc_hwmon_channel_info, +}; + +static int aspeed_adc_probe (struct platform_device *pdev) +{ +struct aspeed_adc_data *data; +struct resource *res; +int ret; +struct device *hwmon_dev; +u32 desired_update_interval_ms; +u32 adc_engine_control_reg_val; +struct device_node * node; + + data =devm_kzalloc (&pdev->dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->pclk =devm_clk_get (&pdev->dev,NULL ); +if (IS_ERR (data->pclk)) { +dev_err (&pdev->dev,"clk_get failed\n" ); +return PTR_ERR (data->pclk); + } + + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 );
drivers/hwmon/aspeed_adc.c line 295
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+ data->base =devm_ioremap_resource (&pdev->dev, res); +if (IS_ERR (data->base)) +return PTR_ERR (data->base); + + ret =of_property_read_u32 (pdev->dev.of_node, +"update-interval-ms" , &desired_update_interval_ms); +if (ret <0 || desired_update_interval_ms ==0 ) { +dev_err (&pdev->dev, +"Missing or zero update-interval-ms property, " +"defaulting to 100ms\n" );
Put the string on one line so it can be easily searched for.
drivers/hwmon/aspeed_adc.c line 305
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+ desired_update_interval_ms =100 ; + } + +mutex_init (&data->lock); + + ret =clk_prepare_enable (data->pclk); +if (ret) { +dev_err (&pdev->dev,"failed to enable clock\n" ); +mutex_destroy (&data->lock); +return ret;
Strange whitespace here.
Quoted 18 lines
drivers/hwmon/aspeed_adc.c new file +18
-0+ } + +/* Figure out which channels are marked available in the device tree. */ + data->enabled_channel_mask =0 ; +for_each_available_child_of_node (pdev->dev.of_node, node) { +u32 pval; +unsigned int channel; + +if (of_property_read_u32 (node,"reg" , &pval)) { +dev_err (&pdev->dev,"invalid reg on %s\n" , + node->full_name); +continue ; + } + + channel = pval; +if (channel >=ASPEED_ADC_NUM_CHANNELS ) { + dev_err(&pdev->dev, + "invalid channel index %d on %s\n",
drivers/hwmon/aspeed_adc.c line 333
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+ channel, node->full_name); +continue ; + } + + data->enabled_channel_mask |=BIT (channel); + +/* Cache a pointer to the label if one is specified. Ignore any +* errors as the label property is optional. +*/ +of_property_read_string (node,"label" , &data->channel_labels[channel]);
I was wondering how long we could keep that pointer around. I think we are ok for the lifetime of the driver, as we hold a reference to the node in dev.of_node.
drivers/hwmon/aspeed_adc.c line 337
Quoted 4 lines
drivers/hwmon/aspeed_adc.c new file +4
-0+ } + +platform_set_drvdata (pdev, data); +aspeed_adc_set_adc_clock_frequency (data, desired_update_interval_ms);
This reads funny. aspeed_adc_set_clock_frequency instead?
Quoted 49 lines
drivers/hwmon/aspeed_adc.c new file +46
-1+ +/* Start all the requested channels in normal mode. */ + adc_engine_control_reg_val = (data->enabled_channel_mask <<16 ) | +ASPEED_ADC_OPERATION_MODE_NORMAL |ASPEED_ADC_ENGINE_ENABLE ; +writel (adc_engine_control_reg_val, data->base +ASPEED_ADC_REG_ADC00 ); + +/* Register sysfs hooks */ + hwmon_dev =devm_hwmon_device_register_with_info ( + &pdev->dev,"aspeed_adc" , data, + &aspeed_adc_hwmon_chip_info,NULL ); +if (IS_ERR (hwmon_dev)) { +clk_disable_unprepare (data->pclk); +mutex_destroy (&data->lock); +return PTR_ERR (hwmon_dev); + } + +return 0 ; +} + +static int aspeed_adc_remove (struct platform_device *pdev) { +struct aspeed_adc_data *data =dev_get_drvdata (&pdev->dev); +clk_disable_unprepare (data->pclk); +mutex_destroy (&data->lock); +return 0 ; +} + +const struct of_device_id aspeed_adc_matches[] = { + { .compatible ="aspeed,ast2400-adc" }, + { .compatible ="aspeed,ast2500-adc" }, +}; +MODULE_DEVICE_TABLE (of, aspeed_adc_matches); + +static struct platform_driver aspeed_adc_driver = { + .probe = aspeed_adc_probe, + .remove = aspeed_adc_remove, + .driver = { + .name =KBUILD_MODNAME , + .of_match_table = aspeed_adc_matches, + } +}; + +module_platform_driver (aspeed_adc_driver); + +MODULE_AUTHOR ("Rick Altherr <[email protected]>" ); +MODULE_DESCRIPTION ("Aspeed AST2400/2500 ADC Driver" ); +MODULE_LICENSE ("GPL" ); --2.11.0.483.g087da7b7c-goog
Quoted 1 line · Rick Altherr
Signed-off-by Rick Altherr <[email protected]>
Quoted 62 lines
--- .../devicetree/bindings/hwmon/aspeed_adc.txt | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/aspeed_adc.txtDocumentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +48
-1@@ -0,0 +1,48 @@ +Aspeed AST2400/2500 ADC + +This device is a 10-bit converter for 16 voltage channels. All inputs are +single ended. Each channel can be individually enabled to allow for use of +alternate pin functions. + +1) adc node + + Required properties: + - compatible : Should be one of + "aspeed,ast2400-adc" + "aspeed,ast2500-adc" + - reg : memory window mapping address and length + - #address-cells : must be <1> corresponding to the channel child binding + - #size-cells : must be <0> corresponding to the channel child binding + - clocks : Input clock used to derive the sample clock. Expected to be the + SoC's APB clock. + - update-interval-ms : initial time between updates on a channel + + The node contains child nodes for each channel that the platform uses. + + Example adc node: + adc@1e6e9000 { + #address-cells = <1>; + #size-cells = <0>; + compatible = "aspeed,ast2400-adc"; + reg = <0x1e6e9000 0xB0>; + clocks = <&clk_apb>; + update-interval-ms = <100>; + + [ child node definitions... ] + }; + +2) channel nodes + + Optional properties: + - status: indicates the operational status of the device. + Value must be either "disabled" or "okay". + - label : string describing the monitored value + + Example channel node: + channel@1 { + status = "okay"; + label = "3V3 rail"; + + pinctrl-names = "default"; + pinctrl-0 = <&pinctrl_adc0_default>; + }; --2.11.0.483.g087da7b7c-goog
Guenter Roeck
maintainerRe: [PATCH 2/2] hwmon: Aspeed AST2400/AST2500 ADC
In reply to Joel StanleyQuoted 7 lines · Joel Stanley
Quoted 6 lines · Rick Altherr
Aspeed AST2400/AST2500 BMC SoCs include a 16 channel, 10-bit ADC. This driver implements reading the ADC values, enabling channels via device tree, and optionally providing channel labels via device tree. Low and high threshold interrupts are supported by the hardware but not implemented.
Quoted 6 lines
Quoted 1 line
Signed-off-by Rick Altherr <[email protected]>Looks good. Some minor comments below.
Is there a reason you wrote a hwmon driver instead of an iio driver? I wasn't sure what the recommended subsystem is.
Excellent point. Question is really if there is a plan to add support for thresholds. If not, an iio driver might be more appropriate.
Guenter
Quoted 476 lines
Quoted 13 lines
--- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/aspeed_adc.c | 383 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 394 insertions(+) create mode 100644 drivers/hwmon/aspeed_adc.cdrivers/hwmon/Kconfig +0
-0drivers/hwmon/Kconfig config SENSORS_ASC7621 · line 266
Quoted 6 lines
drivers/hwmon/Kconfig +3
-0@@ -261,6 +261,16 @@ config SENSORS_ASC7621 This driver can also be built as a module. If so, the module will be called asc7621. +config SENSORS_ASPEED_ADC + tristate "Aspeed AST2400/AST2500 ADC" + depends on ARCH_ASPEEDdepends on
ARCH_ASPEED||COMPILE_TEST.Quoted 57 lines
drivers/hwmon/Kconfig +7
-0+ help + If you say yes here you get support for the Aspeed AST2400/AST2500 + ADC. + + This driver can also be built as a module. If so, the module + will be called aspeed_adc. + config SENSORS_K8TEMP tristate "AMD Athlon64/FX or Opteron temperature sensor" depends on X86 && PCIdrivers/hwmon/Makefile +1
-0@@ -46,6 +46,7 @@ obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.o obj-$(CONFIG_SENSORS_ASC7621) += asc7621.o +obj-$(CONFIG_SENSORS_ASPEED_ADC) += aspeed_adc.o obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o obj-$(CONFIG_SENSORS_DA9052_ADC)+= da9052-hwmon.odrivers/hwmon/aspeed_adc.c new file +29
-0@@ -0,0 +1,383 @@ +/* +* Aspeed AST2400/2500 ADC +* +* Copyright (C) 2017 Google, Inc. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +*/ + +#include <asm/io.h> +#include <linux/clk.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/of_platform.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/types.h> + +#define ASPEED_ADC_NUM_CHANNELS 16 +#define ASPEED_ADC_REF_VOLTAGE 2500/* millivolts */ + +#define ASPEED_ADC_REG_ADC00 0x00 +#define ASPEED_ADC_REG_ADC04 0x04drivers/hwmon/aspeed_adc.c line 39
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+#define ASPEED_ADC_REG_ADC08 0x08 +#define ASPEED_ADC_REG_ADC0C 0x0C +#define ASPEED_ADC_REG_ADC10 0x10 +#define ASPEED_ADC_REG_ADC14 0x14 +#define ASPEED_ADC_REG_ADC18 0x18 +#define ASPEED_ADC_REG_ADC1C 0x1C +#define ASPEED_ADC_REG_ADC20 0x20 +#define ASPEED_ADC_REG_ADC24 0x24 +#define ASPEED_ADC_REG_ADC28 0x28 +#define ASPEED_ADC_REG_ADC2C 0x2CI'm not sure that these defines add any value. I'd either give them names such as "
ASPEED_ADC_ENGINE_CTRL". or just use the register offset directly.Quoted 17 lines
drivers/hwmon/aspeed_adc.c new file +17
-0+ +#define ASPEED_ADC_OPERATION_MODE_POWER_DOWN (0x0 << 1) +#define ASPEED_ADC_OPERATION_MODE_STANDBY (0x1 << 1) +#define ASPEED_ADC_OPERATION_MODE_NORMAL (0x7 << 1) + +#define ASPEED_ADC_ENGINE_ENABLE BIT(0) + +struct aspeed_adc_data { +struct device *dev; +void __iomem *base; +struct clk *pclk; +struct mutex lock; +unsigned long update_interval_ms; +u32 enabled_channel_mask; +const char * channel_labels[ASPEED_ADC_NUM_CHANNELS ]; +}; +drivers/hwmon/aspeed_adc.c line 66
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+static int aspeed_adc_set_adc_clock_frequency ( +struct aspeed_adc_data *data, +unsigned long desired_update_interval_ms) +{ +long pclk_hz =clk_get_rate (data->pclk); +long adc_combined_divisor; +long adc_pre_divisor; +long adc_divisor; +long adc_clock_control_reg_val; +long num_enabled_channels =hweight32 (data->enabled_channel_mask);Some whitespace damage here.
drivers/hwmon/aspeed_adc.c line 71
Quoted 5 lines
drivers/hwmon/aspeed_adc.c new file +5
-0+ +if (desired_update_interval_ms <1 ) +return -EINVAL ; + +/* From the AST2400 datasheet: Nit: kernel style is to leave a blank line on the first line of multi-line comments:
/*
* Foo
* etcQuoted 54 lines
drivers/hwmon/aspeed_adc.c new file +54
-0+* adc_period_s = pclk_period_s * 2 * (pre_divisor+1) * (divisor+1) +* +* and +* +* sample_period_s = 12 * adc_period_s +* +* Substitute pclk_period_s = (1 / pclk_hz) +* +* Solve for (pre-divisor+1) * (divisor+1) as those are settings we need +* to program: +* (pre-divisor+1) * (divisor+1) = (sample_period_s * pclk_hz) / 24 +*/ + +/* Assume PCLK runs at a fairly high frequency so dividing it first +* loses little accuracy. Also note that the above formulas have +* sample_period in seconds while our desired_update_interval is in +* milliseconds, hence the divide by 1000. +*/ + adc_combined_divisor = desired_update_interval_ms * + (pclk_hz /24 /1000 / num_enabled_channels); + +/* Prefer to use the ADC divisor over the ADC pre-divisor. ADC divisor +* is 10-bits wide so anything over 1024 must use the pre-divisor. +*/ + adc_pre_divisor =1 ; +while (adc_combined_divisor/adc_pre_divisor > (1 <<10 )) { + adc_pre_divisor +=1 ; + }; + adc_divisor = adc_combined_divisor / adc_pre_divisor; + +/* Since ADC divisor and pre-divisor are used in division, the register +* value is implicitly incremented by one before use. This means we +* need to subtract one from our calculated values above. +*/ + adc_pre_divisor -=1 ; + adc_divisor -=1 ; + + adc_clock_control_reg_val = (adc_pre_divisor <<17 ) | adc_divisor; + +mutex_lock (&data->lock); + data->update_interval_ms = + (adc_pre_divisor +1 ) * (adc_divisor +1 ) + / (pclk_hz /24 /1000 ); +writel (adc_clock_control_reg_val, data->base +ASPEED_ADC_REG_ADC0C ); +mutex_unlock (&data->lock); + +return 0 ; +} + +static int aspeed_adc_get_channel_reading (struct aspeed_adc_data *data, +int channel,long *val) +{ +u32 data_reg; +u32 data_reg_val;drivers/hwmon/aspeed_adc.c line 135
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+long adc_val; + +/* Each 32-bit data register contains 2 10-bit ADC values. */ + data_reg =ASPEED_ADC_REG_ADC10 + (channel /2 ) *4 ; + data_reg_val =readl (data->base + data_reg); +if (channel %2 ==0 ) { + adc_val = data_reg_val &0x3FF ; + }else { + adc_val = (data_reg_val >>16 ) &0x3FF ; + }I wonder if you could replace the above block with:
adc_val = readw(data->base + ASPEED_ADC_REG_ADC10 + channel);Quoted 150 lines
drivers/hwmon/aspeed_adc.c new file +150
-0+ +/* Scale 10-bit input reading to millivolts. */ + *val = adc_val *ASPEED_ADC_REF_VOLTAGE /1024 ; + +return 0 ; +} + +static umode_t aspeed_adc_hwmon_is_visible (const void *_data, +enum hwmon_sensor_types type, +u32 attr,int channel) +{ +const struct aspeed_adc_data * data = _data; + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { +return S_IRUGO |S_IWUSR ; + }else if (type == hwmon_in) { +/* Only channels that are enabled should be visible. */ +if (channel >=0 && channel <=ASPEED_ADC_NUM_CHANNELS && + (data->enabled_channel_mask &BIT (channel))) { + +/* All enabled channels have an input but labels are +* optional in the device tree. +*/ +if (attr == hwmon_in_input) { +return S_IRUGO ; + }else if (attr == hwmon_in_label && + data->channel_labels[channel] !=NULL ) { +return S_IRUGO ; + } + } + } + +return 0 ; +} + +static int aspeed_adc_hwmon_read (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long *val) +{ +struct aspeed_adc_data *data =dev_get_drvdata (dev); + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { + *val = data->update_interval_ms; +return 0 ; + }else if (type == hwmon_in && attr == hwmon_in_input) { +return aspeed_adc_get_channel_reading (data, channel, val); + } + +return -EOPNOTSUPP ; +} + +static int aspeed_adc_hwmon_read_string (struct device *dev, +enum hwmon_sensor_types type, +u32 attr,int channel,char **str) +{ +struct aspeed_adc_data *data =dev_get_drvdata (dev); + +if (type == hwmon_in && attr == hwmon_in_label) { + *str = (char *)data->channel_labels[channel]; +return 0 ; + } + +return -EOPNOTSUPP ; +} + +static int aspeed_adc_hwmon_write (struct device *dev,enum hwmon_sensor_types type, +u32 attr,int channel,long val) +{ +struct aspeed_adc_data *data =dev_get_drvdata (dev); + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { +return aspeed_adc_set_adc_clock_frequency (data, val); + } + +return -EOPNOTSUPP ; +} + +static const struct hwmon_ops aspeed_adc_hwmon_ops = { + .is_visible = aspeed_adc_hwmon_is_visible, + .read = aspeed_adc_hwmon_read, + .read_string = aspeed_adc_hwmon_read_string, + .write = aspeed_adc_hwmon_write, +}; + +static const u32 aspeed_adc_hwmon_chip_config[] = { +HWMON_C_UPDATE_INTERVAL , +0 +}; + +static const struct hwmon_channel_info aspeed_adc_hwmon_chip_channel = { + .type = hwmon_chip, + .config = aspeed_adc_hwmon_chip_config, +}; + +static const u32 aspeed_adc_hwmon_in_config[] = { +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +0 +}; + +static const struct hwmon_channel_info aspeed_adc_hwmon_in_channel = { + .type = hwmon_in, + .config = aspeed_adc_hwmon_in_config, +}; + +static const struct hwmon_channel_info *aspeed_adc_hwmon_channel_info[] = { + &aspeed_adc_hwmon_chip_channel, + &aspeed_adc_hwmon_in_channel, +NULL +}; + +static const struct hwmon_chip_info aspeed_adc_hwmon_chip_info = { + .ops = &aspeed_adc_hwmon_ops, + .info = aspeed_adc_hwmon_channel_info, +}; + +static int aspeed_adc_probe (struct platform_device *pdev) +{ +struct aspeed_adc_data *data; +struct resource *res; +int ret; +struct device *hwmon_dev; +u32 desired_update_interval_ms; +u32 adc_engine_control_reg_val; +struct device_node * node; + + data =devm_kzalloc (&pdev->dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->pclk =devm_clk_get (&pdev->dev,NULL ); +if (IS_ERR (data->pclk)) { +dev_err (&pdev->dev,"clk_get failed\n" ); +return PTR_ERR (data->pclk); + } + + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 );drivers/hwmon/aspeed_adc.c line 295
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+ data->base =devm_ioremap_resource (&pdev->dev, res); +if (IS_ERR (data->base)) +return PTR_ERR (data->base); + + ret =of_property_read_u32 (pdev->dev.of_node, +"update-interval-ms" , &desired_update_interval_ms); +if (ret <0 || desired_update_interval_ms ==0 ) { +dev_err (&pdev->dev, +"Missing or zero update-interval-ms property, " +"defaulting to 100ms\n" );Put the string on one line so it can be easily searched for.
drivers/hwmon/aspeed_adc.c line 305
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+ desired_update_interval_ms =100 ; + } + +mutex_init (&data->lock); + + ret =clk_prepare_enable (data->pclk); +if (ret) { +dev_err (&pdev->dev,"failed to enable clock\n" ); +mutex_destroy (&data->lock); +return ret;Strange whitespace here.
Quoted 18 lines
drivers/hwmon/aspeed_adc.c new file +18
-0+ } + +/* Figure out which channels are marked available in the device tree. */ + data->enabled_channel_mask =0 ; +for_each_available_child_of_node (pdev->dev.of_node, node) { +u32 pval; +unsigned int channel; + +if (of_property_read_u32 (node,"reg" , &pval)) { +dev_err (&pdev->dev,"invalid reg on %s\n" , + node->full_name); +continue ; + } + + channel = pval; +if (channel >=ASPEED_ADC_NUM_CHANNELS ) { + dev_err(&pdev->dev, + "invalid channel index %d on %s\n",drivers/hwmon/aspeed_adc.c line 333
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+ channel, node->full_name); +continue ; + } + + data->enabled_channel_mask |=BIT (channel); + +/* Cache a pointer to the label if one is specified. Ignore any +* errors as the label property is optional. +*/ +of_property_read_string (node,"label" , &data->channel_labels[channel]);I was wondering how long we could keep that pointer around. I think we are ok for the lifetime of the driver, as we hold a reference to the node in dev.of_node.
drivers/hwmon/aspeed_adc.c line 337
Quoted 4 lines
drivers/hwmon/aspeed_adc.c new file +4
-0+ } + +platform_set_drvdata (pdev, data); +aspeed_adc_set_adc_clock_frequency (data, desired_update_interval_ms);This reads funny.
aspeed_adc_set_clock_frequencyinstead?Quoted 47 lines
drivers/hwmon/aspeed_adc.c new file +46
-1+ +/* Start all the requested channels in normal mode. */ + adc_engine_control_reg_val = (data->enabled_channel_mask <<16 ) | +ASPEED_ADC_OPERATION_MODE_NORMAL |ASPEED_ADC_ENGINE_ENABLE ; +writel (adc_engine_control_reg_val, data->base +ASPEED_ADC_REG_ADC00 ); + +/* Register sysfs hooks */ + hwmon_dev =devm_hwmon_device_register_with_info ( + &pdev->dev,"aspeed_adc" , data, + &aspeed_adc_hwmon_chip_info,NULL ); +if (IS_ERR (hwmon_dev)) { +clk_disable_unprepare (data->pclk); +mutex_destroy (&data->lock); +return PTR_ERR (hwmon_dev); + } + +return 0 ; +} + +static int aspeed_adc_remove (struct platform_device *pdev) { +struct aspeed_adc_data *data =dev_get_drvdata (&pdev->dev); +clk_disable_unprepare (data->pclk); +mutex_destroy (&data->lock); +return 0 ; +} + +const struct of_device_id aspeed_adc_matches[] = { + { .compatible ="aspeed,ast2400-adc" }, + { .compatible ="aspeed,ast2500-adc" }, +}; +MODULE_DEVICE_TABLE (of, aspeed_adc_matches); + +static struct platform_driver aspeed_adc_driver = { + .probe = aspeed_adc_probe, + .remove = aspeed_adc_remove, + .driver = { + .name =KBUILD_MODNAME , + .of_match_table = aspeed_adc_matches, + } +}; + +module_platform_driver (aspeed_adc_driver); + +MODULE_AUTHOR ("Rick Altherr <[email protected]>" ); +MODULE_DESCRIPTION ("Aspeed AST2400/2500 ADC Driver" ); +MODULE_LICENSE ("GPL" ); --Quoted 2 lines
2.11.0.483.g087da7b7c-goog
--
To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 2/2] hwmon: Aspeed AST2400/AST2500 ADC
On Feb 28, 2017 7:45 PM, "Guenter Roeck" <[email protected]> wrote:
Quoted 8 lines · Joel Stanley
Quoted 6 lines · Rick Altherr
Aspeed AST2400/AST2500 BMC SoCs include a 16 channel, 10-bit ADC. This driver implements reading the ADC values, enabling channels via device tree, and optionally providing channel labels via device tree. Low and high threshold interrupts are supported by the hardware but not implemented.
Quoted 8 lines
Quoted 2 lines
Signed-off-by Rick Altherr <[email protected]>Looks good. Some minor comments below.
Is there a reason you wrote a hwmon driver instead of an iio driver? I wasn't sure what the recommended subsystem is.
Excellent point. Question is really if there is a plan to add support for thresholds. If not, an iio driver might be more appropriate.
Guenter
The hardware supports firing interrupts on high and low thresholds. I'm not planning to use that feature yet so I didn't implement it. Would you prefer that I leave it as is (maybe with a TODO), implement the thresholding, or change to iio?
Rick
Quoted 513 lines
---
Quoted 12 lines
drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/aspeed_adc.c | 383 ++++++++++++++++++++++++++++++ +++++++++++++++ 3 files changed, 394 insertions(+) create mode 100644 drivers/hwmon/aspeed_adc.cdrivers/hwmon/Kconfig +0
-0drivers/hwmon/Kconfig config SENSORS_ASC7621 · line 267
Quoted 7 lines
drivers/hwmon/Kconfig +3
-0@@ -261,6 +261,16 @@ config SENSORS_ASC7621 This driver can also be built as a module. If so, the module will be called asc7621. +config SENSORS_ASPEED_ADC + tristate "Aspeed AST2400/AST2500 ADC" + depends on ARCH_ASPEEDdepends on
ARCH_ASPEED||COMPILE_TEST.+ help
Quoted 59 lines
drivers/hwmon/Kconfig +1
-0+ If you say yes here you get support for the AspeedAST2400/AST2500
drivers/hwmon/Kconfig +5
-0+ ADC. + + This driver can also be built as a module. If so, the module + will be called aspeed_adc. + config SENSORS_K8TEMP tristate "AMD Athlon64/FX or Opteron temperature sensor" depends on X86 && PCIdrivers/hwmon/Makefile +1
-0@@ -46,6 +46,7 @@ obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.o obj-$(CONFIG_SENSORS_ASC7621) += asc7621.o +obj-$(CONFIG_SENSORS_ASPEED_ADC) += aspeed_adc.o obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o obj-$(CONFIG_SENSORS_DA9052_ADC)+= da9052-hwmon.odrivers/hwmon/aspeed_adc.c new file +6
-0@@ -0,0 +1,383 @@ +/* +* Aspeed AST2400/2500 ADC +* +* Copyright (C) 2017 Google, Inc. +* +* This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ */drivers/hwmon/aspeed_adc.c new file +19
-0+ +#include <asm/io.h> +#include <linux/clk.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/of_platform.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/types.h> + +#define ASPEED_ADC_NUM_CHANNELS 16 +#define ASPEED_ADC_REF_VOLTAGE 2500/* millivolts */ + +#define ASPEED_ADC_REG_ADC00 0x00 +#define ASPEED_ADC_REG_ADC04 0x04drivers/hwmon/aspeed_adc.c line 39
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+#define ASPEED_ADC_REG_ADC08 0x08 +#define ASPEED_ADC_REG_ADC0C 0x0C +#define ASPEED_ADC_REG_ADC10 0x10 +#define ASPEED_ADC_REG_ADC14 0x14 +#define ASPEED_ADC_REG_ADC18 0x18 +#define ASPEED_ADC_REG_ADC1C 0x1C +#define ASPEED_ADC_REG_ADC20 0x20 +#define ASPEED_ADC_REG_ADC24 0x24 +#define ASPEED_ADC_REG_ADC28 0x28 +#define ASPEED_ADC_REG_ADC2C 0x2CI'm not sure that these defines add any value. I'd either give them names such as "
ASPEED_ADC_ENGINE_CTRL". or just use the register offset directly.+
Quoted 26 lines
drivers/hwmon/aspeed_adc.c new file +26
-0+#define ASPEED_ADC_OPERATION_MODE_POWER_DOWN (0x0 << 1) +#define ASPEED_ADC_OPERATION_MODE_STANDBY (0x1 << 1) +#define ASPEED_ADC_OPERATION_MODE_NORMAL (0x7 << 1) + +#define ASPEED_ADC_ENGINE_ENABLE BIT(0) + +struct aspeed_adc_data { +struct device *dev; +void __iomem *base; +struct clk *pclk; +struct mutex lock; +unsigned long update_interval_ms; +u32 enabled_channel_mask; +const char * channel_labels[ASPEED_ADC_NUM_CHANNELS ]; +}; + +static int aspeed_adc_set_adc_clock_frequency ( +struct aspeed_adc_data *data, +unsigned long desired_update_interval_ms) +{ +long pclk_hz =clk_get_rate (data->pclk); +long adc_combined_divisor; +long adc_pre_divisor; +long adc_divisor; +long adc_clock_control_reg_val; + long num_enabled_channels = hweight32(data->enabled_channeQuoted 2 lines
l_mask);Some whitespace damage here.
+
drivers/hwmon/aspeed_adc.c line 71
Quoted 5 lines
drivers/hwmon/aspeed_adc.c new file +4
-0+if (desired_update_interval_ms <1 ) +return -EINVAL ; + +/* From the AST2400 datasheet: Nit: kernel style is to leave a blank line on the first line of multi-line comments:
/*
* Foo
* etc+ *
adc_period_s=pclk_period_s* 2 * (pre_divisor+1) *Quoted 63 lines
(divisor+1)
drivers/hwmon/aspeed_adc.c new file +8
-0+* +* and +* +* sample_period_s = 12 * adc_period_s +* +* Substitute pclk_period_s = (1 / pclk_hz) +* +* Solve for (pre-divisor+1) * (divisor+1) as those are settings we need
drivers/hwmon/aspeed_adc.c new file +2
-0+* to program: +* (pre-divisor+1) * (divisor+1) = (sample_period_s * pclk_hz) / 24
drivers/hwmon/aspeed_adc.c new file +3
-0+*/ + +/* Assume PCLK runs at a fairly high frequency so dividing it first
drivers/hwmon/aspeed_adc.c new file +2
-0+* loses little accuracy. Also note that the above formulas have +* sample_period in seconds while our desired_update_interval is in
drivers/hwmon/aspeed_adc.c new file +6
-0+* milliseconds, hence the divide by 1000. +*/ + adc_combined_divisor = desired_update_interval_ms * + (pclk_hz /24 /1000 / num_enabled_channels); + +/* Prefer to use the ADC divisor over the ADC pre-divisor. ADC divisor
drivers/hwmon/aspeed_adc.c new file +9
-0+* is 10-bits wide so anything over 1024 must use the pre-divisor. +*/ + adc_pre_divisor =1 ; +while (adc_combined_divisor/adc_pre_divisor > (1 <<10 )) { + adc_pre_divisor +=1 ; + }; + adc_divisor = adc_combined_divisor / adc_pre_divisor; + +/* Since ADC divisor and pre-divisor are used in division, the register
+ * value is implicitly incremented by one before use. This means
wedrivers/hwmon/aspeed_adc.c new file +12
-0+* need to subtract one from our calculated values above. +*/ + adc_pre_divisor -=1 ; + adc_divisor -=1 ; + + adc_clock_control_reg_val = (adc_pre_divisor <<17 ) | adc_divisor; + +mutex_lock (&data->lock); + data->update_interval_ms = + (adc_pre_divisor +1 ) * (adc_divisor +1 ) + / (pclk_hz /24 /1000 ); + writel(adc_clock_control_reg_val, data->base +
ASPEED_ADC_REG_ADC0C);drivers/hwmon/aspeed_adc.c new file +10
-0+mutex_unlock (&data->lock); + +return 0 ; +} + +static int aspeed_adc_get_channel_reading (struct aspeed_adc_data *data, +int channel,long *val) +{ +u32 data_reg; +u32 data_reg_val;drivers/hwmon/aspeed_adc.c line 135
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+long adc_val; + +/* Each 32-bit data register contains 2 10-bit ADC values. */ + data_reg =ASPEED_ADC_REG_ADC10 + (channel /2 ) *4 ; + data_reg_val =readl (data->base + data_reg); +if (channel %2 ==0 ) { + adc_val = data_reg_val &0x3FF ; + }else { + adc_val = (data_reg_val >>16 ) &0x3FF ; + }I wonder if you could replace the above block with:
adc_val = readw(data->base + ASPEED_ADC_REG_ADC10 + channel);+
Quoted 166 lines
drivers/hwmon/aspeed_adc.c new file +8
-0+/* Scale 10-bit input reading to millivolts. */ + *val = adc_val *ASPEED_ADC_REF_VOLTAGE /1024 ; + +return 0 ; +} + +static umode_t aspeed_adc_hwmon_is_visible (const void *_data, + enum hwmon_sensor_typestype,
drivers/hwmon/aspeed_adc.c new file +12
-0+ u32 attr, int channel) +{ +const struct aspeed_adc_data * data = _data; + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { +return S_IRUGO |S_IWUSR ; + }else if (type == hwmon_in) { +/* Only channels that are enabled should be visible. */ +if (channel >=0 && channel <=ASPEED_ADC_NUM_CHANNELS && + (data->enabled_channel_mask &BIT (channel))) { + +/* All enabled channels have an input but labels are
drivers/hwmon/aspeed_adc.c new file +6
-0+* optional in the device tree. +*/ +if (attr == hwmon_in_input) { +return S_IRUGO ; + }else if (attr == hwmon_in_label && + data->channel_labels[channel] !=NULL) {
drivers/hwmon/aspeed_adc.c new file +9
-0+return S_IRUGO ; + } + } + } + +return 0 ; +} + +static int aspeed_adc_hwmon_read(struct device *dev, enum
hwmon_sensor_typestype,drivers/hwmon/aspeed_adc.c new file +9
-0+ u32 attr, int channel, long *val) +{ + struct aspeed_adc_data *data = dev_get_drvdata(dev); + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { + *val = data->update_interval_ms; +return 0 ; + }else if (type == hwmon_in && attr == hwmon_in_input) { + return aspeed_adc_get_channel_reading(data, channel,val);
drivers/hwmon/aspeed_adc.c new file +20
-0+ } + +return -EOPNOTSUPP ; +} + +static int aspeed_adc_hwmon_read_string (struct device *dev, +enum hwmon_sensor_types type, +u32 attr,int channel,char **str) +{ +struct aspeed_adc_data *data =dev_get_drvdata (dev); + +if (type == hwmon_in && attr == hwmon_in_label) { + *str = (char *)data->channel_labels[channel]; +return 0 ; + } + +return -EOPNOTSUPP ; +} + +static int aspeed_adc_hwmon_write(struct device *dev, enum
hwmon_sensor_typestype,drivers/hwmon/aspeed_adc.c new file +54
-0+ u32 attr, int channel, long val) +{ + struct aspeed_adc_data *data = dev_get_drvdata(dev); + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { +return aspeed_adc_set_adc_clock_frequency (data, val); + } + +return -EOPNOTSUPP ; +} + +static const struct hwmon_ops aspeed_adc_hwmon_ops = { + .is_visible = aspeed_adc_hwmon_is_visible, + .read = aspeed_adc_hwmon_read, + .read_string = aspeed_adc_hwmon_read_string, + .write = aspeed_adc_hwmon_write, +}; + +static const u32 aspeed_adc_hwmon_chip_config[] = { +HWMON_C_UPDATE_INTERVAL , +0 +}; + +static const struct hwmon_channel_info aspeed_adc_hwmon_chip_channel = { + .type = hwmon_chip, + .config = aspeed_adc_hwmon_chip_config, +}; + +static const u32 aspeed_adc_hwmon_in_config[] = { +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +0 +}; + +static const struct hwmon_channel_info aspeed_adc_hwmon_in_channel = { + .type = hwmon_in, + .config = aspeed_adc_hwmon_in_config, +}; + +static const struct hwmon_channel_info *aspeed_adc_hwmon_channel_info[]= {
drivers/hwmon/aspeed_adc.c new file +37
-0+ &aspeed_adc_hwmon_chip_channel, + &aspeed_adc_hwmon_in_channel, +NULL +}; + +static const struct hwmon_chip_info aspeed_adc_hwmon_chip_info = { + .ops = &aspeed_adc_hwmon_ops, + .info = aspeed_adc_hwmon_channel_info, +}; + +static int aspeed_adc_probe (struct platform_device *pdev) +{ +struct aspeed_adc_data *data; +struct resource *res; +int ret; +struct device *hwmon_dev; +u32 desired_update_interval_ms; +u32 adc_engine_control_reg_val; +struct device_node * node; + + data =devm_kzalloc (&pdev->dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->pclk =devm_clk_get (&pdev->dev,NULL ); +if (IS_ERR (data->pclk)) { +dev_err (&pdev->dev,"clk_get failed\n" ); +return PTR_ERR (data->pclk); + } + + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); + data->base =devm_ioremap_resource (&pdev->dev, res); +if (IS_ERR (data->base)) +return PTR_ERR (data->base); + + ret = of_property_read_u32(pdev->dev.of_node, + "update-interval-ms",
&desired_update_interval_ms);drivers/hwmon/aspeed_adc.c new file +3
-0+if (ret <0 || desired_update_interval_ms ==0 ) { + dev_err(&pdev->dev, + "Missing or zero update-interval-msQuoted 3 lines
property, "
+ "defaulting to 100ms\n");Put the string on one line so it can be easily searched for.
+
desired_update_interval_ms= 100;drivers/hwmon/aspeed_adc.c line 305
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +9
-0+ } + +mutex_init (&data->lock); + + ret =clk_prepare_enable (data->pclk); +if (ret) { +dev_err (&pdev->dev,"failed to enable clock\n" ); +mutex_destroy (&data->lock); +return ret;Strange whitespace here.
+ }
Quoted 29 lines
drivers/hwmon/aspeed_adc.c new file +2
-0+ +/* Figure out which channels are marked available in the device tree. */
drivers/hwmon/aspeed_adc.c new file +22
-0+ data->enabled_channel_mask =0 ; +for_each_available_child_of_node (pdev->dev.of_node, node) { +u32 pval; +unsigned int channel; + +if (of_property_read_u32 (node,"reg" , &pval)) { +dev_err (&pdev->dev,"invalid reg on %s\n" , + node->full_name); +continue ; + } + + channel = pval; +if (channel >=ASPEED_ADC_NUM_CHANNELS ) { +dev_err (&pdev->dev, +"invalid channel index %d on %s\n" , + channel, node->full_name); +continue ; + } + + data->enabled_channel_mask |=BIT (channel); + +/* Cache a pointer to the label if one is specified. Ignore any
drivers/hwmon/aspeed_adc.c new file +3
-0+* errors as the label property is optional. +*/ + of_property_read_string(node, "label",Quoted 2 lines
&data->channel_labels[channel]);
I was wondering how long we could keep that pointer around. I think we are ok for the lifetime of the driver, as we hold a reference to the node in dev.of_node.
+ }
Quoted 5 lines
drivers/hwmon/aspeed_adc.c new file +3
-0+ +platform_set_drvdata (pdev, data); + aspeed_adc_set_adc_clock_frequency(data,
desired_update_interval_ms);This reads funny.
aspeed_adc_set_clock_frequencyinstead?+
Quoted 50 lines
drivers/hwmon/aspeed_adc.c new file +3
-0+/* Start all the requested channels in normal mode. */ + adc_engine_control_reg_val = (data->enabled_channel_mask <<16 ) | + ASPEED_ADC_OPERATION_MODE_NORMAL |
ASPEED_ADC_ENGINE_ENABLE;
+ writel(adc_engine_control_reg_val,data->base+ASPEED_ADC_REG_ADC00);drivers/hwmon/aspeed_adc.c new file +41
-1+ +/* Register sysfs hooks */ + hwmon_dev =devm_hwmon_device_register_with_info ( + &pdev->dev,"aspeed_adc" , data, + &aspeed_adc_hwmon_chip_info,NULL ); +if (IS_ERR (hwmon_dev)) { +clk_disable_unprepare (data->pclk); +mutex_destroy (&data->lock); +return PTR_ERR (hwmon_dev); + } + +return 0 ; +} + +static int aspeed_adc_remove (struct platform_device *pdev) { +struct aspeed_adc_data *data =dev_get_drvdata (&pdev->dev); +clk_disable_unprepare (data->pclk); +mutex_destroy (&data->lock); +return 0 ; +} + +const struct of_device_id aspeed_adc_matches[] = { + { .compatible ="aspeed,ast2400-adc" }, + { .compatible ="aspeed,ast2500-adc" }, +}; +MODULE_DEVICE_TABLE (of, aspeed_adc_matches); + +static struct platform_driver aspeed_adc_driver = { + .probe = aspeed_adc_probe, + .remove = aspeed_adc_remove, + .driver = { + .name =KBUILD_MODNAME , + .of_match_table = aspeed_adc_matches, + } +}; + +module_platform_driver (aspeed_adc_driver); + +MODULE_AUTHOR ("Rick Altherr <[email protected]>" ); +MODULE_DESCRIPTION ("Aspeed AST2400/2500 ADC Driver" ); +MODULE_LICENSE ("GPL" ); --2.11.0.483.g087da7b7c-goog
Quoted 1 line
--
To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 2/2] hwmon: Aspeed AST2400/AST2500 ADC
In reply to Guenter RoeckResending in plain text.
Quoted 15 lines · Guenter Roeck
Quoted 14 lines · Joel Stanley
Quoted 8 lines · Rick Altherr
Aspeed AST2400/AST2500 BMC SoCs include a 16 channel, 10-bit ADC. This driver implements reading the ADC values, enabling channels via device tree, and optionally providing channel labels via device tree. Low and high threshold interrupts are supported by the hardware but not implemented.
Signed-off-by Rick Altherr <[email protected]>Looks good. Some minor comments below.
Quoted 9 lines
Quoted 2 lines
Is there a reason you wrote a hwmon driver instead of an iio driver? I wasn't sure what the recommended subsystem is.
Excellent point. Question is really if there is a plan to add support for thresholds. If not, an iio driver might be more appropriate.
Guenter
The hardware supports firing interrupts on high and low thresholds. I'm not planning to use that feature yet so I didn't implement it. Would you prefer that I leave it as is (maybe with a TODO), implement the thresholding, or change to iio?
Rick
Quoted 517 lines
Quoted 516 lines
Quoted 14 lines
--- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/aspeed_adc.c | 383 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 394 insertions(+) create mode 100644 drivers/hwmon/aspeed_adc.cdrivers/hwmon/Kconfig +0
-0drivers/hwmon/Kconfig config SENSORS_ASC7621 · line 266
Quoted 6 lines
drivers/hwmon/Kconfig +3
-0@@ -261,6 +261,16 @@ config SENSORS_ASC7621 This driver can also be built as a module. If so, the module will be called asc7621. +config SENSORS_ASPEED_ADC + tristate "Aspeed AST2400/AST2500 ADC" + depends on ARCH_ASPEEDdepends on
ARCH_ASPEED||COMPILE_TEST.Quoted 59 lines
drivers/hwmon/Kconfig +2
-0+ help + If you say yes here you get support for the AspeedAST2400/AST2500
drivers/hwmon/Kconfig +5
-0+ ADC. + + This driver can also be built as a module. If so, the module + will be called aspeed_adc. + config SENSORS_K8TEMP tristate "AMD Athlon64/FX or Opteron temperature sensor" depends on X86 && PCIdrivers/hwmon/Makefile +1
-0@@ -46,6 +46,7 @@ obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.o obj-$(CONFIG_SENSORS_ASC7621) += asc7621.o +obj-$(CONFIG_SENSORS_ASPEED_ADC) += aspeed_adc.o obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o obj-$(CONFIG_SENSORS_DA9052_ADC)+= da9052-hwmon.odrivers/hwmon/aspeed_adc.c new file +6
-0@@ -0,0 +1,383 @@ +/* +* Aspeed AST2400/2500 ADC +* +* Copyright (C) 2017 Google, Inc. +* +* This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ */drivers/hwmon/aspeed_adc.c new file +19
-0+ +#include <asm/io.h> +#include <linux/clk.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/of_platform.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/types.h> + +#define ASPEED_ADC_NUM_CHANNELS 16 +#define ASPEED_ADC_REF_VOLTAGE 2500/* millivolts */ + +#define ASPEED_ADC_REG_ADC00 0x00 +#define ASPEED_ADC_REG_ADC04 0x04drivers/hwmon/aspeed_adc.c line 39
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+#define ASPEED_ADC_REG_ADC08 0x08 +#define ASPEED_ADC_REG_ADC0C 0x0C +#define ASPEED_ADC_REG_ADC10 0x10 +#define ASPEED_ADC_REG_ADC14 0x14 +#define ASPEED_ADC_REG_ADC18 0x18 +#define ASPEED_ADC_REG_ADC1C 0x1C +#define ASPEED_ADC_REG_ADC20 0x20 +#define ASPEED_ADC_REG_ADC24 0x24 +#define ASPEED_ADC_REG_ADC28 0x28 +#define ASPEED_ADC_REG_ADC2C 0x2CI'm not sure that these defines add any value. I'd either give them names such as "
ASPEED_ADC_ENGINE_CTRL". or just use the register offset directly.Quoted 27 lines
drivers/hwmon/aspeed_adc.c new file +27
-0+ +#define ASPEED_ADC_OPERATION_MODE_POWER_DOWN (0x0 << 1) +#define ASPEED_ADC_OPERATION_MODE_STANDBY (0x1 << 1) +#define ASPEED_ADC_OPERATION_MODE_NORMAL (0x7 << 1) + +#define ASPEED_ADC_ENGINE_ENABLE BIT(0) + +struct aspeed_adc_data { +struct device *dev; +void __iomem *base; +struct clk *pclk; +struct mutex lock; +unsigned long update_interval_ms; +u32 enabled_channel_mask; +const char * channel_labels[ASPEED_ADC_NUM_CHANNELS ]; +}; + +static int aspeed_adc_set_adc_clock_frequency ( +struct aspeed_adc_data *data, +unsigned long desired_update_interval_ms) +{ +long pclk_hz =clk_get_rate (data->pclk); +long adc_combined_divisor; +long adc_pre_divisor; +long adc_divisor; +long adc_clock_control_reg_val; + long num_enabled_channels =Quoted 1 line
hweight32(data->enabled_channel_mask);Some whitespace damage here.
drivers/hwmon/aspeed_adc.c line 71
Quoted 5 lines
drivers/hwmon/aspeed_adc.c new file +5
-0+ +if (desired_update_interval_ms <1 ) +return -EINVAL ; + +/* From the AST2400 datasheet: Nit: kernel style is to leave a blank line on the first line of multi-line comments:
/*
* Foo
* etcQuoted 65 lines
+ *
adc_period_s=pclk_period_s* 2 * (pre_divisor+1) *
(divisor+1)drivers/hwmon/aspeed_adc.c new file +8
-0+* +* and +* +* sample_period_s = 12 * adc_period_s +* +* Substitute pclk_period_s = (1 / pclk_hz) +* +* Solve for (pre-divisor+1) * (divisor+1) as those are settings we need
drivers/hwmon/aspeed_adc.c new file +2
-0+* to program: +* (pre-divisor+1) * (divisor+1) = (sample_period_s * pclk_hz) / 24
drivers/hwmon/aspeed_adc.c new file +3
-0+*/ + +/* Assume PCLK runs at a fairly high frequency so dividing it first
drivers/hwmon/aspeed_adc.c new file +2
-0+* loses little accuracy. Also note that the above formulas have +* sample_period in seconds while our desired_update_interval is in
drivers/hwmon/aspeed_adc.c new file +6
-0+* milliseconds, hence the divide by 1000. +*/ + adc_combined_divisor = desired_update_interval_ms * + (pclk_hz /24 /1000 / num_enabled_channels); + +/* Prefer to use the ADC divisor over the ADC pre-divisor. ADC divisor
+ * is 10-bits wide so anything over 1024 must use the
pre-divisor.drivers/hwmon/aspeed_adc.c new file +8
-0+*/ + adc_pre_divisor =1 ; +while (adc_combined_divisor/adc_pre_divisor > (1 <<10 )) { + adc_pre_divisor +=1 ; + }; + adc_divisor = adc_combined_divisor / adc_pre_divisor; + +/* Since ADC divisor and pre-divisor are used in division, the register
+ * value is implicitly incremented by one before use. This means
wedrivers/hwmon/aspeed_adc.c new file +6
-0+* need to subtract one from our calculated values above. +*/ + adc_pre_divisor -=1 ; + adc_divisor -=1 ; + + adc_clock_control_reg_val = (adc_pre_divisor << 17) |
adc_divisor;drivers/hwmon/aspeed_adc.c new file +6
-0+ +mutex_lock (&data->lock); + data->update_interval_ms = + (adc_pre_divisor +1 ) * (adc_divisor +1 ) + / (pclk_hz /24 /1000 ); + writel(adc_clock_control_reg_val, data->base +
ASPEED_ADC_REG_ADC0C);drivers/hwmon/aspeed_adc.c new file +10
-0+mutex_unlock (&data->lock); + +return 0 ; +} + +static int aspeed_adc_get_channel_reading (struct aspeed_adc_data *data, +int channel,long *val) +{ +u32 data_reg; +u32 data_reg_val;drivers/hwmon/aspeed_adc.c line 135
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+long adc_val; + +/* Each 32-bit data register contains 2 10-bit ADC values. */ + data_reg =ASPEED_ADC_REG_ADC10 + (channel /2 ) *4 ; + data_reg_val =readl (data->base + data_reg); +if (channel %2 ==0 ) { + adc_val = data_reg_val &0x3FF ; + }else { + adc_val = (data_reg_val >>16 ) &0x3FF ; + }I wonder if you could replace the above block with:
adc_val = readw(data->base + ASPEED_ADC_REG_ADC10 + channel);Quoted 168 lines
drivers/hwmon/aspeed_adc.c new file +9
-0+ +/* Scale 10-bit input reading to millivolts. */ + *val = adc_val *ASPEED_ADC_REF_VOLTAGE /1024 ; + +return 0 ; +} + +static umode_t aspeed_adc_hwmon_is_visible (const void *_data, + enum hwmon_sensor_typestype,
drivers/hwmon/aspeed_adc.c new file +12
-0+ u32 attr, int channel) +{ +const struct aspeed_adc_data * data = _data; + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { +return S_IRUGO |S_IWUSR ; + }else if (type == hwmon_in) { +/* Only channels that are enabled should be visible. */ +if (channel >=0 && channel <=ASPEED_ADC_NUM_CHANNELS && + (data->enabled_channel_mask &BIT (channel))) { + +/* All enabled channels have an input but labels are
drivers/hwmon/aspeed_adc.c new file +6
-0+* optional in the device tree. +*/ +if (attr == hwmon_in_input) { +return S_IRUGO ; + }else if (attr == hwmon_in_label && + data->channel_labels[channel] !=NULL) {
drivers/hwmon/aspeed_adc.c new file +9
-0+return S_IRUGO ; + } + } + } + +return 0 ; +} + +static int aspeed_adc_hwmon_read(struct device *dev, enum
hwmon_sensor_typestype,drivers/hwmon/aspeed_adc.c new file +9
-0+ u32 attr, int channel, long *val) +{ + struct aspeed_adc_data *data = dev_get_drvdata(dev); + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { + *val = data->update_interval_ms; +return 0 ; + }else if (type == hwmon_in && attr == hwmon_in_input) { + return aspeed_adc_get_channel_reading(data, channel,val);
drivers/hwmon/aspeed_adc.c new file +8
-0+ } + +return -EOPNOTSUPP ; +} + +static int aspeed_adc_hwmon_read_string (struct device *dev, +enum hwmon_sensor_types type, + u32 attr, int channel, char**str)
drivers/hwmon/aspeed_adc.c new file +12
-0+{ +struct aspeed_adc_data *data =dev_get_drvdata (dev); + +if (type == hwmon_in && attr == hwmon_in_label) { + *str = (char *)data->channel_labels[channel]; +return 0 ; + } + +return -EOPNOTSUPP ; +} + +static int aspeed_adc_hwmon_write(struct device *dev, enum
hwmon_sensor_typestype,drivers/hwmon/aspeed_adc.c new file +54
-0+ u32 attr, int channel, long val) +{ + struct aspeed_adc_data *data = dev_get_drvdata(dev); + +if (type == hwmon_chip && attr == hwmon_chip_update_interval) { +return aspeed_adc_set_adc_clock_frequency (data, val); + } + +return -EOPNOTSUPP ; +} + +static const struct hwmon_ops aspeed_adc_hwmon_ops = { + .is_visible = aspeed_adc_hwmon_is_visible, + .read = aspeed_adc_hwmon_read, + .read_string = aspeed_adc_hwmon_read_string, + .write = aspeed_adc_hwmon_write, +}; + +static const u32 aspeed_adc_hwmon_chip_config[] = { +HWMON_C_UPDATE_INTERVAL , +0 +}; + +static const struct hwmon_channel_info aspeed_adc_hwmon_chip_channel = { + .type = hwmon_chip, + .config = aspeed_adc_hwmon_chip_config, +}; + +static const u32 aspeed_adc_hwmon_in_config[] = { +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +HWMON_I_INPUT |HWMON_I_LABEL , +0 +}; + +static const struct hwmon_channel_info aspeed_adc_hwmon_in_channel = { + .type = hwmon_in, + .config = aspeed_adc_hwmon_in_config, +}; + +static const struct hwmon_channel_info *aspeed_adc_hwmon_channel_info[]= {
drivers/hwmon/aspeed_adc.c new file +37
-0+ &aspeed_adc_hwmon_chip_channel, + &aspeed_adc_hwmon_in_channel, +NULL +}; + +static const struct hwmon_chip_info aspeed_adc_hwmon_chip_info = { + .ops = &aspeed_adc_hwmon_ops, + .info = aspeed_adc_hwmon_channel_info, +}; + +static int aspeed_adc_probe (struct platform_device *pdev) +{ +struct aspeed_adc_data *data; +struct resource *res; +int ret; +struct device *hwmon_dev; +u32 desired_update_interval_ms; +u32 adc_engine_control_reg_val; +struct device_node * node; + + data =devm_kzalloc (&pdev->dev,sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->pclk =devm_clk_get (&pdev->dev,NULL ); +if (IS_ERR (data->pclk)) { +dev_err (&pdev->dev,"clk_get failed\n" ); +return PTR_ERR (data->pclk); + } + + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); + data->base =devm_ioremap_resource (&pdev->dev, res); +if (IS_ERR (data->base)) +return PTR_ERR (data->base); + + ret = of_property_read_u32(pdev->dev.of_node, + "update-interval-ms",
&desired_update_interval_ms);drivers/hwmon/aspeed_adc.c new file +3
-0+if (ret <0 || desired_update_interval_ms ==0 ) { + dev_err(&pdev->dev, + "Missing or zero update-interval-msQuoted 2 lines
property, "
+ "defaulting to 100ms\n");Put the string on one line so it can be easily searched for.
drivers/hwmon/aspeed_adc.c line 305
Quoted 10 lines
drivers/hwmon/aspeed_adc.c new file +10
-0+ desired_update_interval_ms =100 ; + } + +mutex_init (&data->lock); + + ret =clk_prepare_enable (data->pclk); +if (ret) { +dev_err (&pdev->dev,"failed to enable clock\n" ); +mutex_destroy (&data->lock); +return ret;Strange whitespace here.
Quoted 30 lines
drivers/hwmon/aspeed_adc.c new file +3
-0+ } + +/* Figure out which channels are marked available in the device tree. */
drivers/hwmon/aspeed_adc.c new file +22
-0+ data->enabled_channel_mask =0 ; +for_each_available_child_of_node (pdev->dev.of_node, node) { +u32 pval; +unsigned int channel; + +if (of_property_read_u32 (node,"reg" , &pval)) { +dev_err (&pdev->dev,"invalid reg on %s\n" , + node->full_name); +continue ; + } + + channel = pval; +if (channel >=ASPEED_ADC_NUM_CHANNELS ) { +dev_err (&pdev->dev, +"invalid channel index %d on %s\n" , + channel, node->full_name); +continue ; + } + + data->enabled_channel_mask |=BIT (channel); + +/* Cache a pointer to the label if one is specified. Ignore any
drivers/hwmon/aspeed_adc.c new file +3
-0+* errors as the label property is optional. +*/ + of_property_read_string(node, "label",Quoted 1 line
&data->channel_labels[channel]);
I was wondering how long we could keep that pointer around. I think we are ok for the lifetime of the driver, as we hold a reference to the node in dev.of_node.
Quoted 5 lines
drivers/hwmon/aspeed_adc.c new file +4
-0+ } + +platform_set_drvdata (pdev, data); + aspeed_adc_set_adc_clock_frequency(data,
desired_update_interval_ms);This reads funny.
aspeed_adc_set_clock_frequencyinstead?Quoted 49 lines
drivers/hwmon/aspeed_adc.c new file +4
-0+ +/* Start all the requested channels in normal mode. */ + adc_engine_control_reg_val = (data->enabled_channel_mask <<16 ) | + ASPEED_ADC_OPERATION_MODE_NORMAL |
ASPEED_ADC_ENGINE_ENABLE;
+ writel(adc_engine_control_reg_val,data->base+ASPEED_ADC_REG_ADC00);drivers/hwmon/aspeed_adc.c new file +41
-1+ +/* Register sysfs hooks */ + hwmon_dev =devm_hwmon_device_register_with_info ( + &pdev->dev,"aspeed_adc" , data, + &aspeed_adc_hwmon_chip_info,NULL ); +if (IS_ERR (hwmon_dev)) { +clk_disable_unprepare (data->pclk); +mutex_destroy (&data->lock); +return PTR_ERR (hwmon_dev); + } + +return 0 ; +} + +static int aspeed_adc_remove (struct platform_device *pdev) { +struct aspeed_adc_data *data =dev_get_drvdata (&pdev->dev); +clk_disable_unprepare (data->pclk); +mutex_destroy (&data->lock); +return 0 ; +} + +const struct of_device_id aspeed_adc_matches[] = { + { .compatible ="aspeed,ast2400-adc" }, + { .compatible ="aspeed,ast2500-adc" }, +}; +MODULE_DEVICE_TABLE (of, aspeed_adc_matches); + +static struct platform_driver aspeed_adc_driver = { + .probe = aspeed_adc_probe, + .remove = aspeed_adc_remove, + .driver = { + .name =KBUILD_MODNAME , + .of_match_table = aspeed_adc_matches, + } +}; + +module_platform_driver (aspeed_adc_driver); + +MODULE_AUTHOR ("Rick Altherr <[email protected]>" ); +MODULE_DESCRIPTION ("Aspeed AST2400/2500 ADC Driver" ); +MODULE_LICENSE ("GPL" ); --Quoted 2 lines
2.11.0.483.g087da7b7c-goog
--
To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Rob Herring
maintainerQuoted 3 lines · Rick Altherr
Signed-off-by Rick Altherr <[email protected]>--- .../devicetree/bindings/hwmon/aspeed_adc.txt | 48 ++++++++++++++++++++++
ADCs should really be documented in one place regardless of whether hwmon or IIO is used. Don't need to move it now, but certainly the bindings need to be compatible.
Quoted 17 lines
1 file changed, 48 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/aspeed_adc.txtDocumentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +8
-0@@ -0,0 +1,48 @@ +Aspeed AST2400/2500 ADC + +This device is a 10-bit converter for 16 voltage channels. All inputs are +single ended. Each channel can be individually enabled to allow for use of +alternate pin functions. + +1) adc node +
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt line 18
Quoted 10 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +10
-0+ Required properties: + - compatible : Should be one of + "aspeed,ast2400-adc" + "aspeed,ast2500-adc" + - reg : memory window mapping address and length + - #address-cells : must be <1> corresponding to the channel child binding + - #size-cells : must be <0> corresponding to the channel child binding + - clocks : Input clock used to derive the sample clock. Expected to be the + SoC's APB clock. + - update-interval-ms : initial time between updates on a channel
This is like sampling rate? I think we have a standard ADC property for that.
Quoted 10 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +10
-0+ + The node contains child nodes for each channel that the platform uses. + + Example adc node: + adc@1e6e9000 { + #address-cells = <1>; + #size-cells = <0>; + compatible = "aspeed,ast2400-adc"; + reg = <0x1e6e9000 0xB0>; + clocks = <&clk_apb>;
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt line 38
Quoted 10 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +10
-0+ update-interval-ms = <100>; + + [ child node definitions... ] + }; + +2) channel nodes + + Optional properties: + - status: indicates the operational status of the device. + Value must be either "disabled" or "okay".
Don't need to document this.
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt line 39
Quoted 1 line
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +1
-0+ - label : string describing the monitored value
You need a reg property for the channel number.
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt line 47
Quoted 8 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +8
-0+ + Example channel node: + channel@1 { + status = "okay"; + label = "3V3 rail"; + + pinctrl-names = "default"; + pinctrl-0 = <&pinctrl_adc0_default>;
Need to document these.
Quoted 4 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +1
-0+ };Signature · 1 line
2.11.0.483.g087da7b7c-goog
Rob Herring
maintainerRe: [PATCH 2/2] hwmon: Aspeed AST2400/AST2500 ADC
In reply to Guenter RoeckQuoted 10 lines · Guenter Roeck
Quoted 9 lines · Joel Stanley
Quoted 7 lines · Rick Altherr
Aspeed AST2400/AST2500 BMC SoCs include a 16 channel, 10-bit ADC. This driver implements reading the ADC values, enabling channels via device tree, and optionally providing channel labels via device tree. Low and high threshold interrupts are supported by the hardware but not implemented.
Signed-off-by Rick Altherr <[email protected]>
Quoted 7 lines
Quoted 4 lines
Looks good. Some minor comments below.
Is there a reason you wrote a hwmon driver instead of an iio driver? I wasn't sure what the recommended subsystem is.
Excellent point. Question is really if there is a plan to add support for thresholds. If not, an iio driver might be more appropriate.
Sigh. We have ADCs in 2 places? Fine for the kernel I guess, but not bindings.
Rob
Guenter Roeck
maintainerRe: [PATCH 2/2] hwmon: Aspeed AST2400/AST2500 ADC
Quoted 13 lines · Rob Herring
Quoted 12 lines · Guenter Roeck
Quoted 11 lines · Joel Stanley
Quoted 7 lines · Rick Altherr
Aspeed AST2400/AST2500 BMC SoCs include a 16 channel, 10-bit ADC. This driver implements reading the ADC values, enabling channels via device tree, and optionally providing channel labels via device tree. Low and high threshold interrupts are supported by the hardware but not implemented.
Signed-off-by Rick Altherr <[email protected]>Looks good. Some minor comments below.
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
Is there a reason you wrote a hwmon driver instead of an iio driver? I wasn't sure what the recommended subsystem is.
Excellent point. Question is really if there is a plan to add support for thresholds. If not, an iio driver might be more appropriate.
Sigh. We have ADCs in 2 places? Fine for the kernel I guess, but not bindings.
Almost every {voltage, current, power, temperature} sensor chip is implemented as ADC. Given that, we have (at least) three places. hwmon, iio, thermal.
Guenter
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Guenter Roeck
maintainerRe: [PATCH 2/2] hwmon: Aspeed AST2400/AST2500 ADC
In reply to Rick AltherrQuoted 25 lines · Rick Altherr
Resending in plain text.
Quoted 22 lines · Guenter Roeck
Quoted 9 lines · Joel Stanley
Quoted 7 lines · Rick Altherr
Aspeed AST2400/AST2500 BMC SoCs include a 16 channel, 10-bit ADC. This driver implements reading the ADC values, enabling channels via device tree, and optionally providing channel labels via device tree. Low and high threshold interrupts are supported by the hardware but not implemented.
Quoted 7 lines
Quoted 1 line
Signed-off-by Rick Altherr <[email protected]>Looks good. Some minor comments below.
Is there a reason you wrote a hwmon driver instead of an iio driver? I wasn't sure what the recommended subsystem is.
Excellent point. Question is really if there is a plan to add support for thresholds. If not, an iio driver might be more appropriate.
Quoted 8 lines
Quoted 2 lines
Guenter
The hardware supports firing interrupts on high and low thresholds. I'm not planning to use that feature yet so I didn't implement it. Would you prefer that I leave it as is (maybe with a TODO), implement the thresholding, or change to iio?
Let's try to determine the intended use of the ADC. I don't find the datasheet online; all I can find is brief summaries which don't me tell much, but suggest that it is a general purpose ADC and not specifically intended for hardware monitoring. What is the minimum sampling rate ? That should give us an idea. If it is in the uS range, iio would be more appropriate (and the iio-hwmon bridge could be used if a channel is in fact used for hardware monitoring).
Thanks,
Guenter
Quoted 9 lines · Rob Herring
Quoted 3 lines · Rick Altherr
Signed-off-by Rick Altherr <[email protected]>--- .../devicetree/bindings/hwmon/aspeed_adc.txt | 48 ++++++++++++++++++++++ADCs should really be documented in one place regardless of whether hwmon or IIO is used. Don't need to move it now, but certainly the bindings need to be compatible.
hwmon maintainers suggested IIO as well. Looks like there is an IIO to hwmon bridge so no concerns there. I think IIO looks like a plausible framework for this part based on my cursory look at the IIO docs. I'll be working on v2 as an IIO driver.
Quoted 22 lines
Quoted 22 lines
1 file changed, 48 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/aspeed_adc.txtDocumentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +13
-0@@ -0,0 +1,48 @@ +Aspeed AST2400/2500 ADC + +This device is a 10-bit converter for 16 voltage channels. All inputs are +single ended. Each channel can be individually enabled to allow for use of +alternate pin functions. + +1) adc node + + Required properties: + - compatible : Should be one of + "aspeed,ast2400-adc" + "aspeed,ast2500-adc" + - reg : memory window mapping address and length
Quoted 9 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt line 18
Quoted 5 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +5
-0+ - #address-cells : must be <1> corresponding to the channel child binding + - #size-cells : must be <0> corresponding to the channel child binding + - clocks : Input clock used to derive the sample clock. Expected to be the + SoC's APB clock. + - update-interval-ms : initial time between updates on a channelThis is like sampling rate? I think we have a standard ADC property for that.
Will look.
Quoted 14 lines
Quoted 14 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +14
-0+ + The node contains child nodes for each channel that the platform uses. + + Example adc node: + adc@1e6e9000 { + #address-cells = <1>; + #size-cells = <0>; + compatible = "aspeed,ast2400-adc"; + reg = <0x1e6e9000 0xB0>; + clocks = <&clk_apb>; + update-interval-ms = <100>; + + [ child node definitions... ] + };
Quoted 9 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt line 38
Quoted 6 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +6
-0+ +2) channel nodes + + Optional properties: + - status: indicates the operational status of the device. + Value must be either "disabled" or "okay".Don't need to document this.
OK
Quoted 4 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt line 39
Quoted 1 line
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +1
-0+ - label : string describing the monitored valueYou need a reg property for the channel number.
Missed that when I fixed the code. Will include in next revision.
Quoted 10 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt line 47
Quoted 8 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +8
-0+ + Example channel node: + channel@1 { + status = "okay"; + label = "3V3 rail"; + + pinctrl-names = "default"; + pinctrl-0 = <&pinctrl_adc0_default>;Need to document these.
These are described in the pinctrl bindings. I only put them in the example as they will be commonly used together but the driver does not use them.
Quoted 5 lines
Quoted 5 lines
Documentation/devicetree/bindings/hwmon/aspeed_adc.txt new file +1
-1+ }; --2.11.0.483.g087da7b7c-goog
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 2/2] hwmon: Aspeed AST2400/AST2500 ADC
In reply to Guenter RoeckQuoted 51 lines · Guenter Roeck
Quoted 32 lines · Rick Altherr
Resending in plain text.
Quoted 28 lines · Guenter Roeck
Quoted 12 lines · Joel Stanley
Quoted 8 lines · Rick Altherr
Aspeed AST2400/AST2500 BMC SoCs include a 16 channel, 10-bit ADC. This driver implements reading the ADC values, enabling channels via device tree, and optionally providing channel labels via device tree. Low and high threshold interrupts are supported by the hardware but not implemented.
Quoted 8 lines
Quoted 1 line
Signed-off-by Rick Altherr <[email protected]>Looks good. Some minor comments below.
Is there a reason you wrote a hwmon driver instead of an iio driver? I wasn't sure what the recommended subsystem is.
Excellent point. Question is really if there is a plan to add support for thresholds. If not, an iio driver might be more appropriate.
Quoted 8 lines
Quoted 2 lines
Guenter
The hardware supports firing interrupts on high and low thresholds. I'm not planning to use that feature yet so I didn't implement it. Would you prefer that I leave it as is (maybe with a TODO), implement the thresholding, or change to iio?
Let's try to determine the intended use of the ADC. I don't find the datasheet
online; all I can find is brief summaries which don't me tell much, but suggest
that it is a general purpose ADC and not specifically intended for hardware monitoring. What is the minimum sampling rate ? That should give us an idea. If it is in the uS range, iio would be more appropriate (and the iio-hwmon bridge could be used if a channel is in fact used for hardware monitoring).
Quoted 3 lines
Thanks,
Guenter
AST2500 is a BMC SoC from Aspeed
(https://www.aspeedtech.com/products.php?fPath=20&rId=440). The BMC is a separate, always-on computer that manages the health and remote management for a server. The driver I sent is for the ADCs included in the SoC that are intended to monitor power rails on the server motherboard but are really just general-purpose ADCs. According to the (not public) datasheet, the sampling rate is 10-500kHz, resolution is 10-bit, and precision +/- 2%. This is my first time writing a linux driver for ADCs. My cursory look at iio indicates that that will work fine for this driver and the hwmon-iio bridge will suffice for the userspace stack which is currently expecting hwmon APIs.