linux-pwm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM
@ 2023-02-17 16:10 Angelo Compagnucci
  2023-02-17 16:10 ` [PATCH v3 2/3] misc: servo-pwm: Add sysfs entries to control motor angle Angelo Compagnucci
                   ` (5 more replies)
  0 siblings, 6 replies; 10+ messages in thread
From: Angelo Compagnucci @ 2023-02-17 16:10 UTC (permalink / raw)
  Cc: Angelo Compagnucci, Derek Kiernan, Dragan Cvetic, Arnd Bergmann,
	Greg Kroah-Hartman, Thierry Reding, Uwe Kleine-König,
	open list, open list:GENERIC PWM SERVO DRIVER

This patch adds a simple driver to control servo motor position via
PWM signal.
The driver allows to set the angle from userspace, while min/max
positions duty cycle and the motor degrees aperture are defined in
the dts.

Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
---
v2:
* Driver mostly rewritten for kernel 6.2
v3:
* Fixed sysfs_emit (greg k-h)

 MAINTAINERS              |   6 ++
 drivers/misc/Kconfig     |  11 +++
 drivers/misc/Makefile    |   1 +
 drivers/misc/servo-pwm.c | 149 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 167 insertions(+)
 create mode 100644 drivers/misc/servo-pwm.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 39ff1a717625..8f4af64deb1b 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8737,6 +8737,12 @@ F:	Documentation/devicetree/bindings/power/power?domain*
 F:	drivers/base/power/domain*.c
 F:	include/linux/pm_domain.h
 
+GENERIC PWM SERVO DRIVER
+M:	"Angelo Compagnucci" <angelo@amarulasolutions.com>
+L:	linux-pwm@vger.kernel.org
+S:	Maintained
+F:	drivers/misc/servo-pwm.c
+
 GENERIC RESISTIVE TOUCHSCREEN ADC DRIVER
 M:	Eugen Hristev <eugen.hristev@microchip.com>
 L:	linux-input@vger.kernel.org
diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index 9947b7892bd5..8a74087149ac 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -518,6 +518,17 @@ config VCPU_STALL_DETECTOR
 
 	  If you do not intend to run this kernel as a guest, say N.
 
+config SERVO_PWM
+	tristate "Servo motor positioning"
+	depends on PWM
+	help
+	  Driver to control generic servo motor positioning.
+	  Writing to the "angle" device attribute, the motor will move to
+	  the angle position.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called servo-pwm.
+
 source "drivers/misc/c2port/Kconfig"
 source "drivers/misc/eeprom/Kconfig"
 source "drivers/misc/cb710/Kconfig"
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index 87b54a4a4422..936629b648a9 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -64,3 +64,4 @@ obj-$(CONFIG_HI6421V600_IRQ)	+= hi6421v600-irq.o
 obj-$(CONFIG_OPEN_DICE)		+= open-dice.o
 obj-$(CONFIG_GP_PCI1XXXX)	+= mchp_pci1xxxx/
 obj-$(CONFIG_VCPU_STALL_DETECTOR)	+= vcpu_stall_detector.o
+obj-$(CONFIG_SERVO_PWM)	+= servo-pwm.o
diff --git a/drivers/misc/servo-pwm.c b/drivers/misc/servo-pwm.c
new file mode 100644
index 000000000000..1303ddda8d07
--- /dev/null
+++ b/drivers/misc/servo-pwm.c
@@ -0,0 +1,149 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2023 Angelo Compagnucci <angelo@amarulasolutions.com>
+ * servo-pwm.c - driver for controlling servo motors via pwm.
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/platform_device.h>
+#include <linux/err.h>
+#include <linux/pwm.h>
+
+#define DEFAULT_DUTY_MIN	500000
+#define DEFAULT_DUTY_MAX	2500000
+#define DEFAULT_DEGREES		175
+#define DEFAULT_ANGLE		0
+
+struct servo_pwm_data {
+	u32 duty_min;
+	u32 duty_max;
+	u32 degrees;
+	u32 angle;
+
+	struct mutex lock;
+	struct pwm_device *pwm;
+	struct pwm_state pwmstate;
+};
+
+static int servo_pwm_set(struct servo_pwm_data *data, int val)
+{
+	u64 new_duty = (((data->duty_max - data->duty_min) /
+			data->degrees) * val) + data->duty_min;
+	int ret;
+
+	mutex_lock(&data->lock);
+
+	data->pwmstate.duty_cycle = new_duty;
+	data->pwmstate.enabled = 1;
+	ret = pwm_apply_state(data->pwm, &data->pwmstate);
+
+	if (!ret)
+		data->angle = val;
+
+	mutex_unlock(&data->lock);
+
+	return ret;
+}
+
+static ssize_t angle_show(struct device *dev, struct device_attribute *attr,
+			  char *buf)
+{
+	struct servo_pwm_data *data = dev_get_drvdata(dev);
+
+	return sysfs_emit(buf, PAGE_SIZE, "%u\n", data->angle);
+}
+
+static ssize_t angle_store(struct device *dev, struct device_attribute *attr,
+			   const char *buf, size_t count)
+{
+	struct servo_pwm_data *data = dev_get_drvdata(dev);
+	int ret, val = 0;
+
+	ret = kstrtoint(buf, 10, &val);
+	if (ret < 0)
+		return -EINVAL;
+
+	if (val < 0 || val > data->degrees)
+		return -EINVAL;
+
+	ret = servo_pwm_set(data, val);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static DEVICE_ATTR_RW(angle);
+
+static ssize_t degrees_show(struct device *dev, struct device_attribute *attr,
+			  char *buf)
+{
+	struct servo_pwm_data *data = dev_get_drvdata(dev);
+
+	return sysfs_emit(buf, PAGE_SIZE, "%u\n", data->degrees);
+}
+
+static DEVICE_ATTR_RO(degrees);
+
+static struct attribute *servo_pwm_attrs[] = {
+	&dev_attr_angle.attr,
+	&dev_attr_degrees.attr,
+	NULL,
+};
+
+ATTRIBUTE_GROUPS(servo_pwm);
+
+static int servo_pwm_probe(struct platform_device *pdev)
+{
+	struct fwnode_handle *fwnode = dev_fwnode(&pdev->dev);
+	struct servo_pwm_data *data;
+
+	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	mutex_init(&data->lock);
+
+	if (!fwnode_property_read_u32(fwnode, "duty-min", &data->duty_min) == 0)
+		data->duty_min = DEFAULT_DUTY_MIN;
+
+	if (!fwnode_property_read_u32(fwnode, "duty-max", &data->duty_max) == 0)
+		data->duty_max = DEFAULT_DUTY_MAX;
+
+	if (!fwnode_property_read_u32(fwnode, "degrees", &data->degrees) == 0)
+		data->degrees = DEFAULT_DEGREES;
+
+	data->pwm = devm_fwnode_pwm_get(&pdev->dev, fwnode, NULL);
+	if (IS_ERR(data->pwm)) {
+		return dev_err_probe(&pdev->dev, PTR_ERR(data->pwm),
+				     "unable to request PWM\n");
+	}
+
+	pwm_init_state(data->pwm, &data->pwmstate);
+
+	platform_set_drvdata(pdev, data);
+
+	return 0;
+}
+
+static const struct of_device_id of_servo_pwm_match[] = {
+	{ .compatible = "servo-pwm", },
+	{},
+};
+MODULE_DEVICE_TABLE(of, of_servo_pwm_match);
+
+static struct platform_driver servo_pwm_driver = {
+	.probe		= servo_pwm_probe,
+	.driver		= {
+		.name			= "servo-pwm",
+		.of_match_table		= of_servo_pwm_match,
+		.dev_groups		= servo_pwm_groups,
+	},
+};
+
+module_platform_driver(servo_pwm_driver);
+
+MODULE_AUTHOR("Angelo Compagnucci <angelo@amarulasolutions.com>");
+MODULE_DESCRIPTION("generic PWM servo motor driver");
+MODULE_LICENSE("GPL");
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 2/3] misc: servo-pwm: Add sysfs entries to control motor angle
  2023-02-17 16:10 [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM Angelo Compagnucci
@ 2023-02-17 16:10 ` Angelo Compagnucci
  2023-02-17 16:10 ` [PATCH v3 3/3] dt-bindings: misc: servo-pwm: Add new bindings for servo-pwm Angelo Compagnucci
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: Angelo Compagnucci @ 2023-02-17 16:10 UTC (permalink / raw)
  Cc: Angelo Compagnucci, open list:GENERIC PWM SERVO DRIVER, open list

Servo motor can be moved between 0 and degrees angle.

Add 'angle' sysfs attribute:
  *read*: Current motor position.
  *write*: Moves the motor to the position.

Add 'degrees' sysfs attribute:
  *read*: how many degrees the motor can move

Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
---
v2:
* Added ABI in testing
v3:
* No changes

 .../ABI/testing/sysfs-driver-servo-pwm        | 20 +++++++++++++++++++
 MAINTAINERS                                   |  1 +
 2 files changed, 21 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-driver-servo-pwm

diff --git a/Documentation/ABI/testing/sysfs-driver-servo-pwm b/Documentation/ABI/testing/sysfs-driver-servo-pwm
new file mode 100644
index 000000000000..9c4d897073fa
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-driver-servo-pwm
@@ -0,0 +1,20 @@
+What:		/sys/devices/platform/servo*/angle
+Date:		Feb 2023
+Contact:	Angelo Compagnucci <angelo@amarulasolutions.com>
+Description:
+		(RW) read or write servo motor position angle.
+		Servo motor can move between 0 and max degrees angle.
+		As soon the vale is written, the motor will move to the selected
+		angle. Reading the value gives the motor position.
+Users:		any user space application which wants to move the servo
+		motor position.
+
+What:		/sys/devices/platform/servo*/degrees
+Date:		Feb 2023
+Contact:	Angelo Compagnucci <angelo@amarulasolutions.com>
+Description:
+		(RO) read the servo motor movement degrees.
+		Servo motor can move between 0 and max degrees angle.
+		Reading the value gives the motor max degrees angle supported.
+Users:		any user space application which wants to know the max degrees
+		angle motor supports.
diff --git a/MAINTAINERS b/MAINTAINERS
index 8f4af64deb1b..356daea0861d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8741,6 +8741,7 @@ GENERIC PWM SERVO DRIVER
 M:	"Angelo Compagnucci" <angelo@amarulasolutions.com>
 L:	linux-pwm@vger.kernel.org
 S:	Maintained
+F:	Documentation/ABI/testing/sysfs-driver-servo-pwm
 F:	drivers/misc/servo-pwm.c
 
 GENERIC RESISTIVE TOUCHSCREEN ADC DRIVER
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v3 3/3] dt-bindings: misc: servo-pwm: Add new bindings for servo-pwm
  2023-02-17 16:10 [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM Angelo Compagnucci
  2023-02-17 16:10 ` [PATCH v3 2/3] misc: servo-pwm: Add sysfs entries to control motor angle Angelo Compagnucci
@ 2023-02-17 16:10 ` Angelo Compagnucci
  2023-02-17 17:29   ` Rob Herring
  2023-02-18 10:16   ` Krzysztof Kozlowski
  2023-02-17 21:12 ` [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM kernel test robot
                   ` (3 subsequent siblings)
  5 siblings, 2 replies; 10+ messages in thread
From: Angelo Compagnucci @ 2023-02-17 16:10 UTC (permalink / raw)
  Cc: Angelo Compagnucci, Rob Herring, Krzysztof Kozlowski,
	open list:GENERIC PWM SERVO DRIVER,
	open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS,
	open list

This binding describes the binding for controlling servo motors through
pwm.

Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
---
v2:
* Converted old txt to yaml
v3:
* Fixed errors rised by make dt_binding_check 

 .../devicetree/bindings/misc/servo-pwm.yaml   | 57 +++++++++++++++++++
 MAINTAINERS                                   |  1 +
 2 files changed, 58 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/misc/servo-pwm.yaml

diff --git a/Documentation/devicetree/bindings/misc/servo-pwm.yaml b/Documentation/devicetree/bindings/misc/servo-pwm.yaml
new file mode 100644
index 000000000000..73e81b939daf
--- /dev/null
+++ b/Documentation/devicetree/bindings/misc/servo-pwm.yaml
@@ -0,0 +1,57 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/misc/servo-pwm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Servo motor connected to PWM
+
+maintainers:
+  - Angelo Compagnucci <angelo@amarulasolutions.com>
+
+description:
+  Each servo is represented as a servo-pwm device.
+  The 20ms period is the accepted standard and so most of the motors
+  support it, while the positioning min/max duty cycle or the motor
+  degrees aperture vary lot between manufacturers.
+  The most common type of servo (SG90) has 180 degrees of movement
+  and moves between 0.5ms and 2.5ms duty cycle.
+
+properties:
+  compatible:
+    const: servo-pwm
+
+  pwms:
+    maxItems: 1
+
+  pwm-names: true
+
+  degrees:
+    description:
+      How many degrees the motor can move.
+    $ref: /schemas/types.yaml#/definitions/uint32
+
+  duty-min:
+    description:
+      Duty cycle for position the motor at 0 degrees.
+    $ref: /schemas/types.yaml#/definitions/uint32
+
+  duty-max:
+    description:
+      Duty cycle for positioning the motor at "degrees" angle.
+    $ref: /schemas/types.yaml#/definitions/uint32
+
+additionalProperties: false
+
+examples:
+  - |
+
+    servo: servo@0 {
+      compatible = "servo-pwm";
+      pwms = <&pwm 0 20000000 0>;
+      degrees = <180>;
+      duty-min = <500000>;
+      duty-max = <2500000>;
+    };
+
+...
diff --git a/MAINTAINERS b/MAINTAINERS
index 356daea0861d..8f41daee62fc 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -8742,6 +8742,7 @@ M:	"Angelo Compagnucci" <angelo@amarulasolutions.com>
 L:	linux-pwm@vger.kernel.org
 S:	Maintained
 F:	Documentation/ABI/testing/sysfs-driver-servo-pwm
+F:	Documentation/devicetree/bindings/misc/servo-pwm.yaml
 F:	drivers/misc/servo-pwm.c
 
 GENERIC RESISTIVE TOUCHSCREEN ADC DRIVER
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 3/3] dt-bindings: misc: servo-pwm: Add new bindings for servo-pwm
  2023-02-17 16:10 ` [PATCH v3 3/3] dt-bindings: misc: servo-pwm: Add new bindings for servo-pwm Angelo Compagnucci
@ 2023-02-17 17:29   ` Rob Herring
  2023-02-18 10:16   ` Krzysztof Kozlowski
  1 sibling, 0 replies; 10+ messages in thread
From: Rob Herring @ 2023-02-17 17:29 UTC (permalink / raw)
  To: Angelo Compagnucci
  Cc: Rob Herring, linux-pwm, devicetree, Angelo Compagnucci,
	linux-kernel, Krzysztof Kozlowski


On Fri, 17 Feb 2023 17:10:37 +0100, Angelo Compagnucci wrote:
> This binding describes the binding for controlling servo motors through
> pwm.
> 
> Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
> ---
> v2:
> * Converted old txt to yaml
> v3:
> * Fixed errors rised by make dt_binding_check
> 
>  .../devicetree/bindings/misc/servo-pwm.yaml   | 57 +++++++++++++++++++
>  MAINTAINERS                                   |  1 +
>  2 files changed, 58 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/misc/servo-pwm.yaml
> 

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check'
on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Documentation/devicetree/bindings/misc/servo-pwm.example.dts:19.24-25.11: Warning (unit_address_vs_reg): /example-0/servo@0: node has a unit name, but no reg or ranges property

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/20230217161038.3130053-3-angelo@amarulasolutions.com

The base for the series is generally the latest rc1. A different dependency
should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above
error(s), then make sure 'yamllint' is installed and dt-schema is up to
date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note
that DT_SCHEMA_FILES can be set to your schema file to speed up checking
your schema. However, it must be unset to test all examples with your schema.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM
  2023-02-17 16:10 [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM Angelo Compagnucci
  2023-02-17 16:10 ` [PATCH v3 2/3] misc: servo-pwm: Add sysfs entries to control motor angle Angelo Compagnucci
  2023-02-17 16:10 ` [PATCH v3 3/3] dt-bindings: misc: servo-pwm: Add new bindings for servo-pwm Angelo Compagnucci
@ 2023-02-17 21:12 ` kernel test robot
  2023-02-17 21:22 ` kernel test robot
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: kernel test robot @ 2023-02-17 21:12 UTC (permalink / raw)
  To: Angelo Compagnucci
  Cc: oe-kbuild-all, Angelo Compagnucci, Derek Kiernan, Dragan Cvetic,
	Arnd Bergmann, Greg Kroah-Hartman, Thierry Reding,
	Uwe Kleine-König, linux-kernel, linux-pwm

Hi Angelo,

I love your patch! Perhaps something to improve:

[auto build test WARNING on char-misc/char-misc-linus]
[also build test WARNING on linus/master v6.2-rc8]
[cannot apply to char-misc/char-misc-testing char-misc/char-misc-next next-20230217]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Angelo-Compagnucci/misc-servo-pwm-Add-sysfs-entries-to-control-motor-angle/20230218-001254
patch link:    https://lore.kernel.org/r/20230217161038.3130053-1-angelo%40amarulasolutions.com
patch subject: [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM
config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20230218/202302180429.NvI1mwuf-lkp@intel.com/config)
compiler: loongarch64-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/0ae16e16da0aa94de0d3ae63166f50a4a6fdef8a
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Angelo-Compagnucci/misc-servo-pwm-Add-sysfs-entries-to-control-motor-angle/20230218-001254
        git checkout 0ae16e16da0aa94de0d3ae63166f50a4a6fdef8a
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=loongarch olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=loongarch SHELL=/bin/bash drivers/misc/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>
| Link: https://lore.kernel.org/oe-kbuild-all/202302180429.NvI1mwuf-lkp@intel.com/

All warnings (new ones prefixed by >>):

   In file included from include/linux/mm_types_task.h:16,
                    from include/linux/mm_types.h:5,
                    from include/linux/buildid.h:5,
                    from include/linux/module.h:14,
                    from drivers/misc/servo-pwm.c:7:
   drivers/misc/servo-pwm.c: In function 'angle_show':
>> arch/loongarch/include/asm/page.h:23:25: warning: passing argument 2 of 'sysfs_emit' makes pointer from integer without a cast [-Wint-conversion]
      23 | #define PAGE_SIZE       (_AC(1, UL) << PAGE_SHIFT)
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~
         |                         |
         |                         long unsigned int
   drivers/misc/servo-pwm.c:54:32: note: in expansion of macro 'PAGE_SIZE'
      54 |         return sysfs_emit(buf, PAGE_SIZE, "%u\n", data->angle);
         |                                ^~~~~~~~~
   In file included from include/linux/kobject.h:20,
                    from include/linux/module.h:21:
   include/linux/sysfs.h:357:39: note: expected 'const char *' but argument is of type 'long unsigned int'
     357 | int sysfs_emit(char *buf, const char *fmt, ...);
         |                           ~~~~~~~~~~~~^~~
   drivers/misc/servo-pwm.c: In function 'degrees_show':
>> arch/loongarch/include/asm/page.h:23:25: warning: passing argument 2 of 'sysfs_emit' makes pointer from integer without a cast [-Wint-conversion]
      23 | #define PAGE_SIZE       (_AC(1, UL) << PAGE_SHIFT)
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~
         |                         |
         |                         long unsigned int
   drivers/misc/servo-pwm.c:84:32: note: in expansion of macro 'PAGE_SIZE'
      84 |         return sysfs_emit(buf, PAGE_SIZE, "%u\n", data->degrees);
         |                                ^~~~~~~~~
   include/linux/sysfs.h:357:39: note: expected 'const char *' but argument is of type 'long unsigned int'
     357 | int sysfs_emit(char *buf, const char *fmt, ...);
         |                           ~~~~~~~~~~~~^~~


vim +/sysfs_emit +23 arch/loongarch/include/asm/page.h

09cfefb7fa70c3 Huacai Chen 2022-05-31  10  
09cfefb7fa70c3 Huacai Chen 2022-05-31  11  /*
09cfefb7fa70c3 Huacai Chen 2022-05-31  12   * PAGE_SHIFT determines the page size
09cfefb7fa70c3 Huacai Chen 2022-05-31  13   */
09cfefb7fa70c3 Huacai Chen 2022-05-31  14  #ifdef CONFIG_PAGE_SIZE_4KB
09cfefb7fa70c3 Huacai Chen 2022-05-31  15  #define PAGE_SHIFT	12
09cfefb7fa70c3 Huacai Chen 2022-05-31  16  #endif
09cfefb7fa70c3 Huacai Chen 2022-05-31  17  #ifdef CONFIG_PAGE_SIZE_16KB
09cfefb7fa70c3 Huacai Chen 2022-05-31  18  #define PAGE_SHIFT	14
09cfefb7fa70c3 Huacai Chen 2022-05-31  19  #endif
09cfefb7fa70c3 Huacai Chen 2022-05-31  20  #ifdef CONFIG_PAGE_SIZE_64KB
09cfefb7fa70c3 Huacai Chen 2022-05-31  21  #define PAGE_SHIFT	16
09cfefb7fa70c3 Huacai Chen 2022-05-31  22  #endif
09cfefb7fa70c3 Huacai Chen 2022-05-31 @23  #define PAGE_SIZE	(_AC(1, UL) << PAGE_SHIFT)
09cfefb7fa70c3 Huacai Chen 2022-05-31  24  #define PAGE_MASK	(~(PAGE_SIZE - 1))
09cfefb7fa70c3 Huacai Chen 2022-05-31  25  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM
  2023-02-17 16:10 [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM Angelo Compagnucci
                   ` (2 preceding siblings ...)
  2023-02-17 21:12 ` [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM kernel test robot
@ 2023-02-17 21:22 ` kernel test robot
  2023-02-20 11:40 ` Thierry Reding
  2023-07-15 19:17 ` Uwe Kleine-König
  5 siblings, 0 replies; 10+ messages in thread
From: kernel test robot @ 2023-02-17 21:22 UTC (permalink / raw)
  To: Angelo Compagnucci
  Cc: oe-kbuild-all, Angelo Compagnucci, Derek Kiernan, Dragan Cvetic,
	Arnd Bergmann, Greg Kroah-Hartman, Thierry Reding,
	Uwe Kleine-König, linux-kernel, linux-pwm

Hi Angelo,

I love your patch! Perhaps something to improve:

[auto build test WARNING on char-misc/char-misc-linus]
[also build test WARNING on linus/master v6.2-rc8]
[cannot apply to char-misc/char-misc-testing char-misc/char-misc-next next-20230217]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Angelo-Compagnucci/misc-servo-pwm-Add-sysfs-entries-to-control-motor-angle/20230218-001254
patch link:    https://lore.kernel.org/r/20230217161038.3130053-1-angelo%40amarulasolutions.com
patch subject: [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM
config: sparc-allyesconfig (https://download.01.org/0day-ci/archive/20230218/202302180513.AlOeQWNt-lkp@intel.com/config)
compiler: sparc64-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/0ae16e16da0aa94de0d3ae63166f50a4a6fdef8a
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Angelo-Compagnucci/misc-servo-pwm-Add-sysfs-entries-to-control-motor-angle/20230218-001254
        git checkout 0ae16e16da0aa94de0d3ae63166f50a4a6fdef8a
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=sparc olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=sparc SHELL=/bin/bash drivers/misc/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>
| Link: https://lore.kernel.org/oe-kbuild-all/202302180513.AlOeQWNt-lkp@intel.com/

All warnings (new ones prefixed by >>):

   In file included from arch/sparc/include/asm/page.h:8,
                    from arch/sparc/include/asm/sparsemem.h:7,
                    from include/linux/numa.h:25,
                    from include/linux/cpumask.h:16,
                    from include/linux/mm_types_task.h:14,
                    from include/linux/mm_types.h:5,
                    from include/linux/buildid.h:5,
                    from include/linux/module.h:14,
                    from drivers/misc/servo-pwm.c:7:
   drivers/misc/servo-pwm.c: In function 'angle_show':
>> arch/sparc/include/asm/page_64.h:9:22: warning: passing argument 2 of 'sysfs_emit' makes pointer from integer without a cast [-Wint-conversion]
       9 | #define PAGE_SIZE    (_AC(1,UL) << PAGE_SHIFT)
         |                      ^~~~~~~~~~~~~~~~~~~~~~~~~
         |                      |
         |                      long unsigned int
   drivers/misc/servo-pwm.c:54:32: note: in expansion of macro 'PAGE_SIZE'
      54 |         return sysfs_emit(buf, PAGE_SIZE, "%u\n", data->angle);
         |                                ^~~~~~~~~
   In file included from include/linux/kobject.h:20,
                    from include/linux/module.h:21:
   include/linux/sysfs.h:357:39: note: expected 'const char *' but argument is of type 'long unsigned int'
     357 | int sysfs_emit(char *buf, const char *fmt, ...);
         |                           ~~~~~~~~~~~~^~~
   drivers/misc/servo-pwm.c: In function 'degrees_show':
>> arch/sparc/include/asm/page_64.h:9:22: warning: passing argument 2 of 'sysfs_emit' makes pointer from integer without a cast [-Wint-conversion]
       9 | #define PAGE_SIZE    (_AC(1,UL) << PAGE_SHIFT)
         |                      ^~~~~~~~~~~~~~~~~~~~~~~~~
         |                      |
         |                      long unsigned int
   drivers/misc/servo-pwm.c:84:32: note: in expansion of macro 'PAGE_SIZE'
      84 |         return sysfs_emit(buf, PAGE_SIZE, "%u\n", data->degrees);
         |                                ^~~~~~~~~
   include/linux/sysfs.h:357:39: note: expected 'const char *' but argument is of type 'long unsigned int'
     357 | int sysfs_emit(char *buf, const char *fmt, ...);
         |                           ~~~~~~~~~~~~^~~


vim +/sysfs_emit +9 arch/sparc/include/asm/page_64.h

f5e706ad886b6a include/asm-sparc/page_64.h Sam Ravnborg 2008-07-17   8  
f5e706ad886b6a include/asm-sparc/page_64.h Sam Ravnborg 2008-07-17  @9  #define PAGE_SIZE    (_AC(1,UL) << PAGE_SHIFT)
f5e706ad886b6a include/asm-sparc/page_64.h Sam Ravnborg 2008-07-17  10  #define PAGE_MASK    (~(PAGE_SIZE-1))
f5e706ad886b6a include/asm-sparc/page_64.h Sam Ravnborg 2008-07-17  11  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 3/3] dt-bindings: misc: servo-pwm: Add new bindings for servo-pwm
  2023-02-17 16:10 ` [PATCH v3 3/3] dt-bindings: misc: servo-pwm: Add new bindings for servo-pwm Angelo Compagnucci
  2023-02-17 17:29   ` Rob Herring
@ 2023-02-18 10:16   ` Krzysztof Kozlowski
  1 sibling, 0 replies; 10+ messages in thread
From: Krzysztof Kozlowski @ 2023-02-18 10:16 UTC (permalink / raw)
  To: Angelo Compagnucci
  Cc: Angelo Compagnucci, Rob Herring, Krzysztof Kozlowski,
	open list:GENERIC PWM SERVO DRIVER,
	open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS,
	open list

On 17/02/2023 17:10, Angelo Compagnucci wrote:
> This binding describes the binding for controlling servo motors through
> pwm.
> 
> Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
> ---
> v2:
> * Converted old txt to yaml
> v3:
> * Fixed errors rised by make dt_binding_check 

Still fails the tests.

Does not look like you tested the bindings. Please run `make
dt_binding_check` (see
Documentation/devicetree/bindings/writing-schema.rst for instructions).

> 
>  .../devicetree/bindings/misc/servo-pwm.yaml   | 57 +++++++++++++++++++
>  MAINTAINERS                                   |  1 +
>  2 files changed, 58 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/misc/servo-pwm.yaml
> 
> diff --git a/Documentation/devicetree/bindings/misc/servo-pwm.yaml b/Documentation/devicetree/bindings/misc/servo-pwm.yaml
> new file mode 100644
> index 000000000000..73e81b939daf
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/misc/servo-pwm.yaml
> @@ -0,0 +1,57 @@
> +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/misc/servo-pwm.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Servo motor connected to PWM
> +
> +maintainers:
> +  - Angelo Compagnucci <angelo@amarulasolutions.com>
> +
> +description:
> +  Each servo is represented as a servo-pwm device.
> +  The 20ms period is the accepted standard and so most of the motors
> +  support it, while the positioning min/max duty cycle or the motor
> +  degrees aperture vary lot between manufacturers.
> +  The most common type of servo (SG90) has 180 degrees of movement
> +  and moves between 0.5ms and 2.5ms duty cycle.
> +
> +properties:
> +  compatible:
> +    const: servo-pwm
> +
> +  pwms:
> +    maxItems: 1
> +
> +  pwm-names: true

You got later feedback from Rob for v2, so this needs changes.

> +
> +  degrees:
> +    description:
> +      How many degrees the motor can move.
> +    $ref: /schemas/types.yaml#/definitions/uint32
> +
> +  duty-min:
> +    description:
> +      Duty cycle for position the motor at 0 degrees.
> +    $ref: /schemas/types.yaml#/definitions/uint32
> +
> +  duty-max:
> +    description:
> +      Duty cycle for positioning the motor at "degrees" angle.
> +    $ref: /schemas/types.yaml#/definitions/uint32
> +
> +additionalProperties: false
> +
> +examples:
> +  - |
> +

Drop blank line.

Krzysztof


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM
  2023-02-17 16:10 [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM Angelo Compagnucci
                   ` (3 preceding siblings ...)
  2023-02-17 21:22 ` kernel test robot
@ 2023-02-20 11:40 ` Thierry Reding
  2023-02-20 11:46   ` Angelo Compagnucci
  2023-07-15 19:17 ` Uwe Kleine-König
  5 siblings, 1 reply; 10+ messages in thread
From: Thierry Reding @ 2023-02-20 11:40 UTC (permalink / raw)
  To: Angelo Compagnucci
  Cc: Angelo Compagnucci, Derek Kiernan, Dragan Cvetic, Arnd Bergmann,
	Greg Kroah-Hartman, Uwe Kleine-König, open list,
	open list:GENERIC PWM SERVO DRIVER

[-- Attachment #1: Type: text/plain, Size: 4065 bytes --]

On Fri, Feb 17, 2023 at 05:10:35PM +0100, Angelo Compagnucci wrote:
> This patch adds a simple driver to control servo motor position via
> PWM signal.
> The driver allows to set the angle from userspace, while min/max
> positions duty cycle and the motor degrees aperture are defined in
> the dts.
> 
> Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
> ---
> v2:
> * Driver mostly rewritten for kernel 6.2
> v3:
> * Fixed sysfs_emit (greg k-h)
> 
>  MAINTAINERS              |   6 ++
>  drivers/misc/Kconfig     |  11 +++
>  drivers/misc/Makefile    |   1 +
>  drivers/misc/servo-pwm.c | 149 +++++++++++++++++++++++++++++++++++++++
>  4 files changed, 167 insertions(+)
>  create mode 100644 drivers/misc/servo-pwm.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 39ff1a717625..8f4af64deb1b 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -8737,6 +8737,12 @@ F:	Documentation/devicetree/bindings/power/power?domain*
>  F:	drivers/base/power/domain*.c
>  F:	include/linux/pm_domain.h
>  
> +GENERIC PWM SERVO DRIVER
> +M:	"Angelo Compagnucci" <angelo@amarulasolutions.com>
> +L:	linux-pwm@vger.kernel.org
> +S:	Maintained
> +F:	drivers/misc/servo-pwm.c
> +
>  GENERIC RESISTIVE TOUCHSCREEN ADC DRIVER
>  M:	Eugen Hristev <eugen.hristev@microchip.com>
>  L:	linux-input@vger.kernel.org
> diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
> index 9947b7892bd5..8a74087149ac 100644
> --- a/drivers/misc/Kconfig
> +++ b/drivers/misc/Kconfig
> @@ -518,6 +518,17 @@ config VCPU_STALL_DETECTOR
>  
>  	  If you do not intend to run this kernel as a guest, say N.
>  
> +config SERVO_PWM
> +	tristate "Servo motor positioning"
> +	depends on PWM
> +	help
> +	  Driver to control generic servo motor positioning.
> +	  Writing to the "angle" device attribute, the motor will move to
> +	  the angle position.
> +
> +	  To compile this driver as a module, choose M here: the module
> +	  will be called servo-pwm.
> +
>  source "drivers/misc/c2port/Kconfig"
>  source "drivers/misc/eeprom/Kconfig"
>  source "drivers/misc/cb710/Kconfig"
> diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
> index 87b54a4a4422..936629b648a9 100644
> --- a/drivers/misc/Makefile
> +++ b/drivers/misc/Makefile
> @@ -64,3 +64,4 @@ obj-$(CONFIG_HI6421V600_IRQ)	+= hi6421v600-irq.o
>  obj-$(CONFIG_OPEN_DICE)		+= open-dice.o
>  obj-$(CONFIG_GP_PCI1XXXX)	+= mchp_pci1xxxx/
>  obj-$(CONFIG_VCPU_STALL_DETECTOR)	+= vcpu_stall_detector.o
> +obj-$(CONFIG_SERVO_PWM)	+= servo-pwm.o
> diff --git a/drivers/misc/servo-pwm.c b/drivers/misc/servo-pwm.c
> new file mode 100644
> index 000000000000..1303ddda8d07
> --- /dev/null
> +++ b/drivers/misc/servo-pwm.c
> @@ -0,0 +1,149 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2023 Angelo Compagnucci <angelo@amarulasolutions.com>
> + * servo-pwm.c - driver for controlling servo motors via pwm.
> + */
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/platform_device.h>
> +#include <linux/err.h>
> +#include <linux/pwm.h>
> +
> +#define DEFAULT_DUTY_MIN	500000
> +#define DEFAULT_DUTY_MAX	2500000
> +#define DEFAULT_DEGREES		175
> +#define DEFAULT_ANGLE		0
> +
> +struct servo_pwm_data {
> +	u32 duty_min;
> +	u32 duty_max;
> +	u32 degrees;
> +	u32 angle;
> +
> +	struct mutex lock;
> +	struct pwm_device *pwm;
> +	struct pwm_state pwmstate;
> +};
> +
> +static int servo_pwm_set(struct servo_pwm_data *data, int val)
> +{
> +	u64 new_duty = (((data->duty_max - data->duty_min) /
> +			data->degrees) * val) + data->duty_min;

This one formula is basically the only thing that this driver adds. The
remaining 150+ lines are essentially boilerplate to expose the "angle"
property via sysfs.

We can already do everything that this driver does via the PWM sysfs, so
I wonder if we really need this.

Also, how are other aspects of the motor (such as velocity) controlled?
Wouldn't you want to expose these other controls as well?

Thierry

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM
  2023-02-20 11:40 ` Thierry Reding
@ 2023-02-20 11:46   ` Angelo Compagnucci
  0 siblings, 0 replies; 10+ messages in thread
From: Angelo Compagnucci @ 2023-02-20 11:46 UTC (permalink / raw)
  To: Thierry Reding
  Cc: Angelo Compagnucci, Derek Kiernan, Dragan Cvetic, Arnd Bergmann,
	Greg Kroah-Hartman, Uwe Kleine-König, open list,
	open list:GENERIC PWM SERVO DRIVER

On Mon, Feb 20, 2023 at 12:40 PM Thierry Reding
<thierry.reding@gmail.com> wrote:
>
> On Fri, Feb 17, 2023 at 05:10:35PM +0100, Angelo Compagnucci wrote:
> > This patch adds a simple driver to control servo motor position via
> > PWM signal.
> > The driver allows to set the angle from userspace, while min/max
> > positions duty cycle and the motor degrees aperture are defined in
> > the dts.
> >
> > Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
> > ---
> > v2:
> > * Driver mostly rewritten for kernel 6.2
> > v3:
> > * Fixed sysfs_emit (greg k-h)
> >
> >  MAINTAINERS              |   6 ++
> >  drivers/misc/Kconfig     |  11 +++
> >  drivers/misc/Makefile    |   1 +
> >  drivers/misc/servo-pwm.c | 149 +++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 167 insertions(+)
> >  create mode 100644 drivers/misc/servo-pwm.c
> >
> > diff --git a/MAINTAINERS b/MAINTAINERS
> > index 39ff1a717625..8f4af64deb1b 100644
> > --- a/MAINTAINERS
> > +++ b/MAINTAINERS
> > @@ -8737,6 +8737,12 @@ F:     Documentation/devicetree/bindings/power/power?domain*
> >  F:   drivers/base/power/domain*.c
> >  F:   include/linux/pm_domain.h
> >
> > +GENERIC PWM SERVO DRIVER
> > +M:   "Angelo Compagnucci" <angelo@amarulasolutions.com>
> > +L:   linux-pwm@vger.kernel.org
> > +S:   Maintained
> > +F:   drivers/misc/servo-pwm.c
> > +
> >  GENERIC RESISTIVE TOUCHSCREEN ADC DRIVER
> >  M:   Eugen Hristev <eugen.hristev@microchip.com>
> >  L:   linux-input@vger.kernel.org
> > diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
> > index 9947b7892bd5..8a74087149ac 100644
> > --- a/drivers/misc/Kconfig
> > +++ b/drivers/misc/Kconfig
> > @@ -518,6 +518,17 @@ config VCPU_STALL_DETECTOR
> >
> >         If you do not intend to run this kernel as a guest, say N.
> >
> > +config SERVO_PWM
> > +     tristate "Servo motor positioning"
> > +     depends on PWM
> > +     help
> > +       Driver to control generic servo motor positioning.
> > +       Writing to the "angle" device attribute, the motor will move to
> > +       the angle position.
> > +
> > +       To compile this driver as a module, choose M here: the module
> > +       will be called servo-pwm.
> > +
> >  source "drivers/misc/c2port/Kconfig"
> >  source "drivers/misc/eeprom/Kconfig"
> >  source "drivers/misc/cb710/Kconfig"
> > diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
> > index 87b54a4a4422..936629b648a9 100644
> > --- a/drivers/misc/Makefile
> > +++ b/drivers/misc/Makefile
> > @@ -64,3 +64,4 @@ obj-$(CONFIG_HI6421V600_IRQ)        += hi6421v600-irq.o
> >  obj-$(CONFIG_OPEN_DICE)              += open-dice.o
> >  obj-$(CONFIG_GP_PCI1XXXX)    += mchp_pci1xxxx/
> >  obj-$(CONFIG_VCPU_STALL_DETECTOR)    += vcpu_stall_detector.o
> > +obj-$(CONFIG_SERVO_PWM)      += servo-pwm.o
> > diff --git a/drivers/misc/servo-pwm.c b/drivers/misc/servo-pwm.c
> > new file mode 100644
> > index 000000000000..1303ddda8d07
> > --- /dev/null
> > +++ b/drivers/misc/servo-pwm.c
> > @@ -0,0 +1,149 @@
> > +// SPDX-License-Identifier: GPL-2.0-or-later
> > +/*
> > + * Copyright (c) 2023 Angelo Compagnucci <angelo@amarulasolutions.com>
> > + * servo-pwm.c - driver for controlling servo motors via pwm.
> > + */
> > +
> > +#include <linux/module.h>
> > +#include <linux/kernel.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/err.h>
> > +#include <linux/pwm.h>
> > +
> > +#define DEFAULT_DUTY_MIN     500000
> > +#define DEFAULT_DUTY_MAX     2500000
> > +#define DEFAULT_DEGREES              175
> > +#define DEFAULT_ANGLE                0
> > +
> > +struct servo_pwm_data {
> > +     u32 duty_min;
> > +     u32 duty_max;
> > +     u32 degrees;
> > +     u32 angle;
> > +
> > +     struct mutex lock;
> > +     struct pwm_device *pwm;
> > +     struct pwm_state pwmstate;
> > +};
> > +
> > +static int servo_pwm_set(struct servo_pwm_data *data, int val)
> > +{
> > +     u64 new_duty = (((data->duty_max - data->duty_min) /
> > +                     data->degrees) * val) + data->duty_min;
>
> This one formula is basically the only thing that this driver adds. The
> remaining 150+ lines are essentially boilerplate to expose the "angle"
> property via sysfs.
>
> We can already do everything that this driver does via the PWM sysfs, so
> I wonder if we really need this.

That's true, but anyway it's a big improvement over writing each one
of the servo parameters inside the sysfs one by one.
Moreover, it makes easier to handle a product based on several servo motors.

> Also, how are other aspects of the motor (such as velocity) controlled?
> Wouldn't you want to expose these other controls as well?

As far as I can tell, a basic servo motor only offers a way to change
the angle through PWM duty cycle.
The speed is controlled by the driver inside the motor: the bigger is
the angle, the faster it moves.

There are more complex servos out there, but they don't use plain
simple PWM like those, they usually use some sort of more complex
digital protocol on busses like I2C, SPI, DMX.

>
> Thierry



-- 

Angelo Compagnucci

Software Engineer

angelo@amarulasolutions.com
__________________________________
Amarula Solutions SRL

Via le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 (0)42 243 5310
info@amarulasolutions.com

www.amarulasolutions.com

[`as] https://www.amarulasolutions.com|

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM
  2023-02-17 16:10 [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM Angelo Compagnucci
                   ` (4 preceding siblings ...)
  2023-02-20 11:40 ` Thierry Reding
@ 2023-07-15 19:17 ` Uwe Kleine-König
  5 siblings, 0 replies; 10+ messages in thread
From: Uwe Kleine-König @ 2023-07-15 19:17 UTC (permalink / raw)
  To: Angelo Compagnucci
  Cc: Angelo Compagnucci, Derek Kiernan, Dragan Cvetic, Arnd Bergmann,
	Greg Kroah-Hartman, Thierry Reding, open list,
	open list:GENERIC PWM SERVO DRIVER

[-- Attachment #1: Type: text/plain, Size: 4988 bytes --]

On Fri, Feb 17, 2023 at 05:10:35PM +0100, Angelo Compagnucci wrote:
> This patch adds a simple driver to control servo motor position via
> PWM signal.
> The driver allows to set the angle from userspace, while min/max
> positions duty cycle and the motor degrees aperture are defined in
> the dts.
> 
> Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
> ---
> v2:
> * Driver mostly rewritten for kernel 6.2
> v3:
> * Fixed sysfs_emit (greg k-h)
> 
>  MAINTAINERS              |   6 ++
>  drivers/misc/Kconfig     |  11 +++
>  drivers/misc/Makefile    |   1 +
>  drivers/misc/servo-pwm.c | 149 +++++++++++++++++++++++++++++++++++++++
>  4 files changed, 167 insertions(+)
>  create mode 100644 drivers/misc/servo-pwm.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 39ff1a717625..8f4af64deb1b 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -8737,6 +8737,12 @@ F:	Documentation/devicetree/bindings/power/power?domain*
>  F:	drivers/base/power/domain*.c
>  F:	include/linux/pm_domain.h
>  
> +GENERIC PWM SERVO DRIVER
> +M:	"Angelo Compagnucci" <angelo@amarulasolutions.com>
> +L:	linux-pwm@vger.kernel.org
> +S:	Maintained
> +F:	drivers/misc/servo-pwm.c
> +
>  GENERIC RESISTIVE TOUCHSCREEN ADC DRIVER
>  M:	Eugen Hristev <eugen.hristev@microchip.com>
>  L:	linux-input@vger.kernel.org
> diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
> index 9947b7892bd5..8a74087149ac 100644
> --- a/drivers/misc/Kconfig
> +++ b/drivers/misc/Kconfig
> @@ -518,6 +518,17 @@ config VCPU_STALL_DETECTOR
>  
>  	  If you do not intend to run this kernel as a guest, say N.
>  
> +config SERVO_PWM
> +	tristate "Servo motor positioning"
> +	depends on PWM
> +	help
> +	  Driver to control generic servo motor positioning.
> +	  Writing to the "angle" device attribute, the motor will move to
> +	  the angle position.
> +
> +	  To compile this driver as a module, choose M here: the module
> +	  will be called servo-pwm.
> +
>  source "drivers/misc/c2port/Kconfig"
>  source "drivers/misc/eeprom/Kconfig"
>  source "drivers/misc/cb710/Kconfig"
> diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
> index 87b54a4a4422..936629b648a9 100644
> --- a/drivers/misc/Makefile
> +++ b/drivers/misc/Makefile
> @@ -64,3 +64,4 @@ obj-$(CONFIG_HI6421V600_IRQ)	+= hi6421v600-irq.o
>  obj-$(CONFIG_OPEN_DICE)		+= open-dice.o
>  obj-$(CONFIG_GP_PCI1XXXX)	+= mchp_pci1xxxx/
>  obj-$(CONFIG_VCPU_STALL_DETECTOR)	+= vcpu_stall_detector.o
> +obj-$(CONFIG_SERVO_PWM)	+= servo-pwm.o
> diff --git a/drivers/misc/servo-pwm.c b/drivers/misc/servo-pwm.c
> new file mode 100644
> index 000000000000..1303ddda8d07
> --- /dev/null
> +++ b/drivers/misc/servo-pwm.c
> @@ -0,0 +1,149 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2023 Angelo Compagnucci <angelo@amarulasolutions.com>
> + * servo-pwm.c - driver for controlling servo motors via pwm.
> + */
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/platform_device.h>
> +#include <linux/err.h>
> +#include <linux/pwm.h>
> +
> +#define DEFAULT_DUTY_MIN	500000
> +#define DEFAULT_DUTY_MAX	2500000
> +#define DEFAULT_DEGREES		175
> +#define DEFAULT_ANGLE		0
> +
> +struct servo_pwm_data {
> +	u32 duty_min;
> +	u32 duty_max;
> +	u32 degrees;
> +	u32 angle;
> +
> +	struct mutex lock;
> +	struct pwm_device *pwm;
> +	struct pwm_state pwmstate;
> +};
> +
> +static int servo_pwm_set(struct servo_pwm_data *data, int val)
> +{
> +	u64 new_duty = (((data->duty_max - data->duty_min) /
> +			data->degrees) * val) + data->duty_min;

You're loosing precision here. Always divide as late as possible. (If
you need an example: With

	duty_max = 1000
	duty_min = 0
	degrees = 251
	val = 79

the exact result for new_duty would be 314.7410358565737. Your term
yields 237. If you divide after multiplying with val you get 314.

All in all I think this driver is too specialized on a single motor
type. IMHO what we would be more helpful is a generic framework that can
abstract various different motors with the same API. 

A while back I already thought about a suitable driver API. The
abstraction I came up with back then was:

	.setspeed(struct motor_device *motor, signed int speed)
	.disable(struct motor_device *motor)

In the end this is probably too simple because it doesn't allow the
consumer to specify how fast the new target speed is to be reached.
(Consider the motor running at 1000 and .setspeed(mymotor, 0) is called.
Should this mean "actively brake", or "stop driving and just coast
down"? I don't have a good idea yet that is both simple enough and still
expressive that it's both useful to consumers and sensible to implement
for different motor types.

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-07-15 19:18 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-17 16:10 [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM Angelo Compagnucci
2023-02-17 16:10 ` [PATCH v3 2/3] misc: servo-pwm: Add sysfs entries to control motor angle Angelo Compagnucci
2023-02-17 16:10 ` [PATCH v3 3/3] dt-bindings: misc: servo-pwm: Add new bindings for servo-pwm Angelo Compagnucci
2023-02-17 17:29   ` Rob Herring
2023-02-18 10:16   ` Krzysztof Kozlowski
2023-02-17 21:12 ` [PATCH v3 1/3] misc: servo-pwm: driver for controlling servo motors via PWM kernel test robot
2023-02-17 21:22 ` kernel test robot
2023-02-20 11:40 ` Thierry Reding
2023-02-20 11:46   ` Angelo Compagnucci
2023-07-15 19:17 ` Uwe Kleine-König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).