2828#include <linux/init.h>
2929#include <linux/kernel.h>
3030#include <linux/module.h>
31+ #include <linux/of.h>
32+ #include <linux/of_device.h>
3133#include <linux/platform_device.h>
3234#include <linux/regmap.h>
3335#include <linux/regulator/driver.h>
3436#include <linux/regulator/machine.h>
37+ #include <linux/regulator/of_regulator.h>
3538#include <linux/regulator/tps51632-regulator.h>
3639#include <linux/slab.h>
3740
@@ -85,49 +88,8 @@ struct tps51632_chip {
8588 struct regulator_desc desc ;
8689 struct regulator_dev * rdev ;
8790 struct regmap * regmap ;
88- bool enable_pwm_dvfs ;
8991};
9092
91- static int tps51632_dcdc_get_voltage_sel (struct regulator_dev * rdev )
92- {
93- struct tps51632_chip * tps = rdev_get_drvdata (rdev );
94- unsigned int data ;
95- int ret ;
96- unsigned int reg = TPS51632_VOLTAGE_SELECT_REG ;
97- int vsel ;
98-
99- if (tps -> enable_pwm_dvfs )
100- reg = TPS51632_VOLTAGE_BASE_REG ;
101-
102- ret = regmap_read (tps -> regmap , reg , & data );
103- if (ret < 0 ) {
104- dev_err (tps -> dev , "reg read failed, err %d\n" , ret );
105- return ret ;
106- }
107-
108- vsel = data & TPS51632_VOUT_MASK ;
109- return vsel ;
110- }
111-
112- static int tps51632_dcdc_set_voltage_sel (struct regulator_dev * rdev ,
113- unsigned selector )
114- {
115- struct tps51632_chip * tps = rdev_get_drvdata (rdev );
116- int ret ;
117- unsigned int reg = TPS51632_VOLTAGE_SELECT_REG ;
118-
119- if (tps -> enable_pwm_dvfs )
120- reg = TPS51632_VOLTAGE_BASE_REG ;
121-
122- if (selector > TPS51632_MAX_VSEL )
123- return - EINVAL ;
124-
125- ret = regmap_write (tps -> regmap , reg , selector );
126- if (ret < 0 )
127- dev_err (tps -> dev , "reg write failed, err %d\n" , ret );
128- return ret ;
129- }
130-
13193static int tps51632_dcdc_set_ramp_delay (struct regulator_dev * rdev ,
13294 int ramp_delay )
13395{
@@ -144,8 +106,8 @@ static int tps51632_dcdc_set_ramp_delay(struct regulator_dev *rdev,
144106}
145107
146108static struct regulator_ops tps51632_dcdc_ops = {
147- .get_voltage_sel = tps51632_dcdc_get_voltage_sel ,
148- .set_voltage_sel = tps51632_dcdc_set_voltage_sel ,
109+ .get_voltage_sel = regulator_get_voltage_sel_regmap ,
110+ .set_voltage_sel = regulator_set_voltage_sel_regmap ,
149111 .list_voltage = regulator_list_voltage_linear ,
150112 .set_voltage_time_sel = regulator_set_voltage_time_sel ,
151113 .set_ramp_delay = tps51632_dcdc_set_ramp_delay ,
@@ -162,7 +124,6 @@ static int tps51632_init_dcdc(struct tps51632_chip *tps,
162124 goto skip_pwm_config ;
163125
164126 control |= TPS51632_DVFS_PWMEN ;
165- tps -> enable_pwm_dvfs = pdata -> enable_pwm_dvfs ;
166127 vsel = TPS51632_VOLT_VSEL (pdata -> base_voltage_uV );
167128 ret = regmap_write (tps -> regmap , TPS51632_VOLTAGE_BASE_REG , vsel );
168129 if (ret < 0 ) {
@@ -205,22 +166,96 @@ static int tps51632_init_dcdc(struct tps51632_chip *tps,
205166 return ret ;
206167}
207168
208- static bool rd_wr_reg (struct device * dev , unsigned int reg )
169+ static bool is_volatile_reg (struct device * dev , unsigned int reg )
170+ {
171+ switch (reg ) {
172+ case TPS51632_OFFSET_REG :
173+ case TPS51632_FAULT_REG :
174+ case TPS51632_IMON_REG :
175+ return true;
176+ default :
177+ return false;
178+ }
179+ }
180+
181+ static bool is_read_reg (struct device * dev , unsigned int reg )
209182{
210- if ((reg >= 0x8 ) && (reg <= 0x10 ))
183+ switch (reg ) {
184+ case 0x08 ... 0x0F :
211185 return false;
212- return true;
186+ default :
187+ return true;
188+ }
189+ }
190+
191+ static bool is_write_reg (struct device * dev , unsigned int reg )
192+ {
193+ switch (reg ) {
194+ case TPS51632_VOLTAGE_SELECT_REG :
195+ case TPS51632_VOLTAGE_BASE_REG :
196+ case TPS51632_VMAX_REG :
197+ case TPS51632_DVFS_CONTROL_REG :
198+ case TPS51632_POWER_STATE_REG :
199+ case TPS51632_SLEW_REGS :
200+ return true;
201+ default :
202+ return false;
203+ }
213204}
214205
215206static const struct regmap_config tps51632_regmap_config = {
216207 .reg_bits = 8 ,
217208 .val_bits = 8 ,
218- .writeable_reg = rd_wr_reg ,
219- .readable_reg = rd_wr_reg ,
209+ .writeable_reg = is_write_reg ,
210+ .readable_reg = is_read_reg ,
211+ .volatile_reg = is_volatile_reg ,
220212 .max_register = TPS51632_MAX_REG - 1 ,
221213 .cache_type = REGCACHE_RBTREE ,
222214};
223215
216+ #if defined(CONFIG_OF )
217+ static const struct of_device_id tps51632_of_match [] = {
218+ { .compatible = "ti,tps51632" ,},
219+ {},
220+ };
221+ MODULE_DEVICE_TABLE (of , tps51632_of_match );
222+
223+ static struct tps51632_regulator_platform_data *
224+ of_get_tps51632_platform_data (struct device * dev )
225+ {
226+ struct tps51632_regulator_platform_data * pdata ;
227+ struct device_node * np = dev -> of_node ;
228+
229+ pdata = devm_kzalloc (dev , sizeof (* pdata ), GFP_KERNEL );
230+ if (!pdata ) {
231+ dev_err (dev , "Memory alloc failed for platform data\n" );
232+ return NULL ;
233+ }
234+
235+ pdata -> reg_init_data = of_get_regulator_init_data (dev , dev -> of_node );
236+ if (!pdata -> reg_init_data ) {
237+ dev_err (dev , "Not able to get OF regulator init data\n" );
238+ return NULL ;
239+ }
240+
241+ pdata -> enable_pwm_dvfs =
242+ of_property_read_bool (np , "ti,enable-pwm-dvfs" );
243+ pdata -> dvfs_step_20mV = of_property_read_bool (np , "ti,dvfs-step-20mV" );
244+
245+ pdata -> base_voltage_uV = pdata -> reg_init_data -> constraints .min_uV ? :
246+ TPS51632_MIN_VOLATGE ;
247+ pdata -> max_voltage_uV = pdata -> reg_init_data -> constraints .max_uV ? :
248+ TPS51632_MAX_VOLATGE ;
249+ return pdata ;
250+ }
251+ #else
252+ static struct tps51632_regulator_platform_data *
253+ of_get_tps51632_platform_data (struct device * dev )
254+ {
255+ return NULL ;
256+ }
257+ #endif
258+
224259static int tps51632_probe (struct i2c_client * client ,
225260 const struct i2c_device_id * id )
226261{
@@ -230,7 +265,19 @@ static int tps51632_probe(struct i2c_client *client,
230265 int ret ;
231266 struct regulator_config config = { };
232267
268+ if (client -> dev .of_node ) {
269+ const struct of_device_id * match ;
270+ match = of_match_device (of_match_ptr (tps51632_of_match ),
271+ & client -> dev );
272+ if (!match ) {
273+ dev_err (& client -> dev , "Error: No device match found\n" );
274+ return - ENODEV ;
275+ }
276+ }
277+
233278 pdata = client -> dev .platform_data ;
279+ if (!pdata && client -> dev .of_node )
280+ pdata = of_get_tps51632_platform_data (& client -> dev );
234281 if (!pdata ) {
235282 dev_err (& client -> dev , "No Platform data\n" );
236283 return - EINVAL ;
@@ -269,6 +316,12 @@ static int tps51632_probe(struct i2c_client *client,
269316 tps -> desc .type = REGULATOR_VOLTAGE ;
270317 tps -> desc .owner = THIS_MODULE ;
271318
319+ if (pdata -> enable_pwm_dvfs )
320+ tps -> desc .vsel_reg = TPS51632_VOLTAGE_BASE_REG ;
321+ else
322+ tps -> desc .vsel_reg = TPS51632_VOLTAGE_SELECT_REG ;
323+ tps -> desc .vsel_mask = TPS51632_VOUT_MASK ;
324+
272325 tps -> regmap = devm_regmap_init_i2c (client , & tps51632_regmap_config );
273326 if (IS_ERR (tps -> regmap )) {
274327 ret = PTR_ERR (tps -> regmap );
@@ -319,6 +372,7 @@ static struct i2c_driver tps51632_i2c_driver = {
319372 .driver = {
320373 .name = "tps51632" ,
321374 .owner = THIS_MODULE ,
375+ .of_match_table = of_match_ptr (tps51632_of_match ),
322376 },
323377 .probe = tps51632_probe ,
324378 .remove = tps51632_remove ,
0 commit comments