Skip to contentSkip to conversation

clk: Add driver for MAX9485

PATCH Applied Daniel Mack 2 participants 9 messages Jun 17, 2018 – Jul 6, 2018

Subsystems (2)

v4 of 5 Applied Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover clk: Add driver for MAX9485

This is v4 of the driver for Maxim's MAX9485 programmable audio clock generator.

v3 → v4:
* Rebased to 4.18-rc1

v2 → v3:

* Addressed a lot of issues pointed out by Stephen Boyd:
* Dropped local register caching
* Implement own OF clk_hw getter in favor of the onecell helper
* Implemented recalc_rate() properly by reading from the hardware
* Dropped input clock frequency validation
* Style issues

* Fixed a gpio line handling bug in resume
* Added Rob's Revied-by to 1/2

v1 → v2:

* Now served as two patches, one for the DT bits, and one for the actual implementation. The DT bits are put in the 1st patch because the implementation depends on the shared header file.

* Added support for suspend/resume

Daniel Mack (2):
dts: clk: add devicetree bindings for MAX9485
clk: Add driver for MAX9485

 .../bindings/clock/maxim,max9485.txt          |  59 +++
 drivers/clk/Kconfig                           |   6 +
 drivers/clk/Makefile                          |   1 +
 drivers/clk/clk-max9485.c                     | 402 ++++++++++++++++++
 include/dt-bindings/clock/maxim,max9485.h     |  18 +
 5 files changed, 486 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/maxim,max9485.txt
 create mode 100644 drivers/clk/clk-max9485.c
 create mode 100644 include/dt-bindings/clock/maxim,max9485.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH v4 1/2 dts: clk: add devicetree bindings for MAX9485

This patch adds the devicetree bindings for MAX9485, a programmable audio clock generator.

---
 .../bindings/clock/maxim,max9485.txt          | 59 +++++++++++++++++++
 include/dt-bindings/clock/maxim,max9485.h     | 18 ++++++
 2 files changed, 77 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/maxim,max9485.txt
 create mode 100644 include/dt-bindings/clock/maxim,max9485.h
Documentation/devicetree/bindings/clock/maxim,max9485.txt new file +59 -0
new file mode 100644
index 000000000000..61bec1100a94
@@ -0,0 +1,59 @@
+Devicetree bindings for Maxim MAX9485 Programmable Audio Clock Generator
+
+This device exposes 4 clocks in total:
+
+- MAX9485_MCLKOUT: 	A gated, buffered output of the input clock of 27 MHz
+- MAX9485_CLKOUT:	A PLL that can be configured to 16 different discrete
+			frequencies
+- MAX9485_CLKOUT[1,2]:	Two gated outputs for MAX9485_CLKOUT
+
+MAX9485_CLKOUT[1,2] are children of MAX9485_CLKOUT which upchain all rate set
+requests.
+
+Required properties:
+- compatible:	"maxim,max9485"
+- clocks:	Input clock, must provice 27.000 MHz
+- clock-names:	Must be set to "xclk"
+- #clock-cells: From common clock binding; shall be set to 1
+
+Optional properties:
+- reset-gpios:		GPIO descriptor connected to the #RESET input pin
+- vdd-supply:		A regulator node for Vdd
+- clock-output-names:	Name of output clocks, as defined in common clock
+			bindings
+
+If not explicitly set, the output names are "mclkout", "clkout", "clkout1"
+and "clkout2".
+
+Clocks are defined as preprocessor macros in the dt-binding header.
+
+Example:
+
+	#include <dt-bindings/clock/maxim,max9485.h>
+
+	xo-27mhz: xo-27mhz {
+		compatible = "fixed-clock";
+		#clock-cells = <0>;
+		clock-frequency = <27000000>;
+	};
+
+	&i2c0 {
+		max9485: audio-clock@63 {
+			reg = <0x63>;
+			compatible = "maxim,max9485";
+			clock-names = "xclk";
+			clocks = <&xo-27mhz>;
+			reset-gpios = <&gpio 1 GPIO_ACTIVE_HIGH>;
+			vdd-supply = <&3v3-reg>;
+			#clock-cells = <1>;
+		};
+	};
+
+	// Clock consumer node
+
+	foo@0 {
+		compatible = "bar,foo";
+		/* ... */
+		clock-names = "foo-input-clk";
+		clocks = <&max9485 MAX9485_CLKOUT1>;
+	};
include/dt-bindings/clock/maxim,max9485.h new file +18 -0
new file mode 100644
index 000000000000..185b09ce1869
@@ -0,0 +1,18 @@
+/*
+ * Copyright (C) 2018 Daniel Mack
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#ifndef __DT_BINDINGS_MAX9485_CLK_H
+#define __DT_BINDINGS_MAX9485_CLK_H
+
+#define MAX9485_MCLKOUT	0
+#define MAX9485_CLKOUT	1
+#define MAX9485_CLKOUT1	2
+#define MAX9485_CLKOUT2	3
+
+#endif /* __DT_BINDINGS_MAX9485_CLK_H */
Signature · 1 line
2.17.1
#3 lore raw

PATCH v4 2/2 clk: Add driver for MAX9485

In reply to Daniel Mack

This patch adds a driver for MAX9485, a programmable audio clock generator.

The device requires a 27.000 MHz clock input. It can provide a gated buffered output of its input clock and two gated outputs of a PLL that can generate one out of 16 discrete frequencies. There is only one PLL however, so the two gated outputs will always have the same frequency but they can be switched individually.

The driver for this device exposes 4 clocks in total:

- MAX9485_MCLKOUT: A gated, buffered output of the input clock
- MAX9485_CLKOUT: A PLL that can be configured to 16 different
discrete frequencies
- MAX9485_CLKOUT[1,2]: Two gated outputs for MAX9485_CLKOUT

Some PLL output frequencies can be achieved with different register settings. The driver will select the one with lowest jitter in such cases.

---
 drivers/clk/Kconfig       |   6 +
 drivers/clk/Makefile      |   1 +
 drivers/clk/clk-max9485.c | 402 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 409 insertions(+)
 create mode 100644 drivers/clk/clk-max9485.c
drivers/clk/Kconfig +6 -0
index 721572a8c429..ae5539a1d283 100644
@@ -45,6 +45,12 @@ config COMMON_CLK_MAX77686
 	  This driver supports Maxim 77620/77686/77802 crystal oscillator
 	  clock.
 
+config COMMON_CLK_MAX9485
+	tristate "Clock driver for Maxim 9485 Programmable Clock Generator"
+	depends on I2C
+	---help---
+	  This driver supports Maxim 9485 Programmable Audio Clock Generator
+
 config COMMON_CLK_RK808
 	tristate "Clock driver for RK805/RK808/RK818"
 	depends on MFD_RK808
drivers/clk/Makefile +1 -0
index ae40cbe770f0..24fc2b634362 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_COMMON_CLK_ASPEED)		+= clk-aspeed.o
 obj-$(CONFIG_ARCH_HIGHBANK)		+= clk-highbank.o
 obj-$(CONFIG_CLK_HSDK)			+= clk-hsdk-pll.o
 obj-$(CONFIG_COMMON_CLK_MAX77686)	+= clk-max77686.o
+obj-$(CONFIG_COMMON_CLK_MAX9485)	+= clk-max9485.o
 obj-$(CONFIG_ARCH_MOXART)		+= clk-moxart.o
 obj-$(CONFIG_ARCH_NOMADIK)		+= clk-nomadik.o
 obj-$(CONFIG_ARCH_NPCM7XX)	    	+= clk-npcm7xx.o
drivers/clk/clk-max9485.c new file +402 -0

402 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer
#4 lore raw

Quoting Daniel Mack (2018-06-17 05:05:35)

drivers/clk/Kconfig config COMMON_CLK_MAX77686 · line 50

Quoted 11 lines
drivers/clk/Kconfig +2 -0
index 721572a8c429..ae5539a1d283 100644
@@ -45,6 +45,12 @@ config COMMON_CLK_MAX77686
          This driver supports Maxim 77620/77686/77802 crystal oscillator
          clock.
 =
 
+config COMMON_CLK_MAX9485
+       tristate "Clock driver for Maxim 9485 Programmable Clock Generato=

r"

Drop the "Clock driver for" part please. These are all clock drivers.

drivers/clk/Kconfig config COMMON_CLK_MAX77686

Quoted 3 lines
drivers/clk/Kconfig +3 -0
+       depends on I2C
+       ---help---
+         This driver supports Maxim 9485 Programmable Audio Clock Genera=

tor

drivers/clk/Makefile obj-$(CONFIG_COMMON_CLK_ASPEED) +=3D clk-=

Quoted 9 lines
drivers/clk/Kconfig +1 -0
+
 config COMMON_CLK_RK808
        tristate "Clock driver for RK805/RK808/RK818"

depends on MFD_RK808

drivers/clk/Makefile +0 -0
index ae40cbe770f0..24fc2b634362 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_COMMON_CLK_ASPEED)               +=3D clk-=

aspeed.o

Quoted 14 lines
drivers/clk/Makefile +1 -0
 obj-$(CONFIG_ARCH_HIGHBANK)            +=3D clk-highbank.o
 obj-$(CONFIG_CLK_HSDK)                 +=3D clk-hsdk-pll.o
 obj-$(CONFIG_COMMON_CLK_MAX77686)      +=3D clk-max77686.o
+obj-$(CONFIG_COMMON_CLK_MAX9485)       +=3D clk-max9485.o
 obj-$(CONFIG_ARCH_MOXART)              +=3D clk-moxart.o
 obj-$(CONFIG_ARCH_NOMADIK)             +=3D clk-nomadik.o
 obj-$(CONFIG_ARCH_NPCM7XX)             +=3D clk-npcm7xx.o
drivers/clk/clk-max9485.c new file +1 -0
new file mode 100644
index 000000000000..4bae7deeda10
@@ -0,0 +1,402 @@
+// SPDX-License-Identifier: GPL-2.0

drivers/clk/clk-max9485.c line 11

Quoted 10 lines
drivers/clk/clk-max9485.c new file +10 -0
+/*
+ * clk-max9485.c: MAX9485 Programmable Audio Clock Generator
+ *
+ * (c) 2018 Daniel Mack <[email protected]>
+ *
+ * References:
+ *   MAX9485 Datasheet
+ *     http://www.maximintegrated.com/datasheet/index.mvp/id/4421
+ *
+ * This program is free software; you can redistribute  it and/or modify=

it

drivers/clk/clk-max9485.c line 12

Quoted 1 line
drivers/clk/clk-max9485.c new file +1 -0
+ * under  the terms of  the GNU General  Public License as published by =

the

drivers/clk/clk-max9485.c line 13

Quoted 1 line
drivers/clk/clk-max9485.c new file +1 -0
+ * Free Software Foundation;  either version 2 of the  License, or (at y=

our

drivers/clk/clk-max9485.c line 14

Quoted 1 line
drivers/clk/clk-max9485.c new file +1 -0
+ * option) any later version.

With SPDX tag this is suppose to be removed. Can you resend with this license blurb removed?

Quoted 41 lines
drivers/clk/clk-max9485.c new file +41 -0
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/regulator/consumer.h>
+
+#include <dt-bindings/clock/maxim,max9485.h>
+
+#define MAX9485_NUM_CLKS 4
+
+/* This chip has only one register of 8 bit width. */
+
+#define MAX9485_FS_12KHZ       (0 << 0)
+#define MAX9485_FS_32KHZ       (1 << 0)
+#define MAX9485_FS_44_1KHZ     (2 << 0)
+#define MAX9485_FS_48KHZ       (3 << 0)
+
+#define MAX9485_SCALE_256      (0 << 2)
+#define MAX9485_SCALE_384      (1 << 2)
+#define MAX9485_SCALE_768      (2 << 2)
+
+#define MAX9485_DOUBLE         BIT(4)
+#define MAX9485_CLKOUT1_ENABLE BIT(5)
+#define MAX9485_CLKOUT2_ENABLE BIT(6)
+#define MAX9485_MCLK_ENABLE    BIT(7)
+#define MAX9485_FREQ_MASK      0x1f
+
+struct max9485_rate {
+       unsigned long out;
+       u8 reg_value;
+};
+
+/*
+ * Ordered by frequency. For frequency the hardware can generate with
+ * multiple settings, the one with lowest jitter is listed first.

drivers/clk/clk-max9485.c

Quoted 10 lines
drivers/clk/clk-max9485.c new file +10 -0
+ */
+static const struct max9485_rate max9485_rates[] =3D {
+       {  3072000, MAX9485_FS_12KHZ   | MAX9485_SCALE_256 },
+       {  4608000, MAX9485_FS_12KHZ   | MAX9485_SCALE_384 },
+       {  8192000, MAX9485_FS_32KHZ   | MAX9485_SCALE_256 },
+       {  9126000, MAX9485_FS_12KHZ   | MAX9485_SCALE_768 },
+       { 11289600, MAX9485_FS_44_1KHZ | MAX9485_SCALE_256 },
+       { 12288000, MAX9485_FS_48KHZ   | MAX9485_SCALE_256 },
+       { 12288000, MAX9485_FS_32KHZ   | MAX9485_SCALE_384 },
+       { 16384000, MAX9485_FS_32KHZ   | MAX9485_SCALE_256 | MAX9485_DOUB=

LE },

drivers/clk/clk-max9485.c

Quoted 3 lines
drivers/clk/clk-max9485.c new file +3 -0
+       { 16934400, MAX9485_FS_44_1KHZ | MAX9485_SCALE_384 },
+       { 18384000, MAX9485_FS_48KHZ   | MAX9485_SCALE_384 },
+       { 22579200, MAX9485_FS_44_1KHZ | MAX9485_SCALE_256 | MAX9485_DOUB=

LE },

drivers/clk/clk-max9485.c line 69

Quoted 1 line
drivers/clk/clk-max9485.c new file +1 -0
+       { 24576000, MAX9485_FS_48KHZ   | MAX9485_SCALE_256 | MAX9485_DOUB=

LE },

drivers/clk/clk-max9485.c line 70

Quoted 1 line
drivers/clk/clk-max9485.c new file +1 -0
+       { 24576000, MAX9485_FS_32KHZ   | MAX9485_SCALE_384 | MAX9485_DOUB=

LE },

drivers/clk/clk-max9485.c line 72

Quoted 2 lines
drivers/clk/clk-max9485.c new file +2 -0
+       { 24576000, MAX9485_FS_32KHZ   | MAX9485_SCALE_768 },
+       { 33868800, MAX9485_FS_44_1KHZ | MAX9485_SCALE_384 | MAX9485_DOUB=

LE },

drivers/clk/clk-max9485.c line 74

Quoted 2 lines
drivers/clk/clk-max9485.c new file +2 -0
+       { 33868800, MAX9485_FS_44_1KHZ | MAX9485_SCALE_768 },
+       { 36864000, MAX9485_FS_48KHZ   | MAX9485_SCALE_384 | MAX9485_DOUB=

LE },

drivers/clk/clk-max9485.c line 76

Quoted 2 lines
drivers/clk/clk-max9485.c new file +2 -0
+       { 36864000, MAX9485_FS_48KHZ   | MAX9485_SCALE_768 },
+       { 49152000, MAX9485_FS_32KHZ   | MAX9485_SCALE_768 | MAX9485_DOUB=

LE },

drivers/clk/clk-max9485.c line 77

Quoted 1 line
drivers/clk/clk-max9485.c new file +1 -0
+       { 67737600, MAX9485_FS_44_1KHZ | MAX9485_SCALE_768 | MAX9485_DOUB=

LE },

drivers/clk/clk-max9485.c line 78

Quoted 1 line
drivers/clk/clk-max9485.c new file +1 -0
+       { 73728000, MAX9485_FS_48KHZ   | MAX9485_SCALE_768 | MAX9485_DOUB=

LE },

drivers/clk/clk-max9485.c line 79

Quoted 1 line
drivers/clk/clk-max9485.c new file +1 -0
+       { } /* sentinel */

Behold the sentinel!

Quoted 25 lines
drivers/clk/clk-max9485.c new file +25 -0
+};
+
+struct max9485_driver_data;
+
+struct max9485_clk_hw {
+       struct clk_hw hw;
+       struct clk_init_data init;
+       u8 enable_bit;
+       struct max9485_driver_data *drvdata;
+};
+
+struct max9485_driver_data {
+       struct clk *xclk;
+       struct i2c_client *client;
+       u8 reg_value;
+       struct regulator *supply;
+       struct gpio_desc *reset_gpio;
+       struct max9485_clk_hw hw[MAX9485_NUM_CLKS];
+};
+
+static inline struct max9485_clk_hw *to_max9485_clk(struct clk_hw *hw)
+{
+       return container_of(hw, struct max9485_clk_hw, hw);
+}
+

drivers/clk/clk-max9485.c line 114

Quoted 10 lines
drivers/clk/clk-max9485.c new file +10 -0
+static int max9485_update_bits(struct max9485_driver_data *drvdata,
+                              u8 mask, u8 value)
+{
+       int ret;
+
+       drvdata->reg_value &=3D ~mask;
+       drvdata->reg_value |=3D value;
+
+       dev_dbg(&drvdata->client->dev,
+               "updating mask %02x value %02x -> %02x\n",

May want to add 0x here so you know it's in hex?
=

Quoted 55 lines
drivers/clk/clk-max9485.c new file +55 -0
+               mask, value, drvdata->reg_value);
+
+       ret =3D i2c_master_send(drvdata->client,
+                             &drvdata->reg_value,
+                             sizeof(drvdata->reg_value));
+
+       return ret < 0 ? ret : 0;
+}
+
+static int max9485_clk_prepare(struct clk_hw *hw)
+{
+       struct max9485_clk_hw *clk_hw =3D to_max9485_clk(hw);
+
+       return max9485_update_bits(clk_hw->drvdata,
+                                  clk_hw->enable_bit,
+                                  clk_hw->enable_bit);
+}
+
+static void max9485_clk_unprepare(struct clk_hw *hw)
+{
+       struct max9485_clk_hw *clk_hw =3D to_max9485_clk(hw);
+
+       max9485_update_bits(clk_hw->drvdata, clk_hw->enable_bit, 0);
+}
+
+/*
+ * CLKOUT - configurable clock output
+ */
+static int max9485_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
+                                  unsigned long parent_rate)
+{
+       struct max9485_clk_hw *clk_hw =3D to_max9485_clk(hw);
+       const struct max9485_rate *entry;
+
+       for (entry =3D max9485_rates; entry->out !=3D 0; entry++)
+               if (entry->out =3D=3D rate)
+                       break;
+
+       if (entry->out =3D=3D 0)
+               return -EINVAL;
+
+       return max9485_update_bits(clk_hw->drvdata,
+                                  MAX9485_FREQ_MASK,
+                                  entry->reg_value);
+}
+
+static unsigned long max9485_clkout_recalc_rate(struct clk_hw *hw,
+                                               unsigned long parent_rate)
+{
+       struct max9485_clk_hw *clk_hw =3D to_max9485_clk(hw);
+       struct max9485_driver_data *drvdata =3D clk_hw->drvdata;
+       const struct max9485_rate *entry;
+       int ret;
+
+       ret =3D i2c_master_recv(drvdata->client,

drivers/clk/clk-max9485.c

Quoted 10 lines
drivers/clk/clk-max9485.c new file +10 -0
+                             &drvdata->reg_value,
+                             sizeof(drvdata->reg_value));
+       if (ret < 0) {
+               dev_warn(&drvdata->client->dev,
+                        "Unable to read register: %d\n", ret);
+               return 0;
+       }
+
+       for (entry =3D max9485_rates; entry->out !=3D 0; entry++)
+               if ((drvdata->reg_value & MAX9485_FREQ_MASK) =3D=3D entry=

->reg_value)

drivers/clk/clk-max9485.c line 180

Quoted 1 line
drivers/clk/clk-max9485.c new file +1 -0
+                       return entry->out;

Can you make a local variable, like u8 val, and then use that everywhere in this function and assign that to drvdata->reg_value after reading it? It will make the lines much shorter. I really don't like how this function is caching the reg_value at all BTW. It means we rely on recalc_rate() being called before things work, which is fragile. Preferably that reading and caching is done once in the probe of this driver and then never done again. Can you change to do that?

Quoted 3 lines
drivers/clk/clk-max9485.c new file +3 -0
+
+       return 0;
+}
#5 lore raw
In reply to Daniel Mack

Hi,

Quoted 2 lines · Daniel Mack

This is v4 of the driver for Maxim's MAX9485 programmable audio clock generator.

Is there a chance to get this merged for 4.19?

Thanks,
Daniel

Quoted 38 lines

v3 → v4:
* Rebased to 4.18-rc1

v2 → v3:

* Addressed a lot of issues pointed out by Stephen Boyd:
* Dropped local register caching
* Implement own OF clk_hw getter in favor of the onecell helper
* Implemented recalc_rate() properly by reading from the hardware
* Dropped input clock frequency validation
* Style issues

* Fixed a gpio line handling bug in resume
* Added Rob's Revied-by to 1/2

v1 → v2:

* Now served as two patches, one for the DT bits, and one for the actual implementation. The DT bits are put in the 1st patch because the implementation depends on the shared header file.

* Added support for suspend/resume

Daniel Mack (2):
dts: clk: add devicetree bindings for MAX9485
clk: Add driver for MAX9485

  .../bindings/clock/maxim,max9485.txt          |  59 +++
  drivers/clk/Kconfig                           |   6 +
  drivers/clk/Makefile                          |   1 +
  drivers/clk/clk-max9485.c                     | 402 ++++++++++++++++++
  include/dt-bindings/clock/maxim,max9485.h     |  18 +
  5 files changed, 486 insertions(+)

create mode 100644 Documentation/devicetree/bindings/clock/maxim,max9485.txt create mode 100644 drivers/clk/clk-max9485.c
create mode 100644 include/dt-bindings/clock/maxim,max9485.h

maintainer
#6 lore raw

Quoting Daniel Mack (2018-07-02 00:58:03)

Quoted 4 lines

Hi,
=

On Sunday, June 17, 2018 02:05 PM, Daniel Mack wrote:

Quoted 8 lines
Quoted 2 lines

This is v4 of the driver for Maxim's MAX9485 programmable audio clock generator.

=

Is there a chance to get this merged for 4.19?
=

=

Of course!

maintainer
#7 lore raw
In reply to Daniel Mack

Quoting Daniel Mack (2018-06-17 05:05:33)

Quoted 3 lines

This is v4 of the driver for Maxim's MAX9485 programmable audio clock generator.
=

Please resend the clk driver. I already applied the binding.

#8 lore raw
Quoted 7 lines · Stephen Boyd

Quoting Daniel Mack (2018-06-17 05:05:33)

Quoted 3 lines

This is v4 of the driver for Maxim's MAX9485 programmable audio clock generator.

Please resend the clk driver. I already applied the binding.

Thanks for the reviews! v5 is coming up in a minute :)

Daniel