root/drivers/regulator/sy8106a-regulator.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sy8106a_i2c_probe

   1 // SPDX-License-Identifier: GPL-2.0+
   2 //
   3 // sy8106a-regulator.c - Regulator device driver for SY8106A
   4 //
   5 // Copyright (C) 2016 Ondřej Jirman <megous@megous.com>
   6 // Copyright (c) 2017-2018 Icenowy Zheng <icenowy@aosc.io>
   7 
   8 #include <linux/err.h>
   9 #include <linux/i2c.h>
  10 #include <linux/module.h>
  11 #include <linux/regmap.h>
  12 #include <linux/regulator/driver.h>
  13 #include <linux/regulator/of_regulator.h>
  14 
  15 #define SY8106A_REG_VOUT1_SEL           0x01
  16 #define SY8106A_REG_VOUT_COM            0x02
  17 #define SY8106A_REG_VOUT1_SEL_MASK      0x7f
  18 #define SY8106A_DISABLE_REG             BIT(0)
  19 /*
  20  * The I2C controlled voltage will only work when this bit is set; otherwise
  21  * it will behave like a fixed regulator.
  22  */
  23 #define SY8106A_GO_BIT                  BIT(7)
  24 
  25 static const struct regmap_config sy8106a_regmap_config = {
  26         .reg_bits = 8,
  27         .val_bits = 8,
  28 };
  29 
  30 static const struct regulator_ops sy8106a_ops = {
  31         .set_voltage_sel = regulator_set_voltage_sel_regmap,
  32         .set_voltage_time_sel = regulator_set_voltage_time_sel,
  33         .get_voltage_sel = regulator_get_voltage_sel_regmap,
  34         .list_voltage = regulator_list_voltage_linear,
  35         /* Enabling/disabling the regulator is not yet implemented */
  36 };
  37 
  38 /* Default limits measured in millivolts */
  39 #define SY8106A_MIN_MV          680
  40 #define SY8106A_MAX_MV          1950
  41 #define SY8106A_STEP_MV         10
  42 
  43 static const struct regulator_desc sy8106a_reg = {
  44         .name = "SY8106A",
  45         .id = 0,
  46         .ops = &sy8106a_ops,
  47         .type = REGULATOR_VOLTAGE,
  48         .n_voltages = ((SY8106A_MAX_MV - SY8106A_MIN_MV) / SY8106A_STEP_MV) + 1,
  49         .min_uV = (SY8106A_MIN_MV * 1000),
  50         .uV_step = (SY8106A_STEP_MV * 1000),
  51         .vsel_reg = SY8106A_REG_VOUT1_SEL,
  52         .vsel_mask = SY8106A_REG_VOUT1_SEL_MASK,
  53         /*
  54          * This ramp_delay is a conservative default value which works on
  55          * H3/H5 boards VDD-CPUX situations.
  56          */
  57         .ramp_delay = 200,
  58         .owner = THIS_MODULE,
  59 };
  60 
  61 /*
  62  * I2C driver interface functions
  63  */
  64 static int sy8106a_i2c_probe(struct i2c_client *i2c,
  65                             const struct i2c_device_id *id)
  66 {
  67         struct device *dev = &i2c->dev;
  68         struct regulator_dev *rdev;
  69         struct regulator_config config = { };
  70         struct regmap *regmap;
  71         unsigned int reg, vsel;
  72         u32 fixed_voltage;
  73         int error;
  74 
  75         error = of_property_read_u32(dev->of_node, "silergy,fixed-microvolt",
  76                                      &fixed_voltage);
  77         if (error)
  78                 return error;
  79 
  80         if (fixed_voltage < SY8106A_MIN_MV * 1000 ||
  81             fixed_voltage > SY8106A_MAX_MV * 1000)
  82                 return -EINVAL;
  83 
  84         regmap = devm_regmap_init_i2c(i2c, &sy8106a_regmap_config);
  85         if (IS_ERR(regmap)) {
  86                 error = PTR_ERR(regmap);
  87                 dev_err(dev, "Failed to allocate register map: %d\n", error);
  88                 return error;
  89         }
  90 
  91         config.dev = &i2c->dev;
  92         config.regmap = regmap;
  93 
  94         config.of_node = dev->of_node;
  95         config.init_data = of_get_regulator_init_data(dev, dev->of_node,
  96                                                       &sy8106a_reg);
  97 
  98         if (!config.init_data)
  99                 return -ENOMEM;
 100 
 101         /* Ensure GO_BIT is enabled when probing */
 102         error = regmap_read(regmap, SY8106A_REG_VOUT1_SEL, &reg);
 103         if (error)
 104                 return error;
 105 
 106         if (!(reg & SY8106A_GO_BIT)) {
 107                 vsel = (fixed_voltage / 1000 - SY8106A_MIN_MV) /
 108                        SY8106A_STEP_MV;
 109 
 110                 error = regmap_write(regmap, SY8106A_REG_VOUT1_SEL,
 111                                      vsel | SY8106A_GO_BIT);
 112                 if (error)
 113                         return error;
 114         }
 115 
 116         /* Probe regulator */
 117         rdev = devm_regulator_register(&i2c->dev, &sy8106a_reg, &config);
 118         if (IS_ERR(rdev)) {
 119                 error = PTR_ERR(rdev);
 120                 dev_err(&i2c->dev, "Failed to register SY8106A regulator: %d\n", error);
 121                 return error;
 122         }
 123 
 124         return 0;
 125 }
 126 
 127 static const struct of_device_id sy8106a_i2c_of_match[] = {
 128         { .compatible = "silergy,sy8106a" },
 129         { },
 130 };
 131 MODULE_DEVICE_TABLE(of, sy8106a_i2c_of_match);
 132 
 133 static const struct i2c_device_id sy8106a_i2c_id[] = {
 134         { "sy8106a", 0 },
 135         { },
 136 };
 137 MODULE_DEVICE_TABLE(i2c, sy8106a_i2c_id);
 138 
 139 static struct i2c_driver sy8106a_regulator_driver = {
 140         .driver = {
 141                 .name = "sy8106a",
 142                 .of_match_table = of_match_ptr(sy8106a_i2c_of_match),
 143         },
 144         .probe = sy8106a_i2c_probe,
 145         .id_table = sy8106a_i2c_id,
 146 };
 147 
 148 module_i2c_driver(sy8106a_regulator_driver);
 149 
 150 MODULE_AUTHOR("Ondřej Jirman <megous@megous.com>");
 151 MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
 152 MODULE_DESCRIPTION("Regulator device driver for Silergy SY8106A");
 153 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */