Blame view

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