Commit | Line | Data |
---|---|---|
30e6599d AA |
1 | /* |
2 | * tps65023-regulator.c | |
3 | * | |
4 | * Supports TPS65023 Regulator | |
5 | * | |
6 | * Copyright (C) 2009 Texas Instrument Incorporated - http://www.ti.com/ | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License as | |
10 | * published by the Free Software Foundation version 2. | |
11 | * | |
12 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind, | |
13 | * whether express or implied; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | */ | |
17 | ||
18 | #include <linux/kernel.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/err.h> | |
22 | #include <linux/platform_device.h> | |
23 | #include <linux/regulator/driver.h> | |
24 | #include <linux/regulator/machine.h> | |
25 | #include <linux/i2c.h> | |
5a0e3ad6 | 26 | #include <linux/slab.h> |
90923351 | 27 | #include <linux/regmap.h> |
30e6599d AA |
28 | |
29 | /* Register definitions */ | |
30 | #define TPS65023_REG_VERSION 0 | |
31 | #define TPS65023_REG_PGOODZ 1 | |
32 | #define TPS65023_REG_MASK 2 | |
33 | #define TPS65023_REG_REG_CTRL 3 | |
34 | #define TPS65023_REG_CON_CTRL 4 | |
35 | #define TPS65023_REG_CON_CTRL2 5 | |
36 | #define TPS65023_REG_DEF_CORE 6 | |
37 | #define TPS65023_REG_DEFSLEW 7 | |
38 | #define TPS65023_REG_LDO_CTRL 8 | |
39 | ||
40 | /* PGOODZ bitfields */ | |
41 | #define TPS65023_PGOODZ_PWRFAILZ BIT(7) | |
42 | #define TPS65023_PGOODZ_LOWBATTZ BIT(6) | |
43 | #define TPS65023_PGOODZ_VDCDC1 BIT(5) | |
44 | #define TPS65023_PGOODZ_VDCDC2 BIT(4) | |
45 | #define TPS65023_PGOODZ_VDCDC3 BIT(3) | |
46 | #define TPS65023_PGOODZ_LDO2 BIT(2) | |
47 | #define TPS65023_PGOODZ_LDO1 BIT(1) | |
48 | ||
49 | /* MASK bitfields */ | |
50 | #define TPS65023_MASK_PWRFAILZ BIT(7) | |
51 | #define TPS65023_MASK_LOWBATTZ BIT(6) | |
52 | #define TPS65023_MASK_VDCDC1 BIT(5) | |
53 | #define TPS65023_MASK_VDCDC2 BIT(4) | |
54 | #define TPS65023_MASK_VDCDC3 BIT(3) | |
55 | #define TPS65023_MASK_LDO2 BIT(2) | |
56 | #define TPS65023_MASK_LDO1 BIT(1) | |
57 | ||
58 | /* REG_CTRL bitfields */ | |
59 | #define TPS65023_REG_CTRL_VDCDC1_EN BIT(5) | |
60 | #define TPS65023_REG_CTRL_VDCDC2_EN BIT(4) | |
61 | #define TPS65023_REG_CTRL_VDCDC3_EN BIT(3) | |
62 | #define TPS65023_REG_CTRL_LDO2_EN BIT(2) | |
63 | #define TPS65023_REG_CTRL_LDO1_EN BIT(1) | |
64 | ||
f068ad8c MF |
65 | /* REG_CTRL2 bitfields */ |
66 | #define TPS65023_REG_CTRL2_GO BIT(7) | |
67 | #define TPS65023_REG_CTRL2_CORE_ADJ BIT(6) | |
68 | #define TPS65023_REG_CTRL2_DCDC2 BIT(2) | |
69 | #define TPS65023_REG_CTRL2_DCDC1 BIT(1) | |
70 | #define TPS65023_REG_CTRL2_DCDC3 BIT(0) | |
71 | ||
30e6599d AA |
72 | /* Number of step-down converters available */ |
73 | #define TPS65023_NUM_DCDC 3 | |
74 | /* Number of LDO voltage regulators available */ | |
75 | #define TPS65023_NUM_LDO 2 | |
76 | /* Number of total regulators available */ | |
77 | #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO) | |
78 | ||
79 | /* DCDCs */ | |
80 | #define TPS65023_DCDC_1 0 | |
81 | #define TPS65023_DCDC_2 1 | |
82 | #define TPS65023_DCDC_3 2 | |
83 | /* LDOs */ | |
84 | #define TPS65023_LDO_1 3 | |
85 | #define TPS65023_LDO_2 4 | |
86 | ||
87 | #define TPS65023_MAX_REG_ID TPS65023_LDO_2 | |
88 | ||
89 | /* Supported voltage values for regulators */ | |
ba3bd8a3 AL |
90 | static const unsigned int VCORE_VSEL_table[] = { |
91 | 800000, 825000, 850000, 875000, | |
92 | 900000, 925000, 950000, 975000, | |
93 | 1000000, 1025000, 1050000, 1075000, | |
94 | 1100000, 1125000, 1150000, 1175000, | |
95 | 1200000, 1225000, 1250000, 1275000, | |
96 | 1300000, 1325000, 1350000, 1375000, | |
97 | 1400000, 1425000, 1450000, 1475000, | |
98 | 1500000, 1525000, 1550000, 1600000, | |
99 | }; | |
100 | ||
101 | static const unsigned int DCDC_FIXED_3300000_VSEL_table[] = { | |
102 | 3300000, | |
103 | }; | |
104 | ||
105 | static const unsigned int DCDC_FIXED_1800000_VSEL_table[] = { | |
106 | 1800000, | |
30e6599d AA |
107 | }; |
108 | ||
437afd2a | 109 | /* Supported voltage values for LDO regulators for tps65020 */ |
2040541a | 110 | static const unsigned int TPS65020_LDO_VSEL_table[] = { |
ba3bd8a3 AL |
111 | 1000000, 1050000, 1100000, 1300000, |
112 | 1800000, 2500000, 3000000, 3300000, | |
437afd2a | 113 | }; |
1c3ede05 MF |
114 | |
115 | /* Supported voltage values for LDO regulators | |
116 | * for tps65021 and tps65023 */ | |
ba3bd8a3 AL |
117 | static const unsigned int TPS65023_LDO1_VSEL_table[] = { |
118 | 1000000, 1100000, 1300000, 1800000, | |
119 | 2200000, 2600000, 2800000, 3150000, | |
30e6599d AA |
120 | }; |
121 | ||
ba3bd8a3 AL |
122 | static const unsigned int TPS65023_LDO2_VSEL_table[] = { |
123 | 1050000, 1200000, 1300000, 1800000, | |
124 | 2500000, 2800000, 3000000, 3300000, | |
30e6599d AA |
125 | }; |
126 | ||
30e6599d AA |
127 | /* Regulator specific details */ |
128 | struct tps_info { | |
129 | const char *name; | |
30e6599d | 130 | u8 table_len; |
ba3bd8a3 | 131 | const unsigned int *table; |
30e6599d AA |
132 | }; |
133 | ||
134 | /* PMIC details */ | |
135 | struct tps_pmic { | |
136 | struct regulator_desc desc[TPS65023_NUM_REGULATOR]; | |
30e6599d AA |
137 | struct regulator_dev *rdev[TPS65023_NUM_REGULATOR]; |
138 | const struct tps_info *info[TPS65023_NUM_REGULATOR]; | |
90923351 | 139 | struct regmap *regmap; |
1c3ede05 MF |
140 | u8 core_regulator; |
141 | }; | |
142 | ||
143 | /* Struct passed as driver data */ | |
144 | struct tps_driver_data { | |
145 | const struct tps_info *info; | |
146 | u8 core_regulator; | |
30e6599d AA |
147 | }; |
148 | ||
a133829e | 149 | static int tps65023_dcdc_get_voltage_sel(struct regulator_dev *dev) |
30e6599d AA |
150 | { |
151 | struct tps_pmic *tps = rdev_get_drvdata(dev); | |
46bcb006 | 152 | int dcdc = rdev_get_id(dev); |
30e6599d AA |
153 | |
154 | if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3) | |
155 | return -EINVAL; | |
156 | ||
46bcb006 | 157 | if (dcdc != tps->core_regulator) |
a133829e | 158 | return 0; |
46bcb006 AL |
159 | |
160 | return regulator_get_voltage_sel_regmap(dev); | |
30e6599d AA |
161 | } |
162 | ||
7061873f AL |
163 | static int tps65023_dcdc_set_voltage_sel(struct regulator_dev *dev, |
164 | unsigned selector) | |
30e6599d AA |
165 | { |
166 | struct tps_pmic *tps = rdev_get_drvdata(dev); | |
167 | int dcdc = rdev_get_id(dev); | |
30e6599d | 168 | |
1c3ede05 | 169 | if (dcdc != tps->core_regulator) |
30e6599d | 170 | return -EINVAL; |
30e6599d | 171 | |
46bcb006 | 172 | return regulator_set_voltage_sel_regmap(dev, selector); |
30e6599d AA |
173 | } |
174 | ||
30e6599d AA |
175 | /* Operations permitted on VDCDCx */ |
176 | static struct regulator_ops tps65023_dcdc_ops = { | |
ee7b1914 AL |
177 | .is_enabled = regulator_is_enabled_regmap, |
178 | .enable = regulator_enable_regmap, | |
179 | .disable = regulator_disable_regmap, | |
a133829e | 180 | .get_voltage_sel = tps65023_dcdc_get_voltage_sel, |
7061873f | 181 | .set_voltage_sel = tps65023_dcdc_set_voltage_sel, |
ba3bd8a3 | 182 | .list_voltage = regulator_list_voltage_table, |
c4bbfbd5 | 183 | .map_voltage = regulator_map_voltage_ascend, |
30e6599d AA |
184 | }; |
185 | ||
186 | /* Operations permitted on LDOx */ | |
187 | static struct regulator_ops tps65023_ldo_ops = { | |
ee7b1914 AL |
188 | .is_enabled = regulator_is_enabled_regmap, |
189 | .enable = regulator_enable_regmap, | |
190 | .disable = regulator_disable_regmap, | |
e90a8447 AL |
191 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
192 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
ba3bd8a3 | 193 | .list_voltage = regulator_list_voltage_table, |
c4bbfbd5 | 194 | .map_voltage = regulator_map_voltage_ascend, |
30e6599d AA |
195 | }; |
196 | ||
90923351 MB |
197 | static struct regmap_config tps65023_regmap_config = { |
198 | .reg_bits = 8, | |
199 | .val_bits = 8, | |
200 | }; | |
201 | ||
a5023574 | 202 | static int tps_65023_probe(struct i2c_client *client, |
54d13ab1 | 203 | const struct i2c_device_id *id) |
30e6599d | 204 | { |
1c3ede05 MF |
205 | const struct tps_driver_data *drv_data = (void *)id->driver_data; |
206 | const struct tps_info *info = drv_data->info; | |
c172708d | 207 | struct regulator_config config = { }; |
30e6599d AA |
208 | struct regulator_init_data *init_data; |
209 | struct regulator_dev *rdev; | |
210 | struct tps_pmic *tps; | |
211 | int i; | |
54d13ab1 | 212 | int error; |
30e6599d AA |
213 | |
214 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
215 | return -EIO; | |
216 | ||
217 | /** | |
218 | * init_data points to array of regulator_init structures | |
219 | * coming from the board-evm file. | |
220 | */ | |
dff91d0b | 221 | init_data = dev_get_platdata(&client->dev); |
30e6599d AA |
222 | if (!init_data) |
223 | return -EIO; | |
224 | ||
19a8da21 | 225 | tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL); |
30e6599d AA |
226 | if (!tps) |
227 | return -ENOMEM; | |
228 | ||
19a8da21 | 229 | tps->regmap = devm_regmap_init_i2c(client, &tps65023_regmap_config); |
90923351 MB |
230 | if (IS_ERR(tps->regmap)) { |
231 | error = PTR_ERR(tps->regmap); | |
232 | dev_err(&client->dev, "Failed to allocate register map: %d\n", | |
233 | error); | |
19a8da21 | 234 | return error; |
90923351 | 235 | } |
30e6599d AA |
236 | |
237 | /* common for all regulators */ | |
1c3ede05 | 238 | tps->core_regulator = drv_data->core_regulator; |
30e6599d AA |
239 | |
240 | for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) { | |
241 | /* Store regulator specific information */ | |
242 | tps->info[i] = info; | |
243 | ||
244 | tps->desc[i].name = info->name; | |
77fa44d0 | 245 | tps->desc[i].id = i; |
ba3bd8a3 AL |
246 | tps->desc[i].n_voltages = info->table_len; |
247 | tps->desc[i].volt_table = info->table; | |
30e6599d AA |
248 | tps->desc[i].ops = (i > TPS65023_DCDC_3 ? |
249 | &tps65023_ldo_ops : &tps65023_dcdc_ops); | |
250 | tps->desc[i].type = REGULATOR_VOLTAGE; | |
251 | tps->desc[i].owner = THIS_MODULE; | |
252 | ||
ee7b1914 | 253 | tps->desc[i].enable_reg = TPS65023_REG_REG_CTRL; |
e90a8447 AL |
254 | switch (i) { |
255 | case TPS65023_LDO_1: | |
256 | tps->desc[i].vsel_reg = TPS65023_REG_LDO_CTRL; | |
257 | tps->desc[i].vsel_mask = 0x07; | |
ee7b1914 | 258 | tps->desc[i].enable_mask = 1 << 1; |
e90a8447 AL |
259 | break; |
260 | case TPS65023_LDO_2: | |
261 | tps->desc[i].vsel_reg = TPS65023_REG_LDO_CTRL; | |
262 | tps->desc[i].vsel_mask = 0x70; | |
ee7b1914 | 263 | tps->desc[i].enable_mask = 1 << 2; |
e90a8447 AL |
264 | break; |
265 | default: /* DCDCx */ | |
ee7b1914 AL |
266 | tps->desc[i].enable_mask = |
267 | 1 << (TPS65023_NUM_REGULATOR - i); | |
46bcb006 AL |
268 | tps->desc[i].vsel_reg = TPS65023_REG_DEF_CORE; |
269 | tps->desc[i].vsel_mask = info->table_len - 1; | |
270 | tps->desc[i].apply_reg = TPS65023_REG_CON_CTRL2; | |
271 | tps->desc[i].apply_bit = TPS65023_REG_CTRL2_GO; | |
e90a8447 | 272 | } |
ee7b1914 | 273 | |
c172708d MB |
274 | config.dev = &client->dev; |
275 | config.init_data = init_data; | |
276 | config.driver_data = tps; | |
ee7b1914 | 277 | config.regmap = tps->regmap; |
c172708d | 278 | |
30e6599d | 279 | /* Register the regulators */ |
9c7c9eae SK |
280 | rdev = devm_regulator_register(&client->dev, &tps->desc[i], |
281 | &config); | |
30e6599d AA |
282 | if (IS_ERR(rdev)) { |
283 | dev_err(&client->dev, "failed to register %s\n", | |
284 | id->name); | |
9c7c9eae | 285 | return PTR_ERR(rdev); |
30e6599d AA |
286 | } |
287 | ||
288 | /* Save regulator for cleanup */ | |
289 | tps->rdev[i] = rdev; | |
290 | } | |
291 | ||
292 | i2c_set_clientdata(client, tps); | |
293 | ||
fc999b83 | 294 | /* Enable setting output voltage by I2C */ |
43530b69 | 295 | regmap_update_bits(tps->regmap, TPS65023_REG_CON_CTRL2, |
5ee034e6 JH |
296 | TPS65023_REG_CTRL2_CORE_ADJ, |
297 | TPS65023_REG_CTRL2_CORE_ADJ); | |
fc999b83 | 298 | |
30e6599d | 299 | return 0; |
30e6599d AA |
300 | } |
301 | ||
437afd2a MF |
302 | static const struct tps_info tps65020_regs[] = { |
303 | { | |
304 | .name = "VDCDC1", | |
ba3bd8a3 AL |
305 | .table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table), |
306 | .table = DCDC_FIXED_3300000_VSEL_table, | |
437afd2a MF |
307 | }, |
308 | { | |
309 | .name = "VDCDC2", | |
ba3bd8a3 AL |
310 | .table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table), |
311 | .table = DCDC_FIXED_1800000_VSEL_table, | |
437afd2a MF |
312 | }, |
313 | { | |
314 | .name = "VDCDC3", | |
437afd2a MF |
315 | .table_len = ARRAY_SIZE(VCORE_VSEL_table), |
316 | .table = VCORE_VSEL_table, | |
317 | }, | |
437afd2a MF |
318 | { |
319 | .name = "LDO1", | |
2040541a AL |
320 | .table_len = ARRAY_SIZE(TPS65020_LDO_VSEL_table), |
321 | .table = TPS65020_LDO_VSEL_table, | |
437afd2a MF |
322 | }, |
323 | { | |
324 | .name = "LDO2", | |
2040541a AL |
325 | .table_len = ARRAY_SIZE(TPS65020_LDO_VSEL_table), |
326 | .table = TPS65020_LDO_VSEL_table, | |
437afd2a MF |
327 | }, |
328 | }; | |
329 | ||
1c3ede05 MF |
330 | static const struct tps_info tps65021_regs[] = { |
331 | { | |
332 | .name = "VDCDC1", | |
ba3bd8a3 AL |
333 | .table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table), |
334 | .table = DCDC_FIXED_3300000_VSEL_table, | |
1c3ede05 MF |
335 | }, |
336 | { | |
337 | .name = "VDCDC2", | |
ba3bd8a3 AL |
338 | .table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table), |
339 | .table = DCDC_FIXED_1800000_VSEL_table, | |
1c3ede05 MF |
340 | }, |
341 | { | |
342 | .name = "VDCDC3", | |
1c3ede05 MF |
343 | .table_len = ARRAY_SIZE(VCORE_VSEL_table), |
344 | .table = VCORE_VSEL_table, | |
345 | }, | |
346 | { | |
347 | .name = "LDO1", | |
1c3ede05 MF |
348 | .table_len = ARRAY_SIZE(TPS65023_LDO1_VSEL_table), |
349 | .table = TPS65023_LDO1_VSEL_table, | |
350 | }, | |
351 | { | |
352 | .name = "LDO2", | |
1c3ede05 MF |
353 | .table_len = ARRAY_SIZE(TPS65023_LDO2_VSEL_table), |
354 | .table = TPS65023_LDO2_VSEL_table, | |
355 | }, | |
356 | }; | |
357 | ||
30e6599d AA |
358 | static const struct tps_info tps65023_regs[] = { |
359 | { | |
360 | .name = "VDCDC1", | |
1c3ede05 MF |
361 | .table_len = ARRAY_SIZE(VCORE_VSEL_table), |
362 | .table = VCORE_VSEL_table, | |
30e6599d AA |
363 | }, |
364 | { | |
365 | .name = "VDCDC2", | |
ba3bd8a3 AL |
366 | .table_len = ARRAY_SIZE(DCDC_FIXED_3300000_VSEL_table), |
367 | .table = DCDC_FIXED_3300000_VSEL_table, | |
30e6599d AA |
368 | }, |
369 | { | |
370 | .name = "VDCDC3", | |
ba3bd8a3 AL |
371 | .table_len = ARRAY_SIZE(DCDC_FIXED_1800000_VSEL_table), |
372 | .table = DCDC_FIXED_1800000_VSEL_table, | |
30e6599d AA |
373 | }, |
374 | { | |
375 | .name = "LDO1", | |
1c3ede05 MF |
376 | .table_len = ARRAY_SIZE(TPS65023_LDO1_VSEL_table), |
377 | .table = TPS65023_LDO1_VSEL_table, | |
30e6599d AA |
378 | }, |
379 | { | |
380 | .name = "LDO2", | |
1c3ede05 MF |
381 | .table_len = ARRAY_SIZE(TPS65023_LDO2_VSEL_table), |
382 | .table = TPS65023_LDO2_VSEL_table, | |
30e6599d AA |
383 | }, |
384 | }; | |
385 | ||
437afd2a MF |
386 | static struct tps_driver_data tps65020_drv_data = { |
387 | .info = tps65020_regs, | |
388 | .core_regulator = TPS65023_DCDC_3, | |
389 | }; | |
390 | ||
1c3ede05 | 391 | static struct tps_driver_data tps65021_drv_data = { |
7061873f AL |
392 | .info = tps65021_regs, |
393 | .core_regulator = TPS65023_DCDC_3, | |
1c3ede05 MF |
394 | }; |
395 | ||
396 | static struct tps_driver_data tps65023_drv_data = { | |
7061873f AL |
397 | .info = tps65023_regs, |
398 | .core_regulator = TPS65023_DCDC_1, | |
1c3ede05 MF |
399 | }; |
400 | ||
9e108d33 LG |
401 | static const struct i2c_device_id tps_65023_id[] = { |
402 | {.name = "tps65023", | |
1c3ede05 | 403 | .driver_data = (unsigned long) &tps65023_drv_data}, |
1880a2fc | 404 | {.name = "tps65021", |
1c3ede05 | 405 | .driver_data = (unsigned long) &tps65021_drv_data,}, |
437afd2a MF |
406 | {.name = "tps65020", |
407 | .driver_data = (unsigned long) &tps65020_drv_data}, | |
9e108d33 | 408 | { }, |
30e6599d AA |
409 | }; |
410 | ||
411 | MODULE_DEVICE_TABLE(i2c, tps_65023_id); | |
412 | ||
413 | static struct i2c_driver tps_65023_i2c_driver = { | |
414 | .driver = { | |
415 | .name = "tps65023", | |
416 | .owner = THIS_MODULE, | |
417 | }, | |
418 | .probe = tps_65023_probe, | |
9e108d33 | 419 | .id_table = tps_65023_id, |
30e6599d AA |
420 | }; |
421 | ||
30e6599d AA |
422 | static int __init tps_65023_init(void) |
423 | { | |
424 | return i2c_add_driver(&tps_65023_i2c_driver); | |
425 | } | |
426 | subsys_initcall(tps_65023_init); | |
427 | ||
30e6599d AA |
428 | static void __exit tps_65023_cleanup(void) |
429 | { | |
430 | i2c_del_driver(&tps_65023_i2c_driver); | |
431 | } | |
432 | module_exit(tps_65023_cleanup); | |
433 | ||
434 | MODULE_AUTHOR("Texas Instruments"); | |
435 | MODULE_DESCRIPTION("TPS65023 voltage regulator driver"); | |
9e108d33 | 436 | MODULE_LICENSE("GPL v2"); |