Merge tag 'arm-dt-5.14' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-2.6-microblaze.git] / drivers / clk / clk-si5341.c
index e0446e6..57ae183 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/i2c.h>
 #include <linux/module.h>
 #include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <asm/unaligned.h>
 
@@ -59,6 +60,7 @@ struct clk_si5341_synth {
 struct clk_si5341_output {
        struct clk_hw hw;
        struct clk_si5341 *data;
+       struct regulator *vddo_reg;
        u8 index;
 };
 #define to_clk_si5341_output(_hw) \
@@ -78,12 +80,15 @@ struct clk_si5341 {
        u8 num_outputs;
        u8 num_synth;
        u16 chip_id;
+       bool xaxb_ext_clk;
+       bool iovdd_33;
 };
 #define to_clk_si5341(_hw)     container_of(_hw, struct clk_si5341, hw)
 
 struct clk_si5341_output_config {
        u8 out_format_drv_bits;
        u8 out_cm_ampl_bits;
+       u8 vdd_sel_bits;
        bool synth_master;
        bool always_on;
 };
@@ -92,12 +97,23 @@ struct clk_si5341_output_config {
 #define SI5341_PN_BASE         0x0002
 #define SI5341_DEVICE_REV      0x0005
 #define SI5341_STATUS          0x000C
+#define SI5341_LOS             0x000D
+#define SI5341_STATUS_STICKY   0x0011
+#define SI5341_LOS_STICKY      0x0012
 #define SI5341_SOFT_RST                0x001C
 #define SI5341_IN_SEL          0x0021
+#define SI5341_DEVICE_READY    0x00FE
 #define SI5341_XAXB_CFG                0x090E
+#define SI5341_IO_VDD_SEL      0x0943
 #define SI5341_IN_EN           0x0949
 #define SI5341_INX_TO_PFD_EN   0x094A
 
+/* Status bits */
+#define SI5341_STATUS_SYSINCAL BIT(0)
+#define SI5341_STATUS_LOSXAXB  BIT(1)
+#define SI5341_STATUS_LOSREF   BIT(2)
+#define SI5341_STATUS_LOL      BIT(3)
+
 /* Input selection */
 #define SI5341_IN_SEL_MASK     0x06
 #define SI5341_IN_SEL_SHIFT    1
@@ -126,6 +142,8 @@ struct clk_si5341_output_config {
 #define SI5341_OUT_R_REG(output)       \
                        ((output)->data->reg_rdiv_offset[(output)->index])
 
+#define SI5341_OUT_MUX_VDD_SEL_MASK 0x38
+
 /* Synthesize N divider */
 #define SI5341_SYNTH_N_NUM(x)  (0x0302 + ((x) * 11))
 #define SI5341_SYNTH_N_DEN(x)  (0x0308 + ((x) * 11))
@@ -335,11 +353,12 @@ static const struct si5341_reg_default si5341_reg_defaults[] = {
        { 0x0804, 0x00 }, /* Not in datasheet */
        { 0x090E, 0x02 }, /* XAXB_EXTCLK_EN=0 XAXB_PDNB=1 (use XTAL) */
        { 0x091C, 0x04 }, /* ZDM_EN=4 (Normal mode) */
-       { 0x0943, 0x00 }, /* IO_VDD_SEL=0 (0=1v8, use 1=3v3) */
        { 0x0949, 0x00 }, /* IN_EN (disable input clocks) */
        { 0x094A, 0x00 }, /* INx_TO_PFD_EN (disabled) */
        { 0x0A02, 0x00 }, /* Not in datasheet */
        { 0x0B44, 0x0F }, /* PDIV_ENB (datasheet does not mention what it is) */
+       { 0x0B57, 0x10 }, /* VCO_RESET_CALCODE (not described in datasheet) */
+       { 0x0B58, 0x05 }, /* VCO_RESET_CALCODE (not described in datasheet) */
 };
 
 /* Read and interpret a 44-bit followed by a 32-bit value in the regmap */
@@ -512,9 +531,11 @@ static int si5341_clk_reparent(struct clk_si5341 *data, u8 index)
                if (err < 0)
                        return err;
 
-               /* Power up XTAL oscillator and buffer */
+               /* Power up XTAL oscillator and buffer, select clock mode */
                err = regmap_update_bits(data->regmap, SI5341_XAXB_CFG,
-                               SI5341_XAXB_CFG_PDNB, SI5341_XAXB_CFG_PDNB);
+                               SI5341_XAXB_CFG_PDNB | SI5341_XAXB_CFG_EXTCLK_EN,
+                               SI5341_XAXB_CFG_PDNB | (data->xaxb_ext_clk ?
+                                       SI5341_XAXB_CFG_EXTCLK_EN : 0));
                if (err < 0)
                        return err;
        }
@@ -623,6 +644,9 @@ static unsigned long si5341_synth_clk_recalc_rate(struct clk_hw *hw,
                        SI5341_SYNTH_N_NUM(synth->index), &n_num, &n_den);
        if (err < 0)
                return err;
+       /* Check for bogus/uninitialized settings */
+       if (!n_num || !n_den)
+               return 0;
 
        /*
         * n_num and n_den are shifted left as much as possible, so to prevent
@@ -806,6 +830,9 @@ static long si5341_output_clk_round_rate(struct clk_hw *hw, unsigned long rate,
 {
        unsigned long r;
 
+       if (!rate)
+               return 0;
+
        r = *parent_rate >> 1;
 
        /* If rate is an even divisor, no changes to parent required */
@@ -834,11 +861,16 @@ static int si5341_output_clk_set_rate(struct clk_hw *hw, unsigned long rate,
                unsigned long parent_rate)
 {
        struct clk_si5341_output *output = to_clk_si5341_output(hw);
-       /* Frequency divider is (r_div + 1) * 2 */
-       u32 r_div = (parent_rate / rate) >> 1;
+       u32 r_div;
        int err;
        u8 r[3];
 
+       if (!rate)
+               return -EINVAL;
+
+       /* Frequency divider is (r_div + 1) * 2 */
+       r_div = (parent_rate / rate) >> 1;
+
        if (r_div <= 1)
                r_div = 0;
        else if (r_div >= BIT(24))
@@ -1083,7 +1115,7 @@ static const struct si5341_reg_default si5341_preamble[] = {
        { 0x0B25, 0x00 },
        { 0x0502, 0x01 },
        { 0x0505, 0x03 },
-       { 0x0957, 0x1F },
+       { 0x0957, 0x17 },
        { 0x0B4E, 0x1A },
 };
 
@@ -1129,6 +1161,11 @@ static int si5341_finalize_defaults(struct clk_si5341 *data)
        int res;
        u32 revision;
 
+       res = regmap_write(data->regmap, SI5341_IO_VDD_SEL,
+                          data->iovdd_33 ? 1 : 0);
+       if (res < 0)
+               return res;
+
        res = regmap_read(data->regmap, SI5341_DEVICE_REV, &revision);
        if (res < 0)
                return res;
@@ -1189,6 +1226,32 @@ static const struct regmap_range_cfg si5341_regmap_ranges[] = {
        },
 };
 
+static int si5341_wait_device_ready(struct i2c_client *client)
+{
+       int count;
+
+       /* Datasheet warns: Any attempt to read or write any register other
+        * than DEVICE_READY before DEVICE_READY reads as 0x0F may corrupt the
+        * NVM programming and may corrupt the register contents, as they are
+        * read from NVM. Note that this includes accesses to the PAGE register.
+        * Also: DEVICE_READY is available on every register page, so no page
+        * change is needed to read it.
+        * Do this outside regmap to avoid automatic PAGE register access.
+        * May take up to 300ms to complete.
+        */
+       for (count = 0; count < 15; ++count) {
+               s32 result = i2c_smbus_read_byte_data(client,
+                                                     SI5341_DEVICE_READY);
+               if (result < 0)
+                       return result;
+               if (result == 0x0F)
+                       return 0;
+               msleep(20);
+       }
+       dev_err(&client->dev, "timeout waiting for DEVICE_READY\n");
+       return -EIO;
+}
+
 static const struct regmap_config si5341_regmap_config = {
        .reg_bits = 8,
        .val_bits = 8,
@@ -1199,11 +1262,11 @@ static const struct regmap_config si5341_regmap_config = {
        .volatile_table = &si5341_regmap_volatile,
 };
 
-static int si5341_dt_parse_dt(struct i2c_client *client,
-       struct clk_si5341_output_config *config)
+static int si5341_dt_parse_dt(struct clk_si5341 *data,
+                             struct clk_si5341_output_config *config)
 {
        struct device_node *child;
-       struct device_node *np = client->dev.of_node;
+       struct device_node *np = data->i2c_client->dev.of_node;
        u32 num;
        u32 val;
 
@@ -1212,13 +1275,13 @@ static int si5341_dt_parse_dt(struct i2c_client *client,
 
        for_each_child_of_node(np, child) {
                if (of_property_read_u32(child, "reg", &num)) {
-                       dev_err(&client->dev, "missing reg property of %s\n",
+                       dev_err(&data->i2c_client->dev, "missing reg property of %s\n",
                                child->name);
                        goto put_child;
                }
 
                if (num >= SI5341_MAX_NUM_OUTPUTS) {
-                       dev_err(&client->dev, "invalid clkout %d\n", num);
+                       dev_err(&data->i2c_client->dev, "invalid clkout %d\n", num);
                        goto put_child;
                }
 
@@ -1237,7 +1300,7 @@ static int si5341_dt_parse_dt(struct i2c_client *client,
                                config[num].out_format_drv_bits |= 0xc0;
                                break;
                        default:
-                               dev_err(&client->dev,
+                               dev_err(&data->i2c_client->dev,
                                        "invalid silabs,format %u for %u\n",
                                        val, num);
                                goto put_child;
@@ -1250,7 +1313,7 @@ static int si5341_dt_parse_dt(struct i2c_client *client,
 
                if (!of_property_read_u32(child, "silabs,common-mode", &val)) {
                        if (val > 0xf) {
-                               dev_err(&client->dev,
+                               dev_err(&data->i2c_client->dev,
                                        "invalid silabs,common-mode %u\n",
                                        val);
                                goto put_child;
@@ -1261,7 +1324,7 @@ static int si5341_dt_parse_dt(struct i2c_client *client,
 
                if (!of_property_read_u32(child, "silabs,amplitude", &val)) {
                        if (val > 0xf) {
-                               dev_err(&client->dev,
+                               dev_err(&data->i2c_client->dev,
                                        "invalid silabs,amplitude %u\n",
                                        val);
                                goto put_child;
@@ -1278,6 +1341,34 @@ static int si5341_dt_parse_dt(struct i2c_client *client,
 
                config[num].always_on =
                        of_property_read_bool(child, "always-on");
+
+               config[num].vdd_sel_bits = 0x08;
+               if (data->clk[num].vddo_reg) {
+                       int vdd = regulator_get_voltage(data->clk[num].vddo_reg);
+
+                       switch (vdd) {
+                       case 3300000:
+                               config[num].vdd_sel_bits |= 0 << 4;
+                               break;
+                       case 1800000:
+                               config[num].vdd_sel_bits |= 1 << 4;
+                               break;
+                       case 2500000:
+                               config[num].vdd_sel_bits |= 2 << 4;
+                               break;
+                       default:
+                               dev_err(&data->i2c_client->dev,
+                                       "unsupported vddo voltage %d for %s\n",
+                                       vdd, child->name);
+                               goto put_child;
+                       }
+               } else {
+                       /* chip seems to default to 2.5V when not set */
+                       dev_warn(&data->i2c_client->dev,
+                               "no regulator set, defaulting vdd_sel to 2.5V for %s\n",
+                               child->name);
+                       config[num].vdd_sel_bits |= 2 << 4;
+               }
        }
 
        return 0;
@@ -1366,6 +1457,94 @@ static int si5341_clk_select_active_input(struct clk_si5341 *data)
        return res;
 }
 
+static ssize_t input_present_show(struct device *dev,
+                                 struct device_attribute *attr,
+                                 char *buf)
+{
+       struct clk_si5341 *data = dev_get_drvdata(dev);
+       u32 status;
+       int res = regmap_read(data->regmap, SI5341_STATUS, &status);
+
+       if (res < 0)
+               return res;
+       res = !(status & SI5341_STATUS_LOSREF);
+       return snprintf(buf, PAGE_SIZE, "%d\n", res);
+}
+static DEVICE_ATTR_RO(input_present);
+
+static ssize_t input_present_sticky_show(struct device *dev,
+                                        struct device_attribute *attr,
+                                        char *buf)
+{
+       struct clk_si5341 *data = dev_get_drvdata(dev);
+       u32 status;
+       int res = regmap_read(data->regmap, SI5341_STATUS_STICKY, &status);
+
+       if (res < 0)
+               return res;
+       res = !(status & SI5341_STATUS_LOSREF);
+       return snprintf(buf, PAGE_SIZE, "%d\n", res);
+}
+static DEVICE_ATTR_RO(input_present_sticky);
+
+static ssize_t pll_locked_show(struct device *dev,
+                              struct device_attribute *attr,
+                              char *buf)
+{
+       struct clk_si5341 *data = dev_get_drvdata(dev);
+       u32 status;
+       int res = regmap_read(data->regmap, SI5341_STATUS, &status);
+
+       if (res < 0)
+               return res;
+       res = !(status & SI5341_STATUS_LOL);
+       return snprintf(buf, PAGE_SIZE, "%d\n", res);
+}
+static DEVICE_ATTR_RO(pll_locked);
+
+static ssize_t pll_locked_sticky_show(struct device *dev,
+                                     struct device_attribute *attr,
+                                     char *buf)
+{
+       struct clk_si5341 *data = dev_get_drvdata(dev);
+       u32 status;
+       int res = regmap_read(data->regmap, SI5341_STATUS_STICKY, &status);
+
+       if (res < 0)
+               return res;
+       res = !(status & SI5341_STATUS_LOL);
+       return snprintf(buf, PAGE_SIZE, "%d\n", res);
+}
+static DEVICE_ATTR_RO(pll_locked_sticky);
+
+static ssize_t clear_sticky_store(struct device *dev,
+                                 struct device_attribute *attr,
+                                 const char *buf, size_t count)
+{
+       struct clk_si5341 *data = dev_get_drvdata(dev);
+       long val;
+
+       if (kstrtol(buf, 10, &val))
+               return -EINVAL;
+       if (val) {
+               int res = regmap_write(data->regmap, SI5341_STATUS_STICKY, 0);
+
+               if (res < 0)
+                       return res;
+       }
+       return count;
+}
+static DEVICE_ATTR_WO(clear_sticky);
+
+static const struct attribute *si5341_attributes[] = {
+       &dev_attr_input_present.attr,
+       &dev_attr_input_present_sticky.attr,
+       &dev_attr_pll_locked.attr,
+       &dev_attr_pll_locked_sticky.attr,
+       &dev_attr_clear_sticky.attr,
+       NULL
+};
+
 static int si5341_probe(struct i2c_client *client,
                const struct i2c_device_id *id)
 {
@@ -1378,6 +1557,7 @@ static int si5341_probe(struct i2c_client *client,
        unsigned int i;
        struct clk_si5341_output_config config[SI5341_MAX_NUM_OUTPUTS];
        bool initialization_required;
+       u32 status;
 
        data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
        if (!data)
@@ -1385,6 +1565,11 @@ static int si5341_probe(struct i2c_client *client,
 
        data->i2c_client = client;
 
+       /* Must be done before otherwise touching hardware */
+       err = si5341_wait_device_ready(client);
+       if (err)
+               return err;
+
        for (i = 0; i < SI5341_NUM_INPUTS; ++i) {
                input = devm_clk_get(&client->dev, si5341_input_clock_names[i]);
                if (IS_ERR(input)) {
@@ -1397,9 +1582,33 @@ static int si5341_probe(struct i2c_client *client,
                }
        }
 
-       err = si5341_dt_parse_dt(client, config);
+       for (i = 0; i < SI5341_MAX_NUM_OUTPUTS; ++i) {
+               char reg_name[10];
+
+               snprintf(reg_name, sizeof(reg_name), "vddo%d", i);
+               data->clk[i].vddo_reg = devm_regulator_get_optional(
+                       &client->dev, reg_name);
+               if (IS_ERR(data->clk[i].vddo_reg)) {
+                       err = PTR_ERR(data->clk[i].vddo_reg);
+                       data->clk[i].vddo_reg = NULL;
+                       if (err == -ENODEV)
+                               continue;
+                       goto cleanup;
+               } else {
+                       err = regulator_enable(data->clk[i].vddo_reg);
+                       if (err) {
+                               dev_err(&client->dev,
+                                       "failed to enable %s regulator: %d\n",
+                                       reg_name, err);
+                               data->clk[i].vddo_reg = NULL;
+                               goto cleanup;
+                       }
+               }
+       }
+
+       err = si5341_dt_parse_dt(data, config);
        if (err)
-               return err;
+               goto cleanup;
 
        if (of_property_read_string(client->dev.of_node, "clock-output-names",
                        &init.name))
@@ -1407,34 +1616,40 @@ static int si5341_probe(struct i2c_client *client,
        root_clock_name = init.name;
 
        data->regmap = devm_regmap_init_i2c(client, &si5341_regmap_config);
-       if (IS_ERR(data->regmap))
-               return PTR_ERR(data->regmap);
+       if (IS_ERR(data->regmap)) {
+               err = PTR_ERR(data->regmap);
+               goto cleanup;
+       }
 
        i2c_set_clientdata(client, data);
 
        err = si5341_probe_chip_id(data);
        if (err < 0)
-               return err;
+               goto cleanup;
 
        if (of_property_read_bool(client->dev.of_node, "silabs,reprogram")) {
                initialization_required = true;
        } else {
                err = si5341_is_programmed_already(data);
                if (err < 0)
-                       return err;
+                       goto cleanup;
 
                initialization_required = !err;
        }
+       data->xaxb_ext_clk = of_property_read_bool(client->dev.of_node,
+                                                  "silabs,xaxb-ext-clk");
+       data->iovdd_33 = of_property_read_bool(client->dev.of_node,
+                                              "silabs,iovdd-33");
 
        if (initialization_required) {
                /* Populate the regmap cache in preparation for "cache only" */
                err = si5341_read_settings(data);
                if (err < 0)
-                       return err;
+                       goto cleanup;
 
                err = si5341_send_preamble(data);
                if (err < 0)
-                       return err;
+                       goto cleanup;
 
                /*
                 * We intend to send all 'final' register values in a single
@@ -1447,19 +1662,19 @@ static int si5341_probe(struct i2c_client *client,
                err = si5341_write_multiple(data, si5341_reg_defaults,
                                        ARRAY_SIZE(si5341_reg_defaults));
                if (err < 0)
-                       return err;
+                       goto cleanup;
        }
 
        /* Input must be up and running at this point */
        err = si5341_clk_select_active_input(data);
        if (err < 0)
-               return err;
+               goto cleanup;
 
        if (initialization_required) {
                /* PLL configuration is required */
                err = si5341_initialize_pll(data);
                if (err < 0)
-                       return err;
+                       goto cleanup;
        }
 
        /* Register the PLL */
@@ -1472,7 +1687,7 @@ static int si5341_probe(struct i2c_client *client,
        err = devm_clk_hw_register(&client->dev, &data->hw);
        if (err) {
                dev_err(&client->dev, "clock registration failed\n");
-               return err;
+               goto cleanup;
        }
 
        init.num_parents = 1;
@@ -1509,13 +1724,17 @@ static int si5341_probe(struct i2c_client *client,
                        regmap_write(data->regmap,
                                SI5341_OUT_CM(&data->clk[i]),
                                config[i].out_cm_ampl_bits);
+                       regmap_update_bits(data->regmap,
+                               SI5341_OUT_MUX_SEL(&data->clk[i]),
+                               SI5341_OUT_MUX_VDD_SEL_MASK,
+                               config[i].vdd_sel_bits);
                }
                err = devm_clk_hw_register(&client->dev, &data->clk[i].hw);
                kfree(init.name); /* clock framework made a copy of the name */
                if (err) {
                        dev_err(&client->dev,
                                "output %u registration failed\n", i);
-                       return err;
+                       goto cleanup;
                }
                if (config[i].always_on)
                        clk_prepare(data->clk[i].hw.clk);
@@ -1525,7 +1744,7 @@ static int si5341_probe(struct i2c_client *client,
                        data);
        if (err) {
                dev_err(&client->dev, "unable to add clk provider\n");
-               return err;
+               goto cleanup;
        }
 
        if (initialization_required) {
@@ -1533,11 +1752,33 @@ static int si5341_probe(struct i2c_client *client,
                regcache_cache_only(data->regmap, false);
                err = regcache_sync(data->regmap);
                if (err < 0)
-                       return err;
+                       goto cleanup;
 
                err = si5341_finalize_defaults(data);
                if (err < 0)
-                       return err;
+                       goto cleanup;
+       }
+
+       /* wait for device to report input clock present and PLL lock */
+       err = regmap_read_poll_timeout(data->regmap, SI5341_STATUS, status,
+               !(status & (SI5341_STATUS_LOSREF | SI5341_STATUS_LOL)),
+              10000, 250000);
+       if (err) {
+               dev_err(&client->dev, "Error waiting for input clock or PLL lock\n");
+               goto cleanup;
+       }
+
+       /* clear sticky alarm bits from initialization */
+       err = regmap_write(data->regmap, SI5341_STATUS_STICKY, 0);
+       if (err) {
+               dev_err(&client->dev, "unable to clear sticky status\n");
+               goto cleanup;
+       }
+
+       err = sysfs_create_files(&client->dev.kobj, si5341_attributes);
+       if (err) {
+               dev_err(&client->dev, "unable to create sysfs files\n");
+               goto cleanup;
        }
 
        /* Free the names, clk framework makes copies */
@@ -1545,6 +1786,28 @@ static int si5341_probe(struct i2c_client *client,
                 devm_kfree(&client->dev, (void *)synth_clock_names[i]);
 
        return 0;
+
+cleanup:
+       for (i = 0; i < SI5341_MAX_NUM_OUTPUTS; ++i) {
+               if (data->clk[i].vddo_reg)
+                       regulator_disable(data->clk[i].vddo_reg);
+       }
+       return err;
+}
+
+static int si5341_remove(struct i2c_client *client)
+{
+       struct clk_si5341 *data = i2c_get_clientdata(client);
+       int i;
+
+       sysfs_remove_files(&client->dev.kobj, si5341_attributes);
+
+       for (i = 0; i < SI5341_MAX_NUM_OUTPUTS; ++i) {
+               if (data->clk[i].vddo_reg)
+                       regulator_disable(data->clk[i].vddo_reg);
+       }
+
+       return 0;
 }
 
 static const struct i2c_device_id si5341_id[] = {
@@ -1573,6 +1836,7 @@ static struct i2c_driver si5341_driver = {
                .of_match_table = clk_si5341_of_match,
        },
        .probe          = si5341_probe,
+       .remove         = si5341_remove,
        .id_table       = si5341_id,
 };
 module_i2c_driver(si5341_driver);